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. |
Nicolas Kaiser | 9611c18 | 2010-10-06 14:23:22 +0200 | [diff] [blame] | 10 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11 | * |
| 12 | * Authors: |
| 13 | * Yaniv Kamay <yaniv@qumranet.com> |
| 14 | * Avi Kivity <avi@qumranet.com> |
| 15 | * |
| 16 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 17 | * the COPYING file in the top-level directory. |
| 18 | * |
| 19 | */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 20 | |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 21 | #include "irq.h" |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 22 | #include "mmu.h" |
Avi Kivity | 836a1b3 | 2010-01-21 15:31:49 +0200 | [diff] [blame] | 23 | #include "x86.h" |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 24 | #include "kvm_cache_regs.h" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 25 | |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 26 | #include <linux/kvm_host.h> |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 27 | #include <linux/types.h> |
| 28 | #include <linux/string.h> |
| 29 | #include <linux/mm.h> |
| 30 | #include <linux/highmem.h> |
| 31 | #include <linux/module.h> |
Izik Eidus | 448353c | 2007-11-26 14:08:14 +0200 | [diff] [blame] | 32 | #include <linux/swap.h> |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 33 | #include <linux/hugetlb.h> |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 34 | #include <linux/compiler.h> |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 35 | #include <linux/srcu.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 36 | #include <linux/slab.h> |
Huang Ying | bf99815 | 2010-05-31 14:28:19 +0800 | [diff] [blame] | 37 | #include <linux/uaccess.h> |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 38 | |
| 39 | #include <asm/page.h> |
| 40 | #include <asm/cmpxchg.h> |
Avi Kivity | 4e54237 | 2007-11-21 14:08:40 +0200 | [diff] [blame] | 41 | #include <asm/io.h> |
Eduardo Habkost | 13673a9 | 2008-11-17 19:03:13 -0200 | [diff] [blame] | 42 | #include <asm/vmx.h> |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 43 | |
Joerg Roedel | 1855267 | 2008-02-07 13:47:41 +0100 | [diff] [blame] | 44 | /* |
| 45 | * When setting this variable to true it enables Two-Dimensional-Paging |
| 46 | * where the hardware walks 2 page tables: |
| 47 | * 1. the guest-virtual to guest-physical |
| 48 | * 2. while doing 1. it walks guest-physical to host-physical |
| 49 | * If the hardware supports that we don't need to do shadow paging. |
| 50 | */ |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 51 | bool tdp_enabled = false; |
Joerg Roedel | 1855267 | 2008-02-07 13:47:41 +0100 | [diff] [blame] | 52 | |
Xiao Guangrong | 8b1fe17 | 2010-08-30 18:22:53 +0800 | [diff] [blame] | 53 | enum { |
| 54 | AUDIT_PRE_PAGE_FAULT, |
| 55 | AUDIT_POST_PAGE_FAULT, |
| 56 | AUDIT_PRE_PTE_WRITE, |
Xiao Guangrong | 6903074 | 2010-09-27 18:09:29 +0800 | [diff] [blame] | 57 | AUDIT_POST_PTE_WRITE, |
| 58 | AUDIT_PRE_SYNC, |
| 59 | AUDIT_POST_SYNC |
Xiao Guangrong | 8b1fe17 | 2010-08-30 18:22:53 +0800 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | char *audit_point_name[] = { |
| 63 | "pre page fault", |
| 64 | "post page fault", |
| 65 | "pre pte write", |
Xiao Guangrong | 6903074 | 2010-09-27 18:09:29 +0800 | [diff] [blame] | 66 | "post pte write", |
| 67 | "pre sync", |
| 68 | "post sync" |
Xiao Guangrong | 8b1fe17 | 2010-08-30 18:22:53 +0800 | [diff] [blame] | 69 | }; |
| 70 | |
Xiao Guangrong | 0375f7f | 2011-11-28 20:41:00 +0800 | [diff] [blame^] | 71 | #ifdef CONFIG_KVM_MMU_AUDIT |
| 72 | static void kvm_mmu_audit(struct kvm_vcpu *vcpu, int point); |
| 73 | #else |
| 74 | static void kvm_mmu_audit(struct kvm_vcpu *vcpu, int point) { } |
| 75 | #endif |
| 76 | |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 77 | #undef MMU_DEBUG |
| 78 | |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 79 | #ifdef MMU_DEBUG |
| 80 | |
| 81 | #define pgprintk(x...) do { if (dbg) printk(x); } while (0) |
| 82 | #define rmap_printk(x...) do { if (dbg) printk(x); } while (0) |
| 83 | |
| 84 | #else |
| 85 | |
| 86 | #define pgprintk(x...) do { } while (0) |
| 87 | #define rmap_printk(x...) do { } while (0) |
| 88 | |
| 89 | #endif |
| 90 | |
Xiao Guangrong | 8b1fe17 | 2010-08-30 18:22:53 +0800 | [diff] [blame] | 91 | #ifdef MMU_DEBUG |
Avi Kivity | 6ada8cc | 2008-06-22 16:45:24 +0300 | [diff] [blame] | 92 | static int dbg = 0; |
| 93 | module_param(dbg, bool, 0644); |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 94 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 95 | |
Marcelo Tosatti | 582801a | 2008-09-23 13:18:41 -0300 | [diff] [blame] | 96 | static int oos_shadow = 1; |
| 97 | module_param(oos_shadow, bool, 0644); |
| 98 | |
Yaozu Dong | d6c69ee | 2007-04-25 14:17:25 +0800 | [diff] [blame] | 99 | #ifndef MMU_DEBUG |
| 100 | #define ASSERT(x) do { } while (0) |
| 101 | #else |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 102 | #define ASSERT(x) \ |
| 103 | if (!(x)) { \ |
| 104 | printk(KERN_WARNING "assertion failed %s:%d: %s\n", \ |
| 105 | __FILE__, __LINE__, #x); \ |
| 106 | } |
Yaozu Dong | d6c69ee | 2007-04-25 14:17:25 +0800 | [diff] [blame] | 107 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 108 | |
Xiao Guangrong | 957ed9e | 2010-08-22 19:12:48 +0800 | [diff] [blame] | 109 | #define PTE_PREFETCH_NUM 8 |
| 110 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 111 | #define PT_FIRST_AVAIL_BITS_SHIFT 9 |
| 112 | #define PT64_SECOND_AVAIL_BITS_SHIFT 52 |
| 113 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 114 | #define PT64_LEVEL_BITS 9 |
| 115 | |
| 116 | #define PT64_LEVEL_SHIFT(level) \ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 117 | (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 118 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 119 | #define PT64_INDEX(address, level)\ |
| 120 | (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1)) |
| 121 | |
| 122 | |
| 123 | #define PT32_LEVEL_BITS 10 |
| 124 | |
| 125 | #define PT32_LEVEL_SHIFT(level) \ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 126 | (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 127 | |
Joerg Roedel | e04da98 | 2009-07-27 16:30:45 +0200 | [diff] [blame] | 128 | #define PT32_LVL_OFFSET_MASK(level) \ |
| 129 | (PT32_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \ |
| 130 | * PT32_LEVEL_BITS))) - 1)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 131 | |
| 132 | #define PT32_INDEX(address, level)\ |
| 133 | (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1)) |
| 134 | |
| 135 | |
Avi Kivity | 27aba76 | 2007-03-09 13:04:31 +0200 | [diff] [blame] | 136 | #define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 137 | #define PT64_DIR_BASE_ADDR_MASK \ |
| 138 | (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1)) |
Joerg Roedel | e04da98 | 2009-07-27 16:30:45 +0200 | [diff] [blame] | 139 | #define PT64_LVL_ADDR_MASK(level) \ |
| 140 | (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \ |
| 141 | * PT64_LEVEL_BITS))) - 1)) |
| 142 | #define PT64_LVL_OFFSET_MASK(level) \ |
| 143 | (PT64_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \ |
| 144 | * PT64_LEVEL_BITS))) - 1)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 145 | |
| 146 | #define PT32_BASE_ADDR_MASK PAGE_MASK |
| 147 | #define PT32_DIR_BASE_ADDR_MASK \ |
| 148 | (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1)) |
Joerg Roedel | e04da98 | 2009-07-27 16:30:45 +0200 | [diff] [blame] | 149 | #define PT32_LVL_ADDR_MASK(level) \ |
| 150 | (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \ |
| 151 | * PT32_LEVEL_BITS))) - 1)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 152 | |
Avi Kivity | 79539ce | 2007-11-21 02:06:21 +0200 | [diff] [blame] | 153 | #define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \ |
| 154 | | PT64_NX_MASK) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 155 | |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 156 | #define PTE_LIST_EXT 4 |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 157 | |
Avi Kivity | fe135d2 | 2007-12-09 16:15:46 +0200 | [diff] [blame] | 158 | #define ACC_EXEC_MASK 1 |
| 159 | #define ACC_WRITE_MASK PT_WRITABLE_MASK |
| 160 | #define ACC_USER_MASK PT_USER_MASK |
| 161 | #define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK) |
| 162 | |
Avi Kivity | 90bb6fc | 2009-12-31 12:10:16 +0200 | [diff] [blame] | 163 | #include <trace/events/kvm.h> |
| 164 | |
Avi Kivity | 0742017 | 2009-07-06 12:21:32 +0300 | [diff] [blame] | 165 | #define CREATE_TRACE_POINTS |
| 166 | #include "mmutrace.h" |
| 167 | |
Izik Eidus | 1403283 | 2009-09-23 21:47:17 +0300 | [diff] [blame] | 168 | #define SPTE_HOST_WRITEABLE (1ULL << PT_FIRST_AVAIL_BITS_SHIFT) |
| 169 | |
Avi Kivity | 135f8c2 | 2008-08-21 17:49:56 +0300 | [diff] [blame] | 170 | #define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level) |
| 171 | |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 172 | struct pte_list_desc { |
| 173 | u64 *sptes[PTE_LIST_EXT]; |
| 174 | struct pte_list_desc *more; |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 175 | }; |
| 176 | |
Avi Kivity | 2d11123 | 2008-12-25 14:39:47 +0200 | [diff] [blame] | 177 | struct kvm_shadow_walk_iterator { |
| 178 | u64 addr; |
| 179 | hpa_t shadow_addr; |
Avi Kivity | 2d11123 | 2008-12-25 14:39:47 +0200 | [diff] [blame] | 180 | u64 *sptep; |
Xiao Guangrong | dd3bfd5 | 2011-07-12 03:32:54 +0800 | [diff] [blame] | 181 | int level; |
Avi Kivity | 2d11123 | 2008-12-25 14:39:47 +0200 | [diff] [blame] | 182 | unsigned index; |
| 183 | }; |
| 184 | |
| 185 | #define for_each_shadow_entry(_vcpu, _addr, _walker) \ |
| 186 | for (shadow_walk_init(&(_walker), _vcpu, _addr); \ |
| 187 | shadow_walk_okay(&(_walker)); \ |
| 188 | shadow_walk_next(&(_walker))) |
| 189 | |
Xiao Guangrong | c2a2ac2 | 2011-07-12 03:32:13 +0800 | [diff] [blame] | 190 | #define for_each_shadow_entry_lockless(_vcpu, _addr, _walker, spte) \ |
| 191 | for (shadow_walk_init(&(_walker), _vcpu, _addr); \ |
| 192 | shadow_walk_okay(&(_walker)) && \ |
| 193 | ({ spte = mmu_spte_get_lockless(_walker.sptep); 1; }); \ |
| 194 | __shadow_walk_next(&(_walker), spte)) |
| 195 | |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 196 | static struct kmem_cache *pte_list_desc_cache; |
Avi Kivity | d3d25b0 | 2007-05-30 12:34:53 +0300 | [diff] [blame] | 197 | static struct kmem_cache *mmu_page_header_cache; |
Dave Hansen | 45221ab | 2010-08-19 18:11:37 -0700 | [diff] [blame] | 198 | static struct percpu_counter kvm_total_used_mmu_pages; |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 199 | |
Sheng Yang | 7b52345 | 2008-04-25 21:13:50 +0800 | [diff] [blame] | 200 | static u64 __read_mostly shadow_nx_mask; |
| 201 | static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */ |
| 202 | static u64 __read_mostly shadow_user_mask; |
| 203 | static u64 __read_mostly shadow_accessed_mask; |
| 204 | static u64 __read_mostly shadow_dirty_mask; |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 205 | static u64 __read_mostly shadow_mmio_mask; |
| 206 | |
| 207 | static void mmu_spte_set(u64 *sptep, u64 spte); |
| 208 | |
| 209 | void kvm_mmu_set_mmio_spte_mask(u64 mmio_mask) |
| 210 | { |
| 211 | shadow_mmio_mask = mmio_mask; |
| 212 | } |
| 213 | EXPORT_SYMBOL_GPL(kvm_mmu_set_mmio_spte_mask); |
| 214 | |
| 215 | static void mark_mmio_spte(u64 *sptep, u64 gfn, unsigned access) |
| 216 | { |
| 217 | access &= ACC_WRITE_MASK | ACC_USER_MASK; |
| 218 | |
Xiao Guangrong | 4f02264 | 2011-07-12 03:34:24 +0800 | [diff] [blame] | 219 | trace_mark_mmio_spte(sptep, gfn, access); |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 220 | mmu_spte_set(sptep, shadow_mmio_mask | access | gfn << PAGE_SHIFT); |
| 221 | } |
| 222 | |
| 223 | static bool is_mmio_spte(u64 spte) |
| 224 | { |
| 225 | return (spte & shadow_mmio_mask) == shadow_mmio_mask; |
| 226 | } |
| 227 | |
| 228 | static gfn_t get_mmio_spte_gfn(u64 spte) |
| 229 | { |
| 230 | return (spte & ~shadow_mmio_mask) >> PAGE_SHIFT; |
| 231 | } |
| 232 | |
| 233 | static unsigned get_mmio_spte_access(u64 spte) |
| 234 | { |
| 235 | return (spte & ~shadow_mmio_mask) & ~PAGE_MASK; |
| 236 | } |
| 237 | |
| 238 | static bool set_mmio_spte(u64 *sptep, gfn_t gfn, pfn_t pfn, unsigned access) |
| 239 | { |
| 240 | if (unlikely(is_noslot_pfn(pfn))) { |
| 241 | mark_mmio_spte(sptep, gfn, access); |
| 242 | return true; |
| 243 | } |
| 244 | |
| 245 | return false; |
| 246 | } |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 247 | |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 248 | static inline u64 rsvd_bits(int s, int e) |
| 249 | { |
| 250 | return ((1ULL << (e - s + 1)) - 1) << s; |
| 251 | } |
| 252 | |
Sheng Yang | 7b52345 | 2008-04-25 21:13:50 +0800 | [diff] [blame] | 253 | void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask, |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 254 | u64 dirty_mask, u64 nx_mask, u64 x_mask) |
Sheng Yang | 7b52345 | 2008-04-25 21:13:50 +0800 | [diff] [blame] | 255 | { |
| 256 | shadow_user_mask = user_mask; |
| 257 | shadow_accessed_mask = accessed_mask; |
| 258 | shadow_dirty_mask = dirty_mask; |
| 259 | shadow_nx_mask = nx_mask; |
| 260 | shadow_x_mask = x_mask; |
| 261 | } |
| 262 | EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes); |
| 263 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 264 | static int is_cpuid_PSE36(void) |
| 265 | { |
| 266 | return 1; |
| 267 | } |
| 268 | |
Avi Kivity | 73b1087 | 2007-01-26 00:56:41 -0800 | [diff] [blame] | 269 | static int is_nx(struct kvm_vcpu *vcpu) |
| 270 | { |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 271 | return vcpu->arch.efer & EFER_NX; |
Avi Kivity | 73b1087 | 2007-01-26 00:56:41 -0800 | [diff] [blame] | 272 | } |
| 273 | |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 274 | static int is_shadow_present_pte(u64 pte) |
| 275 | { |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 276 | return pte & PT_PRESENT_MASK && !is_mmio_spte(pte); |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 277 | } |
| 278 | |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 279 | static int is_large_pte(u64 pte) |
| 280 | { |
| 281 | return pte & PT_PAGE_SIZE_MASK; |
| 282 | } |
| 283 | |
Avi Kivity | 43a3795 | 2009-06-10 14:12:05 +0300 | [diff] [blame] | 284 | static int is_dirty_gpte(unsigned long pte) |
Avi Kivity | e3c5e7ec | 2007-10-11 12:32:30 +0200 | [diff] [blame] | 285 | { |
Avi Kivity | 439e218 | 2009-06-10 12:56:54 +0300 | [diff] [blame] | 286 | return pte & PT_DIRTY_MASK; |
Avi Kivity | e3c5e7ec | 2007-10-11 12:32:30 +0200 | [diff] [blame] | 287 | } |
| 288 | |
Avi Kivity | 43a3795 | 2009-06-10 14:12:05 +0300 | [diff] [blame] | 289 | static int is_rmap_spte(u64 pte) |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 290 | { |
Avi Kivity | 4b1a80f | 2008-03-23 12:18:19 +0200 | [diff] [blame] | 291 | return is_shadow_present_pte(pte); |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 292 | } |
| 293 | |
Marcelo Tosatti | 776e663 | 2009-06-10 12:27:03 -0300 | [diff] [blame] | 294 | static int is_last_spte(u64 pte, int level) |
| 295 | { |
| 296 | if (level == PT_PAGE_TABLE_LEVEL) |
| 297 | return 1; |
Joerg Roedel | 852e3c1 | 2009-07-27 16:30:44 +0200 | [diff] [blame] | 298 | if (is_large_pte(pte)) |
Marcelo Tosatti | 776e663 | 2009-06-10 12:27:03 -0300 | [diff] [blame] | 299 | return 1; |
| 300 | return 0; |
| 301 | } |
| 302 | |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 303 | static pfn_t spte_to_pfn(u64 pte) |
Avi Kivity | 0b49ea8 | 2008-03-23 15:06:23 +0200 | [diff] [blame] | 304 | { |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 305 | return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT; |
Avi Kivity | 0b49ea8 | 2008-03-23 15:06:23 +0200 | [diff] [blame] | 306 | } |
| 307 | |
Avi Kivity | da928521 | 2007-11-21 13:54:47 +0200 | [diff] [blame] | 308 | static gfn_t pse36_gfn_delta(u32 gpte) |
| 309 | { |
| 310 | int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT; |
| 311 | |
| 312 | return (gpte & PT32_DIR_PSE36_MASK) << shift; |
| 313 | } |
| 314 | |
Xiao Guangrong | 603e065 | 2011-07-12 03:31:28 +0800 | [diff] [blame] | 315 | #ifdef CONFIG_X86_64 |
Avi Kivity | d555c33 | 2009-06-10 14:24:23 +0300 | [diff] [blame] | 316 | static void __set_spte(u64 *sptep, u64 spte) |
Avi Kivity | e663ee6 | 2007-05-31 15:46:04 +0300 | [diff] [blame] | 317 | { |
Xiao Guangrong | 603e065 | 2011-07-12 03:31:28 +0800 | [diff] [blame] | 318 | *sptep = spte; |
Avi Kivity | e663ee6 | 2007-05-31 15:46:04 +0300 | [diff] [blame] | 319 | } |
| 320 | |
Xiao Guangrong | 603e065 | 2011-07-12 03:31:28 +0800 | [diff] [blame] | 321 | static void __update_clear_spte_fast(u64 *sptep, u64 spte) |
Avi Kivity | a9221dd | 2010-06-06 14:48:06 +0300 | [diff] [blame] | 322 | { |
Xiao Guangrong | 603e065 | 2011-07-12 03:31:28 +0800 | [diff] [blame] | 323 | *sptep = spte; |
Avi Kivity | a9221dd | 2010-06-06 14:48:06 +0300 | [diff] [blame] | 324 | } |
| 325 | |
Xiao Guangrong | 603e065 | 2011-07-12 03:31:28 +0800 | [diff] [blame] | 326 | static u64 __update_clear_spte_slow(u64 *sptep, u64 spte) |
| 327 | { |
| 328 | return xchg(sptep, spte); |
| 329 | } |
Xiao Guangrong | c2a2ac2 | 2011-07-12 03:32:13 +0800 | [diff] [blame] | 330 | |
| 331 | static u64 __get_spte_lockless(u64 *sptep) |
| 332 | { |
| 333 | return ACCESS_ONCE(*sptep); |
| 334 | } |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 335 | |
| 336 | static bool __check_direct_spte_mmio_pf(u64 spte) |
| 337 | { |
| 338 | /* It is valid if the spte is zapped. */ |
| 339 | return spte == 0ull; |
| 340 | } |
Xiao Guangrong | 603e065 | 2011-07-12 03:31:28 +0800 | [diff] [blame] | 341 | #else |
| 342 | union split_spte { |
| 343 | struct { |
| 344 | u32 spte_low; |
| 345 | u32 spte_high; |
| 346 | }; |
| 347 | u64 spte; |
| 348 | }; |
| 349 | |
Xiao Guangrong | c2a2ac2 | 2011-07-12 03:32:13 +0800 | [diff] [blame] | 350 | static void count_spte_clear(u64 *sptep, u64 spte) |
| 351 | { |
| 352 | struct kvm_mmu_page *sp = page_header(__pa(sptep)); |
| 353 | |
| 354 | if (is_shadow_present_pte(spte)) |
| 355 | return; |
| 356 | |
| 357 | /* Ensure the spte is completely set before we increase the count */ |
| 358 | smp_wmb(); |
| 359 | sp->clear_spte_count++; |
| 360 | } |
| 361 | |
Xiao Guangrong | 603e065 | 2011-07-12 03:31:28 +0800 | [diff] [blame] | 362 | static void __set_spte(u64 *sptep, u64 spte) |
| 363 | { |
| 364 | union split_spte *ssptep, sspte; |
| 365 | |
| 366 | ssptep = (union split_spte *)sptep; |
| 367 | sspte = (union split_spte)spte; |
| 368 | |
| 369 | ssptep->spte_high = sspte.spte_high; |
| 370 | |
| 371 | /* |
| 372 | * If we map the spte from nonpresent to present, We should store |
| 373 | * the high bits firstly, then set present bit, so cpu can not |
| 374 | * fetch this spte while we are setting the spte. |
| 375 | */ |
| 376 | smp_wmb(); |
| 377 | |
| 378 | ssptep->spte_low = sspte.spte_low; |
| 379 | } |
| 380 | |
| 381 | static void __update_clear_spte_fast(u64 *sptep, u64 spte) |
| 382 | { |
| 383 | union split_spte *ssptep, sspte; |
| 384 | |
| 385 | ssptep = (union split_spte *)sptep; |
| 386 | sspte = (union split_spte)spte; |
| 387 | |
| 388 | ssptep->spte_low = sspte.spte_low; |
| 389 | |
| 390 | /* |
| 391 | * If we map the spte from present to nonpresent, we should clear |
| 392 | * present bit firstly to avoid vcpu fetch the old high bits. |
| 393 | */ |
| 394 | smp_wmb(); |
| 395 | |
| 396 | ssptep->spte_high = sspte.spte_high; |
Xiao Guangrong | c2a2ac2 | 2011-07-12 03:32:13 +0800 | [diff] [blame] | 397 | count_spte_clear(sptep, spte); |
Xiao Guangrong | 603e065 | 2011-07-12 03:31:28 +0800 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | static u64 __update_clear_spte_slow(u64 *sptep, u64 spte) |
| 401 | { |
| 402 | union split_spte *ssptep, sspte, orig; |
| 403 | |
| 404 | ssptep = (union split_spte *)sptep; |
| 405 | sspte = (union split_spte)spte; |
| 406 | |
| 407 | /* xchg acts as a barrier before the setting of the high bits */ |
| 408 | orig.spte_low = xchg(&ssptep->spte_low, sspte.spte_low); |
Zhao Jin | 41bc318 | 2011-09-19 12:19:51 +0800 | [diff] [blame] | 409 | orig.spte_high = ssptep->spte_high; |
| 410 | ssptep->spte_high = sspte.spte_high; |
Xiao Guangrong | c2a2ac2 | 2011-07-12 03:32:13 +0800 | [diff] [blame] | 411 | count_spte_clear(sptep, spte); |
Xiao Guangrong | 603e065 | 2011-07-12 03:31:28 +0800 | [diff] [blame] | 412 | |
| 413 | return orig.spte; |
| 414 | } |
Xiao Guangrong | c2a2ac2 | 2011-07-12 03:32:13 +0800 | [diff] [blame] | 415 | |
| 416 | /* |
| 417 | * The idea using the light way get the spte on x86_32 guest is from |
| 418 | * gup_get_pte(arch/x86/mm/gup.c). |
| 419 | * The difference is we can not catch the spte tlb flush if we leave |
| 420 | * guest mode, so we emulate it by increase clear_spte_count when spte |
| 421 | * is cleared. |
| 422 | */ |
| 423 | static u64 __get_spte_lockless(u64 *sptep) |
| 424 | { |
| 425 | struct kvm_mmu_page *sp = page_header(__pa(sptep)); |
| 426 | union split_spte spte, *orig = (union split_spte *)sptep; |
| 427 | int count; |
| 428 | |
| 429 | retry: |
| 430 | count = sp->clear_spte_count; |
| 431 | smp_rmb(); |
| 432 | |
| 433 | spte.spte_low = orig->spte_low; |
| 434 | smp_rmb(); |
| 435 | |
| 436 | spte.spte_high = orig->spte_high; |
| 437 | smp_rmb(); |
| 438 | |
| 439 | if (unlikely(spte.spte_low != orig->spte_low || |
| 440 | count != sp->clear_spte_count)) |
| 441 | goto retry; |
| 442 | |
| 443 | return spte.spte; |
| 444 | } |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 445 | |
| 446 | static bool __check_direct_spte_mmio_pf(u64 spte) |
| 447 | { |
| 448 | union split_spte sspte = (union split_spte)spte; |
| 449 | u32 high_mmio_mask = shadow_mmio_mask >> 32; |
| 450 | |
| 451 | /* It is valid if the spte is zapped. */ |
| 452 | if (spte == 0ull) |
| 453 | return true; |
| 454 | |
| 455 | /* It is valid if the spte is being zapped. */ |
| 456 | if (sspte.spte_low == 0ull && |
| 457 | (sspte.spte_high & high_mmio_mask) == high_mmio_mask) |
| 458 | return true; |
| 459 | |
| 460 | return false; |
| 461 | } |
Xiao Guangrong | 603e065 | 2011-07-12 03:31:28 +0800 | [diff] [blame] | 462 | #endif |
| 463 | |
Xiao Guangrong | 8672b72 | 2010-08-02 16:14:04 +0800 | [diff] [blame] | 464 | static bool spte_has_volatile_bits(u64 spte) |
| 465 | { |
| 466 | if (!shadow_accessed_mask) |
| 467 | return false; |
| 468 | |
| 469 | if (!is_shadow_present_pte(spte)) |
| 470 | return false; |
| 471 | |
Xiao Guangrong | 4132779 | 2010-08-02 16:15:08 +0800 | [diff] [blame] | 472 | if ((spte & shadow_accessed_mask) && |
| 473 | (!is_writable_pte(spte) || (spte & shadow_dirty_mask))) |
Xiao Guangrong | 8672b72 | 2010-08-02 16:14:04 +0800 | [diff] [blame] | 474 | return false; |
| 475 | |
| 476 | return true; |
| 477 | } |
| 478 | |
Xiao Guangrong | 4132779 | 2010-08-02 16:15:08 +0800 | [diff] [blame] | 479 | static bool spte_is_bit_cleared(u64 old_spte, u64 new_spte, u64 bit_mask) |
| 480 | { |
| 481 | return (old_spte & bit_mask) && !(new_spte & bit_mask); |
| 482 | } |
| 483 | |
Xiao Guangrong | 1df9f2d | 2011-07-12 03:30:35 +0800 | [diff] [blame] | 484 | /* Rules for using mmu_spte_set: |
| 485 | * Set the sptep from nonpresent to present. |
| 486 | * Note: the sptep being assigned *must* be either not present |
| 487 | * or in a state where the hardware will not attempt to update |
| 488 | * the spte. |
| 489 | */ |
| 490 | static void mmu_spte_set(u64 *sptep, u64 new_spte) |
| 491 | { |
| 492 | WARN_ON(is_shadow_present_pte(*sptep)); |
| 493 | __set_spte(sptep, new_spte); |
| 494 | } |
| 495 | |
| 496 | /* Rules for using mmu_spte_update: |
| 497 | * Update the state bits, it means the mapped pfn is not changged. |
| 498 | */ |
| 499 | static void mmu_spte_update(u64 *sptep, u64 new_spte) |
Avi Kivity | b79b93f | 2010-06-06 15:46:44 +0300 | [diff] [blame] | 500 | { |
Xiao Guangrong | 4132779 | 2010-08-02 16:15:08 +0800 | [diff] [blame] | 501 | u64 mask, old_spte = *sptep; |
Avi Kivity | b79b93f | 2010-06-06 15:46:44 +0300 | [diff] [blame] | 502 | |
Xiao Guangrong | 4132779 | 2010-08-02 16:15:08 +0800 | [diff] [blame] | 503 | WARN_ON(!is_rmap_spte(new_spte)); |
| 504 | |
Xiao Guangrong | 1df9f2d | 2011-07-12 03:30:35 +0800 | [diff] [blame] | 505 | if (!is_shadow_present_pte(old_spte)) |
| 506 | return mmu_spte_set(sptep, new_spte); |
| 507 | |
Xiao Guangrong | 4132779 | 2010-08-02 16:15:08 +0800 | [diff] [blame] | 508 | new_spte |= old_spte & shadow_dirty_mask; |
| 509 | |
| 510 | mask = shadow_accessed_mask; |
| 511 | if (is_writable_pte(old_spte)) |
| 512 | mask |= shadow_dirty_mask; |
| 513 | |
| 514 | if (!spte_has_volatile_bits(old_spte) || (new_spte & mask) == mask) |
Xiao Guangrong | 603e065 | 2011-07-12 03:31:28 +0800 | [diff] [blame] | 515 | __update_clear_spte_fast(sptep, new_spte); |
Xiao Guangrong | 4132779 | 2010-08-02 16:15:08 +0800 | [diff] [blame] | 516 | else |
Xiao Guangrong | 603e065 | 2011-07-12 03:31:28 +0800 | [diff] [blame] | 517 | old_spte = __update_clear_spte_slow(sptep, new_spte); |
Xiao Guangrong | 4132779 | 2010-08-02 16:15:08 +0800 | [diff] [blame] | 518 | |
| 519 | if (!shadow_accessed_mask) |
| 520 | return; |
| 521 | |
| 522 | if (spte_is_bit_cleared(old_spte, new_spte, shadow_accessed_mask)) |
| 523 | kvm_set_pfn_accessed(spte_to_pfn(old_spte)); |
| 524 | if (spte_is_bit_cleared(old_spte, new_spte, shadow_dirty_mask)) |
| 525 | kvm_set_pfn_dirty(spte_to_pfn(old_spte)); |
Avi Kivity | b79b93f | 2010-06-06 15:46:44 +0300 | [diff] [blame] | 526 | } |
| 527 | |
Xiao Guangrong | 1df9f2d | 2011-07-12 03:30:35 +0800 | [diff] [blame] | 528 | /* |
| 529 | * Rules for using mmu_spte_clear_track_bits: |
| 530 | * It sets the sptep from present to nonpresent, and track the |
| 531 | * state bits, it is used to clear the last level sptep. |
| 532 | */ |
| 533 | static int mmu_spte_clear_track_bits(u64 *sptep) |
| 534 | { |
| 535 | pfn_t pfn; |
| 536 | u64 old_spte = *sptep; |
| 537 | |
| 538 | if (!spte_has_volatile_bits(old_spte)) |
Xiao Guangrong | 603e065 | 2011-07-12 03:31:28 +0800 | [diff] [blame] | 539 | __update_clear_spte_fast(sptep, 0ull); |
Xiao Guangrong | 1df9f2d | 2011-07-12 03:30:35 +0800 | [diff] [blame] | 540 | else |
Xiao Guangrong | 603e065 | 2011-07-12 03:31:28 +0800 | [diff] [blame] | 541 | old_spte = __update_clear_spte_slow(sptep, 0ull); |
Xiao Guangrong | 1df9f2d | 2011-07-12 03:30:35 +0800 | [diff] [blame] | 542 | |
| 543 | if (!is_rmap_spte(old_spte)) |
| 544 | return 0; |
| 545 | |
| 546 | pfn = spte_to_pfn(old_spte); |
| 547 | if (!shadow_accessed_mask || old_spte & shadow_accessed_mask) |
| 548 | kvm_set_pfn_accessed(pfn); |
| 549 | if (!shadow_dirty_mask || (old_spte & shadow_dirty_mask)) |
| 550 | kvm_set_pfn_dirty(pfn); |
| 551 | return 1; |
| 552 | } |
| 553 | |
| 554 | /* |
| 555 | * Rules for using mmu_spte_clear_no_track: |
| 556 | * Directly clear spte without caring the state bits of sptep, |
| 557 | * it is used to set the upper level spte. |
| 558 | */ |
| 559 | static void mmu_spte_clear_no_track(u64 *sptep) |
| 560 | { |
Xiao Guangrong | 603e065 | 2011-07-12 03:31:28 +0800 | [diff] [blame] | 561 | __update_clear_spte_fast(sptep, 0ull); |
Xiao Guangrong | 1df9f2d | 2011-07-12 03:30:35 +0800 | [diff] [blame] | 562 | } |
| 563 | |
Xiao Guangrong | c2a2ac2 | 2011-07-12 03:32:13 +0800 | [diff] [blame] | 564 | static u64 mmu_spte_get_lockless(u64 *sptep) |
| 565 | { |
| 566 | return __get_spte_lockless(sptep); |
| 567 | } |
| 568 | |
| 569 | static void walk_shadow_page_lockless_begin(struct kvm_vcpu *vcpu) |
| 570 | { |
| 571 | rcu_read_lock(); |
| 572 | atomic_inc(&vcpu->kvm->arch.reader_counter); |
| 573 | |
| 574 | /* Increase the counter before walking shadow page table */ |
| 575 | smp_mb__after_atomic_inc(); |
| 576 | } |
| 577 | |
| 578 | static void walk_shadow_page_lockless_end(struct kvm_vcpu *vcpu) |
| 579 | { |
| 580 | /* Decrease the counter after walking shadow page table finished */ |
| 581 | smp_mb__before_atomic_dec(); |
| 582 | atomic_dec(&vcpu->kvm->arch.reader_counter); |
| 583 | rcu_read_unlock(); |
| 584 | } |
| 585 | |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 586 | static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache, |
Avi Kivity | 2e3e588 | 2007-09-10 11:28:17 +0300 | [diff] [blame] | 587 | struct kmem_cache *base_cache, int min) |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 588 | { |
| 589 | void *obj; |
| 590 | |
| 591 | if (cache->nobjs >= min) |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 592 | return 0; |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 593 | while (cache->nobjs < ARRAY_SIZE(cache->objects)) { |
Avi Kivity | 2e3e588 | 2007-09-10 11:28:17 +0300 | [diff] [blame] | 594 | obj = kmem_cache_zalloc(base_cache, GFP_KERNEL); |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 595 | if (!obj) |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 596 | return -ENOMEM; |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 597 | cache->objects[cache->nobjs++] = obj; |
| 598 | } |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 599 | return 0; |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 600 | } |
| 601 | |
Xiao Guangrong | f759e2b | 2011-09-22 16:53:17 +0800 | [diff] [blame] | 602 | static int mmu_memory_cache_free_objects(struct kvm_mmu_memory_cache *cache) |
| 603 | { |
| 604 | return cache->nobjs; |
| 605 | } |
| 606 | |
Xiao Guangrong | e8ad9a7 | 2010-05-13 10:06:02 +0800 | [diff] [blame] | 607 | static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc, |
| 608 | struct kmem_cache *cache) |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 609 | { |
| 610 | while (mc->nobjs) |
Xiao Guangrong | e8ad9a7 | 2010-05-13 10:06:02 +0800 | [diff] [blame] | 611 | kmem_cache_free(cache, mc->objects[--mc->nobjs]); |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 612 | } |
| 613 | |
Avi Kivity | c1158e6 | 2007-07-20 08:18:27 +0300 | [diff] [blame] | 614 | 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] | 615 | int min) |
Avi Kivity | c1158e6 | 2007-07-20 08:18:27 +0300 | [diff] [blame] | 616 | { |
Xiao Guangrong | 842f22e | 2011-03-04 19:01:10 +0800 | [diff] [blame] | 617 | void *page; |
Avi Kivity | c1158e6 | 2007-07-20 08:18:27 +0300 | [diff] [blame] | 618 | |
| 619 | if (cache->nobjs >= min) |
| 620 | return 0; |
| 621 | while (cache->nobjs < ARRAY_SIZE(cache->objects)) { |
Xiao Guangrong | 842f22e | 2011-03-04 19:01:10 +0800 | [diff] [blame] | 622 | page = (void *)__get_free_page(GFP_KERNEL); |
Avi Kivity | c1158e6 | 2007-07-20 08:18:27 +0300 | [diff] [blame] | 623 | if (!page) |
| 624 | return -ENOMEM; |
Xiao Guangrong | 842f22e | 2011-03-04 19:01:10 +0800 | [diff] [blame] | 625 | cache->objects[cache->nobjs++] = page; |
Avi Kivity | c1158e6 | 2007-07-20 08:18:27 +0300 | [diff] [blame] | 626 | } |
| 627 | return 0; |
| 628 | } |
| 629 | |
| 630 | static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc) |
| 631 | { |
| 632 | while (mc->nobjs) |
Avi Kivity | c4d198d | 2007-07-21 09:06:46 +0300 | [diff] [blame] | 633 | free_page((unsigned long)mc->objects[--mc->nobjs]); |
Avi Kivity | c1158e6 | 2007-07-20 08:18:27 +0300 | [diff] [blame] | 634 | } |
| 635 | |
Avi Kivity | 8c43850 | 2007-04-16 11:53:17 +0300 | [diff] [blame] | 636 | static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu) |
| 637 | { |
| 638 | int r; |
| 639 | |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 640 | r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_list_desc_cache, |
Xiao Guangrong | 67052b3 | 2011-05-15 23:27:08 +0800 | [diff] [blame] | 641 | pte_list_desc_cache, 8 + PTE_PREFETCH_NUM); |
Avi Kivity | 2e3e588 | 2007-09-10 11:28:17 +0300 | [diff] [blame] | 642 | if (r) |
| 643 | goto out; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 644 | r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8); |
Avi Kivity | 2e3e588 | 2007-09-10 11:28:17 +0300 | [diff] [blame] | 645 | if (r) |
| 646 | goto out; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 647 | r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache, |
Avi Kivity | 2e3e588 | 2007-09-10 11:28:17 +0300 | [diff] [blame] | 648 | mmu_page_header_cache, 4); |
| 649 | out: |
Avi Kivity | 8c43850 | 2007-04-16 11:53:17 +0300 | [diff] [blame] | 650 | return r; |
| 651 | } |
| 652 | |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 653 | static void mmu_free_memory_caches(struct kvm_vcpu *vcpu) |
| 654 | { |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 655 | mmu_free_memory_cache(&vcpu->arch.mmu_pte_list_desc_cache, |
| 656 | pte_list_desc_cache); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 657 | mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache); |
Xiao Guangrong | e8ad9a7 | 2010-05-13 10:06:02 +0800 | [diff] [blame] | 658 | mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache, |
| 659 | mmu_page_header_cache); |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc, |
| 663 | size_t size) |
| 664 | { |
| 665 | void *p; |
| 666 | |
| 667 | BUG_ON(!mc->nobjs); |
| 668 | p = mc->objects[--mc->nobjs]; |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 669 | return p; |
| 670 | } |
| 671 | |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 672 | static struct pte_list_desc *mmu_alloc_pte_list_desc(struct kvm_vcpu *vcpu) |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 673 | { |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 674 | return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_list_desc_cache, |
| 675 | sizeof(struct pte_list_desc)); |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 676 | } |
| 677 | |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 678 | static void mmu_free_pte_list_desc(struct pte_list_desc *pte_list_desc) |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 679 | { |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 680 | kmem_cache_free(pte_list_desc_cache, pte_list_desc); |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 681 | } |
| 682 | |
Lai Jiangshan | 2032a93 | 2010-05-26 16:49:59 +0800 | [diff] [blame] | 683 | static gfn_t kvm_mmu_page_get_gfn(struct kvm_mmu_page *sp, int index) |
| 684 | { |
| 685 | if (!sp->role.direct) |
| 686 | return sp->gfns[index]; |
| 687 | |
| 688 | return sp->gfn + (index << ((sp->role.level - 1) * PT64_LEVEL_BITS)); |
| 689 | } |
| 690 | |
| 691 | static void kvm_mmu_page_set_gfn(struct kvm_mmu_page *sp, int index, gfn_t gfn) |
| 692 | { |
| 693 | if (sp->role.direct) |
| 694 | BUG_ON(gfn != kvm_mmu_page_get_gfn(sp, index)); |
| 695 | else |
| 696 | sp->gfns[index] = gfn; |
| 697 | } |
| 698 | |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 699 | /* |
Takuya Yoshikawa | d4dbf47 | 2010-12-07 12:59:07 +0900 | [diff] [blame] | 700 | * Return the pointer to the large page information for a given gfn, |
| 701 | * handling slots that are not large page aligned. |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 702 | */ |
Takuya Yoshikawa | d4dbf47 | 2010-12-07 12:59:07 +0900 | [diff] [blame] | 703 | static struct kvm_lpage_info *lpage_info_slot(gfn_t gfn, |
| 704 | struct kvm_memory_slot *slot, |
| 705 | int level) |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 706 | { |
| 707 | unsigned long idx; |
| 708 | |
Joerg Roedel | 8285541 | 2010-07-01 16:00:11 +0200 | [diff] [blame] | 709 | idx = (gfn >> KVM_HPAGE_GFN_SHIFT(level)) - |
| 710 | (slot->base_gfn >> KVM_HPAGE_GFN_SHIFT(level)); |
Takuya Yoshikawa | d4dbf47 | 2010-12-07 12:59:07 +0900 | [diff] [blame] | 711 | return &slot->lpage_info[level - 2][idx]; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | static void account_shadowed(struct kvm *kvm, gfn_t gfn) |
| 715 | { |
Joerg Roedel | d25797b | 2009-07-27 16:30:43 +0200 | [diff] [blame] | 716 | struct kvm_memory_slot *slot; |
Takuya Yoshikawa | d4dbf47 | 2010-12-07 12:59:07 +0900 | [diff] [blame] | 717 | struct kvm_lpage_info *linfo; |
Joerg Roedel | d25797b | 2009-07-27 16:30:43 +0200 | [diff] [blame] | 718 | int i; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 719 | |
Avi Kivity | a1f4d395 | 2010-06-21 11:44:20 +0300 | [diff] [blame] | 720 | slot = gfn_to_memslot(kvm, gfn); |
Joerg Roedel | d25797b | 2009-07-27 16:30:43 +0200 | [diff] [blame] | 721 | for (i = PT_DIRECTORY_LEVEL; |
| 722 | i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) { |
Takuya Yoshikawa | d4dbf47 | 2010-12-07 12:59:07 +0900 | [diff] [blame] | 723 | linfo = lpage_info_slot(gfn, slot, i); |
| 724 | linfo->write_count += 1; |
Joerg Roedel | d25797b | 2009-07-27 16:30:43 +0200 | [diff] [blame] | 725 | } |
Xiao Guangrong | 332b207 | 2011-05-15 23:20:27 +0800 | [diff] [blame] | 726 | kvm->arch.indirect_shadow_pages++; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn) |
| 730 | { |
Joerg Roedel | d25797b | 2009-07-27 16:30:43 +0200 | [diff] [blame] | 731 | struct kvm_memory_slot *slot; |
Takuya Yoshikawa | d4dbf47 | 2010-12-07 12:59:07 +0900 | [diff] [blame] | 732 | struct kvm_lpage_info *linfo; |
Joerg Roedel | d25797b | 2009-07-27 16:30:43 +0200 | [diff] [blame] | 733 | int i; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 734 | |
Avi Kivity | a1f4d395 | 2010-06-21 11:44:20 +0300 | [diff] [blame] | 735 | slot = gfn_to_memslot(kvm, gfn); |
Joerg Roedel | d25797b | 2009-07-27 16:30:43 +0200 | [diff] [blame] | 736 | for (i = PT_DIRECTORY_LEVEL; |
| 737 | i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) { |
Takuya Yoshikawa | d4dbf47 | 2010-12-07 12:59:07 +0900 | [diff] [blame] | 738 | linfo = lpage_info_slot(gfn, slot, i); |
| 739 | linfo->write_count -= 1; |
| 740 | WARN_ON(linfo->write_count < 0); |
Joerg Roedel | d25797b | 2009-07-27 16:30:43 +0200 | [diff] [blame] | 741 | } |
Xiao Guangrong | 332b207 | 2011-05-15 23:20:27 +0800 | [diff] [blame] | 742 | kvm->arch.indirect_shadow_pages--; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 743 | } |
| 744 | |
Joerg Roedel | d25797b | 2009-07-27 16:30:43 +0200 | [diff] [blame] | 745 | static int has_wrprotected_page(struct kvm *kvm, |
| 746 | gfn_t gfn, |
| 747 | int level) |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 748 | { |
Izik Eidus | 2843099 | 2008-10-03 17:40:32 +0300 | [diff] [blame] | 749 | struct kvm_memory_slot *slot; |
Takuya Yoshikawa | d4dbf47 | 2010-12-07 12:59:07 +0900 | [diff] [blame] | 750 | struct kvm_lpage_info *linfo; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 751 | |
Avi Kivity | a1f4d395 | 2010-06-21 11:44:20 +0300 | [diff] [blame] | 752 | slot = gfn_to_memslot(kvm, gfn); |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 753 | if (slot) { |
Takuya Yoshikawa | d4dbf47 | 2010-12-07 12:59:07 +0900 | [diff] [blame] | 754 | linfo = lpage_info_slot(gfn, slot, level); |
| 755 | return linfo->write_count; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | return 1; |
| 759 | } |
| 760 | |
Joerg Roedel | d25797b | 2009-07-27 16:30:43 +0200 | [diff] [blame] | 761 | static int host_mapping_level(struct kvm *kvm, gfn_t gfn) |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 762 | { |
Joerg Roedel | 8f0b1ab | 2010-01-28 12:37:56 +0100 | [diff] [blame] | 763 | unsigned long page_size; |
Joerg Roedel | d25797b | 2009-07-27 16:30:43 +0200 | [diff] [blame] | 764 | int i, ret = 0; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 765 | |
Joerg Roedel | 8f0b1ab | 2010-01-28 12:37:56 +0100 | [diff] [blame] | 766 | page_size = kvm_host_page_size(kvm, gfn); |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 767 | |
Joerg Roedel | d25797b | 2009-07-27 16:30:43 +0200 | [diff] [blame] | 768 | for (i = PT_PAGE_TABLE_LEVEL; |
| 769 | i < (PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES); ++i) { |
| 770 | if (page_size >= KVM_HPAGE_SIZE(i)) |
| 771 | ret = i; |
| 772 | else |
| 773 | break; |
| 774 | } |
| 775 | |
Marcelo Tosatti | 4c2155c | 2008-09-16 20:54:47 -0300 | [diff] [blame] | 776 | return ret; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 777 | } |
| 778 | |
Xiao Guangrong | 5d163b1 | 2011-03-09 15:43:00 +0800 | [diff] [blame] | 779 | static struct kvm_memory_slot * |
| 780 | gfn_to_memslot_dirty_bitmap(struct kvm_vcpu *vcpu, gfn_t gfn, |
| 781 | bool no_dirty_log) |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 782 | { |
| 783 | struct kvm_memory_slot *slot; |
Xiao Guangrong | 5d163b1 | 2011-03-09 15:43:00 +0800 | [diff] [blame] | 784 | |
| 785 | slot = gfn_to_memslot(vcpu->kvm, gfn); |
| 786 | if (!slot || slot->flags & KVM_MEMSLOT_INVALID || |
| 787 | (no_dirty_log && slot->dirty_bitmap)) |
| 788 | slot = NULL; |
| 789 | |
| 790 | return slot; |
| 791 | } |
| 792 | |
| 793 | static bool mapping_level_dirty_bitmap(struct kvm_vcpu *vcpu, gfn_t large_gfn) |
| 794 | { |
Steve | a0a8eab | 2011-06-17 10:25:39 +0800 | [diff] [blame] | 795 | return !gfn_to_memslot_dirty_bitmap(vcpu, large_gfn, true); |
Andrea Arcangeli | 936a5fe | 2011-01-13 15:46:48 -0800 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn) |
| 799 | { |
| 800 | int host_level, level, max_level; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 801 | |
Joerg Roedel | d25797b | 2009-07-27 16:30:43 +0200 | [diff] [blame] | 802 | host_level = host_mapping_level(vcpu->kvm, large_gfn); |
| 803 | |
| 804 | if (host_level == PT_PAGE_TABLE_LEVEL) |
| 805 | return host_level; |
| 806 | |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 807 | max_level = kvm_x86_ops->get_lpage_level() < host_level ? |
| 808 | kvm_x86_ops->get_lpage_level() : host_level; |
| 809 | |
| 810 | for (level = PT_DIRECTORY_LEVEL; level <= max_level; ++level) |
Joerg Roedel | d25797b | 2009-07-27 16:30:43 +0200 | [diff] [blame] | 811 | if (has_wrprotected_page(vcpu->kvm, large_gfn, level)) |
| 812 | break; |
Joerg Roedel | d25797b | 2009-07-27 16:30:43 +0200 | [diff] [blame] | 813 | |
| 814 | return level - 1; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | /* |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 818 | * Pte mapping structures: |
| 819 | * |
| 820 | * If pte_list bit zero is zero, then pte_list point to the spte. |
| 821 | * |
| 822 | * If pte_list bit zero is one, (then pte_list & ~1) points to a struct |
| 823 | * pte_list_desc containing more mappings. |
| 824 | * |
| 825 | * Returns the number of pte entries before the spte was added or zero if |
| 826 | * the spte was not added. |
| 827 | * |
| 828 | */ |
| 829 | static int pte_list_add(struct kvm_vcpu *vcpu, u64 *spte, |
| 830 | unsigned long *pte_list) |
| 831 | { |
| 832 | struct pte_list_desc *desc; |
| 833 | int i, count = 0; |
| 834 | |
| 835 | if (!*pte_list) { |
| 836 | rmap_printk("pte_list_add: %p %llx 0->1\n", spte, *spte); |
| 837 | *pte_list = (unsigned long)spte; |
| 838 | } else if (!(*pte_list & 1)) { |
| 839 | rmap_printk("pte_list_add: %p %llx 1->many\n", spte, *spte); |
| 840 | desc = mmu_alloc_pte_list_desc(vcpu); |
| 841 | desc->sptes[0] = (u64 *)*pte_list; |
| 842 | desc->sptes[1] = spte; |
| 843 | *pte_list = (unsigned long)desc | 1; |
| 844 | ++count; |
| 845 | } else { |
| 846 | rmap_printk("pte_list_add: %p %llx many->many\n", spte, *spte); |
| 847 | desc = (struct pte_list_desc *)(*pte_list & ~1ul); |
| 848 | while (desc->sptes[PTE_LIST_EXT-1] && desc->more) { |
| 849 | desc = desc->more; |
| 850 | count += PTE_LIST_EXT; |
| 851 | } |
| 852 | if (desc->sptes[PTE_LIST_EXT-1]) { |
| 853 | desc->more = mmu_alloc_pte_list_desc(vcpu); |
| 854 | desc = desc->more; |
| 855 | } |
| 856 | for (i = 0; desc->sptes[i]; ++i) |
| 857 | ++count; |
| 858 | desc->sptes[i] = spte; |
| 859 | } |
| 860 | return count; |
| 861 | } |
| 862 | |
| 863 | static u64 *pte_list_next(unsigned long *pte_list, u64 *spte) |
| 864 | { |
| 865 | struct pte_list_desc *desc; |
| 866 | u64 *prev_spte; |
| 867 | int i; |
| 868 | |
| 869 | if (!*pte_list) |
| 870 | return NULL; |
| 871 | else if (!(*pte_list & 1)) { |
| 872 | if (!spte) |
| 873 | return (u64 *)*pte_list; |
| 874 | return NULL; |
| 875 | } |
| 876 | desc = (struct pte_list_desc *)(*pte_list & ~1ul); |
| 877 | prev_spte = NULL; |
| 878 | while (desc) { |
| 879 | for (i = 0; i < PTE_LIST_EXT && desc->sptes[i]; ++i) { |
| 880 | if (prev_spte == spte) |
| 881 | return desc->sptes[i]; |
| 882 | prev_spte = desc->sptes[i]; |
| 883 | } |
| 884 | desc = desc->more; |
| 885 | } |
| 886 | return NULL; |
| 887 | } |
| 888 | |
| 889 | static void |
| 890 | pte_list_desc_remove_entry(unsigned long *pte_list, struct pte_list_desc *desc, |
| 891 | int i, struct pte_list_desc *prev_desc) |
| 892 | { |
| 893 | int j; |
| 894 | |
| 895 | for (j = PTE_LIST_EXT - 1; !desc->sptes[j] && j > i; --j) |
| 896 | ; |
| 897 | desc->sptes[i] = desc->sptes[j]; |
| 898 | desc->sptes[j] = NULL; |
| 899 | if (j != 0) |
| 900 | return; |
| 901 | if (!prev_desc && !desc->more) |
| 902 | *pte_list = (unsigned long)desc->sptes[0]; |
| 903 | else |
| 904 | if (prev_desc) |
| 905 | prev_desc->more = desc->more; |
| 906 | else |
| 907 | *pte_list = (unsigned long)desc->more | 1; |
| 908 | mmu_free_pte_list_desc(desc); |
| 909 | } |
| 910 | |
| 911 | static void pte_list_remove(u64 *spte, unsigned long *pte_list) |
| 912 | { |
| 913 | struct pte_list_desc *desc; |
| 914 | struct pte_list_desc *prev_desc; |
| 915 | int i; |
| 916 | |
| 917 | if (!*pte_list) { |
| 918 | printk(KERN_ERR "pte_list_remove: %p 0->BUG\n", spte); |
| 919 | BUG(); |
| 920 | } else if (!(*pte_list & 1)) { |
| 921 | rmap_printk("pte_list_remove: %p 1->0\n", spte); |
| 922 | if ((u64 *)*pte_list != spte) { |
| 923 | printk(KERN_ERR "pte_list_remove: %p 1->BUG\n", spte); |
| 924 | BUG(); |
| 925 | } |
| 926 | *pte_list = 0; |
| 927 | } else { |
| 928 | rmap_printk("pte_list_remove: %p many->many\n", spte); |
| 929 | desc = (struct pte_list_desc *)(*pte_list & ~1ul); |
| 930 | prev_desc = NULL; |
| 931 | while (desc) { |
| 932 | for (i = 0; i < PTE_LIST_EXT && desc->sptes[i]; ++i) |
| 933 | if (desc->sptes[i] == spte) { |
| 934 | pte_list_desc_remove_entry(pte_list, |
| 935 | desc, i, |
| 936 | prev_desc); |
| 937 | return; |
| 938 | } |
| 939 | prev_desc = desc; |
| 940 | desc = desc->more; |
| 941 | } |
| 942 | pr_err("pte_list_remove: %p many->many\n", spte); |
| 943 | BUG(); |
| 944 | } |
| 945 | } |
| 946 | |
Xiao Guangrong | 67052b3 | 2011-05-15 23:27:08 +0800 | [diff] [blame] | 947 | typedef void (*pte_list_walk_fn) (u64 *spte); |
| 948 | static void pte_list_walk(unsigned long *pte_list, pte_list_walk_fn fn) |
| 949 | { |
| 950 | struct pte_list_desc *desc; |
| 951 | int i; |
| 952 | |
| 953 | if (!*pte_list) |
| 954 | return; |
| 955 | |
| 956 | if (!(*pte_list & 1)) |
| 957 | return fn((u64 *)*pte_list); |
| 958 | |
| 959 | desc = (struct pte_list_desc *)(*pte_list & ~1ul); |
| 960 | while (desc) { |
| 961 | for (i = 0; i < PTE_LIST_EXT && desc->sptes[i]; ++i) |
| 962 | fn(desc->sptes[i]); |
| 963 | desc = desc->more; |
| 964 | } |
| 965 | } |
| 966 | |
Takuya Yoshikawa | 9b9b149 | 2011-11-14 18:22:28 +0900 | [diff] [blame] | 967 | static unsigned long *__gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int level, |
| 968 | struct kvm_memory_slot *slot) |
| 969 | { |
| 970 | struct kvm_lpage_info *linfo; |
| 971 | |
| 972 | if (likely(level == PT_PAGE_TABLE_LEVEL)) |
| 973 | return &slot->rmap[gfn - slot->base_gfn]; |
| 974 | |
| 975 | linfo = lpage_info_slot(gfn, slot, level); |
| 976 | return &linfo->rmap_pde; |
| 977 | } |
| 978 | |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 979 | /* |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 980 | * Take gfn and return the reverse mapping to it. |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 981 | */ |
Joerg Roedel | 44ad994 | 2009-07-27 16:30:42 +0200 | [diff] [blame] | 982 | static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int level) |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 983 | { |
| 984 | struct kvm_memory_slot *slot; |
| 985 | |
| 986 | slot = gfn_to_memslot(kvm, gfn); |
Takuya Yoshikawa | 9b9b149 | 2011-11-14 18:22:28 +0900 | [diff] [blame] | 987 | return __gfn_to_rmap(kvm, gfn, level, slot); |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 988 | } |
| 989 | |
Xiao Guangrong | f759e2b | 2011-09-22 16:53:17 +0800 | [diff] [blame] | 990 | static bool rmap_can_add(struct kvm_vcpu *vcpu) |
| 991 | { |
| 992 | struct kvm_mmu_memory_cache *cache; |
| 993 | |
| 994 | cache = &vcpu->arch.mmu_pte_list_desc_cache; |
| 995 | return mmu_memory_cache_free_objects(cache); |
| 996 | } |
| 997 | |
Joerg Roedel | 44ad994 | 2009-07-27 16:30:42 +0200 | [diff] [blame] | 998 | static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn) |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 999 | { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1000 | struct kvm_mmu_page *sp; |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 1001 | unsigned long *rmapp; |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 1002 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1003 | sp = page_header(__pa(spte)); |
Lai Jiangshan | 2032a93 | 2010-05-26 16:49:59 +0800 | [diff] [blame] | 1004 | kvm_mmu_page_set_gfn(sp, spte - sp->spt, gfn); |
Joerg Roedel | 44ad994 | 2009-07-27 16:30:42 +0200 | [diff] [blame] | 1005 | rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level); |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 1006 | return pte_list_add(vcpu, spte, rmapp); |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 1007 | } |
| 1008 | |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 1009 | static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte) |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 1010 | { |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 1011 | return pte_list_next(rmapp, spte); |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 1012 | } |
| 1013 | |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 1014 | static void rmap_remove(struct kvm *kvm, u64 *spte) |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 1015 | { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1016 | struct kvm_mmu_page *sp; |
Lai Jiangshan | 2032a93 | 2010-05-26 16:49:59 +0800 | [diff] [blame] | 1017 | gfn_t gfn; |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 1018 | unsigned long *rmapp; |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 1019 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1020 | sp = page_header(__pa(spte)); |
Lai Jiangshan | 2032a93 | 2010-05-26 16:49:59 +0800 | [diff] [blame] | 1021 | gfn = kvm_mmu_page_get_gfn(sp, spte - sp->spt); |
| 1022 | rmapp = gfn_to_rmap(kvm, gfn, sp->role.level); |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 1023 | pte_list_remove(spte, rmapp); |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 1024 | } |
| 1025 | |
Xiao Guangrong | c370795 | 2011-07-12 03:28:04 +0800 | [diff] [blame] | 1026 | static void drop_spte(struct kvm *kvm, u64 *sptep) |
Xiao Guangrong | e4b502e | 2010-07-16 11:28:09 +0800 | [diff] [blame] | 1027 | { |
Xiao Guangrong | 1df9f2d | 2011-07-12 03:30:35 +0800 | [diff] [blame] | 1028 | if (mmu_spte_clear_track_bits(sptep)) |
Marcelo Tosatti | eb45fda | 2010-10-25 11:58:22 -0200 | [diff] [blame] | 1029 | rmap_remove(kvm, sptep); |
Avi Kivity | be38d27 | 2010-06-06 14:31:27 +0300 | [diff] [blame] | 1030 | } |
| 1031 | |
Takuya Yoshikawa | 95d4c16 | 2011-11-14 18:24:50 +0900 | [diff] [blame] | 1032 | int kvm_mmu_rmap_write_protect(struct kvm *kvm, u64 gfn, |
| 1033 | struct kvm_memory_slot *slot) |
Izik Eidus | 98348e9 | 2007-10-16 14:42:30 +0200 | [diff] [blame] | 1034 | { |
Izik Eidus | 290fc38 | 2007-09-27 14:11:22 +0200 | [diff] [blame] | 1035 | unsigned long *rmapp; |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1036 | u64 *spte; |
Joerg Roedel | 44ad994 | 2009-07-27 16:30:42 +0200 | [diff] [blame] | 1037 | int i, write_protected = 0; |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1038 | |
Takuya Yoshikawa | 9b9b149 | 2011-11-14 18:22:28 +0900 | [diff] [blame] | 1039 | rmapp = __gfn_to_rmap(kvm, gfn, PT_PAGE_TABLE_LEVEL, slot); |
Izik Eidus | 98348e9 | 2007-10-16 14:42:30 +0200 | [diff] [blame] | 1040 | spte = rmap_next(kvm, rmapp, NULL); |
| 1041 | while (spte) { |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1042 | BUG_ON(!(*spte & PT_PRESENT_MASK)); |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1043 | rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte); |
Takuya Yoshikawa | 8dae444 | 2010-01-18 18:45:10 +0900 | [diff] [blame] | 1044 | if (is_writable_pte(*spte)) { |
Xiao Guangrong | 1df9f2d | 2011-07-12 03:30:35 +0800 | [diff] [blame] | 1045 | mmu_spte_update(spte, *spte & ~PT_WRITABLE_MASK); |
Eddie Dong | caa5b8a | 2007-12-18 06:08:27 +0800 | [diff] [blame] | 1046 | write_protected = 1; |
| 1047 | } |
Izik Eidus | 9647c14 | 2007-10-16 14:43:46 +0200 | [diff] [blame] | 1048 | spte = rmap_next(kvm, rmapp, spte); |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1049 | } |
Izik Eidus | 855149a | 2008-03-20 18:17:24 +0200 | [diff] [blame] | 1050 | |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 1051 | /* check for huge page mappings */ |
Joerg Roedel | 44ad994 | 2009-07-27 16:30:42 +0200 | [diff] [blame] | 1052 | for (i = PT_DIRECTORY_LEVEL; |
| 1053 | i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) { |
Takuya Yoshikawa | 9b9b149 | 2011-11-14 18:22:28 +0900 | [diff] [blame] | 1054 | rmapp = __gfn_to_rmap(kvm, gfn, i, slot); |
Joerg Roedel | 44ad994 | 2009-07-27 16:30:42 +0200 | [diff] [blame] | 1055 | spte = rmap_next(kvm, rmapp, NULL); |
| 1056 | while (spte) { |
Joerg Roedel | 44ad994 | 2009-07-27 16:30:42 +0200 | [diff] [blame] | 1057 | BUG_ON(!(*spte & PT_PRESENT_MASK)); |
Takuya Yoshikawa | d6eebf8 | 2011-11-14 18:21:34 +0900 | [diff] [blame] | 1058 | BUG_ON(!is_large_pte(*spte)); |
Joerg Roedel | 44ad994 | 2009-07-27 16:30:42 +0200 | [diff] [blame] | 1059 | pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn); |
Takuya Yoshikawa | 8dae444 | 2010-01-18 18:45:10 +0900 | [diff] [blame] | 1060 | if (is_writable_pte(*spte)) { |
Xiao Guangrong | c370795 | 2011-07-12 03:28:04 +0800 | [diff] [blame] | 1061 | drop_spte(kvm, spte); |
Joerg Roedel | 44ad994 | 2009-07-27 16:30:42 +0200 | [diff] [blame] | 1062 | --kvm->stat.lpages; |
Joerg Roedel | 44ad994 | 2009-07-27 16:30:42 +0200 | [diff] [blame] | 1063 | spte = NULL; |
| 1064 | write_protected = 1; |
| 1065 | } |
| 1066 | spte = rmap_next(kvm, rmapp, spte); |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 1067 | } |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 1068 | } |
| 1069 | |
Marcelo Tosatti | b1a3682 | 2008-12-01 22:32:03 -0200 | [diff] [blame] | 1070 | return write_protected; |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1071 | } |
| 1072 | |
Takuya Yoshikawa | 95d4c16 | 2011-11-14 18:24:50 +0900 | [diff] [blame] | 1073 | static int rmap_write_protect(struct kvm *kvm, u64 gfn) |
| 1074 | { |
| 1075 | struct kvm_memory_slot *slot; |
| 1076 | |
| 1077 | slot = gfn_to_memslot(kvm, gfn); |
| 1078 | return kvm_mmu_rmap_write_protect(kvm, gfn, slot); |
| 1079 | } |
| 1080 | |
Frederik Deweerdt | 8a8365c | 2009-10-09 11:42:56 +0000 | [diff] [blame] | 1081 | static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp, |
| 1082 | unsigned long data) |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 1083 | { |
| 1084 | u64 *spte; |
| 1085 | int need_tlb_flush = 0; |
| 1086 | |
| 1087 | while ((spte = rmap_next(kvm, rmapp, NULL))) { |
| 1088 | BUG_ON(!(*spte & PT_PRESENT_MASK)); |
| 1089 | rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte); |
Xiao Guangrong | c370795 | 2011-07-12 03:28:04 +0800 | [diff] [blame] | 1090 | drop_spte(kvm, spte); |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 1091 | need_tlb_flush = 1; |
| 1092 | } |
| 1093 | return need_tlb_flush; |
| 1094 | } |
| 1095 | |
Frederik Deweerdt | 8a8365c | 2009-10-09 11:42:56 +0000 | [diff] [blame] | 1096 | static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp, |
| 1097 | unsigned long data) |
Izik Eidus | 3da0dd4 | 2009-09-23 21:47:18 +0300 | [diff] [blame] | 1098 | { |
| 1099 | int need_flush = 0; |
Xiao Guangrong | e4b502e | 2010-07-16 11:28:09 +0800 | [diff] [blame] | 1100 | u64 *spte, new_spte; |
Izik Eidus | 3da0dd4 | 2009-09-23 21:47:18 +0300 | [diff] [blame] | 1101 | pte_t *ptep = (pte_t *)data; |
| 1102 | pfn_t new_pfn; |
| 1103 | |
| 1104 | WARN_ON(pte_huge(*ptep)); |
| 1105 | new_pfn = pte_pfn(*ptep); |
| 1106 | spte = rmap_next(kvm, rmapp, NULL); |
| 1107 | while (spte) { |
| 1108 | BUG_ON(!is_shadow_present_pte(*spte)); |
| 1109 | rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte, *spte); |
| 1110 | need_flush = 1; |
| 1111 | if (pte_write(*ptep)) { |
Xiao Guangrong | c370795 | 2011-07-12 03:28:04 +0800 | [diff] [blame] | 1112 | drop_spte(kvm, spte); |
Izik Eidus | 3da0dd4 | 2009-09-23 21:47:18 +0300 | [diff] [blame] | 1113 | spte = rmap_next(kvm, rmapp, NULL); |
| 1114 | } else { |
| 1115 | new_spte = *spte &~ (PT64_BASE_ADDR_MASK); |
| 1116 | new_spte |= (u64)new_pfn << PAGE_SHIFT; |
| 1117 | |
| 1118 | new_spte &= ~PT_WRITABLE_MASK; |
| 1119 | new_spte &= ~SPTE_HOST_WRITEABLE; |
Avi Kivity | b79b93f | 2010-06-06 15:46:44 +0300 | [diff] [blame] | 1120 | new_spte &= ~shadow_accessed_mask; |
Xiao Guangrong | 1df9f2d | 2011-07-12 03:30:35 +0800 | [diff] [blame] | 1121 | mmu_spte_clear_track_bits(spte); |
| 1122 | mmu_spte_set(spte, new_spte); |
Izik Eidus | 3da0dd4 | 2009-09-23 21:47:18 +0300 | [diff] [blame] | 1123 | spte = rmap_next(kvm, rmapp, spte); |
| 1124 | } |
| 1125 | } |
| 1126 | if (need_flush) |
| 1127 | kvm_flush_remote_tlbs(kvm); |
| 1128 | |
| 1129 | return 0; |
| 1130 | } |
| 1131 | |
Frederik Deweerdt | 8a8365c | 2009-10-09 11:42:56 +0000 | [diff] [blame] | 1132 | static int kvm_handle_hva(struct kvm *kvm, unsigned long hva, |
| 1133 | unsigned long data, |
Izik Eidus | 3da0dd4 | 2009-09-23 21:47:18 +0300 | [diff] [blame] | 1134 | int (*handler)(struct kvm *kvm, unsigned long *rmapp, |
Frederik Deweerdt | 8a8365c | 2009-10-09 11:42:56 +0000 | [diff] [blame] | 1135 | unsigned long data)) |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 1136 | { |
Xiao Guangrong | be6ba0f | 2011-11-24 17:39:18 +0800 | [diff] [blame] | 1137 | int j; |
Avi Kivity | 90bb6fc | 2009-12-31 12:10:16 +0200 | [diff] [blame] | 1138 | int ret; |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 1139 | int retval = 0; |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 1140 | struct kvm_memslots *slots; |
Xiao Guangrong | be6ba0f | 2011-11-24 17:39:18 +0800 | [diff] [blame] | 1141 | struct kvm_memory_slot *memslot; |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 1142 | |
Lai Jiangshan | 90d83dc | 2010-04-19 17:41:23 +0800 | [diff] [blame] | 1143 | slots = kvm_memslots(kvm); |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 1144 | |
Xiao Guangrong | be6ba0f | 2011-11-24 17:39:18 +0800 | [diff] [blame] | 1145 | kvm_for_each_memslot(memslot, slots) { |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 1146 | unsigned long start = memslot->userspace_addr; |
| 1147 | unsigned long end; |
| 1148 | |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 1149 | end = start + (memslot->npages << PAGE_SHIFT); |
| 1150 | if (hva >= start && hva < end) { |
| 1151 | gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT; |
Takuya Yoshikawa | d4dbf47 | 2010-12-07 12:59:07 +0900 | [diff] [blame] | 1152 | gfn_t gfn = memslot->base_gfn + gfn_offset; |
Joerg Roedel | 852e3c1 | 2009-07-27 16:30:44 +0200 | [diff] [blame] | 1153 | |
Avi Kivity | 90bb6fc | 2009-12-31 12:10:16 +0200 | [diff] [blame] | 1154 | ret = handler(kvm, &memslot->rmap[gfn_offset], data); |
Joerg Roedel | 852e3c1 | 2009-07-27 16:30:44 +0200 | [diff] [blame] | 1155 | |
| 1156 | for (j = 0; j < KVM_NR_PAGE_SIZES - 1; ++j) { |
Takuya Yoshikawa | d4dbf47 | 2010-12-07 12:59:07 +0900 | [diff] [blame] | 1157 | struct kvm_lpage_info *linfo; |
Andrea Arcangeli | 6e3e243 | 2010-07-16 11:52:55 +0200 | [diff] [blame] | 1158 | |
Takuya Yoshikawa | d4dbf47 | 2010-12-07 12:59:07 +0900 | [diff] [blame] | 1159 | linfo = lpage_info_slot(gfn, memslot, |
| 1160 | PT_DIRECTORY_LEVEL + j); |
| 1161 | ret |= handler(kvm, &linfo->rmap_pde, data); |
Joerg Roedel | 852e3c1 | 2009-07-27 16:30:44 +0200 | [diff] [blame] | 1162 | } |
Avi Kivity | 90bb6fc | 2009-12-31 12:10:16 +0200 | [diff] [blame] | 1163 | trace_kvm_age_page(hva, memslot, ret); |
| 1164 | retval |= ret; |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | return retval; |
| 1169 | } |
| 1170 | |
| 1171 | int kvm_unmap_hva(struct kvm *kvm, unsigned long hva) |
| 1172 | { |
Izik Eidus | 3da0dd4 | 2009-09-23 21:47:18 +0300 | [diff] [blame] | 1173 | return kvm_handle_hva(kvm, hva, 0, kvm_unmap_rmapp); |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 1174 | } |
| 1175 | |
Izik Eidus | 3da0dd4 | 2009-09-23 21:47:18 +0300 | [diff] [blame] | 1176 | void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte) |
| 1177 | { |
Frederik Deweerdt | 8a8365c | 2009-10-09 11:42:56 +0000 | [diff] [blame] | 1178 | kvm_handle_hva(kvm, hva, (unsigned long)&pte, kvm_set_pte_rmapp); |
Izik Eidus | 3da0dd4 | 2009-09-23 21:47:18 +0300 | [diff] [blame] | 1179 | } |
| 1180 | |
Frederik Deweerdt | 8a8365c | 2009-10-09 11:42:56 +0000 | [diff] [blame] | 1181 | static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp, |
| 1182 | unsigned long data) |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 1183 | { |
| 1184 | u64 *spte; |
| 1185 | int young = 0; |
| 1186 | |
Rik van Riel | 6316e1c | 2010-02-03 16:11:03 -0500 | [diff] [blame] | 1187 | /* |
| 1188 | * Emulate the accessed bit for EPT, by checking if this page has |
| 1189 | * an EPT mapping, and clearing it if it does. On the next access, |
| 1190 | * a new EPT mapping will be established. |
| 1191 | * This has some overhead, but not as much as the cost of swapping |
| 1192 | * out actively used pages or breaking up actively used hugepages. |
| 1193 | */ |
Sheng Yang | 534e38b | 2008-09-08 15:12:30 +0800 | [diff] [blame] | 1194 | if (!shadow_accessed_mask) |
Rik van Riel | 6316e1c | 2010-02-03 16:11:03 -0500 | [diff] [blame] | 1195 | return kvm_unmap_rmapp(kvm, rmapp, data); |
Sheng Yang | 534e38b | 2008-09-08 15:12:30 +0800 | [diff] [blame] | 1196 | |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 1197 | spte = rmap_next(kvm, rmapp, NULL); |
| 1198 | while (spte) { |
| 1199 | int _young; |
| 1200 | u64 _spte = *spte; |
| 1201 | BUG_ON(!(_spte & PT_PRESENT_MASK)); |
| 1202 | _young = _spte & PT_ACCESSED_MASK; |
| 1203 | if (_young) { |
| 1204 | young = 1; |
| 1205 | clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte); |
| 1206 | } |
| 1207 | spte = rmap_next(kvm, rmapp, spte); |
| 1208 | } |
| 1209 | return young; |
| 1210 | } |
| 1211 | |
Andrea Arcangeli | 8ee5382 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 1212 | static int kvm_test_age_rmapp(struct kvm *kvm, unsigned long *rmapp, |
| 1213 | unsigned long data) |
| 1214 | { |
| 1215 | u64 *spte; |
| 1216 | int young = 0; |
| 1217 | |
| 1218 | /* |
| 1219 | * If there's no access bit in the secondary pte set by the |
| 1220 | * hardware it's up to gup-fast/gup to set the access bit in |
| 1221 | * the primary pte or in the page structure. |
| 1222 | */ |
| 1223 | if (!shadow_accessed_mask) |
| 1224 | goto out; |
| 1225 | |
| 1226 | spte = rmap_next(kvm, rmapp, NULL); |
| 1227 | while (spte) { |
| 1228 | u64 _spte = *spte; |
| 1229 | BUG_ON(!(_spte & PT_PRESENT_MASK)); |
| 1230 | young = _spte & PT_ACCESSED_MASK; |
| 1231 | if (young) { |
| 1232 | young = 1; |
| 1233 | break; |
| 1234 | } |
| 1235 | spte = rmap_next(kvm, rmapp, spte); |
| 1236 | } |
| 1237 | out: |
| 1238 | return young; |
| 1239 | } |
| 1240 | |
Marcelo Tosatti | 53a27b3 | 2009-08-05 15:43:58 -0300 | [diff] [blame] | 1241 | #define RMAP_RECYCLE_THRESHOLD 1000 |
| 1242 | |
Joerg Roedel | 852e3c1 | 2009-07-27 16:30:44 +0200 | [diff] [blame] | 1243 | static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn) |
Marcelo Tosatti | 53a27b3 | 2009-08-05 15:43:58 -0300 | [diff] [blame] | 1244 | { |
| 1245 | unsigned long *rmapp; |
Joerg Roedel | 852e3c1 | 2009-07-27 16:30:44 +0200 | [diff] [blame] | 1246 | struct kvm_mmu_page *sp; |
| 1247 | |
| 1248 | sp = page_header(__pa(spte)); |
Marcelo Tosatti | 53a27b3 | 2009-08-05 15:43:58 -0300 | [diff] [blame] | 1249 | |
Joerg Roedel | 852e3c1 | 2009-07-27 16:30:44 +0200 | [diff] [blame] | 1250 | rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level); |
Marcelo Tosatti | 53a27b3 | 2009-08-05 15:43:58 -0300 | [diff] [blame] | 1251 | |
Izik Eidus | 3da0dd4 | 2009-09-23 21:47:18 +0300 | [diff] [blame] | 1252 | kvm_unmap_rmapp(vcpu->kvm, rmapp, 0); |
Marcelo Tosatti | 53a27b3 | 2009-08-05 15:43:58 -0300 | [diff] [blame] | 1253 | kvm_flush_remote_tlbs(vcpu->kvm); |
| 1254 | } |
| 1255 | |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 1256 | int kvm_age_hva(struct kvm *kvm, unsigned long hva) |
| 1257 | { |
Izik Eidus | 3da0dd4 | 2009-09-23 21:47:18 +0300 | [diff] [blame] | 1258 | return kvm_handle_hva(kvm, hva, 0, kvm_age_rmapp); |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 1259 | } |
| 1260 | |
Andrea Arcangeli | 8ee5382 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 1261 | int kvm_test_age_hva(struct kvm *kvm, unsigned long hva) |
| 1262 | { |
| 1263 | return kvm_handle_hva(kvm, hva, 0, kvm_test_age_rmapp); |
| 1264 | } |
| 1265 | |
Yaozu Dong | d6c69ee | 2007-04-25 14:17:25 +0800 | [diff] [blame] | 1266 | #ifdef MMU_DEBUG |
Avi Kivity | 47ad8e6 | 2007-05-06 15:50:58 +0300 | [diff] [blame] | 1267 | static int is_empty_shadow_page(u64 *spt) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1268 | { |
Avi Kivity | 139bdb2 | 2007-01-05 16:36:50 -0800 | [diff] [blame] | 1269 | u64 *pos; |
| 1270 | u64 *end; |
| 1271 | |
Avi Kivity | 47ad8e6 | 2007-05-06 15:50:58 +0300 | [diff] [blame] | 1272 | for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++) |
Avi Kivity | 3c91551 | 2008-05-20 16:21:13 +0300 | [diff] [blame] | 1273 | if (is_shadow_present_pte(*pos)) { |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 1274 | printk(KERN_ERR "%s: %p %llx\n", __func__, |
Avi Kivity | 139bdb2 | 2007-01-05 16:36:50 -0800 | [diff] [blame] | 1275 | pos, *pos); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1276 | return 0; |
Avi Kivity | 139bdb2 | 2007-01-05 16:36:50 -0800 | [diff] [blame] | 1277 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1278 | return 1; |
| 1279 | } |
Yaozu Dong | d6c69ee | 2007-04-25 14:17:25 +0800 | [diff] [blame] | 1280 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1281 | |
Dave Hansen | 45221ab | 2010-08-19 18:11:37 -0700 | [diff] [blame] | 1282 | /* |
| 1283 | * This value is the sum of all of the kvm instances's |
| 1284 | * kvm->arch.n_used_mmu_pages values. We need a global, |
| 1285 | * aggregate version in order to make the slab shrinker |
| 1286 | * faster |
| 1287 | */ |
| 1288 | static inline void kvm_mod_used_mmu_pages(struct kvm *kvm, int nr) |
| 1289 | { |
| 1290 | kvm->arch.n_used_mmu_pages += nr; |
| 1291 | percpu_counter_add(&kvm_total_used_mmu_pages, nr); |
| 1292 | } |
| 1293 | |
Xiao Guangrong | bd4c86e | 2011-07-12 03:27:14 +0800 | [diff] [blame] | 1294 | /* |
| 1295 | * Remove the sp from shadow page cache, after call it, |
| 1296 | * we can not find this sp from the cache, and the shadow |
| 1297 | * page table is still valid. |
| 1298 | * It should be under the protection of mmu lock. |
| 1299 | */ |
| 1300 | static void kvm_mmu_isolate_page(struct kvm_mmu_page *sp) |
Avi Kivity | 260746c | 2007-01-05 16:36:49 -0800 | [diff] [blame] | 1301 | { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1302 | ASSERT(is_empty_shadow_page(sp->spt)); |
Xiao Guangrong | 7775834 | 2010-06-04 21:53:54 +0800 | [diff] [blame] | 1303 | hlist_del(&sp->hash_link); |
Lai Jiangshan | 2032a93 | 2010-05-26 16:49:59 +0800 | [diff] [blame] | 1304 | if (!sp->role.direct) |
Xiao Guangrong | 842f22e | 2011-03-04 19:01:10 +0800 | [diff] [blame] | 1305 | free_page((unsigned long)sp->gfns); |
Xiao Guangrong | bd4c86e | 2011-07-12 03:27:14 +0800 | [diff] [blame] | 1306 | } |
| 1307 | |
| 1308 | /* |
| 1309 | * Free the shadow page table and the sp, we can do it |
| 1310 | * out of the protection of mmu lock. |
| 1311 | */ |
| 1312 | static void kvm_mmu_free_page(struct kvm_mmu_page *sp) |
| 1313 | { |
| 1314 | list_del(&sp->link); |
| 1315 | free_page((unsigned long)sp->spt); |
Xiao Guangrong | e8ad9a7 | 2010-05-13 10:06:02 +0800 | [diff] [blame] | 1316 | kmem_cache_free(mmu_page_header_cache, sp); |
Avi Kivity | 260746c | 2007-01-05 16:36:49 -0800 | [diff] [blame] | 1317 | } |
| 1318 | |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1319 | static unsigned kvm_page_table_hashfn(gfn_t gfn) |
| 1320 | { |
Dong, Eddie | 1ae0a13 | 2008-01-07 13:20:25 +0200 | [diff] [blame] | 1321 | return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1); |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1322 | } |
| 1323 | |
Xiao Guangrong | 67052b3 | 2011-05-15 23:27:08 +0800 | [diff] [blame] | 1324 | static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu, |
| 1325 | struct kvm_mmu_page *sp, u64 *parent_pte) |
| 1326 | { |
| 1327 | if (!parent_pte) |
| 1328 | return; |
| 1329 | |
| 1330 | pte_list_add(vcpu, parent_pte, &sp->parent_ptes); |
| 1331 | } |
| 1332 | |
| 1333 | static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp, |
| 1334 | u64 *parent_pte) |
| 1335 | { |
| 1336 | pte_list_remove(parent_pte, &sp->parent_ptes); |
| 1337 | } |
| 1338 | |
Xiao Guangrong | bcdd9a9 | 2011-05-15 23:28:29 +0800 | [diff] [blame] | 1339 | static void drop_parent_pte(struct kvm_mmu_page *sp, |
| 1340 | u64 *parent_pte) |
| 1341 | { |
| 1342 | mmu_page_remove_parent_pte(sp, parent_pte); |
Xiao Guangrong | 1df9f2d | 2011-07-12 03:30:35 +0800 | [diff] [blame] | 1343 | mmu_spte_clear_no_track(parent_pte); |
Xiao Guangrong | bcdd9a9 | 2011-05-15 23:28:29 +0800 | [diff] [blame] | 1344 | } |
| 1345 | |
Avi Kivity | 25c0de2 | 2007-01-05 16:36:42 -0800 | [diff] [blame] | 1346 | static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu, |
Lai Jiangshan | 2032a93 | 2010-05-26 16:49:59 +0800 | [diff] [blame] | 1347 | u64 *parent_pte, int direct) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1348 | { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1349 | struct kvm_mmu_page *sp; |
Xiao Guangrong | 67052b3 | 2011-05-15 23:27:08 +0800 | [diff] [blame] | 1350 | sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, |
| 1351 | sizeof *sp); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1352 | sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE); |
Lai Jiangshan | 2032a93 | 2010-05-26 16:49:59 +0800 | [diff] [blame] | 1353 | if (!direct) |
| 1354 | sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, |
| 1355 | PAGE_SIZE); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1356 | set_page_private(virt_to_page(sp->spt), (unsigned long)sp); |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 1357 | list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages); |
Xiao Guangrong | 93a5cef | 2011-11-24 17:37:48 +0800 | [diff] [blame] | 1358 | bitmap_zero(sp->slot_bitmap, KVM_MEM_SLOTS_NUM); |
Xiao Guangrong | 67052b3 | 2011-05-15 23:27:08 +0800 | [diff] [blame] | 1359 | sp->parent_ptes = 0; |
| 1360 | mmu_page_add_parent_pte(vcpu, sp, parent_pte); |
Dave Hansen | 45221ab | 2010-08-19 18:11:37 -0700 | [diff] [blame] | 1361 | kvm_mod_used_mmu_pages(vcpu->kvm, +1); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1362 | return sp; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1363 | } |
| 1364 | |
Xiao Guangrong | 67052b3 | 2011-05-15 23:27:08 +0800 | [diff] [blame] | 1365 | static void mark_unsync(u64 *spte); |
Xiao Guangrong | 6b18493 | 2010-04-16 21:29:17 +0800 | [diff] [blame] | 1366 | static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page *sp) |
Marcelo Tosatti | 0074ff6 | 2008-09-23 13:18:40 -0300 | [diff] [blame] | 1367 | { |
Xiao Guangrong | 67052b3 | 2011-05-15 23:27:08 +0800 | [diff] [blame] | 1368 | pte_list_walk(&sp->parent_ptes, mark_unsync); |
Xiao Guangrong | 1047df1 | 2010-06-11 21:35:15 +0800 | [diff] [blame] | 1369 | } |
| 1370 | |
Xiao Guangrong | 67052b3 | 2011-05-15 23:27:08 +0800 | [diff] [blame] | 1371 | static void mark_unsync(u64 *spte) |
Xiao Guangrong | 1047df1 | 2010-06-11 21:35:15 +0800 | [diff] [blame] | 1372 | { |
Xiao Guangrong | 67052b3 | 2011-05-15 23:27:08 +0800 | [diff] [blame] | 1373 | struct kvm_mmu_page *sp; |
Xiao Guangrong | 1047df1 | 2010-06-11 21:35:15 +0800 | [diff] [blame] | 1374 | unsigned int index; |
| 1375 | |
Xiao Guangrong | 67052b3 | 2011-05-15 23:27:08 +0800 | [diff] [blame] | 1376 | sp = page_header(__pa(spte)); |
Xiao Guangrong | 1047df1 | 2010-06-11 21:35:15 +0800 | [diff] [blame] | 1377 | index = spte - sp->spt; |
| 1378 | if (__test_and_set_bit(index, sp->unsync_child_bitmap)) |
| 1379 | return; |
| 1380 | if (sp->unsync_children++) |
| 1381 | return; |
| 1382 | kvm_mmu_mark_parents_unsync(sp); |
Marcelo Tosatti | 0074ff6 | 2008-09-23 13:18:40 -0300 | [diff] [blame] | 1383 | } |
| 1384 | |
Marcelo Tosatti | e8bc217 | 2008-09-23 13:18:33 -0300 | [diff] [blame] | 1385 | static int nonpaging_sync_page(struct kvm_vcpu *vcpu, |
Xiao Guangrong | a4a8e6f | 2010-11-19 17:04:03 +0800 | [diff] [blame] | 1386 | struct kvm_mmu_page *sp) |
Marcelo Tosatti | e8bc217 | 2008-09-23 13:18:33 -0300 | [diff] [blame] | 1387 | { |
| 1388 | return 1; |
| 1389 | } |
| 1390 | |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 1391 | static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva) |
| 1392 | { |
| 1393 | } |
| 1394 | |
Xiao Guangrong | 0f53b5b | 2011-03-09 15:43:51 +0800 | [diff] [blame] | 1395 | static void nonpaging_update_pte(struct kvm_vcpu *vcpu, |
| 1396 | struct kvm_mmu_page *sp, u64 *spte, |
Xiao Guangrong | 7c56252 | 2011-03-28 10:29:27 +0800 | [diff] [blame] | 1397 | const void *pte) |
Xiao Guangrong | 0f53b5b | 2011-03-09 15:43:51 +0800 | [diff] [blame] | 1398 | { |
| 1399 | WARN_ON(1); |
| 1400 | } |
| 1401 | |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1402 | #define KVM_PAGE_ARRAY_NR 16 |
| 1403 | |
| 1404 | struct kvm_mmu_pages { |
| 1405 | struct mmu_page_and_offset { |
| 1406 | struct kvm_mmu_page *sp; |
| 1407 | unsigned int idx; |
| 1408 | } page[KVM_PAGE_ARRAY_NR]; |
| 1409 | unsigned int nr; |
| 1410 | }; |
| 1411 | |
Marcelo Tosatti | 0074ff6 | 2008-09-23 13:18:40 -0300 | [diff] [blame] | 1412 | #define for_each_unsync_children(bitmap, idx) \ |
| 1413 | for (idx = find_first_bit(bitmap, 512); \ |
| 1414 | idx < 512; \ |
| 1415 | idx = find_next_bit(bitmap, 512, idx+1)) |
| 1416 | |
Hannes Eder | cded19f | 2009-02-21 02:19:13 +0100 | [diff] [blame] | 1417 | static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp, |
| 1418 | int idx) |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1419 | { |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1420 | int i; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1421 | |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1422 | if (sp->unsync) |
| 1423 | for (i=0; i < pvec->nr; i++) |
| 1424 | if (pvec->page[i].sp == sp) |
| 1425 | return 0; |
| 1426 | |
| 1427 | pvec->page[pvec->nr].sp = sp; |
| 1428 | pvec->page[pvec->nr].idx = idx; |
| 1429 | pvec->nr++; |
| 1430 | return (pvec->nr == KVM_PAGE_ARRAY_NR); |
| 1431 | } |
| 1432 | |
| 1433 | static int __mmu_unsync_walk(struct kvm_mmu_page *sp, |
| 1434 | struct kvm_mmu_pages *pvec) |
| 1435 | { |
| 1436 | int i, ret, nr_unsync_leaf = 0; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1437 | |
Marcelo Tosatti | 0074ff6 | 2008-09-23 13:18:40 -0300 | [diff] [blame] | 1438 | for_each_unsync_children(sp->unsync_child_bitmap, i) { |
Xiao Guangrong | 7a8f1a7 | 2010-06-11 21:34:04 +0800 | [diff] [blame] | 1439 | struct kvm_mmu_page *child; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1440 | u64 ent = sp->spt[i]; |
| 1441 | |
Xiao Guangrong | 7a8f1a7 | 2010-06-11 21:34:04 +0800 | [diff] [blame] | 1442 | if (!is_shadow_present_pte(ent) || is_large_pte(ent)) |
| 1443 | goto clear_child_bitmap; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1444 | |
Xiao Guangrong | 7a8f1a7 | 2010-06-11 21:34:04 +0800 | [diff] [blame] | 1445 | child = page_header(ent & PT64_BASE_ADDR_MASK); |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1446 | |
Xiao Guangrong | 7a8f1a7 | 2010-06-11 21:34:04 +0800 | [diff] [blame] | 1447 | if (child->unsync_children) { |
| 1448 | if (mmu_pages_add(pvec, child, i)) |
| 1449 | return -ENOSPC; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1450 | |
Xiao Guangrong | 7a8f1a7 | 2010-06-11 21:34:04 +0800 | [diff] [blame] | 1451 | ret = __mmu_unsync_walk(child, pvec); |
| 1452 | if (!ret) |
| 1453 | goto clear_child_bitmap; |
| 1454 | else if (ret > 0) |
| 1455 | nr_unsync_leaf += ret; |
| 1456 | else |
| 1457 | return ret; |
| 1458 | } else if (child->unsync) { |
| 1459 | nr_unsync_leaf++; |
| 1460 | if (mmu_pages_add(pvec, child, i)) |
| 1461 | return -ENOSPC; |
| 1462 | } else |
| 1463 | goto clear_child_bitmap; |
| 1464 | |
| 1465 | continue; |
| 1466 | |
| 1467 | clear_child_bitmap: |
| 1468 | __clear_bit(i, sp->unsync_child_bitmap); |
| 1469 | sp->unsync_children--; |
| 1470 | WARN_ON((int)sp->unsync_children < 0); |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1471 | } |
| 1472 | |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1473 | |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1474 | return nr_unsync_leaf; |
| 1475 | } |
| 1476 | |
| 1477 | static int mmu_unsync_walk(struct kvm_mmu_page *sp, |
| 1478 | struct kvm_mmu_pages *pvec) |
| 1479 | { |
| 1480 | if (!sp->unsync_children) |
| 1481 | return 0; |
| 1482 | |
| 1483 | mmu_pages_add(pvec, sp, 0); |
| 1484 | return __mmu_unsync_walk(sp, pvec); |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1485 | } |
| 1486 | |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1487 | static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp) |
| 1488 | { |
| 1489 | WARN_ON(!sp->unsync); |
Xiao Guangrong | 5e1b3dd | 2010-04-28 11:55:06 +0800 | [diff] [blame] | 1490 | trace_kvm_mmu_sync_page(sp); |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1491 | sp->unsync = 0; |
| 1492 | --kvm->stat.mmu_unsync; |
| 1493 | } |
| 1494 | |
Xiao Guangrong | 7775834 | 2010-06-04 21:53:54 +0800 | [diff] [blame] | 1495 | static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp, |
| 1496 | struct list_head *invalid_list); |
| 1497 | static void kvm_mmu_commit_zap_page(struct kvm *kvm, |
| 1498 | struct list_head *invalid_list); |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1499 | |
Xiao Guangrong | f41d335 | 2010-06-04 21:56:11 +0800 | [diff] [blame] | 1500 | #define for_each_gfn_sp(kvm, sp, gfn, pos) \ |
| 1501 | hlist_for_each_entry(sp, pos, \ |
Xiao Guangrong | 7ae680e | 2010-06-04 21:53:07 +0800 | [diff] [blame] | 1502 | &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \ |
| 1503 | if ((sp)->gfn != (gfn)) {} else |
| 1504 | |
Xiao Guangrong | f41d335 | 2010-06-04 21:56:11 +0800 | [diff] [blame] | 1505 | #define for_each_gfn_indirect_valid_sp(kvm, sp, gfn, pos) \ |
| 1506 | hlist_for_each_entry(sp, pos, \ |
Xiao Guangrong | 7ae680e | 2010-06-04 21:53:07 +0800 | [diff] [blame] | 1507 | &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \ |
| 1508 | if ((sp)->gfn != (gfn) || (sp)->role.direct || \ |
| 1509 | (sp)->role.invalid) {} else |
| 1510 | |
Xiao Guangrong | f918b44 | 2010-06-11 21:30:36 +0800 | [diff] [blame] | 1511 | /* @sp->gfn should be write-protected at the call site */ |
Xiao Guangrong | 1d9dc7e | 2010-05-15 18:51:24 +0800 | [diff] [blame] | 1512 | static int __kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 1513 | struct list_head *invalid_list, bool clear_unsync) |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1514 | { |
Avi Kivity | 5b7e010 | 2010-04-14 19:20:03 +0300 | [diff] [blame] | 1515 | if (sp->role.cr4_pae != !!is_pae(vcpu)) { |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 1516 | kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list); |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1517 | return 1; |
| 1518 | } |
| 1519 | |
Xiao Guangrong | f918b44 | 2010-06-11 21:30:36 +0800 | [diff] [blame] | 1520 | if (clear_unsync) |
Xiao Guangrong | 1d9dc7e | 2010-05-15 18:51:24 +0800 | [diff] [blame] | 1521 | kvm_unlink_unsync_page(vcpu->kvm, sp); |
Xiao Guangrong | 1d9dc7e | 2010-05-15 18:51:24 +0800 | [diff] [blame] | 1522 | |
Xiao Guangrong | a4a8e6f | 2010-11-19 17:04:03 +0800 | [diff] [blame] | 1523 | if (vcpu->arch.mmu.sync_page(vcpu, sp)) { |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 1524 | kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list); |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1525 | return 1; |
| 1526 | } |
| 1527 | |
| 1528 | kvm_mmu_flush_tlb(vcpu); |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1529 | return 0; |
| 1530 | } |
| 1531 | |
Xiao Guangrong | 1d9dc7e | 2010-05-15 18:51:24 +0800 | [diff] [blame] | 1532 | static int kvm_sync_page_transient(struct kvm_vcpu *vcpu, |
| 1533 | struct kvm_mmu_page *sp) |
| 1534 | { |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 1535 | LIST_HEAD(invalid_list); |
Xiao Guangrong | 1d9dc7e | 2010-05-15 18:51:24 +0800 | [diff] [blame] | 1536 | int ret; |
| 1537 | |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 1538 | ret = __kvm_sync_page(vcpu, sp, &invalid_list, false); |
Xiao Guangrong | be71e06 | 2010-06-11 21:31:38 +0800 | [diff] [blame] | 1539 | if (ret) |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 1540 | kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list); |
| 1541 | |
Xiao Guangrong | 1d9dc7e | 2010-05-15 18:51:24 +0800 | [diff] [blame] | 1542 | return ret; |
| 1543 | } |
| 1544 | |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 1545 | static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, |
| 1546 | struct list_head *invalid_list) |
Xiao Guangrong | 1d9dc7e | 2010-05-15 18:51:24 +0800 | [diff] [blame] | 1547 | { |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 1548 | return __kvm_sync_page(vcpu, sp, invalid_list, true); |
Xiao Guangrong | 1d9dc7e | 2010-05-15 18:51:24 +0800 | [diff] [blame] | 1549 | } |
| 1550 | |
Xiao Guangrong | 9f1a122 | 2010-05-24 15:41:33 +0800 | [diff] [blame] | 1551 | /* @gfn should be write-protected at the call site */ |
| 1552 | static void kvm_sync_pages(struct kvm_vcpu *vcpu, gfn_t gfn) |
| 1553 | { |
Xiao Guangrong | 9f1a122 | 2010-05-24 15:41:33 +0800 | [diff] [blame] | 1554 | struct kvm_mmu_page *s; |
Xiao Guangrong | f41d335 | 2010-06-04 21:56:11 +0800 | [diff] [blame] | 1555 | struct hlist_node *node; |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 1556 | LIST_HEAD(invalid_list); |
Xiao Guangrong | 9f1a122 | 2010-05-24 15:41:33 +0800 | [diff] [blame] | 1557 | bool flush = false; |
| 1558 | |
Xiao Guangrong | f41d335 | 2010-06-04 21:56:11 +0800 | [diff] [blame] | 1559 | for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) { |
Xiao Guangrong | 7ae680e | 2010-06-04 21:53:07 +0800 | [diff] [blame] | 1560 | if (!s->unsync) |
Xiao Guangrong | 9f1a122 | 2010-05-24 15:41:33 +0800 | [diff] [blame] | 1561 | continue; |
| 1562 | |
| 1563 | WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL); |
Xiao Guangrong | a4a8e6f | 2010-11-19 17:04:03 +0800 | [diff] [blame] | 1564 | kvm_unlink_unsync_page(vcpu->kvm, s); |
Xiao Guangrong | 9f1a122 | 2010-05-24 15:41:33 +0800 | [diff] [blame] | 1565 | if ((s->role.cr4_pae != !!is_pae(vcpu)) || |
Xiao Guangrong | a4a8e6f | 2010-11-19 17:04:03 +0800 | [diff] [blame] | 1566 | (vcpu->arch.mmu.sync_page(vcpu, s))) { |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 1567 | kvm_mmu_prepare_zap_page(vcpu->kvm, s, &invalid_list); |
Xiao Guangrong | 9f1a122 | 2010-05-24 15:41:33 +0800 | [diff] [blame] | 1568 | continue; |
| 1569 | } |
Xiao Guangrong | 9f1a122 | 2010-05-24 15:41:33 +0800 | [diff] [blame] | 1570 | flush = true; |
| 1571 | } |
| 1572 | |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 1573 | kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list); |
Xiao Guangrong | 9f1a122 | 2010-05-24 15:41:33 +0800 | [diff] [blame] | 1574 | if (flush) |
| 1575 | kvm_mmu_flush_tlb(vcpu); |
| 1576 | } |
| 1577 | |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1578 | struct mmu_page_path { |
| 1579 | struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1]; |
| 1580 | unsigned int idx[PT64_ROOT_LEVEL-1]; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1581 | }; |
| 1582 | |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1583 | #define for_each_sp(pvec, sp, parents, i) \ |
| 1584 | for (i = mmu_pages_next(&pvec, &parents, -1), \ |
| 1585 | sp = pvec.page[i].sp; \ |
| 1586 | i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \ |
| 1587 | i = mmu_pages_next(&pvec, &parents, i)) |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1588 | |
Hannes Eder | cded19f | 2009-02-21 02:19:13 +0100 | [diff] [blame] | 1589 | static int mmu_pages_next(struct kvm_mmu_pages *pvec, |
| 1590 | struct mmu_page_path *parents, |
| 1591 | int i) |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1592 | { |
| 1593 | int n; |
| 1594 | |
| 1595 | for (n = i+1; n < pvec->nr; n++) { |
| 1596 | struct kvm_mmu_page *sp = pvec->page[n].sp; |
| 1597 | |
| 1598 | if (sp->role.level == PT_PAGE_TABLE_LEVEL) { |
| 1599 | parents->idx[0] = pvec->page[n].idx; |
| 1600 | return n; |
| 1601 | } |
| 1602 | |
| 1603 | parents->parent[sp->role.level-2] = sp; |
| 1604 | parents->idx[sp->role.level-1] = pvec->page[n].idx; |
| 1605 | } |
| 1606 | |
| 1607 | return n; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1608 | } |
| 1609 | |
Hannes Eder | cded19f | 2009-02-21 02:19:13 +0100 | [diff] [blame] | 1610 | static void mmu_pages_clear_parents(struct mmu_page_path *parents) |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1611 | { |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1612 | struct kvm_mmu_page *sp; |
| 1613 | unsigned int level = 0; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1614 | |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1615 | do { |
| 1616 | unsigned int idx = parents->idx[level]; |
| 1617 | |
| 1618 | sp = parents->parent[level]; |
| 1619 | if (!sp) |
| 1620 | return; |
| 1621 | |
| 1622 | --sp->unsync_children; |
| 1623 | WARN_ON((int)sp->unsync_children < 0); |
| 1624 | __clear_bit(idx, sp->unsync_child_bitmap); |
| 1625 | level++; |
| 1626 | } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children); |
| 1627 | } |
| 1628 | |
| 1629 | static void kvm_mmu_pages_init(struct kvm_mmu_page *parent, |
| 1630 | struct mmu_page_path *parents, |
| 1631 | struct kvm_mmu_pages *pvec) |
| 1632 | { |
| 1633 | parents->parent[parent->role.level-1] = NULL; |
| 1634 | pvec->nr = 0; |
| 1635 | } |
| 1636 | |
| 1637 | static void mmu_sync_children(struct kvm_vcpu *vcpu, |
| 1638 | struct kvm_mmu_page *parent) |
| 1639 | { |
| 1640 | int i; |
| 1641 | struct kvm_mmu_page *sp; |
| 1642 | struct mmu_page_path parents; |
| 1643 | struct kvm_mmu_pages pages; |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 1644 | LIST_HEAD(invalid_list); |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1645 | |
| 1646 | kvm_mmu_pages_init(parent, &parents, &pages); |
| 1647 | while (mmu_unsync_walk(parent, &pages)) { |
Marcelo Tosatti | b1a3682 | 2008-12-01 22:32:03 -0200 | [diff] [blame] | 1648 | int protected = 0; |
| 1649 | |
| 1650 | for_each_sp(pages, sp, parents, i) |
| 1651 | protected |= rmap_write_protect(vcpu->kvm, sp->gfn); |
| 1652 | |
| 1653 | if (protected) |
| 1654 | kvm_flush_remote_tlbs(vcpu->kvm); |
| 1655 | |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1656 | for_each_sp(pages, sp, parents, i) { |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 1657 | kvm_sync_page(vcpu, sp, &invalid_list); |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1658 | mmu_pages_clear_parents(&parents); |
| 1659 | } |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 1660 | kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list); |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1661 | cond_resched_lock(&vcpu->kvm->mmu_lock); |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1662 | kvm_mmu_pages_init(parent, &parents, &pages); |
| 1663 | } |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1664 | } |
| 1665 | |
Xiao Guangrong | c370795 | 2011-07-12 03:28:04 +0800 | [diff] [blame] | 1666 | static void init_shadow_page_table(struct kvm_mmu_page *sp) |
| 1667 | { |
| 1668 | int i; |
| 1669 | |
| 1670 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) |
| 1671 | sp->spt[i] = 0ull; |
| 1672 | } |
| 1673 | |
Xiao Guangrong | a30f47c | 2011-09-22 16:58:36 +0800 | [diff] [blame] | 1674 | static void __clear_sp_write_flooding_count(struct kvm_mmu_page *sp) |
| 1675 | { |
| 1676 | sp->write_flooding_count = 0; |
| 1677 | } |
| 1678 | |
| 1679 | static void clear_sp_write_flooding_count(u64 *spte) |
| 1680 | { |
| 1681 | struct kvm_mmu_page *sp = page_header(__pa(spte)); |
| 1682 | |
| 1683 | __clear_sp_write_flooding_count(sp); |
| 1684 | } |
| 1685 | |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1686 | static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu, |
| 1687 | gfn_t gfn, |
| 1688 | gva_t gaddr, |
| 1689 | unsigned level, |
Avi Kivity | f6e2c02b | 2009-01-11 13:02:10 +0200 | [diff] [blame] | 1690 | int direct, |
Avi Kivity | 41074d0 | 2007-12-09 17:00:02 +0200 | [diff] [blame] | 1691 | unsigned access, |
Avi Kivity | f7d9c7b | 2008-02-26 22:12:10 +0200 | [diff] [blame] | 1692 | u64 *parent_pte) |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1693 | { |
| 1694 | union kvm_mmu_page_role role; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1695 | unsigned quadrant; |
Xiao Guangrong | 9f1a122 | 2010-05-24 15:41:33 +0800 | [diff] [blame] | 1696 | struct kvm_mmu_page *sp; |
Xiao Guangrong | f41d335 | 2010-06-04 21:56:11 +0800 | [diff] [blame] | 1697 | struct hlist_node *node; |
Xiao Guangrong | 9f1a122 | 2010-05-24 15:41:33 +0800 | [diff] [blame] | 1698 | bool need_sync = false; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1699 | |
Avi Kivity | a770f6f | 2008-12-21 19:20:09 +0200 | [diff] [blame] | 1700 | role = vcpu->arch.mmu.base_role; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1701 | role.level = level; |
Avi Kivity | f6e2c02b | 2009-01-11 13:02:10 +0200 | [diff] [blame] | 1702 | role.direct = direct; |
Avi Kivity | 84b0c8c | 2010-03-14 10:16:40 +0200 | [diff] [blame] | 1703 | if (role.direct) |
Avi Kivity | 5b7e010 | 2010-04-14 19:20:03 +0300 | [diff] [blame] | 1704 | role.cr4_pae = 0; |
Avi Kivity | 41074d0 | 2007-12-09 17:00:02 +0200 | [diff] [blame] | 1705 | role.access = access; |
Joerg Roedel | c5a78f2b | 2010-09-10 17:30:39 +0200 | [diff] [blame] | 1706 | if (!vcpu->arch.mmu.direct_map |
| 1707 | && vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) { |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1708 | quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level)); |
| 1709 | quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1; |
| 1710 | role.quadrant = quadrant; |
| 1711 | } |
Xiao Guangrong | f41d335 | 2010-06-04 21:56:11 +0800 | [diff] [blame] | 1712 | for_each_gfn_sp(vcpu->kvm, sp, gfn, node) { |
Xiao Guangrong | 7ae680e | 2010-06-04 21:53:07 +0800 | [diff] [blame] | 1713 | if (!need_sync && sp->unsync) |
| 1714 | need_sync = true; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1715 | |
Xiao Guangrong | 7ae680e | 2010-06-04 21:53:07 +0800 | [diff] [blame] | 1716 | if (sp->role.word != role.word) |
| 1717 | continue; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1718 | |
Xiao Guangrong | 7ae680e | 2010-06-04 21:53:07 +0800 | [diff] [blame] | 1719 | if (sp->unsync && kvm_sync_page_transient(vcpu, sp)) |
| 1720 | break; |
Xiao Guangrong | e02aa90 | 2010-05-15 18:52:34 +0800 | [diff] [blame] | 1721 | |
Xiao Guangrong | 7ae680e | 2010-06-04 21:53:07 +0800 | [diff] [blame] | 1722 | mmu_page_add_parent_pte(vcpu, sp, parent_pte); |
| 1723 | if (sp->unsync_children) { |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 1724 | kvm_make_request(KVM_REQ_MMU_SYNC, vcpu); |
Xiao Guangrong | 7ae680e | 2010-06-04 21:53:07 +0800 | [diff] [blame] | 1725 | kvm_mmu_mark_parents_unsync(sp); |
| 1726 | } else if (sp->unsync) |
| 1727 | kvm_mmu_mark_parents_unsync(sp); |
Xiao Guangrong | e02aa90 | 2010-05-15 18:52:34 +0800 | [diff] [blame] | 1728 | |
Xiao Guangrong | a30f47c | 2011-09-22 16:58:36 +0800 | [diff] [blame] | 1729 | __clear_sp_write_flooding_count(sp); |
Xiao Guangrong | 7ae680e | 2010-06-04 21:53:07 +0800 | [diff] [blame] | 1730 | trace_kvm_mmu_get_page(sp, false); |
| 1731 | return sp; |
| 1732 | } |
Avi Kivity | dfc5aa0 | 2007-12-18 19:47:18 +0200 | [diff] [blame] | 1733 | ++vcpu->kvm->stat.mmu_cache_miss; |
Lai Jiangshan | 2032a93 | 2010-05-26 16:49:59 +0800 | [diff] [blame] | 1734 | sp = kvm_mmu_alloc_page(vcpu, parent_pte, direct); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1735 | if (!sp) |
| 1736 | return sp; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1737 | sp->gfn = gfn; |
| 1738 | sp->role = role; |
Xiao Guangrong | 7ae680e | 2010-06-04 21:53:07 +0800 | [diff] [blame] | 1739 | hlist_add_head(&sp->hash_link, |
| 1740 | &vcpu->kvm->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)]); |
Avi Kivity | f6e2c02b | 2009-01-11 13:02:10 +0200 | [diff] [blame] | 1741 | if (!direct) { |
Marcelo Tosatti | b1a3682 | 2008-12-01 22:32:03 -0200 | [diff] [blame] | 1742 | if (rmap_write_protect(vcpu->kvm, gfn)) |
| 1743 | kvm_flush_remote_tlbs(vcpu->kvm); |
Xiao Guangrong | 9f1a122 | 2010-05-24 15:41:33 +0800 | [diff] [blame] | 1744 | if (level > PT_PAGE_TABLE_LEVEL && need_sync) |
| 1745 | kvm_sync_pages(vcpu, gfn); |
| 1746 | |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1747 | account_shadowed(vcpu->kvm, gfn); |
| 1748 | } |
Xiao Guangrong | c370795 | 2011-07-12 03:28:04 +0800 | [diff] [blame] | 1749 | init_shadow_page_table(sp); |
Avi Kivity | f691fe1 | 2009-07-06 15:58:14 +0300 | [diff] [blame] | 1750 | trace_kvm_mmu_get_page(sp, true); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1751 | return sp; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1752 | } |
| 1753 | |
Avi Kivity | 2d11123 | 2008-12-25 14:39:47 +0200 | [diff] [blame] | 1754 | static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator, |
| 1755 | struct kvm_vcpu *vcpu, u64 addr) |
| 1756 | { |
| 1757 | iterator->addr = addr; |
| 1758 | iterator->shadow_addr = vcpu->arch.mmu.root_hpa; |
| 1759 | iterator->level = vcpu->arch.mmu.shadow_root_level; |
Joerg Roedel | 81407ca | 2010-09-10 17:31:00 +0200 | [diff] [blame] | 1760 | |
| 1761 | if (iterator->level == PT64_ROOT_LEVEL && |
| 1762 | vcpu->arch.mmu.root_level < PT64_ROOT_LEVEL && |
| 1763 | !vcpu->arch.mmu.direct_map) |
| 1764 | --iterator->level; |
| 1765 | |
Avi Kivity | 2d11123 | 2008-12-25 14:39:47 +0200 | [diff] [blame] | 1766 | if (iterator->level == PT32E_ROOT_LEVEL) { |
| 1767 | iterator->shadow_addr |
| 1768 | = vcpu->arch.mmu.pae_root[(addr >> 30) & 3]; |
| 1769 | iterator->shadow_addr &= PT64_BASE_ADDR_MASK; |
| 1770 | --iterator->level; |
| 1771 | if (!iterator->shadow_addr) |
| 1772 | iterator->level = 0; |
| 1773 | } |
| 1774 | } |
| 1775 | |
| 1776 | static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator) |
| 1777 | { |
| 1778 | if (iterator->level < PT_PAGE_TABLE_LEVEL) |
| 1779 | return false; |
Marcelo Tosatti | 4d88954 | 2009-06-11 12:07:41 -0300 | [diff] [blame] | 1780 | |
Avi Kivity | 2d11123 | 2008-12-25 14:39:47 +0200 | [diff] [blame] | 1781 | iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level); |
| 1782 | iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index; |
| 1783 | return true; |
| 1784 | } |
| 1785 | |
Xiao Guangrong | c2a2ac2 | 2011-07-12 03:32:13 +0800 | [diff] [blame] | 1786 | static void __shadow_walk_next(struct kvm_shadow_walk_iterator *iterator, |
| 1787 | u64 spte) |
Avi Kivity | 2d11123 | 2008-12-25 14:39:47 +0200 | [diff] [blame] | 1788 | { |
Xiao Guangrong | c2a2ac2 | 2011-07-12 03:32:13 +0800 | [diff] [blame] | 1789 | if (is_last_spte(spte, iterator->level)) { |
Xiao Guangrong | 052331b | 2011-07-12 03:21:17 +0800 | [diff] [blame] | 1790 | iterator->level = 0; |
| 1791 | return; |
| 1792 | } |
| 1793 | |
Xiao Guangrong | c2a2ac2 | 2011-07-12 03:32:13 +0800 | [diff] [blame] | 1794 | iterator->shadow_addr = spte & PT64_BASE_ADDR_MASK; |
Avi Kivity | 2d11123 | 2008-12-25 14:39:47 +0200 | [diff] [blame] | 1795 | --iterator->level; |
| 1796 | } |
| 1797 | |
Xiao Guangrong | c2a2ac2 | 2011-07-12 03:32:13 +0800 | [diff] [blame] | 1798 | static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator) |
| 1799 | { |
| 1800 | return __shadow_walk_next(iterator, *iterator->sptep); |
| 1801 | } |
| 1802 | |
Avi Kivity | 32ef26a | 2010-07-13 14:27:04 +0300 | [diff] [blame] | 1803 | static void link_shadow_page(u64 *sptep, struct kvm_mmu_page *sp) |
| 1804 | { |
| 1805 | u64 spte; |
| 1806 | |
| 1807 | spte = __pa(sp->spt) |
| 1808 | | PT_PRESENT_MASK | PT_ACCESSED_MASK |
| 1809 | | PT_WRITABLE_MASK | PT_USER_MASK; |
Xiao Guangrong | 1df9f2d | 2011-07-12 03:30:35 +0800 | [diff] [blame] | 1810 | mmu_spte_set(sptep, spte); |
Avi Kivity | 32ef26a | 2010-07-13 14:27:04 +0300 | [diff] [blame] | 1811 | } |
| 1812 | |
Avi Kivity | a3aa51c | 2010-07-13 14:27:06 +0300 | [diff] [blame] | 1813 | static void drop_large_spte(struct kvm_vcpu *vcpu, u64 *sptep) |
| 1814 | { |
| 1815 | if (is_large_pte(*sptep)) { |
Xiao Guangrong | c370795 | 2011-07-12 03:28:04 +0800 | [diff] [blame] | 1816 | drop_spte(vcpu->kvm, sptep); |
Avi Kivity | a3aa51c | 2010-07-13 14:27:06 +0300 | [diff] [blame] | 1817 | kvm_flush_remote_tlbs(vcpu->kvm); |
| 1818 | } |
| 1819 | } |
| 1820 | |
Avi Kivity | a357bd2 | 2010-07-13 14:27:07 +0300 | [diff] [blame] | 1821 | static void validate_direct_spte(struct kvm_vcpu *vcpu, u64 *sptep, |
| 1822 | unsigned direct_access) |
| 1823 | { |
| 1824 | if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep)) { |
| 1825 | struct kvm_mmu_page *child; |
| 1826 | |
| 1827 | /* |
| 1828 | * For the direct sp, if the guest pte's dirty bit |
| 1829 | * changed form clean to dirty, it will corrupt the |
| 1830 | * sp's access: allow writable in the read-only sp, |
| 1831 | * so we should update the spte at this point to get |
| 1832 | * a new sp with the correct access. |
| 1833 | */ |
| 1834 | child = page_header(*sptep & PT64_BASE_ADDR_MASK); |
| 1835 | if (child->role.access == direct_access) |
| 1836 | return; |
| 1837 | |
Xiao Guangrong | bcdd9a9 | 2011-05-15 23:28:29 +0800 | [diff] [blame] | 1838 | drop_parent_pte(child, sptep); |
Avi Kivity | a357bd2 | 2010-07-13 14:27:07 +0300 | [diff] [blame] | 1839 | kvm_flush_remote_tlbs(vcpu->kvm); |
| 1840 | } |
| 1841 | } |
| 1842 | |
Xiao Guangrong | 505aef8 | 2011-09-22 16:56:06 +0800 | [diff] [blame] | 1843 | static bool mmu_page_zap_pte(struct kvm *kvm, struct kvm_mmu_page *sp, |
Xiao Guangrong | 38e3b2b | 2011-05-15 23:27:52 +0800 | [diff] [blame] | 1844 | u64 *spte) |
| 1845 | { |
| 1846 | u64 pte; |
| 1847 | struct kvm_mmu_page *child; |
| 1848 | |
| 1849 | pte = *spte; |
| 1850 | if (is_shadow_present_pte(pte)) { |
Xiao Guangrong | 505aef8 | 2011-09-22 16:56:06 +0800 | [diff] [blame] | 1851 | if (is_last_spte(pte, sp->role.level)) { |
Xiao Guangrong | c370795 | 2011-07-12 03:28:04 +0800 | [diff] [blame] | 1852 | drop_spte(kvm, spte); |
Xiao Guangrong | 505aef8 | 2011-09-22 16:56:06 +0800 | [diff] [blame] | 1853 | if (is_large_pte(pte)) |
| 1854 | --kvm->stat.lpages; |
| 1855 | } else { |
Xiao Guangrong | 38e3b2b | 2011-05-15 23:27:52 +0800 | [diff] [blame] | 1856 | child = page_header(pte & PT64_BASE_ADDR_MASK); |
Xiao Guangrong | bcdd9a9 | 2011-05-15 23:28:29 +0800 | [diff] [blame] | 1857 | drop_parent_pte(child, spte); |
Xiao Guangrong | 38e3b2b | 2011-05-15 23:27:52 +0800 | [diff] [blame] | 1858 | } |
Xiao Guangrong | 505aef8 | 2011-09-22 16:56:06 +0800 | [diff] [blame] | 1859 | return true; |
| 1860 | } |
| 1861 | |
| 1862 | if (is_mmio_spte(pte)) |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 1863 | mmu_spte_clear_no_track(spte); |
Xiao Guangrong | c370795 | 2011-07-12 03:28:04 +0800 | [diff] [blame] | 1864 | |
Xiao Guangrong | 505aef8 | 2011-09-22 16:56:06 +0800 | [diff] [blame] | 1865 | return false; |
Xiao Guangrong | 38e3b2b | 2011-05-15 23:27:52 +0800 | [diff] [blame] | 1866 | } |
| 1867 | |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 1868 | static void kvm_mmu_page_unlink_children(struct kvm *kvm, |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1869 | struct kvm_mmu_page *sp) |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1870 | { |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 1871 | unsigned i; |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 1872 | |
Xiao Guangrong | 38e3b2b | 2011-05-15 23:27:52 +0800 | [diff] [blame] | 1873 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) |
| 1874 | mmu_page_zap_pte(kvm, sp, sp->spt + i); |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1875 | } |
| 1876 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1877 | 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] | 1878 | { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1879 | mmu_page_remove_parent_pte(sp, parent_pte); |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1880 | } |
| 1881 | |
Avi Kivity | 31aa2b4 | 2008-07-11 17:59:46 +0300 | [diff] [blame] | 1882 | static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp) |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1883 | { |
| 1884 | u64 *parent_pte; |
| 1885 | |
Xiao Guangrong | bcdd9a9 | 2011-05-15 23:28:29 +0800 | [diff] [blame] | 1886 | while ((parent_pte = pte_list_next(&sp->parent_ptes, NULL))) |
| 1887 | drop_parent_pte(sp, parent_pte); |
Avi Kivity | 31aa2b4 | 2008-07-11 17:59:46 +0300 | [diff] [blame] | 1888 | } |
| 1889 | |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1890 | static int mmu_zap_unsync_children(struct kvm *kvm, |
Xiao Guangrong | 7775834 | 2010-06-04 21:53:54 +0800 | [diff] [blame] | 1891 | struct kvm_mmu_page *parent, |
| 1892 | struct list_head *invalid_list) |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1893 | { |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1894 | int i, zapped = 0; |
| 1895 | struct mmu_page_path parents; |
| 1896 | struct kvm_mmu_pages pages; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1897 | |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1898 | if (parent->role.level == PT_PAGE_TABLE_LEVEL) |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1899 | return 0; |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1900 | |
| 1901 | kvm_mmu_pages_init(parent, &parents, &pages); |
| 1902 | while (mmu_unsync_walk(parent, &pages)) { |
| 1903 | struct kvm_mmu_page *sp; |
| 1904 | |
| 1905 | for_each_sp(pages, sp, parents, i) { |
Xiao Guangrong | 7775834 | 2010-06-04 21:53:54 +0800 | [diff] [blame] | 1906 | kvm_mmu_prepare_zap_page(kvm, sp, invalid_list); |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1907 | mmu_pages_clear_parents(&parents); |
Xiao Guangrong | 77662e0 | 2010-04-16 16:34:42 +0800 | [diff] [blame] | 1908 | zapped++; |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1909 | } |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1910 | kvm_mmu_pages_init(parent, &parents, &pages); |
| 1911 | } |
| 1912 | |
| 1913 | return zapped; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1914 | } |
| 1915 | |
Xiao Guangrong | 7775834 | 2010-06-04 21:53:54 +0800 | [diff] [blame] | 1916 | static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp, |
| 1917 | struct list_head *invalid_list) |
Avi Kivity | 31aa2b4 | 2008-07-11 17:59:46 +0300 | [diff] [blame] | 1918 | { |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1919 | int ret; |
Avi Kivity | f691fe1 | 2009-07-06 15:58:14 +0300 | [diff] [blame] | 1920 | |
Xiao Guangrong | 7775834 | 2010-06-04 21:53:54 +0800 | [diff] [blame] | 1921 | trace_kvm_mmu_prepare_zap_page(sp); |
Avi Kivity | 31aa2b4 | 2008-07-11 17:59:46 +0300 | [diff] [blame] | 1922 | ++kvm->stat.mmu_shadow_zapped; |
Xiao Guangrong | 7775834 | 2010-06-04 21:53:54 +0800 | [diff] [blame] | 1923 | ret = mmu_zap_unsync_children(kvm, sp, invalid_list); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1924 | kvm_mmu_page_unlink_children(kvm, sp); |
Avi Kivity | 31aa2b4 | 2008-07-11 17:59:46 +0300 | [diff] [blame] | 1925 | kvm_mmu_unlink_parents(kvm, sp); |
Avi Kivity | f6e2c02b | 2009-01-11 13:02:10 +0200 | [diff] [blame] | 1926 | if (!sp->role.invalid && !sp->role.direct) |
Avi Kivity | 5b5c6a5 | 2008-07-11 18:07:26 +0300 | [diff] [blame] | 1927 | unaccount_shadowed(kvm, sp->gfn); |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1928 | if (sp->unsync) |
| 1929 | kvm_unlink_unsync_page(kvm, sp); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1930 | if (!sp->root_count) { |
Gui Jianfeng | 54a4f02 | 2010-05-05 09:03:49 +0800 | [diff] [blame] | 1931 | /* Count self */ |
| 1932 | ret++; |
Xiao Guangrong | 7775834 | 2010-06-04 21:53:54 +0800 | [diff] [blame] | 1933 | list_move(&sp->link, invalid_list); |
Xiao Guangrong | aa6bd18 | 2011-07-12 03:26:40 +0800 | [diff] [blame] | 1934 | kvm_mod_used_mmu_pages(kvm, -1); |
Marcelo Tosatti | 2e53d63 | 2008-02-20 14:47:24 -0500 | [diff] [blame] | 1935 | } else { |
Avi Kivity | 5b5c6a5 | 2008-07-11 18:07:26 +0300 | [diff] [blame] | 1936 | list_move(&sp->link, &kvm->arch.active_mmu_pages); |
Marcelo Tosatti | 2e53d63 | 2008-02-20 14:47:24 -0500 | [diff] [blame] | 1937 | kvm_reload_remote_mmus(kvm); |
| 1938 | } |
Xiao Guangrong | 7775834 | 2010-06-04 21:53:54 +0800 | [diff] [blame] | 1939 | |
| 1940 | sp->role.invalid = 1; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1941 | return ret; |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1942 | } |
| 1943 | |
Xiao Guangrong | c2a2ac2 | 2011-07-12 03:32:13 +0800 | [diff] [blame] | 1944 | static void kvm_mmu_isolate_pages(struct list_head *invalid_list) |
| 1945 | { |
| 1946 | struct kvm_mmu_page *sp; |
| 1947 | |
| 1948 | list_for_each_entry(sp, invalid_list, link) |
| 1949 | kvm_mmu_isolate_page(sp); |
| 1950 | } |
| 1951 | |
| 1952 | static void free_pages_rcu(struct rcu_head *head) |
| 1953 | { |
| 1954 | struct kvm_mmu_page *next, *sp; |
| 1955 | |
| 1956 | sp = container_of(head, struct kvm_mmu_page, rcu); |
| 1957 | while (sp) { |
| 1958 | if (!list_empty(&sp->link)) |
| 1959 | next = list_first_entry(&sp->link, |
| 1960 | struct kvm_mmu_page, link); |
| 1961 | else |
| 1962 | next = NULL; |
| 1963 | kvm_mmu_free_page(sp); |
| 1964 | sp = next; |
| 1965 | } |
| 1966 | } |
| 1967 | |
Xiao Guangrong | 7775834 | 2010-06-04 21:53:54 +0800 | [diff] [blame] | 1968 | static void kvm_mmu_commit_zap_page(struct kvm *kvm, |
| 1969 | struct list_head *invalid_list) |
| 1970 | { |
| 1971 | struct kvm_mmu_page *sp; |
| 1972 | |
| 1973 | if (list_empty(invalid_list)) |
| 1974 | return; |
| 1975 | |
| 1976 | kvm_flush_remote_tlbs(kvm); |
| 1977 | |
Xiao Guangrong | c2a2ac2 | 2011-07-12 03:32:13 +0800 | [diff] [blame] | 1978 | if (atomic_read(&kvm->arch.reader_counter)) { |
| 1979 | kvm_mmu_isolate_pages(invalid_list); |
| 1980 | sp = list_first_entry(invalid_list, struct kvm_mmu_page, link); |
| 1981 | list_del_init(invalid_list); |
Xiao Guangrong | 4f02264 | 2011-07-12 03:34:24 +0800 | [diff] [blame] | 1982 | |
| 1983 | trace_kvm_mmu_delay_free_pages(sp); |
Xiao Guangrong | c2a2ac2 | 2011-07-12 03:32:13 +0800 | [diff] [blame] | 1984 | call_rcu(&sp->rcu, free_pages_rcu); |
| 1985 | return; |
| 1986 | } |
| 1987 | |
Xiao Guangrong | 7775834 | 2010-06-04 21:53:54 +0800 | [diff] [blame] | 1988 | do { |
| 1989 | sp = list_first_entry(invalid_list, struct kvm_mmu_page, link); |
| 1990 | WARN_ON(!sp->role.invalid || sp->root_count); |
Xiao Guangrong | bd4c86e | 2011-07-12 03:27:14 +0800 | [diff] [blame] | 1991 | kvm_mmu_isolate_page(sp); |
Xiao Guangrong | aa6bd18 | 2011-07-12 03:26:40 +0800 | [diff] [blame] | 1992 | kvm_mmu_free_page(sp); |
Xiao Guangrong | 7775834 | 2010-06-04 21:53:54 +0800 | [diff] [blame] | 1993 | } while (!list_empty(invalid_list)); |
| 1994 | |
| 1995 | } |
| 1996 | |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 1997 | /* |
| 1998 | * Changing the number of mmu pages allocated to the vm |
Dave Hansen | 49d5ca2 | 2010-08-19 18:11:28 -0700 | [diff] [blame] | 1999 | * Note: if goal_nr_mmu_pages is too small, you will get dead lock |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 2000 | */ |
Dave Hansen | 49d5ca2 | 2010-08-19 18:11:28 -0700 | [diff] [blame] | 2001 | void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int goal_nr_mmu_pages) |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 2002 | { |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 2003 | LIST_HEAD(invalid_list); |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 2004 | /* |
| 2005 | * If we set the number of mmu pages to be smaller be than the |
| 2006 | * number of actived pages , we must to free some mmu pages before we |
| 2007 | * change the value |
| 2008 | */ |
| 2009 | |
Dave Hansen | 49d5ca2 | 2010-08-19 18:11:28 -0700 | [diff] [blame] | 2010 | if (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages) { |
| 2011 | while (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages && |
Xiao Guangrong | 77662e0 | 2010-04-16 16:34:42 +0800 | [diff] [blame] | 2012 | !list_empty(&kvm->arch.active_mmu_pages)) { |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 2013 | struct kvm_mmu_page *page; |
| 2014 | |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 2015 | page = container_of(kvm->arch.active_mmu_pages.prev, |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 2016 | struct kvm_mmu_page, link); |
Xiaotian Feng | 80b63fa | 2010-08-24 10:31:07 +0800 | [diff] [blame] | 2017 | kvm_mmu_prepare_zap_page(kvm, page, &invalid_list); |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 2018 | } |
Xiao Guangrong | aa6bd18 | 2011-07-12 03:26:40 +0800 | [diff] [blame] | 2019 | kvm_mmu_commit_zap_page(kvm, &invalid_list); |
Dave Hansen | 49d5ca2 | 2010-08-19 18:11:28 -0700 | [diff] [blame] | 2020 | goal_nr_mmu_pages = kvm->arch.n_used_mmu_pages; |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 2021 | } |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 2022 | |
Dave Hansen | 49d5ca2 | 2010-08-19 18:11:28 -0700 | [diff] [blame] | 2023 | kvm->arch.n_max_mmu_pages = goal_nr_mmu_pages; |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 2024 | } |
| 2025 | |
Xiao Guangrong | 1cb3f3a | 2011-09-22 17:02:48 +0800 | [diff] [blame] | 2026 | int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn) |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 2027 | { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2028 | struct kvm_mmu_page *sp; |
Xiao Guangrong | f41d335 | 2010-06-04 21:56:11 +0800 | [diff] [blame] | 2029 | struct hlist_node *node; |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 2030 | LIST_HEAD(invalid_list); |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 2031 | int r; |
| 2032 | |
Xiao Guangrong | 9ad17b10 | 2010-08-28 19:19:42 +0800 | [diff] [blame] | 2033 | pgprintk("%s: looking for gfn %llx\n", __func__, gfn); |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 2034 | r = 0; |
Xiao Guangrong | 1cb3f3a | 2011-09-22 17:02:48 +0800 | [diff] [blame] | 2035 | spin_lock(&kvm->mmu_lock); |
Xiao Guangrong | f41d335 | 2010-06-04 21:56:11 +0800 | [diff] [blame] | 2036 | for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) { |
Xiao Guangrong | 9ad17b10 | 2010-08-28 19:19:42 +0800 | [diff] [blame] | 2037 | pgprintk("%s: gfn %llx role %x\n", __func__, gfn, |
Xiao Guangrong | 7ae680e | 2010-06-04 21:53:07 +0800 | [diff] [blame] | 2038 | sp->role.word); |
| 2039 | r = 1; |
Xiao Guangrong | f41d335 | 2010-06-04 21:56:11 +0800 | [diff] [blame] | 2040 | kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list); |
Xiao Guangrong | 7ae680e | 2010-06-04 21:53:07 +0800 | [diff] [blame] | 2041 | } |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 2042 | kvm_mmu_commit_zap_page(kvm, &invalid_list); |
Xiao Guangrong | 1cb3f3a | 2011-09-22 17:02:48 +0800 | [diff] [blame] | 2043 | spin_unlock(&kvm->mmu_lock); |
| 2044 | |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 2045 | return r; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 2046 | } |
Xiao Guangrong | 1cb3f3a | 2011-09-22 17:02:48 +0800 | [diff] [blame] | 2047 | EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page); |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 2048 | |
Avi Kivity | 38c335f | 2007-11-21 14:20:22 +0200 | [diff] [blame] | 2049 | 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] | 2050 | { |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 2051 | int slot = memslot_id(kvm, gfn); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2052 | struct kvm_mmu_page *sp = page_header(__pa(pte)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2053 | |
Sheng Yang | 291f26b | 2008-10-16 17:30:57 +0800 | [diff] [blame] | 2054 | __set_bit(slot, sp->slot_bitmap); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2055 | } |
| 2056 | |
Sheng Yang | 74be52e | 2008-10-09 16:01:56 +0800 | [diff] [blame] | 2057 | /* |
| 2058 | * The function is based on mtrr_type_lookup() in |
| 2059 | * arch/x86/kernel/cpu/mtrr/generic.c |
| 2060 | */ |
| 2061 | static int get_mtrr_type(struct mtrr_state_type *mtrr_state, |
| 2062 | u64 start, u64 end) |
| 2063 | { |
| 2064 | int i; |
| 2065 | u64 base, mask; |
| 2066 | u8 prev_match, curr_match; |
| 2067 | int num_var_ranges = KVM_NR_VAR_MTRR; |
| 2068 | |
| 2069 | if (!mtrr_state->enabled) |
| 2070 | return 0xFF; |
| 2071 | |
| 2072 | /* Make end inclusive end, instead of exclusive */ |
| 2073 | end--; |
| 2074 | |
| 2075 | /* Look in fixed ranges. Just return the type as per start */ |
| 2076 | if (mtrr_state->have_fixed && (start < 0x100000)) { |
| 2077 | int idx; |
| 2078 | |
| 2079 | if (start < 0x80000) { |
| 2080 | idx = 0; |
| 2081 | idx += (start >> 16); |
| 2082 | return mtrr_state->fixed_ranges[idx]; |
| 2083 | } else if (start < 0xC0000) { |
| 2084 | idx = 1 * 8; |
| 2085 | idx += ((start - 0x80000) >> 14); |
| 2086 | return mtrr_state->fixed_ranges[idx]; |
| 2087 | } else if (start < 0x1000000) { |
| 2088 | idx = 3 * 8; |
| 2089 | idx += ((start - 0xC0000) >> 12); |
| 2090 | return mtrr_state->fixed_ranges[idx]; |
| 2091 | } |
| 2092 | } |
| 2093 | |
| 2094 | /* |
| 2095 | * Look in variable ranges |
| 2096 | * Look of multiple ranges matching this address and pick type |
| 2097 | * as per MTRR precedence |
| 2098 | */ |
| 2099 | if (!(mtrr_state->enabled & 2)) |
| 2100 | return mtrr_state->def_type; |
| 2101 | |
| 2102 | prev_match = 0xFF; |
| 2103 | for (i = 0; i < num_var_ranges; ++i) { |
| 2104 | unsigned short start_state, end_state; |
| 2105 | |
| 2106 | if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11))) |
| 2107 | continue; |
| 2108 | |
| 2109 | base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) + |
| 2110 | (mtrr_state->var_ranges[i].base_lo & PAGE_MASK); |
| 2111 | mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) + |
| 2112 | (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK); |
| 2113 | |
| 2114 | start_state = ((start & mask) == (base & mask)); |
| 2115 | end_state = ((end & mask) == (base & mask)); |
| 2116 | if (start_state != end_state) |
| 2117 | return 0xFE; |
| 2118 | |
| 2119 | if ((start & mask) != (base & mask)) |
| 2120 | continue; |
| 2121 | |
| 2122 | curr_match = mtrr_state->var_ranges[i].base_lo & 0xff; |
| 2123 | if (prev_match == 0xFF) { |
| 2124 | prev_match = curr_match; |
| 2125 | continue; |
| 2126 | } |
| 2127 | |
| 2128 | if (prev_match == MTRR_TYPE_UNCACHABLE || |
| 2129 | curr_match == MTRR_TYPE_UNCACHABLE) |
| 2130 | return MTRR_TYPE_UNCACHABLE; |
| 2131 | |
| 2132 | if ((prev_match == MTRR_TYPE_WRBACK && |
| 2133 | curr_match == MTRR_TYPE_WRTHROUGH) || |
| 2134 | (prev_match == MTRR_TYPE_WRTHROUGH && |
| 2135 | curr_match == MTRR_TYPE_WRBACK)) { |
| 2136 | prev_match = MTRR_TYPE_WRTHROUGH; |
| 2137 | curr_match = MTRR_TYPE_WRTHROUGH; |
| 2138 | } |
| 2139 | |
| 2140 | if (prev_match != curr_match) |
| 2141 | return MTRR_TYPE_UNCACHABLE; |
| 2142 | } |
| 2143 | |
| 2144 | if (prev_match != 0xFF) |
| 2145 | return prev_match; |
| 2146 | |
| 2147 | return mtrr_state->def_type; |
| 2148 | } |
| 2149 | |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 2150 | u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn) |
Sheng Yang | 74be52e | 2008-10-09 16:01:56 +0800 | [diff] [blame] | 2151 | { |
| 2152 | u8 mtrr; |
| 2153 | |
| 2154 | mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT, |
| 2155 | (gfn << PAGE_SHIFT) + PAGE_SIZE); |
| 2156 | if (mtrr == 0xfe || mtrr == 0xff) |
| 2157 | mtrr = MTRR_TYPE_WRBACK; |
| 2158 | return mtrr; |
| 2159 | } |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 2160 | EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type); |
Sheng Yang | 74be52e | 2008-10-09 16:01:56 +0800 | [diff] [blame] | 2161 | |
Xiao Guangrong | 9cf5cf5 | 2010-05-24 15:40:07 +0800 | [diff] [blame] | 2162 | static void __kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp) |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 2163 | { |
Xiao Guangrong | 5e1b3dd | 2010-04-28 11:55:06 +0800 | [diff] [blame] | 2164 | trace_kvm_mmu_unsync_page(sp); |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 2165 | ++vcpu->kvm->stat.mmu_unsync; |
| 2166 | sp->unsync = 1; |
Marcelo Tosatti | 6cffe8c | 2008-12-01 22:32:04 -0200 | [diff] [blame] | 2167 | |
Xiao Guangrong | 6b18493 | 2010-04-16 21:29:17 +0800 | [diff] [blame] | 2168 | kvm_mmu_mark_parents_unsync(sp); |
Xiao Guangrong | 9cf5cf5 | 2010-05-24 15:40:07 +0800 | [diff] [blame] | 2169 | } |
| 2170 | |
| 2171 | static void kvm_unsync_pages(struct kvm_vcpu *vcpu, gfn_t gfn) |
| 2172 | { |
Xiao Guangrong | 9cf5cf5 | 2010-05-24 15:40:07 +0800 | [diff] [blame] | 2173 | struct kvm_mmu_page *s; |
Xiao Guangrong | f41d335 | 2010-06-04 21:56:11 +0800 | [diff] [blame] | 2174 | struct hlist_node *node; |
Xiao Guangrong | 9cf5cf5 | 2010-05-24 15:40:07 +0800 | [diff] [blame] | 2175 | |
Xiao Guangrong | f41d335 | 2010-06-04 21:56:11 +0800 | [diff] [blame] | 2176 | for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) { |
Xiao Guangrong | 7ae680e | 2010-06-04 21:53:07 +0800 | [diff] [blame] | 2177 | if (s->unsync) |
Xiao Guangrong | 9cf5cf5 | 2010-05-24 15:40:07 +0800 | [diff] [blame] | 2178 | continue; |
| 2179 | WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL); |
| 2180 | __kvm_unsync_page(vcpu, s); |
| 2181 | } |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 2182 | } |
| 2183 | |
| 2184 | static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn, |
| 2185 | bool can_unsync) |
| 2186 | { |
Xiao Guangrong | 9cf5cf5 | 2010-05-24 15:40:07 +0800 | [diff] [blame] | 2187 | struct kvm_mmu_page *s; |
Xiao Guangrong | f41d335 | 2010-06-04 21:56:11 +0800 | [diff] [blame] | 2188 | struct hlist_node *node; |
Xiao Guangrong | 9cf5cf5 | 2010-05-24 15:40:07 +0800 | [diff] [blame] | 2189 | bool need_unsync = false; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 2190 | |
Xiao Guangrong | f41d335 | 2010-06-04 21:56:11 +0800 | [diff] [blame] | 2191 | for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) { |
Xiao Guangrong | 36a2e67 | 2010-06-30 16:02:02 +0800 | [diff] [blame] | 2192 | if (!can_unsync) |
| 2193 | return 1; |
| 2194 | |
Xiao Guangrong | 9cf5cf5 | 2010-05-24 15:40:07 +0800 | [diff] [blame] | 2195 | if (s->role.level != PT_PAGE_TABLE_LEVEL) |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 2196 | return 1; |
Xiao Guangrong | 9cf5cf5 | 2010-05-24 15:40:07 +0800 | [diff] [blame] | 2197 | |
| 2198 | if (!need_unsync && !s->unsync) { |
Xiao Guangrong | 36a2e67 | 2010-06-30 16:02:02 +0800 | [diff] [blame] | 2199 | if (!oos_shadow) |
Xiao Guangrong | 9cf5cf5 | 2010-05-24 15:40:07 +0800 | [diff] [blame] | 2200 | return 1; |
| 2201 | need_unsync = true; |
| 2202 | } |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 2203 | } |
Xiao Guangrong | 9cf5cf5 | 2010-05-24 15:40:07 +0800 | [diff] [blame] | 2204 | if (need_unsync) |
| 2205 | kvm_unsync_pages(vcpu, gfn); |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 2206 | return 0; |
| 2207 | } |
| 2208 | |
Avi Kivity | d555c33 | 2009-06-10 14:24:23 +0300 | [diff] [blame] | 2209 | static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep, |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 2210 | unsigned pte_access, int user_fault, |
Xiao Guangrong | 640d9b0 | 2011-07-12 03:24:39 +0800 | [diff] [blame] | 2211 | int write_fault, int level, |
Marcelo Tosatti | c2d0ee4 | 2009-04-05 14:54:47 -0300 | [diff] [blame] | 2212 | gfn_t gfn, pfn_t pfn, bool speculative, |
Lai Jiangshan | 9bdbba1 | 2010-11-19 17:03:22 +0800 | [diff] [blame] | 2213 | bool can_unsync, bool host_writable) |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 2214 | { |
Xiao Guangrong | b330aa0 | 2010-11-19 17:02:35 +0800 | [diff] [blame] | 2215 | u64 spte, entry = *sptep; |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 2216 | int ret = 0; |
Sheng Yang | 64d4d52 | 2008-10-09 16:01:57 +0800 | [diff] [blame] | 2217 | |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 2218 | if (set_mmio_spte(sptep, gfn, pfn, pte_access)) |
| 2219 | return 0; |
| 2220 | |
Marcelo Tosatti | 982c256 | 2010-10-22 14:18:16 -0200 | [diff] [blame] | 2221 | spte = PT_PRESENT_MASK; |
Avi Kivity | 947da53 | 2008-03-18 11:05:52 +0200 | [diff] [blame] | 2222 | if (!speculative) |
Avi Kivity | 3201b5d | 2008-08-27 20:01:04 +0300 | [diff] [blame] | 2223 | spte |= shadow_accessed_mask; |
Xiao Guangrong | 640d9b0 | 2011-07-12 03:24:39 +0800 | [diff] [blame] | 2224 | |
Sheng Yang | 7b52345 | 2008-04-25 21:13:50 +0800 | [diff] [blame] | 2225 | if (pte_access & ACC_EXEC_MASK) |
| 2226 | spte |= shadow_x_mask; |
| 2227 | else |
| 2228 | spte |= shadow_nx_mask; |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 2229 | if (pte_access & ACC_USER_MASK) |
Sheng Yang | 7b52345 | 2008-04-25 21:13:50 +0800 | [diff] [blame] | 2230 | spte |= shadow_user_mask; |
Joerg Roedel | 852e3c1 | 2009-07-27 16:30:44 +0200 | [diff] [blame] | 2231 | if (level > PT_PAGE_TABLE_LEVEL) |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 2232 | spte |= PT_PAGE_SIZE_MASK; |
Avi Kivity | b0bc3ee | 2010-09-13 16:45:28 +0200 | [diff] [blame] | 2233 | if (tdp_enabled) |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 2234 | spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn, |
| 2235 | kvm_is_mmio_pfn(pfn)); |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 2236 | |
Lai Jiangshan | 9bdbba1 | 2010-11-19 17:03:22 +0800 | [diff] [blame] | 2237 | if (host_writable) |
Izik Eidus | 1403283 | 2009-09-23 21:47:17 +0300 | [diff] [blame] | 2238 | spte |= SPTE_HOST_WRITEABLE; |
Xiao Guangrong | f8e453b | 2010-12-23 16:09:29 +0800 | [diff] [blame] | 2239 | else |
| 2240 | pte_access &= ~ACC_WRITE_MASK; |
Izik Eidus | 1403283 | 2009-09-23 21:47:17 +0300 | [diff] [blame] | 2241 | |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 2242 | spte |= (u64)pfn << PAGE_SHIFT; |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 2243 | |
| 2244 | if ((pte_access & ACC_WRITE_MASK) |
Joerg Roedel | c5a78f2b | 2010-09-10 17:30:39 +0200 | [diff] [blame] | 2245 | || (!vcpu->arch.mmu.direct_map && write_fault |
| 2246 | && !is_write_protection(vcpu) && !user_fault)) { |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 2247 | |
Joerg Roedel | 852e3c1 | 2009-07-27 16:30:44 +0200 | [diff] [blame] | 2248 | if (level > PT_PAGE_TABLE_LEVEL && |
| 2249 | has_wrprotected_page(vcpu->kvm, gfn, level)) { |
Marcelo Tosatti | 38187c8 | 2008-09-23 13:18:32 -0300 | [diff] [blame] | 2250 | ret = 1; |
Xiao Guangrong | c370795 | 2011-07-12 03:28:04 +0800 | [diff] [blame] | 2251 | drop_spte(vcpu->kvm, sptep); |
Avi Kivity | be38d27 | 2010-06-06 14:31:27 +0300 | [diff] [blame] | 2252 | goto done; |
Marcelo Tosatti | 38187c8 | 2008-09-23 13:18:32 -0300 | [diff] [blame] | 2253 | } |
| 2254 | |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 2255 | spte |= PT_WRITABLE_MASK; |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 2256 | |
Joerg Roedel | c5a78f2b | 2010-09-10 17:30:39 +0200 | [diff] [blame] | 2257 | if (!vcpu->arch.mmu.direct_map |
Avi Kivity | 411c588 | 2011-06-06 16:11:54 +0300 | [diff] [blame] | 2258 | && !(pte_access & ACC_WRITE_MASK)) { |
Avi Kivity | 69325a1 | 2010-05-27 14:35:58 +0300 | [diff] [blame] | 2259 | spte &= ~PT_USER_MASK; |
Avi Kivity | 411c588 | 2011-06-06 16:11:54 +0300 | [diff] [blame] | 2260 | /* |
| 2261 | * If we converted a user page to a kernel page, |
| 2262 | * so that the kernel can write to it when cr0.wp=0, |
| 2263 | * then we should prevent the kernel from executing it |
| 2264 | * if SMEP is enabled. |
| 2265 | */ |
| 2266 | if (kvm_read_cr4_bits(vcpu, X86_CR4_SMEP)) |
| 2267 | spte |= PT64_NX_MASK; |
| 2268 | } |
Avi Kivity | 69325a1 | 2010-05-27 14:35:58 +0300 | [diff] [blame] | 2269 | |
Marcelo Tosatti | ecc5589 | 2008-11-25 15:58:07 +0100 | [diff] [blame] | 2270 | /* |
| 2271 | * Optimization: for pte sync, if spte was writable the hash |
| 2272 | * lookup is unnecessary (and expensive). Write protection |
| 2273 | * is responsibility of mmu_get_page / kvm_sync_page. |
| 2274 | * Same reasoning can be applied to dirty page accounting. |
| 2275 | */ |
Takuya Yoshikawa | 8dae444 | 2010-01-18 18:45:10 +0900 | [diff] [blame] | 2276 | if (!can_unsync && is_writable_pte(*sptep)) |
Marcelo Tosatti | ecc5589 | 2008-11-25 15:58:07 +0100 | [diff] [blame] | 2277 | goto set_pte; |
| 2278 | |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 2279 | if (mmu_need_write_protect(vcpu, gfn, can_unsync)) { |
Xiao Guangrong | 9ad17b10 | 2010-08-28 19:19:42 +0800 | [diff] [blame] | 2280 | pgprintk("%s: found shadow page for %llx, marking ro\n", |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 2281 | __func__, gfn); |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 2282 | ret = 1; |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 2283 | pte_access &= ~ACC_WRITE_MASK; |
Takuya Yoshikawa | 8dae444 | 2010-01-18 18:45:10 +0900 | [diff] [blame] | 2284 | if (is_writable_pte(spte)) |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 2285 | spte &= ~PT_WRITABLE_MASK; |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 2286 | } |
| 2287 | } |
| 2288 | |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 2289 | if (pte_access & ACC_WRITE_MASK) |
| 2290 | mark_page_dirty(vcpu->kvm, gfn); |
| 2291 | |
Marcelo Tosatti | 38187c8 | 2008-09-23 13:18:32 -0300 | [diff] [blame] | 2292 | set_pte: |
Xiao Guangrong | 1df9f2d | 2011-07-12 03:30:35 +0800 | [diff] [blame] | 2293 | mmu_spte_update(sptep, spte); |
Xiao Guangrong | b330aa0 | 2010-11-19 17:02:35 +0800 | [diff] [blame] | 2294 | /* |
| 2295 | * If we overwrite a writable spte with a read-only one we |
| 2296 | * should flush remote TLBs. Otherwise rmap_write_protect |
| 2297 | * will find a read-only spte, even though the writable spte |
| 2298 | * might be cached on a CPU's TLB. |
| 2299 | */ |
| 2300 | if (is_writable_pte(entry) && !is_writable_pte(*sptep)) |
| 2301 | kvm_flush_remote_tlbs(vcpu->kvm); |
Avi Kivity | be38d27 | 2010-06-06 14:31:27 +0300 | [diff] [blame] | 2302 | done: |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 2303 | return ret; |
| 2304 | } |
| 2305 | |
Avi Kivity | d555c33 | 2009-06-10 14:24:23 +0300 | [diff] [blame] | 2306 | static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep, |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 2307 | unsigned pt_access, unsigned pte_access, |
Xiao Guangrong | 640d9b0 | 2011-07-12 03:24:39 +0800 | [diff] [blame] | 2308 | int user_fault, int write_fault, |
Xiao Guangrong | b90a0e6 | 2011-07-12 03:25:56 +0800 | [diff] [blame] | 2309 | int *emulate, int level, gfn_t gfn, |
Izik Eidus | 1403283 | 2009-09-23 21:47:17 +0300 | [diff] [blame] | 2310 | pfn_t pfn, bool speculative, |
Lai Jiangshan | 9bdbba1 | 2010-11-19 17:03:22 +0800 | [diff] [blame] | 2311 | bool host_writable) |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 2312 | { |
| 2313 | int was_rmapped = 0; |
Marcelo Tosatti | 53a27b3 | 2009-08-05 15:43:58 -0300 | [diff] [blame] | 2314 | int rmap_count; |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 2315 | |
| 2316 | pgprintk("%s: spte %llx access %x write_fault %d" |
Xiao Guangrong | 9ad17b10 | 2010-08-28 19:19:42 +0800 | [diff] [blame] | 2317 | " user_fault %d gfn %llx\n", |
Avi Kivity | d555c33 | 2009-06-10 14:24:23 +0300 | [diff] [blame] | 2318 | __func__, *sptep, pt_access, |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 2319 | write_fault, user_fault, gfn); |
| 2320 | |
Avi Kivity | d555c33 | 2009-06-10 14:24:23 +0300 | [diff] [blame] | 2321 | if (is_rmap_spte(*sptep)) { |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 2322 | /* |
| 2323 | * If we overwrite a PTE page pointer with a 2MB PMD, unlink |
| 2324 | * the parent of the now unreachable PTE. |
| 2325 | */ |
Joerg Roedel | 852e3c1 | 2009-07-27 16:30:44 +0200 | [diff] [blame] | 2326 | if (level > PT_PAGE_TABLE_LEVEL && |
| 2327 | !is_large_pte(*sptep)) { |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 2328 | struct kvm_mmu_page *child; |
Avi Kivity | d555c33 | 2009-06-10 14:24:23 +0300 | [diff] [blame] | 2329 | u64 pte = *sptep; |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 2330 | |
| 2331 | child = page_header(pte & PT64_BASE_ADDR_MASK); |
Xiao Guangrong | bcdd9a9 | 2011-05-15 23:28:29 +0800 | [diff] [blame] | 2332 | drop_parent_pte(child, sptep); |
Marcelo Tosatti | 3be2264 | 2010-05-28 09:44:59 -0300 | [diff] [blame] | 2333 | kvm_flush_remote_tlbs(vcpu->kvm); |
Avi Kivity | d555c33 | 2009-06-10 14:24:23 +0300 | [diff] [blame] | 2334 | } else if (pfn != spte_to_pfn(*sptep)) { |
Xiao Guangrong | 9ad17b10 | 2010-08-28 19:19:42 +0800 | [diff] [blame] | 2335 | pgprintk("hfn old %llx new %llx\n", |
Avi Kivity | d555c33 | 2009-06-10 14:24:23 +0300 | [diff] [blame] | 2336 | spte_to_pfn(*sptep), pfn); |
Xiao Guangrong | c370795 | 2011-07-12 03:28:04 +0800 | [diff] [blame] | 2337 | drop_spte(vcpu->kvm, sptep); |
Xiao Guangrong | 9154635 | 2010-06-30 16:04:06 +0800 | [diff] [blame] | 2338 | kvm_flush_remote_tlbs(vcpu->kvm); |
Joerg Roedel | 6bed6b9 | 2009-02-18 14:08:59 +0100 | [diff] [blame] | 2339 | } else |
| 2340 | was_rmapped = 1; |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 2341 | } |
Joerg Roedel | 852e3c1 | 2009-07-27 16:30:44 +0200 | [diff] [blame] | 2342 | |
Avi Kivity | d555c33 | 2009-06-10 14:24:23 +0300 | [diff] [blame] | 2343 | if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault, |
Xiao Guangrong | 640d9b0 | 2011-07-12 03:24:39 +0800 | [diff] [blame] | 2344 | level, gfn, pfn, speculative, true, |
Lai Jiangshan | 9bdbba1 | 2010-11-19 17:03:22 +0800 | [diff] [blame] | 2345 | host_writable)) { |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 2346 | if (write_fault) |
Xiao Guangrong | b90a0e6 | 2011-07-12 03:25:56 +0800 | [diff] [blame] | 2347 | *emulate = 1; |
Xiao Guangrong | 5304efd | 2010-06-08 20:05:57 +0800 | [diff] [blame] | 2348 | kvm_mmu_flush_tlb(vcpu); |
Marcelo Tosatti | a378b4e | 2008-09-23 13:18:31 -0300 | [diff] [blame] | 2349 | } |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 2350 | |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 2351 | if (unlikely(is_mmio_spte(*sptep) && emulate)) |
| 2352 | *emulate = 1; |
| 2353 | |
Avi Kivity | d555c33 | 2009-06-10 14:24:23 +0300 | [diff] [blame] | 2354 | pgprintk("%s: setting spte %llx\n", __func__, *sptep); |
Xiao Guangrong | 9ad17b10 | 2010-08-28 19:19:42 +0800 | [diff] [blame] | 2355 | pgprintk("instantiating %s PTE (%s) at %llx (%llx) addr %p\n", |
Avi Kivity | d555c33 | 2009-06-10 14:24:23 +0300 | [diff] [blame] | 2356 | is_large_pte(*sptep)? "2MB" : "4kB", |
Joerg Roedel | a205bc1 | 2009-07-09 16:36:01 +0200 | [diff] [blame] | 2357 | *sptep & PT_PRESENT_MASK ?"RW":"R", gfn, |
| 2358 | *sptep, sptep); |
Avi Kivity | d555c33 | 2009-06-10 14:24:23 +0300 | [diff] [blame] | 2359 | if (!was_rmapped && is_large_pte(*sptep)) |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 2360 | ++vcpu->kvm->stat.lpages; |
| 2361 | |
Xiao Guangrong | ffb61bb | 2011-07-12 03:22:01 +0800 | [diff] [blame] | 2362 | if (is_shadow_present_pte(*sptep)) { |
| 2363 | page_header_update_slot(vcpu->kvm, sptep, gfn); |
| 2364 | if (!was_rmapped) { |
| 2365 | rmap_count = rmap_add(vcpu, sptep, gfn); |
| 2366 | if (rmap_count > RMAP_RECYCLE_THRESHOLD) |
| 2367 | rmap_recycle(vcpu, sptep, gfn); |
| 2368 | } |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 2369 | } |
Xiao Guangrong | 9ed5520 | 2010-07-16 11:25:17 +0800 | [diff] [blame] | 2370 | kvm_release_pfn_clean(pfn); |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 2371 | } |
| 2372 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2373 | static void nonpaging_new_cr3(struct kvm_vcpu *vcpu) |
| 2374 | { |
| 2375 | } |
| 2376 | |
Xiao Guangrong | 957ed9e | 2010-08-22 19:12:48 +0800 | [diff] [blame] | 2377 | static pfn_t pte_prefetch_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn, |
| 2378 | bool no_dirty_log) |
| 2379 | { |
| 2380 | struct kvm_memory_slot *slot; |
| 2381 | unsigned long hva; |
| 2382 | |
Xiao Guangrong | 5d163b1 | 2011-03-09 15:43:00 +0800 | [diff] [blame] | 2383 | slot = gfn_to_memslot_dirty_bitmap(vcpu, gfn, no_dirty_log); |
Xiao Guangrong | 957ed9e | 2010-08-22 19:12:48 +0800 | [diff] [blame] | 2384 | if (!slot) { |
Xiao Guangrong | fce92dc | 2011-07-12 03:28:54 +0800 | [diff] [blame] | 2385 | get_page(fault_page); |
| 2386 | return page_to_pfn(fault_page); |
Xiao Guangrong | 957ed9e | 2010-08-22 19:12:48 +0800 | [diff] [blame] | 2387 | } |
| 2388 | |
| 2389 | hva = gfn_to_hva_memslot(slot, gfn); |
| 2390 | |
| 2391 | return hva_to_pfn_atomic(vcpu->kvm, hva); |
| 2392 | } |
| 2393 | |
| 2394 | static int direct_pte_prefetch_many(struct kvm_vcpu *vcpu, |
| 2395 | struct kvm_mmu_page *sp, |
| 2396 | u64 *start, u64 *end) |
| 2397 | { |
| 2398 | struct page *pages[PTE_PREFETCH_NUM]; |
| 2399 | unsigned access = sp->role.access; |
| 2400 | int i, ret; |
| 2401 | gfn_t gfn; |
| 2402 | |
| 2403 | gfn = kvm_mmu_page_get_gfn(sp, start - sp->spt); |
Xiao Guangrong | 5d163b1 | 2011-03-09 15:43:00 +0800 | [diff] [blame] | 2404 | if (!gfn_to_memslot_dirty_bitmap(vcpu, gfn, access & ACC_WRITE_MASK)) |
Xiao Guangrong | 957ed9e | 2010-08-22 19:12:48 +0800 | [diff] [blame] | 2405 | return -1; |
| 2406 | |
| 2407 | ret = gfn_to_page_many_atomic(vcpu->kvm, gfn, pages, end - start); |
| 2408 | if (ret <= 0) |
| 2409 | return -1; |
| 2410 | |
| 2411 | for (i = 0; i < ret; i++, gfn++, start++) |
| 2412 | mmu_set_spte(vcpu, start, ACC_ALL, |
Xiao Guangrong | 640d9b0 | 2011-07-12 03:24:39 +0800 | [diff] [blame] | 2413 | access, 0, 0, NULL, |
Xiao Guangrong | 957ed9e | 2010-08-22 19:12:48 +0800 | [diff] [blame] | 2414 | sp->role.level, gfn, |
| 2415 | page_to_pfn(pages[i]), true, true); |
| 2416 | |
| 2417 | return 0; |
| 2418 | } |
| 2419 | |
| 2420 | static void __direct_pte_prefetch(struct kvm_vcpu *vcpu, |
| 2421 | struct kvm_mmu_page *sp, u64 *sptep) |
| 2422 | { |
| 2423 | u64 *spte, *start = NULL; |
| 2424 | int i; |
| 2425 | |
| 2426 | WARN_ON(!sp->role.direct); |
| 2427 | |
| 2428 | i = (sptep - sp->spt) & ~(PTE_PREFETCH_NUM - 1); |
| 2429 | spte = sp->spt + i; |
| 2430 | |
| 2431 | for (i = 0; i < PTE_PREFETCH_NUM; i++, spte++) { |
Xiao Guangrong | c370795 | 2011-07-12 03:28:04 +0800 | [diff] [blame] | 2432 | if (is_shadow_present_pte(*spte) || spte == sptep) { |
Xiao Guangrong | 957ed9e | 2010-08-22 19:12:48 +0800 | [diff] [blame] | 2433 | if (!start) |
| 2434 | continue; |
| 2435 | if (direct_pte_prefetch_many(vcpu, sp, start, spte) < 0) |
| 2436 | break; |
| 2437 | start = NULL; |
| 2438 | } else if (!start) |
| 2439 | start = spte; |
| 2440 | } |
| 2441 | } |
| 2442 | |
| 2443 | static void direct_pte_prefetch(struct kvm_vcpu *vcpu, u64 *sptep) |
| 2444 | { |
| 2445 | struct kvm_mmu_page *sp; |
| 2446 | |
| 2447 | /* |
| 2448 | * Since it's no accessed bit on EPT, it's no way to |
| 2449 | * distinguish between actually accessed translations |
| 2450 | * and prefetched, so disable pte prefetch if EPT is |
| 2451 | * enabled. |
| 2452 | */ |
| 2453 | if (!shadow_accessed_mask) |
| 2454 | return; |
| 2455 | |
| 2456 | sp = page_header(__pa(sptep)); |
| 2457 | if (sp->role.level > PT_PAGE_TABLE_LEVEL) |
| 2458 | return; |
| 2459 | |
| 2460 | __direct_pte_prefetch(vcpu, sp, sptep); |
| 2461 | } |
| 2462 | |
Joerg Roedel | 4d9976b | 2008-02-07 13:47:42 +0100 | [diff] [blame] | 2463 | static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write, |
Xiao Guangrong | 2ec4739 | 2010-12-07 10:34:42 +0800 | [diff] [blame] | 2464 | int map_writable, int level, gfn_t gfn, pfn_t pfn, |
| 2465 | bool prefault) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2466 | { |
Avi Kivity | 9f652d21 | 2008-12-25 14:54:25 +0200 | [diff] [blame] | 2467 | struct kvm_shadow_walk_iterator iterator; |
| 2468 | struct kvm_mmu_page *sp; |
Xiao Guangrong | b90a0e6 | 2011-07-12 03:25:56 +0800 | [diff] [blame] | 2469 | int emulate = 0; |
Avi Kivity | 9f652d21 | 2008-12-25 14:54:25 +0200 | [diff] [blame] | 2470 | gfn_t pseudo_gfn; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2471 | |
Avi Kivity | 9f652d21 | 2008-12-25 14:54:25 +0200 | [diff] [blame] | 2472 | for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) { |
Joerg Roedel | 852e3c1 | 2009-07-27 16:30:44 +0200 | [diff] [blame] | 2473 | if (iterator.level == level) { |
Marcelo Tosatti | 612819c | 2010-10-22 14:18:18 -0200 | [diff] [blame] | 2474 | unsigned pte_access = ACC_ALL; |
| 2475 | |
Marcelo Tosatti | 612819c | 2010-10-22 14:18:18 -0200 | [diff] [blame] | 2476 | mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, pte_access, |
Xiao Guangrong | b90a0e6 | 2011-07-12 03:25:56 +0800 | [diff] [blame] | 2477 | 0, write, &emulate, |
Xiao Guangrong | 2ec4739 | 2010-12-07 10:34:42 +0800 | [diff] [blame] | 2478 | level, gfn, pfn, prefault, map_writable); |
Xiao Guangrong | 957ed9e | 2010-08-22 19:12:48 +0800 | [diff] [blame] | 2479 | direct_pte_prefetch(vcpu, iterator.sptep); |
Avi Kivity | 9f652d21 | 2008-12-25 14:54:25 +0200 | [diff] [blame] | 2480 | ++vcpu->stat.pf_fixed; |
| 2481 | break; |
| 2482 | } |
| 2483 | |
Xiao Guangrong | c370795 | 2011-07-12 03:28:04 +0800 | [diff] [blame] | 2484 | if (!is_shadow_present_pte(*iterator.sptep)) { |
Lai Jiangshan | c9fa0b3 | 2010-05-26 16:48:25 +0800 | [diff] [blame] | 2485 | u64 base_addr = iterator.addr; |
| 2486 | |
| 2487 | base_addr &= PT64_LVL_ADDR_MASK(iterator.level); |
| 2488 | pseudo_gfn = base_addr >> PAGE_SHIFT; |
Avi Kivity | 9f652d21 | 2008-12-25 14:54:25 +0200 | [diff] [blame] | 2489 | sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr, |
| 2490 | iterator.level - 1, |
| 2491 | 1, ACC_ALL, iterator.sptep); |
| 2492 | if (!sp) { |
| 2493 | pgprintk("nonpaging_map: ENOMEM\n"); |
| 2494 | kvm_release_pfn_clean(pfn); |
| 2495 | return -ENOMEM; |
| 2496 | } |
| 2497 | |
Xiao Guangrong | 1df9f2d | 2011-07-12 03:30:35 +0800 | [diff] [blame] | 2498 | mmu_spte_set(iterator.sptep, |
| 2499 | __pa(sp->spt) |
| 2500 | | PT_PRESENT_MASK | PT_WRITABLE_MASK |
| 2501 | | shadow_user_mask | shadow_x_mask |
| 2502 | | shadow_accessed_mask); |
Avi Kivity | 9f652d21 | 2008-12-25 14:54:25 +0200 | [diff] [blame] | 2503 | } |
| 2504 | } |
Xiao Guangrong | b90a0e6 | 2011-07-12 03:25:56 +0800 | [diff] [blame] | 2505 | return emulate; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2506 | } |
| 2507 | |
Huang Ying | 77db5cb | 2010-10-08 16:24:15 +0800 | [diff] [blame] | 2508 | static void kvm_send_hwpoison_signal(unsigned long address, struct task_struct *tsk) |
Huang Ying | bf99815 | 2010-05-31 14:28:19 +0800 | [diff] [blame] | 2509 | { |
Huang Ying | 77db5cb | 2010-10-08 16:24:15 +0800 | [diff] [blame] | 2510 | siginfo_t info; |
Huang Ying | bf99815 | 2010-05-31 14:28:19 +0800 | [diff] [blame] | 2511 | |
Huang Ying | 77db5cb | 2010-10-08 16:24:15 +0800 | [diff] [blame] | 2512 | info.si_signo = SIGBUS; |
| 2513 | info.si_errno = 0; |
| 2514 | info.si_code = BUS_MCEERR_AR; |
| 2515 | info.si_addr = (void __user *)address; |
| 2516 | info.si_addr_lsb = PAGE_SHIFT; |
| 2517 | |
| 2518 | send_sig_info(SIGBUS, &info, tsk); |
Huang Ying | bf99815 | 2010-05-31 14:28:19 +0800 | [diff] [blame] | 2519 | } |
| 2520 | |
Xiao Guangrong | d7c5520 | 2011-07-12 03:29:38 +0800 | [diff] [blame] | 2521 | static int kvm_handle_bad_page(struct kvm_vcpu *vcpu, gfn_t gfn, pfn_t pfn) |
Huang Ying | bf99815 | 2010-05-31 14:28:19 +0800 | [diff] [blame] | 2522 | { |
| 2523 | kvm_release_pfn_clean(pfn); |
| 2524 | if (is_hwpoison_pfn(pfn)) { |
Xiao Guangrong | bebb106 | 2011-07-12 03:23:20 +0800 | [diff] [blame] | 2525 | kvm_send_hwpoison_signal(gfn_to_hva(vcpu->kvm, gfn), current); |
Huang Ying | bf99815 | 2010-05-31 14:28:19 +0800 | [diff] [blame] | 2526 | return 0; |
Xiao Guangrong | d7c5520 | 2011-07-12 03:29:38 +0800 | [diff] [blame] | 2527 | } |
Gleb Natapov | edba23e | 2010-07-07 20:16:45 +0300 | [diff] [blame] | 2528 | |
Xiao Guangrong | d7c5520 | 2011-07-12 03:29:38 +0800 | [diff] [blame] | 2529 | return -EFAULT; |
Huang Ying | bf99815 | 2010-05-31 14:28:19 +0800 | [diff] [blame] | 2530 | } |
| 2531 | |
Andrea Arcangeli | 936a5fe | 2011-01-13 15:46:48 -0800 | [diff] [blame] | 2532 | static void transparent_hugepage_adjust(struct kvm_vcpu *vcpu, |
| 2533 | gfn_t *gfnp, pfn_t *pfnp, int *levelp) |
| 2534 | { |
| 2535 | pfn_t pfn = *pfnp; |
| 2536 | gfn_t gfn = *gfnp; |
| 2537 | int level = *levelp; |
| 2538 | |
| 2539 | /* |
| 2540 | * Check if it's a transparent hugepage. If this would be an |
| 2541 | * hugetlbfs page, level wouldn't be set to |
| 2542 | * PT_PAGE_TABLE_LEVEL and there would be no adjustment done |
| 2543 | * here. |
| 2544 | */ |
| 2545 | if (!is_error_pfn(pfn) && !kvm_is_mmio_pfn(pfn) && |
| 2546 | level == PT_PAGE_TABLE_LEVEL && |
| 2547 | PageTransCompound(pfn_to_page(pfn)) && |
| 2548 | !has_wrprotected_page(vcpu->kvm, gfn, PT_DIRECTORY_LEVEL)) { |
| 2549 | unsigned long mask; |
| 2550 | /* |
| 2551 | * mmu_notifier_retry was successful and we hold the |
| 2552 | * mmu_lock here, so the pmd can't become splitting |
| 2553 | * from under us, and in turn |
| 2554 | * __split_huge_page_refcount() can't run from under |
| 2555 | * us and we can safely transfer the refcount from |
| 2556 | * PG_tail to PG_head as we switch the pfn to tail to |
| 2557 | * head. |
| 2558 | */ |
| 2559 | *levelp = level = PT_DIRECTORY_LEVEL; |
| 2560 | mask = KVM_PAGES_PER_HPAGE(level) - 1; |
| 2561 | VM_BUG_ON((gfn & mask) != (pfn & mask)); |
| 2562 | if (pfn & mask) { |
| 2563 | gfn &= ~mask; |
| 2564 | *gfnp = gfn; |
| 2565 | kvm_release_pfn_clean(pfn); |
| 2566 | pfn &= ~mask; |
| 2567 | if (!get_page_unless_zero(pfn_to_page(pfn))) |
| 2568 | BUG(); |
| 2569 | *pfnp = pfn; |
| 2570 | } |
| 2571 | } |
| 2572 | } |
| 2573 | |
Xiao Guangrong | d7c5520 | 2011-07-12 03:29:38 +0800 | [diff] [blame] | 2574 | static bool mmu_invalid_pfn(pfn_t pfn) |
| 2575 | { |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 2576 | return unlikely(is_invalid_pfn(pfn)); |
Xiao Guangrong | d7c5520 | 2011-07-12 03:29:38 +0800 | [diff] [blame] | 2577 | } |
| 2578 | |
| 2579 | static bool handle_abnormal_pfn(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn, |
| 2580 | pfn_t pfn, unsigned access, int *ret_val) |
| 2581 | { |
| 2582 | bool ret = true; |
| 2583 | |
| 2584 | /* The pfn is invalid, report the error! */ |
| 2585 | if (unlikely(is_invalid_pfn(pfn))) { |
| 2586 | *ret_val = kvm_handle_bad_page(vcpu, gfn, pfn); |
| 2587 | goto exit; |
| 2588 | } |
| 2589 | |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 2590 | if (unlikely(is_noslot_pfn(pfn))) |
Xiao Guangrong | d7c5520 | 2011-07-12 03:29:38 +0800 | [diff] [blame] | 2591 | vcpu_cache_mmio_info(vcpu, gva, gfn, access); |
Xiao Guangrong | d7c5520 | 2011-07-12 03:29:38 +0800 | [diff] [blame] | 2592 | |
| 2593 | ret = false; |
| 2594 | exit: |
| 2595 | return ret; |
| 2596 | } |
| 2597 | |
Xiao Guangrong | 78b2c54 | 2010-12-07 10:48:06 +0800 | [diff] [blame] | 2598 | static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn, |
Xiao Guangrong | 060c2ab | 2010-11-12 14:49:11 +0800 | [diff] [blame] | 2599 | gva_t gva, pfn_t *pfn, bool write, bool *writable); |
| 2600 | |
| 2601 | static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn, |
Xiao Guangrong | 78b2c54 | 2010-12-07 10:48:06 +0800 | [diff] [blame] | 2602 | bool prefault) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2603 | { |
| 2604 | int r; |
Joerg Roedel | 852e3c1 | 2009-07-27 16:30:44 +0200 | [diff] [blame] | 2605 | int level; |
Andrea Arcangeli | 936a5fe | 2011-01-13 15:46:48 -0800 | [diff] [blame] | 2606 | int force_pt_level; |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 2607 | pfn_t pfn; |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 2608 | unsigned long mmu_seq; |
Marcelo Tosatti | 612819c | 2010-10-22 14:18:18 -0200 | [diff] [blame] | 2609 | bool map_writable; |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 2610 | |
Andrea Arcangeli | 936a5fe | 2011-01-13 15:46:48 -0800 | [diff] [blame] | 2611 | force_pt_level = mapping_level_dirty_bitmap(vcpu, gfn); |
| 2612 | if (likely(!force_pt_level)) { |
| 2613 | level = mapping_level(vcpu, gfn); |
| 2614 | /* |
| 2615 | * This path builds a PAE pagetable - so we can map |
| 2616 | * 2mb pages at maximum. Therefore check if the level |
| 2617 | * is larger than that. |
| 2618 | */ |
| 2619 | if (level > PT_DIRECTORY_LEVEL) |
| 2620 | level = PT_DIRECTORY_LEVEL; |
Joerg Roedel | 852e3c1 | 2009-07-27 16:30:44 +0200 | [diff] [blame] | 2621 | |
Andrea Arcangeli | 936a5fe | 2011-01-13 15:46:48 -0800 | [diff] [blame] | 2622 | gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1); |
| 2623 | } else |
| 2624 | level = PT_PAGE_TABLE_LEVEL; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 2625 | |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 2626 | mmu_seq = vcpu->kvm->mmu_notifier_seq; |
Marcelo Tosatti | 4c2155c | 2008-09-16 20:54:47 -0300 | [diff] [blame] | 2627 | smp_rmb(); |
Xiao Guangrong | 060c2ab | 2010-11-12 14:49:11 +0800 | [diff] [blame] | 2628 | |
Xiao Guangrong | 78b2c54 | 2010-12-07 10:48:06 +0800 | [diff] [blame] | 2629 | if (try_async_pf(vcpu, prefault, gfn, v, &pfn, write, &map_writable)) |
Xiao Guangrong | 060c2ab | 2010-11-12 14:49:11 +0800 | [diff] [blame] | 2630 | return 0; |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 2631 | |
Xiao Guangrong | d7c5520 | 2011-07-12 03:29:38 +0800 | [diff] [blame] | 2632 | if (handle_abnormal_pfn(vcpu, v, gfn, pfn, ACC_ALL, &r)) |
| 2633 | return r; |
Avi Kivity | d196e34 | 2008-01-24 11:44:11 +0200 | [diff] [blame] | 2634 | |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 2635 | spin_lock(&vcpu->kvm->mmu_lock); |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 2636 | if (mmu_notifier_retry(vcpu, mmu_seq)) |
| 2637 | goto out_unlock; |
Avi Kivity | eb787d1 | 2007-12-31 15:27:49 +0200 | [diff] [blame] | 2638 | kvm_mmu_free_some_pages(vcpu); |
Andrea Arcangeli | 936a5fe | 2011-01-13 15:46:48 -0800 | [diff] [blame] | 2639 | if (likely(!force_pt_level)) |
| 2640 | transparent_hugepage_adjust(vcpu, &gfn, &pfn, &level); |
Xiao Guangrong | 2ec4739 | 2010-12-07 10:34:42 +0800 | [diff] [blame] | 2641 | r = __direct_map(vcpu, v, write, map_writable, level, gfn, pfn, |
| 2642 | prefault); |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 2643 | spin_unlock(&vcpu->kvm->mmu_lock); |
| 2644 | |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 2645 | |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2646 | return r; |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 2647 | |
| 2648 | out_unlock: |
| 2649 | spin_unlock(&vcpu->kvm->mmu_lock); |
| 2650 | kvm_release_pfn_clean(pfn); |
| 2651 | return 0; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2652 | } |
| 2653 | |
| 2654 | |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2655 | static void mmu_free_roots(struct kvm_vcpu *vcpu) |
| 2656 | { |
| 2657 | int i; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2658 | struct kvm_mmu_page *sp; |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 2659 | LIST_HEAD(invalid_list); |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2660 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2661 | if (!VALID_PAGE(vcpu->arch.mmu.root_hpa)) |
Avi Kivity | 7b53aa5 | 2007-06-05 12:17:03 +0300 | [diff] [blame] | 2662 | return; |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 2663 | spin_lock(&vcpu->kvm->mmu_lock); |
Joerg Roedel | 81407ca | 2010-09-10 17:31:00 +0200 | [diff] [blame] | 2664 | if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL && |
| 2665 | (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL || |
| 2666 | vcpu->arch.mmu.direct_map)) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2667 | hpa_t root = vcpu->arch.mmu.root_hpa; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2668 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2669 | sp = page_header(root); |
| 2670 | --sp->root_count; |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 2671 | if (!sp->root_count && sp->role.invalid) { |
| 2672 | kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list); |
| 2673 | kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list); |
| 2674 | } |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2675 | vcpu->arch.mmu.root_hpa = INVALID_PAGE; |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 2676 | spin_unlock(&vcpu->kvm->mmu_lock); |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2677 | return; |
| 2678 | } |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2679 | for (i = 0; i < 4; ++i) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2680 | hpa_t root = vcpu->arch.mmu.pae_root[i]; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2681 | |
Avi Kivity | 417726a | 2007-04-12 17:35:58 +0300 | [diff] [blame] | 2682 | if (root) { |
Avi Kivity | 417726a | 2007-04-12 17:35:58 +0300 | [diff] [blame] | 2683 | root &= PT64_BASE_ADDR_MASK; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2684 | sp = page_header(root); |
| 2685 | --sp->root_count; |
Marcelo Tosatti | 2e53d63 | 2008-02-20 14:47:24 -0500 | [diff] [blame] | 2686 | if (!sp->root_count && sp->role.invalid) |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 2687 | kvm_mmu_prepare_zap_page(vcpu->kvm, sp, |
| 2688 | &invalid_list); |
Avi Kivity | 417726a | 2007-04-12 17:35:58 +0300 | [diff] [blame] | 2689 | } |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2690 | vcpu->arch.mmu.pae_root[i] = INVALID_PAGE; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2691 | } |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 2692 | kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list); |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 2693 | spin_unlock(&vcpu->kvm->mmu_lock); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2694 | vcpu->arch.mmu.root_hpa = INVALID_PAGE; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2695 | } |
| 2696 | |
Marcelo Tosatti | 8986ecc | 2009-05-12 18:55:45 -0300 | [diff] [blame] | 2697 | static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn) |
| 2698 | { |
| 2699 | int ret = 0; |
| 2700 | |
| 2701 | if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) { |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 2702 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Marcelo Tosatti | 8986ecc | 2009-05-12 18:55:45 -0300 | [diff] [blame] | 2703 | ret = 1; |
| 2704 | } |
| 2705 | |
| 2706 | return ret; |
| 2707 | } |
| 2708 | |
Joerg Roedel | 651dd37 | 2010-09-10 17:30:59 +0200 | [diff] [blame] | 2709 | static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu) |
| 2710 | { |
| 2711 | struct kvm_mmu_page *sp; |
Avi Kivity | 7ebaf15 | 2010-10-03 18:51:39 +0200 | [diff] [blame] | 2712 | unsigned i; |
Joerg Roedel | 651dd37 | 2010-09-10 17:30:59 +0200 | [diff] [blame] | 2713 | |
| 2714 | if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) { |
| 2715 | spin_lock(&vcpu->kvm->mmu_lock); |
| 2716 | kvm_mmu_free_some_pages(vcpu); |
| 2717 | sp = kvm_mmu_get_page(vcpu, 0, 0, PT64_ROOT_LEVEL, |
| 2718 | 1, ACC_ALL, NULL); |
| 2719 | ++sp->root_count; |
| 2720 | spin_unlock(&vcpu->kvm->mmu_lock); |
| 2721 | vcpu->arch.mmu.root_hpa = __pa(sp->spt); |
| 2722 | } else if (vcpu->arch.mmu.shadow_root_level == PT32E_ROOT_LEVEL) { |
| 2723 | for (i = 0; i < 4; ++i) { |
| 2724 | hpa_t root = vcpu->arch.mmu.pae_root[i]; |
| 2725 | |
| 2726 | ASSERT(!VALID_PAGE(root)); |
| 2727 | spin_lock(&vcpu->kvm->mmu_lock); |
| 2728 | kvm_mmu_free_some_pages(vcpu); |
Avi Kivity | 649497d | 2010-12-28 12:09:07 +0200 | [diff] [blame] | 2729 | sp = kvm_mmu_get_page(vcpu, i << (30 - PAGE_SHIFT), |
| 2730 | i << 30, |
Joerg Roedel | 651dd37 | 2010-09-10 17:30:59 +0200 | [diff] [blame] | 2731 | PT32_ROOT_LEVEL, 1, ACC_ALL, |
| 2732 | NULL); |
| 2733 | root = __pa(sp->spt); |
| 2734 | ++sp->root_count; |
| 2735 | spin_unlock(&vcpu->kvm->mmu_lock); |
| 2736 | vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK; |
Joerg Roedel | 651dd37 | 2010-09-10 17:30:59 +0200 | [diff] [blame] | 2737 | } |
Xiao Guangrong | 6292757 | 2010-09-27 18:02:12 +0800 | [diff] [blame] | 2738 | vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root); |
Joerg Roedel | 651dd37 | 2010-09-10 17:30:59 +0200 | [diff] [blame] | 2739 | } else |
| 2740 | BUG(); |
| 2741 | |
| 2742 | return 0; |
| 2743 | } |
| 2744 | |
| 2745 | static int mmu_alloc_shadow_roots(struct kvm_vcpu *vcpu) |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2746 | { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2747 | struct kvm_mmu_page *sp; |
Joerg Roedel | 81407ca | 2010-09-10 17:31:00 +0200 | [diff] [blame] | 2748 | u64 pdptr, pm_mask; |
| 2749 | gfn_t root_gfn; |
| 2750 | int i; |
Avi Kivity | 3bb65a2 | 2007-01-05 16:36:51 -0800 | [diff] [blame] | 2751 | |
Joerg Roedel | 5777ed3 | 2010-09-10 17:30:42 +0200 | [diff] [blame] | 2752 | root_gfn = vcpu->arch.mmu.get_cr3(vcpu) >> PAGE_SHIFT; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2753 | |
Joerg Roedel | 651dd37 | 2010-09-10 17:30:59 +0200 | [diff] [blame] | 2754 | if (mmu_check_root(vcpu, root_gfn)) |
| 2755 | return 1; |
| 2756 | |
| 2757 | /* |
| 2758 | * Do we shadow a long mode page table? If so we need to |
| 2759 | * write-protect the guests page table root. |
| 2760 | */ |
| 2761 | if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2762 | hpa_t root = vcpu->arch.mmu.root_hpa; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2763 | |
| 2764 | ASSERT(!VALID_PAGE(root)); |
Joerg Roedel | 651dd37 | 2010-09-10 17:30:59 +0200 | [diff] [blame] | 2765 | |
Avi Kivity | 8facbbf | 2010-05-04 12:58:32 +0300 | [diff] [blame] | 2766 | spin_lock(&vcpu->kvm->mmu_lock); |
Marcelo Tosatti | 24955b6c9 | 2010-05-12 21:00:35 -0300 | [diff] [blame] | 2767 | kvm_mmu_free_some_pages(vcpu); |
Joerg Roedel | 651dd37 | 2010-09-10 17:30:59 +0200 | [diff] [blame] | 2768 | sp = kvm_mmu_get_page(vcpu, root_gfn, 0, PT64_ROOT_LEVEL, |
| 2769 | 0, ACC_ALL, NULL); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2770 | root = __pa(sp->spt); |
| 2771 | ++sp->root_count; |
Avi Kivity | 8facbbf | 2010-05-04 12:58:32 +0300 | [diff] [blame] | 2772 | spin_unlock(&vcpu->kvm->mmu_lock); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2773 | vcpu->arch.mmu.root_hpa = root; |
Marcelo Tosatti | 8986ecc | 2009-05-12 18:55:45 -0300 | [diff] [blame] | 2774 | return 0; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2775 | } |
Joerg Roedel | f87f928 | 2010-09-02 17:29:45 +0200 | [diff] [blame] | 2776 | |
Joerg Roedel | 651dd37 | 2010-09-10 17:30:59 +0200 | [diff] [blame] | 2777 | /* |
| 2778 | * We shadow a 32 bit page table. This may be a legacy 2-level |
Joerg Roedel | 81407ca | 2010-09-10 17:31:00 +0200 | [diff] [blame] | 2779 | * or a PAE 3-level page table. In either case we need to be aware that |
| 2780 | * the shadow page table may be a PAE or a long mode page table. |
Joerg Roedel | 651dd37 | 2010-09-10 17:30:59 +0200 | [diff] [blame] | 2781 | */ |
Joerg Roedel | 81407ca | 2010-09-10 17:31:00 +0200 | [diff] [blame] | 2782 | pm_mask = PT_PRESENT_MASK; |
| 2783 | if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) |
| 2784 | pm_mask |= PT_ACCESSED_MASK | PT_WRITABLE_MASK | PT_USER_MASK; |
| 2785 | |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2786 | for (i = 0; i < 4; ++i) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2787 | hpa_t root = vcpu->arch.mmu.pae_root[i]; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2788 | |
| 2789 | ASSERT(!VALID_PAGE(root)); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2790 | if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) { |
Avi Kivity | e4e517b | 2011-07-28 11:36:17 +0300 | [diff] [blame] | 2791 | pdptr = vcpu->arch.mmu.get_pdptr(vcpu, i); |
Avi Kivity | 43a3795 | 2009-06-10 14:12:05 +0300 | [diff] [blame] | 2792 | if (!is_present_gpte(pdptr)) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2793 | vcpu->arch.mmu.pae_root[i] = 0; |
Avi Kivity | 417726a | 2007-04-12 17:35:58 +0300 | [diff] [blame] | 2794 | continue; |
| 2795 | } |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 2796 | root_gfn = pdptr >> PAGE_SHIFT; |
Joerg Roedel | f87f928 | 2010-09-02 17:29:45 +0200 | [diff] [blame] | 2797 | if (mmu_check_root(vcpu, root_gfn)) |
| 2798 | return 1; |
Eric Northup | 5a7388c | 2010-04-26 17:00:05 -0700 | [diff] [blame] | 2799 | } |
Avi Kivity | 8facbbf | 2010-05-04 12:58:32 +0300 | [diff] [blame] | 2800 | spin_lock(&vcpu->kvm->mmu_lock); |
Marcelo Tosatti | 24955b6c9 | 2010-05-12 21:00:35 -0300 | [diff] [blame] | 2801 | kvm_mmu_free_some_pages(vcpu); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2802 | sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30, |
Joerg Roedel | 651dd37 | 2010-09-10 17:30:59 +0200 | [diff] [blame] | 2803 | PT32_ROOT_LEVEL, 0, |
Avi Kivity | f7d9c7b | 2008-02-26 22:12:10 +0200 | [diff] [blame] | 2804 | ACC_ALL, NULL); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2805 | root = __pa(sp->spt); |
| 2806 | ++sp->root_count; |
Avi Kivity | 8facbbf | 2010-05-04 12:58:32 +0300 | [diff] [blame] | 2807 | spin_unlock(&vcpu->kvm->mmu_lock); |
| 2808 | |
Joerg Roedel | 81407ca | 2010-09-10 17:31:00 +0200 | [diff] [blame] | 2809 | vcpu->arch.mmu.pae_root[i] = root | pm_mask; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2810 | } |
Xiao Guangrong | 6292757 | 2010-09-27 18:02:12 +0800 | [diff] [blame] | 2811 | vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root); |
Joerg Roedel | 81407ca | 2010-09-10 17:31:00 +0200 | [diff] [blame] | 2812 | |
| 2813 | /* |
| 2814 | * If we shadow a 32 bit page table with a long mode page |
| 2815 | * table we enter this path. |
| 2816 | */ |
| 2817 | if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) { |
| 2818 | if (vcpu->arch.mmu.lm_root == NULL) { |
| 2819 | /* |
| 2820 | * The additional page necessary for this is only |
| 2821 | * allocated on demand. |
| 2822 | */ |
| 2823 | |
| 2824 | u64 *lm_root; |
| 2825 | |
| 2826 | lm_root = (void*)get_zeroed_page(GFP_KERNEL); |
| 2827 | if (lm_root == NULL) |
| 2828 | return 1; |
| 2829 | |
| 2830 | lm_root[0] = __pa(vcpu->arch.mmu.pae_root) | pm_mask; |
| 2831 | |
| 2832 | vcpu->arch.mmu.lm_root = lm_root; |
| 2833 | } |
| 2834 | |
| 2835 | vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.lm_root); |
| 2836 | } |
| 2837 | |
Marcelo Tosatti | 8986ecc | 2009-05-12 18:55:45 -0300 | [diff] [blame] | 2838 | return 0; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2839 | } |
| 2840 | |
Joerg Roedel | 651dd37 | 2010-09-10 17:30:59 +0200 | [diff] [blame] | 2841 | static int mmu_alloc_roots(struct kvm_vcpu *vcpu) |
| 2842 | { |
| 2843 | if (vcpu->arch.mmu.direct_map) |
| 2844 | return mmu_alloc_direct_roots(vcpu); |
| 2845 | else |
| 2846 | return mmu_alloc_shadow_roots(vcpu); |
| 2847 | } |
| 2848 | |
Marcelo Tosatti | 0ba73cd | 2008-09-23 13:18:34 -0300 | [diff] [blame] | 2849 | static void mmu_sync_roots(struct kvm_vcpu *vcpu) |
| 2850 | { |
| 2851 | int i; |
| 2852 | struct kvm_mmu_page *sp; |
| 2853 | |
Joerg Roedel | 81407ca | 2010-09-10 17:31:00 +0200 | [diff] [blame] | 2854 | if (vcpu->arch.mmu.direct_map) |
| 2855 | return; |
| 2856 | |
Marcelo Tosatti | 0ba73cd | 2008-09-23 13:18:34 -0300 | [diff] [blame] | 2857 | if (!VALID_PAGE(vcpu->arch.mmu.root_hpa)) |
| 2858 | return; |
Xiao Guangrong | 6903074 | 2010-09-27 18:09:29 +0800 | [diff] [blame] | 2859 | |
Xiao Guangrong | bebb106 | 2011-07-12 03:23:20 +0800 | [diff] [blame] | 2860 | vcpu_clear_mmio_info(vcpu, ~0ul); |
Xiao Guangrong | 0375f7f | 2011-11-28 20:41:00 +0800 | [diff] [blame^] | 2861 | kvm_mmu_audit(vcpu, AUDIT_PRE_SYNC); |
Joerg Roedel | 81407ca | 2010-09-10 17:31:00 +0200 | [diff] [blame] | 2862 | if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) { |
Marcelo Tosatti | 0ba73cd | 2008-09-23 13:18:34 -0300 | [diff] [blame] | 2863 | hpa_t root = vcpu->arch.mmu.root_hpa; |
| 2864 | sp = page_header(root); |
| 2865 | mmu_sync_children(vcpu, sp); |
Xiao Guangrong | 0375f7f | 2011-11-28 20:41:00 +0800 | [diff] [blame^] | 2866 | kvm_mmu_audit(vcpu, AUDIT_POST_SYNC); |
Marcelo Tosatti | 0ba73cd | 2008-09-23 13:18:34 -0300 | [diff] [blame] | 2867 | return; |
| 2868 | } |
| 2869 | for (i = 0; i < 4; ++i) { |
| 2870 | hpa_t root = vcpu->arch.mmu.pae_root[i]; |
| 2871 | |
Marcelo Tosatti | 8986ecc | 2009-05-12 18:55:45 -0300 | [diff] [blame] | 2872 | if (root && VALID_PAGE(root)) { |
Marcelo Tosatti | 0ba73cd | 2008-09-23 13:18:34 -0300 | [diff] [blame] | 2873 | root &= PT64_BASE_ADDR_MASK; |
| 2874 | sp = page_header(root); |
| 2875 | mmu_sync_children(vcpu, sp); |
| 2876 | } |
| 2877 | } |
Xiao Guangrong | 0375f7f | 2011-11-28 20:41:00 +0800 | [diff] [blame^] | 2878 | kvm_mmu_audit(vcpu, AUDIT_POST_SYNC); |
Marcelo Tosatti | 0ba73cd | 2008-09-23 13:18:34 -0300 | [diff] [blame] | 2879 | } |
| 2880 | |
| 2881 | void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu) |
| 2882 | { |
| 2883 | spin_lock(&vcpu->kvm->mmu_lock); |
| 2884 | mmu_sync_roots(vcpu); |
| 2885 | spin_unlock(&vcpu->kvm->mmu_lock); |
| 2886 | } |
| 2887 | |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 2888 | static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr, |
Avi Kivity | ab9ae31 | 2010-11-22 17:53:26 +0200 | [diff] [blame] | 2889 | u32 access, struct x86_exception *exception) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2890 | { |
Avi Kivity | ab9ae31 | 2010-11-22 17:53:26 +0200 | [diff] [blame] | 2891 | if (exception) |
| 2892 | exception->error_code = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2893 | return vaddr; |
| 2894 | } |
| 2895 | |
Joerg Roedel | 6539e73 | 2010-09-10 17:30:50 +0200 | [diff] [blame] | 2896 | static gpa_t nonpaging_gva_to_gpa_nested(struct kvm_vcpu *vcpu, gva_t vaddr, |
Avi Kivity | ab9ae31 | 2010-11-22 17:53:26 +0200 | [diff] [blame] | 2897 | u32 access, |
| 2898 | struct x86_exception *exception) |
Joerg Roedel | 6539e73 | 2010-09-10 17:30:50 +0200 | [diff] [blame] | 2899 | { |
Avi Kivity | ab9ae31 | 2010-11-22 17:53:26 +0200 | [diff] [blame] | 2900 | if (exception) |
| 2901 | exception->error_code = 0; |
Joerg Roedel | 6539e73 | 2010-09-10 17:30:50 +0200 | [diff] [blame] | 2902 | return vcpu->arch.nested_mmu.translate_gpa(vcpu, vaddr, access); |
| 2903 | } |
| 2904 | |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 2905 | static bool quickly_check_mmio_pf(struct kvm_vcpu *vcpu, u64 addr, bool direct) |
| 2906 | { |
| 2907 | if (direct) |
| 2908 | return vcpu_match_mmio_gpa(vcpu, addr); |
| 2909 | |
| 2910 | return vcpu_match_mmio_gva(vcpu, addr); |
| 2911 | } |
| 2912 | |
| 2913 | |
| 2914 | /* |
| 2915 | * On direct hosts, the last spte is only allows two states |
| 2916 | * for mmio page fault: |
| 2917 | * - It is the mmio spte |
| 2918 | * - It is zapped or it is being zapped. |
| 2919 | * |
| 2920 | * This function completely checks the spte when the last spte |
| 2921 | * is not the mmio spte. |
| 2922 | */ |
| 2923 | static bool check_direct_spte_mmio_pf(u64 spte) |
| 2924 | { |
| 2925 | return __check_direct_spte_mmio_pf(spte); |
| 2926 | } |
| 2927 | |
| 2928 | static u64 walk_shadow_page_get_mmio_spte(struct kvm_vcpu *vcpu, u64 addr) |
| 2929 | { |
| 2930 | struct kvm_shadow_walk_iterator iterator; |
| 2931 | u64 spte = 0ull; |
| 2932 | |
| 2933 | walk_shadow_page_lockless_begin(vcpu); |
| 2934 | for_each_shadow_entry_lockless(vcpu, addr, iterator, spte) |
| 2935 | if (!is_shadow_present_pte(spte)) |
| 2936 | break; |
| 2937 | walk_shadow_page_lockless_end(vcpu); |
| 2938 | |
| 2939 | return spte; |
| 2940 | } |
| 2941 | |
| 2942 | /* |
| 2943 | * If it is a real mmio page fault, return 1 and emulat the instruction |
| 2944 | * directly, return 0 to let CPU fault again on the address, -1 is |
| 2945 | * returned if bug is detected. |
| 2946 | */ |
| 2947 | int handle_mmio_page_fault_common(struct kvm_vcpu *vcpu, u64 addr, bool direct) |
| 2948 | { |
| 2949 | u64 spte; |
| 2950 | |
| 2951 | if (quickly_check_mmio_pf(vcpu, addr, direct)) |
| 2952 | return 1; |
| 2953 | |
| 2954 | spte = walk_shadow_page_get_mmio_spte(vcpu, addr); |
| 2955 | |
| 2956 | if (is_mmio_spte(spte)) { |
| 2957 | gfn_t gfn = get_mmio_spte_gfn(spte); |
| 2958 | unsigned access = get_mmio_spte_access(spte); |
| 2959 | |
| 2960 | if (direct) |
| 2961 | addr = 0; |
Xiao Guangrong | 4f02264 | 2011-07-12 03:34:24 +0800 | [diff] [blame] | 2962 | |
| 2963 | trace_handle_mmio_page_fault(addr, gfn, access); |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 2964 | vcpu_cache_mmio_info(vcpu, addr, gfn, access); |
| 2965 | return 1; |
| 2966 | } |
| 2967 | |
| 2968 | /* |
| 2969 | * It's ok if the gva is remapped by other cpus on shadow guest, |
| 2970 | * it's a BUG if the gfn is not a mmio page. |
| 2971 | */ |
| 2972 | if (direct && !check_direct_spte_mmio_pf(spte)) |
| 2973 | return -1; |
| 2974 | |
| 2975 | /* |
| 2976 | * If the page table is zapped by other cpus, let CPU fault again on |
| 2977 | * the address. |
| 2978 | */ |
| 2979 | return 0; |
| 2980 | } |
| 2981 | EXPORT_SYMBOL_GPL(handle_mmio_page_fault_common); |
| 2982 | |
| 2983 | static int handle_mmio_page_fault(struct kvm_vcpu *vcpu, u64 addr, |
| 2984 | u32 error_code, bool direct) |
| 2985 | { |
| 2986 | int ret; |
| 2987 | |
| 2988 | ret = handle_mmio_page_fault_common(vcpu, addr, direct); |
| 2989 | WARN_ON(ret < 0); |
| 2990 | return ret; |
| 2991 | } |
| 2992 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2993 | static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva, |
Xiao Guangrong | 78b2c54 | 2010-12-07 10:48:06 +0800 | [diff] [blame] | 2994 | u32 error_code, bool prefault) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2995 | { |
Avi Kivity | e833240 | 2007-12-09 18:43:00 +0200 | [diff] [blame] | 2996 | gfn_t gfn; |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 2997 | int r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2998 | |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 2999 | pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code); |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 3000 | |
| 3001 | if (unlikely(error_code & PFERR_RSVD_MASK)) |
| 3002 | return handle_mmio_page_fault(vcpu, gva, error_code, true); |
| 3003 | |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 3004 | r = mmu_topup_memory_caches(vcpu); |
| 3005 | if (r) |
| 3006 | return r; |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 3007 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3008 | ASSERT(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3009 | ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3010 | |
Avi Kivity | e833240 | 2007-12-09 18:43:00 +0200 | [diff] [blame] | 3011 | gfn = gva >> PAGE_SHIFT; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3012 | |
Avi Kivity | e833240 | 2007-12-09 18:43:00 +0200 | [diff] [blame] | 3013 | return nonpaging_map(vcpu, gva & PAGE_MASK, |
Xiao Guangrong | 78b2c54 | 2010-12-07 10:48:06 +0800 | [diff] [blame] | 3014 | error_code & PFERR_WRITE_MASK, gfn, prefault); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3015 | } |
| 3016 | |
Jan Kiszka | 7e1fbea | 2010-10-20 15:18:02 +0200 | [diff] [blame] | 3017 | static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn) |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 3018 | { |
| 3019 | struct kvm_arch_async_pf arch; |
Xiao Guangrong | fb67e14 | 2010-12-07 10:35:25 +0800 | [diff] [blame] | 3020 | |
Gleb Natapov | 7c90705 | 2010-10-14 11:22:53 +0200 | [diff] [blame] | 3021 | arch.token = (vcpu->arch.apf.id++ << 12) | vcpu->vcpu_id; |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 3022 | arch.gfn = gfn; |
Xiao Guangrong | c4806ac | 2010-11-12 14:49:55 +0800 | [diff] [blame] | 3023 | arch.direct_map = vcpu->arch.mmu.direct_map; |
Xiao Guangrong | fb67e14 | 2010-12-07 10:35:25 +0800 | [diff] [blame] | 3024 | arch.cr3 = vcpu->arch.mmu.get_cr3(vcpu); |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 3025 | |
| 3026 | return kvm_setup_async_pf(vcpu, gva, gfn, &arch); |
| 3027 | } |
| 3028 | |
| 3029 | static bool can_do_async_pf(struct kvm_vcpu *vcpu) |
| 3030 | { |
| 3031 | if (unlikely(!irqchip_in_kernel(vcpu->kvm) || |
| 3032 | kvm_event_needs_reinjection(vcpu))) |
| 3033 | return false; |
| 3034 | |
| 3035 | return kvm_x86_ops->interrupt_allowed(vcpu); |
| 3036 | } |
| 3037 | |
Xiao Guangrong | 78b2c54 | 2010-12-07 10:48:06 +0800 | [diff] [blame] | 3038 | static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn, |
Marcelo Tosatti | 612819c | 2010-10-22 14:18:18 -0200 | [diff] [blame] | 3039 | gva_t gva, pfn_t *pfn, bool write, bool *writable) |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 3040 | { |
| 3041 | bool async; |
| 3042 | |
Marcelo Tosatti | 612819c | 2010-10-22 14:18:18 -0200 | [diff] [blame] | 3043 | *pfn = gfn_to_pfn_async(vcpu->kvm, gfn, &async, write, writable); |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 3044 | |
| 3045 | if (!async) |
| 3046 | return false; /* *pfn has correct page already */ |
| 3047 | |
| 3048 | put_page(pfn_to_page(*pfn)); |
| 3049 | |
Xiao Guangrong | 78b2c54 | 2010-12-07 10:48:06 +0800 | [diff] [blame] | 3050 | if (!prefault && can_do_async_pf(vcpu)) { |
Xiao Guangrong | c9b263d | 2010-11-01 16:58:43 +0800 | [diff] [blame] | 3051 | trace_kvm_try_async_get_page(gva, gfn); |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 3052 | if (kvm_find_async_pf_gfn(vcpu, gfn)) { |
| 3053 | trace_kvm_async_pf_doublefault(gva, gfn); |
| 3054 | kvm_make_request(KVM_REQ_APF_HALT, vcpu); |
| 3055 | return true; |
| 3056 | } else if (kvm_arch_setup_async_pf(vcpu, gva, gfn)) |
| 3057 | return true; |
| 3058 | } |
| 3059 | |
Marcelo Tosatti | 612819c | 2010-10-22 14:18:18 -0200 | [diff] [blame] | 3060 | *pfn = gfn_to_pfn_prot(vcpu->kvm, gfn, write, writable); |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 3061 | |
| 3062 | return false; |
| 3063 | } |
| 3064 | |
Gleb Natapov | 56028d0 | 2010-10-17 18:13:42 +0200 | [diff] [blame] | 3065 | static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa, u32 error_code, |
Xiao Guangrong | 78b2c54 | 2010-12-07 10:48:06 +0800 | [diff] [blame] | 3066 | bool prefault) |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 3067 | { |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 3068 | pfn_t pfn; |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 3069 | int r; |
Joerg Roedel | 852e3c1 | 2009-07-27 16:30:44 +0200 | [diff] [blame] | 3070 | int level; |
Andrea Arcangeli | 936a5fe | 2011-01-13 15:46:48 -0800 | [diff] [blame] | 3071 | int force_pt_level; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 3072 | gfn_t gfn = gpa >> PAGE_SHIFT; |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 3073 | unsigned long mmu_seq; |
Marcelo Tosatti | 612819c | 2010-10-22 14:18:18 -0200 | [diff] [blame] | 3074 | int write = error_code & PFERR_WRITE_MASK; |
| 3075 | bool map_writable; |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 3076 | |
| 3077 | ASSERT(vcpu); |
| 3078 | ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa)); |
| 3079 | |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 3080 | if (unlikely(error_code & PFERR_RSVD_MASK)) |
| 3081 | return handle_mmio_page_fault(vcpu, gpa, error_code, true); |
| 3082 | |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 3083 | r = mmu_topup_memory_caches(vcpu); |
| 3084 | if (r) |
| 3085 | return r; |
| 3086 | |
Andrea Arcangeli | 936a5fe | 2011-01-13 15:46:48 -0800 | [diff] [blame] | 3087 | force_pt_level = mapping_level_dirty_bitmap(vcpu, gfn); |
| 3088 | if (likely(!force_pt_level)) { |
| 3089 | level = mapping_level(vcpu, gfn); |
| 3090 | gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1); |
| 3091 | } else |
| 3092 | level = PT_PAGE_TABLE_LEVEL; |
Joerg Roedel | 852e3c1 | 2009-07-27 16:30:44 +0200 | [diff] [blame] | 3093 | |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 3094 | mmu_seq = vcpu->kvm->mmu_notifier_seq; |
Marcelo Tosatti | 4c2155c | 2008-09-16 20:54:47 -0300 | [diff] [blame] | 3095 | smp_rmb(); |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 3096 | |
Xiao Guangrong | 78b2c54 | 2010-12-07 10:48:06 +0800 | [diff] [blame] | 3097 | if (try_async_pf(vcpu, prefault, gfn, gpa, &pfn, write, &map_writable)) |
Gleb Natapov | af585b9 | 2010-10-14 11:22:46 +0200 | [diff] [blame] | 3098 | return 0; |
| 3099 | |
Xiao Guangrong | d7c5520 | 2011-07-12 03:29:38 +0800 | [diff] [blame] | 3100 | if (handle_abnormal_pfn(vcpu, 0, gfn, pfn, ACC_ALL, &r)) |
| 3101 | return r; |
| 3102 | |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 3103 | spin_lock(&vcpu->kvm->mmu_lock); |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 3104 | if (mmu_notifier_retry(vcpu, mmu_seq)) |
| 3105 | goto out_unlock; |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 3106 | kvm_mmu_free_some_pages(vcpu); |
Andrea Arcangeli | 936a5fe | 2011-01-13 15:46:48 -0800 | [diff] [blame] | 3107 | if (likely(!force_pt_level)) |
| 3108 | transparent_hugepage_adjust(vcpu, &gfn, &pfn, &level); |
Marcelo Tosatti | 612819c | 2010-10-22 14:18:18 -0200 | [diff] [blame] | 3109 | r = __direct_map(vcpu, gpa, write, map_writable, |
Xiao Guangrong | 2ec4739 | 2010-12-07 10:34:42 +0800 | [diff] [blame] | 3110 | level, gfn, pfn, prefault); |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 3111 | spin_unlock(&vcpu->kvm->mmu_lock); |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 3112 | |
| 3113 | return r; |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 3114 | |
| 3115 | out_unlock: |
| 3116 | spin_unlock(&vcpu->kvm->mmu_lock); |
| 3117 | kvm_release_pfn_clean(pfn); |
| 3118 | return 0; |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 3119 | } |
| 3120 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3121 | static void nonpaging_free(struct kvm_vcpu *vcpu) |
| 3122 | { |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 3123 | mmu_free_roots(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3124 | } |
| 3125 | |
Joerg Roedel | 52fde8d | 2010-09-10 17:30:44 +0200 | [diff] [blame] | 3126 | static int nonpaging_init_context(struct kvm_vcpu *vcpu, |
| 3127 | struct kvm_mmu *context) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3128 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3129 | context->new_cr3 = nonpaging_new_cr3; |
| 3130 | context->page_fault = nonpaging_page_fault; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3131 | context->gva_to_gpa = nonpaging_gva_to_gpa; |
| 3132 | context->free = nonpaging_free; |
Marcelo Tosatti | e8bc217 | 2008-09-23 13:18:33 -0300 | [diff] [blame] | 3133 | context->sync_page = nonpaging_sync_page; |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 3134 | context->invlpg = nonpaging_invlpg; |
Xiao Guangrong | 0f53b5b | 2011-03-09 15:43:51 +0800 | [diff] [blame] | 3135 | context->update_pte = nonpaging_update_pte; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 3136 | context->root_level = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3137 | context->shadow_root_level = PT32E_ROOT_LEVEL; |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 3138 | context->root_hpa = INVALID_PAGE; |
Joerg Roedel | c5a78f2b | 2010-09-10 17:30:39 +0200 | [diff] [blame] | 3139 | context->direct_map = true; |
Joerg Roedel | 2d48a98 | 2010-09-10 17:31:01 +0200 | [diff] [blame] | 3140 | context->nx = false; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3141 | return 0; |
| 3142 | } |
| 3143 | |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 3144 | void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3145 | { |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 3146 | ++vcpu->stat.tlb_flush; |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 3147 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3148 | } |
| 3149 | |
| 3150 | static void paging_new_cr3(struct kvm_vcpu *vcpu) |
| 3151 | { |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 3152 | pgprintk("%s: cr3 %lx\n", __func__, kvm_read_cr3(vcpu)); |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 3153 | mmu_free_roots(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3154 | } |
| 3155 | |
Joerg Roedel | 5777ed3 | 2010-09-10 17:30:42 +0200 | [diff] [blame] | 3156 | static unsigned long get_cr3(struct kvm_vcpu *vcpu) |
| 3157 | { |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 3158 | return kvm_read_cr3(vcpu); |
Joerg Roedel | 5777ed3 | 2010-09-10 17:30:42 +0200 | [diff] [blame] | 3159 | } |
| 3160 | |
Avi Kivity | 6389ee9 | 2010-11-29 16:12:30 +0200 | [diff] [blame] | 3161 | static void inject_page_fault(struct kvm_vcpu *vcpu, |
| 3162 | struct x86_exception *fault) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3163 | { |
Avi Kivity | 6389ee9 | 2010-11-29 16:12:30 +0200 | [diff] [blame] | 3164 | vcpu->arch.mmu.inject_page_fault(vcpu, fault); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3165 | } |
| 3166 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3167 | static void paging_free(struct kvm_vcpu *vcpu) |
| 3168 | { |
| 3169 | nonpaging_free(vcpu); |
| 3170 | } |
| 3171 | |
Joerg Roedel | 3241f22 | 2010-09-10 17:30:45 +0200 | [diff] [blame] | 3172 | static bool is_rsvd_bits_set(struct kvm_mmu *mmu, u64 gpte, int level) |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 3173 | { |
| 3174 | int bit7; |
| 3175 | |
| 3176 | bit7 = (gpte >> 7) & 1; |
Joerg Roedel | 3241f22 | 2010-09-10 17:30:45 +0200 | [diff] [blame] | 3177 | return (gpte & mmu->rsvd_bits_mask[bit7][level-1]) != 0; |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 3178 | } |
| 3179 | |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 3180 | static bool sync_mmio_spte(u64 *sptep, gfn_t gfn, unsigned access, |
| 3181 | int *nr_present) |
| 3182 | { |
| 3183 | if (unlikely(is_mmio_spte(*sptep))) { |
| 3184 | if (gfn != get_mmio_spte_gfn(*sptep)) { |
| 3185 | mmu_spte_clear_no_track(sptep); |
| 3186 | return true; |
| 3187 | } |
| 3188 | |
| 3189 | (*nr_present)++; |
| 3190 | mark_mmio_spte(sptep, gfn, access); |
| 3191 | return true; |
| 3192 | } |
| 3193 | |
| 3194 | return false; |
| 3195 | } |
| 3196 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3197 | #define PTTYPE 64 |
| 3198 | #include "paging_tmpl.h" |
| 3199 | #undef PTTYPE |
| 3200 | |
| 3201 | #define PTTYPE 32 |
| 3202 | #include "paging_tmpl.h" |
| 3203 | #undef PTTYPE |
| 3204 | |
Joerg Roedel | 52fde8d | 2010-09-10 17:30:44 +0200 | [diff] [blame] | 3205 | static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, |
| 3206 | struct kvm_mmu *context, |
| 3207 | int level) |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 3208 | { |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 3209 | int maxphyaddr = cpuid_maxphyaddr(vcpu); |
| 3210 | u64 exb_bit_rsvd = 0; |
| 3211 | |
Joerg Roedel | 2d48a98 | 2010-09-10 17:31:01 +0200 | [diff] [blame] | 3212 | if (!context->nx) |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 3213 | exb_bit_rsvd = rsvd_bits(63, 63); |
| 3214 | switch (level) { |
| 3215 | case PT32_ROOT_LEVEL: |
| 3216 | /* no rsvd bits for 2 level 4K page table entries */ |
| 3217 | context->rsvd_bits_mask[0][1] = 0; |
| 3218 | context->rsvd_bits_mask[0][0] = 0; |
Xiao Guangrong | f815bce | 2010-03-19 17:58:53 +0800 | [diff] [blame] | 3219 | context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0]; |
| 3220 | |
| 3221 | if (!is_pse(vcpu)) { |
| 3222 | context->rsvd_bits_mask[1][1] = 0; |
| 3223 | break; |
| 3224 | } |
| 3225 | |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 3226 | if (is_cpuid_PSE36()) |
| 3227 | /* 36bits PSE 4MB page */ |
| 3228 | context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21); |
| 3229 | else |
| 3230 | /* 32 bits PSE 4MB page */ |
| 3231 | context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21); |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 3232 | break; |
| 3233 | case PT32E_ROOT_LEVEL: |
Dong, Eddie | 20c466b | 2009-03-31 23:03:45 +0800 | [diff] [blame] | 3234 | context->rsvd_bits_mask[0][2] = |
| 3235 | rsvd_bits(maxphyaddr, 63) | |
| 3236 | rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */ |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 3237 | context->rsvd_bits_mask[0][1] = exb_bit_rsvd | |
Sheng Yang | 4c26b4c | 2009-04-02 10:28:37 +0800 | [diff] [blame] | 3238 | rsvd_bits(maxphyaddr, 62); /* PDE */ |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 3239 | context->rsvd_bits_mask[0][0] = exb_bit_rsvd | |
| 3240 | rsvd_bits(maxphyaddr, 62); /* PTE */ |
| 3241 | context->rsvd_bits_mask[1][1] = exb_bit_rsvd | |
| 3242 | rsvd_bits(maxphyaddr, 62) | |
| 3243 | rsvd_bits(13, 20); /* large page */ |
Xiao Guangrong | f815bce | 2010-03-19 17:58:53 +0800 | [diff] [blame] | 3244 | context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0]; |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 3245 | break; |
| 3246 | case PT64_ROOT_LEVEL: |
| 3247 | context->rsvd_bits_mask[0][3] = exb_bit_rsvd | |
| 3248 | rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8); |
| 3249 | context->rsvd_bits_mask[0][2] = exb_bit_rsvd | |
| 3250 | rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8); |
| 3251 | context->rsvd_bits_mask[0][1] = exb_bit_rsvd | |
Sheng Yang | 4c26b4c | 2009-04-02 10:28:37 +0800 | [diff] [blame] | 3252 | rsvd_bits(maxphyaddr, 51); |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 3253 | context->rsvd_bits_mask[0][0] = exb_bit_rsvd | |
| 3254 | rsvd_bits(maxphyaddr, 51); |
| 3255 | context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3]; |
Joerg Roedel | e04da98 | 2009-07-27 16:30:45 +0200 | [diff] [blame] | 3256 | context->rsvd_bits_mask[1][2] = exb_bit_rsvd | |
| 3257 | rsvd_bits(maxphyaddr, 51) | |
| 3258 | rsvd_bits(13, 29); |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 3259 | context->rsvd_bits_mask[1][1] = exb_bit_rsvd | |
Sheng Yang | 4c26b4c | 2009-04-02 10:28:37 +0800 | [diff] [blame] | 3260 | rsvd_bits(maxphyaddr, 51) | |
| 3261 | rsvd_bits(13, 20); /* large page */ |
Xiao Guangrong | f815bce | 2010-03-19 17:58:53 +0800 | [diff] [blame] | 3262 | context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0]; |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 3263 | break; |
| 3264 | } |
| 3265 | } |
| 3266 | |
Joerg Roedel | 52fde8d | 2010-09-10 17:30:44 +0200 | [diff] [blame] | 3267 | static int paging64_init_context_common(struct kvm_vcpu *vcpu, |
| 3268 | struct kvm_mmu *context, |
| 3269 | int level) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3270 | { |
Joerg Roedel | 2d48a98 | 2010-09-10 17:31:01 +0200 | [diff] [blame] | 3271 | context->nx = is_nx(vcpu); |
| 3272 | |
Joerg Roedel | 52fde8d | 2010-09-10 17:30:44 +0200 | [diff] [blame] | 3273 | reset_rsvds_bits_mask(vcpu, context, level); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3274 | |
| 3275 | ASSERT(is_pae(vcpu)); |
| 3276 | context->new_cr3 = paging_new_cr3; |
| 3277 | context->page_fault = paging64_page_fault; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3278 | context->gva_to_gpa = paging64_gva_to_gpa; |
Marcelo Tosatti | e8bc217 | 2008-09-23 13:18:33 -0300 | [diff] [blame] | 3279 | context->sync_page = paging64_sync_page; |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 3280 | context->invlpg = paging64_invlpg; |
Xiao Guangrong | 0f53b5b | 2011-03-09 15:43:51 +0800 | [diff] [blame] | 3281 | context->update_pte = paging64_update_pte; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3282 | context->free = paging_free; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 3283 | context->root_level = level; |
| 3284 | context->shadow_root_level = level; |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 3285 | context->root_hpa = INVALID_PAGE; |
Joerg Roedel | c5a78f2b | 2010-09-10 17:30:39 +0200 | [diff] [blame] | 3286 | context->direct_map = false; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3287 | return 0; |
| 3288 | } |
| 3289 | |
Joerg Roedel | 52fde8d | 2010-09-10 17:30:44 +0200 | [diff] [blame] | 3290 | static int paging64_init_context(struct kvm_vcpu *vcpu, |
| 3291 | struct kvm_mmu *context) |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 3292 | { |
Joerg Roedel | 52fde8d | 2010-09-10 17:30:44 +0200 | [diff] [blame] | 3293 | return paging64_init_context_common(vcpu, context, PT64_ROOT_LEVEL); |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 3294 | } |
| 3295 | |
Joerg Roedel | 52fde8d | 2010-09-10 17:30:44 +0200 | [diff] [blame] | 3296 | static int paging32_init_context(struct kvm_vcpu *vcpu, |
| 3297 | struct kvm_mmu *context) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3298 | { |
Joerg Roedel | 2d48a98 | 2010-09-10 17:31:01 +0200 | [diff] [blame] | 3299 | context->nx = false; |
| 3300 | |
Joerg Roedel | 52fde8d | 2010-09-10 17:30:44 +0200 | [diff] [blame] | 3301 | reset_rsvds_bits_mask(vcpu, context, PT32_ROOT_LEVEL); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3302 | |
| 3303 | context->new_cr3 = paging_new_cr3; |
| 3304 | context->page_fault = paging32_page_fault; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3305 | context->gva_to_gpa = paging32_gva_to_gpa; |
| 3306 | context->free = paging_free; |
Marcelo Tosatti | e8bc217 | 2008-09-23 13:18:33 -0300 | [diff] [blame] | 3307 | context->sync_page = paging32_sync_page; |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 3308 | context->invlpg = paging32_invlpg; |
Xiao Guangrong | 0f53b5b | 2011-03-09 15:43:51 +0800 | [diff] [blame] | 3309 | context->update_pte = paging32_update_pte; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3310 | context->root_level = PT32_ROOT_LEVEL; |
| 3311 | context->shadow_root_level = PT32E_ROOT_LEVEL; |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 3312 | context->root_hpa = INVALID_PAGE; |
Joerg Roedel | c5a78f2b | 2010-09-10 17:30:39 +0200 | [diff] [blame] | 3313 | context->direct_map = false; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3314 | return 0; |
| 3315 | } |
| 3316 | |
Joerg Roedel | 52fde8d | 2010-09-10 17:30:44 +0200 | [diff] [blame] | 3317 | static int paging32E_init_context(struct kvm_vcpu *vcpu, |
| 3318 | struct kvm_mmu *context) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3319 | { |
Joerg Roedel | 52fde8d | 2010-09-10 17:30:44 +0200 | [diff] [blame] | 3320 | return paging64_init_context_common(vcpu, context, PT32E_ROOT_LEVEL); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3321 | } |
| 3322 | |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 3323 | static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu) |
| 3324 | { |
Joerg Roedel | 14dfe85 | 2010-09-10 17:30:49 +0200 | [diff] [blame] | 3325 | struct kvm_mmu *context = vcpu->arch.walk_mmu; |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 3326 | |
Avi Kivity | c445f8e | 2010-12-21 16:26:01 +0200 | [diff] [blame] | 3327 | context->base_role.word = 0; |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 3328 | context->new_cr3 = nonpaging_new_cr3; |
| 3329 | context->page_fault = tdp_page_fault; |
| 3330 | context->free = nonpaging_free; |
Marcelo Tosatti | e8bc217 | 2008-09-23 13:18:33 -0300 | [diff] [blame] | 3331 | context->sync_page = nonpaging_sync_page; |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 3332 | context->invlpg = nonpaging_invlpg; |
Xiao Guangrong | 0f53b5b | 2011-03-09 15:43:51 +0800 | [diff] [blame] | 3333 | context->update_pte = nonpaging_update_pte; |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 3334 | context->shadow_root_level = kvm_x86_ops->get_tdp_level(); |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 3335 | context->root_hpa = INVALID_PAGE; |
Joerg Roedel | c5a78f2b | 2010-09-10 17:30:39 +0200 | [diff] [blame] | 3336 | context->direct_map = true; |
Joerg Roedel | 1c97f0a | 2010-09-10 17:30:41 +0200 | [diff] [blame] | 3337 | context->set_cr3 = kvm_x86_ops->set_tdp_cr3; |
Joerg Roedel | 5777ed3 | 2010-09-10 17:30:42 +0200 | [diff] [blame] | 3338 | context->get_cr3 = get_cr3; |
Avi Kivity | e4e517b | 2011-07-28 11:36:17 +0300 | [diff] [blame] | 3339 | context->get_pdptr = kvm_pdptr_read; |
Joerg Roedel | cb659db | 2010-09-10 17:30:43 +0200 | [diff] [blame] | 3340 | context->inject_page_fault = kvm_inject_page_fault; |
Joerg Roedel | 2d48a98 | 2010-09-10 17:31:01 +0200 | [diff] [blame] | 3341 | context->nx = is_nx(vcpu); |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 3342 | |
| 3343 | if (!is_paging(vcpu)) { |
Joerg Roedel | 2d48a98 | 2010-09-10 17:31:01 +0200 | [diff] [blame] | 3344 | context->nx = false; |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 3345 | context->gva_to_gpa = nonpaging_gva_to_gpa; |
| 3346 | context->root_level = 0; |
| 3347 | } else if (is_long_mode(vcpu)) { |
Joerg Roedel | 2d48a98 | 2010-09-10 17:31:01 +0200 | [diff] [blame] | 3348 | context->nx = is_nx(vcpu); |
Joerg Roedel | 52fde8d | 2010-09-10 17:30:44 +0200 | [diff] [blame] | 3349 | reset_rsvds_bits_mask(vcpu, context, PT64_ROOT_LEVEL); |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 3350 | context->gva_to_gpa = paging64_gva_to_gpa; |
| 3351 | context->root_level = PT64_ROOT_LEVEL; |
| 3352 | } else if (is_pae(vcpu)) { |
Joerg Roedel | 2d48a98 | 2010-09-10 17:31:01 +0200 | [diff] [blame] | 3353 | context->nx = is_nx(vcpu); |
Joerg Roedel | 52fde8d | 2010-09-10 17:30:44 +0200 | [diff] [blame] | 3354 | reset_rsvds_bits_mask(vcpu, context, PT32E_ROOT_LEVEL); |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 3355 | context->gva_to_gpa = paging64_gva_to_gpa; |
| 3356 | context->root_level = PT32E_ROOT_LEVEL; |
| 3357 | } else { |
Joerg Roedel | 2d48a98 | 2010-09-10 17:31:01 +0200 | [diff] [blame] | 3358 | context->nx = false; |
Joerg Roedel | 52fde8d | 2010-09-10 17:30:44 +0200 | [diff] [blame] | 3359 | reset_rsvds_bits_mask(vcpu, context, PT32_ROOT_LEVEL); |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 3360 | context->gva_to_gpa = paging32_gva_to_gpa; |
| 3361 | context->root_level = PT32_ROOT_LEVEL; |
| 3362 | } |
| 3363 | |
| 3364 | return 0; |
| 3365 | } |
| 3366 | |
Joerg Roedel | 52fde8d | 2010-09-10 17:30:44 +0200 | [diff] [blame] | 3367 | int kvm_init_shadow_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *context) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3368 | { |
Avi Kivity | a770f6f | 2008-12-21 19:20:09 +0200 | [diff] [blame] | 3369 | int r; |
Avi Kivity | 411c588 | 2011-06-06 16:11:54 +0300 | [diff] [blame] | 3370 | bool smep = kvm_read_cr4_bits(vcpu, X86_CR4_SMEP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3371 | ASSERT(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3372 | ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3373 | |
| 3374 | if (!is_paging(vcpu)) |
Joerg Roedel | 52fde8d | 2010-09-10 17:30:44 +0200 | [diff] [blame] | 3375 | r = nonpaging_init_context(vcpu, context); |
Avi Kivity | a9058ec | 2006-12-29 16:49:37 -0800 | [diff] [blame] | 3376 | else if (is_long_mode(vcpu)) |
Joerg Roedel | 52fde8d | 2010-09-10 17:30:44 +0200 | [diff] [blame] | 3377 | r = paging64_init_context(vcpu, context); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3378 | else if (is_pae(vcpu)) |
Joerg Roedel | 52fde8d | 2010-09-10 17:30:44 +0200 | [diff] [blame] | 3379 | r = paging32E_init_context(vcpu, context); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3380 | else |
Joerg Roedel | 52fde8d | 2010-09-10 17:30:44 +0200 | [diff] [blame] | 3381 | r = paging32_init_context(vcpu, context); |
Avi Kivity | a770f6f | 2008-12-21 19:20:09 +0200 | [diff] [blame] | 3382 | |
Avi Kivity | 5b7e010 | 2010-04-14 19:20:03 +0300 | [diff] [blame] | 3383 | vcpu->arch.mmu.base_role.cr4_pae = !!is_pae(vcpu); |
Joerg Roedel | f43addd | 2010-09-10 17:30:40 +0200 | [diff] [blame] | 3384 | vcpu->arch.mmu.base_role.cr0_wp = is_write_protection(vcpu); |
Avi Kivity | 411c588 | 2011-06-06 16:11:54 +0300 | [diff] [blame] | 3385 | vcpu->arch.mmu.base_role.smep_andnot_wp |
| 3386 | = smep && !is_write_protection(vcpu); |
Joerg Roedel | 52fde8d | 2010-09-10 17:30:44 +0200 | [diff] [blame] | 3387 | |
| 3388 | return r; |
| 3389 | } |
| 3390 | EXPORT_SYMBOL_GPL(kvm_init_shadow_mmu); |
| 3391 | |
| 3392 | static int init_kvm_softmmu(struct kvm_vcpu *vcpu) |
| 3393 | { |
Joerg Roedel | 14dfe85 | 2010-09-10 17:30:49 +0200 | [diff] [blame] | 3394 | int r = kvm_init_shadow_mmu(vcpu, vcpu->arch.walk_mmu); |
Joerg Roedel | 52fde8d | 2010-09-10 17:30:44 +0200 | [diff] [blame] | 3395 | |
Joerg Roedel | 14dfe85 | 2010-09-10 17:30:49 +0200 | [diff] [blame] | 3396 | vcpu->arch.walk_mmu->set_cr3 = kvm_x86_ops->set_cr3; |
| 3397 | vcpu->arch.walk_mmu->get_cr3 = get_cr3; |
Avi Kivity | e4e517b | 2011-07-28 11:36:17 +0300 | [diff] [blame] | 3398 | vcpu->arch.walk_mmu->get_pdptr = kvm_pdptr_read; |
Joerg Roedel | 14dfe85 | 2010-09-10 17:30:49 +0200 | [diff] [blame] | 3399 | vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault; |
Avi Kivity | a770f6f | 2008-12-21 19:20:09 +0200 | [diff] [blame] | 3400 | |
| 3401 | return r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3402 | } |
| 3403 | |
Joerg Roedel | 02f59dc | 2010-09-10 17:30:54 +0200 | [diff] [blame] | 3404 | static int init_kvm_nested_mmu(struct kvm_vcpu *vcpu) |
| 3405 | { |
| 3406 | struct kvm_mmu *g_context = &vcpu->arch.nested_mmu; |
| 3407 | |
| 3408 | g_context->get_cr3 = get_cr3; |
Avi Kivity | e4e517b | 2011-07-28 11:36:17 +0300 | [diff] [blame] | 3409 | g_context->get_pdptr = kvm_pdptr_read; |
Joerg Roedel | 02f59dc | 2010-09-10 17:30:54 +0200 | [diff] [blame] | 3410 | g_context->inject_page_fault = kvm_inject_page_fault; |
| 3411 | |
| 3412 | /* |
| 3413 | * Note that arch.mmu.gva_to_gpa translates l2_gva to l1_gpa. The |
| 3414 | * translation of l2_gpa to l1_gpa addresses is done using the |
| 3415 | * arch.nested_mmu.gva_to_gpa function. Basically the gva_to_gpa |
| 3416 | * functions between mmu and nested_mmu are swapped. |
| 3417 | */ |
| 3418 | if (!is_paging(vcpu)) { |
Joerg Roedel | 2d48a98 | 2010-09-10 17:31:01 +0200 | [diff] [blame] | 3419 | g_context->nx = false; |
Joerg Roedel | 02f59dc | 2010-09-10 17:30:54 +0200 | [diff] [blame] | 3420 | g_context->root_level = 0; |
| 3421 | g_context->gva_to_gpa = nonpaging_gva_to_gpa_nested; |
| 3422 | } else if (is_long_mode(vcpu)) { |
Joerg Roedel | 2d48a98 | 2010-09-10 17:31:01 +0200 | [diff] [blame] | 3423 | g_context->nx = is_nx(vcpu); |
Joerg Roedel | 02f59dc | 2010-09-10 17:30:54 +0200 | [diff] [blame] | 3424 | reset_rsvds_bits_mask(vcpu, g_context, PT64_ROOT_LEVEL); |
| 3425 | g_context->root_level = PT64_ROOT_LEVEL; |
| 3426 | g_context->gva_to_gpa = paging64_gva_to_gpa_nested; |
| 3427 | } else if (is_pae(vcpu)) { |
Joerg Roedel | 2d48a98 | 2010-09-10 17:31:01 +0200 | [diff] [blame] | 3428 | g_context->nx = is_nx(vcpu); |
Joerg Roedel | 02f59dc | 2010-09-10 17:30:54 +0200 | [diff] [blame] | 3429 | reset_rsvds_bits_mask(vcpu, g_context, PT32E_ROOT_LEVEL); |
| 3430 | g_context->root_level = PT32E_ROOT_LEVEL; |
| 3431 | g_context->gva_to_gpa = paging64_gva_to_gpa_nested; |
| 3432 | } else { |
Joerg Roedel | 2d48a98 | 2010-09-10 17:31:01 +0200 | [diff] [blame] | 3433 | g_context->nx = false; |
Joerg Roedel | 02f59dc | 2010-09-10 17:30:54 +0200 | [diff] [blame] | 3434 | reset_rsvds_bits_mask(vcpu, g_context, PT32_ROOT_LEVEL); |
| 3435 | g_context->root_level = PT32_ROOT_LEVEL; |
| 3436 | g_context->gva_to_gpa = paging32_gva_to_gpa_nested; |
| 3437 | } |
| 3438 | |
| 3439 | return 0; |
| 3440 | } |
| 3441 | |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 3442 | static int init_kvm_mmu(struct kvm_vcpu *vcpu) |
| 3443 | { |
Joerg Roedel | 02f59dc | 2010-09-10 17:30:54 +0200 | [diff] [blame] | 3444 | if (mmu_is_nested(vcpu)) |
| 3445 | return init_kvm_nested_mmu(vcpu); |
| 3446 | else if (tdp_enabled) |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 3447 | return init_kvm_tdp_mmu(vcpu); |
| 3448 | else |
| 3449 | return init_kvm_softmmu(vcpu); |
| 3450 | } |
| 3451 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3452 | static void destroy_kvm_mmu(struct kvm_vcpu *vcpu) |
| 3453 | { |
| 3454 | ASSERT(vcpu); |
Sheng Yang | 62ad075 | 2010-05-12 16:40:41 +0800 | [diff] [blame] | 3455 | if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) |
| 3456 | /* mmu.free() should set root_hpa = INVALID_PAGE */ |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3457 | vcpu->arch.mmu.free(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3458 | } |
| 3459 | |
| 3460 | int kvm_mmu_reset_context(struct kvm_vcpu *vcpu) |
| 3461 | { |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 3462 | destroy_kvm_mmu(vcpu); |
Marcelo Tosatti | f8f7e5e | 2011-06-21 14:00:10 -0300 | [diff] [blame] | 3463 | return init_kvm_mmu(vcpu); |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 3464 | } |
Eddie Dong | 8668a3c | 2007-10-10 14:26:45 +0800 | [diff] [blame] | 3465 | EXPORT_SYMBOL_GPL(kvm_mmu_reset_context); |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 3466 | |
| 3467 | int kvm_mmu_load(struct kvm_vcpu *vcpu) |
| 3468 | { |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 3469 | int r; |
| 3470 | |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 3471 | r = mmu_topup_memory_caches(vcpu); |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 3472 | if (r) |
| 3473 | goto out; |
Marcelo Tosatti | 8986ecc | 2009-05-12 18:55:45 -0300 | [diff] [blame] | 3474 | r = mmu_alloc_roots(vcpu); |
Avi Kivity | 8facbbf | 2010-05-04 12:58:32 +0300 | [diff] [blame] | 3475 | spin_lock(&vcpu->kvm->mmu_lock); |
Marcelo Tosatti | 0ba73cd | 2008-09-23 13:18:34 -0300 | [diff] [blame] | 3476 | mmu_sync_roots(vcpu); |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 3477 | spin_unlock(&vcpu->kvm->mmu_lock); |
Marcelo Tosatti | 8986ecc | 2009-05-12 18:55:45 -0300 | [diff] [blame] | 3478 | if (r) |
| 3479 | goto out; |
Sheng Yang | 3662cb1 | 2009-07-09 17:00:42 +0800 | [diff] [blame] | 3480 | /* set_cr3() should ensure TLB has been flushed */ |
Joerg Roedel | f43addd | 2010-09-10 17:30:40 +0200 | [diff] [blame] | 3481 | vcpu->arch.mmu.set_cr3(vcpu, vcpu->arch.mmu.root_hpa); |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 3482 | out: |
| 3483 | return r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3484 | } |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 3485 | EXPORT_SYMBOL_GPL(kvm_mmu_load); |
| 3486 | |
| 3487 | void kvm_mmu_unload(struct kvm_vcpu *vcpu) |
| 3488 | { |
| 3489 | mmu_free_roots(vcpu); |
| 3490 | } |
Joerg Roedel | 4b16184 | 2010-09-10 17:31:03 +0200 | [diff] [blame] | 3491 | EXPORT_SYMBOL_GPL(kvm_mmu_unload); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3492 | |
Avi Kivity | 0028425 | 2007-05-01 16:53:31 +0300 | [diff] [blame] | 3493 | static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu, |
Xiao Guangrong | 7c56252 | 2011-03-28 10:29:27 +0800 | [diff] [blame] | 3494 | struct kvm_mmu_page *sp, u64 *spte, |
| 3495 | const void *new) |
Avi Kivity | 0028425 | 2007-05-01 16:53:31 +0300 | [diff] [blame] | 3496 | { |
Marcelo Tosatti | 3094538 | 2008-06-11 20:32:40 -0300 | [diff] [blame] | 3497 | if (sp->role.level != PT_PAGE_TABLE_LEVEL) { |
Joerg Roedel | 7e4e405 | 2009-07-27 16:30:46 +0200 | [diff] [blame] | 3498 | ++vcpu->kvm->stat.mmu_pde_zapped; |
| 3499 | return; |
Marcelo Tosatti | 3094538 | 2008-06-11 20:32:40 -0300 | [diff] [blame] | 3500 | } |
Avi Kivity | 0028425 | 2007-05-01 16:53:31 +0300 | [diff] [blame] | 3501 | |
Avi Kivity | 4cee576 | 2007-11-18 16:37:07 +0200 | [diff] [blame] | 3502 | ++vcpu->kvm->stat.mmu_pte_updated; |
Xiao Guangrong | 7c56252 | 2011-03-28 10:29:27 +0800 | [diff] [blame] | 3503 | vcpu->arch.mmu.update_pte(vcpu, sp, spte, new); |
Avi Kivity | 0028425 | 2007-05-01 16:53:31 +0300 | [diff] [blame] | 3504 | } |
| 3505 | |
Avi Kivity | 79539ce | 2007-11-21 02:06:21 +0200 | [diff] [blame] | 3506 | static bool need_remote_flush(u64 old, u64 new) |
| 3507 | { |
| 3508 | if (!is_shadow_present_pte(old)) |
| 3509 | return false; |
| 3510 | if (!is_shadow_present_pte(new)) |
| 3511 | return true; |
| 3512 | if ((old ^ new) & PT64_BASE_ADDR_MASK) |
| 3513 | return true; |
| 3514 | old ^= PT64_NX_MASK; |
| 3515 | new ^= PT64_NX_MASK; |
| 3516 | return (old & ~new & PT64_PERM_MASK) != 0; |
| 3517 | } |
| 3518 | |
Xiao Guangrong | 0671a8e | 2010-06-04 21:56:59 +0800 | [diff] [blame] | 3519 | static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, bool zap_page, |
| 3520 | bool remote_flush, bool local_flush) |
Avi Kivity | 79539ce | 2007-11-21 02:06:21 +0200 | [diff] [blame] | 3521 | { |
Xiao Guangrong | 0671a8e | 2010-06-04 21:56:59 +0800 | [diff] [blame] | 3522 | if (zap_page) |
| 3523 | return; |
| 3524 | |
| 3525 | if (remote_flush) |
Avi Kivity | 79539ce | 2007-11-21 02:06:21 +0200 | [diff] [blame] | 3526 | kvm_flush_remote_tlbs(vcpu->kvm); |
Xiao Guangrong | 0671a8e | 2010-06-04 21:56:59 +0800 | [diff] [blame] | 3527 | else if (local_flush) |
Avi Kivity | 79539ce | 2007-11-21 02:06:21 +0200 | [diff] [blame] | 3528 | kvm_mmu_flush_tlb(vcpu); |
| 3529 | } |
| 3530 | |
Xiao Guangrong | 889e5cb | 2011-09-22 16:57:23 +0800 | [diff] [blame] | 3531 | static u64 mmu_pte_write_fetch_gpte(struct kvm_vcpu *vcpu, gpa_t *gpa, |
| 3532 | const u8 *new, int *bytes) |
Avi Kivity | da4a00f | 2007-01-05 16:36:44 -0800 | [diff] [blame] | 3533 | { |
Xiao Guangrong | 889e5cb | 2011-09-22 16:57:23 +0800 | [diff] [blame] | 3534 | u64 gentry; |
| 3535 | int r; |
Avi Kivity | 72016f3 | 2010-03-15 13:59:53 +0200 | [diff] [blame] | 3536 | |
Avi Kivity | 08e850c | 2010-03-15 13:59:57 +0200 | [diff] [blame] | 3537 | /* |
| 3538 | * Assume that the pte write on a page table of the same type |
Xiao Guangrong | 49b26e2 | 2011-03-04 19:00:00 +0800 | [diff] [blame] | 3539 | * as the current vcpu paging mode since we update the sptes only |
| 3540 | * when they have the same mode. |
Avi Kivity | 08e850c | 2010-03-15 13:59:57 +0200 | [diff] [blame] | 3541 | */ |
Xiao Guangrong | 889e5cb | 2011-09-22 16:57:23 +0800 | [diff] [blame] | 3542 | if (is_pae(vcpu) && *bytes == 4) { |
Avi Kivity | 08e850c | 2010-03-15 13:59:57 +0200 | [diff] [blame] | 3543 | /* Handle a 32-bit guest writing two halves of a 64-bit gpte */ |
Xiao Guangrong | 889e5cb | 2011-09-22 16:57:23 +0800 | [diff] [blame] | 3544 | *gpa &= ~(gpa_t)7; |
| 3545 | *bytes = 8; |
| 3546 | r = kvm_read_guest(vcpu->kvm, *gpa, &gentry, min(*bytes, 8)); |
Avi Kivity | 08e850c | 2010-03-15 13:59:57 +0200 | [diff] [blame] | 3547 | if (r) |
| 3548 | gentry = 0; |
| 3549 | new = (const u8 *)&gentry; |
| 3550 | } |
| 3551 | |
Xiao Guangrong | 889e5cb | 2011-09-22 16:57:23 +0800 | [diff] [blame] | 3552 | switch (*bytes) { |
Avi Kivity | 72016f3 | 2010-03-15 13:59:53 +0200 | [diff] [blame] | 3553 | case 4: |
| 3554 | gentry = *(const u32 *)new; |
| 3555 | break; |
| 3556 | case 8: |
| 3557 | gentry = *(const u64 *)new; |
| 3558 | break; |
| 3559 | default: |
| 3560 | gentry = 0; |
| 3561 | break; |
| 3562 | } |
| 3563 | |
Xiao Guangrong | 889e5cb | 2011-09-22 16:57:23 +0800 | [diff] [blame] | 3564 | return gentry; |
| 3565 | } |
| 3566 | |
| 3567 | /* |
| 3568 | * If we're seeing too many writes to a page, it may no longer be a page table, |
| 3569 | * or we may be forking, in which case it is better to unmap the page. |
| 3570 | */ |
Xiao Guangrong | a30f47c | 2011-09-22 16:58:36 +0800 | [diff] [blame] | 3571 | static bool detect_write_flooding(struct kvm_mmu_page *sp, u64 *spte) |
Xiao Guangrong | 889e5cb | 2011-09-22 16:57:23 +0800 | [diff] [blame] | 3572 | { |
Xiao Guangrong | a30f47c | 2011-09-22 16:58:36 +0800 | [diff] [blame] | 3573 | /* |
| 3574 | * Skip write-flooding detected for the sp whose level is 1, because |
| 3575 | * it can become unsync, then the guest page is not write-protected. |
| 3576 | */ |
| 3577 | if (sp->role.level == 1) |
| 3578 | return false; |
Xiao Guangrong | 889e5cb | 2011-09-22 16:57:23 +0800 | [diff] [blame] | 3579 | |
Xiao Guangrong | a30f47c | 2011-09-22 16:58:36 +0800 | [diff] [blame] | 3580 | return ++sp->write_flooding_count >= 3; |
Xiao Guangrong | 889e5cb | 2011-09-22 16:57:23 +0800 | [diff] [blame] | 3581 | } |
| 3582 | |
| 3583 | /* |
| 3584 | * Misaligned accesses are too much trouble to fix up; also, they usually |
| 3585 | * indicate a page is not used as a page table. |
| 3586 | */ |
| 3587 | static bool detect_write_misaligned(struct kvm_mmu_page *sp, gpa_t gpa, |
| 3588 | int bytes) |
| 3589 | { |
| 3590 | unsigned offset, pte_size, misaligned; |
| 3591 | |
| 3592 | pgprintk("misaligned: gpa %llx bytes %d role %x\n", |
| 3593 | gpa, bytes, sp->role.word); |
| 3594 | |
| 3595 | offset = offset_in_page(gpa); |
| 3596 | pte_size = sp->role.cr4_pae ? 8 : 4; |
Xiao Guangrong | 5d9ca30 | 2011-09-22 16:57:55 +0800 | [diff] [blame] | 3597 | |
| 3598 | /* |
| 3599 | * Sometimes, the OS only writes the last one bytes to update status |
| 3600 | * bits, for example, in linux, andb instruction is used in clear_bit(). |
| 3601 | */ |
| 3602 | if (!(offset & (pte_size - 1)) && bytes == 1) |
| 3603 | return false; |
| 3604 | |
Xiao Guangrong | 889e5cb | 2011-09-22 16:57:23 +0800 | [diff] [blame] | 3605 | misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1); |
| 3606 | misaligned |= bytes < 4; |
| 3607 | |
| 3608 | return misaligned; |
| 3609 | } |
| 3610 | |
| 3611 | static u64 *get_written_sptes(struct kvm_mmu_page *sp, gpa_t gpa, int *nspte) |
| 3612 | { |
| 3613 | unsigned page_offset, quadrant; |
| 3614 | u64 *spte; |
| 3615 | int level; |
| 3616 | |
| 3617 | page_offset = offset_in_page(gpa); |
| 3618 | level = sp->role.level; |
| 3619 | *nspte = 1; |
| 3620 | if (!sp->role.cr4_pae) { |
| 3621 | page_offset <<= 1; /* 32->64 */ |
| 3622 | /* |
| 3623 | * A 32-bit pde maps 4MB while the shadow pdes map |
| 3624 | * only 2MB. So we need to double the offset again |
| 3625 | * and zap two pdes instead of one. |
| 3626 | */ |
| 3627 | if (level == PT32_ROOT_LEVEL) { |
| 3628 | page_offset &= ~7; /* kill rounding error */ |
| 3629 | page_offset <<= 1; |
| 3630 | *nspte = 2; |
| 3631 | } |
| 3632 | quadrant = page_offset >> PAGE_SHIFT; |
| 3633 | page_offset &= ~PAGE_MASK; |
| 3634 | if (quadrant != sp->role.quadrant) |
| 3635 | return NULL; |
| 3636 | } |
| 3637 | |
| 3638 | spte = &sp->spt[page_offset / sizeof(*spte)]; |
| 3639 | return spte; |
| 3640 | } |
| 3641 | |
| 3642 | void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa, |
| 3643 | const u8 *new, int bytes) |
| 3644 | { |
| 3645 | gfn_t gfn = gpa >> PAGE_SHIFT; |
| 3646 | union kvm_mmu_page_role mask = { .word = 0 }; |
| 3647 | struct kvm_mmu_page *sp; |
| 3648 | struct hlist_node *node; |
| 3649 | LIST_HEAD(invalid_list); |
| 3650 | u64 entry, gentry, *spte; |
| 3651 | int npte; |
Xiao Guangrong | a30f47c | 2011-09-22 16:58:36 +0800 | [diff] [blame] | 3652 | bool remote_flush, local_flush, zap_page; |
Xiao Guangrong | 889e5cb | 2011-09-22 16:57:23 +0800 | [diff] [blame] | 3653 | |
| 3654 | /* |
| 3655 | * If we don't have indirect shadow pages, it means no page is |
| 3656 | * write-protected, so we can exit simply. |
| 3657 | */ |
| 3658 | if (!ACCESS_ONCE(vcpu->kvm->arch.indirect_shadow_pages)) |
| 3659 | return; |
| 3660 | |
| 3661 | zap_page = remote_flush = local_flush = false; |
| 3662 | |
| 3663 | pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes); |
| 3664 | |
| 3665 | gentry = mmu_pte_write_fetch_gpte(vcpu, &gpa, new, &bytes); |
| 3666 | |
| 3667 | /* |
| 3668 | * No need to care whether allocation memory is successful |
| 3669 | * or not since pte prefetch is skiped if it does not have |
| 3670 | * enough objects in the cache. |
| 3671 | */ |
| 3672 | mmu_topup_memory_caches(vcpu); |
| 3673 | |
| 3674 | spin_lock(&vcpu->kvm->mmu_lock); |
| 3675 | ++vcpu->kvm->stat.mmu_pte_write; |
Xiao Guangrong | 0375f7f | 2011-11-28 20:41:00 +0800 | [diff] [blame^] | 3676 | kvm_mmu_audit(vcpu, AUDIT_PRE_PTE_WRITE); |
Xiao Guangrong | 889e5cb | 2011-09-22 16:57:23 +0800 | [diff] [blame] | 3677 | |
Xiao Guangrong | fa1de2b | 2010-07-16 11:19:51 +0800 | [diff] [blame] | 3678 | mask.cr0_wp = mask.cr4_pae = mask.nxe = 1; |
Xiao Guangrong | f41d335 | 2010-06-04 21:56:11 +0800 | [diff] [blame] | 3679 | for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn, node) { |
Xiao Guangrong | a30f47c | 2011-09-22 16:58:36 +0800 | [diff] [blame] | 3680 | spte = get_written_sptes(sp, gpa, &npte); |
Xiao Guangrong | 889e5cb | 2011-09-22 16:57:23 +0800 | [diff] [blame] | 3681 | |
Xiao Guangrong | a30f47c | 2011-09-22 16:58:36 +0800 | [diff] [blame] | 3682 | if (detect_write_misaligned(sp, gpa, bytes) || |
| 3683 | detect_write_flooding(sp, spte)) { |
Xiao Guangrong | 0671a8e | 2010-06-04 21:56:59 +0800 | [diff] [blame] | 3684 | zap_page |= !!kvm_mmu_prepare_zap_page(vcpu->kvm, sp, |
Xiao Guangrong | f41d335 | 2010-06-04 21:56:11 +0800 | [diff] [blame] | 3685 | &invalid_list); |
Avi Kivity | 4cee576 | 2007-11-18 16:37:07 +0200 | [diff] [blame] | 3686 | ++vcpu->kvm->stat.mmu_flooded; |
Avi Kivity | 0e7bc4b | 2007-01-05 16:36:48 -0800 | [diff] [blame] | 3687 | continue; |
| 3688 | } |
Xiao Guangrong | 889e5cb | 2011-09-22 16:57:23 +0800 | [diff] [blame] | 3689 | |
| 3690 | spte = get_written_sptes(sp, gpa, &npte); |
| 3691 | if (!spte) |
| 3692 | continue; |
| 3693 | |
Xiao Guangrong | 0671a8e | 2010-06-04 21:56:59 +0800 | [diff] [blame] | 3694 | local_flush = true; |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 3695 | while (npte--) { |
Avi Kivity | 79539ce | 2007-11-21 02:06:21 +0200 | [diff] [blame] | 3696 | entry = *spte; |
Xiao Guangrong | 38e3b2b | 2011-05-15 23:27:52 +0800 | [diff] [blame] | 3697 | mmu_page_zap_pte(vcpu->kvm, sp, spte); |
Xiao Guangrong | fa1de2b | 2010-07-16 11:19:51 +0800 | [diff] [blame] | 3698 | if (gentry && |
| 3699 | !((sp->role.word ^ vcpu->arch.mmu.base_role.word) |
Xiao Guangrong | f759e2b | 2011-09-22 16:53:17 +0800 | [diff] [blame] | 3700 | & mask.word) && rmap_can_add(vcpu)) |
Xiao Guangrong | 7c56252 | 2011-03-28 10:29:27 +0800 | [diff] [blame] | 3701 | mmu_pte_write_new_pte(vcpu, sp, spte, &gentry); |
Xiao Guangrong | 0671a8e | 2010-06-04 21:56:59 +0800 | [diff] [blame] | 3702 | if (!remote_flush && need_remote_flush(entry, *spte)) |
| 3703 | remote_flush = true; |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 3704 | ++spte; |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 3705 | } |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 3706 | } |
Xiao Guangrong | 0671a8e | 2010-06-04 21:56:59 +0800 | [diff] [blame] | 3707 | mmu_pte_write_flush_tlb(vcpu, zap_page, remote_flush, local_flush); |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 3708 | kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list); |
Xiao Guangrong | 0375f7f | 2011-11-28 20:41:00 +0800 | [diff] [blame^] | 3709 | kvm_mmu_audit(vcpu, AUDIT_POST_PTE_WRITE); |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 3710 | spin_unlock(&vcpu->kvm->mmu_lock); |
Avi Kivity | da4a00f | 2007-01-05 16:36:44 -0800 | [diff] [blame] | 3711 | } |
| 3712 | |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 3713 | int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva) |
| 3714 | { |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3715 | gpa_t gpa; |
| 3716 | int r; |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 3717 | |
Joerg Roedel | c5a78f2b | 2010-09-10 17:30:39 +0200 | [diff] [blame] | 3718 | if (vcpu->arch.mmu.direct_map) |
Avi Kivity | 60f2478 | 2009-08-27 13:37:06 +0300 | [diff] [blame] | 3719 | return 0; |
| 3720 | |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3721 | gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3722 | |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3723 | r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT); |
Xiao Guangrong | 1cb3f3a | 2011-09-22 17:02:48 +0800 | [diff] [blame] | 3724 | |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3725 | return r; |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 3726 | } |
Avi Kivity | 577bdc4 | 2008-07-19 08:57:05 +0300 | [diff] [blame] | 3727 | EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt); |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 3728 | |
Avi Kivity | 22d95b1 | 2007-09-14 20:26:06 +0300 | [diff] [blame] | 3729 | void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu) |
Avi Kivity | ebeace8 | 2007-01-05 16:36:47 -0800 | [diff] [blame] | 3730 | { |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 3731 | LIST_HEAD(invalid_list); |
Xiao Guangrong | 103ad25 | 2010-06-04 21:54:38 +0800 | [diff] [blame] | 3732 | |
Dave Hansen | e0df7b9 | 2010-08-19 18:11:05 -0700 | [diff] [blame] | 3733 | while (kvm_mmu_available_pages(vcpu->kvm) < KVM_REFILL_PAGES && |
Izik Eidus | 3b80fff | 2009-07-28 15:26:58 -0300 | [diff] [blame] | 3734 | !list_empty(&vcpu->kvm->arch.active_mmu_pages)) { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 3735 | struct kvm_mmu_page *sp; |
Avi Kivity | ebeace8 | 2007-01-05 16:36:47 -0800 | [diff] [blame] | 3736 | |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 3737 | sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev, |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 3738 | struct kvm_mmu_page, link); |
Dave Hansen | e0df7b9 | 2010-08-19 18:11:05 -0700 | [diff] [blame] | 3739 | kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list); |
Avi Kivity | 4cee576 | 2007-11-18 16:37:07 +0200 | [diff] [blame] | 3740 | ++vcpu->kvm->stat.mmu_recycled; |
Avi Kivity | ebeace8 | 2007-01-05 16:36:47 -0800 | [diff] [blame] | 3741 | } |
Xiao Guangrong | aa6bd18 | 2011-07-12 03:26:40 +0800 | [diff] [blame] | 3742 | kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list); |
Avi Kivity | ebeace8 | 2007-01-05 16:36:47 -0800 | [diff] [blame] | 3743 | } |
Avi Kivity | ebeace8 | 2007-01-05 16:36:47 -0800 | [diff] [blame] | 3744 | |
Xiao Guangrong | 1cb3f3a | 2011-09-22 17:02:48 +0800 | [diff] [blame] | 3745 | static bool is_mmio_page_fault(struct kvm_vcpu *vcpu, gva_t addr) |
| 3746 | { |
| 3747 | if (vcpu->arch.mmu.direct_map || mmu_is_nested(vcpu)) |
| 3748 | return vcpu_match_mmio_gpa(vcpu, addr); |
| 3749 | |
| 3750 | return vcpu_match_mmio_gva(vcpu, addr); |
| 3751 | } |
| 3752 | |
Andre Przywara | dc25e89 | 2010-12-21 11:12:07 +0100 | [diff] [blame] | 3753 | int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code, |
| 3754 | void *insn, int insn_len) |
Avi Kivity | 3067714 | 2007-10-28 18:48:59 +0200 | [diff] [blame] | 3755 | { |
Xiao Guangrong | 1cb3f3a | 2011-09-22 17:02:48 +0800 | [diff] [blame] | 3756 | int r, emulation_type = EMULTYPE_RETRY; |
Avi Kivity | 3067714 | 2007-10-28 18:48:59 +0200 | [diff] [blame] | 3757 | enum emulation_result er; |
| 3758 | |
Gleb Natapov | 56028d0 | 2010-10-17 18:13:42 +0200 | [diff] [blame] | 3759 | r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code, false); |
Avi Kivity | 3067714 | 2007-10-28 18:48:59 +0200 | [diff] [blame] | 3760 | if (r < 0) |
| 3761 | goto out; |
| 3762 | |
| 3763 | if (!r) { |
| 3764 | r = 1; |
| 3765 | goto out; |
| 3766 | } |
| 3767 | |
Xiao Guangrong | 1cb3f3a | 2011-09-22 17:02:48 +0800 | [diff] [blame] | 3768 | if (is_mmio_page_fault(vcpu, cr2)) |
| 3769 | emulation_type = 0; |
| 3770 | |
| 3771 | er = x86_emulate_instruction(vcpu, cr2, emulation_type, insn, insn_len); |
Avi Kivity | 3067714 | 2007-10-28 18:48:59 +0200 | [diff] [blame] | 3772 | |
| 3773 | switch (er) { |
| 3774 | case EMULATE_DONE: |
| 3775 | return 1; |
| 3776 | case EMULATE_DO_MMIO: |
| 3777 | ++vcpu->stat.mmio_exits; |
Gleb Natapov | 6d77dbf | 2010-05-10 11:16:56 +0300 | [diff] [blame] | 3778 | /* fall through */ |
Avi Kivity | 3067714 | 2007-10-28 18:48:59 +0200 | [diff] [blame] | 3779 | case EMULATE_FAIL: |
Avi Kivity | 3f5d18a | 2009-06-11 15:43:28 +0300 | [diff] [blame] | 3780 | return 0; |
Avi Kivity | 3067714 | 2007-10-28 18:48:59 +0200 | [diff] [blame] | 3781 | default: |
| 3782 | BUG(); |
| 3783 | } |
| 3784 | out: |
Avi Kivity | 3067714 | 2007-10-28 18:48:59 +0200 | [diff] [blame] | 3785 | return r; |
| 3786 | } |
| 3787 | EXPORT_SYMBOL_GPL(kvm_mmu_page_fault); |
| 3788 | |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 3789 | void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva) |
| 3790 | { |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 3791 | vcpu->arch.mmu.invlpg(vcpu, gva); |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 3792 | kvm_mmu_flush_tlb(vcpu); |
| 3793 | ++vcpu->stat.invlpg; |
| 3794 | } |
| 3795 | EXPORT_SYMBOL_GPL(kvm_mmu_invlpg); |
| 3796 | |
Joerg Roedel | 1855267 | 2008-02-07 13:47:41 +0100 | [diff] [blame] | 3797 | void kvm_enable_tdp(void) |
| 3798 | { |
| 3799 | tdp_enabled = true; |
| 3800 | } |
| 3801 | EXPORT_SYMBOL_GPL(kvm_enable_tdp); |
| 3802 | |
Joerg Roedel | 5f4cb66 | 2008-07-14 20:36:36 +0200 | [diff] [blame] | 3803 | void kvm_disable_tdp(void) |
| 3804 | { |
| 3805 | tdp_enabled = false; |
| 3806 | } |
| 3807 | EXPORT_SYMBOL_GPL(kvm_disable_tdp); |
| 3808 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3809 | static void free_mmu_pages(struct kvm_vcpu *vcpu) |
| 3810 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3811 | free_page((unsigned long)vcpu->arch.mmu.pae_root); |
Joerg Roedel | 81407ca | 2010-09-10 17:31:00 +0200 | [diff] [blame] | 3812 | if (vcpu->arch.mmu.lm_root != NULL) |
| 3813 | free_page((unsigned long)vcpu->arch.mmu.lm_root); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3814 | } |
| 3815 | |
| 3816 | static int alloc_mmu_pages(struct kvm_vcpu *vcpu) |
| 3817 | { |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 3818 | struct page *page; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3819 | int i; |
| 3820 | |
| 3821 | ASSERT(vcpu); |
| 3822 | |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 3823 | /* |
| 3824 | * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64. |
| 3825 | * Therefore we need to allocate shadow page tables in the first |
| 3826 | * 4GB of memory, which happens to fit the DMA32 zone. |
| 3827 | */ |
| 3828 | page = alloc_page(GFP_KERNEL | __GFP_DMA32); |
| 3829 | if (!page) |
Wei Yongjun | d7fa6ab | 2010-01-22 16:55:05 +0800 | [diff] [blame] | 3830 | return -ENOMEM; |
| 3831 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3832 | vcpu->arch.mmu.pae_root = page_address(page); |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 3833 | for (i = 0; i < 4; ++i) |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3834 | vcpu->arch.mmu.pae_root[i] = INVALID_PAGE; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 3835 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3836 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3837 | } |
| 3838 | |
Ingo Molnar | 8018c27 | 2006-12-29 16:50:01 -0800 | [diff] [blame] | 3839 | int kvm_mmu_create(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3840 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3841 | ASSERT(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3842 | ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3843 | |
Ingo Molnar | 8018c27 | 2006-12-29 16:50:01 -0800 | [diff] [blame] | 3844 | return alloc_mmu_pages(vcpu); |
| 3845 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3846 | |
Ingo Molnar | 8018c27 | 2006-12-29 16:50:01 -0800 | [diff] [blame] | 3847 | int kvm_mmu_setup(struct kvm_vcpu *vcpu) |
| 3848 | { |
| 3849 | ASSERT(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3850 | ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa)); |
Avi Kivity | 2c26495 | 2006-12-22 01:05:28 -0800 | [diff] [blame] | 3851 | |
Ingo Molnar | 8018c27 | 2006-12-29 16:50:01 -0800 | [diff] [blame] | 3852 | return init_kvm_mmu(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3853 | } |
| 3854 | |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 3855 | void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3856 | { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 3857 | struct kvm_mmu_page *sp; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3858 | |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 3859 | list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3860 | int i; |
| 3861 | u64 *pt; |
| 3862 | |
Sheng Yang | 291f26b | 2008-10-16 17:30:57 +0800 | [diff] [blame] | 3863 | if (!test_bit(slot, sp->slot_bitmap)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3864 | continue; |
| 3865 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 3866 | pt = sp->spt; |
Avi Kivity | 8234b22 | 2010-12-27 12:08:45 +0200 | [diff] [blame] | 3867 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) { |
Xiao Guangrong | da8dc75 | 2011-03-04 18:56:41 +0800 | [diff] [blame] | 3868 | if (!is_shadow_present_pte(pt[i]) || |
| 3869 | !is_last_spte(pt[i], sp->role.level)) |
| 3870 | continue; |
| 3871 | |
| 3872 | if (is_large_pte(pt[i])) { |
Xiao Guangrong | c370795 | 2011-07-12 03:28:04 +0800 | [diff] [blame] | 3873 | drop_spte(kvm, &pt[i]); |
Avi Kivity | 8234b22 | 2010-12-27 12:08:45 +0200 | [diff] [blame] | 3874 | --kvm->stat.lpages; |
Xiao Guangrong | da8dc75 | 2011-03-04 18:56:41 +0800 | [diff] [blame] | 3875 | continue; |
Avi Kivity | 8234b22 | 2010-12-27 12:08:45 +0200 | [diff] [blame] | 3876 | } |
Xiao Guangrong | da8dc75 | 2011-03-04 18:56:41 +0800 | [diff] [blame] | 3877 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3878 | /* avoid RMW */ |
Gui Jianfeng | 01c168a | 2010-05-27 16:09:48 +0800 | [diff] [blame] | 3879 | if (is_writable_pte(pt[i])) |
Xiao Guangrong | 1df9f2d | 2011-07-12 03:30:35 +0800 | [diff] [blame] | 3880 | mmu_spte_update(&pt[i], |
| 3881 | pt[i] & ~PT_WRITABLE_MASK); |
Avi Kivity | 8234b22 | 2010-12-27 12:08:45 +0200 | [diff] [blame] | 3882 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3883 | } |
Avi Kivity | 171d595 | 2008-08-27 16:40:51 +0300 | [diff] [blame] | 3884 | kvm_flush_remote_tlbs(kvm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3885 | } |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 3886 | |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 3887 | void kvm_mmu_zap_all(struct kvm *kvm) |
Dor Laor | e0fa826 | 2007-03-30 13:06:33 +0300 | [diff] [blame] | 3888 | { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 3889 | struct kvm_mmu_page *sp, *node; |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 3890 | LIST_HEAD(invalid_list); |
Dor Laor | e0fa826 | 2007-03-30 13:06:33 +0300 | [diff] [blame] | 3891 | |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 3892 | spin_lock(&kvm->mmu_lock); |
Xiao Guangrong | 3246af0 | 2010-04-16 16:35:54 +0800 | [diff] [blame] | 3893 | restart: |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 3894 | list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link) |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 3895 | if (kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list)) |
Xiao Guangrong | 3246af0 | 2010-04-16 16:35:54 +0800 | [diff] [blame] | 3896 | goto restart; |
| 3897 | |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 3898 | kvm_mmu_commit_zap_page(kvm, &invalid_list); |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 3899 | spin_unlock(&kvm->mmu_lock); |
Dor Laor | e0fa826 | 2007-03-30 13:06:33 +0300 | [diff] [blame] | 3900 | } |
| 3901 | |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 3902 | static int kvm_mmu_remove_some_alloc_mmu_pages(struct kvm *kvm, |
| 3903 | struct list_head *invalid_list) |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 3904 | { |
| 3905 | struct kvm_mmu_page *page; |
| 3906 | |
| 3907 | page = container_of(kvm->arch.active_mmu_pages.prev, |
| 3908 | struct kvm_mmu_page, link); |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 3909 | return kvm_mmu_prepare_zap_page(kvm, page, invalid_list); |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 3910 | } |
| 3911 | |
Ying Han | 1495f23 | 2011-05-24 17:12:27 -0700 | [diff] [blame] | 3912 | static int mmu_shrink(struct shrinker *shrink, struct shrink_control *sc) |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 3913 | { |
| 3914 | struct kvm *kvm; |
| 3915 | struct kvm *kvm_freed = NULL; |
Ying Han | 1495f23 | 2011-05-24 17:12:27 -0700 | [diff] [blame] | 3916 | int nr_to_scan = sc->nr_to_scan; |
Dave Hansen | 45221ab | 2010-08-19 18:11:37 -0700 | [diff] [blame] | 3917 | |
| 3918 | if (nr_to_scan == 0) |
| 3919 | goto out; |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 3920 | |
Jan Kiszka | e935b83 | 2011-02-08 12:55:33 +0100 | [diff] [blame] | 3921 | raw_spin_lock(&kvm_lock); |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 3922 | |
| 3923 | list_for_each_entry(kvm, &vm_list, vm_list) { |
Dave Hansen | 45221ab | 2010-08-19 18:11:37 -0700 | [diff] [blame] | 3924 | int idx, freed_pages; |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 3925 | LIST_HEAD(invalid_list); |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 3926 | |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 3927 | idx = srcu_read_lock(&kvm->srcu); |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 3928 | spin_lock(&kvm->mmu_lock); |
Dave Hansen | 45221ab | 2010-08-19 18:11:37 -0700 | [diff] [blame] | 3929 | if (!kvm_freed && nr_to_scan > 0 && |
| 3930 | kvm->arch.n_used_mmu_pages > 0) { |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 3931 | freed_pages = kvm_mmu_remove_some_alloc_mmu_pages(kvm, |
| 3932 | &invalid_list); |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 3933 | kvm_freed = kvm; |
| 3934 | } |
| 3935 | nr_to_scan--; |
| 3936 | |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 3937 | kvm_mmu_commit_zap_page(kvm, &invalid_list); |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 3938 | spin_unlock(&kvm->mmu_lock); |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 3939 | srcu_read_unlock(&kvm->srcu, idx); |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 3940 | } |
| 3941 | if (kvm_freed) |
| 3942 | list_move_tail(&kvm_freed->vm_list, &vm_list); |
| 3943 | |
Jan Kiszka | e935b83 | 2011-02-08 12:55:33 +0100 | [diff] [blame] | 3944 | raw_spin_unlock(&kvm_lock); |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 3945 | |
Dave Hansen | 45221ab | 2010-08-19 18:11:37 -0700 | [diff] [blame] | 3946 | out: |
| 3947 | return percpu_counter_read_positive(&kvm_total_used_mmu_pages); |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 3948 | } |
| 3949 | |
| 3950 | static struct shrinker mmu_shrinker = { |
| 3951 | .shrink = mmu_shrink, |
| 3952 | .seeks = DEFAULT_SEEKS * 10, |
| 3953 | }; |
| 3954 | |
Ingo Molnar | 2ddfd20 | 2008-05-22 10:37:48 +0200 | [diff] [blame] | 3955 | static void mmu_destroy_caches(void) |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 3956 | { |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 3957 | if (pte_list_desc_cache) |
| 3958 | kmem_cache_destroy(pte_list_desc_cache); |
Avi Kivity | d3d25b0 | 2007-05-30 12:34:53 +0300 | [diff] [blame] | 3959 | if (mmu_page_header_cache) |
| 3960 | kmem_cache_destroy(mmu_page_header_cache); |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 3961 | } |
| 3962 | |
| 3963 | int kvm_mmu_module_init(void) |
| 3964 | { |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 3965 | pte_list_desc_cache = kmem_cache_create("pte_list_desc", |
| 3966 | sizeof(struct pte_list_desc), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 3967 | 0, 0, NULL); |
Xiao Guangrong | 53c07b1 | 2011-05-15 23:26:20 +0800 | [diff] [blame] | 3968 | if (!pte_list_desc_cache) |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 3969 | goto nomem; |
| 3970 | |
Avi Kivity | d3d25b0 | 2007-05-30 12:34:53 +0300 | [diff] [blame] | 3971 | mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header", |
| 3972 | sizeof(struct kvm_mmu_page), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 3973 | 0, 0, NULL); |
Avi Kivity | d3d25b0 | 2007-05-30 12:34:53 +0300 | [diff] [blame] | 3974 | if (!mmu_page_header_cache) |
| 3975 | goto nomem; |
| 3976 | |
Wei Yongjun | 45bf21a | 2010-08-23 16:13:15 +0800 | [diff] [blame] | 3977 | if (percpu_counter_init(&kvm_total_used_mmu_pages, 0)) |
| 3978 | goto nomem; |
| 3979 | |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 3980 | register_shrinker(&mmu_shrinker); |
| 3981 | |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 3982 | return 0; |
| 3983 | |
| 3984 | nomem: |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 3985 | mmu_destroy_caches(); |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 3986 | return -ENOMEM; |
| 3987 | } |
| 3988 | |
Zhang Xiantao | 3ad82a7 | 2007-11-20 13:11:38 +0800 | [diff] [blame] | 3989 | /* |
| 3990 | * Caculate mmu pages needed for kvm. |
| 3991 | */ |
| 3992 | unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm) |
| 3993 | { |
Zhang Xiantao | 3ad82a7 | 2007-11-20 13:11:38 +0800 | [diff] [blame] | 3994 | unsigned int nr_mmu_pages; |
| 3995 | unsigned int nr_pages = 0; |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 3996 | struct kvm_memslots *slots; |
Xiao Guangrong | be6ba0f | 2011-11-24 17:39:18 +0800 | [diff] [blame] | 3997 | struct kvm_memory_slot *memslot; |
Zhang Xiantao | 3ad82a7 | 2007-11-20 13:11:38 +0800 | [diff] [blame] | 3998 | |
Lai Jiangshan | 90d83dc | 2010-04-19 17:41:23 +0800 | [diff] [blame] | 3999 | slots = kvm_memslots(kvm); |
| 4000 | |
Xiao Guangrong | be6ba0f | 2011-11-24 17:39:18 +0800 | [diff] [blame] | 4001 | kvm_for_each_memslot(memslot, slots) |
| 4002 | nr_pages += memslot->npages; |
Zhang Xiantao | 3ad82a7 | 2007-11-20 13:11:38 +0800 | [diff] [blame] | 4003 | |
| 4004 | nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000; |
| 4005 | nr_mmu_pages = max(nr_mmu_pages, |
| 4006 | (unsigned int) KVM_MIN_ALLOC_MMU_PAGES); |
| 4007 | |
| 4008 | return nr_mmu_pages; |
| 4009 | } |
| 4010 | |
Marcelo Tosatti | 94d8b05 | 2009-06-11 12:07:42 -0300 | [diff] [blame] | 4011 | int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4]) |
| 4012 | { |
| 4013 | struct kvm_shadow_walk_iterator iterator; |
Xiao Guangrong | c2a2ac2 | 2011-07-12 03:32:13 +0800 | [diff] [blame] | 4014 | u64 spte; |
Marcelo Tosatti | 94d8b05 | 2009-06-11 12:07:42 -0300 | [diff] [blame] | 4015 | int nr_sptes = 0; |
| 4016 | |
Xiao Guangrong | c2a2ac2 | 2011-07-12 03:32:13 +0800 | [diff] [blame] | 4017 | walk_shadow_page_lockless_begin(vcpu); |
| 4018 | for_each_shadow_entry_lockless(vcpu, addr, iterator, spte) { |
| 4019 | sptes[iterator.level-1] = spte; |
Marcelo Tosatti | 94d8b05 | 2009-06-11 12:07:42 -0300 | [diff] [blame] | 4020 | nr_sptes++; |
Xiao Guangrong | c2a2ac2 | 2011-07-12 03:32:13 +0800 | [diff] [blame] | 4021 | if (!is_shadow_present_pte(spte)) |
Marcelo Tosatti | 94d8b05 | 2009-06-11 12:07:42 -0300 | [diff] [blame] | 4022 | break; |
| 4023 | } |
Xiao Guangrong | c2a2ac2 | 2011-07-12 03:32:13 +0800 | [diff] [blame] | 4024 | walk_shadow_page_lockless_end(vcpu); |
Marcelo Tosatti | 94d8b05 | 2009-06-11 12:07:42 -0300 | [diff] [blame] | 4025 | |
| 4026 | return nr_sptes; |
| 4027 | } |
| 4028 | EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy); |
| 4029 | |
Xiao Guangrong | c42fffe | 2010-09-27 18:07:07 +0800 | [diff] [blame] | 4030 | void kvm_mmu_destroy(struct kvm_vcpu *vcpu) |
| 4031 | { |
| 4032 | ASSERT(vcpu); |
| 4033 | |
| 4034 | destroy_kvm_mmu(vcpu); |
| 4035 | free_mmu_pages(vcpu); |
| 4036 | mmu_free_memory_caches(vcpu); |
Xiao Guangrong | b034cf0 | 2010-12-23 16:08:35 +0800 | [diff] [blame] | 4037 | } |
| 4038 | |
| 4039 | #ifdef CONFIG_KVM_MMU_AUDIT |
| 4040 | #include "mmu_audit.c" |
| 4041 | #else |
| 4042 | static void mmu_audit_disable(void) { } |
| 4043 | #endif |
| 4044 | |
| 4045 | void kvm_mmu_module_exit(void) |
| 4046 | { |
| 4047 | mmu_destroy_caches(); |
| 4048 | percpu_counter_destroy(&kvm_total_used_mmu_pages); |
| 4049 | unregister_shrinker(&mmu_shrinker); |
Xiao Guangrong | c42fffe | 2010-09-27 18:07:07 +0800 | [diff] [blame] | 4050 | mmu_audit_disable(); |
| 4051 | } |