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 | 8672b72 | 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 | 8672b72 | 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 | a1f4d39 | 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 | a1f4d39 | 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 | a1f4d39 | 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 | 8672b72 | 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 | 8672b72 | 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 | 8672b72 | 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 | f6e2c02 | 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 | f6e2c02 | 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; |
Joerg Roedel | c5a78f2b | 2010-09-10 17:30:39 +0200 | [diff] [blame^] | 1451 | if (!vcpu->arch.mmu.direct_map |
| 1452 | && vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) { |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1453 | quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level)); |
| 1454 | quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1; |
| 1455 | role.quadrant = quadrant; |
| 1456 | } |
Xiao Guangrong | f41d335 | 2010-06-04 21:56:11 +0800 | [diff] [blame] | 1457 | for_each_gfn_sp(vcpu->kvm, sp, gfn, node) { |
Xiao Guangrong | 7ae680e | 2010-06-04 21:53:07 +0800 | [diff] [blame] | 1458 | if (!need_sync && sp->unsync) |
| 1459 | need_sync = true; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1460 | |
Xiao Guangrong | 7ae680e | 2010-06-04 21:53:07 +0800 | [diff] [blame] | 1461 | if (sp->role.word != role.word) |
| 1462 | continue; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1463 | |
Xiao Guangrong | 7ae680e | 2010-06-04 21:53:07 +0800 | [diff] [blame] | 1464 | if (sp->unsync && kvm_sync_page_transient(vcpu, sp)) |
| 1465 | break; |
Xiao Guangrong | e02aa90 | 2010-05-15 18:52:34 +0800 | [diff] [blame] | 1466 | |
Xiao Guangrong | 7ae680e | 2010-06-04 21:53:07 +0800 | [diff] [blame] | 1467 | mmu_page_add_parent_pte(vcpu, sp, parent_pte); |
| 1468 | if (sp->unsync_children) { |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 1469 | kvm_make_request(KVM_REQ_MMU_SYNC, vcpu); |
Xiao Guangrong | 7ae680e | 2010-06-04 21:53:07 +0800 | [diff] [blame] | 1470 | kvm_mmu_mark_parents_unsync(sp); |
| 1471 | } else if (sp->unsync) |
| 1472 | kvm_mmu_mark_parents_unsync(sp); |
Xiao Guangrong | e02aa90 | 2010-05-15 18:52:34 +0800 | [diff] [blame] | 1473 | |
Xiao Guangrong | 7ae680e | 2010-06-04 21:53:07 +0800 | [diff] [blame] | 1474 | trace_kvm_mmu_get_page(sp, false); |
| 1475 | return sp; |
| 1476 | } |
Avi Kivity | dfc5aa0 | 2007-12-18 19:47:18 +0200 | [diff] [blame] | 1477 | ++vcpu->kvm->stat.mmu_cache_miss; |
Lai Jiangshan | 2032a93 | 2010-05-26 16:49:59 +0800 | [diff] [blame] | 1478 | sp = kvm_mmu_alloc_page(vcpu, parent_pte, direct); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1479 | if (!sp) |
| 1480 | return sp; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1481 | sp->gfn = gfn; |
| 1482 | sp->role = role; |
Xiao Guangrong | 7ae680e | 2010-06-04 21:53:07 +0800 | [diff] [blame] | 1483 | hlist_add_head(&sp->hash_link, |
| 1484 | &vcpu->kvm->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)]); |
Avi Kivity | f6e2c02 | 2009-01-11 13:02:10 +0200 | [diff] [blame] | 1485 | if (!direct) { |
Marcelo Tosatti | b1a3682 | 2008-12-01 22:32:03 -0200 | [diff] [blame] | 1486 | if (rmap_write_protect(vcpu->kvm, gfn)) |
| 1487 | kvm_flush_remote_tlbs(vcpu->kvm); |
Xiao Guangrong | 9f1a122 | 2010-05-24 15:41:33 +0800 | [diff] [blame] | 1488 | if (level > PT_PAGE_TABLE_LEVEL && need_sync) |
| 1489 | kvm_sync_pages(vcpu, gfn); |
| 1490 | |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1491 | account_shadowed(vcpu->kvm, gfn); |
| 1492 | } |
Avi Kivity | 131d827 | 2008-05-29 14:56:28 +0300 | [diff] [blame] | 1493 | if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte) |
| 1494 | vcpu->arch.mmu.prefetch_page(vcpu, sp); |
| 1495 | else |
| 1496 | nonpaging_prefetch_page(vcpu, sp); |
Avi Kivity | f691fe1 | 2009-07-06 15:58:14 +0300 | [diff] [blame] | 1497 | trace_kvm_mmu_get_page(sp, true); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1498 | return sp; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1499 | } |
| 1500 | |
Avi Kivity | 2d11123 | 2008-12-25 14:39:47 +0200 | [diff] [blame] | 1501 | static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator, |
| 1502 | struct kvm_vcpu *vcpu, u64 addr) |
| 1503 | { |
| 1504 | iterator->addr = addr; |
| 1505 | iterator->shadow_addr = vcpu->arch.mmu.root_hpa; |
| 1506 | iterator->level = vcpu->arch.mmu.shadow_root_level; |
| 1507 | if (iterator->level == PT32E_ROOT_LEVEL) { |
| 1508 | iterator->shadow_addr |
| 1509 | = vcpu->arch.mmu.pae_root[(addr >> 30) & 3]; |
| 1510 | iterator->shadow_addr &= PT64_BASE_ADDR_MASK; |
| 1511 | --iterator->level; |
| 1512 | if (!iterator->shadow_addr) |
| 1513 | iterator->level = 0; |
| 1514 | } |
| 1515 | } |
| 1516 | |
| 1517 | static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator) |
| 1518 | { |
| 1519 | if (iterator->level < PT_PAGE_TABLE_LEVEL) |
| 1520 | return false; |
Marcelo Tosatti | 4d88954 | 2009-06-11 12:07:41 -0300 | [diff] [blame] | 1521 | |
| 1522 | if (iterator->level == PT_PAGE_TABLE_LEVEL) |
| 1523 | if (is_large_pte(*iterator->sptep)) |
| 1524 | return false; |
| 1525 | |
Avi Kivity | 2d11123 | 2008-12-25 14:39:47 +0200 | [diff] [blame] | 1526 | iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level); |
| 1527 | iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index; |
| 1528 | return true; |
| 1529 | } |
| 1530 | |
| 1531 | static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator) |
| 1532 | { |
| 1533 | iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK; |
| 1534 | --iterator->level; |
| 1535 | } |
| 1536 | |
Avi Kivity | 32ef26a | 2010-07-13 14:27:04 +0300 | [diff] [blame] | 1537 | static void link_shadow_page(u64 *sptep, struct kvm_mmu_page *sp) |
| 1538 | { |
| 1539 | u64 spte; |
| 1540 | |
| 1541 | spte = __pa(sp->spt) |
| 1542 | | PT_PRESENT_MASK | PT_ACCESSED_MASK |
| 1543 | | PT_WRITABLE_MASK | PT_USER_MASK; |
Avi Kivity | 121eee9 | 2010-07-13 14:27:05 +0300 | [diff] [blame] | 1544 | __set_spte(sptep, spte); |
Avi Kivity | 32ef26a | 2010-07-13 14:27:04 +0300 | [diff] [blame] | 1545 | } |
| 1546 | |
Avi Kivity | a3aa51c | 2010-07-13 14:27:06 +0300 | [diff] [blame] | 1547 | static void drop_large_spte(struct kvm_vcpu *vcpu, u64 *sptep) |
| 1548 | { |
| 1549 | if (is_large_pte(*sptep)) { |
| 1550 | drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte); |
| 1551 | kvm_flush_remote_tlbs(vcpu->kvm); |
| 1552 | } |
| 1553 | } |
| 1554 | |
Avi Kivity | a357bd2 | 2010-07-13 14:27:07 +0300 | [diff] [blame] | 1555 | static void validate_direct_spte(struct kvm_vcpu *vcpu, u64 *sptep, |
| 1556 | unsigned direct_access) |
| 1557 | { |
| 1558 | if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep)) { |
| 1559 | struct kvm_mmu_page *child; |
| 1560 | |
| 1561 | /* |
| 1562 | * For the direct sp, if the guest pte's dirty bit |
| 1563 | * changed form clean to dirty, it will corrupt the |
| 1564 | * sp's access: allow writable in the read-only sp, |
| 1565 | * so we should update the spte at this point to get |
| 1566 | * a new sp with the correct access. |
| 1567 | */ |
| 1568 | child = page_header(*sptep & PT64_BASE_ADDR_MASK); |
| 1569 | if (child->role.access == direct_access) |
| 1570 | return; |
| 1571 | |
| 1572 | mmu_page_remove_parent_pte(child, sptep); |
| 1573 | __set_spte(sptep, shadow_trap_nonpresent_pte); |
| 1574 | kvm_flush_remote_tlbs(vcpu->kvm); |
| 1575 | } |
| 1576 | } |
| 1577 | |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 1578 | static void kvm_mmu_page_unlink_children(struct kvm *kvm, |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1579 | struct kvm_mmu_page *sp) |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1580 | { |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 1581 | unsigned i; |
| 1582 | u64 *pt; |
| 1583 | u64 ent; |
| 1584 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1585 | pt = sp->spt; |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 1586 | |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 1587 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) { |
| 1588 | ent = pt[i]; |
| 1589 | |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 1590 | if (is_shadow_present_pte(ent)) { |
Marcelo Tosatti | 776e663 | 2009-06-10 12:27:03 -0300 | [diff] [blame] | 1591 | if (!is_last_spte(ent, sp->role.level)) { |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 1592 | ent &= PT64_BASE_ADDR_MASK; |
| 1593 | mmu_page_remove_parent_pte(page_header(ent), |
| 1594 | &pt[i]); |
| 1595 | } else { |
Marcelo Tosatti | 776e663 | 2009-06-10 12:27:03 -0300 | [diff] [blame] | 1596 | if (is_large_pte(ent)) |
| 1597 | --kvm->stat.lpages; |
Avi Kivity | be38d27 | 2010-06-06 14:31:27 +0300 | [diff] [blame] | 1598 | drop_spte(kvm, &pt[i], |
| 1599 | shadow_trap_nonpresent_pte); |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 1600 | } |
| 1601 | } |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 1602 | pt[i] = shadow_trap_nonpresent_pte; |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 1603 | } |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1604 | } |
| 1605 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1606 | 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] | 1607 | { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1608 | mmu_page_remove_parent_pte(sp, parent_pte); |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1609 | } |
| 1610 | |
Avi Kivity | 12b7d28 | 2007-09-23 14:10:49 +0200 | [diff] [blame] | 1611 | static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm) |
| 1612 | { |
| 1613 | int i; |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 1614 | struct kvm_vcpu *vcpu; |
Avi Kivity | 12b7d28 | 2007-09-23 14:10:49 +0200 | [diff] [blame] | 1615 | |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 1616 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 1617 | vcpu->arch.last_pte_updated = NULL; |
Avi Kivity | 12b7d28 | 2007-09-23 14:10:49 +0200 | [diff] [blame] | 1618 | } |
| 1619 | |
Avi Kivity | 31aa2b4 | 2008-07-11 17:59:46 +0300 | [diff] [blame] | 1620 | 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] | 1621 | { |
| 1622 | u64 *parent_pte; |
| 1623 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1624 | while (sp->multimapped || sp->parent_pte) { |
| 1625 | if (!sp->multimapped) |
| 1626 | parent_pte = sp->parent_pte; |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1627 | else { |
| 1628 | struct kvm_pte_chain *chain; |
| 1629 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1630 | chain = container_of(sp->parent_ptes.first, |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1631 | struct kvm_pte_chain, link); |
| 1632 | parent_pte = chain->parent_ptes[0]; |
| 1633 | } |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 1634 | BUG_ON(!parent_pte); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1635 | kvm_mmu_put_page(sp, parent_pte); |
Avi Kivity | d555c33 | 2009-06-10 14:24:23 +0300 | [diff] [blame] | 1636 | __set_spte(parent_pte, shadow_trap_nonpresent_pte); |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1637 | } |
Avi Kivity | 31aa2b4 | 2008-07-11 17:59:46 +0300 | [diff] [blame] | 1638 | } |
| 1639 | |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1640 | static int mmu_zap_unsync_children(struct kvm *kvm, |
Xiao Guangrong | 7775834 | 2010-06-04 21:53:54 +0800 | [diff] [blame] | 1641 | struct kvm_mmu_page *parent, |
| 1642 | struct list_head *invalid_list) |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1643 | { |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1644 | int i, zapped = 0; |
| 1645 | struct mmu_page_path parents; |
| 1646 | struct kvm_mmu_pages pages; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1647 | |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1648 | if (parent->role.level == PT_PAGE_TABLE_LEVEL) |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1649 | return 0; |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1650 | |
| 1651 | kvm_mmu_pages_init(parent, &parents, &pages); |
| 1652 | while (mmu_unsync_walk(parent, &pages)) { |
| 1653 | struct kvm_mmu_page *sp; |
| 1654 | |
| 1655 | for_each_sp(pages, sp, parents, i) { |
Xiao Guangrong | 7775834 | 2010-06-04 21:53:54 +0800 | [diff] [blame] | 1656 | kvm_mmu_prepare_zap_page(kvm, sp, invalid_list); |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1657 | mmu_pages_clear_parents(&parents); |
Xiao Guangrong | 77662e0 | 2010-04-16 16:34:42 +0800 | [diff] [blame] | 1658 | zapped++; |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1659 | } |
Marcelo Tosatti | 60c8aec | 2008-12-01 22:32:02 -0200 | [diff] [blame] | 1660 | kvm_mmu_pages_init(parent, &parents, &pages); |
| 1661 | } |
| 1662 | |
| 1663 | return zapped; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1664 | } |
| 1665 | |
Xiao Guangrong | 7775834 | 2010-06-04 21:53:54 +0800 | [diff] [blame] | 1666 | static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp, |
| 1667 | struct list_head *invalid_list) |
Avi Kivity | 31aa2b4 | 2008-07-11 17:59:46 +0300 | [diff] [blame] | 1668 | { |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1669 | int ret; |
Avi Kivity | f691fe1 | 2009-07-06 15:58:14 +0300 | [diff] [blame] | 1670 | |
Xiao Guangrong | 7775834 | 2010-06-04 21:53:54 +0800 | [diff] [blame] | 1671 | trace_kvm_mmu_prepare_zap_page(sp); |
Avi Kivity | 31aa2b4 | 2008-07-11 17:59:46 +0300 | [diff] [blame] | 1672 | ++kvm->stat.mmu_shadow_zapped; |
Xiao Guangrong | 7775834 | 2010-06-04 21:53:54 +0800 | [diff] [blame] | 1673 | ret = mmu_zap_unsync_children(kvm, sp, invalid_list); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1674 | kvm_mmu_page_unlink_children(kvm, sp); |
Avi Kivity | 31aa2b4 | 2008-07-11 17:59:46 +0300 | [diff] [blame] | 1675 | kvm_mmu_unlink_parents(kvm, sp); |
Avi Kivity | f6e2c02 | 2009-01-11 13:02:10 +0200 | [diff] [blame] | 1676 | if (!sp->role.invalid && !sp->role.direct) |
Avi Kivity | 5b5c6a5 | 2008-07-11 18:07:26 +0300 | [diff] [blame] | 1677 | unaccount_shadowed(kvm, sp->gfn); |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1678 | if (sp->unsync) |
| 1679 | kvm_unlink_unsync_page(kvm, sp); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1680 | if (!sp->root_count) { |
Gui Jianfeng | 54a4f02 | 2010-05-05 09:03:49 +0800 | [diff] [blame] | 1681 | /* Count self */ |
| 1682 | ret++; |
Xiao Guangrong | 7775834 | 2010-06-04 21:53:54 +0800 | [diff] [blame] | 1683 | list_move(&sp->link, invalid_list); |
Marcelo Tosatti | 2e53d63 | 2008-02-20 14:47:24 -0500 | [diff] [blame] | 1684 | } else { |
Avi Kivity | 5b5c6a5 | 2008-07-11 18:07:26 +0300 | [diff] [blame] | 1685 | list_move(&sp->link, &kvm->arch.active_mmu_pages); |
Marcelo Tosatti | 2e53d63 | 2008-02-20 14:47:24 -0500 | [diff] [blame] | 1686 | kvm_reload_remote_mmus(kvm); |
| 1687 | } |
Xiao Guangrong | 7775834 | 2010-06-04 21:53:54 +0800 | [diff] [blame] | 1688 | |
| 1689 | sp->role.invalid = 1; |
Avi Kivity | 12b7d28 | 2007-09-23 14:10:49 +0200 | [diff] [blame] | 1690 | kvm_mmu_reset_last_pte_updated(kvm); |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1691 | return ret; |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1692 | } |
| 1693 | |
Xiao Guangrong | 7775834 | 2010-06-04 21:53:54 +0800 | [diff] [blame] | 1694 | static void kvm_mmu_commit_zap_page(struct kvm *kvm, |
| 1695 | struct list_head *invalid_list) |
| 1696 | { |
| 1697 | struct kvm_mmu_page *sp; |
| 1698 | |
| 1699 | if (list_empty(invalid_list)) |
| 1700 | return; |
| 1701 | |
| 1702 | kvm_flush_remote_tlbs(kvm); |
| 1703 | |
| 1704 | do { |
| 1705 | sp = list_first_entry(invalid_list, struct kvm_mmu_page, link); |
| 1706 | WARN_ON(!sp->role.invalid || sp->root_count); |
| 1707 | kvm_mmu_free_page(kvm, sp); |
| 1708 | } while (!list_empty(invalid_list)); |
| 1709 | |
| 1710 | } |
| 1711 | |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 1712 | /* |
| 1713 | * Changing the number of mmu pages allocated to the vm |
Dave Hansen | 49d5ca2 | 2010-08-19 18:11:28 -0700 | [diff] [blame] | 1714 | * 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] | 1715 | */ |
Dave Hansen | 49d5ca2 | 2010-08-19 18:11:28 -0700 | [diff] [blame] | 1716 | 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] | 1717 | { |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 1718 | LIST_HEAD(invalid_list); |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 1719 | /* |
| 1720 | * If we set the number of mmu pages to be smaller be than the |
| 1721 | * number of actived pages , we must to free some mmu pages before we |
| 1722 | * change the value |
| 1723 | */ |
| 1724 | |
Dave Hansen | 49d5ca2 | 2010-08-19 18:11:28 -0700 | [diff] [blame] | 1725 | if (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages) { |
| 1726 | while (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages && |
Xiao Guangrong | 77662e0 | 2010-04-16 16:34:42 +0800 | [diff] [blame] | 1727 | !list_empty(&kvm->arch.active_mmu_pages)) { |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 1728 | struct kvm_mmu_page *page; |
| 1729 | |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 1730 | page = container_of(kvm->arch.active_mmu_pages.prev, |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 1731 | struct kvm_mmu_page, link); |
Xiaotian Feng | 80b63fa | 2010-08-24 10:31:07 +0800 | [diff] [blame] | 1732 | kvm_mmu_prepare_zap_page(kvm, page, &invalid_list); |
| 1733 | kvm_mmu_commit_zap_page(kvm, &invalid_list); |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 1734 | } |
Dave Hansen | 49d5ca2 | 2010-08-19 18:11:28 -0700 | [diff] [blame] | 1735 | goal_nr_mmu_pages = kvm->arch.n_used_mmu_pages; |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 1736 | } |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 1737 | |
Dave Hansen | 49d5ca2 | 2010-08-19 18:11:28 -0700 | [diff] [blame] | 1738 | kvm->arch.n_max_mmu_pages = goal_nr_mmu_pages; |
Izik Eidus | 82ce2c9 | 2007-10-02 18:52:55 +0200 | [diff] [blame] | 1739 | } |
| 1740 | |
Anthony Liguori | f67a46f | 2007-10-10 19:25:50 -0500 | [diff] [blame] | 1741 | static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn) |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1742 | { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1743 | struct kvm_mmu_page *sp; |
Xiao Guangrong | f41d335 | 2010-06-04 21:56:11 +0800 | [diff] [blame] | 1744 | struct hlist_node *node; |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 1745 | LIST_HEAD(invalid_list); |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1746 | int r; |
| 1747 | |
Xiao Guangrong | 9ad17b1 | 2010-08-28 19:19:42 +0800 | [diff] [blame] | 1748 | pgprintk("%s: looking for gfn %llx\n", __func__, gfn); |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1749 | r = 0; |
Xiao Guangrong | f41d335 | 2010-06-04 21:56:11 +0800 | [diff] [blame] | 1750 | |
| 1751 | for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) { |
Xiao Guangrong | 9ad17b1 | 2010-08-28 19:19:42 +0800 | [diff] [blame] | 1752 | pgprintk("%s: gfn %llx role %x\n", __func__, gfn, |
Xiao Guangrong | 7ae680e | 2010-06-04 21:53:07 +0800 | [diff] [blame] | 1753 | sp->role.word); |
| 1754 | r = 1; |
Xiao Guangrong | f41d335 | 2010-06-04 21:56:11 +0800 | [diff] [blame] | 1755 | kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list); |
Xiao Guangrong | 7ae680e | 2010-06-04 21:53:07 +0800 | [diff] [blame] | 1756 | } |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 1757 | kvm_mmu_commit_zap_page(kvm, &invalid_list); |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1758 | return r; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 1759 | } |
| 1760 | |
Anthony Liguori | f67a46f | 2007-10-10 19:25:50 -0500 | [diff] [blame] | 1761 | static void mmu_unshadow(struct kvm *kvm, gfn_t gfn) |
Avi Kivity | 97a0a01 | 2007-05-31 15:08:29 +0300 | [diff] [blame] | 1762 | { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1763 | struct kvm_mmu_page *sp; |
Xiao Guangrong | f41d335 | 2010-06-04 21:56:11 +0800 | [diff] [blame] | 1764 | struct hlist_node *node; |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 1765 | LIST_HEAD(invalid_list); |
Avi Kivity | 97a0a01 | 2007-05-31 15:08:29 +0300 | [diff] [blame] | 1766 | |
Xiao Guangrong | f41d335 | 2010-06-04 21:56:11 +0800 | [diff] [blame] | 1767 | for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) { |
Xiao Guangrong | 9ad17b1 | 2010-08-28 19:19:42 +0800 | [diff] [blame] | 1768 | pgprintk("%s: zap %llx %x\n", |
Xiao Guangrong | 7ae680e | 2010-06-04 21:53:07 +0800 | [diff] [blame] | 1769 | __func__, gfn, sp->role.word); |
Xiao Guangrong | f41d335 | 2010-06-04 21:56:11 +0800 | [diff] [blame] | 1770 | kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list); |
Avi Kivity | 97a0a01 | 2007-05-31 15:08:29 +0300 | [diff] [blame] | 1771 | } |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 1772 | kvm_mmu_commit_zap_page(kvm, &invalid_list); |
Avi Kivity | 97a0a01 | 2007-05-31 15:08:29 +0300 | [diff] [blame] | 1773 | } |
| 1774 | |
Avi Kivity | 38c335f | 2007-11-21 14:20:22 +0200 | [diff] [blame] | 1775 | 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] | 1776 | { |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 1777 | int slot = memslot_id(kvm, gfn); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 1778 | struct kvm_mmu_page *sp = page_header(__pa(pte)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1779 | |
Sheng Yang | 291f26b | 2008-10-16 17:30:57 +0800 | [diff] [blame] | 1780 | __set_bit(slot, sp->slot_bitmap); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1781 | } |
| 1782 | |
Marcelo Tosatti | 6844dec | 2008-09-23 13:18:38 -0300 | [diff] [blame] | 1783 | static void mmu_convert_notrap(struct kvm_mmu_page *sp) |
| 1784 | { |
| 1785 | int i; |
| 1786 | u64 *pt = sp->spt; |
| 1787 | |
| 1788 | if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte) |
| 1789 | return; |
| 1790 | |
| 1791 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) { |
| 1792 | if (pt[i] == shadow_notrap_nonpresent_pte) |
Avi Kivity | d555c33 | 2009-06-10 14:24:23 +0300 | [diff] [blame] | 1793 | __set_spte(&pt[i], shadow_trap_nonpresent_pte); |
Marcelo Tosatti | 6844dec | 2008-09-23 13:18:38 -0300 | [diff] [blame] | 1794 | } |
| 1795 | } |
| 1796 | |
Sheng Yang | 74be52e | 2008-10-09 16:01:56 +0800 | [diff] [blame] | 1797 | /* |
| 1798 | * The function is based on mtrr_type_lookup() in |
| 1799 | * arch/x86/kernel/cpu/mtrr/generic.c |
| 1800 | */ |
| 1801 | static int get_mtrr_type(struct mtrr_state_type *mtrr_state, |
| 1802 | u64 start, u64 end) |
| 1803 | { |
| 1804 | int i; |
| 1805 | u64 base, mask; |
| 1806 | u8 prev_match, curr_match; |
| 1807 | int num_var_ranges = KVM_NR_VAR_MTRR; |
| 1808 | |
| 1809 | if (!mtrr_state->enabled) |
| 1810 | return 0xFF; |
| 1811 | |
| 1812 | /* Make end inclusive end, instead of exclusive */ |
| 1813 | end--; |
| 1814 | |
| 1815 | /* Look in fixed ranges. Just return the type as per start */ |
| 1816 | if (mtrr_state->have_fixed && (start < 0x100000)) { |
| 1817 | int idx; |
| 1818 | |
| 1819 | if (start < 0x80000) { |
| 1820 | idx = 0; |
| 1821 | idx += (start >> 16); |
| 1822 | return mtrr_state->fixed_ranges[idx]; |
| 1823 | } else if (start < 0xC0000) { |
| 1824 | idx = 1 * 8; |
| 1825 | idx += ((start - 0x80000) >> 14); |
| 1826 | return mtrr_state->fixed_ranges[idx]; |
| 1827 | } else if (start < 0x1000000) { |
| 1828 | idx = 3 * 8; |
| 1829 | idx += ((start - 0xC0000) >> 12); |
| 1830 | return mtrr_state->fixed_ranges[idx]; |
| 1831 | } |
| 1832 | } |
| 1833 | |
| 1834 | /* |
| 1835 | * Look in variable ranges |
| 1836 | * Look of multiple ranges matching this address and pick type |
| 1837 | * as per MTRR precedence |
| 1838 | */ |
| 1839 | if (!(mtrr_state->enabled & 2)) |
| 1840 | return mtrr_state->def_type; |
| 1841 | |
| 1842 | prev_match = 0xFF; |
| 1843 | for (i = 0; i < num_var_ranges; ++i) { |
| 1844 | unsigned short start_state, end_state; |
| 1845 | |
| 1846 | if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11))) |
| 1847 | continue; |
| 1848 | |
| 1849 | base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) + |
| 1850 | (mtrr_state->var_ranges[i].base_lo & PAGE_MASK); |
| 1851 | mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) + |
| 1852 | (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK); |
| 1853 | |
| 1854 | start_state = ((start & mask) == (base & mask)); |
| 1855 | end_state = ((end & mask) == (base & mask)); |
| 1856 | if (start_state != end_state) |
| 1857 | return 0xFE; |
| 1858 | |
| 1859 | if ((start & mask) != (base & mask)) |
| 1860 | continue; |
| 1861 | |
| 1862 | curr_match = mtrr_state->var_ranges[i].base_lo & 0xff; |
| 1863 | if (prev_match == 0xFF) { |
| 1864 | prev_match = curr_match; |
| 1865 | continue; |
| 1866 | } |
| 1867 | |
| 1868 | if (prev_match == MTRR_TYPE_UNCACHABLE || |
| 1869 | curr_match == MTRR_TYPE_UNCACHABLE) |
| 1870 | return MTRR_TYPE_UNCACHABLE; |
| 1871 | |
| 1872 | if ((prev_match == MTRR_TYPE_WRBACK && |
| 1873 | curr_match == MTRR_TYPE_WRTHROUGH) || |
| 1874 | (prev_match == MTRR_TYPE_WRTHROUGH && |
| 1875 | curr_match == MTRR_TYPE_WRBACK)) { |
| 1876 | prev_match = MTRR_TYPE_WRTHROUGH; |
| 1877 | curr_match = MTRR_TYPE_WRTHROUGH; |
| 1878 | } |
| 1879 | |
| 1880 | if (prev_match != curr_match) |
| 1881 | return MTRR_TYPE_UNCACHABLE; |
| 1882 | } |
| 1883 | |
| 1884 | if (prev_match != 0xFF) |
| 1885 | return prev_match; |
| 1886 | |
| 1887 | return mtrr_state->def_type; |
| 1888 | } |
| 1889 | |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 1890 | 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] | 1891 | { |
| 1892 | u8 mtrr; |
| 1893 | |
| 1894 | mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT, |
| 1895 | (gfn << PAGE_SHIFT) + PAGE_SIZE); |
| 1896 | if (mtrr == 0xfe || mtrr == 0xff) |
| 1897 | mtrr = MTRR_TYPE_WRBACK; |
| 1898 | return mtrr; |
| 1899 | } |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 1900 | EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type); |
Sheng Yang | 74be52e | 2008-10-09 16:01:56 +0800 | [diff] [blame] | 1901 | |
Xiao Guangrong | 9cf5cf5 | 2010-05-24 15:40:07 +0800 | [diff] [blame] | 1902 | 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] | 1903 | { |
Xiao Guangrong | 5e1b3dd | 2010-04-28 11:55:06 +0800 | [diff] [blame] | 1904 | trace_kvm_mmu_unsync_page(sp); |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1905 | ++vcpu->kvm->stat.mmu_unsync; |
| 1906 | sp->unsync = 1; |
Marcelo Tosatti | 6cffe8c | 2008-12-01 22:32:04 -0200 | [diff] [blame] | 1907 | |
Xiao Guangrong | 6b18493 | 2010-04-16 21:29:17 +0800 | [diff] [blame] | 1908 | kvm_mmu_mark_parents_unsync(sp); |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1909 | mmu_convert_notrap(sp); |
Xiao Guangrong | 9cf5cf5 | 2010-05-24 15:40:07 +0800 | [diff] [blame] | 1910 | } |
| 1911 | |
| 1912 | static void kvm_unsync_pages(struct kvm_vcpu *vcpu, gfn_t gfn) |
| 1913 | { |
Xiao Guangrong | 9cf5cf5 | 2010-05-24 15:40:07 +0800 | [diff] [blame] | 1914 | struct kvm_mmu_page *s; |
Xiao Guangrong | f41d335 | 2010-06-04 21:56:11 +0800 | [diff] [blame] | 1915 | struct hlist_node *node; |
Xiao Guangrong | 9cf5cf5 | 2010-05-24 15:40:07 +0800 | [diff] [blame] | 1916 | |
Xiao Guangrong | f41d335 | 2010-06-04 21:56:11 +0800 | [diff] [blame] | 1917 | for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) { |
Xiao Guangrong | 7ae680e | 2010-06-04 21:53:07 +0800 | [diff] [blame] | 1918 | if (s->unsync) |
Xiao Guangrong | 9cf5cf5 | 2010-05-24 15:40:07 +0800 | [diff] [blame] | 1919 | continue; |
| 1920 | WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL); |
| 1921 | __kvm_unsync_page(vcpu, s); |
| 1922 | } |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1923 | } |
| 1924 | |
| 1925 | static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn, |
| 1926 | bool can_unsync) |
| 1927 | { |
Xiao Guangrong | 9cf5cf5 | 2010-05-24 15:40:07 +0800 | [diff] [blame] | 1928 | struct kvm_mmu_page *s; |
Xiao Guangrong | f41d335 | 2010-06-04 21:56:11 +0800 | [diff] [blame] | 1929 | struct hlist_node *node; |
Xiao Guangrong | 9cf5cf5 | 2010-05-24 15:40:07 +0800 | [diff] [blame] | 1930 | bool need_unsync = false; |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1931 | |
Xiao Guangrong | f41d335 | 2010-06-04 21:56:11 +0800 | [diff] [blame] | 1932 | for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) { |
Xiao Guangrong | 36a2e67 | 2010-06-30 16:02:02 +0800 | [diff] [blame] | 1933 | if (!can_unsync) |
| 1934 | return 1; |
| 1935 | |
Xiao Guangrong | 9cf5cf5 | 2010-05-24 15:40:07 +0800 | [diff] [blame] | 1936 | if (s->role.level != PT_PAGE_TABLE_LEVEL) |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1937 | return 1; |
Xiao Guangrong | 9cf5cf5 | 2010-05-24 15:40:07 +0800 | [diff] [blame] | 1938 | |
| 1939 | if (!need_unsync && !s->unsync) { |
Xiao Guangrong | 36a2e67 | 2010-06-30 16:02:02 +0800 | [diff] [blame] | 1940 | if (!oos_shadow) |
Xiao Guangrong | 9cf5cf5 | 2010-05-24 15:40:07 +0800 | [diff] [blame] | 1941 | return 1; |
| 1942 | need_unsync = true; |
| 1943 | } |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1944 | } |
Xiao Guangrong | 9cf5cf5 | 2010-05-24 15:40:07 +0800 | [diff] [blame] | 1945 | if (need_unsync) |
| 1946 | kvm_unsync_pages(vcpu, gfn); |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 1947 | return 0; |
| 1948 | } |
| 1949 | |
Avi Kivity | d555c33 | 2009-06-10 14:24:23 +0300 | [diff] [blame] | 1950 | static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep, |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 1951 | unsigned pte_access, int user_fault, |
Joerg Roedel | 852e3c1 | 2009-07-27 16:30:44 +0200 | [diff] [blame] | 1952 | int write_fault, int dirty, int level, |
Marcelo Tosatti | c2d0ee4 | 2009-04-05 14:54:47 -0300 | [diff] [blame] | 1953 | gfn_t gfn, pfn_t pfn, bool speculative, |
Izik Eidus | 1403283 | 2009-09-23 21:47:17 +0300 | [diff] [blame] | 1954 | bool can_unsync, bool reset_host_protection) |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1955 | { |
| 1956 | u64 spte; |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 1957 | int ret = 0; |
Sheng Yang | 64d4d52 | 2008-10-09 16:01:57 +0800 | [diff] [blame] | 1958 | |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1959 | /* |
| 1960 | * We don't set the accessed bit, since we sometimes want to see |
| 1961 | * whether the guest actually used the pte (in order to detect |
| 1962 | * demand paging). |
| 1963 | */ |
Xiao Guangrong | 4132779 | 2010-08-02 16:15:08 +0800 | [diff] [blame] | 1964 | spte = shadow_base_present_pte; |
Avi Kivity | 947da53 | 2008-03-18 11:05:52 +0200 | [diff] [blame] | 1965 | if (!speculative) |
Avi Kivity | 3201b5d | 2008-08-27 20:01:04 +0300 | [diff] [blame] | 1966 | spte |= shadow_accessed_mask; |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1967 | if (!dirty) |
| 1968 | pte_access &= ~ACC_WRITE_MASK; |
Sheng Yang | 7b52345 | 2008-04-25 21:13:50 +0800 | [diff] [blame] | 1969 | if (pte_access & ACC_EXEC_MASK) |
| 1970 | spte |= shadow_x_mask; |
| 1971 | else |
| 1972 | spte |= shadow_nx_mask; |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1973 | if (pte_access & ACC_USER_MASK) |
Sheng Yang | 7b52345 | 2008-04-25 21:13:50 +0800 | [diff] [blame] | 1974 | spte |= shadow_user_mask; |
Joerg Roedel | 852e3c1 | 2009-07-27 16:30:44 +0200 | [diff] [blame] | 1975 | if (level > PT_PAGE_TABLE_LEVEL) |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 1976 | spte |= PT_PAGE_SIZE_MASK; |
Joerg Roedel | c5a78f2b | 2010-09-10 17:30:39 +0200 | [diff] [blame^] | 1977 | if (vcpu->arch.mmu.direct_map) |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 1978 | spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn, |
| 1979 | kvm_is_mmio_pfn(pfn)); |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1980 | |
Izik Eidus | 1403283 | 2009-09-23 21:47:17 +0300 | [diff] [blame] | 1981 | if (reset_host_protection) |
| 1982 | spte |= SPTE_HOST_WRITEABLE; |
| 1983 | |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 1984 | spte |= (u64)pfn << PAGE_SHIFT; |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1985 | |
| 1986 | if ((pte_access & ACC_WRITE_MASK) |
Joerg Roedel | c5a78f2b | 2010-09-10 17:30:39 +0200 | [diff] [blame^] | 1987 | || (!vcpu->arch.mmu.direct_map && write_fault |
| 1988 | && !is_write_protection(vcpu) && !user_fault)) { |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1989 | |
Joerg Roedel | 852e3c1 | 2009-07-27 16:30:44 +0200 | [diff] [blame] | 1990 | if (level > PT_PAGE_TABLE_LEVEL && |
| 1991 | has_wrprotected_page(vcpu->kvm, gfn, level)) { |
Marcelo Tosatti | 38187c8 | 2008-09-23 13:18:32 -0300 | [diff] [blame] | 1992 | ret = 1; |
Avi Kivity | be38d27 | 2010-06-06 14:31:27 +0300 | [diff] [blame] | 1993 | drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte); |
| 1994 | goto done; |
Marcelo Tosatti | 38187c8 | 2008-09-23 13:18:32 -0300 | [diff] [blame] | 1995 | } |
| 1996 | |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1997 | spte |= PT_WRITABLE_MASK; |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 1998 | |
Joerg Roedel | c5a78f2b | 2010-09-10 17:30:39 +0200 | [diff] [blame^] | 1999 | if (!vcpu->arch.mmu.direct_map |
| 2000 | && !(pte_access & ACC_WRITE_MASK)) |
Avi Kivity | 69325a1 | 2010-05-27 14:35:58 +0300 | [diff] [blame] | 2001 | spte &= ~PT_USER_MASK; |
| 2002 | |
Marcelo Tosatti | ecc5589 | 2008-11-25 15:58:07 +0100 | [diff] [blame] | 2003 | /* |
| 2004 | * Optimization: for pte sync, if spte was writable the hash |
| 2005 | * lookup is unnecessary (and expensive). Write protection |
| 2006 | * is responsibility of mmu_get_page / kvm_sync_page. |
| 2007 | * Same reasoning can be applied to dirty page accounting. |
| 2008 | */ |
Takuya Yoshikawa | 8dae444 | 2010-01-18 18:45:10 +0900 | [diff] [blame] | 2009 | if (!can_unsync && is_writable_pte(*sptep)) |
Marcelo Tosatti | ecc5589 | 2008-11-25 15:58:07 +0100 | [diff] [blame] | 2010 | goto set_pte; |
| 2011 | |
Marcelo Tosatti | 4731d4c | 2008-09-23 13:18:39 -0300 | [diff] [blame] | 2012 | if (mmu_need_write_protect(vcpu, gfn, can_unsync)) { |
Xiao Guangrong | 9ad17b1 | 2010-08-28 19:19:42 +0800 | [diff] [blame] | 2013 | pgprintk("%s: found shadow page for %llx, marking ro\n", |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 2014 | __func__, gfn); |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 2015 | ret = 1; |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 2016 | pte_access &= ~ACC_WRITE_MASK; |
Takuya Yoshikawa | 8dae444 | 2010-01-18 18:45:10 +0900 | [diff] [blame] | 2017 | if (is_writable_pte(spte)) |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 2018 | spte &= ~PT_WRITABLE_MASK; |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 2019 | } |
| 2020 | } |
| 2021 | |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 2022 | if (pte_access & ACC_WRITE_MASK) |
| 2023 | mark_page_dirty(vcpu->kvm, gfn); |
| 2024 | |
Marcelo Tosatti | 38187c8 | 2008-09-23 13:18:32 -0300 | [diff] [blame] | 2025 | set_pte: |
Avi Kivity | b79b93f | 2010-06-06 15:46:44 +0300 | [diff] [blame] | 2026 | update_spte(sptep, spte); |
Avi Kivity | be38d27 | 2010-06-06 14:31:27 +0300 | [diff] [blame] | 2027 | done: |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 2028 | return ret; |
| 2029 | } |
| 2030 | |
Avi Kivity | d555c33 | 2009-06-10 14:24:23 +0300 | [diff] [blame] | 2031 | static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep, |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 2032 | unsigned pt_access, unsigned pte_access, |
| 2033 | int user_fault, int write_fault, int dirty, |
Joerg Roedel | 852e3c1 | 2009-07-27 16:30:44 +0200 | [diff] [blame] | 2034 | int *ptwrite, int level, gfn_t gfn, |
Izik Eidus | 1403283 | 2009-09-23 21:47:17 +0300 | [diff] [blame] | 2035 | pfn_t pfn, bool speculative, |
| 2036 | bool reset_host_protection) |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 2037 | { |
| 2038 | int was_rmapped = 0; |
Marcelo Tosatti | 53a27b3 | 2009-08-05 15:43:58 -0300 | [diff] [blame] | 2039 | int rmap_count; |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 2040 | |
| 2041 | pgprintk("%s: spte %llx access %x write_fault %d" |
Xiao Guangrong | 9ad17b1 | 2010-08-28 19:19:42 +0800 | [diff] [blame] | 2042 | " user_fault %d gfn %llx\n", |
Avi Kivity | d555c33 | 2009-06-10 14:24:23 +0300 | [diff] [blame] | 2043 | __func__, *sptep, pt_access, |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 2044 | write_fault, user_fault, gfn); |
| 2045 | |
Avi Kivity | d555c33 | 2009-06-10 14:24:23 +0300 | [diff] [blame] | 2046 | if (is_rmap_spte(*sptep)) { |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 2047 | /* |
| 2048 | * If we overwrite a PTE page pointer with a 2MB PMD, unlink |
| 2049 | * the parent of the now unreachable PTE. |
| 2050 | */ |
Joerg Roedel | 852e3c1 | 2009-07-27 16:30:44 +0200 | [diff] [blame] | 2051 | if (level > PT_PAGE_TABLE_LEVEL && |
| 2052 | !is_large_pte(*sptep)) { |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 2053 | struct kvm_mmu_page *child; |
Avi Kivity | d555c33 | 2009-06-10 14:24:23 +0300 | [diff] [blame] | 2054 | u64 pte = *sptep; |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 2055 | |
| 2056 | child = page_header(pte & PT64_BASE_ADDR_MASK); |
Avi Kivity | d555c33 | 2009-06-10 14:24:23 +0300 | [diff] [blame] | 2057 | mmu_page_remove_parent_pte(child, sptep); |
Marcelo Tosatti | 3be2264 | 2010-05-28 09:44:59 -0300 | [diff] [blame] | 2058 | __set_spte(sptep, shadow_trap_nonpresent_pte); |
| 2059 | kvm_flush_remote_tlbs(vcpu->kvm); |
Avi Kivity | d555c33 | 2009-06-10 14:24:23 +0300 | [diff] [blame] | 2060 | } else if (pfn != spte_to_pfn(*sptep)) { |
Xiao Guangrong | 9ad17b1 | 2010-08-28 19:19:42 +0800 | [diff] [blame] | 2061 | pgprintk("hfn old %llx new %llx\n", |
Avi Kivity | d555c33 | 2009-06-10 14:24:23 +0300 | [diff] [blame] | 2062 | spte_to_pfn(*sptep), pfn); |
Avi Kivity | be38d27 | 2010-06-06 14:31:27 +0300 | [diff] [blame] | 2063 | drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte); |
Xiao Guangrong | 9154635 | 2010-06-30 16:04:06 +0800 | [diff] [blame] | 2064 | kvm_flush_remote_tlbs(vcpu->kvm); |
Joerg Roedel | 6bed6b9 | 2009-02-18 14:08:59 +0100 | [diff] [blame] | 2065 | } else |
| 2066 | was_rmapped = 1; |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 2067 | } |
Joerg Roedel | 852e3c1 | 2009-07-27 16:30:44 +0200 | [diff] [blame] | 2068 | |
Avi Kivity | d555c33 | 2009-06-10 14:24:23 +0300 | [diff] [blame] | 2069 | if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault, |
Izik Eidus | 1403283 | 2009-09-23 21:47:17 +0300 | [diff] [blame] | 2070 | dirty, level, gfn, pfn, speculative, true, |
| 2071 | reset_host_protection)) { |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 2072 | if (write_fault) |
| 2073 | *ptwrite = 1; |
Xiao Guangrong | 5304efd | 2010-06-08 20:05:57 +0800 | [diff] [blame] | 2074 | kvm_mmu_flush_tlb(vcpu); |
Marcelo Tosatti | a378b4e | 2008-09-23 13:18:31 -0300 | [diff] [blame] | 2075 | } |
Marcelo Tosatti | 1e73f9d | 2008-09-23 13:18:30 -0300 | [diff] [blame] | 2076 | |
Avi Kivity | d555c33 | 2009-06-10 14:24:23 +0300 | [diff] [blame] | 2077 | pgprintk("%s: setting spte %llx\n", __func__, *sptep); |
Xiao Guangrong | 9ad17b1 | 2010-08-28 19:19:42 +0800 | [diff] [blame] | 2078 | pgprintk("instantiating %s PTE (%s) at %llx (%llx) addr %p\n", |
Avi Kivity | d555c33 | 2009-06-10 14:24:23 +0300 | [diff] [blame] | 2079 | is_large_pte(*sptep)? "2MB" : "4kB", |
Joerg Roedel | a205bc1 | 2009-07-09 16:36:01 +0200 | [diff] [blame] | 2080 | *sptep & PT_PRESENT_MASK ?"RW":"R", gfn, |
| 2081 | *sptep, sptep); |
Avi Kivity | d555c33 | 2009-06-10 14:24:23 +0300 | [diff] [blame] | 2082 | if (!was_rmapped && is_large_pte(*sptep)) |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 2083 | ++vcpu->kvm->stat.lpages; |
| 2084 | |
Avi Kivity | d555c33 | 2009-06-10 14:24:23 +0300 | [diff] [blame] | 2085 | page_header_update_slot(vcpu->kvm, sptep, gfn); |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 2086 | if (!was_rmapped) { |
Joerg Roedel | 44ad994 | 2009-07-27 16:30:42 +0200 | [diff] [blame] | 2087 | rmap_count = rmap_add(vcpu, sptep, gfn); |
Marcelo Tosatti | 53a27b3 | 2009-08-05 15:43:58 -0300 | [diff] [blame] | 2088 | if (rmap_count > RMAP_RECYCLE_THRESHOLD) |
Joerg Roedel | 852e3c1 | 2009-07-27 16:30:44 +0200 | [diff] [blame] | 2089 | rmap_recycle(vcpu, sptep, gfn); |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 2090 | } |
Xiao Guangrong | 9ed5520 | 2010-07-16 11:25:17 +0800 | [diff] [blame] | 2091 | kvm_release_pfn_clean(pfn); |
Avi Kivity | 1b7fcd3 | 2008-05-15 13:51:35 +0300 | [diff] [blame] | 2092 | if (speculative) { |
Avi Kivity | d555c33 | 2009-06-10 14:24:23 +0300 | [diff] [blame] | 2093 | vcpu->arch.last_pte_updated = sptep; |
Avi Kivity | 1b7fcd3 | 2008-05-15 13:51:35 +0300 | [diff] [blame] | 2094 | vcpu->arch.last_pte_gfn = gfn; |
| 2095 | } |
Avi Kivity | 1c4f1fd | 2007-12-09 17:40:31 +0200 | [diff] [blame] | 2096 | } |
| 2097 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2098 | static void nonpaging_new_cr3(struct kvm_vcpu *vcpu) |
| 2099 | { |
| 2100 | } |
| 2101 | |
Xiao Guangrong | 957ed9e | 2010-08-22 19:12:48 +0800 | [diff] [blame] | 2102 | static struct kvm_memory_slot * |
| 2103 | pte_prefetch_gfn_to_memslot(struct kvm_vcpu *vcpu, gfn_t gfn, bool no_dirty_log) |
| 2104 | { |
| 2105 | struct kvm_memory_slot *slot; |
| 2106 | |
| 2107 | slot = gfn_to_memslot(vcpu->kvm, gfn); |
| 2108 | if (!slot || slot->flags & KVM_MEMSLOT_INVALID || |
| 2109 | (no_dirty_log && slot->dirty_bitmap)) |
| 2110 | slot = NULL; |
| 2111 | |
| 2112 | return slot; |
| 2113 | } |
| 2114 | |
| 2115 | static pfn_t pte_prefetch_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn, |
| 2116 | bool no_dirty_log) |
| 2117 | { |
| 2118 | struct kvm_memory_slot *slot; |
| 2119 | unsigned long hva; |
| 2120 | |
| 2121 | slot = pte_prefetch_gfn_to_memslot(vcpu, gfn, no_dirty_log); |
| 2122 | if (!slot) { |
| 2123 | get_page(bad_page); |
| 2124 | return page_to_pfn(bad_page); |
| 2125 | } |
| 2126 | |
| 2127 | hva = gfn_to_hva_memslot(slot, gfn); |
| 2128 | |
| 2129 | return hva_to_pfn_atomic(vcpu->kvm, hva); |
| 2130 | } |
| 2131 | |
| 2132 | static int direct_pte_prefetch_many(struct kvm_vcpu *vcpu, |
| 2133 | struct kvm_mmu_page *sp, |
| 2134 | u64 *start, u64 *end) |
| 2135 | { |
| 2136 | struct page *pages[PTE_PREFETCH_NUM]; |
| 2137 | unsigned access = sp->role.access; |
| 2138 | int i, ret; |
| 2139 | gfn_t gfn; |
| 2140 | |
| 2141 | gfn = kvm_mmu_page_get_gfn(sp, start - sp->spt); |
| 2142 | if (!pte_prefetch_gfn_to_memslot(vcpu, gfn, access & ACC_WRITE_MASK)) |
| 2143 | return -1; |
| 2144 | |
| 2145 | ret = gfn_to_page_many_atomic(vcpu->kvm, gfn, pages, end - start); |
| 2146 | if (ret <= 0) |
| 2147 | return -1; |
| 2148 | |
| 2149 | for (i = 0; i < ret; i++, gfn++, start++) |
| 2150 | mmu_set_spte(vcpu, start, ACC_ALL, |
| 2151 | access, 0, 0, 1, NULL, |
| 2152 | sp->role.level, gfn, |
| 2153 | page_to_pfn(pages[i]), true, true); |
| 2154 | |
| 2155 | return 0; |
| 2156 | } |
| 2157 | |
| 2158 | static void __direct_pte_prefetch(struct kvm_vcpu *vcpu, |
| 2159 | struct kvm_mmu_page *sp, u64 *sptep) |
| 2160 | { |
| 2161 | u64 *spte, *start = NULL; |
| 2162 | int i; |
| 2163 | |
| 2164 | WARN_ON(!sp->role.direct); |
| 2165 | |
| 2166 | i = (sptep - sp->spt) & ~(PTE_PREFETCH_NUM - 1); |
| 2167 | spte = sp->spt + i; |
| 2168 | |
| 2169 | for (i = 0; i < PTE_PREFETCH_NUM; i++, spte++) { |
| 2170 | if (*spte != shadow_trap_nonpresent_pte || spte == sptep) { |
| 2171 | if (!start) |
| 2172 | continue; |
| 2173 | if (direct_pte_prefetch_many(vcpu, sp, start, spte) < 0) |
| 2174 | break; |
| 2175 | start = NULL; |
| 2176 | } else if (!start) |
| 2177 | start = spte; |
| 2178 | } |
| 2179 | } |
| 2180 | |
| 2181 | static void direct_pte_prefetch(struct kvm_vcpu *vcpu, u64 *sptep) |
| 2182 | { |
| 2183 | struct kvm_mmu_page *sp; |
| 2184 | |
| 2185 | /* |
| 2186 | * Since it's no accessed bit on EPT, it's no way to |
| 2187 | * distinguish between actually accessed translations |
| 2188 | * and prefetched, so disable pte prefetch if EPT is |
| 2189 | * enabled. |
| 2190 | */ |
| 2191 | if (!shadow_accessed_mask) |
| 2192 | return; |
| 2193 | |
| 2194 | sp = page_header(__pa(sptep)); |
| 2195 | if (sp->role.level > PT_PAGE_TABLE_LEVEL) |
| 2196 | return; |
| 2197 | |
| 2198 | __direct_pte_prefetch(vcpu, sp, sptep); |
| 2199 | } |
| 2200 | |
Joerg Roedel | 4d9976b | 2008-02-07 13:47:42 +0100 | [diff] [blame] | 2201 | 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] | 2202 | int level, gfn_t gfn, pfn_t pfn) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2203 | { |
Avi Kivity | 9f652d2 | 2008-12-25 14:54:25 +0200 | [diff] [blame] | 2204 | struct kvm_shadow_walk_iterator iterator; |
| 2205 | struct kvm_mmu_page *sp; |
| 2206 | int pt_write = 0; |
| 2207 | gfn_t pseudo_gfn; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2208 | |
Avi Kivity | 9f652d2 | 2008-12-25 14:54:25 +0200 | [diff] [blame] | 2209 | for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) { |
Joerg Roedel | 852e3c1 | 2009-07-27 16:30:44 +0200 | [diff] [blame] | 2210 | if (iterator.level == level) { |
Avi Kivity | 9f652d2 | 2008-12-25 14:54:25 +0200 | [diff] [blame] | 2211 | mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL, |
| 2212 | 0, write, 1, &pt_write, |
Izik Eidus | 1403283 | 2009-09-23 21:47:17 +0300 | [diff] [blame] | 2213 | level, gfn, pfn, false, true); |
Xiao Guangrong | 957ed9e | 2010-08-22 19:12:48 +0800 | [diff] [blame] | 2214 | direct_pte_prefetch(vcpu, iterator.sptep); |
Avi Kivity | 9f652d2 | 2008-12-25 14:54:25 +0200 | [diff] [blame] | 2215 | ++vcpu->stat.pf_fixed; |
| 2216 | break; |
| 2217 | } |
| 2218 | |
| 2219 | if (*iterator.sptep == shadow_trap_nonpresent_pte) { |
Lai Jiangshan | c9fa0b3 | 2010-05-26 16:48:25 +0800 | [diff] [blame] | 2220 | u64 base_addr = iterator.addr; |
| 2221 | |
| 2222 | base_addr &= PT64_LVL_ADDR_MASK(iterator.level); |
| 2223 | pseudo_gfn = base_addr >> PAGE_SHIFT; |
Avi Kivity | 9f652d2 | 2008-12-25 14:54:25 +0200 | [diff] [blame] | 2224 | sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr, |
| 2225 | iterator.level - 1, |
| 2226 | 1, ACC_ALL, iterator.sptep); |
| 2227 | if (!sp) { |
| 2228 | pgprintk("nonpaging_map: ENOMEM\n"); |
| 2229 | kvm_release_pfn_clean(pfn); |
| 2230 | return -ENOMEM; |
| 2231 | } |
| 2232 | |
Avi Kivity | d555c33 | 2009-06-10 14:24:23 +0300 | [diff] [blame] | 2233 | __set_spte(iterator.sptep, |
| 2234 | __pa(sp->spt) |
| 2235 | | PT_PRESENT_MASK | PT_WRITABLE_MASK |
| 2236 | | shadow_user_mask | shadow_x_mask); |
Avi Kivity | 9f652d2 | 2008-12-25 14:54:25 +0200 | [diff] [blame] | 2237 | } |
| 2238 | } |
| 2239 | return pt_write; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2240 | } |
| 2241 | |
Huang Ying | bf99815 | 2010-05-31 14:28:19 +0800 | [diff] [blame] | 2242 | static void kvm_send_hwpoison_signal(struct kvm *kvm, gfn_t gfn) |
| 2243 | { |
| 2244 | char buf[1]; |
| 2245 | void __user *hva; |
| 2246 | int r; |
| 2247 | |
| 2248 | /* Touch the page, so send SIGBUS */ |
| 2249 | hva = (void __user *)gfn_to_hva(kvm, gfn); |
| 2250 | r = copy_from_user(buf, hva, 1); |
| 2251 | } |
| 2252 | |
| 2253 | static int kvm_handle_bad_page(struct kvm *kvm, gfn_t gfn, pfn_t pfn) |
| 2254 | { |
| 2255 | kvm_release_pfn_clean(pfn); |
| 2256 | if (is_hwpoison_pfn(pfn)) { |
| 2257 | kvm_send_hwpoison_signal(kvm, gfn); |
| 2258 | return 0; |
Gleb Natapov | edba23e | 2010-07-07 20:16:45 +0300 | [diff] [blame] | 2259 | } else if (is_fault_pfn(pfn)) |
| 2260 | return -EFAULT; |
| 2261 | |
Huang Ying | bf99815 | 2010-05-31 14:28:19 +0800 | [diff] [blame] | 2262 | return 1; |
| 2263 | } |
| 2264 | |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2265 | static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn) |
| 2266 | { |
| 2267 | int r; |
Joerg Roedel | 852e3c1 | 2009-07-27 16:30:44 +0200 | [diff] [blame] | 2268 | int level; |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 2269 | pfn_t pfn; |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 2270 | unsigned long mmu_seq; |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 2271 | |
Joerg Roedel | 852e3c1 | 2009-07-27 16:30:44 +0200 | [diff] [blame] | 2272 | level = mapping_level(vcpu, gfn); |
| 2273 | |
| 2274 | /* |
| 2275 | * This path builds a PAE pagetable - so we can map 2mb pages at |
| 2276 | * maximum. Therefore check if the level is larger than that. |
| 2277 | */ |
| 2278 | if (level > PT_DIRECTORY_LEVEL) |
| 2279 | level = PT_DIRECTORY_LEVEL; |
| 2280 | |
| 2281 | gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1); |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 2282 | |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 2283 | mmu_seq = vcpu->kvm->mmu_notifier_seq; |
Marcelo Tosatti | 4c2155c | 2008-09-16 20:54:47 -0300 | [diff] [blame] | 2284 | smp_rmb(); |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 2285 | pfn = gfn_to_pfn(vcpu->kvm, gfn); |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 2286 | |
Avi Kivity | d196e34 | 2008-01-24 11:44:11 +0200 | [diff] [blame] | 2287 | /* mmio */ |
Huang Ying | bf99815 | 2010-05-31 14:28:19 +0800 | [diff] [blame] | 2288 | if (is_error_pfn(pfn)) |
| 2289 | return kvm_handle_bad_page(vcpu->kvm, gfn, pfn); |
Avi Kivity | d196e34 | 2008-01-24 11:44:11 +0200 | [diff] [blame] | 2290 | |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 2291 | spin_lock(&vcpu->kvm->mmu_lock); |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 2292 | if (mmu_notifier_retry(vcpu, mmu_seq)) |
| 2293 | goto out_unlock; |
Avi Kivity | eb787d1 | 2007-12-31 15:27:49 +0200 | [diff] [blame] | 2294 | kvm_mmu_free_some_pages(vcpu); |
Joerg Roedel | 852e3c1 | 2009-07-27 16:30:44 +0200 | [diff] [blame] | 2295 | r = __direct_map(vcpu, v, write, level, gfn, pfn); |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 2296 | spin_unlock(&vcpu->kvm->mmu_lock); |
| 2297 | |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 2298 | |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2299 | return r; |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 2300 | |
| 2301 | out_unlock: |
| 2302 | spin_unlock(&vcpu->kvm->mmu_lock); |
| 2303 | kvm_release_pfn_clean(pfn); |
| 2304 | return 0; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 2305 | } |
| 2306 | |
| 2307 | |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2308 | static void mmu_free_roots(struct kvm_vcpu *vcpu) |
| 2309 | { |
| 2310 | int i; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2311 | struct kvm_mmu_page *sp; |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 2312 | LIST_HEAD(invalid_list); |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2313 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2314 | if (!VALID_PAGE(vcpu->arch.mmu.root_hpa)) |
Avi Kivity | 7b53aa5 | 2007-06-05 12:17:03 +0300 | [diff] [blame] | 2315 | return; |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 2316 | spin_lock(&vcpu->kvm->mmu_lock); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2317 | if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) { |
| 2318 | hpa_t root = vcpu->arch.mmu.root_hpa; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2319 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2320 | sp = page_header(root); |
| 2321 | --sp->root_count; |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 2322 | if (!sp->root_count && sp->role.invalid) { |
| 2323 | kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list); |
| 2324 | kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list); |
| 2325 | } |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2326 | vcpu->arch.mmu.root_hpa = INVALID_PAGE; |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 2327 | spin_unlock(&vcpu->kvm->mmu_lock); |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2328 | return; |
| 2329 | } |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2330 | for (i = 0; i < 4; ++i) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2331 | hpa_t root = vcpu->arch.mmu.pae_root[i]; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2332 | |
Avi Kivity | 417726a | 2007-04-12 17:35:58 +0300 | [diff] [blame] | 2333 | if (root) { |
Avi Kivity | 417726a | 2007-04-12 17:35:58 +0300 | [diff] [blame] | 2334 | root &= PT64_BASE_ADDR_MASK; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2335 | sp = page_header(root); |
| 2336 | --sp->root_count; |
Marcelo Tosatti | 2e53d63 | 2008-02-20 14:47:24 -0500 | [diff] [blame] | 2337 | if (!sp->root_count && sp->role.invalid) |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 2338 | kvm_mmu_prepare_zap_page(vcpu->kvm, sp, |
| 2339 | &invalid_list); |
Avi Kivity | 417726a | 2007-04-12 17:35:58 +0300 | [diff] [blame] | 2340 | } |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2341 | vcpu->arch.mmu.pae_root[i] = INVALID_PAGE; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2342 | } |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 2343 | kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list); |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 2344 | spin_unlock(&vcpu->kvm->mmu_lock); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2345 | vcpu->arch.mmu.root_hpa = INVALID_PAGE; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2346 | } |
| 2347 | |
Marcelo Tosatti | 8986ecc | 2009-05-12 18:55:45 -0300 | [diff] [blame] | 2348 | static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn) |
| 2349 | { |
| 2350 | int ret = 0; |
| 2351 | |
| 2352 | if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) { |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 2353 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Marcelo Tosatti | 8986ecc | 2009-05-12 18:55:45 -0300 | [diff] [blame] | 2354 | ret = 1; |
| 2355 | } |
| 2356 | |
| 2357 | return ret; |
| 2358 | } |
| 2359 | |
| 2360 | static int mmu_alloc_roots(struct kvm_vcpu *vcpu) |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2361 | { |
| 2362 | int i; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 2363 | gfn_t root_gfn; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2364 | struct kvm_mmu_page *sp; |
Avi Kivity | f6e2c02 | 2009-01-11 13:02:10 +0200 | [diff] [blame] | 2365 | int direct = 0; |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 2366 | u64 pdptr; |
Avi Kivity | 3bb65a2 | 2007-01-05 16:36:51 -0800 | [diff] [blame] | 2367 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2368 | root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2369 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2370 | if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) { |
| 2371 | hpa_t root = vcpu->arch.mmu.root_hpa; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2372 | |
| 2373 | ASSERT(!VALID_PAGE(root)); |
Marcelo Tosatti | 8986ecc | 2009-05-12 18:55:45 -0300 | [diff] [blame] | 2374 | if (mmu_check_root(vcpu, root_gfn)) |
| 2375 | return 1; |
Joerg Roedel | c5a78f2b | 2010-09-10 17:30:39 +0200 | [diff] [blame^] | 2376 | if (vcpu->arch.mmu.direct_map) { |
Eric Northup | 5a7388c | 2010-04-26 17:00:05 -0700 | [diff] [blame] | 2377 | direct = 1; |
| 2378 | root_gfn = 0; |
| 2379 | } |
Avi Kivity | 8facbbf | 2010-05-04 12:58:32 +0300 | [diff] [blame] | 2380 | spin_lock(&vcpu->kvm->mmu_lock); |
Marcelo Tosatti | 24955b6 | 2010-05-12 21:00:35 -0300 | [diff] [blame] | 2381 | kvm_mmu_free_some_pages(vcpu); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2382 | sp = kvm_mmu_get_page(vcpu, root_gfn, 0, |
Avi Kivity | f6e2c02 | 2009-01-11 13:02:10 +0200 | [diff] [blame] | 2383 | PT64_ROOT_LEVEL, direct, |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 2384 | ACC_ALL, NULL); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2385 | root = __pa(sp->spt); |
| 2386 | ++sp->root_count; |
Avi Kivity | 8facbbf | 2010-05-04 12:58:32 +0300 | [diff] [blame] | 2387 | spin_unlock(&vcpu->kvm->mmu_lock); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2388 | vcpu->arch.mmu.root_hpa = root; |
Marcelo Tosatti | 8986ecc | 2009-05-12 18:55:45 -0300 | [diff] [blame] | 2389 | return 0; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2390 | } |
Avi Kivity | f6e2c02 | 2009-01-11 13:02:10 +0200 | [diff] [blame] | 2391 | direct = !is_paging(vcpu); |
Joerg Roedel | f87f928 | 2010-09-02 17:29:45 +0200 | [diff] [blame] | 2392 | |
| 2393 | if (mmu_check_root(vcpu, root_gfn)) |
| 2394 | return 1; |
| 2395 | |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2396 | for (i = 0; i < 4; ++i) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2397 | hpa_t root = vcpu->arch.mmu.pae_root[i]; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2398 | |
| 2399 | ASSERT(!VALID_PAGE(root)); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2400 | if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) { |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 2401 | pdptr = kvm_pdptr_read(vcpu, i); |
Avi Kivity | 43a3795 | 2009-06-10 14:12:05 +0300 | [diff] [blame] | 2402 | if (!is_present_gpte(pdptr)) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2403 | vcpu->arch.mmu.pae_root[i] = 0; |
Avi Kivity | 417726a | 2007-04-12 17:35:58 +0300 | [diff] [blame] | 2404 | continue; |
| 2405 | } |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 2406 | root_gfn = pdptr >> PAGE_SHIFT; |
Joerg Roedel | f87f928 | 2010-09-02 17:29:45 +0200 | [diff] [blame] | 2407 | if (mmu_check_root(vcpu, root_gfn)) |
| 2408 | return 1; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2409 | } else if (vcpu->arch.mmu.root_level == 0) |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 2410 | root_gfn = 0; |
Joerg Roedel | c5a78f2b | 2010-09-10 17:30:39 +0200 | [diff] [blame^] | 2411 | if (vcpu->arch.mmu.direct_map) { |
Eric Northup | 5a7388c | 2010-04-26 17:00:05 -0700 | [diff] [blame] | 2412 | direct = 1; |
| 2413 | root_gfn = i << 30; |
| 2414 | } |
Avi Kivity | 8facbbf | 2010-05-04 12:58:32 +0300 | [diff] [blame] | 2415 | spin_lock(&vcpu->kvm->mmu_lock); |
Marcelo Tosatti | 24955b6 | 2010-05-12 21:00:35 -0300 | [diff] [blame] | 2416 | kvm_mmu_free_some_pages(vcpu); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2417 | sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30, |
Avi Kivity | f6e2c02 | 2009-01-11 13:02:10 +0200 | [diff] [blame] | 2418 | PT32_ROOT_LEVEL, direct, |
Avi Kivity | f7d9c7b | 2008-02-26 22:12:10 +0200 | [diff] [blame] | 2419 | ACC_ALL, NULL); |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2420 | root = __pa(sp->spt); |
| 2421 | ++sp->root_count; |
Avi Kivity | 8facbbf | 2010-05-04 12:58:32 +0300 | [diff] [blame] | 2422 | spin_unlock(&vcpu->kvm->mmu_lock); |
| 2423 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2424 | vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2425 | } |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2426 | vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root); |
Marcelo Tosatti | 8986ecc | 2009-05-12 18:55:45 -0300 | [diff] [blame] | 2427 | return 0; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2428 | } |
| 2429 | |
Marcelo Tosatti | 0ba73cd | 2008-09-23 13:18:34 -0300 | [diff] [blame] | 2430 | static void mmu_sync_roots(struct kvm_vcpu *vcpu) |
| 2431 | { |
| 2432 | int i; |
| 2433 | struct kvm_mmu_page *sp; |
| 2434 | |
| 2435 | if (!VALID_PAGE(vcpu->arch.mmu.root_hpa)) |
| 2436 | return; |
| 2437 | if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) { |
| 2438 | hpa_t root = vcpu->arch.mmu.root_hpa; |
| 2439 | sp = page_header(root); |
| 2440 | mmu_sync_children(vcpu, sp); |
| 2441 | return; |
| 2442 | } |
| 2443 | for (i = 0; i < 4; ++i) { |
| 2444 | hpa_t root = vcpu->arch.mmu.pae_root[i]; |
| 2445 | |
Marcelo Tosatti | 8986ecc | 2009-05-12 18:55:45 -0300 | [diff] [blame] | 2446 | if (root && VALID_PAGE(root)) { |
Marcelo Tosatti | 0ba73cd | 2008-09-23 13:18:34 -0300 | [diff] [blame] | 2447 | root &= PT64_BASE_ADDR_MASK; |
| 2448 | sp = page_header(root); |
| 2449 | mmu_sync_children(vcpu, sp); |
| 2450 | } |
| 2451 | } |
| 2452 | } |
| 2453 | |
| 2454 | void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu) |
| 2455 | { |
| 2456 | spin_lock(&vcpu->kvm->mmu_lock); |
| 2457 | mmu_sync_roots(vcpu); |
| 2458 | spin_unlock(&vcpu->kvm->mmu_lock); |
| 2459 | } |
| 2460 | |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 2461 | static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr, |
| 2462 | u32 access, u32 *error) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2463 | { |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 2464 | if (error) |
| 2465 | *error = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2466 | return vaddr; |
| 2467 | } |
| 2468 | |
| 2469 | static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva, |
Avi Kivity | 3f3e712 | 2007-11-21 14:54:16 +0200 | [diff] [blame] | 2470 | u32 error_code) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2471 | { |
Avi Kivity | e833240 | 2007-12-09 18:43:00 +0200 | [diff] [blame] | 2472 | gfn_t gfn; |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 2473 | int r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2474 | |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 2475 | pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code); |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 2476 | r = mmu_topup_memory_caches(vcpu); |
| 2477 | if (r) |
| 2478 | return r; |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 2479 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2480 | ASSERT(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2481 | ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2482 | |
Avi Kivity | e833240 | 2007-12-09 18:43:00 +0200 | [diff] [blame] | 2483 | gfn = gva >> PAGE_SHIFT; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2484 | |
Avi Kivity | e833240 | 2007-12-09 18:43:00 +0200 | [diff] [blame] | 2485 | return nonpaging_map(vcpu, gva & PAGE_MASK, |
| 2486 | error_code & PFERR_WRITE_MASK, gfn); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2487 | } |
| 2488 | |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 2489 | static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa, |
| 2490 | u32 error_code) |
| 2491 | { |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 2492 | pfn_t pfn; |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 2493 | int r; |
Joerg Roedel | 852e3c1 | 2009-07-27 16:30:44 +0200 | [diff] [blame] | 2494 | int level; |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 2495 | gfn_t gfn = gpa >> PAGE_SHIFT; |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 2496 | unsigned long mmu_seq; |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 2497 | |
| 2498 | ASSERT(vcpu); |
| 2499 | ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa)); |
| 2500 | |
| 2501 | r = mmu_topup_memory_caches(vcpu); |
| 2502 | if (r) |
| 2503 | return r; |
| 2504 | |
Joerg Roedel | 852e3c1 | 2009-07-27 16:30:44 +0200 | [diff] [blame] | 2505 | level = mapping_level(vcpu, gfn); |
| 2506 | |
| 2507 | gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1); |
| 2508 | |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 2509 | mmu_seq = vcpu->kvm->mmu_notifier_seq; |
Marcelo Tosatti | 4c2155c | 2008-09-16 20:54:47 -0300 | [diff] [blame] | 2510 | smp_rmb(); |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 2511 | pfn = gfn_to_pfn(vcpu->kvm, gfn); |
Huang Ying | bf99815 | 2010-05-31 14:28:19 +0800 | [diff] [blame] | 2512 | if (is_error_pfn(pfn)) |
| 2513 | return kvm_handle_bad_page(vcpu->kvm, gfn, pfn); |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 2514 | spin_lock(&vcpu->kvm->mmu_lock); |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 2515 | if (mmu_notifier_retry(vcpu, mmu_seq)) |
| 2516 | goto out_unlock; |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 2517 | kvm_mmu_free_some_pages(vcpu); |
| 2518 | r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK, |
Joerg Roedel | 852e3c1 | 2009-07-27 16:30:44 +0200 | [diff] [blame] | 2519 | level, gfn, pfn); |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 2520 | spin_unlock(&vcpu->kvm->mmu_lock); |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 2521 | |
| 2522 | return r; |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 2523 | |
| 2524 | out_unlock: |
| 2525 | spin_unlock(&vcpu->kvm->mmu_lock); |
| 2526 | kvm_release_pfn_clean(pfn); |
| 2527 | return 0; |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 2528 | } |
| 2529 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2530 | static void nonpaging_free(struct kvm_vcpu *vcpu) |
| 2531 | { |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2532 | mmu_free_roots(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2533 | } |
| 2534 | |
| 2535 | static int nonpaging_init_context(struct kvm_vcpu *vcpu) |
| 2536 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2537 | struct kvm_mmu *context = &vcpu->arch.mmu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2538 | |
| 2539 | context->new_cr3 = nonpaging_new_cr3; |
| 2540 | context->page_fault = nonpaging_page_fault; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2541 | context->gva_to_gpa = nonpaging_gva_to_gpa; |
| 2542 | context->free = nonpaging_free; |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 2543 | context->prefetch_page = nonpaging_prefetch_page; |
Marcelo Tosatti | e8bc217 | 2008-09-23 13:18:33 -0300 | [diff] [blame] | 2544 | context->sync_page = nonpaging_sync_page; |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 2545 | context->invlpg = nonpaging_invlpg; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 2546 | context->root_level = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2547 | context->shadow_root_level = PT32E_ROOT_LEVEL; |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 2548 | context->root_hpa = INVALID_PAGE; |
Joerg Roedel | c5a78f2b | 2010-09-10 17:30:39 +0200 | [diff] [blame^] | 2549 | context->direct_map = true; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2550 | return 0; |
| 2551 | } |
| 2552 | |
Avi Kivity | d835dfe | 2007-11-21 02:57:59 +0200 | [diff] [blame] | 2553 | void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2554 | { |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 2555 | ++vcpu->stat.tlb_flush; |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 2556 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2557 | } |
| 2558 | |
| 2559 | static void paging_new_cr3(struct kvm_vcpu *vcpu) |
| 2560 | { |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 2561 | pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3); |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 2562 | mmu_free_roots(vcpu); |
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 inject_page_fault(struct kvm_vcpu *vcpu, |
| 2566 | u64 addr, |
| 2567 | u32 err_code) |
| 2568 | { |
Avi Kivity | c3c91fe | 2007-11-25 14:04:58 +0200 | [diff] [blame] | 2569 | kvm_inject_page_fault(vcpu, addr, err_code); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2570 | } |
| 2571 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2572 | static void paging_free(struct kvm_vcpu *vcpu) |
| 2573 | { |
| 2574 | nonpaging_free(vcpu); |
| 2575 | } |
| 2576 | |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 2577 | static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level) |
| 2578 | { |
| 2579 | int bit7; |
| 2580 | |
| 2581 | bit7 = (gpte >> 7) & 1; |
| 2582 | return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0; |
| 2583 | } |
| 2584 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2585 | #define PTTYPE 64 |
| 2586 | #include "paging_tmpl.h" |
| 2587 | #undef PTTYPE |
| 2588 | |
| 2589 | #define PTTYPE 32 |
| 2590 | #include "paging_tmpl.h" |
| 2591 | #undef PTTYPE |
| 2592 | |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 2593 | static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level) |
| 2594 | { |
| 2595 | struct kvm_mmu *context = &vcpu->arch.mmu; |
| 2596 | int maxphyaddr = cpuid_maxphyaddr(vcpu); |
| 2597 | u64 exb_bit_rsvd = 0; |
| 2598 | |
| 2599 | if (!is_nx(vcpu)) |
| 2600 | exb_bit_rsvd = rsvd_bits(63, 63); |
| 2601 | switch (level) { |
| 2602 | case PT32_ROOT_LEVEL: |
| 2603 | /* no rsvd bits for 2 level 4K page table entries */ |
| 2604 | context->rsvd_bits_mask[0][1] = 0; |
| 2605 | context->rsvd_bits_mask[0][0] = 0; |
Xiao Guangrong | f815bce | 2010-03-19 17:58:53 +0800 | [diff] [blame] | 2606 | context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0]; |
| 2607 | |
| 2608 | if (!is_pse(vcpu)) { |
| 2609 | context->rsvd_bits_mask[1][1] = 0; |
| 2610 | break; |
| 2611 | } |
| 2612 | |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 2613 | if (is_cpuid_PSE36()) |
| 2614 | /* 36bits PSE 4MB page */ |
| 2615 | context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21); |
| 2616 | else |
| 2617 | /* 32 bits PSE 4MB page */ |
| 2618 | context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21); |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 2619 | break; |
| 2620 | case PT32E_ROOT_LEVEL: |
Dong, Eddie | 20c466b | 2009-03-31 23:03:45 +0800 | [diff] [blame] | 2621 | context->rsvd_bits_mask[0][2] = |
| 2622 | rsvd_bits(maxphyaddr, 63) | |
| 2623 | rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */ |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 2624 | context->rsvd_bits_mask[0][1] = exb_bit_rsvd | |
Sheng Yang | 4c26b4c | 2009-04-02 10:28:37 +0800 | [diff] [blame] | 2625 | rsvd_bits(maxphyaddr, 62); /* PDE */ |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 2626 | context->rsvd_bits_mask[0][0] = exb_bit_rsvd | |
| 2627 | rsvd_bits(maxphyaddr, 62); /* PTE */ |
| 2628 | context->rsvd_bits_mask[1][1] = exb_bit_rsvd | |
| 2629 | rsvd_bits(maxphyaddr, 62) | |
| 2630 | rsvd_bits(13, 20); /* large page */ |
Xiao Guangrong | f815bce | 2010-03-19 17:58:53 +0800 | [diff] [blame] | 2631 | context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0]; |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 2632 | break; |
| 2633 | case PT64_ROOT_LEVEL: |
| 2634 | context->rsvd_bits_mask[0][3] = exb_bit_rsvd | |
| 2635 | rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8); |
| 2636 | context->rsvd_bits_mask[0][2] = exb_bit_rsvd | |
| 2637 | rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8); |
| 2638 | context->rsvd_bits_mask[0][1] = exb_bit_rsvd | |
Sheng Yang | 4c26b4c | 2009-04-02 10:28:37 +0800 | [diff] [blame] | 2639 | rsvd_bits(maxphyaddr, 51); |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 2640 | context->rsvd_bits_mask[0][0] = exb_bit_rsvd | |
| 2641 | rsvd_bits(maxphyaddr, 51); |
| 2642 | context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3]; |
Joerg Roedel | e04da98 | 2009-07-27 16:30:45 +0200 | [diff] [blame] | 2643 | context->rsvd_bits_mask[1][2] = exb_bit_rsvd | |
| 2644 | rsvd_bits(maxphyaddr, 51) | |
| 2645 | rsvd_bits(13, 29); |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 2646 | context->rsvd_bits_mask[1][1] = exb_bit_rsvd | |
Sheng Yang | 4c26b4c | 2009-04-02 10:28:37 +0800 | [diff] [blame] | 2647 | rsvd_bits(maxphyaddr, 51) | |
| 2648 | rsvd_bits(13, 20); /* large page */ |
Xiao Guangrong | f815bce | 2010-03-19 17:58:53 +0800 | [diff] [blame] | 2649 | context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0]; |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 2650 | break; |
| 2651 | } |
| 2652 | } |
| 2653 | |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2654 | static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2655 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2656 | struct kvm_mmu *context = &vcpu->arch.mmu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2657 | |
| 2658 | ASSERT(is_pae(vcpu)); |
| 2659 | context->new_cr3 = paging_new_cr3; |
| 2660 | context->page_fault = paging64_page_fault; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2661 | context->gva_to_gpa = paging64_gva_to_gpa; |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 2662 | context->prefetch_page = paging64_prefetch_page; |
Marcelo Tosatti | e8bc217 | 2008-09-23 13:18:33 -0300 | [diff] [blame] | 2663 | context->sync_page = paging64_sync_page; |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 2664 | context->invlpg = paging64_invlpg; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2665 | context->free = paging_free; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2666 | context->root_level = level; |
| 2667 | context->shadow_root_level = level; |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 2668 | context->root_hpa = INVALID_PAGE; |
Joerg Roedel | c5a78f2b | 2010-09-10 17:30:39 +0200 | [diff] [blame^] | 2669 | context->direct_map = false; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2670 | return 0; |
| 2671 | } |
| 2672 | |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2673 | static int paging64_init_context(struct kvm_vcpu *vcpu) |
| 2674 | { |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 2675 | reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL); |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2676 | return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL); |
| 2677 | } |
| 2678 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2679 | static int paging32_init_context(struct kvm_vcpu *vcpu) |
| 2680 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2681 | struct kvm_mmu *context = &vcpu->arch.mmu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2682 | |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 2683 | reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2684 | context->new_cr3 = paging_new_cr3; |
| 2685 | context->page_fault = paging32_page_fault; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2686 | context->gva_to_gpa = paging32_gva_to_gpa; |
| 2687 | context->free = paging_free; |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 2688 | context->prefetch_page = paging32_prefetch_page; |
Marcelo Tosatti | e8bc217 | 2008-09-23 13:18:33 -0300 | [diff] [blame] | 2689 | context->sync_page = paging32_sync_page; |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 2690 | context->invlpg = paging32_invlpg; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2691 | context->root_level = PT32_ROOT_LEVEL; |
| 2692 | context->shadow_root_level = PT32E_ROOT_LEVEL; |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 2693 | context->root_hpa = INVALID_PAGE; |
Joerg Roedel | c5a78f2b | 2010-09-10 17:30:39 +0200 | [diff] [blame^] | 2694 | context->direct_map = false; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2695 | return 0; |
| 2696 | } |
| 2697 | |
| 2698 | static int paging32E_init_context(struct kvm_vcpu *vcpu) |
| 2699 | { |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 2700 | reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL); |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 2701 | return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2702 | } |
| 2703 | |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 2704 | static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu) |
| 2705 | { |
| 2706 | struct kvm_mmu *context = &vcpu->arch.mmu; |
| 2707 | |
| 2708 | context->new_cr3 = nonpaging_new_cr3; |
| 2709 | context->page_fault = tdp_page_fault; |
| 2710 | context->free = nonpaging_free; |
| 2711 | context->prefetch_page = nonpaging_prefetch_page; |
Marcelo Tosatti | e8bc217 | 2008-09-23 13:18:33 -0300 | [diff] [blame] | 2712 | context->sync_page = nonpaging_sync_page; |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 2713 | context->invlpg = nonpaging_invlpg; |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 2714 | context->shadow_root_level = kvm_x86_ops->get_tdp_level(); |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 2715 | context->root_hpa = INVALID_PAGE; |
Joerg Roedel | c5a78f2b | 2010-09-10 17:30:39 +0200 | [diff] [blame^] | 2716 | context->direct_map = true; |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 2717 | |
| 2718 | if (!is_paging(vcpu)) { |
| 2719 | context->gva_to_gpa = nonpaging_gva_to_gpa; |
| 2720 | context->root_level = 0; |
| 2721 | } else if (is_long_mode(vcpu)) { |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 2722 | reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL); |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 2723 | context->gva_to_gpa = paging64_gva_to_gpa; |
| 2724 | context->root_level = PT64_ROOT_LEVEL; |
| 2725 | } else if (is_pae(vcpu)) { |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 2726 | reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL); |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 2727 | context->gva_to_gpa = paging64_gva_to_gpa; |
| 2728 | context->root_level = PT32E_ROOT_LEVEL; |
| 2729 | } else { |
Dong, Eddie | 82725b2 | 2009-03-30 16:21:08 +0800 | [diff] [blame] | 2730 | reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL); |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 2731 | context->gva_to_gpa = paging32_gva_to_gpa; |
| 2732 | context->root_level = PT32_ROOT_LEVEL; |
| 2733 | } |
| 2734 | |
| 2735 | return 0; |
| 2736 | } |
| 2737 | |
| 2738 | static int init_kvm_softmmu(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2739 | { |
Avi Kivity | a770f6f | 2008-12-21 19:20:09 +0200 | [diff] [blame] | 2740 | int r; |
| 2741 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2742 | ASSERT(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2743 | ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2744 | |
| 2745 | if (!is_paging(vcpu)) |
Avi Kivity | a770f6f | 2008-12-21 19:20:09 +0200 | [diff] [blame] | 2746 | r = nonpaging_init_context(vcpu); |
Avi Kivity | a9058ec | 2006-12-29 16:49:37 -0800 | [diff] [blame] | 2747 | else if (is_long_mode(vcpu)) |
Avi Kivity | a770f6f | 2008-12-21 19:20:09 +0200 | [diff] [blame] | 2748 | r = paging64_init_context(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2749 | else if (is_pae(vcpu)) |
Avi Kivity | a770f6f | 2008-12-21 19:20:09 +0200 | [diff] [blame] | 2750 | r = paging32E_init_context(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2751 | else |
Avi Kivity | a770f6f | 2008-12-21 19:20:09 +0200 | [diff] [blame] | 2752 | r = paging32_init_context(vcpu); |
| 2753 | |
Avi Kivity | 5b7e010 | 2010-04-14 19:20:03 +0300 | [diff] [blame] | 2754 | vcpu->arch.mmu.base_role.cr4_pae = !!is_pae(vcpu); |
Avi Kivity | 3dbe141 | 2010-05-12 11:48:18 +0300 | [diff] [blame] | 2755 | vcpu->arch.mmu.base_role.cr0_wp = is_write_protection(vcpu); |
Avi Kivity | a770f6f | 2008-12-21 19:20:09 +0200 | [diff] [blame] | 2756 | |
| 2757 | return r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2758 | } |
| 2759 | |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 2760 | static int init_kvm_mmu(struct kvm_vcpu *vcpu) |
| 2761 | { |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 2762 | vcpu->arch.update_pte.pfn = bad_pfn; |
| 2763 | |
Joerg Roedel | fb72d16 | 2008-02-07 13:47:44 +0100 | [diff] [blame] | 2764 | if (tdp_enabled) |
| 2765 | return init_kvm_tdp_mmu(vcpu); |
| 2766 | else |
| 2767 | return init_kvm_softmmu(vcpu); |
| 2768 | } |
| 2769 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2770 | static void destroy_kvm_mmu(struct kvm_vcpu *vcpu) |
| 2771 | { |
| 2772 | ASSERT(vcpu); |
Sheng Yang | 62ad075 | 2010-05-12 16:40:41 +0800 | [diff] [blame] | 2773 | if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) |
| 2774 | /* mmu.free() should set root_hpa = INVALID_PAGE */ |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2775 | vcpu->arch.mmu.free(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2776 | } |
| 2777 | |
| 2778 | int kvm_mmu_reset_context(struct kvm_vcpu *vcpu) |
| 2779 | { |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 2780 | destroy_kvm_mmu(vcpu); |
| 2781 | return init_kvm_mmu(vcpu); |
| 2782 | } |
Eddie Dong | 8668a3c | 2007-10-10 14:26:45 +0800 | [diff] [blame] | 2783 | EXPORT_SYMBOL_GPL(kvm_mmu_reset_context); |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 2784 | |
| 2785 | int kvm_mmu_load(struct kvm_vcpu *vcpu) |
| 2786 | { |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 2787 | int r; |
| 2788 | |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 2789 | r = mmu_topup_memory_caches(vcpu); |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 2790 | if (r) |
| 2791 | goto out; |
Marcelo Tosatti | 8986ecc | 2009-05-12 18:55:45 -0300 | [diff] [blame] | 2792 | r = mmu_alloc_roots(vcpu); |
Avi Kivity | 8facbbf | 2010-05-04 12:58:32 +0300 | [diff] [blame] | 2793 | spin_lock(&vcpu->kvm->mmu_lock); |
Marcelo Tosatti | 0ba73cd | 2008-09-23 13:18:34 -0300 | [diff] [blame] | 2794 | mmu_sync_roots(vcpu); |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 2795 | spin_unlock(&vcpu->kvm->mmu_lock); |
Marcelo Tosatti | 8986ecc | 2009-05-12 18:55:45 -0300 | [diff] [blame] | 2796 | if (r) |
| 2797 | goto out; |
Sheng Yang | 3662cb1 | 2009-07-09 17:00:42 +0800 | [diff] [blame] | 2798 | /* set_cr3() should ensure TLB has been flushed */ |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2799 | kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa); |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 2800 | out: |
| 2801 | return r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2802 | } |
Avi Kivity | 17c3ba9 | 2007-06-04 15:58:30 +0300 | [diff] [blame] | 2803 | EXPORT_SYMBOL_GPL(kvm_mmu_load); |
| 2804 | |
| 2805 | void kvm_mmu_unload(struct kvm_vcpu *vcpu) |
| 2806 | { |
| 2807 | mmu_free_roots(vcpu); |
| 2808 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2809 | |
Avi Kivity | 09072da | 2007-05-01 14:16:52 +0300 | [diff] [blame] | 2810 | static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu, |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2811 | struct kvm_mmu_page *sp, |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 2812 | u64 *spte) |
| 2813 | { |
| 2814 | u64 pte; |
| 2815 | struct kvm_mmu_page *child; |
| 2816 | |
| 2817 | pte = *spte; |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 2818 | if (is_shadow_present_pte(pte)) { |
Marcelo Tosatti | 776e663 | 2009-06-10 12:27:03 -0300 | [diff] [blame] | 2819 | if (is_last_spte(pte, sp->role.level)) |
Avi Kivity | be38d27 | 2010-06-06 14:31:27 +0300 | [diff] [blame] | 2820 | drop_spte(vcpu->kvm, spte, shadow_trap_nonpresent_pte); |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 2821 | else { |
| 2822 | child = page_header(pte & PT64_BASE_ADDR_MASK); |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 2823 | mmu_page_remove_parent_pte(child, spte); |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 2824 | } |
| 2825 | } |
Avi Kivity | d555c33 | 2009-06-10 14:24:23 +0300 | [diff] [blame] | 2826 | __set_spte(spte, shadow_trap_nonpresent_pte); |
Marcelo Tosatti | 05da455 | 2008-02-23 11:44:30 -0300 | [diff] [blame] | 2827 | if (is_large_pte(pte)) |
| 2828 | --vcpu->kvm->stat.lpages; |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 2829 | } |
| 2830 | |
Avi Kivity | 0028425 | 2007-05-01 16:53:31 +0300 | [diff] [blame] | 2831 | static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu, |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2832 | struct kvm_mmu_page *sp, |
Avi Kivity | 0028425 | 2007-05-01 16:53:31 +0300 | [diff] [blame] | 2833 | u64 *spte, |
Dong, Eddie | 489f1d6 | 2008-01-07 11:14:20 +0200 | [diff] [blame] | 2834 | const void *new) |
Avi Kivity | 0028425 | 2007-05-01 16:53:31 +0300 | [diff] [blame] | 2835 | { |
Marcelo Tosatti | 3094538 | 2008-06-11 20:32:40 -0300 | [diff] [blame] | 2836 | if (sp->role.level != PT_PAGE_TABLE_LEVEL) { |
Joerg Roedel | 7e4e405 | 2009-07-27 16:30:46 +0200 | [diff] [blame] | 2837 | ++vcpu->kvm->stat.mmu_pde_zapped; |
| 2838 | return; |
Marcelo Tosatti | 3094538 | 2008-06-11 20:32:40 -0300 | [diff] [blame] | 2839 | } |
Avi Kivity | 0028425 | 2007-05-01 16:53:31 +0300 | [diff] [blame] | 2840 | |
Xiao Guangrong | fa1de2b | 2010-07-16 11:19:51 +0800 | [diff] [blame] | 2841 | if (is_rsvd_bits_set(vcpu, *(u64 *)new, PT_PAGE_TABLE_LEVEL)) |
| 2842 | return; |
| 2843 | |
Avi Kivity | 4cee576 | 2007-11-18 16:37:07 +0200 | [diff] [blame] | 2844 | ++vcpu->kvm->stat.mmu_pte_updated; |
Avi Kivity | 5b7e010 | 2010-04-14 19:20:03 +0300 | [diff] [blame] | 2845 | if (!sp->role.cr4_pae) |
Dong, Eddie | 489f1d6 | 2008-01-07 11:14:20 +0200 | [diff] [blame] | 2846 | paging32_update_pte(vcpu, sp, spte, new); |
Avi Kivity | 0028425 | 2007-05-01 16:53:31 +0300 | [diff] [blame] | 2847 | else |
Dong, Eddie | 489f1d6 | 2008-01-07 11:14:20 +0200 | [diff] [blame] | 2848 | paging64_update_pte(vcpu, sp, spte, new); |
Avi Kivity | 0028425 | 2007-05-01 16:53:31 +0300 | [diff] [blame] | 2849 | } |
| 2850 | |
Avi Kivity | 79539ce | 2007-11-21 02:06:21 +0200 | [diff] [blame] | 2851 | static bool need_remote_flush(u64 old, u64 new) |
| 2852 | { |
| 2853 | if (!is_shadow_present_pte(old)) |
| 2854 | return false; |
| 2855 | if (!is_shadow_present_pte(new)) |
| 2856 | return true; |
| 2857 | if ((old ^ new) & PT64_BASE_ADDR_MASK) |
| 2858 | return true; |
| 2859 | old ^= PT64_NX_MASK; |
| 2860 | new ^= PT64_NX_MASK; |
| 2861 | return (old & ~new & PT64_PERM_MASK) != 0; |
| 2862 | } |
| 2863 | |
Xiao Guangrong | 0671a8e | 2010-06-04 21:56:59 +0800 | [diff] [blame] | 2864 | static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, bool zap_page, |
| 2865 | bool remote_flush, bool local_flush) |
Avi Kivity | 79539ce | 2007-11-21 02:06:21 +0200 | [diff] [blame] | 2866 | { |
Xiao Guangrong | 0671a8e | 2010-06-04 21:56:59 +0800 | [diff] [blame] | 2867 | if (zap_page) |
| 2868 | return; |
| 2869 | |
| 2870 | if (remote_flush) |
Avi Kivity | 79539ce | 2007-11-21 02:06:21 +0200 | [diff] [blame] | 2871 | kvm_flush_remote_tlbs(vcpu->kvm); |
Xiao Guangrong | 0671a8e | 2010-06-04 21:56:59 +0800 | [diff] [blame] | 2872 | else if (local_flush) |
Avi Kivity | 79539ce | 2007-11-21 02:06:21 +0200 | [diff] [blame] | 2873 | kvm_mmu_flush_tlb(vcpu); |
| 2874 | } |
| 2875 | |
Avi Kivity | 12b7d28 | 2007-09-23 14:10:49 +0200 | [diff] [blame] | 2876 | static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu) |
| 2877 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2878 | u64 *spte = vcpu->arch.last_pte_updated; |
Avi Kivity | 12b7d28 | 2007-09-23 14:10:49 +0200 | [diff] [blame] | 2879 | |
Sheng Yang | 7b52345 | 2008-04-25 21:13:50 +0800 | [diff] [blame] | 2880 | return !!(spte && (*spte & shadow_accessed_mask)); |
Avi Kivity | 12b7d28 | 2007-09-23 14:10:49 +0200 | [diff] [blame] | 2881 | } |
| 2882 | |
Avi Kivity | d7824ff | 2007-12-30 12:29:05 +0200 | [diff] [blame] | 2883 | 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] | 2884 | u64 gpte) |
Avi Kivity | d7824ff | 2007-12-30 12:29:05 +0200 | [diff] [blame] | 2885 | { |
| 2886 | gfn_t gfn; |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 2887 | pfn_t pfn; |
Avi Kivity | d7824ff | 2007-12-30 12:29:05 +0200 | [diff] [blame] | 2888 | |
Avi Kivity | 43a3795 | 2009-06-10 14:12:05 +0300 | [diff] [blame] | 2889 | if (!is_present_gpte(gpte)) |
Avi Kivity | d7824ff | 2007-12-30 12:29:05 +0200 | [diff] [blame] | 2890 | return; |
| 2891 | gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT; |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 2892 | |
Andrea Arcangeli | e930bff | 2008-07-25 16:24:52 +0200 | [diff] [blame] | 2893 | vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq; |
Marcelo Tosatti | 4c2155c | 2008-09-16 20:54:47 -0300 | [diff] [blame] | 2894 | smp_rmb(); |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 2895 | pfn = gfn_to_pfn(vcpu->kvm, gfn); |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 2896 | |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 2897 | if (is_error_pfn(pfn)) { |
| 2898 | kvm_release_pfn_clean(pfn); |
Avi Kivity | d196e34 | 2008-01-24 11:44:11 +0200 | [diff] [blame] | 2899 | return; |
| 2900 | } |
Avi Kivity | d7824ff | 2007-12-30 12:29:05 +0200 | [diff] [blame] | 2901 | vcpu->arch.update_pte.gfn = gfn; |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 2902 | vcpu->arch.update_pte.pfn = pfn; |
Avi Kivity | d7824ff | 2007-12-30 12:29:05 +0200 | [diff] [blame] | 2903 | } |
| 2904 | |
Avi Kivity | 1b7fcd3 | 2008-05-15 13:51:35 +0300 | [diff] [blame] | 2905 | static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn) |
| 2906 | { |
| 2907 | u64 *spte = vcpu->arch.last_pte_updated; |
| 2908 | |
| 2909 | if (spte |
| 2910 | && vcpu->arch.last_pte_gfn == gfn |
| 2911 | && shadow_accessed_mask |
| 2912 | && !(*spte & shadow_accessed_mask) |
| 2913 | && is_shadow_present_pte(*spte)) |
| 2914 | set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte); |
| 2915 | } |
| 2916 | |
Avi Kivity | 09072da | 2007-05-01 14:16:52 +0300 | [diff] [blame] | 2917 | void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa, |
Marcelo Tosatti | ad218f8 | 2008-12-01 22:32:05 -0200 | [diff] [blame] | 2918 | const u8 *new, int bytes, |
| 2919 | bool guest_initiated) |
Avi Kivity | da4a00f | 2007-01-05 16:36:44 -0800 | [diff] [blame] | 2920 | { |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 2921 | gfn_t gfn = gpa >> PAGE_SHIFT; |
Xiao Guangrong | fa1de2b | 2010-07-16 11:19:51 +0800 | [diff] [blame] | 2922 | union kvm_mmu_page_role mask = { .word = 0 }; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 2923 | struct kvm_mmu_page *sp; |
Xiao Guangrong | f41d335 | 2010-06-04 21:56:11 +0800 | [diff] [blame] | 2924 | struct hlist_node *node; |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 2925 | LIST_HEAD(invalid_list); |
Dong, Eddie | 489f1d6 | 2008-01-07 11:14:20 +0200 | [diff] [blame] | 2926 | u64 entry, gentry; |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 2927 | u64 *spte; |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 2928 | unsigned offset = offset_in_page(gpa); |
Avi Kivity | 0e7bc4b | 2007-01-05 16:36:48 -0800 | [diff] [blame] | 2929 | unsigned pte_size; |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 2930 | unsigned page_offset; |
Avi Kivity | 0e7bc4b | 2007-01-05 16:36:48 -0800 | [diff] [blame] | 2931 | unsigned misaligned; |
Avi Kivity | fce0657 | 2007-05-01 16:44:05 +0300 | [diff] [blame] | 2932 | unsigned quadrant; |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 2933 | int level; |
Avi Kivity | 86a5ba0 | 2007-01-05 16:36:50 -0800 | [diff] [blame] | 2934 | int flooded = 0; |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 2935 | int npte; |
Dong, Eddie | 489f1d6 | 2008-01-07 11:14:20 +0200 | [diff] [blame] | 2936 | int r; |
Avi Kivity | 08e850c | 2010-03-15 13:59:57 +0200 | [diff] [blame] | 2937 | int invlpg_counter; |
Xiao Guangrong | 0671a8e | 2010-06-04 21:56:59 +0800 | [diff] [blame] | 2938 | bool remote_flush, local_flush, zap_page; |
| 2939 | |
| 2940 | zap_page = remote_flush = local_flush = false; |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 2941 | |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 2942 | pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes); |
Avi Kivity | 72016f3 | 2010-03-15 13:59:53 +0200 | [diff] [blame] | 2943 | |
Avi Kivity | 08e850c | 2010-03-15 13:59:57 +0200 | [diff] [blame] | 2944 | invlpg_counter = atomic_read(&vcpu->kvm->arch.invlpg_counter); |
| 2945 | |
| 2946 | /* |
| 2947 | * Assume that the pte write on a page table of the same type |
| 2948 | * as the current vcpu paging mode. This is nearly always true |
| 2949 | * (might be false while changing modes). Note it is verified later |
| 2950 | * by update_pte(). |
| 2951 | */ |
| 2952 | if ((is_pae(vcpu) && bytes == 4) || !new) { |
| 2953 | /* Handle a 32-bit guest writing two halves of a 64-bit gpte */ |
| 2954 | if (is_pae(vcpu)) { |
| 2955 | gpa &= ~(gpa_t)7; |
| 2956 | bytes = 8; |
| 2957 | } |
| 2958 | r = kvm_read_guest(vcpu->kvm, gpa, &gentry, min(bytes, 8)); |
| 2959 | if (r) |
| 2960 | gentry = 0; |
| 2961 | new = (const u8 *)&gentry; |
| 2962 | } |
| 2963 | |
Avi Kivity | 72016f3 | 2010-03-15 13:59:53 +0200 | [diff] [blame] | 2964 | switch (bytes) { |
| 2965 | case 4: |
| 2966 | gentry = *(const u32 *)new; |
| 2967 | break; |
| 2968 | case 8: |
| 2969 | gentry = *(const u64 *)new; |
| 2970 | break; |
| 2971 | default: |
| 2972 | gentry = 0; |
| 2973 | break; |
| 2974 | } |
| 2975 | |
Avi Kivity | 72016f3 | 2010-03-15 13:59:53 +0200 | [diff] [blame] | 2976 | mmu_guess_page_from_pte_write(vcpu, gpa, gentry); |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 2977 | spin_lock(&vcpu->kvm->mmu_lock); |
Avi Kivity | 08e850c | 2010-03-15 13:59:57 +0200 | [diff] [blame] | 2978 | if (atomic_read(&vcpu->kvm->arch.invlpg_counter) != invlpg_counter) |
| 2979 | gentry = 0; |
Avi Kivity | 1b7fcd3 | 2008-05-15 13:51:35 +0300 | [diff] [blame] | 2980 | kvm_mmu_access_page(vcpu, gfn); |
Avi Kivity | eb787d1 | 2007-12-31 15:27:49 +0200 | [diff] [blame] | 2981 | kvm_mmu_free_some_pages(vcpu); |
Avi Kivity | 4cee576 | 2007-11-18 16:37:07 +0200 | [diff] [blame] | 2982 | ++vcpu->kvm->stat.mmu_pte_write; |
Xiao Guangrong | 8b1fe17 | 2010-08-30 18:22:53 +0800 | [diff] [blame] | 2983 | trace_kvm_mmu_audit(vcpu, AUDIT_PRE_PTE_WRITE); |
Marcelo Tosatti | ad218f8 | 2008-12-01 22:32:05 -0200 | [diff] [blame] | 2984 | if (guest_initiated) { |
| 2985 | if (gfn == vcpu->arch.last_pt_write_gfn |
| 2986 | && !last_updated_pte_accessed(vcpu)) { |
| 2987 | ++vcpu->arch.last_pt_write_count; |
| 2988 | if (vcpu->arch.last_pt_write_count >= 3) |
| 2989 | flooded = 1; |
| 2990 | } else { |
| 2991 | vcpu->arch.last_pt_write_gfn = gfn; |
| 2992 | vcpu->arch.last_pt_write_count = 1; |
| 2993 | vcpu->arch.last_pte_updated = NULL; |
| 2994 | } |
Avi Kivity | 86a5ba0 | 2007-01-05 16:36:50 -0800 | [diff] [blame] | 2995 | } |
Xiao Guangrong | 3246af0 | 2010-04-16 16:35:54 +0800 | [diff] [blame] | 2996 | |
Xiao Guangrong | fa1de2b | 2010-07-16 11:19:51 +0800 | [diff] [blame] | 2997 | mask.cr0_wp = mask.cr4_pae = mask.nxe = 1; |
Xiao Guangrong | f41d335 | 2010-06-04 21:56:11 +0800 | [diff] [blame] | 2998 | for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn, node) { |
Avi Kivity | 5b7e010 | 2010-04-14 19:20:03 +0300 | [diff] [blame] | 2999 | pte_size = sp->role.cr4_pae ? 8 : 4; |
Avi Kivity | 0e7bc4b | 2007-01-05 16:36:48 -0800 | [diff] [blame] | 3000 | misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1); |
Avi Kivity | e925c5b | 2007-04-30 14:47:02 +0300 | [diff] [blame] | 3001 | misaligned |= bytes < 4; |
Avi Kivity | 86a5ba0 | 2007-01-05 16:36:50 -0800 | [diff] [blame] | 3002 | if (misaligned || flooded) { |
Avi Kivity | 0e7bc4b | 2007-01-05 16:36:48 -0800 | [diff] [blame] | 3003 | /* |
| 3004 | * Misaligned accesses are too much trouble to fix |
| 3005 | * up; also, they usually indicate a page is not used |
| 3006 | * as a page table. |
Avi Kivity | 86a5ba0 | 2007-01-05 16:36:50 -0800 | [diff] [blame] | 3007 | * |
| 3008 | * If we're seeing too many writes to a page, |
| 3009 | * it may no longer be a page table, or we may be |
| 3010 | * forking, in which case it is better to unmap the |
| 3011 | * page. |
Avi Kivity | 0e7bc4b | 2007-01-05 16:36:48 -0800 | [diff] [blame] | 3012 | */ |
| 3013 | pgprintk("misaligned: gpa %llx bytes %d role %x\n", |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 3014 | gpa, bytes, sp->role.word); |
Xiao Guangrong | 0671a8e | 2010-06-04 21:56:59 +0800 | [diff] [blame] | 3015 | zap_page |= !!kvm_mmu_prepare_zap_page(vcpu->kvm, sp, |
Xiao Guangrong | f41d335 | 2010-06-04 21:56:11 +0800 | [diff] [blame] | 3016 | &invalid_list); |
Avi Kivity | 4cee576 | 2007-11-18 16:37:07 +0200 | [diff] [blame] | 3017 | ++vcpu->kvm->stat.mmu_flooded; |
Avi Kivity | 0e7bc4b | 2007-01-05 16:36:48 -0800 | [diff] [blame] | 3018 | continue; |
| 3019 | } |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 3020 | page_offset = offset; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 3021 | level = sp->role.level; |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 3022 | npte = 1; |
Avi Kivity | 5b7e010 | 2010-04-14 19:20:03 +0300 | [diff] [blame] | 3023 | if (!sp->role.cr4_pae) { |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 3024 | page_offset <<= 1; /* 32->64 */ |
| 3025 | /* |
| 3026 | * A 32-bit pde maps 4MB while the shadow pdes map |
| 3027 | * only 2MB. So we need to double the offset again |
| 3028 | * and zap two pdes instead of one. |
| 3029 | */ |
| 3030 | if (level == PT32_ROOT_LEVEL) { |
Avi Kivity | 6b8d0f9 | 2007-04-18 11:18:18 +0300 | [diff] [blame] | 3031 | page_offset &= ~7; /* kill rounding error */ |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 3032 | page_offset <<= 1; |
| 3033 | npte = 2; |
| 3034 | } |
Avi Kivity | fce0657 | 2007-05-01 16:44:05 +0300 | [diff] [blame] | 3035 | quadrant = page_offset >> PAGE_SHIFT; |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 3036 | page_offset &= ~PAGE_MASK; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 3037 | if (quadrant != sp->role.quadrant) |
Avi Kivity | fce0657 | 2007-05-01 16:44:05 +0300 | [diff] [blame] | 3038 | continue; |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 3039 | } |
Xiao Guangrong | 0671a8e | 2010-06-04 21:56:59 +0800 | [diff] [blame] | 3040 | local_flush = true; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 3041 | spte = &sp->spt[page_offset / sizeof(*spte)]; |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 3042 | while (npte--) { |
Avi Kivity | 79539ce | 2007-11-21 02:06:21 +0200 | [diff] [blame] | 3043 | entry = *spte; |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 3044 | mmu_pte_write_zap_pte(vcpu, sp, spte); |
Xiao Guangrong | fa1de2b | 2010-07-16 11:19:51 +0800 | [diff] [blame] | 3045 | if (gentry && |
| 3046 | !((sp->role.word ^ vcpu->arch.mmu.base_role.word) |
| 3047 | & mask.word)) |
Avi Kivity | 72016f3 | 2010-03-15 13:59:53 +0200 | [diff] [blame] | 3048 | mmu_pte_write_new_pte(vcpu, sp, spte, &gentry); |
Xiao Guangrong | 0671a8e | 2010-06-04 21:56:59 +0800 | [diff] [blame] | 3049 | if (!remote_flush && need_remote_flush(entry, *spte)) |
| 3050 | remote_flush = true; |
Avi Kivity | ac1b714 | 2007-03-08 17:13:32 +0200 | [diff] [blame] | 3051 | ++spte; |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 3052 | } |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 3053 | } |
Xiao Guangrong | 0671a8e | 2010-06-04 21:56:59 +0800 | [diff] [blame] | 3054 | mmu_pte_write_flush_tlb(vcpu, zap_page, remote_flush, local_flush); |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 3055 | kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list); |
Xiao Guangrong | 8b1fe17 | 2010-08-30 18:22:53 +0800 | [diff] [blame] | 3056 | trace_kvm_mmu_audit(vcpu, AUDIT_POST_PTE_WRITE); |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 3057 | spin_unlock(&vcpu->kvm->mmu_lock); |
Anthony Liguori | 35149e2 | 2008-04-02 14:46:56 -0500 | [diff] [blame] | 3058 | if (!is_error_pfn(vcpu->arch.update_pte.pfn)) { |
| 3059 | kvm_release_pfn_clean(vcpu->arch.update_pte.pfn); |
| 3060 | vcpu->arch.update_pte.pfn = bad_pfn; |
Avi Kivity | d7824ff | 2007-12-30 12:29:05 +0200 | [diff] [blame] | 3061 | } |
Avi Kivity | da4a00f | 2007-01-05 16:36:44 -0800 | [diff] [blame] | 3062 | } |
| 3063 | |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 3064 | int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva) |
| 3065 | { |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3066 | gpa_t gpa; |
| 3067 | int r; |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 3068 | |
Joerg Roedel | c5a78f2b | 2010-09-10 17:30:39 +0200 | [diff] [blame^] | 3069 | if (vcpu->arch.mmu.direct_map) |
Avi Kivity | 60f2478 | 2009-08-27 13:37:06 +0300 | [diff] [blame] | 3070 | return 0; |
| 3071 | |
Gleb Natapov | 1871c60 | 2010-02-10 14:21:32 +0200 | [diff] [blame] | 3072 | gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3073 | |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 3074 | spin_lock(&vcpu->kvm->mmu_lock); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3075 | r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT); |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 3076 | spin_unlock(&vcpu->kvm->mmu_lock); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3077 | return r; |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 3078 | } |
Avi Kivity | 577bdc4 | 2008-07-19 08:57:05 +0300 | [diff] [blame] | 3079 | EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt); |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 3080 | |
Avi Kivity | 22d95b1 | 2007-09-14 20:26:06 +0300 | [diff] [blame] | 3081 | void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu) |
Avi Kivity | ebeace8 | 2007-01-05 16:36:47 -0800 | [diff] [blame] | 3082 | { |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 3083 | LIST_HEAD(invalid_list); |
Xiao Guangrong | 103ad25 | 2010-06-04 21:54:38 +0800 | [diff] [blame] | 3084 | |
Dave Hansen | e0df7b9 | 2010-08-19 18:11:05 -0700 | [diff] [blame] | 3085 | while (kvm_mmu_available_pages(vcpu->kvm) < KVM_REFILL_PAGES && |
Izik Eidus | 3b80fff | 2009-07-28 15:26:58 -0300 | [diff] [blame] | 3086 | !list_empty(&vcpu->kvm->arch.active_mmu_pages)) { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 3087 | struct kvm_mmu_page *sp; |
Avi Kivity | ebeace8 | 2007-01-05 16:36:47 -0800 | [diff] [blame] | 3088 | |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 3089 | sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev, |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 3090 | struct kvm_mmu_page, link); |
Dave Hansen | e0df7b9 | 2010-08-19 18:11:05 -0700 | [diff] [blame] | 3091 | kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list); |
Xiaotian Feng | 80b63fa | 2010-08-24 10:31:07 +0800 | [diff] [blame] | 3092 | kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list); |
Avi Kivity | 4cee576 | 2007-11-18 16:37:07 +0200 | [diff] [blame] | 3093 | ++vcpu->kvm->stat.mmu_recycled; |
Avi Kivity | ebeace8 | 2007-01-05 16:36:47 -0800 | [diff] [blame] | 3094 | } |
| 3095 | } |
Avi Kivity | ebeace8 | 2007-01-05 16:36:47 -0800 | [diff] [blame] | 3096 | |
Avi Kivity | 3067714 | 2007-10-28 18:48:59 +0200 | [diff] [blame] | 3097 | int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code) |
| 3098 | { |
| 3099 | int r; |
| 3100 | enum emulation_result er; |
| 3101 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3102 | r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code); |
Avi Kivity | 3067714 | 2007-10-28 18:48:59 +0200 | [diff] [blame] | 3103 | if (r < 0) |
| 3104 | goto out; |
| 3105 | |
| 3106 | if (!r) { |
| 3107 | r = 1; |
| 3108 | goto out; |
| 3109 | } |
| 3110 | |
Avi Kivity | b733bfb | 2007-10-28 18:52:05 +0200 | [diff] [blame] | 3111 | r = mmu_topup_memory_caches(vcpu); |
| 3112 | if (r) |
| 3113 | goto out; |
| 3114 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3115 | er = emulate_instruction(vcpu, cr2, error_code, 0); |
Avi Kivity | 3067714 | 2007-10-28 18:48:59 +0200 | [diff] [blame] | 3116 | |
| 3117 | switch (er) { |
| 3118 | case EMULATE_DONE: |
| 3119 | return 1; |
| 3120 | case EMULATE_DO_MMIO: |
| 3121 | ++vcpu->stat.mmio_exits; |
Gleb Natapov | 6d77dbf | 2010-05-10 11:16:56 +0300 | [diff] [blame] | 3122 | /* fall through */ |
Avi Kivity | 3067714 | 2007-10-28 18:48:59 +0200 | [diff] [blame] | 3123 | case EMULATE_FAIL: |
Avi Kivity | 3f5d18a | 2009-06-11 15:43:28 +0300 | [diff] [blame] | 3124 | return 0; |
Avi Kivity | 3067714 | 2007-10-28 18:48:59 +0200 | [diff] [blame] | 3125 | default: |
| 3126 | BUG(); |
| 3127 | } |
| 3128 | out: |
Avi Kivity | 3067714 | 2007-10-28 18:48:59 +0200 | [diff] [blame] | 3129 | return r; |
| 3130 | } |
| 3131 | EXPORT_SYMBOL_GPL(kvm_mmu_page_fault); |
| 3132 | |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 3133 | void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva) |
| 3134 | { |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 3135 | vcpu->arch.mmu.invlpg(vcpu, gva); |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 3136 | kvm_mmu_flush_tlb(vcpu); |
| 3137 | ++vcpu->stat.invlpg; |
| 3138 | } |
| 3139 | EXPORT_SYMBOL_GPL(kvm_mmu_invlpg); |
| 3140 | |
Joerg Roedel | 1855267 | 2008-02-07 13:47:41 +0100 | [diff] [blame] | 3141 | void kvm_enable_tdp(void) |
| 3142 | { |
| 3143 | tdp_enabled = true; |
| 3144 | } |
| 3145 | EXPORT_SYMBOL_GPL(kvm_enable_tdp); |
| 3146 | |
Joerg Roedel | 5f4cb66 | 2008-07-14 20:36:36 +0200 | [diff] [blame] | 3147 | void kvm_disable_tdp(void) |
| 3148 | { |
| 3149 | tdp_enabled = false; |
| 3150 | } |
| 3151 | EXPORT_SYMBOL_GPL(kvm_disable_tdp); |
| 3152 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3153 | static void free_mmu_pages(struct kvm_vcpu *vcpu) |
| 3154 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3155 | free_page((unsigned long)vcpu->arch.mmu.pae_root); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3156 | } |
| 3157 | |
| 3158 | static int alloc_mmu_pages(struct kvm_vcpu *vcpu) |
| 3159 | { |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 3160 | struct page *page; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3161 | int i; |
| 3162 | |
| 3163 | ASSERT(vcpu); |
| 3164 | |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 3165 | /* |
| 3166 | * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64. |
| 3167 | * Therefore we need to allocate shadow page tables in the first |
| 3168 | * 4GB of memory, which happens to fit the DMA32 zone. |
| 3169 | */ |
| 3170 | page = alloc_page(GFP_KERNEL | __GFP_DMA32); |
| 3171 | if (!page) |
Wei Yongjun | d7fa6ab | 2010-01-22 16:55:05 +0800 | [diff] [blame] | 3172 | return -ENOMEM; |
| 3173 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3174 | vcpu->arch.mmu.pae_root = page_address(page); |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 3175 | for (i = 0; i < 4; ++i) |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3176 | vcpu->arch.mmu.pae_root[i] = INVALID_PAGE; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 3177 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3178 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3179 | } |
| 3180 | |
Ingo Molnar | 8018c27 | 2006-12-29 16:50:01 -0800 | [diff] [blame] | 3181 | int kvm_mmu_create(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3182 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3183 | ASSERT(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3184 | ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3185 | |
Ingo Molnar | 8018c27 | 2006-12-29 16:50:01 -0800 | [diff] [blame] | 3186 | return alloc_mmu_pages(vcpu); |
| 3187 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3188 | |
Ingo Molnar | 8018c27 | 2006-12-29 16:50:01 -0800 | [diff] [blame] | 3189 | int kvm_mmu_setup(struct kvm_vcpu *vcpu) |
| 3190 | { |
| 3191 | ASSERT(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3192 | ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa)); |
Avi Kivity | 2c26495 | 2006-12-22 01:05:28 -0800 | [diff] [blame] | 3193 | |
Ingo Molnar | 8018c27 | 2006-12-29 16:50:01 -0800 | [diff] [blame] | 3194 | return init_kvm_mmu(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3195 | } |
| 3196 | |
| 3197 | void kvm_mmu_destroy(struct kvm_vcpu *vcpu) |
| 3198 | { |
| 3199 | ASSERT(vcpu); |
| 3200 | |
| 3201 | destroy_kvm_mmu(vcpu); |
| 3202 | free_mmu_pages(vcpu); |
Avi Kivity | 714b93d | 2007-01-05 16:36:53 -0800 | [diff] [blame] | 3203 | mmu_free_memory_caches(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3204 | } |
| 3205 | |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 3206 | void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3207 | { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 3208 | struct kvm_mmu_page *sp; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3209 | |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 3210 | list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3211 | int i; |
| 3212 | u64 *pt; |
| 3213 | |
Sheng Yang | 291f26b | 2008-10-16 17:30:57 +0800 | [diff] [blame] | 3214 | if (!test_bit(slot, sp->slot_bitmap)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3215 | continue; |
| 3216 | |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 3217 | pt = sp->spt; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3218 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) |
| 3219 | /* avoid RMW */ |
Gui Jianfeng | 01c168a | 2010-05-27 16:09:48 +0800 | [diff] [blame] | 3220 | if (is_writable_pte(pt[i])) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3221 | pt[i] &= ~PT_WRITABLE_MASK; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3222 | } |
Avi Kivity | 171d595 | 2008-08-27 16:40:51 +0300 | [diff] [blame] | 3223 | kvm_flush_remote_tlbs(kvm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3224 | } |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 3225 | |
Avi Kivity | 90cb052 | 2007-07-17 13:04:56 +0300 | [diff] [blame] | 3226 | void kvm_mmu_zap_all(struct kvm *kvm) |
Dor Laor | e0fa826 | 2007-03-30 13:06:33 +0300 | [diff] [blame] | 3227 | { |
Avi Kivity | 4db3531 | 2007-11-21 15:28:32 +0200 | [diff] [blame] | 3228 | struct kvm_mmu_page *sp, *node; |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 3229 | LIST_HEAD(invalid_list); |
Dor Laor | e0fa826 | 2007-03-30 13:06:33 +0300 | [diff] [blame] | 3230 | |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 3231 | spin_lock(&kvm->mmu_lock); |
Xiao Guangrong | 3246af0 | 2010-04-16 16:35:54 +0800 | [diff] [blame] | 3232 | restart: |
Zhang Xiantao | f05e70a | 2007-12-14 10:01:48 +0800 | [diff] [blame] | 3233 | 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] | 3234 | if (kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list)) |
Xiao Guangrong | 3246af0 | 2010-04-16 16:35:54 +0800 | [diff] [blame] | 3235 | goto restart; |
| 3236 | |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 3237 | kvm_mmu_commit_zap_page(kvm, &invalid_list); |
Marcelo Tosatti | aaee2c9 | 2007-12-20 19:18:26 -0500 | [diff] [blame] | 3238 | spin_unlock(&kvm->mmu_lock); |
Dor Laor | e0fa826 | 2007-03-30 13:06:33 +0300 | [diff] [blame] | 3239 | } |
| 3240 | |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 3241 | static int kvm_mmu_remove_some_alloc_mmu_pages(struct kvm *kvm, |
| 3242 | struct list_head *invalid_list) |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 3243 | { |
| 3244 | struct kvm_mmu_page *page; |
| 3245 | |
| 3246 | page = container_of(kvm->arch.active_mmu_pages.prev, |
| 3247 | struct kvm_mmu_page, link); |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 3248 | return kvm_mmu_prepare_zap_page(kvm, page, invalid_list); |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 3249 | } |
| 3250 | |
Dave Chinner | 7f8275d | 2010-07-19 14:56:17 +1000 | [diff] [blame] | 3251 | 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] | 3252 | { |
| 3253 | struct kvm *kvm; |
| 3254 | struct kvm *kvm_freed = NULL; |
Dave Hansen | 45221ab | 2010-08-19 18:11:37 -0700 | [diff] [blame] | 3255 | |
| 3256 | if (nr_to_scan == 0) |
| 3257 | goto out; |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 3258 | |
| 3259 | spin_lock(&kvm_lock); |
| 3260 | |
| 3261 | list_for_each_entry(kvm, &vm_list, vm_list) { |
Dave Hansen | 45221ab | 2010-08-19 18:11:37 -0700 | [diff] [blame] | 3262 | int idx, freed_pages; |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 3263 | LIST_HEAD(invalid_list); |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 3264 | |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 3265 | idx = srcu_read_lock(&kvm->srcu); |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 3266 | spin_lock(&kvm->mmu_lock); |
Dave Hansen | 45221ab | 2010-08-19 18:11:37 -0700 | [diff] [blame] | 3267 | if (!kvm_freed && nr_to_scan > 0 && |
| 3268 | kvm->arch.n_used_mmu_pages > 0) { |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 3269 | freed_pages = kvm_mmu_remove_some_alloc_mmu_pages(kvm, |
| 3270 | &invalid_list); |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 3271 | kvm_freed = kvm; |
| 3272 | } |
| 3273 | nr_to_scan--; |
| 3274 | |
Xiao Guangrong | d98ba05 | 2010-06-04 21:55:29 +0800 | [diff] [blame] | 3275 | kvm_mmu_commit_zap_page(kvm, &invalid_list); |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 3276 | spin_unlock(&kvm->mmu_lock); |
Marcelo Tosatti | f656ce0 | 2009-12-23 14:35:25 -0200 | [diff] [blame] | 3277 | srcu_read_unlock(&kvm->srcu, idx); |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 3278 | } |
| 3279 | if (kvm_freed) |
| 3280 | list_move_tail(&kvm_freed->vm_list, &vm_list); |
| 3281 | |
| 3282 | spin_unlock(&kvm_lock); |
| 3283 | |
Dave Hansen | 45221ab | 2010-08-19 18:11:37 -0700 | [diff] [blame] | 3284 | out: |
| 3285 | return percpu_counter_read_positive(&kvm_total_used_mmu_pages); |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 3286 | } |
| 3287 | |
| 3288 | static struct shrinker mmu_shrinker = { |
| 3289 | .shrink = mmu_shrink, |
| 3290 | .seeks = DEFAULT_SEEKS * 10, |
| 3291 | }; |
| 3292 | |
Ingo Molnar | 2ddfd20 | 2008-05-22 10:37:48 +0200 | [diff] [blame] | 3293 | static void mmu_destroy_caches(void) |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 3294 | { |
| 3295 | if (pte_chain_cache) |
| 3296 | kmem_cache_destroy(pte_chain_cache); |
| 3297 | if (rmap_desc_cache) |
| 3298 | kmem_cache_destroy(rmap_desc_cache); |
Avi Kivity | d3d25b0 | 2007-05-30 12:34:53 +0300 | [diff] [blame] | 3299 | if (mmu_page_header_cache) |
| 3300 | kmem_cache_destroy(mmu_page_header_cache); |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 3301 | } |
| 3302 | |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 3303 | void kvm_mmu_module_exit(void) |
| 3304 | { |
| 3305 | mmu_destroy_caches(); |
Wei Yongjun | 45bf21a | 2010-08-23 16:13:15 +0800 | [diff] [blame] | 3306 | percpu_counter_destroy(&kvm_total_used_mmu_pages); |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 3307 | unregister_shrinker(&mmu_shrinker); |
| 3308 | } |
| 3309 | |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 3310 | int kvm_mmu_module_init(void) |
| 3311 | { |
| 3312 | pte_chain_cache = kmem_cache_create("kvm_pte_chain", |
| 3313 | sizeof(struct kvm_pte_chain), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 3314 | 0, 0, NULL); |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 3315 | if (!pte_chain_cache) |
| 3316 | goto nomem; |
| 3317 | rmap_desc_cache = kmem_cache_create("kvm_rmap_desc", |
| 3318 | sizeof(struct kvm_rmap_desc), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 3319 | 0, 0, NULL); |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 3320 | if (!rmap_desc_cache) |
| 3321 | goto nomem; |
| 3322 | |
Avi Kivity | d3d25b0 | 2007-05-30 12:34:53 +0300 | [diff] [blame] | 3323 | mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header", |
| 3324 | sizeof(struct kvm_mmu_page), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 3325 | 0, 0, NULL); |
Avi Kivity | d3d25b0 | 2007-05-30 12:34:53 +0300 | [diff] [blame] | 3326 | if (!mmu_page_header_cache) |
| 3327 | goto nomem; |
| 3328 | |
Wei Yongjun | 45bf21a | 2010-08-23 16:13:15 +0800 | [diff] [blame] | 3329 | if (percpu_counter_init(&kvm_total_used_mmu_pages, 0)) |
| 3330 | goto nomem; |
| 3331 | |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 3332 | register_shrinker(&mmu_shrinker); |
| 3333 | |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 3334 | return 0; |
| 3335 | |
| 3336 | nomem: |
Izik Eidus | 3ee16c8 | 2008-03-30 15:17:21 +0300 | [diff] [blame] | 3337 | mmu_destroy_caches(); |
Avi Kivity | b5a33a7 | 2007-04-15 16:31:09 +0300 | [diff] [blame] | 3338 | return -ENOMEM; |
| 3339 | } |
| 3340 | |
Zhang Xiantao | 3ad82a7 | 2007-11-20 13:11:38 +0800 | [diff] [blame] | 3341 | /* |
| 3342 | * Caculate mmu pages needed for kvm. |
| 3343 | */ |
| 3344 | unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm) |
| 3345 | { |
| 3346 | int i; |
| 3347 | unsigned int nr_mmu_pages; |
| 3348 | unsigned int nr_pages = 0; |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 3349 | struct kvm_memslots *slots; |
Zhang Xiantao | 3ad82a7 | 2007-11-20 13:11:38 +0800 | [diff] [blame] | 3350 | |
Lai Jiangshan | 90d83dc | 2010-04-19 17:41:23 +0800 | [diff] [blame] | 3351 | slots = kvm_memslots(kvm); |
| 3352 | |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 3353 | for (i = 0; i < slots->nmemslots; i++) |
| 3354 | nr_pages += slots->memslots[i].npages; |
Zhang Xiantao | 3ad82a7 | 2007-11-20 13:11:38 +0800 | [diff] [blame] | 3355 | |
| 3356 | nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000; |
| 3357 | nr_mmu_pages = max(nr_mmu_pages, |
| 3358 | (unsigned int) KVM_MIN_ALLOC_MMU_PAGES); |
| 3359 | |
| 3360 | return nr_mmu_pages; |
| 3361 | } |
| 3362 | |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 3363 | static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer, |
| 3364 | unsigned len) |
| 3365 | { |
| 3366 | if (len > buffer->len) |
| 3367 | return NULL; |
| 3368 | return buffer->ptr; |
| 3369 | } |
| 3370 | |
| 3371 | static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer, |
| 3372 | unsigned len) |
| 3373 | { |
| 3374 | void *ret; |
| 3375 | |
| 3376 | ret = pv_mmu_peek_buffer(buffer, len); |
| 3377 | if (!ret) |
| 3378 | return ret; |
| 3379 | buffer->ptr += len; |
| 3380 | buffer->len -= len; |
| 3381 | buffer->processed += len; |
| 3382 | return ret; |
| 3383 | } |
| 3384 | |
| 3385 | static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu, |
| 3386 | gpa_t addr, gpa_t value) |
| 3387 | { |
| 3388 | int bytes = 8; |
| 3389 | int r; |
| 3390 | |
| 3391 | if (!is_long_mode(vcpu) && !is_pae(vcpu)) |
| 3392 | bytes = 4; |
| 3393 | |
| 3394 | r = mmu_topup_memory_caches(vcpu); |
| 3395 | if (r) |
| 3396 | return r; |
| 3397 | |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 3398 | if (!emulator_write_phys(vcpu, addr, &value, bytes)) |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 3399 | return -EFAULT; |
| 3400 | |
| 3401 | return 1; |
| 3402 | } |
| 3403 | |
| 3404 | static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu) |
| 3405 | { |
Avi Kivity | 2390218 | 2010-06-10 17:02:16 +0300 | [diff] [blame] | 3406 | (void)kvm_set_cr3(vcpu, vcpu->arch.cr3); |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 3407 | return 1; |
| 3408 | } |
| 3409 | |
| 3410 | static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr) |
| 3411 | { |
| 3412 | spin_lock(&vcpu->kvm->mmu_lock); |
| 3413 | mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT); |
| 3414 | spin_unlock(&vcpu->kvm->mmu_lock); |
| 3415 | return 1; |
| 3416 | } |
| 3417 | |
| 3418 | static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu, |
| 3419 | struct kvm_pv_mmu_op_buffer *buffer) |
| 3420 | { |
| 3421 | struct kvm_mmu_op_header *header; |
| 3422 | |
| 3423 | header = pv_mmu_peek_buffer(buffer, sizeof *header); |
| 3424 | if (!header) |
| 3425 | return 0; |
| 3426 | switch (header->op) { |
| 3427 | case KVM_MMU_OP_WRITE_PTE: { |
| 3428 | struct kvm_mmu_op_write_pte *wpte; |
| 3429 | |
| 3430 | wpte = pv_mmu_read_buffer(buffer, sizeof *wpte); |
| 3431 | if (!wpte) |
| 3432 | return 0; |
| 3433 | return kvm_pv_mmu_write(vcpu, wpte->pte_phys, |
| 3434 | wpte->pte_val); |
| 3435 | } |
| 3436 | case KVM_MMU_OP_FLUSH_TLB: { |
| 3437 | struct kvm_mmu_op_flush_tlb *ftlb; |
| 3438 | |
| 3439 | ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb); |
| 3440 | if (!ftlb) |
| 3441 | return 0; |
| 3442 | return kvm_pv_mmu_flush_tlb(vcpu); |
| 3443 | } |
| 3444 | case KVM_MMU_OP_RELEASE_PT: { |
| 3445 | struct kvm_mmu_op_release_pt *rpt; |
| 3446 | |
| 3447 | rpt = pv_mmu_read_buffer(buffer, sizeof *rpt); |
| 3448 | if (!rpt) |
| 3449 | return 0; |
| 3450 | return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys); |
| 3451 | } |
| 3452 | default: return 0; |
| 3453 | } |
| 3454 | } |
| 3455 | |
| 3456 | int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes, |
| 3457 | gpa_t addr, unsigned long *ret) |
| 3458 | { |
| 3459 | int r; |
Dave Hansen | 6ad18fb | 2008-08-11 10:01:49 -0700 | [diff] [blame] | 3460 | struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer; |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 3461 | |
Dave Hansen | 6ad18fb | 2008-08-11 10:01:49 -0700 | [diff] [blame] | 3462 | buffer->ptr = buffer->buf; |
| 3463 | buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf); |
| 3464 | buffer->processed = 0; |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 3465 | |
Dave Hansen | 6ad18fb | 2008-08-11 10:01:49 -0700 | [diff] [blame] | 3466 | r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len); |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 3467 | if (r) |
| 3468 | goto out; |
| 3469 | |
Dave Hansen | 6ad18fb | 2008-08-11 10:01:49 -0700 | [diff] [blame] | 3470 | while (buffer->len) { |
| 3471 | r = kvm_pv_mmu_op_one(vcpu, buffer); |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 3472 | if (r < 0) |
| 3473 | goto out; |
| 3474 | if (r == 0) |
| 3475 | break; |
| 3476 | } |
| 3477 | |
| 3478 | r = 1; |
| 3479 | out: |
Dave Hansen | 6ad18fb | 2008-08-11 10:01:49 -0700 | [diff] [blame] | 3480 | *ret = buffer->processed; |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 3481 | return r; |
| 3482 | } |
| 3483 | |
Marcelo Tosatti | 94d8b05 | 2009-06-11 12:07:42 -0300 | [diff] [blame] | 3484 | int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4]) |
| 3485 | { |
| 3486 | struct kvm_shadow_walk_iterator iterator; |
| 3487 | int nr_sptes = 0; |
| 3488 | |
| 3489 | spin_lock(&vcpu->kvm->mmu_lock); |
| 3490 | for_each_shadow_entry(vcpu, addr, iterator) { |
| 3491 | sptes[iterator.level-1] = *iterator.sptep; |
| 3492 | nr_sptes++; |
| 3493 | if (!is_shadow_present_pte(*iterator.sptep)) |
| 3494 | break; |
| 3495 | } |
| 3496 | spin_unlock(&vcpu->kvm->mmu_lock); |
| 3497 | |
| 3498 | return nr_sptes; |
| 3499 | } |
| 3500 | EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy); |
| 3501 | |
Xiao Guangrong | 8b1fe17 | 2010-08-30 18:22:53 +0800 | [diff] [blame] | 3502 | #ifdef CONFIG_KVM_MMU_AUDIT |
Xiao Guangrong | 2f4f337 | 2010-08-30 18:24:10 +0800 | [diff] [blame] | 3503 | #include "mmu_audit.c" |
Avi Kivity | 37a7d8b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 3504 | #endif |