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. |
| 10 | * |
| 11 | * Authors: |
| 12 | * Yaniv Kamay <yaniv@qumranet.com> |
| 13 | * Avi Kivity <avi@qumranet.com> |
| 14 | * |
| 15 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 16 | * the COPYING file in the top-level directory. |
| 17 | * |
| 18 | */ |
| 19 | #include <linux/types.h> |
| 20 | #include <linux/string.h> |
| 21 | #include <asm/page.h> |
| 22 | #include <linux/mm.h> |
| 23 | #include <linux/highmem.h> |
| 24 | #include <linux/module.h> |
| 25 | |
| 26 | #include "vmx.h" |
| 27 | #include "kvm.h" |
| 28 | |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 29 | #define pgprintk(x...) do { printk(x); } while (0) |
| 30 | #define rmap_printk(x...) do { printk(x); } while (0) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 31 | |
| 32 | #define ASSERT(x) \ |
| 33 | if (!(x)) { \ |
| 34 | printk(KERN_WARNING "assertion failed %s:%d: %s\n", \ |
| 35 | __FILE__, __LINE__, #x); \ |
| 36 | } |
| 37 | |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 38 | #define PT64_PT_BITS 9 |
| 39 | #define PT64_ENT_PER_PAGE (1 << PT64_PT_BITS) |
| 40 | #define PT32_PT_BITS 10 |
| 41 | #define PT32_ENT_PER_PAGE (1 << PT32_PT_BITS) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 42 | |
| 43 | #define PT_WRITABLE_SHIFT 1 |
| 44 | |
| 45 | #define PT_PRESENT_MASK (1ULL << 0) |
| 46 | #define PT_WRITABLE_MASK (1ULL << PT_WRITABLE_SHIFT) |
| 47 | #define PT_USER_MASK (1ULL << 2) |
| 48 | #define PT_PWT_MASK (1ULL << 3) |
| 49 | #define PT_PCD_MASK (1ULL << 4) |
| 50 | #define PT_ACCESSED_MASK (1ULL << 5) |
| 51 | #define PT_DIRTY_MASK (1ULL << 6) |
| 52 | #define PT_PAGE_SIZE_MASK (1ULL << 7) |
| 53 | #define PT_PAT_MASK (1ULL << 7) |
| 54 | #define PT_GLOBAL_MASK (1ULL << 8) |
| 55 | #define PT64_NX_MASK (1ULL << 63) |
| 56 | |
| 57 | #define PT_PAT_SHIFT 7 |
| 58 | #define PT_DIR_PAT_SHIFT 12 |
| 59 | #define PT_DIR_PAT_MASK (1ULL << PT_DIR_PAT_SHIFT) |
| 60 | |
| 61 | #define PT32_DIR_PSE36_SIZE 4 |
| 62 | #define PT32_DIR_PSE36_SHIFT 13 |
| 63 | #define PT32_DIR_PSE36_MASK (((1ULL << PT32_DIR_PSE36_SIZE) - 1) << PT32_DIR_PSE36_SHIFT) |
| 64 | |
| 65 | |
| 66 | #define PT32_PTE_COPY_MASK \ |
Avi Kivity | 8c7bb72 | 2006-12-13 00:34:02 -0800 | [diff] [blame] | 67 | (PT_PRESENT_MASK | PT_ACCESSED_MASK | PT_DIRTY_MASK | PT_GLOBAL_MASK) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 68 | |
Avi Kivity | 8c7bb72 | 2006-12-13 00:34:02 -0800 | [diff] [blame] | 69 | #define PT64_PTE_COPY_MASK (PT64_NX_MASK | PT32_PTE_COPY_MASK) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 70 | |
| 71 | #define PT_FIRST_AVAIL_BITS_SHIFT 9 |
| 72 | #define PT64_SECOND_AVAIL_BITS_SHIFT 52 |
| 73 | |
| 74 | #define PT_SHADOW_PS_MARK (1ULL << PT_FIRST_AVAIL_BITS_SHIFT) |
| 75 | #define PT_SHADOW_IO_MARK (1ULL << PT_FIRST_AVAIL_BITS_SHIFT) |
| 76 | |
| 77 | #define PT_SHADOW_WRITABLE_SHIFT (PT_FIRST_AVAIL_BITS_SHIFT + 1) |
| 78 | #define PT_SHADOW_WRITABLE_MASK (1ULL << PT_SHADOW_WRITABLE_SHIFT) |
| 79 | |
| 80 | #define PT_SHADOW_USER_SHIFT (PT_SHADOW_WRITABLE_SHIFT + 1) |
| 81 | #define PT_SHADOW_USER_MASK (1ULL << (PT_SHADOW_USER_SHIFT)) |
| 82 | |
| 83 | #define PT_SHADOW_BITS_OFFSET (PT_SHADOW_WRITABLE_SHIFT - PT_WRITABLE_SHIFT) |
| 84 | |
| 85 | #define VALID_PAGE(x) ((x) != INVALID_PAGE) |
| 86 | |
| 87 | #define PT64_LEVEL_BITS 9 |
| 88 | |
| 89 | #define PT64_LEVEL_SHIFT(level) \ |
| 90 | ( PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS ) |
| 91 | |
| 92 | #define PT64_LEVEL_MASK(level) \ |
| 93 | (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level)) |
| 94 | |
| 95 | #define PT64_INDEX(address, level)\ |
| 96 | (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1)) |
| 97 | |
| 98 | |
| 99 | #define PT32_LEVEL_BITS 10 |
| 100 | |
| 101 | #define PT32_LEVEL_SHIFT(level) \ |
| 102 | ( PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS ) |
| 103 | |
| 104 | #define PT32_LEVEL_MASK(level) \ |
| 105 | (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level)) |
| 106 | |
| 107 | #define PT32_INDEX(address, level)\ |
| 108 | (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1)) |
| 109 | |
| 110 | |
| 111 | #define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & PAGE_MASK) |
| 112 | #define PT64_DIR_BASE_ADDR_MASK \ |
| 113 | (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1)) |
| 114 | |
| 115 | #define PT32_BASE_ADDR_MASK PAGE_MASK |
| 116 | #define PT32_DIR_BASE_ADDR_MASK \ |
| 117 | (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1)) |
| 118 | |
| 119 | |
| 120 | #define PFERR_PRESENT_MASK (1U << 0) |
| 121 | #define PFERR_WRITE_MASK (1U << 1) |
| 122 | #define PFERR_USER_MASK (1U << 2) |
| 123 | |
| 124 | #define PT64_ROOT_LEVEL 4 |
| 125 | #define PT32_ROOT_LEVEL 2 |
| 126 | #define PT32E_ROOT_LEVEL 3 |
| 127 | |
| 128 | #define PT_DIRECTORY_LEVEL 2 |
| 129 | #define PT_PAGE_TABLE_LEVEL 1 |
| 130 | |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 131 | #define RMAP_EXT 4 |
| 132 | |
| 133 | struct kvm_rmap_desc { |
| 134 | u64 *shadow_ptes[RMAP_EXT]; |
| 135 | struct kvm_rmap_desc *more; |
| 136 | }; |
| 137 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 138 | static int is_write_protection(struct kvm_vcpu *vcpu) |
| 139 | { |
| 140 | return vcpu->cr0 & CR0_WP_MASK; |
| 141 | } |
| 142 | |
| 143 | static int is_cpuid_PSE36(void) |
| 144 | { |
| 145 | return 1; |
| 146 | } |
| 147 | |
| 148 | static int is_present_pte(unsigned long pte) |
| 149 | { |
| 150 | return pte & PT_PRESENT_MASK; |
| 151 | } |
| 152 | |
| 153 | static int is_writeble_pte(unsigned long pte) |
| 154 | { |
| 155 | return pte & PT_WRITABLE_MASK; |
| 156 | } |
| 157 | |
| 158 | static int is_io_pte(unsigned long pte) |
| 159 | { |
| 160 | return pte & PT_SHADOW_IO_MARK; |
| 161 | } |
| 162 | |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 163 | static int is_rmap_pte(u64 pte) |
| 164 | { |
| 165 | return (pte & (PT_WRITABLE_MASK | PT_PRESENT_MASK)) |
| 166 | == (PT_WRITABLE_MASK | PT_PRESENT_MASK); |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * Reverse mapping data structures: |
| 171 | * |
| 172 | * If page->private bit zero is zero, then page->private points to the |
| 173 | * shadow page table entry that points to page_address(page). |
| 174 | * |
| 175 | * If page->private bit zero is one, (then page->private & ~1) points |
| 176 | * to a struct kvm_rmap_desc containing more mappings. |
| 177 | */ |
| 178 | static void rmap_add(struct kvm *kvm, u64 *spte) |
| 179 | { |
| 180 | struct page *page; |
| 181 | struct kvm_rmap_desc *desc; |
| 182 | int i; |
| 183 | |
| 184 | if (!is_rmap_pte(*spte)) |
| 185 | return; |
| 186 | page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT); |
| 187 | if (!page->private) { |
| 188 | rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte); |
| 189 | page->private = (unsigned long)spte; |
| 190 | } else if (!(page->private & 1)) { |
| 191 | rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte); |
| 192 | desc = kzalloc(sizeof *desc, GFP_NOWAIT); |
| 193 | if (!desc) |
| 194 | BUG(); /* FIXME: return error */ |
| 195 | desc->shadow_ptes[0] = (u64 *)page->private; |
| 196 | desc->shadow_ptes[1] = spte; |
| 197 | page->private = (unsigned long)desc | 1; |
| 198 | } else { |
| 199 | rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte); |
| 200 | desc = (struct kvm_rmap_desc *)(page->private & ~1ul); |
| 201 | while (desc->shadow_ptes[RMAP_EXT-1] && desc->more) |
| 202 | desc = desc->more; |
| 203 | if (desc->shadow_ptes[RMAP_EXT-1]) { |
| 204 | desc->more = kzalloc(sizeof *desc->more, GFP_NOWAIT); |
| 205 | if (!desc->more) |
| 206 | BUG(); /* FIXME: return error */ |
| 207 | desc = desc->more; |
| 208 | } |
| 209 | for (i = 0; desc->shadow_ptes[i]; ++i) |
| 210 | ; |
| 211 | desc->shadow_ptes[i] = spte; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | static void rmap_desc_remove_entry(struct page *page, |
| 216 | struct kvm_rmap_desc *desc, |
| 217 | int i, |
| 218 | struct kvm_rmap_desc *prev_desc) |
| 219 | { |
| 220 | int j; |
| 221 | |
| 222 | for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j) |
| 223 | ; |
| 224 | desc->shadow_ptes[i] = desc->shadow_ptes[j]; |
| 225 | desc->shadow_ptes[j] = 0; |
| 226 | if (j != 0) |
| 227 | return; |
| 228 | if (!prev_desc && !desc->more) |
| 229 | page->private = (unsigned long)desc->shadow_ptes[0]; |
| 230 | else |
| 231 | if (prev_desc) |
| 232 | prev_desc->more = desc->more; |
| 233 | else |
| 234 | page->private = (unsigned long)desc->more | 1; |
| 235 | kfree(desc); |
| 236 | } |
| 237 | |
| 238 | static void rmap_remove(struct kvm *kvm, u64 *spte) |
| 239 | { |
| 240 | struct page *page; |
| 241 | struct kvm_rmap_desc *desc; |
| 242 | struct kvm_rmap_desc *prev_desc; |
| 243 | int i; |
| 244 | |
| 245 | if (!is_rmap_pte(*spte)) |
| 246 | return; |
| 247 | page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT); |
| 248 | if (!page->private) { |
| 249 | printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte); |
| 250 | BUG(); |
| 251 | } else if (!(page->private & 1)) { |
| 252 | rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte); |
| 253 | if ((u64 *)page->private != spte) { |
| 254 | printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n", |
| 255 | spte, *spte); |
| 256 | BUG(); |
| 257 | } |
| 258 | page->private = 0; |
| 259 | } else { |
| 260 | rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte); |
| 261 | desc = (struct kvm_rmap_desc *)(page->private & ~1ul); |
| 262 | prev_desc = NULL; |
| 263 | while (desc) { |
| 264 | for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) |
| 265 | if (desc->shadow_ptes[i] == spte) { |
| 266 | rmap_desc_remove_entry(page, desc, i, |
| 267 | prev_desc); |
| 268 | return; |
| 269 | } |
| 270 | prev_desc = desc; |
| 271 | desc = desc->more; |
| 272 | } |
| 273 | BUG(); |
| 274 | } |
| 275 | } |
| 276 | |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 277 | static void rmap_write_protect(struct kvm *kvm, u64 gfn) |
| 278 | { |
| 279 | struct page *page; |
| 280 | struct kvm_memory_slot *slot; |
| 281 | struct kvm_rmap_desc *desc; |
| 282 | u64 *spte; |
| 283 | |
| 284 | slot = gfn_to_memslot(kvm, gfn); |
| 285 | BUG_ON(!slot); |
| 286 | page = gfn_to_page(slot, gfn); |
| 287 | |
| 288 | while (page->private) { |
| 289 | if (!(page->private & 1)) |
| 290 | spte = (u64 *)page->private; |
| 291 | else { |
| 292 | desc = (struct kvm_rmap_desc *)(page->private & ~1ul); |
| 293 | spte = desc->shadow_ptes[0]; |
| 294 | } |
| 295 | BUG_ON(!spte); |
| 296 | BUG_ON((*spte & PT64_BASE_ADDR_MASK) != |
| 297 | page_to_pfn(page) << PAGE_SHIFT); |
| 298 | BUG_ON(!(*spte & PT_PRESENT_MASK)); |
| 299 | BUG_ON(!(*spte & PT_WRITABLE_MASK)); |
| 300 | rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte); |
| 301 | rmap_remove(kvm, spte); |
| 302 | *spte &= ~(u64)PT_WRITABLE_MASK; |
| 303 | } |
| 304 | } |
| 305 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 306 | static int is_empty_shadow_page(hpa_t page_hpa) |
| 307 | { |
| 308 | u32 *pos; |
| 309 | u32 *end; |
| 310 | for (pos = __va(page_hpa), end = pos + PAGE_SIZE / sizeof(u32); |
| 311 | pos != end; pos++) |
| 312 | if (*pos != 0) |
| 313 | return 0; |
| 314 | return 1; |
| 315 | } |
| 316 | |
Avi Kivity | 260746c | 2007-01-05 16:36:49 -0800 | [diff] [blame] | 317 | static void kvm_mmu_free_page(struct kvm_vcpu *vcpu, hpa_t page_hpa) |
| 318 | { |
| 319 | struct kvm_mmu_page *page_head = page_header(page_hpa); |
| 320 | |
Avi Kivity | 5f1e0b6 | 2007-01-05 16:36:49 -0800 | [diff] [blame^] | 321 | ASSERT(is_empty_shadow_page(page_hpa)); |
Avi Kivity | 260746c | 2007-01-05 16:36:49 -0800 | [diff] [blame] | 322 | list_del(&page_head->link); |
| 323 | page_head->page_hpa = page_hpa; |
| 324 | list_add(&page_head->link, &vcpu->free_pages); |
| 325 | ++vcpu->kvm->n_free_mmu_pages; |
| 326 | } |
| 327 | |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 328 | static unsigned kvm_page_table_hashfn(gfn_t gfn) |
| 329 | { |
| 330 | return gfn; |
| 331 | } |
| 332 | |
Avi Kivity | 25c0de2 | 2007-01-05 16:36:42 -0800 | [diff] [blame] | 333 | static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu, |
| 334 | u64 *parent_pte) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 335 | { |
| 336 | struct kvm_mmu_page *page; |
| 337 | |
| 338 | if (list_empty(&vcpu->free_pages)) |
Avi Kivity | 25c0de2 | 2007-01-05 16:36:42 -0800 | [diff] [blame] | 339 | return NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 340 | |
| 341 | page = list_entry(vcpu->free_pages.next, struct kvm_mmu_page, link); |
| 342 | list_del(&page->link); |
| 343 | list_add(&page->link, &vcpu->kvm->active_mmu_pages); |
| 344 | ASSERT(is_empty_shadow_page(page->page_hpa)); |
| 345 | page->slot_bitmap = 0; |
| 346 | page->global = 1; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 347 | page->multimapped = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 348 | page->parent_pte = parent_pte; |
Avi Kivity | ebeace8 | 2007-01-05 16:36:47 -0800 | [diff] [blame] | 349 | --vcpu->kvm->n_free_mmu_pages; |
Avi Kivity | 25c0de2 | 2007-01-05 16:36:42 -0800 | [diff] [blame] | 350 | return page; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 351 | } |
| 352 | |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 353 | static void mmu_page_add_parent_pte(struct kvm_mmu_page *page, u64 *parent_pte) |
| 354 | { |
| 355 | struct kvm_pte_chain *pte_chain; |
| 356 | struct hlist_node *node; |
| 357 | int i; |
| 358 | |
| 359 | if (!parent_pte) |
| 360 | return; |
| 361 | if (!page->multimapped) { |
| 362 | u64 *old = page->parent_pte; |
| 363 | |
| 364 | if (!old) { |
| 365 | page->parent_pte = parent_pte; |
| 366 | return; |
| 367 | } |
| 368 | page->multimapped = 1; |
| 369 | pte_chain = kzalloc(sizeof(struct kvm_pte_chain), GFP_NOWAIT); |
| 370 | BUG_ON(!pte_chain); |
| 371 | INIT_HLIST_HEAD(&page->parent_ptes); |
| 372 | hlist_add_head(&pte_chain->link, &page->parent_ptes); |
| 373 | pte_chain->parent_ptes[0] = old; |
| 374 | } |
| 375 | hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link) { |
| 376 | if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1]) |
| 377 | continue; |
| 378 | for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) |
| 379 | if (!pte_chain->parent_ptes[i]) { |
| 380 | pte_chain->parent_ptes[i] = parent_pte; |
| 381 | return; |
| 382 | } |
| 383 | } |
| 384 | pte_chain = kzalloc(sizeof(struct kvm_pte_chain), GFP_NOWAIT); |
| 385 | BUG_ON(!pte_chain); |
| 386 | hlist_add_head(&pte_chain->link, &page->parent_ptes); |
| 387 | pte_chain->parent_ptes[0] = parent_pte; |
| 388 | } |
| 389 | |
| 390 | static void mmu_page_remove_parent_pte(struct kvm_mmu_page *page, |
| 391 | u64 *parent_pte) |
| 392 | { |
| 393 | struct kvm_pte_chain *pte_chain; |
| 394 | struct hlist_node *node; |
| 395 | int i; |
| 396 | |
| 397 | if (!page->multimapped) { |
| 398 | BUG_ON(page->parent_pte != parent_pte); |
| 399 | page->parent_pte = NULL; |
| 400 | return; |
| 401 | } |
| 402 | hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link) |
| 403 | for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) { |
| 404 | if (!pte_chain->parent_ptes[i]) |
| 405 | break; |
| 406 | if (pte_chain->parent_ptes[i] != parent_pte) |
| 407 | continue; |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 408 | while (i + 1 < NR_PTE_CHAIN_ENTRIES |
| 409 | && pte_chain->parent_ptes[i + 1]) { |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 410 | pte_chain->parent_ptes[i] |
| 411 | = pte_chain->parent_ptes[i + 1]; |
| 412 | ++i; |
| 413 | } |
| 414 | pte_chain->parent_ptes[i] = NULL; |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 415 | if (i == 0) { |
| 416 | hlist_del(&pte_chain->link); |
| 417 | kfree(pte_chain); |
| 418 | if (hlist_empty(&page->parent_ptes)) { |
| 419 | page->multimapped = 0; |
| 420 | page->parent_pte = NULL; |
| 421 | } |
| 422 | } |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 423 | return; |
| 424 | } |
| 425 | BUG(); |
| 426 | } |
| 427 | |
| 428 | static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm_vcpu *vcpu, |
| 429 | gfn_t gfn) |
| 430 | { |
| 431 | unsigned index; |
| 432 | struct hlist_head *bucket; |
| 433 | struct kvm_mmu_page *page; |
| 434 | struct hlist_node *node; |
| 435 | |
| 436 | pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn); |
| 437 | index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES; |
| 438 | bucket = &vcpu->kvm->mmu_page_hash[index]; |
| 439 | hlist_for_each_entry(page, node, bucket, hash_link) |
| 440 | if (page->gfn == gfn && !page->role.metaphysical) { |
| 441 | pgprintk("%s: found role %x\n", |
| 442 | __FUNCTION__, page->role.word); |
| 443 | return page; |
| 444 | } |
| 445 | return NULL; |
| 446 | } |
| 447 | |
| 448 | static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu, |
| 449 | gfn_t gfn, |
| 450 | gva_t gaddr, |
| 451 | unsigned level, |
| 452 | int metaphysical, |
| 453 | u64 *parent_pte) |
| 454 | { |
| 455 | union kvm_mmu_page_role role; |
| 456 | unsigned index; |
| 457 | unsigned quadrant; |
| 458 | struct hlist_head *bucket; |
| 459 | struct kvm_mmu_page *page; |
| 460 | struct hlist_node *node; |
| 461 | |
| 462 | role.word = 0; |
| 463 | role.glevels = vcpu->mmu.root_level; |
| 464 | role.level = level; |
| 465 | role.metaphysical = metaphysical; |
| 466 | if (vcpu->mmu.root_level <= PT32_ROOT_LEVEL) { |
| 467 | quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level)); |
| 468 | quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1; |
| 469 | role.quadrant = quadrant; |
| 470 | } |
| 471 | pgprintk("%s: looking gfn %lx role %x\n", __FUNCTION__, |
| 472 | gfn, role.word); |
| 473 | index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES; |
| 474 | bucket = &vcpu->kvm->mmu_page_hash[index]; |
| 475 | hlist_for_each_entry(page, node, bucket, hash_link) |
| 476 | if (page->gfn == gfn && page->role.word == role.word) { |
| 477 | mmu_page_add_parent_pte(page, parent_pte); |
| 478 | pgprintk("%s: found\n", __FUNCTION__); |
| 479 | return page; |
| 480 | } |
| 481 | page = kvm_mmu_alloc_page(vcpu, parent_pte); |
| 482 | if (!page) |
| 483 | return page; |
| 484 | pgprintk("%s: adding gfn %lx role %x\n", __FUNCTION__, gfn, role.word); |
| 485 | page->gfn = gfn; |
| 486 | page->role = role; |
| 487 | hlist_add_head(&page->hash_link, bucket); |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 488 | if (!metaphysical) |
| 489 | rmap_write_protect(vcpu->kvm, gfn); |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 490 | return page; |
| 491 | } |
| 492 | |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 493 | static void kvm_mmu_page_unlink_children(struct kvm_vcpu *vcpu, |
| 494 | struct kvm_mmu_page *page) |
| 495 | { |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 496 | unsigned i; |
| 497 | u64 *pt; |
| 498 | u64 ent; |
| 499 | |
| 500 | pt = __va(page->page_hpa); |
| 501 | |
| 502 | if (page->role.level == PT_PAGE_TABLE_LEVEL) { |
| 503 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) { |
| 504 | if (pt[i] & PT_PRESENT_MASK) |
| 505 | rmap_remove(vcpu->kvm, &pt[i]); |
| 506 | pt[i] = 0; |
| 507 | } |
| 508 | return; |
| 509 | } |
| 510 | |
| 511 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) { |
| 512 | ent = pt[i]; |
| 513 | |
| 514 | pt[i] = 0; |
| 515 | if (!(ent & PT_PRESENT_MASK)) |
| 516 | continue; |
| 517 | ent &= PT64_BASE_ADDR_MASK; |
| 518 | mmu_page_remove_parent_pte(page_header(ent), &pt[i]); |
| 519 | } |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 520 | } |
| 521 | |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 522 | static void kvm_mmu_put_page(struct kvm_vcpu *vcpu, |
| 523 | struct kvm_mmu_page *page, |
| 524 | u64 *parent_pte) |
| 525 | { |
| 526 | mmu_page_remove_parent_pte(page, parent_pte); |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | static void kvm_mmu_zap_page(struct kvm_vcpu *vcpu, |
| 530 | struct kvm_mmu_page *page) |
| 531 | { |
| 532 | u64 *parent_pte; |
| 533 | |
| 534 | while (page->multimapped || page->parent_pte) { |
| 535 | if (!page->multimapped) |
| 536 | parent_pte = page->parent_pte; |
| 537 | else { |
| 538 | struct kvm_pte_chain *chain; |
| 539 | |
| 540 | chain = container_of(page->parent_ptes.first, |
| 541 | struct kvm_pte_chain, link); |
| 542 | parent_pte = chain->parent_ptes[0]; |
| 543 | } |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 544 | BUG_ON(!parent_pte); |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 545 | kvm_mmu_put_page(vcpu, page, parent_pte); |
| 546 | *parent_pte = 0; |
| 547 | } |
Avi Kivity | cc4529e | 2007-01-05 16:36:47 -0800 | [diff] [blame] | 548 | kvm_mmu_page_unlink_children(vcpu, page); |
| 549 | hlist_del(&page->hash_link); |
Avi Kivity | ebeace8 | 2007-01-05 16:36:47 -0800 | [diff] [blame] | 550 | kvm_mmu_free_page(vcpu, page->page_hpa); |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | static int kvm_mmu_unprotect_page(struct kvm_vcpu *vcpu, gfn_t gfn) |
| 554 | { |
| 555 | unsigned index; |
| 556 | struct hlist_head *bucket; |
| 557 | struct kvm_mmu_page *page; |
| 558 | struct hlist_node *node, *n; |
| 559 | int r; |
| 560 | |
| 561 | pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn); |
| 562 | r = 0; |
| 563 | index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES; |
| 564 | bucket = &vcpu->kvm->mmu_page_hash[index]; |
| 565 | hlist_for_each_entry_safe(page, node, n, bucket, hash_link) |
| 566 | if (page->gfn == gfn && !page->role.metaphysical) { |
Avi Kivity | 697fe2e | 2007-01-05 16:36:46 -0800 | [diff] [blame] | 567 | pgprintk("%s: gfn %lx role %x\n", __FUNCTION__, gfn, |
| 568 | page->role.word); |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 569 | kvm_mmu_zap_page(vcpu, page); |
| 570 | r = 1; |
| 571 | } |
| 572 | return r; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 573 | } |
| 574 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 575 | static void page_header_update_slot(struct kvm *kvm, void *pte, gpa_t gpa) |
| 576 | { |
| 577 | int slot = memslot_id(kvm, gfn_to_memslot(kvm, gpa >> PAGE_SHIFT)); |
| 578 | struct kvm_mmu_page *page_head = page_header(__pa(pte)); |
| 579 | |
| 580 | __set_bit(slot, &page_head->slot_bitmap); |
| 581 | } |
| 582 | |
| 583 | hpa_t safe_gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa) |
| 584 | { |
| 585 | hpa_t hpa = gpa_to_hpa(vcpu, gpa); |
| 586 | |
| 587 | return is_error_hpa(hpa) ? bad_page_address | (gpa & ~PAGE_MASK): hpa; |
| 588 | } |
| 589 | |
| 590 | hpa_t gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa) |
| 591 | { |
| 592 | struct kvm_memory_slot *slot; |
| 593 | struct page *page; |
| 594 | |
| 595 | ASSERT((gpa & HPA_ERR_MASK) == 0); |
| 596 | slot = gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT); |
| 597 | if (!slot) |
| 598 | return gpa | HPA_ERR_MASK; |
| 599 | page = gfn_to_page(slot, gpa >> PAGE_SHIFT); |
| 600 | return ((hpa_t)page_to_pfn(page) << PAGE_SHIFT) |
| 601 | | (gpa & (PAGE_SIZE-1)); |
| 602 | } |
| 603 | |
| 604 | hpa_t gva_to_hpa(struct kvm_vcpu *vcpu, gva_t gva) |
| 605 | { |
| 606 | gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva); |
| 607 | |
| 608 | if (gpa == UNMAPPED_GVA) |
| 609 | return UNMAPPED_GVA; |
| 610 | return gpa_to_hpa(vcpu, gpa); |
| 611 | } |
| 612 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 613 | static void nonpaging_new_cr3(struct kvm_vcpu *vcpu) |
| 614 | { |
| 615 | } |
| 616 | |
| 617 | static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, hpa_t p) |
| 618 | { |
| 619 | int level = PT32E_ROOT_LEVEL; |
| 620 | hpa_t table_addr = vcpu->mmu.root_hpa; |
| 621 | |
| 622 | for (; ; level--) { |
| 623 | u32 index = PT64_INDEX(v, level); |
| 624 | u64 *table; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 625 | u64 pte; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 626 | |
| 627 | ASSERT(VALID_PAGE(table_addr)); |
| 628 | table = __va(table_addr); |
| 629 | |
| 630 | if (level == 1) { |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 631 | pte = table[index]; |
| 632 | if (is_present_pte(pte) && is_writeble_pte(pte)) |
| 633 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 634 | mark_page_dirty(vcpu->kvm, v >> PAGE_SHIFT); |
| 635 | page_header_update_slot(vcpu->kvm, table, v); |
| 636 | table[index] = p | PT_PRESENT_MASK | PT_WRITABLE_MASK | |
| 637 | PT_USER_MASK; |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 638 | rmap_add(vcpu->kvm, &table[index]); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 639 | return 0; |
| 640 | } |
| 641 | |
| 642 | if (table[index] == 0) { |
Avi Kivity | 25c0de2 | 2007-01-05 16:36:42 -0800 | [diff] [blame] | 643 | struct kvm_mmu_page *new_table; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 644 | gfn_t pseudo_gfn; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 645 | |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 646 | pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK) |
| 647 | >> PAGE_SHIFT; |
| 648 | new_table = kvm_mmu_get_page(vcpu, pseudo_gfn, |
| 649 | v, level - 1, |
| 650 | 1, &table[index]); |
Avi Kivity | 25c0de2 | 2007-01-05 16:36:42 -0800 | [diff] [blame] | 651 | if (!new_table) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 652 | pgprintk("nonpaging_map: ENOMEM\n"); |
| 653 | return -ENOMEM; |
| 654 | } |
| 655 | |
Avi Kivity | 25c0de2 | 2007-01-05 16:36:42 -0800 | [diff] [blame] | 656 | table[index] = new_table->page_hpa | PT_PRESENT_MASK |
| 657 | | PT_WRITABLE_MASK | PT_USER_MASK; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 658 | } |
| 659 | table_addr = table[index] & PT64_BASE_ADDR_MASK; |
| 660 | } |
| 661 | } |
| 662 | |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 663 | static void mmu_free_roots(struct kvm_vcpu *vcpu) |
| 664 | { |
| 665 | int i; |
| 666 | |
| 667 | #ifdef CONFIG_X86_64 |
| 668 | if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) { |
| 669 | hpa_t root = vcpu->mmu.root_hpa; |
| 670 | |
| 671 | ASSERT(VALID_PAGE(root)); |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 672 | vcpu->mmu.root_hpa = INVALID_PAGE; |
| 673 | return; |
| 674 | } |
| 675 | #endif |
| 676 | for (i = 0; i < 4; ++i) { |
| 677 | hpa_t root = vcpu->mmu.pae_root[i]; |
| 678 | |
| 679 | ASSERT(VALID_PAGE(root)); |
| 680 | root &= PT64_BASE_ADDR_MASK; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 681 | vcpu->mmu.pae_root[i] = INVALID_PAGE; |
| 682 | } |
| 683 | vcpu->mmu.root_hpa = INVALID_PAGE; |
| 684 | } |
| 685 | |
| 686 | static void mmu_alloc_roots(struct kvm_vcpu *vcpu) |
| 687 | { |
| 688 | int i; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 689 | gfn_t root_gfn; |
| 690 | root_gfn = vcpu->cr3 >> PAGE_SHIFT; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 691 | |
| 692 | #ifdef CONFIG_X86_64 |
| 693 | if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) { |
| 694 | hpa_t root = vcpu->mmu.root_hpa; |
| 695 | |
| 696 | ASSERT(!VALID_PAGE(root)); |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 697 | root = kvm_mmu_get_page(vcpu, root_gfn, 0, |
| 698 | PT64_ROOT_LEVEL, 0, NULL)->page_hpa; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 699 | vcpu->mmu.root_hpa = root; |
| 700 | return; |
| 701 | } |
| 702 | #endif |
| 703 | for (i = 0; i < 4; ++i) { |
| 704 | hpa_t root = vcpu->mmu.pae_root[i]; |
| 705 | |
| 706 | ASSERT(!VALID_PAGE(root)); |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 707 | if (vcpu->mmu.root_level == PT32E_ROOT_LEVEL) |
| 708 | root_gfn = vcpu->pdptrs[i] >> PAGE_SHIFT; |
| 709 | else if (vcpu->mmu.root_level == 0) |
| 710 | root_gfn = 0; |
| 711 | root = kvm_mmu_get_page(vcpu, root_gfn, i << 30, |
| 712 | PT32_ROOT_LEVEL, !is_paging(vcpu), |
| 713 | NULL)->page_hpa; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 714 | vcpu->mmu.pae_root[i] = root | PT_PRESENT_MASK; |
| 715 | } |
| 716 | vcpu->mmu.root_hpa = __pa(vcpu->mmu.pae_root); |
| 717 | } |
| 718 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 719 | static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr) |
| 720 | { |
| 721 | return vaddr; |
| 722 | } |
| 723 | |
| 724 | static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva, |
| 725 | u32 error_code) |
| 726 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 727 | gpa_t addr = gva; |
Avi Kivity | ebeace8 | 2007-01-05 16:36:47 -0800 | [diff] [blame] | 728 | hpa_t paddr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 729 | |
| 730 | ASSERT(vcpu); |
| 731 | ASSERT(VALID_PAGE(vcpu->mmu.root_hpa)); |
| 732 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 733 | |
Avi Kivity | ebeace8 | 2007-01-05 16:36:47 -0800 | [diff] [blame] | 734 | paddr = gpa_to_hpa(vcpu , addr & PT64_BASE_ADDR_MASK); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 735 | |
Avi Kivity | ebeace8 | 2007-01-05 16:36:47 -0800 | [diff] [blame] | 736 | if (is_error_hpa(paddr)) |
| 737 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 738 | |
Avi Kivity | ebeace8 | 2007-01-05 16:36:47 -0800 | [diff] [blame] | 739 | return nonpaging_map(vcpu, addr & PAGE_MASK, paddr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 740 | } |
| 741 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 742 | static void nonpaging_free(struct kvm_vcpu *vcpu) |
| 743 | { |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 744 | mmu_free_roots(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | static int nonpaging_init_context(struct kvm_vcpu *vcpu) |
| 748 | { |
| 749 | struct kvm_mmu *context = &vcpu->mmu; |
| 750 | |
| 751 | context->new_cr3 = nonpaging_new_cr3; |
| 752 | context->page_fault = nonpaging_page_fault; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 753 | context->gva_to_gpa = nonpaging_gva_to_gpa; |
| 754 | context->free = nonpaging_free; |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 755 | context->root_level = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 756 | context->shadow_root_level = PT32E_ROOT_LEVEL; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 757 | mmu_alloc_roots(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 758 | ASSERT(VALID_PAGE(context->root_hpa)); |
| 759 | kvm_arch_ops->set_cr3(vcpu, context->root_hpa); |
| 760 | return 0; |
| 761 | } |
| 762 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 763 | static void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu) |
| 764 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 765 | ++kvm_stat.tlb_flush; |
| 766 | kvm_arch_ops->tlb_flush(vcpu); |
| 767 | } |
| 768 | |
| 769 | static void paging_new_cr3(struct kvm_vcpu *vcpu) |
| 770 | { |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 771 | pgprintk("%s: cr3 %lx\n", __FUNCTION__, vcpu->cr3); |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 772 | mmu_free_roots(vcpu); |
| 773 | mmu_alloc_roots(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 774 | kvm_mmu_flush_tlb(vcpu); |
Avi Kivity | cea0f0e | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 775 | kvm_arch_ops->set_cr3(vcpu, vcpu->mmu.root_hpa); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | static void mark_pagetable_nonglobal(void *shadow_pte) |
| 779 | { |
| 780 | page_header(__pa(shadow_pte))->global = 0; |
| 781 | } |
| 782 | |
| 783 | static inline void set_pte_common(struct kvm_vcpu *vcpu, |
| 784 | u64 *shadow_pte, |
| 785 | gpa_t gaddr, |
| 786 | int dirty, |
Avi Kivity | 815af8d | 2007-01-05 16:36:44 -0800 | [diff] [blame] | 787 | u64 access_bits, |
| 788 | gfn_t gfn) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 789 | { |
| 790 | hpa_t paddr; |
| 791 | |
| 792 | *shadow_pte |= access_bits << PT_SHADOW_BITS_OFFSET; |
| 793 | if (!dirty) |
| 794 | access_bits &= ~PT_WRITABLE_MASK; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 795 | |
| 796 | paddr = gpa_to_hpa(vcpu, gaddr & PT64_BASE_ADDR_MASK); |
| 797 | |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 798 | *shadow_pte |= access_bits; |
| 799 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 800 | if (!(*shadow_pte & PT_GLOBAL_MASK)) |
| 801 | mark_pagetable_nonglobal(shadow_pte); |
| 802 | |
| 803 | if (is_error_hpa(paddr)) { |
| 804 | *shadow_pte |= gaddr; |
| 805 | *shadow_pte |= PT_SHADOW_IO_MARK; |
| 806 | *shadow_pte &= ~PT_PRESENT_MASK; |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 807 | return; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 808 | } |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 809 | |
| 810 | *shadow_pte |= paddr; |
| 811 | |
| 812 | if (access_bits & PT_WRITABLE_MASK) { |
| 813 | struct kvm_mmu_page *shadow; |
| 814 | |
Avi Kivity | 815af8d | 2007-01-05 16:36:44 -0800 | [diff] [blame] | 815 | shadow = kvm_mmu_lookup_page(vcpu, gfn); |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 816 | if (shadow) { |
| 817 | pgprintk("%s: found shadow page for %lx, marking ro\n", |
Avi Kivity | 815af8d | 2007-01-05 16:36:44 -0800 | [diff] [blame] | 818 | __FUNCTION__, gfn); |
Avi Kivity | 374cbac | 2007-01-05 16:36:43 -0800 | [diff] [blame] | 819 | access_bits &= ~PT_WRITABLE_MASK; |
| 820 | *shadow_pte &= ~PT_WRITABLE_MASK; |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | if (access_bits & PT_WRITABLE_MASK) |
| 825 | mark_page_dirty(vcpu->kvm, gaddr >> PAGE_SHIFT); |
| 826 | |
| 827 | page_header_update_slot(vcpu->kvm, shadow_pte, gaddr); |
| 828 | rmap_add(vcpu->kvm, shadow_pte); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | static void inject_page_fault(struct kvm_vcpu *vcpu, |
| 832 | u64 addr, |
| 833 | u32 err_code) |
| 834 | { |
| 835 | kvm_arch_ops->inject_page_fault(vcpu, addr, err_code); |
| 836 | } |
| 837 | |
| 838 | static inline int fix_read_pf(u64 *shadow_ent) |
| 839 | { |
| 840 | if ((*shadow_ent & PT_SHADOW_USER_MASK) && |
| 841 | !(*shadow_ent & PT_USER_MASK)) { |
| 842 | /* |
| 843 | * If supervisor write protect is disabled, we shadow kernel |
| 844 | * pages as user pages so we can trap the write access. |
| 845 | */ |
| 846 | *shadow_ent |= PT_USER_MASK; |
| 847 | *shadow_ent &= ~PT_WRITABLE_MASK; |
| 848 | |
| 849 | return 1; |
| 850 | |
| 851 | } |
| 852 | return 0; |
| 853 | } |
| 854 | |
| 855 | static int may_access(u64 pte, int write, int user) |
| 856 | { |
| 857 | |
| 858 | if (user && !(pte & PT_USER_MASK)) |
| 859 | return 0; |
| 860 | if (write && !(pte & PT_WRITABLE_MASK)) |
| 861 | return 0; |
| 862 | return 1; |
| 863 | } |
| 864 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 865 | static void paging_free(struct kvm_vcpu *vcpu) |
| 866 | { |
| 867 | nonpaging_free(vcpu); |
| 868 | } |
| 869 | |
| 870 | #define PTTYPE 64 |
| 871 | #include "paging_tmpl.h" |
| 872 | #undef PTTYPE |
| 873 | |
| 874 | #define PTTYPE 32 |
| 875 | #include "paging_tmpl.h" |
| 876 | #undef PTTYPE |
| 877 | |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 878 | static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 879 | { |
| 880 | struct kvm_mmu *context = &vcpu->mmu; |
| 881 | |
| 882 | ASSERT(is_pae(vcpu)); |
| 883 | context->new_cr3 = paging_new_cr3; |
| 884 | context->page_fault = paging64_page_fault; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 885 | context->gva_to_gpa = paging64_gva_to_gpa; |
| 886 | context->free = paging_free; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 887 | context->root_level = level; |
| 888 | context->shadow_root_level = level; |
| 889 | mmu_alloc_roots(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 890 | ASSERT(VALID_PAGE(context->root_hpa)); |
| 891 | kvm_arch_ops->set_cr3(vcpu, context->root_hpa | |
| 892 | (vcpu->cr3 & (CR3_PCD_MASK | CR3_WPT_MASK))); |
| 893 | return 0; |
| 894 | } |
| 895 | |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 896 | static int paging64_init_context(struct kvm_vcpu *vcpu) |
| 897 | { |
| 898 | return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL); |
| 899 | } |
| 900 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 901 | static int paging32_init_context(struct kvm_vcpu *vcpu) |
| 902 | { |
| 903 | struct kvm_mmu *context = &vcpu->mmu; |
| 904 | |
| 905 | context->new_cr3 = paging_new_cr3; |
| 906 | context->page_fault = paging32_page_fault; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 907 | context->gva_to_gpa = paging32_gva_to_gpa; |
| 908 | context->free = paging_free; |
| 909 | context->root_level = PT32_ROOT_LEVEL; |
| 910 | context->shadow_root_level = PT32E_ROOT_LEVEL; |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 911 | mmu_alloc_roots(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 912 | ASSERT(VALID_PAGE(context->root_hpa)); |
| 913 | kvm_arch_ops->set_cr3(vcpu, context->root_hpa | |
| 914 | (vcpu->cr3 & (CR3_PCD_MASK | CR3_WPT_MASK))); |
| 915 | return 0; |
| 916 | } |
| 917 | |
| 918 | static int paging32E_init_context(struct kvm_vcpu *vcpu) |
| 919 | { |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 920 | return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | static int init_kvm_mmu(struct kvm_vcpu *vcpu) |
| 924 | { |
| 925 | ASSERT(vcpu); |
| 926 | ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa)); |
| 927 | |
| 928 | if (!is_paging(vcpu)) |
| 929 | return nonpaging_init_context(vcpu); |
Avi Kivity | a9058ec | 2006-12-29 16:49:37 -0800 | [diff] [blame] | 930 | else if (is_long_mode(vcpu)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 931 | return paging64_init_context(vcpu); |
| 932 | else if (is_pae(vcpu)) |
| 933 | return paging32E_init_context(vcpu); |
| 934 | else |
| 935 | return paging32_init_context(vcpu); |
| 936 | } |
| 937 | |
| 938 | static void destroy_kvm_mmu(struct kvm_vcpu *vcpu) |
| 939 | { |
| 940 | ASSERT(vcpu); |
| 941 | if (VALID_PAGE(vcpu->mmu.root_hpa)) { |
| 942 | vcpu->mmu.free(vcpu); |
| 943 | vcpu->mmu.root_hpa = INVALID_PAGE; |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | int kvm_mmu_reset_context(struct kvm_vcpu *vcpu) |
| 948 | { |
| 949 | destroy_kvm_mmu(vcpu); |
| 950 | return init_kvm_mmu(vcpu); |
| 951 | } |
| 952 | |
Avi Kivity | da4a00f | 2007-01-05 16:36:44 -0800 | [diff] [blame] | 953 | void kvm_mmu_pre_write(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes) |
| 954 | { |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 955 | gfn_t gfn = gpa >> PAGE_SHIFT; |
| 956 | struct kvm_mmu_page *page; |
| 957 | struct kvm_mmu_page *child; |
Avi Kivity | 0e7bc4b | 2007-01-05 16:36:48 -0800 | [diff] [blame] | 958 | struct hlist_node *node, *n; |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 959 | struct hlist_head *bucket; |
| 960 | unsigned index; |
| 961 | u64 *spte; |
| 962 | u64 pte; |
| 963 | unsigned offset = offset_in_page(gpa); |
Avi Kivity | 0e7bc4b | 2007-01-05 16:36:48 -0800 | [diff] [blame] | 964 | unsigned pte_size; |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 965 | unsigned page_offset; |
Avi Kivity | 0e7bc4b | 2007-01-05 16:36:48 -0800 | [diff] [blame] | 966 | unsigned misaligned; |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 967 | int level; |
| 968 | |
Avi Kivity | da4a00f | 2007-01-05 16:36:44 -0800 | [diff] [blame] | 969 | pgprintk("%s: gpa %llx bytes %d\n", __FUNCTION__, gpa, bytes); |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 970 | index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES; |
| 971 | bucket = &vcpu->kvm->mmu_page_hash[index]; |
Avi Kivity | 0e7bc4b | 2007-01-05 16:36:48 -0800 | [diff] [blame] | 972 | hlist_for_each_entry_safe(page, node, n, bucket, hash_link) { |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 973 | if (page->gfn != gfn || page->role.metaphysical) |
| 974 | continue; |
Avi Kivity | 0e7bc4b | 2007-01-05 16:36:48 -0800 | [diff] [blame] | 975 | pte_size = page->role.glevels == PT32_ROOT_LEVEL ? 4 : 8; |
| 976 | misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1); |
| 977 | if (misaligned) { |
| 978 | /* |
| 979 | * Misaligned accesses are too much trouble to fix |
| 980 | * up; also, they usually indicate a page is not used |
| 981 | * as a page table. |
| 982 | */ |
| 983 | pgprintk("misaligned: gpa %llx bytes %d role %x\n", |
| 984 | gpa, bytes, page->role.word); |
| 985 | kvm_mmu_zap_page(vcpu, page); |
| 986 | continue; |
| 987 | } |
Avi Kivity | 9b7a032 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 988 | page_offset = offset; |
| 989 | level = page->role.level; |
| 990 | if (page->role.glevels == PT32_ROOT_LEVEL) { |
| 991 | page_offset <<= 1; /* 32->64 */ |
| 992 | page_offset &= ~PAGE_MASK; |
| 993 | } |
| 994 | spte = __va(page->page_hpa); |
| 995 | spte += page_offset / sizeof(*spte); |
| 996 | pte = *spte; |
| 997 | if (is_present_pte(pte)) { |
| 998 | if (level == PT_PAGE_TABLE_LEVEL) |
| 999 | rmap_remove(vcpu->kvm, spte); |
| 1000 | else { |
| 1001 | child = page_header(pte & PT64_BASE_ADDR_MASK); |
| 1002 | mmu_page_remove_parent_pte(child, spte); |
| 1003 | } |
| 1004 | } |
| 1005 | *spte = 0; |
| 1006 | } |
Avi Kivity | da4a00f | 2007-01-05 16:36:44 -0800 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | void kvm_mmu_post_write(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes) |
| 1010 | { |
| 1011 | } |
| 1012 | |
Avi Kivity | a436036 | 2007-01-05 16:36:45 -0800 | [diff] [blame] | 1013 | int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva) |
| 1014 | { |
| 1015 | gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva); |
| 1016 | |
| 1017 | return kvm_mmu_unprotect_page(vcpu, gpa >> PAGE_SHIFT); |
| 1018 | } |
| 1019 | |
Avi Kivity | ebeace8 | 2007-01-05 16:36:47 -0800 | [diff] [blame] | 1020 | void kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu) |
| 1021 | { |
| 1022 | while (vcpu->kvm->n_free_mmu_pages < KVM_REFILL_PAGES) { |
| 1023 | struct kvm_mmu_page *page; |
| 1024 | |
| 1025 | page = container_of(vcpu->kvm->active_mmu_pages.prev, |
| 1026 | struct kvm_mmu_page, link); |
| 1027 | kvm_mmu_zap_page(vcpu, page); |
| 1028 | } |
| 1029 | } |
| 1030 | EXPORT_SYMBOL_GPL(kvm_mmu_free_some_pages); |
| 1031 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1032 | static void free_mmu_pages(struct kvm_vcpu *vcpu) |
| 1033 | { |
| 1034 | while (!list_empty(&vcpu->free_pages)) { |
| 1035 | struct kvm_mmu_page *page; |
| 1036 | |
| 1037 | page = list_entry(vcpu->free_pages.next, |
| 1038 | struct kvm_mmu_page, link); |
| 1039 | list_del(&page->link); |
| 1040 | __free_page(pfn_to_page(page->page_hpa >> PAGE_SHIFT)); |
| 1041 | page->page_hpa = INVALID_PAGE; |
| 1042 | } |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1043 | free_page((unsigned long)vcpu->mmu.pae_root); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | static int alloc_mmu_pages(struct kvm_vcpu *vcpu) |
| 1047 | { |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1048 | struct page *page; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1049 | int i; |
| 1050 | |
| 1051 | ASSERT(vcpu); |
| 1052 | |
| 1053 | for (i = 0; i < KVM_NUM_MMU_PAGES; i++) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1054 | struct kvm_mmu_page *page_header = &vcpu->page_header_buf[i]; |
| 1055 | |
| 1056 | INIT_LIST_HEAD(&page_header->link); |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1057 | if ((page = alloc_page(GFP_KERNEL)) == NULL) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1058 | goto error_1; |
| 1059 | page->private = (unsigned long)page_header; |
| 1060 | page_header->page_hpa = (hpa_t)page_to_pfn(page) << PAGE_SHIFT; |
| 1061 | memset(__va(page_header->page_hpa), 0, PAGE_SIZE); |
| 1062 | list_add(&page_header->link, &vcpu->free_pages); |
Avi Kivity | ebeace8 | 2007-01-05 16:36:47 -0800 | [diff] [blame] | 1063 | ++vcpu->kvm->n_free_mmu_pages; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1064 | } |
Avi Kivity | 17ac10a | 2007-01-05 16:36:40 -0800 | [diff] [blame] | 1065 | |
| 1066 | /* |
| 1067 | * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64. |
| 1068 | * Therefore we need to allocate shadow page tables in the first |
| 1069 | * 4GB of memory, which happens to fit the DMA32 zone. |
| 1070 | */ |
| 1071 | page = alloc_page(GFP_KERNEL | __GFP_DMA32); |
| 1072 | if (!page) |
| 1073 | goto error_1; |
| 1074 | vcpu->mmu.pae_root = page_address(page); |
| 1075 | for (i = 0; i < 4; ++i) |
| 1076 | vcpu->mmu.pae_root[i] = INVALID_PAGE; |
| 1077 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1078 | return 0; |
| 1079 | |
| 1080 | error_1: |
| 1081 | free_mmu_pages(vcpu); |
| 1082 | return -ENOMEM; |
| 1083 | } |
| 1084 | |
Ingo Molnar | 8018c27 | 2006-12-29 16:50:01 -0800 | [diff] [blame] | 1085 | int kvm_mmu_create(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1086 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1087 | ASSERT(vcpu); |
| 1088 | ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa)); |
| 1089 | ASSERT(list_empty(&vcpu->free_pages)); |
| 1090 | |
Ingo Molnar | 8018c27 | 2006-12-29 16:50:01 -0800 | [diff] [blame] | 1091 | return alloc_mmu_pages(vcpu); |
| 1092 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1093 | |
Ingo Molnar | 8018c27 | 2006-12-29 16:50:01 -0800 | [diff] [blame] | 1094 | int kvm_mmu_setup(struct kvm_vcpu *vcpu) |
| 1095 | { |
| 1096 | ASSERT(vcpu); |
| 1097 | ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa)); |
| 1098 | ASSERT(!list_empty(&vcpu->free_pages)); |
Avi Kivity | 2c26495 | 2006-12-22 01:05:28 -0800 | [diff] [blame] | 1099 | |
Ingo Molnar | 8018c27 | 2006-12-29 16:50:01 -0800 | [diff] [blame] | 1100 | return init_kvm_mmu(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | void kvm_mmu_destroy(struct kvm_vcpu *vcpu) |
| 1104 | { |
| 1105 | ASSERT(vcpu); |
| 1106 | |
| 1107 | destroy_kvm_mmu(vcpu); |
| 1108 | free_mmu_pages(vcpu); |
| 1109 | } |
| 1110 | |
| 1111 | void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot) |
| 1112 | { |
| 1113 | struct kvm_mmu_page *page; |
| 1114 | |
| 1115 | list_for_each_entry(page, &kvm->active_mmu_pages, link) { |
| 1116 | int i; |
| 1117 | u64 *pt; |
| 1118 | |
| 1119 | if (!test_bit(slot, &page->slot_bitmap)) |
| 1120 | continue; |
| 1121 | |
| 1122 | pt = __va(page->page_hpa); |
| 1123 | for (i = 0; i < PT64_ENT_PER_PAGE; ++i) |
| 1124 | /* avoid RMW */ |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 1125 | if (pt[i] & PT_WRITABLE_MASK) { |
| 1126 | rmap_remove(kvm, &pt[i]); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1127 | pt[i] &= ~PT_WRITABLE_MASK; |
Avi Kivity | cd4a4e5 | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 1128 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1129 | } |
| 1130 | } |