blob: 1932a3aeda1d3651171b401f45e81cdab9d2da19 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
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 */
Avi Kivity6aa8b732006-12-10 02:21:36 -080019
20#include "vmx.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080021#include "mmu.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080022
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Avi Kivitye4956062007-06-28 14:15:57 -040024#include <linux/types.h>
25#include <linux/string.h>
26#include <linux/mm.h>
27#include <linux/highmem.h>
28#include <linux/module.h>
Izik Eidus448353c2007-11-26 14:08:14 +020029#include <linux/swap.h>
Marcelo Tosatti05da4552008-02-23 11:44:30 -030030#include <linux/hugetlb.h>
Avi Kivitye4956062007-06-28 14:15:57 -040031
32#include <asm/page.h>
33#include <asm/cmpxchg.h>
Avi Kivity4e542372007-11-21 14:08:40 +020034#include <asm/io.h>
Avi Kivitye4956062007-06-28 14:15:57 -040035
Joerg Roedel18552672008-02-07 13:47:41 +010036/*
37 * When setting this variable to true it enables Two-Dimensional-Paging
38 * where the hardware walks 2 page tables:
39 * 1. the guest-virtual to guest-physical
40 * 2. while doing 1. it walks guest-physical to host-physical
41 * If the hardware supports that we don't need to do shadow paging.
42 */
43static bool tdp_enabled = false;
44
Avi Kivity37a7d8b2007-01-05 16:36:56 -080045#undef MMU_DEBUG
46
47#undef AUDIT
48
49#ifdef AUDIT
50static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
51#else
52static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
53#endif
54
55#ifdef MMU_DEBUG
56
57#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
58#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
59
60#else
61
62#define pgprintk(x...) do { } while (0)
63#define rmap_printk(x...) do { } while (0)
64
65#endif
66
67#if defined(MMU_DEBUG) || defined(AUDIT)
68static int dbg = 1;
69#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080070
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080071#ifndef MMU_DEBUG
72#define ASSERT(x) do { } while (0)
73#else
Avi Kivity6aa8b732006-12-10 02:21:36 -080074#define ASSERT(x) \
75 if (!(x)) { \
76 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
77 __FILE__, __LINE__, #x); \
78 }
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080079#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080080
Avi Kivitycea0f0e2007-01-05 16:36:43 -080081#define PT64_PT_BITS 9
82#define PT64_ENT_PER_PAGE (1 << PT64_PT_BITS)
83#define PT32_PT_BITS 10
84#define PT32_ENT_PER_PAGE (1 << PT32_PT_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -080085
86#define PT_WRITABLE_SHIFT 1
87
88#define PT_PRESENT_MASK (1ULL << 0)
89#define PT_WRITABLE_MASK (1ULL << PT_WRITABLE_SHIFT)
90#define PT_USER_MASK (1ULL << 2)
91#define PT_PWT_MASK (1ULL << 3)
92#define PT_PCD_MASK (1ULL << 4)
93#define PT_ACCESSED_MASK (1ULL << 5)
94#define PT_DIRTY_MASK (1ULL << 6)
95#define PT_PAGE_SIZE_MASK (1ULL << 7)
96#define PT_PAT_MASK (1ULL << 7)
97#define PT_GLOBAL_MASK (1ULL << 8)
Avi Kivityfe135d22007-12-09 16:15:46 +020098#define PT64_NX_SHIFT 63
99#define PT64_NX_MASK (1ULL << PT64_NX_SHIFT)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800100
101#define PT_PAT_SHIFT 7
102#define PT_DIR_PAT_SHIFT 12
103#define PT_DIR_PAT_MASK (1ULL << PT_DIR_PAT_SHIFT)
104
105#define PT32_DIR_PSE36_SIZE 4
106#define PT32_DIR_PSE36_SHIFT 13
Mike Dayd77c26f2007-10-08 09:02:08 -0400107#define PT32_DIR_PSE36_MASK \
108 (((1ULL << PT32_DIR_PSE36_SIZE) - 1) << PT32_DIR_PSE36_SHIFT)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800109
110
Avi Kivity6aa8b732006-12-10 02:21:36 -0800111#define PT_FIRST_AVAIL_BITS_SHIFT 9
112#define PT64_SECOND_AVAIL_BITS_SHIFT 52
113
Avi Kivity6aa8b732006-12-10 02:21:36 -0800114#define VALID_PAGE(x) ((x) != INVALID_PAGE)
115
116#define PT64_LEVEL_BITS 9
117
118#define PT64_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400119 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800120
121#define PT64_LEVEL_MASK(level) \
122 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
123
124#define PT64_INDEX(address, level)\
125 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
126
127
128#define PT32_LEVEL_BITS 10
129
130#define PT32_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400131 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800132
133#define PT32_LEVEL_MASK(level) \
134 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
135
136#define PT32_INDEX(address, level)\
137 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
138
139
Avi Kivity27aba762007-03-09 13:04:31 +0200140#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800141#define PT64_DIR_BASE_ADDR_MASK \
142 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
143
144#define PT32_BASE_ADDR_MASK PAGE_MASK
145#define PT32_DIR_BASE_ADDR_MASK \
146 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
147
Avi Kivity79539ce2007-11-21 02:06:21 +0200148#define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
149 | PT64_NX_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800150
151#define PFERR_PRESENT_MASK (1U << 0)
152#define PFERR_WRITE_MASK (1U << 1)
153#define PFERR_USER_MASK (1U << 2)
Avi Kivity73b10872007-01-26 00:56:41 -0800154#define PFERR_FETCH_MASK (1U << 4)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800155
156#define PT64_ROOT_LEVEL 4
157#define PT32_ROOT_LEVEL 2
158#define PT32E_ROOT_LEVEL 3
159
160#define PT_DIRECTORY_LEVEL 2
161#define PT_PAGE_TABLE_LEVEL 1
162
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800163#define RMAP_EXT 4
164
Avi Kivityfe135d22007-12-09 16:15:46 +0200165#define ACC_EXEC_MASK 1
166#define ACC_WRITE_MASK PT_WRITABLE_MASK
167#define ACC_USER_MASK PT_USER_MASK
168#define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
169
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800170struct kvm_rmap_desc {
171 u64 *shadow_ptes[RMAP_EXT];
172 struct kvm_rmap_desc *more;
173};
174
Avi Kivityb5a33a72007-04-15 16:31:09 +0300175static struct kmem_cache *pte_chain_cache;
176static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300177static struct kmem_cache *mmu_page_header_cache;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300178
Avi Kivityc7addb92007-09-16 18:58:32 +0200179static u64 __read_mostly shadow_trap_nonpresent_pte;
180static u64 __read_mostly shadow_notrap_nonpresent_pte;
181
182void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
183{
184 shadow_trap_nonpresent_pte = trap_pte;
185 shadow_notrap_nonpresent_pte = notrap_pte;
186}
187EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
188
Avi Kivity6aa8b732006-12-10 02:21:36 -0800189static int is_write_protection(struct kvm_vcpu *vcpu)
190{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800191 return vcpu->arch.cr0 & X86_CR0_WP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800192}
193
194static int is_cpuid_PSE36(void)
195{
196 return 1;
197}
198
Avi Kivity73b10872007-01-26 00:56:41 -0800199static int is_nx(struct kvm_vcpu *vcpu)
200{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800201 return vcpu->arch.shadow_efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800202}
203
Avi Kivity6aa8b732006-12-10 02:21:36 -0800204static int is_present_pte(unsigned long pte)
205{
206 return pte & PT_PRESENT_MASK;
207}
208
Avi Kivityc7addb92007-09-16 18:58:32 +0200209static int is_shadow_present_pte(u64 pte)
210{
Avi Kivityc7addb92007-09-16 18:58:32 +0200211 return pte != shadow_trap_nonpresent_pte
212 && pte != shadow_notrap_nonpresent_pte;
213}
214
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300215static int is_large_pte(u64 pte)
216{
217 return pte & PT_PAGE_SIZE_MASK;
218}
219
Avi Kivity6aa8b732006-12-10 02:21:36 -0800220static int is_writeble_pte(unsigned long pte)
221{
222 return pte & PT_WRITABLE_MASK;
223}
224
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200225static int is_dirty_pte(unsigned long pte)
226{
227 return pte & PT_DIRTY_MASK;
228}
229
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800230static int is_rmap_pte(u64 pte)
231{
Avi Kivity4b1a80f2008-03-23 12:18:19 +0200232 return is_shadow_present_pte(pte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800233}
234
Avi Kivityda928522007-11-21 13:54:47 +0200235static gfn_t pse36_gfn_delta(u32 gpte)
236{
237 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
238
239 return (gpte & PT32_DIR_PSE36_MASK) << shift;
240}
241
Avi Kivitye663ee62007-05-31 15:46:04 +0300242static void set_shadow_pte(u64 *sptep, u64 spte)
243{
244#ifdef CONFIG_X86_64
245 set_64bit((unsigned long *)sptep, spte);
246#else
247 set_64bit((unsigned long long *)sptep, spte);
248#endif
249}
250
Avi Kivitye2dec932007-01-05 16:36:54 -0800251static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300252 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800253{
254 void *obj;
255
256 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800257 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800258 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300259 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800260 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800261 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800262 cache->objects[cache->nobjs++] = obj;
263 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800264 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800265}
266
267static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
268{
269 while (mc->nobjs)
270 kfree(mc->objects[--mc->nobjs]);
271}
272
Avi Kivityc1158e62007-07-20 08:18:27 +0300273static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300274 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300275{
276 struct page *page;
277
278 if (cache->nobjs >= min)
279 return 0;
280 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300281 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300282 if (!page)
283 return -ENOMEM;
284 set_page_private(page, 0);
285 cache->objects[cache->nobjs++] = page_address(page);
286 }
287 return 0;
288}
289
290static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
291{
292 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300293 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300294}
295
Avi Kivity8c438502007-04-16 11:53:17 +0300296static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
297{
298 int r;
299
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800300 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300301 pte_chain_cache, 4);
302 if (r)
303 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800304 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300305 rmap_desc_cache, 1);
306 if (r)
307 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800308 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300309 if (r)
310 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800311 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300312 mmu_page_header_cache, 4);
313out:
Avi Kivity8c438502007-04-16 11:53:17 +0300314 return r;
315}
316
Avi Kivity714b93d2007-01-05 16:36:53 -0800317static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
318{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800319 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
320 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
321 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
322 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800323}
324
325static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
326 size_t size)
327{
328 void *p;
329
330 BUG_ON(!mc->nobjs);
331 p = mc->objects[--mc->nobjs];
332 memset(p, 0, size);
333 return p;
334}
335
Avi Kivity714b93d2007-01-05 16:36:53 -0800336static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
337{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800338 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800339 sizeof(struct kvm_pte_chain));
340}
341
Avi Kivity90cb0522007-07-17 13:04:56 +0300342static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800343{
Avi Kivity90cb0522007-07-17 13:04:56 +0300344 kfree(pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800345}
346
347static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
348{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800349 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800350 sizeof(struct kvm_rmap_desc));
351}
352
Avi Kivity90cb0522007-07-17 13:04:56 +0300353static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800354{
Avi Kivity90cb0522007-07-17 13:04:56 +0300355 kfree(rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800356}
357
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800358/*
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300359 * Return the pointer to the largepage write count for a given
360 * gfn, handling slots that are not large page aligned.
361 */
362static int *slot_largepage_idx(gfn_t gfn, struct kvm_memory_slot *slot)
363{
364 unsigned long idx;
365
366 idx = (gfn / KVM_PAGES_PER_HPAGE) -
367 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
368 return &slot->lpage_info[idx].write_count;
369}
370
371static void account_shadowed(struct kvm *kvm, gfn_t gfn)
372{
373 int *write_count;
374
375 write_count = slot_largepage_idx(gfn, gfn_to_memslot(kvm, gfn));
376 *write_count += 1;
377 WARN_ON(*write_count > KVM_PAGES_PER_HPAGE);
378}
379
380static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
381{
382 int *write_count;
383
384 write_count = slot_largepage_idx(gfn, gfn_to_memslot(kvm, gfn));
385 *write_count -= 1;
386 WARN_ON(*write_count < 0);
387}
388
389static int has_wrprotected_page(struct kvm *kvm, gfn_t gfn)
390{
391 struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
392 int *largepage_idx;
393
394 if (slot) {
395 largepage_idx = slot_largepage_idx(gfn, slot);
396 return *largepage_idx;
397 }
398
399 return 1;
400}
401
402static int host_largepage_backed(struct kvm *kvm, gfn_t gfn)
403{
404 struct vm_area_struct *vma;
405 unsigned long addr;
406
407 addr = gfn_to_hva(kvm, gfn);
408 if (kvm_is_error_hva(addr))
409 return 0;
410
411 vma = find_vma(current->mm, addr);
412 if (vma && is_vm_hugetlb_page(vma))
413 return 1;
414
415 return 0;
416}
417
418static int is_largepage_backed(struct kvm_vcpu *vcpu, gfn_t large_gfn)
419{
420 struct kvm_memory_slot *slot;
421
422 if (has_wrprotected_page(vcpu->kvm, large_gfn))
423 return 0;
424
425 if (!host_largepage_backed(vcpu->kvm, large_gfn))
426 return 0;
427
428 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
429 if (slot && slot->dirty_bitmap)
430 return 0;
431
432 return 1;
433}
434
435/*
Izik Eidus290fc382007-09-27 14:11:22 +0200436 * Take gfn and return the reverse mapping to it.
437 * Note: gfn must be unaliased before this function get called
438 */
439
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300440static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int lpage)
Izik Eidus290fc382007-09-27 14:11:22 +0200441{
442 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300443 unsigned long idx;
Izik Eidus290fc382007-09-27 14:11:22 +0200444
445 slot = gfn_to_memslot(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300446 if (!lpage)
447 return &slot->rmap[gfn - slot->base_gfn];
448
449 idx = (gfn / KVM_PAGES_PER_HPAGE) -
450 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
451
452 return &slot->lpage_info[idx].rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200453}
454
455/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800456 * Reverse mapping data structures:
457 *
Izik Eidus290fc382007-09-27 14:11:22 +0200458 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
459 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800460 *
Izik Eidus290fc382007-09-27 14:11:22 +0200461 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
462 * containing more mappings.
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800463 */
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300464static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn, int lpage)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800465{
Avi Kivity4db35312007-11-21 15:28:32 +0200466 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800467 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200468 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800469 int i;
470
471 if (!is_rmap_pte(*spte))
472 return;
Izik Eidus290fc382007-09-27 14:11:22 +0200473 gfn = unalias_gfn(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200474 sp = page_header(__pa(spte));
475 sp->gfns[spte - sp->spt] = gfn;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300476 rmapp = gfn_to_rmap(vcpu->kvm, gfn, lpage);
Izik Eidus290fc382007-09-27 14:11:22 +0200477 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800478 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200479 *rmapp = (unsigned long)spte;
480 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800481 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800482 desc = mmu_alloc_rmap_desc(vcpu);
Izik Eidus290fc382007-09-27 14:11:22 +0200483 desc->shadow_ptes[0] = (u64 *)*rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800484 desc->shadow_ptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200485 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800486 } else {
487 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200488 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800489 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
490 desc = desc->more;
491 if (desc->shadow_ptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800492 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800493 desc = desc->more;
494 }
495 for (i = 0; desc->shadow_ptes[i]; ++i)
496 ;
497 desc->shadow_ptes[i] = spte;
498 }
499}
500
Izik Eidus290fc382007-09-27 14:11:22 +0200501static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800502 struct kvm_rmap_desc *desc,
503 int i,
504 struct kvm_rmap_desc *prev_desc)
505{
506 int j;
507
508 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
509 ;
510 desc->shadow_ptes[i] = desc->shadow_ptes[j];
Al Viro11718b4d2007-02-09 16:39:20 +0000511 desc->shadow_ptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800512 if (j != 0)
513 return;
514 if (!prev_desc && !desc->more)
Izik Eidus290fc382007-09-27 14:11:22 +0200515 *rmapp = (unsigned long)desc->shadow_ptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800516 else
517 if (prev_desc)
518 prev_desc->more = desc->more;
519 else
Izik Eidus290fc382007-09-27 14:11:22 +0200520 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300521 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800522}
523
Izik Eidus290fc382007-09-27 14:11:22 +0200524static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800525{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800526 struct kvm_rmap_desc *desc;
527 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200528 struct kvm_mmu_page *sp;
Avi Kivity76c35c62007-11-21 15:32:41 +0200529 struct page *page;
Izik Eidus290fc382007-09-27 14:11:22 +0200530 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800531 int i;
532
533 if (!is_rmap_pte(*spte))
534 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200535 sp = page_header(__pa(spte));
Avi Kivity76c35c62007-11-21 15:32:41 +0200536 page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT);
Izik Eidus448353c2007-11-26 14:08:14 +0200537 mark_page_accessed(page);
Izik Eidusb4231d62007-11-20 11:49:33 +0200538 if (is_writeble_pte(*spte))
Avi Kivity76c35c62007-11-21 15:32:41 +0200539 kvm_release_page_dirty(page);
Izik Eidusb4231d62007-11-20 11:49:33 +0200540 else
Avi Kivity76c35c62007-11-21 15:32:41 +0200541 kvm_release_page_clean(page);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300542 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], is_large_pte(*spte));
Izik Eidus290fc382007-09-27 14:11:22 +0200543 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800544 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
545 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200546 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800547 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200548 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800549 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
550 spte, *spte);
551 BUG();
552 }
Izik Eidus290fc382007-09-27 14:11:22 +0200553 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800554 } else {
555 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200556 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800557 prev_desc = NULL;
558 while (desc) {
559 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
560 if (desc->shadow_ptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200561 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800562 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800563 prev_desc);
564 return;
565 }
566 prev_desc = desc;
567 desc = desc->more;
568 }
569 BUG();
570 }
571}
572
Izik Eidus98348e92007-10-16 14:42:30 +0200573static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800574{
Avi Kivity374cbac2007-01-05 16:36:43 -0800575 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200576 struct kvm_rmap_desc *prev_desc;
577 u64 *prev_spte;
578 int i;
579
580 if (!*rmapp)
581 return NULL;
582 else if (!(*rmapp & 1)) {
583 if (!spte)
584 return (u64 *)*rmapp;
585 return NULL;
586 }
587 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
588 prev_desc = NULL;
589 prev_spte = NULL;
590 while (desc) {
591 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) {
592 if (prev_spte == spte)
593 return desc->shadow_ptes[i];
594 prev_spte = desc->shadow_ptes[i];
595 }
596 desc = desc->more;
597 }
598 return NULL;
599}
600
601static void rmap_write_protect(struct kvm *kvm, u64 gfn)
602{
Izik Eidus290fc382007-09-27 14:11:22 +0200603 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800604 u64 *spte;
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800605 int write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800606
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500607 gfn = unalias_gfn(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300608 rmapp = gfn_to_rmap(kvm, gfn, 0);
Avi Kivity374cbac2007-01-05 16:36:43 -0800609
Izik Eidus98348e92007-10-16 14:42:30 +0200610 spte = rmap_next(kvm, rmapp, NULL);
611 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800612 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800613 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800614 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800615 if (is_writeble_pte(*spte)) {
Izik Eidus9647c142007-10-16 14:43:46 +0200616 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800617 write_protected = 1;
618 }
Izik Eidus9647c142007-10-16 14:43:46 +0200619 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800620 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300621 /* check for huge page mappings */
622 rmapp = gfn_to_rmap(kvm, gfn, 1);
623 spte = rmap_next(kvm, rmapp, NULL);
624 while (spte) {
625 BUG_ON(!spte);
626 BUG_ON(!(*spte & PT_PRESENT_MASK));
627 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
628 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
629 if (is_writeble_pte(*spte)) {
630 rmap_remove(kvm, spte);
631 --kvm->stat.lpages;
632 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
633 write_protected = 1;
634 }
635 spte = rmap_next(kvm, rmapp, spte);
636 }
637
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800638 if (write_protected)
639 kvm_flush_remote_tlbs(kvm);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300640
641 account_shadowed(kvm, gfn);
Avi Kivity374cbac2007-01-05 16:36:43 -0800642}
643
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800644#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300645static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800646{
Avi Kivity139bdb22007-01-05 16:36:50 -0800647 u64 *pos;
648 u64 *end;
649
Avi Kivity47ad8e62007-05-06 15:50:58 +0300650 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivityd196e342008-01-24 11:44:11 +0200651 if (*pos != shadow_trap_nonpresent_pte) {
Avi Kivity139bdb22007-01-05 16:36:50 -0800652 printk(KERN_ERR "%s: %p %llx\n", __FUNCTION__,
653 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800654 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800655 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800656 return 1;
657}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800658#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800659
Avi Kivity4db35312007-11-21 15:28:32 +0200660static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800661{
Avi Kivity4db35312007-11-21 15:28:32 +0200662 ASSERT(is_empty_shadow_page(sp->spt));
663 list_del(&sp->link);
664 __free_page(virt_to_page(sp->spt));
665 __free_page(virt_to_page(sp->gfns));
666 kfree(sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800667 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800668}
669
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800670static unsigned kvm_page_table_hashfn(gfn_t gfn)
671{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200672 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800673}
674
Avi Kivity25c0de22007-01-05 16:36:42 -0800675static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
676 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800677{
Avi Kivity4db35312007-11-21 15:28:32 +0200678 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800679
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800680 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
681 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
682 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200683 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800684 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Avi Kivity4db35312007-11-21 15:28:32 +0200685 ASSERT(is_empty_shadow_page(sp->spt));
686 sp->slot_bitmap = 0;
687 sp->multimapped = 0;
688 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800689 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200690 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800691}
692
Avi Kivity714b93d2007-01-05 16:36:53 -0800693static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200694 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800695{
696 struct kvm_pte_chain *pte_chain;
697 struct hlist_node *node;
698 int i;
699
700 if (!parent_pte)
701 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200702 if (!sp->multimapped) {
703 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800704
705 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200706 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800707 return;
708 }
Avi Kivity4db35312007-11-21 15:28:32 +0200709 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800710 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200711 INIT_HLIST_HEAD(&sp->parent_ptes);
712 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800713 pte_chain->parent_ptes[0] = old;
714 }
Avi Kivity4db35312007-11-21 15:28:32 +0200715 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800716 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
717 continue;
718 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
719 if (!pte_chain->parent_ptes[i]) {
720 pte_chain->parent_ptes[i] = parent_pte;
721 return;
722 }
723 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800724 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800725 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200726 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800727 pte_chain->parent_ptes[0] = parent_pte;
728}
729
Avi Kivity4db35312007-11-21 15:28:32 +0200730static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800731 u64 *parent_pte)
732{
733 struct kvm_pte_chain *pte_chain;
734 struct hlist_node *node;
735 int i;
736
Avi Kivity4db35312007-11-21 15:28:32 +0200737 if (!sp->multimapped) {
738 BUG_ON(sp->parent_pte != parent_pte);
739 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800740 return;
741 }
Avi Kivity4db35312007-11-21 15:28:32 +0200742 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800743 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
744 if (!pte_chain->parent_ptes[i])
745 break;
746 if (pte_chain->parent_ptes[i] != parent_pte)
747 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800748 while (i + 1 < NR_PTE_CHAIN_ENTRIES
749 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800750 pte_chain->parent_ptes[i]
751 = pte_chain->parent_ptes[i + 1];
752 ++i;
753 }
754 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800755 if (i == 0) {
756 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300757 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200758 if (hlist_empty(&sp->parent_ptes)) {
759 sp->multimapped = 0;
760 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800761 }
762 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800763 return;
764 }
765 BUG();
766}
767
Avi Kivity4db35312007-11-21 15:28:32 +0200768static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800769{
770 unsigned index;
771 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200772 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800773 struct hlist_node *node;
774
775 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200776 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800777 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200778 hlist_for_each_entry(sp, node, bucket, hash_link)
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500779 if (sp->gfn == gfn && !sp->role.metaphysical
780 && !sp->role.invalid) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800781 pgprintk("%s: found role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +0200782 __FUNCTION__, sp->role.word);
783 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800784 }
785 return NULL;
786}
787
788static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
789 gfn_t gfn,
790 gva_t gaddr,
791 unsigned level,
792 int metaphysical,
Avi Kivity41074d02007-12-09 17:00:02 +0200793 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +0200794 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800795{
796 union kvm_mmu_page_role role;
797 unsigned index;
798 unsigned quadrant;
799 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200800 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800801 struct hlist_node *node;
802
803 role.word = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800804 role.glevels = vcpu->arch.mmu.root_level;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800805 role.level = level;
806 role.metaphysical = metaphysical;
Avi Kivity41074d02007-12-09 17:00:02 +0200807 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800808 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800809 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
810 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
811 role.quadrant = quadrant;
812 }
813 pgprintk("%s: looking gfn %lx role %x\n", __FUNCTION__,
814 gfn, role.word);
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200815 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800816 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200817 hlist_for_each_entry(sp, node, bucket, hash_link)
818 if (sp->gfn == gfn && sp->role.word == role.word) {
819 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800820 pgprintk("%s: found\n", __FUNCTION__);
Avi Kivity4db35312007-11-21 15:28:32 +0200821 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800822 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +0200823 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +0200824 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
825 if (!sp)
826 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800827 pgprintk("%s: adding gfn %lx role %x\n", __FUNCTION__, gfn, role.word);
Avi Kivity4db35312007-11-21 15:28:32 +0200828 sp->gfn = gfn;
829 sp->role = role;
830 hlist_add_head(&sp->hash_link, bucket);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800831 vcpu->arch.mmu.prefetch_page(vcpu, sp);
Avi Kivity374cbac2007-01-05 16:36:43 -0800832 if (!metaphysical)
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500833 rmap_write_protect(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200834 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800835}
836
Avi Kivity90cb0522007-07-17 13:04:56 +0300837static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +0200838 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -0800839{
Avi Kivity697fe2e2007-01-05 16:36:46 -0800840 unsigned i;
841 u64 *pt;
842 u64 ent;
843
Avi Kivity4db35312007-11-21 15:28:32 +0200844 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800845
Avi Kivity4db35312007-11-21 15:28:32 +0200846 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800847 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Avi Kivityc7addb92007-09-16 18:58:32 +0200848 if (is_shadow_present_pte(pt[i]))
Izik Eidus290fc382007-09-27 14:11:22 +0200849 rmap_remove(kvm, &pt[i]);
Avi Kivityc7addb92007-09-16 18:58:32 +0200850 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800851 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300852 kvm_flush_remote_tlbs(kvm);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800853 return;
854 }
855
856 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
857 ent = pt[i];
858
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300859 if (is_shadow_present_pte(ent)) {
860 if (!is_large_pte(ent)) {
861 ent &= PT64_BASE_ADDR_MASK;
862 mmu_page_remove_parent_pte(page_header(ent),
863 &pt[i]);
864 } else {
865 --kvm->stat.lpages;
866 rmap_remove(kvm, &pt[i]);
867 }
868 }
Avi Kivityc7addb92007-09-16 18:58:32 +0200869 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800870 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300871 kvm_flush_remote_tlbs(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800872}
873
Avi Kivity4db35312007-11-21 15:28:32 +0200874static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800875{
Avi Kivity4db35312007-11-21 15:28:32 +0200876 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800877}
878
Avi Kivity12b7d282007-09-23 14:10:49 +0200879static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
880{
881 int i;
882
883 for (i = 0; i < KVM_MAX_VCPUS; ++i)
884 if (kvm->vcpus[i])
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800885 kvm->vcpus[i]->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +0200886}
887
Avi Kivity4db35312007-11-21 15:28:32 +0200888static void kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -0800889{
890 u64 *parent_pte;
891
Avi Kivity4cee5762007-11-18 16:37:07 +0200892 ++kvm->stat.mmu_shadow_zapped;
Avi Kivity4db35312007-11-21 15:28:32 +0200893 while (sp->multimapped || sp->parent_pte) {
894 if (!sp->multimapped)
895 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -0800896 else {
897 struct kvm_pte_chain *chain;
898
Avi Kivity4db35312007-11-21 15:28:32 +0200899 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -0800900 struct kvm_pte_chain, link);
901 parent_pte = chain->parent_ptes[0];
902 }
Avi Kivity697fe2e2007-01-05 16:36:46 -0800903 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +0200904 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityc7addb92007-09-16 18:58:32 +0200905 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800906 }
Avi Kivity4db35312007-11-21 15:28:32 +0200907 kvm_mmu_page_unlink_children(kvm, sp);
908 if (!sp->root_count) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300909 if (!sp->role.metaphysical)
910 unaccount_shadowed(kvm, sp->gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200911 hlist_del(&sp->hash_link);
912 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500913 } else {
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800914 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500915 sp->role.invalid = 1;
916 kvm_reload_remote_mmus(kvm);
917 }
Avi Kivity12b7d282007-09-23 14:10:49 +0200918 kvm_mmu_reset_last_pte_updated(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800919}
920
Izik Eidus82ce2c92007-10-02 18:52:55 +0200921/*
922 * Changing the number of mmu pages allocated to the vm
923 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
924 */
925void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
926{
927 /*
928 * If we set the number of mmu pages to be smaller be than the
929 * number of actived pages , we must to free some mmu pages before we
930 * change the value
931 */
932
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800933 if ((kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages) >
Izik Eidus82ce2c92007-10-02 18:52:55 +0200934 kvm_nr_mmu_pages) {
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800935 int n_used_mmu_pages = kvm->arch.n_alloc_mmu_pages
936 - kvm->arch.n_free_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200937
938 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
939 struct kvm_mmu_page *page;
940
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800941 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +0200942 struct kvm_mmu_page, link);
943 kvm_mmu_zap_page(kvm, page);
944 n_used_mmu_pages--;
945 }
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800946 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200947 }
948 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800949 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
950 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200951
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800952 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200953}
954
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500955static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -0800956{
957 unsigned index;
958 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200959 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -0800960 struct hlist_node *node, *n;
961 int r;
962
963 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
964 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200965 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800966 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200967 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
968 if (sp->gfn == gfn && !sp->role.metaphysical) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800969 pgprintk("%s: gfn %lx role %x\n", __FUNCTION__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +0200970 sp->role.word);
971 kvm_mmu_zap_page(kvm, sp);
Avi Kivitya4360362007-01-05 16:36:45 -0800972 r = 1;
973 }
974 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800975}
976
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500977static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +0300978{
Avi Kivity4db35312007-11-21 15:28:32 +0200979 struct kvm_mmu_page *sp;
Avi Kivity97a0a012007-05-31 15:08:29 +0300980
Avi Kivity4db35312007-11-21 15:28:32 +0200981 while ((sp = kvm_mmu_lookup_page(kvm, gfn)) != NULL) {
982 pgprintk("%s: zap %lx %x\n", __FUNCTION__, gfn, sp->role.word);
983 kvm_mmu_zap_page(kvm, sp);
Avi Kivity97a0a012007-05-31 15:08:29 +0300984 }
985}
986
Avi Kivity38c335f2007-11-21 14:20:22 +0200987static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800988{
Avi Kivity38c335f2007-11-21 14:20:22 +0200989 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity4db35312007-11-21 15:28:32 +0200990 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800991
Avi Kivity4db35312007-11-21 15:28:32 +0200992 __set_bit(slot, &sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800993}
994
Avi Kivity039576c2007-03-20 12:46:50 +0200995struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
996{
Izik Eidus72dc67a2008-02-10 18:04:15 +0200997 struct page *page;
998
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800999 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Avi Kivity039576c2007-03-20 12:46:50 +02001000
1001 if (gpa == UNMAPPED_GVA)
1002 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001003
1004 down_read(&current->mm->mmap_sem);
1005 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1006 up_read(&current->mm->mmap_sem);
1007
1008 return page;
Avi Kivity039576c2007-03-20 12:46:50 +02001009}
1010
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001011static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1012 unsigned pt_access, unsigned pte_access,
1013 int user_fault, int write_fault, int dirty,
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001014 int *ptwrite, int largepage, gfn_t gfn,
1015 struct page *page)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001016{
1017 u64 spte;
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001018 int was_rmapped = 0;
Izik Eidus75e68e62008-01-12 23:49:09 +02001019 int was_writeble = is_writeble_pte(*shadow_pte);
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001020 hfn_t host_pfn = (*shadow_pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001021
Avi Kivitybc750ba2007-12-09 18:39:41 +02001022 pgprintk("%s: spte %llx access %x write_fault %d"
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001023 " user_fault %d gfn %lx\n",
Avi Kivitybc750ba2007-12-09 18:39:41 +02001024 __FUNCTION__, *shadow_pte, pt_access,
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001025 write_fault, user_fault, gfn);
1026
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001027 if (is_rmap_pte(*shadow_pte)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001028 /*
1029 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1030 * the parent of the now unreachable PTE.
1031 */
1032 if (largepage && !is_large_pte(*shadow_pte)) {
1033 struct kvm_mmu_page *child;
1034 u64 pte = *shadow_pte;
1035
1036 child = page_header(pte & PT64_BASE_ADDR_MASK);
1037 mmu_page_remove_parent_pte(child, shadow_pte);
1038 } else if (host_pfn != page_to_pfn(page)) {
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001039 pgprintk("hfn old %lx new %lx\n",
1040 host_pfn, page_to_pfn(page));
1041 rmap_remove(vcpu->kvm, shadow_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001042 } else {
1043 if (largepage)
1044 was_rmapped = is_large_pte(*shadow_pte);
1045 else
1046 was_rmapped = 1;
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001047 }
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001048 }
1049
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001050
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001051 /*
1052 * We don't set the accessed bit, since we sometimes want to see
1053 * whether the guest actually used the pte (in order to detect
1054 * demand paging).
1055 */
1056 spte = PT_PRESENT_MASK | PT_DIRTY_MASK;
1057 if (!dirty)
1058 pte_access &= ~ACC_WRITE_MASK;
1059 if (!(pte_access & ACC_EXEC_MASK))
1060 spte |= PT64_NX_MASK;
1061
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001062 spte |= PT_PRESENT_MASK;
1063 if (pte_access & ACC_USER_MASK)
1064 spte |= PT_USER_MASK;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001065 if (largepage)
1066 spte |= PT_PAGE_SIZE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001067
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001068 spte |= page_to_phys(page);
1069
1070 if ((pte_access & ACC_WRITE_MASK)
1071 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
1072 struct kvm_mmu_page *shadow;
1073
1074 spte |= PT_WRITABLE_MASK;
1075 if (user_fault) {
1076 mmu_unshadow(vcpu->kvm, gfn);
1077 goto unshadowed;
1078 }
1079
1080 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001081 if (shadow ||
1082 (largepage && has_wrprotected_page(vcpu->kvm, gfn))) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001083 pgprintk("%s: found shadow page for %lx, marking ro\n",
1084 __FUNCTION__, gfn);
1085 pte_access &= ~ACC_WRITE_MASK;
1086 if (is_writeble_pte(spte)) {
1087 spte &= ~PT_WRITABLE_MASK;
1088 kvm_x86_ops->tlb_flush(vcpu);
1089 }
1090 if (write_fault)
1091 *ptwrite = 1;
1092 }
1093 }
1094
1095unshadowed:
1096
1097 if (pte_access & ACC_WRITE_MASK)
1098 mark_page_dirty(vcpu->kvm, gfn);
1099
1100 pgprintk("%s: setting spte %llx\n", __FUNCTION__, spte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001101 pgprintk("instantiating %s PTE (%s) at %d (%llx) addr %llx\n",
1102 (spte&PT_PAGE_SIZE_MASK)? "2MB" : "4kB",
1103 (spte&PT_WRITABLE_MASK)?"RW":"R", gfn, spte, shadow_pte);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001104 set_shadow_pte(shadow_pte, spte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001105 if (!was_rmapped && (spte & PT_PAGE_SIZE_MASK)
1106 && (spte & PT_PRESENT_MASK))
1107 ++vcpu->kvm->stat.lpages;
1108
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001109 page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
1110 if (!was_rmapped) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001111 rmap_add(vcpu, shadow_pte, gfn, largepage);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001112 if (!is_rmap_pte(*shadow_pte))
1113 kvm_release_page_clean(page);
Izik Eidus75e68e62008-01-12 23:49:09 +02001114 } else {
1115 if (was_writeble)
1116 kvm_release_page_dirty(page);
1117 else
1118 kvm_release_page_clean(page);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001119 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001120 if (!ptwrite || !*ptwrite)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001121 vcpu->arch.last_pte_updated = shadow_pte;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001122}
1123
Avi Kivity6aa8b732006-12-10 02:21:36 -08001124static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1125{
1126}
1127
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001128static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001129 int largepage, gfn_t gfn, struct page *page,
1130 int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001131{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001132 hpa_t table_addr = vcpu->arch.mmu.root_hpa;
Avi Kivitye8332402007-12-09 18:43:00 +02001133 int pt_write = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001134
1135 for (; ; level--) {
1136 u32 index = PT64_INDEX(v, level);
1137 u64 *table;
1138
1139 ASSERT(VALID_PAGE(table_addr));
1140 table = __va(table_addr);
1141
1142 if (level == 1) {
Avi Kivitye8332402007-12-09 18:43:00 +02001143 mmu_set_spte(vcpu, &table[index], ACC_ALL, ACC_ALL,
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001144 0, write, 1, &pt_write, 0, gfn, page);
1145 return pt_write;
1146 }
1147
1148 if (largepage && level == 2) {
1149 mmu_set_spte(vcpu, &table[index], ACC_ALL, ACC_ALL,
1150 0, write, 1, &pt_write, 1, gfn, page);
Avi Kivityd196e342008-01-24 11:44:11 +02001151 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001152 }
1153
Avi Kivityc7addb92007-09-16 18:58:32 +02001154 if (table[index] == shadow_trap_nonpresent_pte) {
Avi Kivity25c0de22007-01-05 16:36:42 -08001155 struct kvm_mmu_page *new_table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001156 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001157
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001158 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
1159 >> PAGE_SHIFT;
1160 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
1161 v, level - 1,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001162 1, ACC_ALL, &table[index]);
Avi Kivity25c0de22007-01-05 16:36:42 -08001163 if (!new_table) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001164 pgprintk("nonpaging_map: ENOMEM\n");
Avi Kivityd7824ff2007-12-30 12:29:05 +02001165 kvm_release_page_clean(page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001166 return -ENOMEM;
1167 }
1168
Avi Kivity47ad8e62007-05-06 15:50:58 +03001169 table[index] = __pa(new_table->spt) | PT_PRESENT_MASK
Avi Kivity25c0de22007-01-05 16:36:42 -08001170 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001171 }
1172 table_addr = table[index] & PT64_BASE_ADDR_MASK;
1173 }
1174}
1175
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001176static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1177{
1178 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001179 int largepage = 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001180
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001181 struct page *page;
1182
Izik Eidus72dc67a2008-02-10 18:04:15 +02001183 down_read(&vcpu->kvm->slots_lock);
1184
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001185 down_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001186 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1187 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1188 largepage = 1;
1189 }
1190
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001191 page = gfn_to_page(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001192 up_read(&current->mm->mmap_sem);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001193
Avi Kivityd196e342008-01-24 11:44:11 +02001194 /* mmio */
1195 if (is_error_page(page)) {
1196 kvm_release_page_clean(page);
1197 up_read(&vcpu->kvm->slots_lock);
1198 return 1;
1199 }
1200
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001201 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02001202 kvm_mmu_free_some_pages(vcpu);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001203 r = __direct_map(vcpu, v, write, largepage, gfn, page,
1204 PT32E_ROOT_LEVEL);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001205 spin_unlock(&vcpu->kvm->mmu_lock);
1206
Izik Eidus72dc67a2008-02-10 18:04:15 +02001207 up_read(&vcpu->kvm->slots_lock);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001208
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001209 return r;
1210}
1211
1212
Avi Kivityc7addb92007-09-16 18:58:32 +02001213static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1214 struct kvm_mmu_page *sp)
1215{
1216 int i;
1217
1218 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1219 sp->spt[i] = shadow_trap_nonpresent_pte;
1220}
1221
Avi Kivity17ac10a2007-01-05 16:36:40 -08001222static void mmu_free_roots(struct kvm_vcpu *vcpu)
1223{
1224 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02001225 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001226
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001227 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03001228 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001229 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001230#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001231 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1232 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001233
Avi Kivity4db35312007-11-21 15:28:32 +02001234 sp = page_header(root);
1235 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001236 if (!sp->root_count && sp->role.invalid)
1237 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001238 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001239 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001240 return;
1241 }
1242#endif
1243 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001244 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001245
Avi Kivity417726a2007-04-12 17:35:58 +03001246 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03001247 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001248 sp = page_header(root);
1249 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001250 if (!sp->root_count && sp->role.invalid)
1251 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03001252 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001253 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001254 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001255 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001256 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001257}
1258
1259static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
1260{
1261 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001262 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02001263 struct kvm_mmu_page *sp;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001264 int metaphysical = 0;
Avi Kivity3bb65a22007-01-05 16:36:51 -08001265
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001266 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001267
1268#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001269 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1270 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001271
1272 ASSERT(!VALID_PAGE(root));
Joerg Roedelfb72d162008-02-07 13:47:44 +01001273 if (tdp_enabled)
1274 metaphysical = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001275 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001276 PT64_ROOT_LEVEL, metaphysical,
1277 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001278 root = __pa(sp->spt);
1279 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001280 vcpu->arch.mmu.root_hpa = root;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001281 return;
1282 }
1283#endif
Joerg Roedelfb72d162008-02-07 13:47:44 +01001284 metaphysical = !is_paging(vcpu);
1285 if (tdp_enabled)
1286 metaphysical = 1;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001287 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001288 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001289
1290 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001291 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
1292 if (!is_present_pte(vcpu->arch.pdptrs[i])) {
1293 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03001294 continue;
1295 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001296 root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT;
1297 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001298 root_gfn = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02001299 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001300 PT32_ROOT_LEVEL, metaphysical,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001301 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001302 root = __pa(sp->spt);
1303 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001304 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001305 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001306 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001307}
1308
Avi Kivity6aa8b732006-12-10 02:21:36 -08001309static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
1310{
1311 return vaddr;
1312}
1313
1314static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02001315 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001316{
Avi Kivitye8332402007-12-09 18:43:00 +02001317 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08001318 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001319
Avi Kivitye8332402007-12-09 18:43:00 +02001320 pgprintk("%s: gva %lx error %x\n", __FUNCTION__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08001321 r = mmu_topup_memory_caches(vcpu);
1322 if (r)
1323 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08001324
Avi Kivity6aa8b732006-12-10 02:21:36 -08001325 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001326 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001327
Avi Kivitye8332402007-12-09 18:43:00 +02001328 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001329
Avi Kivitye8332402007-12-09 18:43:00 +02001330 return nonpaging_map(vcpu, gva & PAGE_MASK,
1331 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001332}
1333
Joerg Roedelfb72d162008-02-07 13:47:44 +01001334static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
1335 u32 error_code)
1336{
1337 struct page *page;
1338 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001339 int largepage = 0;
1340 gfn_t gfn = gpa >> PAGE_SHIFT;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001341
1342 ASSERT(vcpu);
1343 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
1344
1345 r = mmu_topup_memory_caches(vcpu);
1346 if (r)
1347 return r;
1348
1349 down_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001350 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1351 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1352 largepage = 1;
1353 }
1354 page = gfn_to_page(vcpu->kvm, gfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001355 if (is_error_page(page)) {
1356 kvm_release_page_clean(page);
1357 up_read(&current->mm->mmap_sem);
1358 return 1;
1359 }
1360 spin_lock(&vcpu->kvm->mmu_lock);
1361 kvm_mmu_free_some_pages(vcpu);
1362 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001363 largepage, gfn, page, TDP_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001364 spin_unlock(&vcpu->kvm->mmu_lock);
1365 up_read(&current->mm->mmap_sem);
1366
1367 return r;
1368}
1369
Avi Kivity6aa8b732006-12-10 02:21:36 -08001370static void nonpaging_free(struct kvm_vcpu *vcpu)
1371{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001372 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001373}
1374
1375static int nonpaging_init_context(struct kvm_vcpu *vcpu)
1376{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001377 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001378
1379 context->new_cr3 = nonpaging_new_cr3;
1380 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001381 context->gva_to_gpa = nonpaging_gva_to_gpa;
1382 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001383 context->prefetch_page = nonpaging_prefetch_page;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001384 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001385 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001386 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001387 return 0;
1388}
1389
Avi Kivityd835dfe2007-11-21 02:57:59 +02001390void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001391{
Avi Kivity1165f5f2007-04-19 17:27:43 +03001392 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001393 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001394}
1395
1396static void paging_new_cr3(struct kvm_vcpu *vcpu)
1397{
Marcelo Tosatti24993d52008-02-14 21:25:39 -02001398 pgprintk("%s: cr3 %lx\n", __FUNCTION__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001399 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001400}
1401
Avi Kivity6aa8b732006-12-10 02:21:36 -08001402static void inject_page_fault(struct kvm_vcpu *vcpu,
1403 u64 addr,
1404 u32 err_code)
1405{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02001406 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001407}
1408
Avi Kivity6aa8b732006-12-10 02:21:36 -08001409static void paging_free(struct kvm_vcpu *vcpu)
1410{
1411 nonpaging_free(vcpu);
1412}
1413
1414#define PTTYPE 64
1415#include "paging_tmpl.h"
1416#undef PTTYPE
1417
1418#define PTTYPE 32
1419#include "paging_tmpl.h"
1420#undef PTTYPE
1421
Avi Kivity17ac10a2007-01-05 16:36:40 -08001422static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001423{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001424 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001425
1426 ASSERT(is_pae(vcpu));
1427 context->new_cr3 = paging_new_cr3;
1428 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001429 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02001430 context->prefetch_page = paging64_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001431 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001432 context->root_level = level;
1433 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001434 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001435 return 0;
1436}
1437
Avi Kivity17ac10a2007-01-05 16:36:40 -08001438static int paging64_init_context(struct kvm_vcpu *vcpu)
1439{
1440 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1441}
1442
Avi Kivity6aa8b732006-12-10 02:21:36 -08001443static int paging32_init_context(struct kvm_vcpu *vcpu)
1444{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001445 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001446
1447 context->new_cr3 = paging_new_cr3;
1448 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001449 context->gva_to_gpa = paging32_gva_to_gpa;
1450 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001451 context->prefetch_page = paging32_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001452 context->root_level = PT32_ROOT_LEVEL;
1453 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001454 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001455 return 0;
1456}
1457
1458static int paging32E_init_context(struct kvm_vcpu *vcpu)
1459{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001460 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001461}
1462
Joerg Roedelfb72d162008-02-07 13:47:44 +01001463static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
1464{
1465 struct kvm_mmu *context = &vcpu->arch.mmu;
1466
1467 context->new_cr3 = nonpaging_new_cr3;
1468 context->page_fault = tdp_page_fault;
1469 context->free = nonpaging_free;
1470 context->prefetch_page = nonpaging_prefetch_page;
1471 context->shadow_root_level = TDP_ROOT_LEVEL;
1472 context->root_hpa = INVALID_PAGE;
1473
1474 if (!is_paging(vcpu)) {
1475 context->gva_to_gpa = nonpaging_gva_to_gpa;
1476 context->root_level = 0;
1477 } else if (is_long_mode(vcpu)) {
1478 context->gva_to_gpa = paging64_gva_to_gpa;
1479 context->root_level = PT64_ROOT_LEVEL;
1480 } else if (is_pae(vcpu)) {
1481 context->gva_to_gpa = paging64_gva_to_gpa;
1482 context->root_level = PT32E_ROOT_LEVEL;
1483 } else {
1484 context->gva_to_gpa = paging32_gva_to_gpa;
1485 context->root_level = PT32_ROOT_LEVEL;
1486 }
1487
1488 return 0;
1489}
1490
1491static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001492{
1493 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001494 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001495
1496 if (!is_paging(vcpu))
1497 return nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08001498 else if (is_long_mode(vcpu))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001499 return paging64_init_context(vcpu);
1500 else if (is_pae(vcpu))
1501 return paging32E_init_context(vcpu);
1502 else
1503 return paging32_init_context(vcpu);
1504}
1505
Joerg Roedelfb72d162008-02-07 13:47:44 +01001506static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1507{
1508 if (tdp_enabled)
1509 return init_kvm_tdp_mmu(vcpu);
1510 else
1511 return init_kvm_softmmu(vcpu);
1512}
1513
Avi Kivity6aa8b732006-12-10 02:21:36 -08001514static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1515{
1516 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001517 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
1518 vcpu->arch.mmu.free(vcpu);
1519 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001520 }
1521}
1522
1523int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1524{
Avi Kivity17c3ba92007-06-04 15:58:30 +03001525 destroy_kvm_mmu(vcpu);
1526 return init_kvm_mmu(vcpu);
1527}
Eddie Dong8668a3c2007-10-10 14:26:45 +08001528EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001529
1530int kvm_mmu_load(struct kvm_vcpu *vcpu)
1531{
Avi Kivity714b93d2007-01-05 16:36:53 -08001532 int r;
1533
Avi Kivitye2dec932007-01-05 16:36:54 -08001534 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001535 if (r)
1536 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001537 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02001538 kvm_mmu_free_some_pages(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001539 mmu_alloc_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001540 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001541 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001542 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001543out:
1544 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001545}
Avi Kivity17c3ba92007-06-04 15:58:30 +03001546EXPORT_SYMBOL_GPL(kvm_mmu_load);
1547
1548void kvm_mmu_unload(struct kvm_vcpu *vcpu)
1549{
1550 mmu_free_roots(vcpu);
1551}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001552
Avi Kivity09072da2007-05-01 14:16:52 +03001553static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02001554 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02001555 u64 *spte)
1556{
1557 u64 pte;
1558 struct kvm_mmu_page *child;
1559
1560 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02001561 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001562 if (sp->role.level == PT_PAGE_TABLE_LEVEL ||
1563 is_large_pte(pte))
Izik Eidus290fc382007-09-27 14:11:22 +02001564 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001565 else {
1566 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03001567 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001568 }
1569 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001570 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001571 if (is_large_pte(pte))
1572 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02001573}
1574
Avi Kivity00284252007-05-01 16:53:31 +03001575static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02001576 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03001577 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02001578 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03001579{
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001580 if ((sp->role.level != PT_PAGE_TABLE_LEVEL)
1581 && !vcpu->arch.update_pte.largepage) {
Avi Kivity4cee5762007-11-18 16:37:07 +02001582 ++vcpu->kvm->stat.mmu_pde_zapped;
Avi Kivity00284252007-05-01 16:53:31 +03001583 return;
Avi Kivity4cee5762007-11-18 16:37:07 +02001584 }
Avi Kivity00284252007-05-01 16:53:31 +03001585
Avi Kivity4cee5762007-11-18 16:37:07 +02001586 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02001587 if (sp->role.glevels == PT32_ROOT_LEVEL)
Dong, Eddie489f1d62008-01-07 11:14:20 +02001588 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03001589 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02001590 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03001591}
1592
Avi Kivity79539ce2007-11-21 02:06:21 +02001593static bool need_remote_flush(u64 old, u64 new)
1594{
1595 if (!is_shadow_present_pte(old))
1596 return false;
1597 if (!is_shadow_present_pte(new))
1598 return true;
1599 if ((old ^ new) & PT64_BASE_ADDR_MASK)
1600 return true;
1601 old ^= PT64_NX_MASK;
1602 new ^= PT64_NX_MASK;
1603 return (old & ~new & PT64_PERM_MASK) != 0;
1604}
1605
1606static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
1607{
1608 if (need_remote_flush(old, new))
1609 kvm_flush_remote_tlbs(vcpu->kvm);
1610 else
1611 kvm_mmu_flush_tlb(vcpu);
1612}
1613
Avi Kivity12b7d282007-09-23 14:10:49 +02001614static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
1615{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001616 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02001617
1618 return !!(spte && (*spte & PT_ACCESSED_MASK));
1619}
1620
Avi Kivityd7824ff2007-12-30 12:29:05 +02001621static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1622 const u8 *new, int bytes)
1623{
1624 gfn_t gfn;
1625 int r;
1626 u64 gpte = 0;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001627 struct page *page;
Avi Kivityd7824ff2007-12-30 12:29:05 +02001628
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001629 vcpu->arch.update_pte.largepage = 0;
1630
Avi Kivityd7824ff2007-12-30 12:29:05 +02001631 if (bytes != 4 && bytes != 8)
1632 return;
1633
1634 /*
1635 * Assume that the pte write on a page table of the same type
1636 * as the current vcpu paging mode. This is nearly always true
1637 * (might be false while changing modes). Note it is verified later
1638 * by update_pte().
1639 */
1640 if (is_pae(vcpu)) {
1641 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
1642 if ((bytes == 4) && (gpa % 4 == 0)) {
1643 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
1644 if (r)
1645 return;
1646 memcpy((void *)&gpte + (gpa % 8), new, 4);
1647 } else if ((bytes == 8) && (gpa % 8 == 0)) {
1648 memcpy((void *)&gpte, new, 8);
1649 }
1650 } else {
1651 if ((bytes == 4) && (gpa % 4 == 0))
1652 memcpy((void *)&gpte, new, 4);
1653 }
1654 if (!is_present_pte(gpte))
1655 return;
1656 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001657
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001658 down_read(&current->mm->mmap_sem);
1659 if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) {
1660 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1661 vcpu->arch.update_pte.largepage = 1;
1662 }
Izik Eidus72dc67a2008-02-10 18:04:15 +02001663 page = gfn_to_page(vcpu->kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001664 up_read(&current->mm->mmap_sem);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001665
Avi Kivityd196e342008-01-24 11:44:11 +02001666 if (is_error_page(page)) {
1667 kvm_release_page_clean(page);
1668 return;
1669 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02001670 vcpu->arch.update_pte.gfn = gfn;
Avi Kivitye48bb492008-03-23 14:21:08 +02001671 vcpu->arch.update_pte.page = page;
Avi Kivityd7824ff2007-12-30 12:29:05 +02001672}
1673
Avi Kivity09072da2007-05-01 14:16:52 +03001674void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Shaohua Life551882007-07-23 14:51:39 +08001675 const u8 *new, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001676{
Avi Kivity9b7a0322007-01-05 16:36:45 -08001677 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02001678 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001679 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001680 struct hlist_head *bucket;
1681 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02001682 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001683 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001684 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001685 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001686 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001687 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03001688 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001689 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001690 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02001691 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02001692 int r;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001693
Avi Kivityda4a00f2007-01-05 16:36:44 -08001694 pgprintk("%s: gpa %llx bytes %d\n", __FUNCTION__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02001695 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001696 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02001697 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02001698 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02001699 kvm_mmu_audit(vcpu, "pre pte write");
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001700 if (gfn == vcpu->arch.last_pt_write_gfn
Avi Kivity12b7d282007-09-23 14:10:49 +02001701 && !last_updated_pte_accessed(vcpu)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001702 ++vcpu->arch.last_pt_write_count;
1703 if (vcpu->arch.last_pt_write_count >= 3)
Avi Kivity86a5ba02007-01-05 16:36:50 -08001704 flooded = 1;
1705 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001706 vcpu->arch.last_pt_write_gfn = gfn;
1707 vcpu->arch.last_pt_write_count = 1;
1708 vcpu->arch.last_pte_updated = NULL;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001709 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001710 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001711 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001712 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
1713 if (sp->gfn != gfn || sp->role.metaphysical)
Avi Kivity9b7a0322007-01-05 16:36:45 -08001714 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02001715 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001716 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03001717 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001718 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001719 /*
1720 * Misaligned accesses are too much trouble to fix
1721 * up; also, they usually indicate a page is not used
1722 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08001723 *
1724 * If we're seeing too many writes to a page,
1725 * it may no longer be a page table, or we may be
1726 * forking, in which case it is better to unmap the
1727 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001728 */
1729 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02001730 gpa, bytes, sp->role.word);
1731 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02001732 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001733 continue;
1734 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001735 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02001736 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02001737 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001738 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001739 page_offset <<= 1; /* 32->64 */
1740 /*
1741 * A 32-bit pde maps 4MB while the shadow pdes map
1742 * only 2MB. So we need to double the offset again
1743 * and zap two pdes instead of one.
1744 */
1745 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03001746 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02001747 page_offset <<= 1;
1748 npte = 2;
1749 }
Avi Kivityfce06572007-05-01 16:44:05 +03001750 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001751 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001752 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03001753 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001754 }
Avi Kivity4db35312007-11-21 15:28:32 +02001755 spte = &sp->spt[page_offset / sizeof(*spte)];
Dong, Eddie489f1d62008-01-07 11:14:20 +02001756 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
1757 gentry = 0;
1758 r = kvm_read_guest_atomic(vcpu->kvm,
1759 gpa & ~(u64)(pte_size - 1),
1760 &gentry, pte_size);
1761 new = (const void *)&gentry;
1762 if (r < 0)
1763 new = NULL;
1764 }
Avi Kivityac1b7142007-03-08 17:13:32 +02001765 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02001766 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02001767 mmu_pte_write_zap_pte(vcpu, sp, spte);
Dong, Eddie489f1d62008-01-07 11:14:20 +02001768 if (new)
1769 mmu_pte_write_new_pte(vcpu, sp, spte, new);
Avi Kivity79539ce2007-11-21 02:06:21 +02001770 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001771 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001772 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001773 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001774 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001775 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivityd7824ff2007-12-30 12:29:05 +02001776 if (vcpu->arch.update_pte.page) {
1777 kvm_release_page_clean(vcpu->arch.update_pte.page);
1778 vcpu->arch.update_pte.page = NULL;
1779 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08001780}
1781
Avi Kivitya4360362007-01-05 16:36:45 -08001782int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1783{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001784 gpa_t gpa;
1785 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08001786
Izik Eidus72dc67a2008-02-10 18:04:15 +02001787 down_read(&vcpu->kvm->slots_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001788 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001789 up_read(&vcpu->kvm->slots_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001790
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001791 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001792 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001793 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001794 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08001795}
1796
Avi Kivity22d95b12007-09-14 20:26:06 +03001797void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08001798{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001799 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
Avi Kivity4db35312007-11-21 15:28:32 +02001800 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08001801
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001802 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02001803 struct kvm_mmu_page, link);
1804 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02001805 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08001806 }
1807}
Avi Kivityebeace82007-01-05 16:36:47 -08001808
Avi Kivity30677142007-10-28 18:48:59 +02001809int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
1810{
1811 int r;
1812 enum emulation_result er;
1813
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001814 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02001815 if (r < 0)
1816 goto out;
1817
1818 if (!r) {
1819 r = 1;
1820 goto out;
1821 }
1822
Avi Kivityb733bfb2007-10-28 18:52:05 +02001823 r = mmu_topup_memory_caches(vcpu);
1824 if (r)
1825 goto out;
1826
Avi Kivity30677142007-10-28 18:48:59 +02001827 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02001828
1829 switch (er) {
1830 case EMULATE_DONE:
1831 return 1;
1832 case EMULATE_DO_MMIO:
1833 ++vcpu->stat.mmio_exits;
1834 return 0;
1835 case EMULATE_FAIL:
1836 kvm_report_emulation_failure(vcpu, "pagetable");
1837 return 1;
1838 default:
1839 BUG();
1840 }
1841out:
Avi Kivity30677142007-10-28 18:48:59 +02001842 return r;
1843}
1844EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
1845
Joerg Roedel18552672008-02-07 13:47:41 +01001846void kvm_enable_tdp(void)
1847{
1848 tdp_enabled = true;
1849}
1850EXPORT_SYMBOL_GPL(kvm_enable_tdp);
1851
Avi Kivity6aa8b732006-12-10 02:21:36 -08001852static void free_mmu_pages(struct kvm_vcpu *vcpu)
1853{
Avi Kivity4db35312007-11-21 15:28:32 +02001854 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001855
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001856 while (!list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
1857 sp = container_of(vcpu->kvm->arch.active_mmu_pages.next,
Avi Kivity4db35312007-11-21 15:28:32 +02001858 struct kvm_mmu_page, link);
1859 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivityf51234c2007-01-05 16:36:52 -08001860 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001861 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001862}
1863
1864static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
1865{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001866 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001867 int i;
1868
1869 ASSERT(vcpu);
1870
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001871 if (vcpu->kvm->arch.n_requested_mmu_pages)
1872 vcpu->kvm->arch.n_free_mmu_pages =
1873 vcpu->kvm->arch.n_requested_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001874 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001875 vcpu->kvm->arch.n_free_mmu_pages =
1876 vcpu->kvm->arch.n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001877 /*
1878 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1879 * Therefore we need to allocate shadow page tables in the first
1880 * 4GB of memory, which happens to fit the DMA32 zone.
1881 */
1882 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
1883 if (!page)
1884 goto error_1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001885 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001886 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001887 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001888
Avi Kivity6aa8b732006-12-10 02:21:36 -08001889 return 0;
1890
1891error_1:
1892 free_mmu_pages(vcpu);
1893 return -ENOMEM;
1894}
1895
Ingo Molnar8018c272006-12-29 16:50:01 -08001896int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001897{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001898 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001899 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001900
Ingo Molnar8018c272006-12-29 16:50:01 -08001901 return alloc_mmu_pages(vcpu);
1902}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001903
Ingo Molnar8018c272006-12-29 16:50:01 -08001904int kvm_mmu_setup(struct kvm_vcpu *vcpu)
1905{
1906 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001907 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08001908
Ingo Molnar8018c272006-12-29 16:50:01 -08001909 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001910}
1911
1912void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
1913{
1914 ASSERT(vcpu);
1915
1916 destroy_kvm_mmu(vcpu);
1917 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001918 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001919}
1920
Avi Kivity90cb0522007-07-17 13:04:56 +03001921void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001922{
Avi Kivity4db35312007-11-21 15:28:32 +02001923 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001924
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001925 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001926 int i;
1927 u64 *pt;
1928
Avi Kivity4db35312007-11-21 15:28:32 +02001929 if (!test_bit(slot, &sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001930 continue;
1931
Avi Kivity4db35312007-11-21 15:28:32 +02001932 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001933 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1934 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02001935 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001936 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001937 }
1938}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001939
Avi Kivity90cb0522007-07-17 13:04:56 +03001940void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03001941{
Avi Kivity4db35312007-11-21 15:28:32 +02001942 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03001943
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001944 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001945 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Avi Kivity4db35312007-11-21 15:28:32 +02001946 kvm_mmu_zap_page(kvm, sp);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001947 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03001948
Avi Kivity90cb0522007-07-17 13:04:56 +03001949 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03001950}
1951
Avi Kivityb5a33a72007-04-15 16:31:09 +03001952void kvm_mmu_module_exit(void)
1953{
1954 if (pte_chain_cache)
1955 kmem_cache_destroy(pte_chain_cache);
1956 if (rmap_desc_cache)
1957 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03001958 if (mmu_page_header_cache)
1959 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001960}
1961
1962int kvm_mmu_module_init(void)
1963{
1964 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
1965 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09001966 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001967 if (!pte_chain_cache)
1968 goto nomem;
1969 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
1970 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09001971 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001972 if (!rmap_desc_cache)
1973 goto nomem;
1974
Avi Kivityd3d25b02007-05-30 12:34:53 +03001975 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
1976 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09001977 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03001978 if (!mmu_page_header_cache)
1979 goto nomem;
1980
Avi Kivityb5a33a72007-04-15 16:31:09 +03001981 return 0;
1982
1983nomem:
1984 kvm_mmu_module_exit();
1985 return -ENOMEM;
1986}
1987
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08001988/*
1989 * Caculate mmu pages needed for kvm.
1990 */
1991unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
1992{
1993 int i;
1994 unsigned int nr_mmu_pages;
1995 unsigned int nr_pages = 0;
1996
1997 for (i = 0; i < kvm->nmemslots; i++)
1998 nr_pages += kvm->memslots[i].npages;
1999
2000 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2001 nr_mmu_pages = max(nr_mmu_pages,
2002 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2003
2004 return nr_mmu_pages;
2005}
2006
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002007#ifdef AUDIT
2008
2009static const char *audit_msg;
2010
2011static gva_t canonicalize(gva_t gva)
2012{
2013#ifdef CONFIG_X86_64
2014 gva = (long long)(gva << 16) >> 16;
2015#endif
2016 return gva;
2017}
2018
2019static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
2020 gva_t va, int level)
2021{
2022 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
2023 int i;
2024 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
2025
2026 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
2027 u64 ent = pt[i];
2028
Avi Kivityc7addb92007-09-16 18:58:32 +02002029 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002030 continue;
2031
2032 va = canonicalize(va);
Avi Kivityc7addb92007-09-16 18:58:32 +02002033 if (level > 1) {
2034 if (ent == shadow_notrap_nonpresent_pte)
2035 printk(KERN_ERR "audit: (%s) nontrapping pte"
2036 " in nonleaf level: levels %d gva %lx"
2037 " level %d pte %llx\n", audit_msg,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002038 vcpu->arch.mmu.root_level, va, level, ent);
Avi Kivityc7addb92007-09-16 18:58:32 +02002039
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002040 audit_mappings_page(vcpu, ent, va, level - 1);
Avi Kivityc7addb92007-09-16 18:58:32 +02002041 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002042 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
Avi Kivity1d28f5f2007-11-21 15:01:44 +02002043 struct page *page = gpa_to_page(vcpu, gpa);
2044 hpa_t hpa = page_to_phys(page);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002045
Avi Kivityc7addb92007-09-16 18:58:32 +02002046 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002047 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02002048 printk(KERN_ERR "xx audit error: (%s) levels %d"
2049 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002050 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04002051 va, gpa, hpa, ent,
2052 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02002053 else if (ent == shadow_notrap_nonpresent_pte
2054 && !is_error_hpa(hpa))
2055 printk(KERN_ERR "audit: (%s) notrap shadow,"
2056 " valid guest gva %lx\n", audit_msg, va);
Izik Eidusb4231d62007-11-20 11:49:33 +02002057 kvm_release_page_clean(page);
Avi Kivityc7addb92007-09-16 18:58:32 +02002058
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002059 }
2060 }
2061}
2062
2063static void audit_mappings(struct kvm_vcpu *vcpu)
2064{
Avi Kivity1ea252a2007-03-08 11:48:09 +02002065 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002066
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002067 if (vcpu->arch.mmu.root_level == 4)
2068 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002069 else
2070 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002071 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002072 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002073 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002074 i << 30,
2075 2);
2076}
2077
2078static int count_rmaps(struct kvm_vcpu *vcpu)
2079{
2080 int nmaps = 0;
2081 int i, j, k;
2082
2083 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
2084 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
2085 struct kvm_rmap_desc *d;
2086
2087 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02002088 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002089
Izik Eidus290fc382007-09-27 14:11:22 +02002090 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002091 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02002092 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002093 ++nmaps;
2094 continue;
2095 }
Izik Eidus290fc382007-09-27 14:11:22 +02002096 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002097 while (d) {
2098 for (k = 0; k < RMAP_EXT; ++k)
2099 if (d->shadow_ptes[k])
2100 ++nmaps;
2101 else
2102 break;
2103 d = d->more;
2104 }
2105 }
2106 }
2107 return nmaps;
2108}
2109
2110static int count_writable_mappings(struct kvm_vcpu *vcpu)
2111{
2112 int nmaps = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02002113 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002114 int i;
2115
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002116 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02002117 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002118
Avi Kivity4db35312007-11-21 15:28:32 +02002119 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002120 continue;
2121
2122 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
2123 u64 ent = pt[i];
2124
2125 if (!(ent & PT_PRESENT_MASK))
2126 continue;
2127 if (!(ent & PT_WRITABLE_MASK))
2128 continue;
2129 ++nmaps;
2130 }
2131 }
2132 return nmaps;
2133}
2134
2135static void audit_rmap(struct kvm_vcpu *vcpu)
2136{
2137 int n_rmap = count_rmaps(vcpu);
2138 int n_actual = count_writable_mappings(vcpu);
2139
2140 if (n_rmap != n_actual)
2141 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
2142 __FUNCTION__, audit_msg, n_rmap, n_actual);
2143}
2144
2145static void audit_write_protection(struct kvm_vcpu *vcpu)
2146{
Avi Kivity4db35312007-11-21 15:28:32 +02002147 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02002148 struct kvm_memory_slot *slot;
2149 unsigned long *rmapp;
2150 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002151
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002152 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02002153 if (sp->role.metaphysical)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002154 continue;
2155
Avi Kivity4db35312007-11-21 15:28:32 +02002156 slot = gfn_to_memslot(vcpu->kvm, sp->gfn);
2157 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02002158 rmapp = &slot->rmap[gfn - slot->base_gfn];
2159 if (*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002160 printk(KERN_ERR "%s: (%s) shadow page has writable"
2161 " mappings: gfn %lx role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02002162 __FUNCTION__, audit_msg, sp->gfn,
2163 sp->role.word);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002164 }
2165}
2166
2167static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
2168{
2169 int olddbg = dbg;
2170
2171 dbg = 0;
2172 audit_msg = msg;
2173 audit_rmap(vcpu);
2174 audit_write_protection(vcpu);
2175 audit_mappings(vcpu);
2176 dbg = olddbg;
2177}
2178
2179#endif