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