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