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