blob: b82abee78f1769f63be393f86ec646f8ae326705 [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>
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050031#include <linux/compiler.h>
Avi Kivitye4956062007-06-28 14:15:57 -040032
33#include <asm/page.h>
34#include <asm/cmpxchg.h>
Avi Kivity4e542372007-11-21 14:08:40 +020035#include <asm/io.h>
Avi Kivitye4956062007-06-28 14:15:57 -040036
Joerg Roedel18552672008-02-07 13:47:41 +010037/*
38 * When setting this variable to true it enables Two-Dimensional-Paging
39 * where the hardware walks 2 page tables:
40 * 1. the guest-virtual to guest-physical
41 * 2. while doing 1. it walks guest-physical to host-physical
42 * If the hardware supports that we don't need to do shadow paging.
43 */
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050044bool tdp_enabled = false;
Joerg Roedel18552672008-02-07 13:47:41 +010045
Avi Kivity37a7d8b2007-01-05 16:36:56 -080046#undef MMU_DEBUG
47
48#undef AUDIT
49
50#ifdef AUDIT
51static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
52#else
53static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
54#endif
55
56#ifdef MMU_DEBUG
57
58#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
59#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
60
61#else
62
63#define pgprintk(x...) do { } while (0)
64#define rmap_printk(x...) do { } while (0)
65
66#endif
67
68#if defined(MMU_DEBUG) || defined(AUDIT)
Avi Kivity6ada8cc2008-06-22 16:45:24 +030069static int dbg = 0;
70module_param(dbg, bool, 0644);
Avi Kivity37a7d8b2007-01-05 16:36:56 -080071#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080072
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080073#ifndef MMU_DEBUG
74#define ASSERT(x) do { } while (0)
75#else
Avi Kivity6aa8b732006-12-10 02:21:36 -080076#define ASSERT(x) \
77 if (!(x)) { \
78 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
79 __FILE__, __LINE__, #x); \
80 }
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080081#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080082
Avi Kivity6aa8b732006-12-10 02:21:36 -080083#define PT_FIRST_AVAIL_BITS_SHIFT 9
84#define PT64_SECOND_AVAIL_BITS_SHIFT 52
85
Avi Kivity6aa8b732006-12-10 02:21:36 -080086#define VALID_PAGE(x) ((x) != INVALID_PAGE)
87
88#define PT64_LEVEL_BITS 9
89
90#define PT64_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -040091 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -080092
93#define PT64_LEVEL_MASK(level) \
94 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
95
96#define PT64_INDEX(address, level)\
97 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
98
99
100#define PT32_LEVEL_BITS 10
101
102#define PT32_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400103 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800104
105#define PT32_LEVEL_MASK(level) \
106 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
107
108#define PT32_INDEX(address, level)\
109 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
110
111
Avi Kivity27aba762007-03-09 13:04:31 +0200112#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800113#define PT64_DIR_BASE_ADDR_MASK \
114 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
115
116#define PT32_BASE_ADDR_MASK PAGE_MASK
117#define PT32_DIR_BASE_ADDR_MASK \
118 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
119
Avi Kivity79539ce2007-11-21 02:06:21 +0200120#define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
121 | PT64_NX_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800122
123#define PFERR_PRESENT_MASK (1U << 0)
124#define PFERR_WRITE_MASK (1U << 1)
125#define PFERR_USER_MASK (1U << 2)
Avi Kivity73b10872007-01-26 00:56:41 -0800126#define PFERR_FETCH_MASK (1U << 4)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800127
Avi Kivity6aa8b732006-12-10 02:21:36 -0800128#define PT_DIRECTORY_LEVEL 2
129#define PT_PAGE_TABLE_LEVEL 1
130
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800131#define RMAP_EXT 4
132
Avi Kivityfe135d22007-12-09 16:15:46 +0200133#define ACC_EXEC_MASK 1
134#define ACC_WRITE_MASK PT_WRITABLE_MASK
135#define ACC_USER_MASK PT_USER_MASK
136#define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
137
Avi Kivity135f8c22008-08-21 17:49:56 +0300138#define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
139
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800140struct kvm_rmap_desc {
141 u64 *shadow_ptes[RMAP_EXT];
142 struct kvm_rmap_desc *more;
143};
144
Avi Kivity3d000db2008-08-22 19:24:38 +0300145struct kvm_shadow_walk {
146 int (*entry)(struct kvm_shadow_walk *walk, struct kvm_vcpu *vcpu,
Sheng Yangd40a1ee2008-09-01 19:41:20 +0800147 u64 addr, u64 *spte, int level);
Avi Kivity3d000db2008-08-22 19:24:38 +0300148};
149
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300150typedef int (*mmu_parent_walk_fn) (struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp);
151
Avi Kivityb5a33a72007-04-15 16:31:09 +0300152static struct kmem_cache *pte_chain_cache;
153static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300154static struct kmem_cache *mmu_page_header_cache;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300155
Avi Kivityc7addb92007-09-16 18:58:32 +0200156static u64 __read_mostly shadow_trap_nonpresent_pte;
157static u64 __read_mostly shadow_notrap_nonpresent_pte;
Sheng Yang7b523452008-04-25 21:13:50 +0800158static u64 __read_mostly shadow_base_present_pte;
159static u64 __read_mostly shadow_nx_mask;
160static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
161static u64 __read_mostly shadow_user_mask;
162static u64 __read_mostly shadow_accessed_mask;
163static u64 __read_mostly shadow_dirty_mask;
Avi Kivityc7addb92007-09-16 18:58:32 +0200164
165void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
166{
167 shadow_trap_nonpresent_pte = trap_pte;
168 shadow_notrap_nonpresent_pte = notrap_pte;
169}
170EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
171
Sheng Yang7b523452008-04-25 21:13:50 +0800172void kvm_mmu_set_base_ptes(u64 base_pte)
173{
174 shadow_base_present_pte = base_pte;
175}
176EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes);
177
178void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
179 u64 dirty_mask, u64 nx_mask, u64 x_mask)
180{
181 shadow_user_mask = user_mask;
182 shadow_accessed_mask = accessed_mask;
183 shadow_dirty_mask = dirty_mask;
184 shadow_nx_mask = nx_mask;
185 shadow_x_mask = x_mask;
186}
187EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_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{
Sheng Yang7b523452008-04-25 21:13:50 +0800227 return pte & shadow_dirty_mask;
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200228}
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
Anthony Liguori35149e22008-04-02 14:46:56 -0500235static pfn_t spte_to_pfn(u64 pte)
Avi Kivity0b49ea82008-03-23 15:06:23 +0200236{
Anthony Liguori35149e22008-04-02 14:46:56 -0500237 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity0b49ea82008-03-23 15:06:23 +0200238}
239
Avi Kivityda928522007-11-21 13:54:47 +0200240static gfn_t pse36_gfn_delta(u32 gpte)
241{
242 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
243
244 return (gpte & PT32_DIR_PSE36_MASK) << shift;
245}
246
Avi Kivitye663ee62007-05-31 15:46:04 +0300247static void set_shadow_pte(u64 *sptep, u64 spte)
248{
249#ifdef CONFIG_X86_64
250 set_64bit((unsigned long *)sptep, spte);
251#else
252 set_64bit((unsigned long long *)sptep, spte);
253#endif
254}
255
Avi Kivitye2dec932007-01-05 16:36:54 -0800256static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300257 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800258{
259 void *obj;
260
261 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800262 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800263 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300264 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800265 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800266 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800267 cache->objects[cache->nobjs++] = obj;
268 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800269 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800270}
271
272static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
273{
274 while (mc->nobjs)
275 kfree(mc->objects[--mc->nobjs]);
276}
277
Avi Kivityc1158e62007-07-20 08:18:27 +0300278static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300279 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300280{
281 struct page *page;
282
283 if (cache->nobjs >= min)
284 return 0;
285 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300286 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300287 if (!page)
288 return -ENOMEM;
289 set_page_private(page, 0);
290 cache->objects[cache->nobjs++] = page_address(page);
291 }
292 return 0;
293}
294
295static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
296{
297 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300298 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300299}
300
Avi Kivity8c438502007-04-16 11:53:17 +0300301static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
302{
303 int r;
304
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800305 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300306 pte_chain_cache, 4);
307 if (r)
308 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800309 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300310 rmap_desc_cache, 1);
311 if (r)
312 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800313 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300314 if (r)
315 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800316 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300317 mmu_page_header_cache, 4);
318out:
Avi Kivity8c438502007-04-16 11:53:17 +0300319 return r;
320}
321
Avi Kivity714b93d2007-01-05 16:36:53 -0800322static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
323{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800324 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
325 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
326 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
327 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800328}
329
330static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
331 size_t size)
332{
333 void *p;
334
335 BUG_ON(!mc->nobjs);
336 p = mc->objects[--mc->nobjs];
337 memset(p, 0, size);
338 return p;
339}
340
Avi Kivity714b93d2007-01-05 16:36:53 -0800341static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
342{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800343 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800344 sizeof(struct kvm_pte_chain));
345}
346
Avi Kivity90cb0522007-07-17 13:04:56 +0300347static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800348{
Avi Kivity90cb0522007-07-17 13:04:56 +0300349 kfree(pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800350}
351
352static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
353{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800354 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800355 sizeof(struct kvm_rmap_desc));
356}
357
Avi Kivity90cb0522007-07-17 13:04:56 +0300358static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800359{
Avi Kivity90cb0522007-07-17 13:04:56 +0300360 kfree(rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800361}
362
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800363/*
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300364 * Return the pointer to the largepage write count for a given
365 * gfn, handling slots that are not large page aligned.
366 */
367static int *slot_largepage_idx(gfn_t gfn, struct kvm_memory_slot *slot)
368{
369 unsigned long idx;
370
371 idx = (gfn / KVM_PAGES_PER_HPAGE) -
372 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
373 return &slot->lpage_info[idx].write_count;
374}
375
376static void account_shadowed(struct kvm *kvm, gfn_t gfn)
377{
378 int *write_count;
379
380 write_count = slot_largepage_idx(gfn, gfn_to_memslot(kvm, gfn));
381 *write_count += 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300382}
383
384static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
385{
386 int *write_count;
387
388 write_count = slot_largepage_idx(gfn, gfn_to_memslot(kvm, gfn));
389 *write_count -= 1;
390 WARN_ON(*write_count < 0);
391}
392
393static int has_wrprotected_page(struct kvm *kvm, gfn_t gfn)
394{
395 struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
396 int *largepage_idx;
397
398 if (slot) {
399 largepage_idx = slot_largepage_idx(gfn, slot);
400 return *largepage_idx;
401 }
402
403 return 1;
404}
405
406static int host_largepage_backed(struct kvm *kvm, gfn_t gfn)
407{
408 struct vm_area_struct *vma;
409 unsigned long addr;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300410 int ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300411
412 addr = gfn_to_hva(kvm, gfn);
413 if (kvm_is_error_hva(addr))
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300414 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300415
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300416 down_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300417 vma = find_vma(current->mm, addr);
418 if (vma && is_vm_hugetlb_page(vma))
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300419 ret = 1;
420 up_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300421
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300422 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300423}
424
425static int is_largepage_backed(struct kvm_vcpu *vcpu, gfn_t large_gfn)
426{
427 struct kvm_memory_slot *slot;
428
429 if (has_wrprotected_page(vcpu->kvm, large_gfn))
430 return 0;
431
432 if (!host_largepage_backed(vcpu->kvm, large_gfn))
433 return 0;
434
435 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
436 if (slot && slot->dirty_bitmap)
437 return 0;
438
439 return 1;
440}
441
442/*
Izik Eidus290fc382007-09-27 14:11:22 +0200443 * Take gfn and return the reverse mapping to it.
444 * Note: gfn must be unaliased before this function get called
445 */
446
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300447static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int lpage)
Izik Eidus290fc382007-09-27 14:11:22 +0200448{
449 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300450 unsigned long idx;
Izik Eidus290fc382007-09-27 14:11:22 +0200451
452 slot = gfn_to_memslot(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300453 if (!lpage)
454 return &slot->rmap[gfn - slot->base_gfn];
455
456 idx = (gfn / KVM_PAGES_PER_HPAGE) -
457 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
458
459 return &slot->lpage_info[idx].rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200460}
461
462/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800463 * Reverse mapping data structures:
464 *
Izik Eidus290fc382007-09-27 14:11:22 +0200465 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
466 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800467 *
Izik Eidus290fc382007-09-27 14:11:22 +0200468 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
469 * containing more mappings.
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800470 */
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300471static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn, int lpage)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800472{
Avi Kivity4db35312007-11-21 15:28:32 +0200473 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800474 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200475 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800476 int i;
477
478 if (!is_rmap_pte(*spte))
479 return;
Izik Eidus290fc382007-09-27 14:11:22 +0200480 gfn = unalias_gfn(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200481 sp = page_header(__pa(spte));
482 sp->gfns[spte - sp->spt] = gfn;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300483 rmapp = gfn_to_rmap(vcpu->kvm, gfn, lpage);
Izik Eidus290fc382007-09-27 14:11:22 +0200484 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800485 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200486 *rmapp = (unsigned long)spte;
487 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800488 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800489 desc = mmu_alloc_rmap_desc(vcpu);
Izik Eidus290fc382007-09-27 14:11:22 +0200490 desc->shadow_ptes[0] = (u64 *)*rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800491 desc->shadow_ptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200492 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800493 } else {
494 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200495 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800496 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
497 desc = desc->more;
498 if (desc->shadow_ptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800499 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800500 desc = desc->more;
501 }
502 for (i = 0; desc->shadow_ptes[i]; ++i)
503 ;
504 desc->shadow_ptes[i] = spte;
505 }
506}
507
Izik Eidus290fc382007-09-27 14:11:22 +0200508static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800509 struct kvm_rmap_desc *desc,
510 int i,
511 struct kvm_rmap_desc *prev_desc)
512{
513 int j;
514
515 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
516 ;
517 desc->shadow_ptes[i] = desc->shadow_ptes[j];
Al Viro11718b4d2007-02-09 16:39:20 +0000518 desc->shadow_ptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800519 if (j != 0)
520 return;
521 if (!prev_desc && !desc->more)
Izik Eidus290fc382007-09-27 14:11:22 +0200522 *rmapp = (unsigned long)desc->shadow_ptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800523 else
524 if (prev_desc)
525 prev_desc->more = desc->more;
526 else
Izik Eidus290fc382007-09-27 14:11:22 +0200527 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300528 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800529}
530
Izik Eidus290fc382007-09-27 14:11:22 +0200531static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800532{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800533 struct kvm_rmap_desc *desc;
534 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200535 struct kvm_mmu_page *sp;
Anthony Liguori35149e22008-04-02 14:46:56 -0500536 pfn_t pfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200537 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800538 int i;
539
540 if (!is_rmap_pte(*spte))
541 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200542 sp = page_header(__pa(spte));
Anthony Liguori35149e22008-04-02 14:46:56 -0500543 pfn = spte_to_pfn(*spte);
Sheng Yang7b523452008-04-25 21:13:50 +0800544 if (*spte & shadow_accessed_mask)
Anthony Liguori35149e22008-04-02 14:46:56 -0500545 kvm_set_pfn_accessed(pfn);
Izik Eidusb4231d62007-11-20 11:49:33 +0200546 if (is_writeble_pte(*spte))
Anthony Liguori35149e22008-04-02 14:46:56 -0500547 kvm_release_pfn_dirty(pfn);
Izik Eidusb4231d62007-11-20 11:49:33 +0200548 else
Anthony Liguori35149e22008-04-02 14:46:56 -0500549 kvm_release_pfn_clean(pfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300550 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], is_large_pte(*spte));
Izik Eidus290fc382007-09-27 14:11:22 +0200551 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800552 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
553 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200554 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800555 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200556 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800557 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
558 spte, *spte);
559 BUG();
560 }
Izik Eidus290fc382007-09-27 14:11:22 +0200561 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800562 } else {
563 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200564 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800565 prev_desc = NULL;
566 while (desc) {
567 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
568 if (desc->shadow_ptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200569 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800570 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800571 prev_desc);
572 return;
573 }
574 prev_desc = desc;
575 desc = desc->more;
576 }
577 BUG();
578 }
579}
580
Izik Eidus98348e92007-10-16 14:42:30 +0200581static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800582{
Avi Kivity374cbac2007-01-05 16:36:43 -0800583 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200584 struct kvm_rmap_desc *prev_desc;
585 u64 *prev_spte;
586 int i;
587
588 if (!*rmapp)
589 return NULL;
590 else if (!(*rmapp & 1)) {
591 if (!spte)
592 return (u64 *)*rmapp;
593 return NULL;
594 }
595 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
596 prev_desc = NULL;
597 prev_spte = NULL;
598 while (desc) {
599 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) {
600 if (prev_spte == spte)
601 return desc->shadow_ptes[i];
602 prev_spte = desc->shadow_ptes[i];
603 }
604 desc = desc->more;
605 }
606 return NULL;
607}
608
609static void rmap_write_protect(struct kvm *kvm, u64 gfn)
610{
Izik Eidus290fc382007-09-27 14:11:22 +0200611 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800612 u64 *spte;
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800613 int write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800614
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500615 gfn = unalias_gfn(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300616 rmapp = gfn_to_rmap(kvm, gfn, 0);
Avi Kivity374cbac2007-01-05 16:36:43 -0800617
Izik Eidus98348e92007-10-16 14:42:30 +0200618 spte = rmap_next(kvm, rmapp, NULL);
619 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800620 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800621 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800622 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800623 if (is_writeble_pte(*spte)) {
Izik Eidus9647c142007-10-16 14:43:46 +0200624 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800625 write_protected = 1;
626 }
Izik Eidus9647c142007-10-16 14:43:46 +0200627 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800628 }
Izik Eidus855149a2008-03-20 18:17:24 +0200629 if (write_protected) {
Anthony Liguori35149e22008-04-02 14:46:56 -0500630 pfn_t pfn;
Izik Eidus855149a2008-03-20 18:17:24 +0200631
632 spte = rmap_next(kvm, rmapp, NULL);
Anthony Liguori35149e22008-04-02 14:46:56 -0500633 pfn = spte_to_pfn(*spte);
634 kvm_set_pfn_dirty(pfn);
Izik Eidus855149a2008-03-20 18:17:24 +0200635 }
636
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300637 /* check for huge page mappings */
638 rmapp = gfn_to_rmap(kvm, gfn, 1);
639 spte = rmap_next(kvm, rmapp, NULL);
640 while (spte) {
641 BUG_ON(!spte);
642 BUG_ON(!(*spte & PT_PRESENT_MASK));
643 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
644 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
645 if (is_writeble_pte(*spte)) {
646 rmap_remove(kvm, spte);
647 --kvm->stat.lpages;
648 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti6597ca02008-06-08 01:48:53 -0300649 spte = NULL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300650 write_protected = 1;
651 }
652 spte = rmap_next(kvm, rmapp, spte);
653 }
654
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800655 if (write_protected)
656 kvm_flush_remote_tlbs(kvm);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300657
658 account_shadowed(kvm, gfn);
Avi Kivity374cbac2007-01-05 16:36:43 -0800659}
660
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200661static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp)
662{
663 u64 *spte;
664 int need_tlb_flush = 0;
665
666 while ((spte = rmap_next(kvm, rmapp, NULL))) {
667 BUG_ON(!(*spte & PT_PRESENT_MASK));
668 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
669 rmap_remove(kvm, spte);
670 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
671 need_tlb_flush = 1;
672 }
673 return need_tlb_flush;
674}
675
676static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
677 int (*handler)(struct kvm *kvm, unsigned long *rmapp))
678{
679 int i;
680 int retval = 0;
681
682 /*
683 * If mmap_sem isn't taken, we can look the memslots with only
684 * the mmu_lock by skipping over the slots with userspace_addr == 0.
685 */
686 for (i = 0; i < kvm->nmemslots; i++) {
687 struct kvm_memory_slot *memslot = &kvm->memslots[i];
688 unsigned long start = memslot->userspace_addr;
689 unsigned long end;
690
691 /* mmu_lock protects userspace_addr */
692 if (!start)
693 continue;
694
695 end = start + (memslot->npages << PAGE_SHIFT);
696 if (hva >= start && hva < end) {
697 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
698 retval |= handler(kvm, &memslot->rmap[gfn_offset]);
699 retval |= handler(kvm,
700 &memslot->lpage_info[
701 gfn_offset /
702 KVM_PAGES_PER_HPAGE].rmap_pde);
703 }
704 }
705
706 return retval;
707}
708
709int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
710{
711 return kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
712}
713
714static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp)
715{
716 u64 *spte;
717 int young = 0;
718
Sheng Yang534e38b2008-09-08 15:12:30 +0800719 /* always return old for EPT */
720 if (!shadow_accessed_mask)
721 return 0;
722
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200723 spte = rmap_next(kvm, rmapp, NULL);
724 while (spte) {
725 int _young;
726 u64 _spte = *spte;
727 BUG_ON(!(_spte & PT_PRESENT_MASK));
728 _young = _spte & PT_ACCESSED_MASK;
729 if (_young) {
730 young = 1;
731 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
732 }
733 spte = rmap_next(kvm, rmapp, spte);
734 }
735 return young;
736}
737
738int kvm_age_hva(struct kvm *kvm, unsigned long hva)
739{
740 return kvm_handle_hva(kvm, hva, kvm_age_rmapp);
741}
742
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800743#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300744static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800745{
Avi Kivity139bdb22007-01-05 16:36:50 -0800746 u64 *pos;
747 u64 *end;
748
Avi Kivity47ad8e62007-05-06 15:50:58 +0300749 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300750 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800751 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800752 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800753 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800754 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800755 return 1;
756}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800757#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800758
Avi Kivity4db35312007-11-21 15:28:32 +0200759static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800760{
Avi Kivity4db35312007-11-21 15:28:32 +0200761 ASSERT(is_empty_shadow_page(sp->spt));
762 list_del(&sp->link);
763 __free_page(virt_to_page(sp->spt));
764 __free_page(virt_to_page(sp->gfns));
765 kfree(sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800766 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800767}
768
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800769static unsigned kvm_page_table_hashfn(gfn_t gfn)
770{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200771 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800772}
773
Avi Kivity25c0de22007-01-05 16:36:42 -0800774static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
775 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800776{
Avi Kivity4db35312007-11-21 15:28:32 +0200777 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800778
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800779 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
780 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
781 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200782 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800783 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Avi Kivity4db35312007-11-21 15:28:32 +0200784 ASSERT(is_empty_shadow_page(sp->spt));
785 sp->slot_bitmap = 0;
786 sp->multimapped = 0;
787 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800788 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200789 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800790}
791
Avi Kivity714b93d2007-01-05 16:36:53 -0800792static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200793 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800794{
795 struct kvm_pte_chain *pte_chain;
796 struct hlist_node *node;
797 int i;
798
799 if (!parent_pte)
800 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200801 if (!sp->multimapped) {
802 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800803
804 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200805 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800806 return;
807 }
Avi Kivity4db35312007-11-21 15:28:32 +0200808 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800809 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200810 INIT_HLIST_HEAD(&sp->parent_ptes);
811 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800812 pte_chain->parent_ptes[0] = old;
813 }
Avi Kivity4db35312007-11-21 15:28:32 +0200814 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800815 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
816 continue;
817 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
818 if (!pte_chain->parent_ptes[i]) {
819 pte_chain->parent_ptes[i] = parent_pte;
820 return;
821 }
822 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800823 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800824 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200825 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800826 pte_chain->parent_ptes[0] = parent_pte;
827}
828
Avi Kivity4db35312007-11-21 15:28:32 +0200829static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800830 u64 *parent_pte)
831{
832 struct kvm_pte_chain *pte_chain;
833 struct hlist_node *node;
834 int i;
835
Avi Kivity4db35312007-11-21 15:28:32 +0200836 if (!sp->multimapped) {
837 BUG_ON(sp->parent_pte != parent_pte);
838 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800839 return;
840 }
Avi Kivity4db35312007-11-21 15:28:32 +0200841 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800842 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
843 if (!pte_chain->parent_ptes[i])
844 break;
845 if (pte_chain->parent_ptes[i] != parent_pte)
846 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800847 while (i + 1 < NR_PTE_CHAIN_ENTRIES
848 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800849 pte_chain->parent_ptes[i]
850 = pte_chain->parent_ptes[i + 1];
851 ++i;
852 }
853 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800854 if (i == 0) {
855 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300856 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200857 if (hlist_empty(&sp->parent_ptes)) {
858 sp->multimapped = 0;
859 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800860 }
861 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800862 return;
863 }
864 BUG();
865}
866
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300867
868static void mmu_parent_walk(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
869 mmu_parent_walk_fn fn)
870{
871 struct kvm_pte_chain *pte_chain;
872 struct hlist_node *node;
873 struct kvm_mmu_page *parent_sp;
874 int i;
875
876 if (!sp->multimapped && sp->parent_pte) {
877 parent_sp = page_header(__pa(sp->parent_pte));
878 fn(vcpu, parent_sp);
879 mmu_parent_walk(vcpu, parent_sp, fn);
880 return;
881 }
882 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
883 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
884 if (!pte_chain->parent_ptes[i])
885 break;
886 parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
887 fn(vcpu, parent_sp);
888 mmu_parent_walk(vcpu, parent_sp, fn);
889 }
890}
891
Avi Kivityd761a502008-05-29 14:55:03 +0300892static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
893 struct kvm_mmu_page *sp)
894{
895 int i;
896
897 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
898 sp->spt[i] = shadow_trap_nonpresent_pte;
899}
900
Marcelo Tosattie8bc2172008-09-23 13:18:33 -0300901static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
902 struct kvm_mmu_page *sp)
903{
904 return 1;
905}
906
Marcelo Tosattia7052892008-09-23 13:18:35 -0300907static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
908{
909}
910
Avi Kivity4db35312007-11-21 15:28:32 +0200911static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800912{
913 unsigned index;
914 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200915 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800916 struct hlist_node *node;
917
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800918 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200919 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800920 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200921 hlist_for_each_entry(sp, node, bucket, hash_link)
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500922 if (sp->gfn == gfn && !sp->role.metaphysical
923 && !sp->role.invalid) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800924 pgprintk("%s: found role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800925 __func__, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +0200926 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800927 }
928 return NULL;
929}
930
931static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
932 gfn_t gfn,
933 gva_t gaddr,
934 unsigned level,
935 int metaphysical,
Avi Kivity41074d02007-12-09 17:00:02 +0200936 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +0200937 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800938{
939 union kvm_mmu_page_role role;
940 unsigned index;
941 unsigned quadrant;
942 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200943 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800944 struct hlist_node *node;
945
946 role.word = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800947 role.glevels = vcpu->arch.mmu.root_level;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800948 role.level = level;
949 role.metaphysical = metaphysical;
Avi Kivity41074d02007-12-09 17:00:02 +0200950 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800951 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800952 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
953 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
954 role.quadrant = quadrant;
955 }
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800956 pgprintk("%s: looking gfn %lx role %x\n", __func__,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800957 gfn, role.word);
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200958 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800959 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200960 hlist_for_each_entry(sp, node, bucket, hash_link)
961 if (sp->gfn == gfn && sp->role.word == role.word) {
962 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800963 pgprintk("%s: found\n", __func__);
Avi Kivity4db35312007-11-21 15:28:32 +0200964 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800965 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +0200966 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +0200967 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
968 if (!sp)
969 return sp;
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800970 pgprintk("%s: adding gfn %lx role %x\n", __func__, gfn, role.word);
Avi Kivity4db35312007-11-21 15:28:32 +0200971 sp->gfn = gfn;
972 sp->role = role;
973 hlist_add_head(&sp->hash_link, bucket);
Avi Kivity374cbac2007-01-05 16:36:43 -0800974 if (!metaphysical)
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500975 rmap_write_protect(vcpu->kvm, gfn);
Avi Kivity131d8272008-05-29 14:56:28 +0300976 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
977 vcpu->arch.mmu.prefetch_page(vcpu, sp);
978 else
979 nonpaging_prefetch_page(vcpu, sp);
Avi Kivity4db35312007-11-21 15:28:32 +0200980 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800981}
982
Avi Kivity3d000db2008-08-22 19:24:38 +0300983static int walk_shadow(struct kvm_shadow_walk *walker,
Sheng Yangd40a1ee2008-09-01 19:41:20 +0800984 struct kvm_vcpu *vcpu, u64 addr)
Avi Kivity3d000db2008-08-22 19:24:38 +0300985{
986 hpa_t shadow_addr;
987 int level;
988 int r;
989 u64 *sptep;
990 unsigned index;
991
992 shadow_addr = vcpu->arch.mmu.root_hpa;
993 level = vcpu->arch.mmu.shadow_root_level;
994 if (level == PT32E_ROOT_LEVEL) {
995 shadow_addr = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
996 shadow_addr &= PT64_BASE_ADDR_MASK;
997 --level;
998 }
999
1000 while (level >= PT_PAGE_TABLE_LEVEL) {
1001 index = SHADOW_PT_INDEX(addr, level);
1002 sptep = ((u64 *)__va(shadow_addr)) + index;
1003 r = walker->entry(walker, vcpu, addr, sptep, level);
1004 if (r)
1005 return r;
1006 shadow_addr = *sptep & PT64_BASE_ADDR_MASK;
1007 --level;
1008 }
1009 return 0;
1010}
1011
Avi Kivity90cb0522007-07-17 13:04:56 +03001012static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001013 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001014{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001015 unsigned i;
1016 u64 *pt;
1017 u64 ent;
1018
Avi Kivity4db35312007-11-21 15:28:32 +02001019 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001020
Avi Kivity4db35312007-11-21 15:28:32 +02001021 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
Avi Kivity697fe2e2007-01-05 16:36:46 -08001022 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Avi Kivityc7addb92007-09-16 18:58:32 +02001023 if (is_shadow_present_pte(pt[i]))
Izik Eidus290fc382007-09-27 14:11:22 +02001024 rmap_remove(kvm, &pt[i]);
Avi Kivityc7addb92007-09-16 18:58:32 +02001025 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001026 }
1027 return;
1028 }
1029
1030 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1031 ent = pt[i];
1032
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001033 if (is_shadow_present_pte(ent)) {
1034 if (!is_large_pte(ent)) {
1035 ent &= PT64_BASE_ADDR_MASK;
1036 mmu_page_remove_parent_pte(page_header(ent),
1037 &pt[i]);
1038 } else {
1039 --kvm->stat.lpages;
1040 rmap_remove(kvm, &pt[i]);
1041 }
1042 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001043 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001044 }
Avi Kivitya4360362007-01-05 16:36:45 -08001045}
1046
Avi Kivity4db35312007-11-21 15:28:32 +02001047static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001048{
Avi Kivity4db35312007-11-21 15:28:32 +02001049 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001050}
1051
Avi Kivity12b7d282007-09-23 14:10:49 +02001052static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1053{
1054 int i;
1055
1056 for (i = 0; i < KVM_MAX_VCPUS; ++i)
1057 if (kvm->vcpus[i])
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001058 kvm->vcpus[i]->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001059}
1060
Avi Kivity31aa2b42008-07-11 17:59:46 +03001061static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001062{
1063 u64 *parent_pte;
1064
Avi Kivity4db35312007-11-21 15:28:32 +02001065 while (sp->multimapped || sp->parent_pte) {
1066 if (!sp->multimapped)
1067 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001068 else {
1069 struct kvm_pte_chain *chain;
1070
Avi Kivity4db35312007-11-21 15:28:32 +02001071 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001072 struct kvm_pte_chain, link);
1073 parent_pte = chain->parent_ptes[0];
1074 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001075 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001076 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityc7addb92007-09-16 18:58:32 +02001077 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001078 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001079}
1080
1081static void kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1082{
1083 ++kvm->stat.mmu_shadow_zapped;
Avi Kivity4db35312007-11-21 15:28:32 +02001084 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001085 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001086 kvm_flush_remote_tlbs(kvm);
1087 if (!sp->role.invalid && !sp->role.metaphysical)
1088 unaccount_shadowed(kvm, sp->gfn);
Avi Kivity4db35312007-11-21 15:28:32 +02001089 if (!sp->root_count) {
1090 hlist_del(&sp->hash_link);
1091 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001092 } else {
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001093 sp->role.invalid = 1;
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001094 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001095 kvm_reload_remote_mmus(kvm);
1096 }
Avi Kivity12b7d282007-09-23 14:10:49 +02001097 kvm_mmu_reset_last_pte_updated(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -08001098}
1099
Izik Eidus82ce2c92007-10-02 18:52:55 +02001100/*
1101 * Changing the number of mmu pages allocated to the vm
1102 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1103 */
1104void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1105{
1106 /*
1107 * If we set the number of mmu pages to be smaller be than the
1108 * number of actived pages , we must to free some mmu pages before we
1109 * change the value
1110 */
1111
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001112 if ((kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages) >
Izik Eidus82ce2c92007-10-02 18:52:55 +02001113 kvm_nr_mmu_pages) {
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001114 int n_used_mmu_pages = kvm->arch.n_alloc_mmu_pages
1115 - kvm->arch.n_free_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001116
1117 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
1118 struct kvm_mmu_page *page;
1119
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001120 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001121 struct kvm_mmu_page, link);
1122 kvm_mmu_zap_page(kvm, page);
1123 n_used_mmu_pages--;
1124 }
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001125 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001126 }
1127 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001128 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1129 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001130
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001131 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001132}
1133
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001134static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001135{
1136 unsigned index;
1137 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001138 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -08001139 struct hlist_node *node, *n;
1140 int r;
1141
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001142 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001143 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001144 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001145 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001146 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
1147 if (sp->gfn == gfn && !sp->role.metaphysical) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001148 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02001149 sp->role.word);
1150 kvm_mmu_zap_page(kvm, sp);
Avi Kivitya4360362007-01-05 16:36:45 -08001151 r = 1;
1152 }
1153 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001154}
1155
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001156static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001157{
Avi Kivity4db35312007-11-21 15:28:32 +02001158 struct kvm_mmu_page *sp;
Avi Kivity97a0a012007-05-31 15:08:29 +03001159
Avi Kivity4db35312007-11-21 15:28:32 +02001160 while ((sp = kvm_mmu_lookup_page(kvm, gfn)) != NULL) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001161 pgprintk("%s: zap %lx %x\n", __func__, gfn, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001162 kvm_mmu_zap_page(kvm, sp);
Avi Kivity97a0a012007-05-31 15:08:29 +03001163 }
1164}
1165
Avi Kivity38c335f2007-11-21 14:20:22 +02001166static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001167{
Avi Kivity38c335f2007-11-21 14:20:22 +02001168 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity4db35312007-11-21 15:28:32 +02001169 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001170
Avi Kivity4db35312007-11-21 15:28:32 +02001171 __set_bit(slot, &sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001172}
1173
Avi Kivity039576c2007-03-20 12:46:50 +02001174struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1175{
Izik Eidus72dc67a2008-02-10 18:04:15 +02001176 struct page *page;
1177
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001178 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Avi Kivity039576c2007-03-20 12:46:50 +02001179
1180 if (gpa == UNMAPPED_GVA)
1181 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001182
Izik Eidus72dc67a2008-02-10 18:04:15 +02001183 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001184
1185 return page;
Avi Kivity039576c2007-03-20 12:46:50 +02001186}
1187
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001188static int set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1189 unsigned pte_access, int user_fault,
1190 int write_fault, int dirty, int largepage,
1191 gfn_t gfn, pfn_t pfn, bool speculative)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001192{
1193 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001194 int ret = 0;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001195 /*
1196 * We don't set the accessed bit, since we sometimes want to see
1197 * whether the guest actually used the pte (in order to detect
1198 * demand paging).
1199 */
Sheng Yang7b523452008-04-25 21:13:50 +08001200 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001201 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001202 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001203 if (!dirty)
1204 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001205 if (pte_access & ACC_EXEC_MASK)
1206 spte |= shadow_x_mask;
1207 else
1208 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001209 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001210 spte |= shadow_user_mask;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001211 if (largepage)
1212 spte |= PT_PAGE_SIZE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001213
Anthony Liguori35149e22008-04-02 14:46:56 -05001214 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001215
1216 if ((pte_access & ACC_WRITE_MASK)
1217 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
1218 struct kvm_mmu_page *shadow;
1219
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001220 if (largepage && has_wrprotected_page(vcpu->kvm, gfn)) {
1221 ret = 1;
1222 spte = shadow_trap_nonpresent_pte;
1223 goto set_pte;
1224 }
1225
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001226 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001227
1228 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001229 if (shadow) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001230 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001231 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001232 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001233 pte_access &= ~ACC_WRITE_MASK;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001234 if (is_writeble_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001235 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001236 }
1237 }
1238
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001239 if (pte_access & ACC_WRITE_MASK)
1240 mark_page_dirty(vcpu->kvm, gfn);
1241
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001242set_pte:
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001243 set_shadow_pte(shadow_pte, spte);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001244 return ret;
1245}
1246
1247
1248static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1249 unsigned pt_access, unsigned pte_access,
1250 int user_fault, int write_fault, int dirty,
1251 int *ptwrite, int largepage, gfn_t gfn,
1252 pfn_t pfn, bool speculative)
1253{
1254 int was_rmapped = 0;
1255 int was_writeble = is_writeble_pte(*shadow_pte);
1256
1257 pgprintk("%s: spte %llx access %x write_fault %d"
1258 " user_fault %d gfn %lx\n",
1259 __func__, *shadow_pte, pt_access,
1260 write_fault, user_fault, gfn);
1261
1262 if (is_rmap_pte(*shadow_pte)) {
1263 /*
1264 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1265 * the parent of the now unreachable PTE.
1266 */
1267 if (largepage && !is_large_pte(*shadow_pte)) {
1268 struct kvm_mmu_page *child;
1269 u64 pte = *shadow_pte;
1270
1271 child = page_header(pte & PT64_BASE_ADDR_MASK);
1272 mmu_page_remove_parent_pte(child, shadow_pte);
1273 } else if (pfn != spte_to_pfn(*shadow_pte)) {
1274 pgprintk("hfn old %lx new %lx\n",
1275 spte_to_pfn(*shadow_pte), pfn);
1276 rmap_remove(vcpu->kvm, shadow_pte);
1277 } else {
1278 if (largepage)
1279 was_rmapped = is_large_pte(*shadow_pte);
1280 else
1281 was_rmapped = 1;
1282 }
1283 }
1284 if (set_spte(vcpu, shadow_pte, pte_access, user_fault, write_fault,
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001285 dirty, largepage, gfn, pfn, speculative)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001286 if (write_fault)
1287 *ptwrite = 1;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001288 kvm_x86_ops->tlb_flush(vcpu);
1289 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001290
1291 pgprintk("%s: setting spte %llx\n", __func__, *shadow_pte);
1292 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
1293 is_large_pte(*shadow_pte)? "2MB" : "4kB",
1294 is_present_pte(*shadow_pte)?"RW":"R", gfn,
1295 *shadow_pte, shadow_pte);
1296 if (!was_rmapped && is_large_pte(*shadow_pte))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001297 ++vcpu->kvm->stat.lpages;
1298
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001299 page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
1300 if (!was_rmapped) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001301 rmap_add(vcpu, shadow_pte, gfn, largepage);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001302 if (!is_rmap_pte(*shadow_pte))
Anthony Liguori35149e22008-04-02 14:46:56 -05001303 kvm_release_pfn_clean(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001304 } else {
1305 if (was_writeble)
Anthony Liguori35149e22008-04-02 14:46:56 -05001306 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001307 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001308 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001309 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001310 if (speculative) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001311 vcpu->arch.last_pte_updated = shadow_pte;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001312 vcpu->arch.last_pte_gfn = gfn;
1313 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001314}
1315
Avi Kivity6aa8b732006-12-10 02:21:36 -08001316static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1317{
1318}
1319
Avi Kivity140754b2008-08-22 19:28:04 +03001320struct direct_shadow_walk {
1321 struct kvm_shadow_walk walker;
1322 pfn_t pfn;
1323 int write;
1324 int largepage;
1325 int pt_write;
1326};
1327
1328static int direct_map_entry(struct kvm_shadow_walk *_walk,
1329 struct kvm_vcpu *vcpu,
Sheng Yangd40a1ee2008-09-01 19:41:20 +08001330 u64 addr, u64 *sptep, int level)
Avi Kivity140754b2008-08-22 19:28:04 +03001331{
1332 struct direct_shadow_walk *walk =
1333 container_of(_walk, struct direct_shadow_walk, walker);
1334 struct kvm_mmu_page *sp;
1335 gfn_t pseudo_gfn;
1336 gfn_t gfn = addr >> PAGE_SHIFT;
1337
1338 if (level == PT_PAGE_TABLE_LEVEL
1339 || (walk->largepage && level == PT_DIRECTORY_LEVEL)) {
1340 mmu_set_spte(vcpu, sptep, ACC_ALL, ACC_ALL,
1341 0, walk->write, 1, &walk->pt_write,
1342 walk->largepage, gfn, walk->pfn, false);
Avi Kivitybc2d4292008-08-27 16:30:56 +03001343 ++vcpu->stat.pf_fixed;
Avi Kivity140754b2008-08-22 19:28:04 +03001344 return 1;
1345 }
1346
1347 if (*sptep == shadow_trap_nonpresent_pte) {
1348 pseudo_gfn = (addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
Sheng Yangd40a1ee2008-09-01 19:41:20 +08001349 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, (gva_t)addr, level - 1,
Avi Kivity140754b2008-08-22 19:28:04 +03001350 1, ACC_ALL, sptep);
1351 if (!sp) {
1352 pgprintk("nonpaging_map: ENOMEM\n");
1353 kvm_release_pfn_clean(walk->pfn);
1354 return -ENOMEM;
1355 }
1356
1357 set_shadow_pte(sptep,
1358 __pa(sp->spt)
1359 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1360 | shadow_user_mask | shadow_x_mask);
1361 }
1362 return 0;
1363}
1364
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001365static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Avi Kivity6c41f422008-08-26 16:16:08 +03001366 int largepage, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001367{
Avi Kivity140754b2008-08-22 19:28:04 +03001368 int r;
1369 struct direct_shadow_walk walker = {
1370 .walker = { .entry = direct_map_entry, },
1371 .pfn = pfn,
1372 .largepage = largepage,
1373 .write = write,
1374 .pt_write = 0,
1375 };
Avi Kivity6aa8b732006-12-10 02:21:36 -08001376
Sheng Yangd40a1ee2008-09-01 19:41:20 +08001377 r = walk_shadow(&walker.walker, vcpu, gfn << PAGE_SHIFT);
Avi Kivity140754b2008-08-22 19:28:04 +03001378 if (r < 0)
1379 return r;
1380 return walker.pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001381}
1382
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001383static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1384{
1385 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001386 int largepage = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05001387 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001388 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001389
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001390 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1391 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1392 largepage = 1;
1393 }
1394
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001395 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001396 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05001397 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001398
Avi Kivityd196e342008-01-24 11:44:11 +02001399 /* mmio */
Anthony Liguori35149e22008-04-02 14:46:56 -05001400 if (is_error_pfn(pfn)) {
1401 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02001402 return 1;
1403 }
1404
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001405 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001406 if (mmu_notifier_retry(vcpu, mmu_seq))
1407 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02001408 kvm_mmu_free_some_pages(vcpu);
Avi Kivity6c41f422008-08-26 16:16:08 +03001409 r = __direct_map(vcpu, v, write, largepage, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001410 spin_unlock(&vcpu->kvm->mmu_lock);
1411
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001412
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001413 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001414
1415out_unlock:
1416 spin_unlock(&vcpu->kvm->mmu_lock);
1417 kvm_release_pfn_clean(pfn);
1418 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001419}
1420
1421
Avi Kivity17ac10a2007-01-05 16:36:40 -08001422static void mmu_free_roots(struct kvm_vcpu *vcpu)
1423{
1424 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02001425 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001426
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001427 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03001428 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001429 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001430 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1431 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001432
Avi Kivity4db35312007-11-21 15:28:32 +02001433 sp = page_header(root);
1434 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001435 if (!sp->root_count && sp->role.invalid)
1436 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001437 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001438 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001439 return;
1440 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001441 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001442 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001443
Avi Kivity417726a2007-04-12 17:35:58 +03001444 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03001445 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001446 sp = page_header(root);
1447 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001448 if (!sp->root_count && sp->role.invalid)
1449 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03001450 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001451 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001452 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001453 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001454 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001455}
1456
1457static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
1458{
1459 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001460 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02001461 struct kvm_mmu_page *sp;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001462 int metaphysical = 0;
Avi Kivity3bb65a22007-01-05 16:36:51 -08001463
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001464 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001465
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001466 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1467 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001468
1469 ASSERT(!VALID_PAGE(root));
Joerg Roedelfb72d162008-02-07 13:47:44 +01001470 if (tdp_enabled)
1471 metaphysical = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001472 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001473 PT64_ROOT_LEVEL, metaphysical,
1474 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001475 root = __pa(sp->spt);
1476 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001477 vcpu->arch.mmu.root_hpa = root;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001478 return;
1479 }
Joerg Roedelfb72d162008-02-07 13:47:44 +01001480 metaphysical = !is_paging(vcpu);
1481 if (tdp_enabled)
1482 metaphysical = 1;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001483 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001484 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001485
1486 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001487 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
1488 if (!is_present_pte(vcpu->arch.pdptrs[i])) {
1489 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03001490 continue;
1491 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001492 root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT;
1493 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001494 root_gfn = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02001495 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001496 PT32_ROOT_LEVEL, metaphysical,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001497 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001498 root = __pa(sp->spt);
1499 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001500 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001501 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001502 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001503}
1504
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03001505static void mmu_sync_children(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1506{
1507}
1508
1509static void mmu_sync_roots(struct kvm_vcpu *vcpu)
1510{
1511 int i;
1512 struct kvm_mmu_page *sp;
1513
1514 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
1515 return;
1516 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1517 hpa_t root = vcpu->arch.mmu.root_hpa;
1518 sp = page_header(root);
1519 mmu_sync_children(vcpu, sp);
1520 return;
1521 }
1522 for (i = 0; i < 4; ++i) {
1523 hpa_t root = vcpu->arch.mmu.pae_root[i];
1524
1525 if (root) {
1526 root &= PT64_BASE_ADDR_MASK;
1527 sp = page_header(root);
1528 mmu_sync_children(vcpu, sp);
1529 }
1530 }
1531}
1532
1533void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
1534{
1535 spin_lock(&vcpu->kvm->mmu_lock);
1536 mmu_sync_roots(vcpu);
1537 spin_unlock(&vcpu->kvm->mmu_lock);
1538}
1539
Avi Kivity6aa8b732006-12-10 02:21:36 -08001540static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
1541{
1542 return vaddr;
1543}
1544
1545static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02001546 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001547{
Avi Kivitye8332402007-12-09 18:43:00 +02001548 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08001549 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001550
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001551 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08001552 r = mmu_topup_memory_caches(vcpu);
1553 if (r)
1554 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08001555
Avi Kivity6aa8b732006-12-10 02:21:36 -08001556 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001557 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001558
Avi Kivitye8332402007-12-09 18:43:00 +02001559 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001560
Avi Kivitye8332402007-12-09 18:43:00 +02001561 return nonpaging_map(vcpu, gva & PAGE_MASK,
1562 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001563}
1564
Joerg Roedelfb72d162008-02-07 13:47:44 +01001565static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
1566 u32 error_code)
1567{
Anthony Liguori35149e22008-04-02 14:46:56 -05001568 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001569 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001570 int largepage = 0;
1571 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001572 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001573
1574 ASSERT(vcpu);
1575 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
1576
1577 r = mmu_topup_memory_caches(vcpu);
1578 if (r)
1579 return r;
1580
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001581 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1582 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1583 largepage = 1;
1584 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001585 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001586 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05001587 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Anthony Liguori35149e22008-04-02 14:46:56 -05001588 if (is_error_pfn(pfn)) {
1589 kvm_release_pfn_clean(pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001590 return 1;
1591 }
1592 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001593 if (mmu_notifier_retry(vcpu, mmu_seq))
1594 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001595 kvm_mmu_free_some_pages(vcpu);
1596 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Avi Kivity6c41f422008-08-26 16:16:08 +03001597 largepage, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001598 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001599
1600 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001601
1602out_unlock:
1603 spin_unlock(&vcpu->kvm->mmu_lock);
1604 kvm_release_pfn_clean(pfn);
1605 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001606}
1607
Avi Kivity6aa8b732006-12-10 02:21:36 -08001608static void nonpaging_free(struct kvm_vcpu *vcpu)
1609{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001610 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001611}
1612
1613static int nonpaging_init_context(struct kvm_vcpu *vcpu)
1614{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001615 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001616
1617 context->new_cr3 = nonpaging_new_cr3;
1618 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001619 context->gva_to_gpa = nonpaging_gva_to_gpa;
1620 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001621 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001622 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03001623 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001624 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001625 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001626 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001627 return 0;
1628}
1629
Avi Kivityd835dfe2007-11-21 02:57:59 +02001630void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001631{
Avi Kivity1165f5f2007-04-19 17:27:43 +03001632 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001633 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001634}
1635
1636static void paging_new_cr3(struct kvm_vcpu *vcpu)
1637{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001638 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001639 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001640}
1641
Avi Kivity6aa8b732006-12-10 02:21:36 -08001642static void inject_page_fault(struct kvm_vcpu *vcpu,
1643 u64 addr,
1644 u32 err_code)
1645{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02001646 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001647}
1648
Avi Kivity6aa8b732006-12-10 02:21:36 -08001649static void paging_free(struct kvm_vcpu *vcpu)
1650{
1651 nonpaging_free(vcpu);
1652}
1653
1654#define PTTYPE 64
1655#include "paging_tmpl.h"
1656#undef PTTYPE
1657
1658#define PTTYPE 32
1659#include "paging_tmpl.h"
1660#undef PTTYPE
1661
Avi Kivity17ac10a2007-01-05 16:36:40 -08001662static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001663{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001664 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001665
1666 ASSERT(is_pae(vcpu));
1667 context->new_cr3 = paging_new_cr3;
1668 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001669 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02001670 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001671 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03001672 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001673 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001674 context->root_level = level;
1675 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001676 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001677 return 0;
1678}
1679
Avi Kivity17ac10a2007-01-05 16:36:40 -08001680static int paging64_init_context(struct kvm_vcpu *vcpu)
1681{
1682 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1683}
1684
Avi Kivity6aa8b732006-12-10 02:21:36 -08001685static int paging32_init_context(struct kvm_vcpu *vcpu)
1686{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001687 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001688
1689 context->new_cr3 = paging_new_cr3;
1690 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001691 context->gva_to_gpa = paging32_gva_to_gpa;
1692 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001693 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001694 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03001695 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001696 context->root_level = PT32_ROOT_LEVEL;
1697 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001698 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001699 return 0;
1700}
1701
1702static int paging32E_init_context(struct kvm_vcpu *vcpu)
1703{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001704 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001705}
1706
Joerg Roedelfb72d162008-02-07 13:47:44 +01001707static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
1708{
1709 struct kvm_mmu *context = &vcpu->arch.mmu;
1710
1711 context->new_cr3 = nonpaging_new_cr3;
1712 context->page_fault = tdp_page_fault;
1713 context->free = nonpaging_free;
1714 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03001715 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03001716 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08001717 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01001718 context->root_hpa = INVALID_PAGE;
1719
1720 if (!is_paging(vcpu)) {
1721 context->gva_to_gpa = nonpaging_gva_to_gpa;
1722 context->root_level = 0;
1723 } else if (is_long_mode(vcpu)) {
1724 context->gva_to_gpa = paging64_gva_to_gpa;
1725 context->root_level = PT64_ROOT_LEVEL;
1726 } else if (is_pae(vcpu)) {
1727 context->gva_to_gpa = paging64_gva_to_gpa;
1728 context->root_level = PT32E_ROOT_LEVEL;
1729 } else {
1730 context->gva_to_gpa = paging32_gva_to_gpa;
1731 context->root_level = PT32_ROOT_LEVEL;
1732 }
1733
1734 return 0;
1735}
1736
1737static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001738{
1739 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001740 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001741
1742 if (!is_paging(vcpu))
1743 return nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08001744 else if (is_long_mode(vcpu))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001745 return paging64_init_context(vcpu);
1746 else if (is_pae(vcpu))
1747 return paging32E_init_context(vcpu);
1748 else
1749 return paging32_init_context(vcpu);
1750}
1751
Joerg Roedelfb72d162008-02-07 13:47:44 +01001752static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1753{
Anthony Liguori35149e22008-04-02 14:46:56 -05001754 vcpu->arch.update_pte.pfn = bad_pfn;
1755
Joerg Roedelfb72d162008-02-07 13:47:44 +01001756 if (tdp_enabled)
1757 return init_kvm_tdp_mmu(vcpu);
1758 else
1759 return init_kvm_softmmu(vcpu);
1760}
1761
Avi Kivity6aa8b732006-12-10 02:21:36 -08001762static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1763{
1764 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001765 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
1766 vcpu->arch.mmu.free(vcpu);
1767 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001768 }
1769}
1770
1771int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1772{
Avi Kivity17c3ba92007-06-04 15:58:30 +03001773 destroy_kvm_mmu(vcpu);
1774 return init_kvm_mmu(vcpu);
1775}
Eddie Dong8668a3c2007-10-10 14:26:45 +08001776EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001777
1778int kvm_mmu_load(struct kvm_vcpu *vcpu)
1779{
Avi Kivity714b93d2007-01-05 16:36:53 -08001780 int r;
1781
Avi Kivitye2dec932007-01-05 16:36:54 -08001782 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001783 if (r)
1784 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001785 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02001786 kvm_mmu_free_some_pages(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001787 mmu_alloc_roots(vcpu);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03001788 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001789 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001790 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001791 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001792out:
1793 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001794}
Avi Kivity17c3ba92007-06-04 15:58:30 +03001795EXPORT_SYMBOL_GPL(kvm_mmu_load);
1796
1797void kvm_mmu_unload(struct kvm_vcpu *vcpu)
1798{
1799 mmu_free_roots(vcpu);
1800}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001801
Avi Kivity09072da2007-05-01 14:16:52 +03001802static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02001803 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02001804 u64 *spte)
1805{
1806 u64 pte;
1807 struct kvm_mmu_page *child;
1808
1809 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02001810 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001811 if (sp->role.level == PT_PAGE_TABLE_LEVEL ||
1812 is_large_pte(pte))
Izik Eidus290fc382007-09-27 14:11:22 +02001813 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001814 else {
1815 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03001816 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001817 }
1818 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001819 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001820 if (is_large_pte(pte))
1821 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02001822}
1823
Avi Kivity00284252007-05-01 16:53:31 +03001824static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02001825 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03001826 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02001827 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03001828{
Marcelo Tosatti30945382008-06-11 20:32:40 -03001829 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
1830 if (!vcpu->arch.update_pte.largepage ||
1831 sp->role.glevels == PT32_ROOT_LEVEL) {
1832 ++vcpu->kvm->stat.mmu_pde_zapped;
1833 return;
1834 }
1835 }
Avi Kivity00284252007-05-01 16:53:31 +03001836
Avi Kivity4cee5762007-11-18 16:37:07 +02001837 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02001838 if (sp->role.glevels == PT32_ROOT_LEVEL)
Dong, Eddie489f1d62008-01-07 11:14:20 +02001839 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03001840 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02001841 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03001842}
1843
Avi Kivity79539ce2007-11-21 02:06:21 +02001844static bool need_remote_flush(u64 old, u64 new)
1845{
1846 if (!is_shadow_present_pte(old))
1847 return false;
1848 if (!is_shadow_present_pte(new))
1849 return true;
1850 if ((old ^ new) & PT64_BASE_ADDR_MASK)
1851 return true;
1852 old ^= PT64_NX_MASK;
1853 new ^= PT64_NX_MASK;
1854 return (old & ~new & PT64_PERM_MASK) != 0;
1855}
1856
1857static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
1858{
1859 if (need_remote_flush(old, new))
1860 kvm_flush_remote_tlbs(vcpu->kvm);
1861 else
1862 kvm_mmu_flush_tlb(vcpu);
1863}
1864
Avi Kivity12b7d282007-09-23 14:10:49 +02001865static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
1866{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001867 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02001868
Sheng Yang7b523452008-04-25 21:13:50 +08001869 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02001870}
1871
Avi Kivityd7824ff2007-12-30 12:29:05 +02001872static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1873 const u8 *new, int bytes)
1874{
1875 gfn_t gfn;
1876 int r;
1877 u64 gpte = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05001878 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02001879
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001880 vcpu->arch.update_pte.largepage = 0;
1881
Avi Kivityd7824ff2007-12-30 12:29:05 +02001882 if (bytes != 4 && bytes != 8)
1883 return;
1884
1885 /*
1886 * Assume that the pte write on a page table of the same type
1887 * as the current vcpu paging mode. This is nearly always true
1888 * (might be false while changing modes). Note it is verified later
1889 * by update_pte().
1890 */
1891 if (is_pae(vcpu)) {
1892 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
1893 if ((bytes == 4) && (gpa % 4 == 0)) {
1894 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
1895 if (r)
1896 return;
1897 memcpy((void *)&gpte + (gpa % 8), new, 4);
1898 } else if ((bytes == 8) && (gpa % 8 == 0)) {
1899 memcpy((void *)&gpte, new, 8);
1900 }
1901 } else {
1902 if ((bytes == 4) && (gpa % 4 == 0))
1903 memcpy((void *)&gpte, new, 4);
1904 }
1905 if (!is_present_pte(gpte))
1906 return;
1907 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001908
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001909 if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) {
1910 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1911 vcpu->arch.update_pte.largepage = 1;
1912 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001913 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001914 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05001915 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001916
Anthony Liguori35149e22008-04-02 14:46:56 -05001917 if (is_error_pfn(pfn)) {
1918 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02001919 return;
1920 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02001921 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001922 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02001923}
1924
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001925static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
1926{
1927 u64 *spte = vcpu->arch.last_pte_updated;
1928
1929 if (spte
1930 && vcpu->arch.last_pte_gfn == gfn
1931 && shadow_accessed_mask
1932 && !(*spte & shadow_accessed_mask)
1933 && is_shadow_present_pte(*spte))
1934 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
1935}
1936
Avi Kivity09072da2007-05-01 14:16:52 +03001937void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Shaohua Life551882007-07-23 14:51:39 +08001938 const u8 *new, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001939{
Avi Kivity9b7a0322007-01-05 16:36:45 -08001940 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02001941 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001942 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001943 struct hlist_head *bucket;
1944 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02001945 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001946 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001947 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001948 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001949 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001950 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03001951 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001952 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001953 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02001954 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02001955 int r;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001956
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001957 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02001958 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001959 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001960 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02001961 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02001962 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02001963 kvm_mmu_audit(vcpu, "pre pte write");
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001964 if (gfn == vcpu->arch.last_pt_write_gfn
Avi Kivity12b7d282007-09-23 14:10:49 +02001965 && !last_updated_pte_accessed(vcpu)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001966 ++vcpu->arch.last_pt_write_count;
1967 if (vcpu->arch.last_pt_write_count >= 3)
Avi Kivity86a5ba02007-01-05 16:36:50 -08001968 flooded = 1;
1969 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001970 vcpu->arch.last_pt_write_gfn = gfn;
1971 vcpu->arch.last_pt_write_count = 1;
1972 vcpu->arch.last_pte_updated = NULL;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001973 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001974 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001975 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001976 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001977 if (sp->gfn != gfn || sp->role.metaphysical || sp->role.invalid)
Avi Kivity9b7a0322007-01-05 16:36:45 -08001978 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02001979 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001980 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03001981 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001982 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001983 /*
1984 * Misaligned accesses are too much trouble to fix
1985 * up; also, they usually indicate a page is not used
1986 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08001987 *
1988 * If we're seeing too many writes to a page,
1989 * it may no longer be a page table, or we may be
1990 * forking, in which case it is better to unmap the
1991 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001992 */
1993 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02001994 gpa, bytes, sp->role.word);
1995 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02001996 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001997 continue;
1998 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001999 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02002000 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02002001 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002002 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02002003 page_offset <<= 1; /* 32->64 */
2004 /*
2005 * A 32-bit pde maps 4MB while the shadow pdes map
2006 * only 2MB. So we need to double the offset again
2007 * and zap two pdes instead of one.
2008 */
2009 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03002010 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02002011 page_offset <<= 1;
2012 npte = 2;
2013 }
Avi Kivityfce06572007-05-01 16:44:05 +03002014 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002015 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002016 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03002017 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002018 }
Avi Kivity4db35312007-11-21 15:28:32 +02002019 spte = &sp->spt[page_offset / sizeof(*spte)];
Dong, Eddie489f1d62008-01-07 11:14:20 +02002020 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
2021 gentry = 0;
2022 r = kvm_read_guest_atomic(vcpu->kvm,
2023 gpa & ~(u64)(pte_size - 1),
2024 &gentry, pte_size);
2025 new = (const void *)&gentry;
2026 if (r < 0)
2027 new = NULL;
2028 }
Avi Kivityac1b7142007-03-08 17:13:32 +02002029 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02002030 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02002031 mmu_pte_write_zap_pte(vcpu, sp, spte);
Dong, Eddie489f1d62008-01-07 11:14:20 +02002032 if (new)
2033 mmu_pte_write_new_pte(vcpu, sp, spte, new);
Avi Kivity79539ce2007-11-21 02:06:21 +02002034 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002035 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002036 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002037 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002038 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002039 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05002040 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2041 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2042 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002043 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08002044}
2045
Avi Kivitya4360362007-01-05 16:36:45 -08002046int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2047{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002048 gpa_t gpa;
2049 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08002050
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002051 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002052
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002053 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002054 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002055 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002056 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08002057}
Avi Kivity577bdc42008-07-19 08:57:05 +03002058EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08002059
Avi Kivity22d95b12007-09-14 20:26:06 +03002060void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08002061{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002062 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
Avi Kivity4db35312007-11-21 15:28:32 +02002063 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08002064
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002065 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02002066 struct kvm_mmu_page, link);
2067 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02002068 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08002069 }
2070}
Avi Kivityebeace82007-01-05 16:36:47 -08002071
Avi Kivity30677142007-10-28 18:48:59 +02002072int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2073{
2074 int r;
2075 enum emulation_result er;
2076
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002077 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02002078 if (r < 0)
2079 goto out;
2080
2081 if (!r) {
2082 r = 1;
2083 goto out;
2084 }
2085
Avi Kivityb733bfb2007-10-28 18:52:05 +02002086 r = mmu_topup_memory_caches(vcpu);
2087 if (r)
2088 goto out;
2089
Avi Kivity30677142007-10-28 18:48:59 +02002090 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02002091
2092 switch (er) {
2093 case EMULATE_DONE:
2094 return 1;
2095 case EMULATE_DO_MMIO:
2096 ++vcpu->stat.mmio_exits;
2097 return 0;
2098 case EMULATE_FAIL:
2099 kvm_report_emulation_failure(vcpu, "pagetable");
2100 return 1;
2101 default:
2102 BUG();
2103 }
2104out:
Avi Kivity30677142007-10-28 18:48:59 +02002105 return r;
2106}
2107EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2108
Marcelo Tosattia7052892008-09-23 13:18:35 -03002109void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2110{
2111 spin_lock(&vcpu->kvm->mmu_lock);
2112 vcpu->arch.mmu.invlpg(vcpu, gva);
2113 spin_unlock(&vcpu->kvm->mmu_lock);
2114 kvm_mmu_flush_tlb(vcpu);
2115 ++vcpu->stat.invlpg;
2116}
2117EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2118
Joerg Roedel18552672008-02-07 13:47:41 +01002119void kvm_enable_tdp(void)
2120{
2121 tdp_enabled = true;
2122}
2123EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2124
Joerg Roedel5f4cb662008-07-14 20:36:36 +02002125void kvm_disable_tdp(void)
2126{
2127 tdp_enabled = false;
2128}
2129EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2130
Avi Kivity6aa8b732006-12-10 02:21:36 -08002131static void free_mmu_pages(struct kvm_vcpu *vcpu)
2132{
Avi Kivity4db35312007-11-21 15:28:32 +02002133 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002134
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002135 while (!list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
2136 sp = container_of(vcpu->kvm->arch.active_mmu_pages.next,
Avi Kivity4db35312007-11-21 15:28:32 +02002137 struct kvm_mmu_page, link);
2138 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity8d2d73b2008-06-04 18:42:24 +03002139 cond_resched();
Avi Kivityf51234c2007-01-05 16:36:52 -08002140 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002141 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002142}
2143
2144static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2145{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002146 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002147 int i;
2148
2149 ASSERT(vcpu);
2150
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002151 if (vcpu->kvm->arch.n_requested_mmu_pages)
2152 vcpu->kvm->arch.n_free_mmu_pages =
2153 vcpu->kvm->arch.n_requested_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02002154 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002155 vcpu->kvm->arch.n_free_mmu_pages =
2156 vcpu->kvm->arch.n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002157 /*
2158 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2159 * Therefore we need to allocate shadow page tables in the first
2160 * 4GB of memory, which happens to fit the DMA32 zone.
2161 */
2162 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2163 if (!page)
2164 goto error_1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002165 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002166 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002167 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002168
Avi Kivity6aa8b732006-12-10 02:21:36 -08002169 return 0;
2170
2171error_1:
2172 free_mmu_pages(vcpu);
2173 return -ENOMEM;
2174}
2175
Ingo Molnar8018c272006-12-29 16:50:01 -08002176int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002177{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002178 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002179 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002180
Ingo Molnar8018c272006-12-29 16:50:01 -08002181 return alloc_mmu_pages(vcpu);
2182}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002183
Ingo Molnar8018c272006-12-29 16:50:01 -08002184int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2185{
2186 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002187 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08002188
Ingo Molnar8018c272006-12-29 16:50:01 -08002189 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002190}
2191
2192void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2193{
2194 ASSERT(vcpu);
2195
2196 destroy_kvm_mmu(vcpu);
2197 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002198 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002199}
2200
Avi Kivity90cb0522007-07-17 13:04:56 +03002201void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002202{
Avi Kivity4db35312007-11-21 15:28:32 +02002203 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002204
Avi Kivity2245a282008-08-27 16:32:24 +03002205 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002206 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002207 int i;
2208 u64 *pt;
2209
Avi Kivity4db35312007-11-21 15:28:32 +02002210 if (!test_bit(slot, &sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002211 continue;
2212
Avi Kivity4db35312007-11-21 15:28:32 +02002213 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002214 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2215 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02002216 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002217 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002218 }
Avi Kivity171d5952008-08-27 16:40:51 +03002219 kvm_flush_remote_tlbs(kvm);
Avi Kivity2245a282008-08-27 16:32:24 +03002220 spin_unlock(&kvm->mmu_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002221}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002222
Avi Kivity90cb0522007-07-17 13:04:56 +03002223void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03002224{
Avi Kivity4db35312007-11-21 15:28:32 +02002225 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03002226
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002227 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002228 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Avi Kivity4db35312007-11-21 15:28:32 +02002229 kvm_mmu_zap_page(kvm, sp);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002230 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03002231
Avi Kivity90cb0522007-07-17 13:04:56 +03002232 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03002233}
2234
Harvey Harrison8b2cf732008-04-27 12:14:13 -07002235static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
Izik Eidus3ee16c82008-03-30 15:17:21 +03002236{
2237 struct kvm_mmu_page *page;
2238
2239 page = container_of(kvm->arch.active_mmu_pages.prev,
2240 struct kvm_mmu_page, link);
2241 kvm_mmu_zap_page(kvm, page);
2242}
2243
2244static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2245{
2246 struct kvm *kvm;
2247 struct kvm *kvm_freed = NULL;
2248 int cache_count = 0;
2249
2250 spin_lock(&kvm_lock);
2251
2252 list_for_each_entry(kvm, &vm_list, vm_list) {
2253 int npages;
2254
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002255 if (!down_read_trylock(&kvm->slots_lock))
2256 continue;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002257 spin_lock(&kvm->mmu_lock);
2258 npages = kvm->arch.n_alloc_mmu_pages -
2259 kvm->arch.n_free_mmu_pages;
2260 cache_count += npages;
2261 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2262 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2263 cache_count--;
2264 kvm_freed = kvm;
2265 }
2266 nr_to_scan--;
2267
2268 spin_unlock(&kvm->mmu_lock);
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002269 up_read(&kvm->slots_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002270 }
2271 if (kvm_freed)
2272 list_move_tail(&kvm_freed->vm_list, &vm_list);
2273
2274 spin_unlock(&kvm_lock);
2275
2276 return cache_count;
2277}
2278
2279static struct shrinker mmu_shrinker = {
2280 .shrink = mmu_shrink,
2281 .seeks = DEFAULT_SEEKS * 10,
2282};
2283
Ingo Molnar2ddfd202008-05-22 10:37:48 +02002284static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03002285{
2286 if (pte_chain_cache)
2287 kmem_cache_destroy(pte_chain_cache);
2288 if (rmap_desc_cache)
2289 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002290 if (mmu_page_header_cache)
2291 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002292}
2293
Izik Eidus3ee16c82008-03-30 15:17:21 +03002294void kvm_mmu_module_exit(void)
2295{
2296 mmu_destroy_caches();
2297 unregister_shrinker(&mmu_shrinker);
2298}
2299
Avi Kivityb5a33a72007-04-15 16:31:09 +03002300int kvm_mmu_module_init(void)
2301{
2302 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2303 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09002304 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002305 if (!pte_chain_cache)
2306 goto nomem;
2307 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2308 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09002309 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002310 if (!rmap_desc_cache)
2311 goto nomem;
2312
Avi Kivityd3d25b02007-05-30 12:34:53 +03002313 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2314 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09002315 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002316 if (!mmu_page_header_cache)
2317 goto nomem;
2318
Izik Eidus3ee16c82008-03-30 15:17:21 +03002319 register_shrinker(&mmu_shrinker);
2320
Avi Kivityb5a33a72007-04-15 16:31:09 +03002321 return 0;
2322
2323nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03002324 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03002325 return -ENOMEM;
2326}
2327
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08002328/*
2329 * Caculate mmu pages needed for kvm.
2330 */
2331unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
2332{
2333 int i;
2334 unsigned int nr_mmu_pages;
2335 unsigned int nr_pages = 0;
2336
2337 for (i = 0; i < kvm->nmemslots; i++)
2338 nr_pages += kvm->memslots[i].npages;
2339
2340 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2341 nr_mmu_pages = max(nr_mmu_pages,
2342 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2343
2344 return nr_mmu_pages;
2345}
2346
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002347static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2348 unsigned len)
2349{
2350 if (len > buffer->len)
2351 return NULL;
2352 return buffer->ptr;
2353}
2354
2355static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2356 unsigned len)
2357{
2358 void *ret;
2359
2360 ret = pv_mmu_peek_buffer(buffer, len);
2361 if (!ret)
2362 return ret;
2363 buffer->ptr += len;
2364 buffer->len -= len;
2365 buffer->processed += len;
2366 return ret;
2367}
2368
2369static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
2370 gpa_t addr, gpa_t value)
2371{
2372 int bytes = 8;
2373 int r;
2374
2375 if (!is_long_mode(vcpu) && !is_pae(vcpu))
2376 bytes = 4;
2377
2378 r = mmu_topup_memory_caches(vcpu);
2379 if (r)
2380 return r;
2381
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002382 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002383 return -EFAULT;
2384
2385 return 1;
2386}
2387
2388static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2389{
2390 kvm_x86_ops->tlb_flush(vcpu);
2391 return 1;
2392}
2393
2394static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
2395{
2396 spin_lock(&vcpu->kvm->mmu_lock);
2397 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
2398 spin_unlock(&vcpu->kvm->mmu_lock);
2399 return 1;
2400}
2401
2402static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
2403 struct kvm_pv_mmu_op_buffer *buffer)
2404{
2405 struct kvm_mmu_op_header *header;
2406
2407 header = pv_mmu_peek_buffer(buffer, sizeof *header);
2408 if (!header)
2409 return 0;
2410 switch (header->op) {
2411 case KVM_MMU_OP_WRITE_PTE: {
2412 struct kvm_mmu_op_write_pte *wpte;
2413
2414 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
2415 if (!wpte)
2416 return 0;
2417 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
2418 wpte->pte_val);
2419 }
2420 case KVM_MMU_OP_FLUSH_TLB: {
2421 struct kvm_mmu_op_flush_tlb *ftlb;
2422
2423 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
2424 if (!ftlb)
2425 return 0;
2426 return kvm_pv_mmu_flush_tlb(vcpu);
2427 }
2428 case KVM_MMU_OP_RELEASE_PT: {
2429 struct kvm_mmu_op_release_pt *rpt;
2430
2431 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
2432 if (!rpt)
2433 return 0;
2434 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
2435 }
2436 default: return 0;
2437 }
2438}
2439
2440int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
2441 gpa_t addr, unsigned long *ret)
2442{
2443 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002444 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002445
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002446 buffer->ptr = buffer->buf;
2447 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
2448 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002449
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002450 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002451 if (r)
2452 goto out;
2453
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002454 while (buffer->len) {
2455 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002456 if (r < 0)
2457 goto out;
2458 if (r == 0)
2459 break;
2460 }
2461
2462 r = 1;
2463out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07002464 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002465 return r;
2466}
2467
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002468#ifdef AUDIT
2469
2470static const char *audit_msg;
2471
2472static gva_t canonicalize(gva_t gva)
2473{
2474#ifdef CONFIG_X86_64
2475 gva = (long long)(gva << 16) >> 16;
2476#endif
2477 return gva;
2478}
2479
2480static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
2481 gva_t va, int level)
2482{
2483 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
2484 int i;
2485 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
2486
2487 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
2488 u64 ent = pt[i];
2489
Avi Kivityc7addb92007-09-16 18:58:32 +02002490 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002491 continue;
2492
2493 va = canonicalize(va);
Avi Kivityc7addb92007-09-16 18:58:32 +02002494 if (level > 1) {
2495 if (ent == shadow_notrap_nonpresent_pte)
2496 printk(KERN_ERR "audit: (%s) nontrapping pte"
2497 " in nonleaf level: levels %d gva %lx"
2498 " level %d pte %llx\n", audit_msg,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002499 vcpu->arch.mmu.root_level, va, level, ent);
Avi Kivityc7addb92007-09-16 18:58:32 +02002500
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002501 audit_mappings_page(vcpu, ent, va, level - 1);
Avi Kivityc7addb92007-09-16 18:58:32 +02002502 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002503 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05002504 hpa_t hpa = (hpa_t)gpa_to_pfn(vcpu, gpa) << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002505
Avi Kivityc7addb92007-09-16 18:58:32 +02002506 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002507 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02002508 printk(KERN_ERR "xx audit error: (%s) levels %d"
2509 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002510 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04002511 va, gpa, hpa, ent,
2512 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02002513 else if (ent == shadow_notrap_nonpresent_pte
2514 && !is_error_hpa(hpa))
2515 printk(KERN_ERR "audit: (%s) notrap shadow,"
2516 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05002517 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02002518
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002519 }
2520 }
2521}
2522
2523static void audit_mappings(struct kvm_vcpu *vcpu)
2524{
Avi Kivity1ea252a2007-03-08 11:48:09 +02002525 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002526
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002527 if (vcpu->arch.mmu.root_level == 4)
2528 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002529 else
2530 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002531 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002532 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002533 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002534 i << 30,
2535 2);
2536}
2537
2538static int count_rmaps(struct kvm_vcpu *vcpu)
2539{
2540 int nmaps = 0;
2541 int i, j, k;
2542
2543 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
2544 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
2545 struct kvm_rmap_desc *d;
2546
2547 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02002548 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002549
Izik Eidus290fc382007-09-27 14:11:22 +02002550 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002551 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02002552 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002553 ++nmaps;
2554 continue;
2555 }
Izik Eidus290fc382007-09-27 14:11:22 +02002556 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002557 while (d) {
2558 for (k = 0; k < RMAP_EXT; ++k)
2559 if (d->shadow_ptes[k])
2560 ++nmaps;
2561 else
2562 break;
2563 d = d->more;
2564 }
2565 }
2566 }
2567 return nmaps;
2568}
2569
2570static int count_writable_mappings(struct kvm_vcpu *vcpu)
2571{
2572 int nmaps = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02002573 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002574 int i;
2575
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002576 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02002577 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002578
Avi Kivity4db35312007-11-21 15:28:32 +02002579 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002580 continue;
2581
2582 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
2583 u64 ent = pt[i];
2584
2585 if (!(ent & PT_PRESENT_MASK))
2586 continue;
2587 if (!(ent & PT_WRITABLE_MASK))
2588 continue;
2589 ++nmaps;
2590 }
2591 }
2592 return nmaps;
2593}
2594
2595static void audit_rmap(struct kvm_vcpu *vcpu)
2596{
2597 int n_rmap = count_rmaps(vcpu);
2598 int n_actual = count_writable_mappings(vcpu);
2599
2600 if (n_rmap != n_actual)
2601 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002602 __func__, audit_msg, n_rmap, n_actual);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002603}
2604
2605static void audit_write_protection(struct kvm_vcpu *vcpu)
2606{
Avi Kivity4db35312007-11-21 15:28:32 +02002607 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02002608 struct kvm_memory_slot *slot;
2609 unsigned long *rmapp;
2610 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002611
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002612 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02002613 if (sp->role.metaphysical)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002614 continue;
2615
Avi Kivity4db35312007-11-21 15:28:32 +02002616 slot = gfn_to_memslot(vcpu->kvm, sp->gfn);
2617 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02002618 rmapp = &slot->rmap[gfn - slot->base_gfn];
2619 if (*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002620 printk(KERN_ERR "%s: (%s) shadow page has writable"
2621 " mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002622 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02002623 sp->role.word);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002624 }
2625}
2626
2627static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
2628{
2629 int olddbg = dbg;
2630
2631 dbg = 0;
2632 audit_msg = msg;
2633 audit_rmap(vcpu);
2634 audit_write_protection(vcpu);
2635 audit_mappings(vcpu);
2636 dbg = olddbg;
2637}
2638
2639#endif