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