blob: b67585c1ef089c261e06b6a76b1f843684bf4f6c [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
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivity6de4f3a2009-05-31 22:58:47 +030021#include "kvm_cache_regs.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>
Eduardo Habkost13673a92008-11-17 19:03:13 -020036#include <asm/vmx.h>
Avi Kivitye4956062007-06-28 14:15:57 -040037
Joerg Roedel18552672008-02-07 13:47:41 +010038/*
39 * When setting this variable to true it enables Two-Dimensional-Paging
40 * where the hardware walks 2 page tables:
41 * 1. the guest-virtual to guest-physical
42 * 2. while doing 1. it walks guest-physical to host-physical
43 * If the hardware supports that we don't need to do shadow paging.
44 */
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050045bool tdp_enabled = false;
Joerg Roedel18552672008-02-07 13:47:41 +010046
Avi Kivity37a7d8b2007-01-05 16:36:56 -080047#undef MMU_DEBUG
48
49#undef AUDIT
50
51#ifdef AUDIT
52static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
53#else
54static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
55#endif
56
57#ifdef MMU_DEBUG
58
59#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
60#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
61
62#else
63
64#define pgprintk(x...) do { } while (0)
65#define rmap_printk(x...) do { } while (0)
66
67#endif
68
69#if defined(MMU_DEBUG) || defined(AUDIT)
Avi Kivity6ada8cc2008-06-22 16:45:24 +030070static int dbg = 0;
71module_param(dbg, bool, 0644);
Avi Kivity37a7d8b2007-01-05 16:36:56 -080072#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080073
Marcelo Tosatti582801a2008-09-23 13:18:41 -030074static int oos_shadow = 1;
75module_param(oos_shadow, bool, 0644);
76
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080077#ifndef MMU_DEBUG
78#define ASSERT(x) do { } while (0)
79#else
Avi Kivity6aa8b732006-12-10 02:21:36 -080080#define ASSERT(x) \
81 if (!(x)) { \
82 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
83 __FILE__, __LINE__, #x); \
84 }
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080085#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080086
Avi Kivity6aa8b732006-12-10 02:21:36 -080087#define PT_FIRST_AVAIL_BITS_SHIFT 9
88#define PT64_SECOND_AVAIL_BITS_SHIFT 52
89
Avi Kivity6aa8b732006-12-10 02:21:36 -080090#define VALID_PAGE(x) ((x) != INVALID_PAGE)
91
92#define PT64_LEVEL_BITS 9
93
94#define PT64_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -040095 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -080096
97#define PT64_LEVEL_MASK(level) \
98 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
99
100#define PT64_INDEX(address, level)\
101 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
102
103
104#define PT32_LEVEL_BITS 10
105
106#define PT32_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400107 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800108
109#define PT32_LEVEL_MASK(level) \
110 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
111
112#define PT32_INDEX(address, level)\
113 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
114
115
Avi Kivity27aba762007-03-09 13:04:31 +0200116#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800117#define PT64_DIR_BASE_ADDR_MASK \
118 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
119
120#define PT32_BASE_ADDR_MASK PAGE_MASK
121#define PT32_DIR_BASE_ADDR_MASK \
122 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
123
Avi Kivity79539ce2007-11-21 02:06:21 +0200124#define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
125 | PT64_NX_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800126
127#define PFERR_PRESENT_MASK (1U << 0)
128#define PFERR_WRITE_MASK (1U << 1)
129#define PFERR_USER_MASK (1U << 2)
Dong, Eddie82725b22009-03-30 16:21:08 +0800130#define PFERR_RSVD_MASK (1U << 3)
Avi Kivity73b10872007-01-26 00:56:41 -0800131#define PFERR_FETCH_MASK (1U << 4)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800132
Avi Kivity6aa8b732006-12-10 02:21:36 -0800133#define PT_DIRECTORY_LEVEL 2
134#define PT_PAGE_TABLE_LEVEL 1
135
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800136#define RMAP_EXT 4
137
Avi Kivityfe135d22007-12-09 16:15:46 +0200138#define ACC_EXEC_MASK 1
139#define ACC_WRITE_MASK PT_WRITABLE_MASK
140#define ACC_USER_MASK PT_USER_MASK
141#define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
142
Avi Kivity135f8c22008-08-21 17:49:56 +0300143#define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
144
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800145struct kvm_rmap_desc {
Avi Kivityd555c332009-06-10 14:24:23 +0300146 u64 *sptes[RMAP_EXT];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800147 struct kvm_rmap_desc *more;
148};
149
Avi Kivity2d111232008-12-25 14:39:47 +0200150struct kvm_shadow_walk_iterator {
151 u64 addr;
152 hpa_t shadow_addr;
153 int level;
154 u64 *sptep;
155 unsigned index;
156};
157
158#define for_each_shadow_entry(_vcpu, _addr, _walker) \
159 for (shadow_walk_init(&(_walker), _vcpu, _addr); \
160 shadow_walk_okay(&(_walker)); \
161 shadow_walk_next(&(_walker)))
162
163
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -0300164struct kvm_unsync_walk {
165 int (*entry) (struct kvm_mmu_page *sp, struct kvm_unsync_walk *walk);
166};
167
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300168typedef int (*mmu_parent_walk_fn) (struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp);
169
Avi Kivityb5a33a72007-04-15 16:31:09 +0300170static struct kmem_cache *pte_chain_cache;
171static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300172static struct kmem_cache *mmu_page_header_cache;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300173
Avi Kivityc7addb92007-09-16 18:58:32 +0200174static u64 __read_mostly shadow_trap_nonpresent_pte;
175static u64 __read_mostly shadow_notrap_nonpresent_pte;
Sheng Yang7b523452008-04-25 21:13:50 +0800176static u64 __read_mostly shadow_base_present_pte;
177static u64 __read_mostly shadow_nx_mask;
178static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
179static u64 __read_mostly shadow_user_mask;
180static u64 __read_mostly shadow_accessed_mask;
181static u64 __read_mostly shadow_dirty_mask;
Avi Kivityc7addb92007-09-16 18:58:32 +0200182
Dong, Eddie82725b22009-03-30 16:21:08 +0800183static inline u64 rsvd_bits(int s, int e)
184{
185 return ((1ULL << (e - s + 1)) - 1) << s;
186}
187
Avi Kivityc7addb92007-09-16 18:58:32 +0200188void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
189{
190 shadow_trap_nonpresent_pte = trap_pte;
191 shadow_notrap_nonpresent_pte = notrap_pte;
192}
193EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
194
Sheng Yang7b523452008-04-25 21:13:50 +0800195void kvm_mmu_set_base_ptes(u64 base_pte)
196{
197 shadow_base_present_pte = base_pte;
198}
199EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes);
200
201void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
Sheng Yang4b12f0d2009-04-27 20:35:42 +0800202 u64 dirty_mask, u64 nx_mask, u64 x_mask)
Sheng Yang7b523452008-04-25 21:13:50 +0800203{
204 shadow_user_mask = user_mask;
205 shadow_accessed_mask = accessed_mask;
206 shadow_dirty_mask = dirty_mask;
207 shadow_nx_mask = nx_mask;
208 shadow_x_mask = x_mask;
209}
210EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
211
Avi Kivity6aa8b732006-12-10 02:21:36 -0800212static int is_write_protection(struct kvm_vcpu *vcpu)
213{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800214 return vcpu->arch.cr0 & X86_CR0_WP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800215}
216
217static int is_cpuid_PSE36(void)
218{
219 return 1;
220}
221
Avi Kivity73b10872007-01-26 00:56:41 -0800222static int is_nx(struct kvm_vcpu *vcpu)
223{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800224 return vcpu->arch.shadow_efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800225}
226
Avi Kivityc7addb92007-09-16 18:58:32 +0200227static int is_shadow_present_pte(u64 pte)
228{
Avi Kivityc7addb92007-09-16 18:58:32 +0200229 return pte != shadow_trap_nonpresent_pte
230 && pte != shadow_notrap_nonpresent_pte;
231}
232
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300233static int is_large_pte(u64 pte)
234{
235 return pte & PT_PAGE_SIZE_MASK;
236}
237
Avi Kivity6aa8b732006-12-10 02:21:36 -0800238static int is_writeble_pte(unsigned long pte)
239{
240 return pte & PT_WRITABLE_MASK;
241}
242
Avi Kivity43a37952009-06-10 14:12:05 +0300243static int is_dirty_gpte(unsigned long pte)
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200244{
Avi Kivity439e2182009-06-10 12:56:54 +0300245 return pte & PT_DIRTY_MASK;
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200246}
247
Avi Kivity43a37952009-06-10 14:12:05 +0300248static int is_rmap_spte(u64 pte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800249{
Avi Kivity4b1a80f2008-03-23 12:18:19 +0200250 return is_shadow_present_pte(pte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800251}
252
Marcelo Tosatti776e6632009-06-10 12:27:03 -0300253static int is_last_spte(u64 pte, int level)
254{
255 if (level == PT_PAGE_TABLE_LEVEL)
256 return 1;
257 if (level == PT_DIRECTORY_LEVEL && is_large_pte(pte))
258 return 1;
259 return 0;
260}
261
Anthony Liguori35149e22008-04-02 14:46:56 -0500262static pfn_t spte_to_pfn(u64 pte)
Avi Kivity0b49ea82008-03-23 15:06:23 +0200263{
Anthony Liguori35149e22008-04-02 14:46:56 -0500264 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity0b49ea82008-03-23 15:06:23 +0200265}
266
Avi Kivityda928522007-11-21 13:54:47 +0200267static gfn_t pse36_gfn_delta(u32 gpte)
268{
269 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
270
271 return (gpte & PT32_DIR_PSE36_MASK) << shift;
272}
273
Avi Kivityd555c332009-06-10 14:24:23 +0300274static void __set_spte(u64 *sptep, u64 spte)
Avi Kivitye663ee62007-05-31 15:46:04 +0300275{
276#ifdef CONFIG_X86_64
277 set_64bit((unsigned long *)sptep, spte);
278#else
279 set_64bit((unsigned long long *)sptep, spte);
280#endif
281}
282
Avi Kivitye2dec932007-01-05 16:36:54 -0800283static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300284 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800285{
286 void *obj;
287
288 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800289 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800290 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300291 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800292 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800293 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800294 cache->objects[cache->nobjs++] = obj;
295 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800296 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800297}
298
299static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
300{
301 while (mc->nobjs)
302 kfree(mc->objects[--mc->nobjs]);
303}
304
Avi Kivityc1158e62007-07-20 08:18:27 +0300305static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300306 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300307{
308 struct page *page;
309
310 if (cache->nobjs >= min)
311 return 0;
312 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300313 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300314 if (!page)
315 return -ENOMEM;
316 set_page_private(page, 0);
317 cache->objects[cache->nobjs++] = page_address(page);
318 }
319 return 0;
320}
321
322static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
323{
324 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300325 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300326}
327
Avi Kivity8c438502007-04-16 11:53:17 +0300328static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
329{
330 int r;
331
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800332 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300333 pte_chain_cache, 4);
334 if (r)
335 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800336 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
Marcelo Tosattic41ef342008-10-28 18:16:58 -0200337 rmap_desc_cache, 4);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300338 if (r)
339 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800340 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300341 if (r)
342 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800343 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300344 mmu_page_header_cache, 4);
345out:
Avi Kivity8c438502007-04-16 11:53:17 +0300346 return r;
347}
348
Avi Kivity714b93d2007-01-05 16:36:53 -0800349static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
350{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800351 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
352 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
353 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
354 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800355}
356
357static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
358 size_t size)
359{
360 void *p;
361
362 BUG_ON(!mc->nobjs);
363 p = mc->objects[--mc->nobjs];
Avi Kivity714b93d2007-01-05 16:36:53 -0800364 return p;
365}
366
Avi Kivity714b93d2007-01-05 16:36:53 -0800367static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
368{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800369 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800370 sizeof(struct kvm_pte_chain));
371}
372
Avi Kivity90cb0522007-07-17 13:04:56 +0300373static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800374{
Avi Kivity90cb0522007-07-17 13:04:56 +0300375 kfree(pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800376}
377
378static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
379{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800380 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800381 sizeof(struct kvm_rmap_desc));
382}
383
Avi Kivity90cb0522007-07-17 13:04:56 +0300384static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800385{
Avi Kivity90cb0522007-07-17 13:04:56 +0300386 kfree(rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800387}
388
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800389/*
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300390 * Return the pointer to the largepage write count for a given
391 * gfn, handling slots that are not large page aligned.
392 */
393static int *slot_largepage_idx(gfn_t gfn, struct kvm_memory_slot *slot)
394{
395 unsigned long idx;
396
Joerg Roedelec04b262009-06-19 15:16:23 +0200397 idx = (gfn / KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL)) -
398 (slot->base_gfn / KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL));
399 return &slot->lpage_info[0][idx].write_count;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300400}
401
402static void account_shadowed(struct kvm *kvm, gfn_t gfn)
403{
404 int *write_count;
405
Izik Eidus28430992008-10-03 17:40:32 +0300406 gfn = unalias_gfn(kvm, gfn);
407 write_count = slot_largepage_idx(gfn,
408 gfn_to_memslot_unaliased(kvm, gfn));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300409 *write_count += 1;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300410}
411
412static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
413{
414 int *write_count;
415
Izik Eidus28430992008-10-03 17:40:32 +0300416 gfn = unalias_gfn(kvm, gfn);
417 write_count = slot_largepage_idx(gfn,
418 gfn_to_memslot_unaliased(kvm, gfn));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300419 *write_count -= 1;
420 WARN_ON(*write_count < 0);
421}
422
423static int has_wrprotected_page(struct kvm *kvm, gfn_t gfn)
424{
Izik Eidus28430992008-10-03 17:40:32 +0300425 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300426 int *largepage_idx;
427
Izik Eidus28430992008-10-03 17:40:32 +0300428 gfn = unalias_gfn(kvm, gfn);
429 slot = gfn_to_memslot_unaliased(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300430 if (slot) {
431 largepage_idx = slot_largepage_idx(gfn, slot);
432 return *largepage_idx;
433 }
434
435 return 1;
436}
437
438static int host_largepage_backed(struct kvm *kvm, gfn_t gfn)
439{
440 struct vm_area_struct *vma;
441 unsigned long addr;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300442 int ret = 0;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300443
444 addr = gfn_to_hva(kvm, gfn);
445 if (kvm_is_error_hva(addr))
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300446 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300447
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300448 down_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300449 vma = find_vma(current->mm, addr);
450 if (vma && is_vm_hugetlb_page(vma))
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300451 ret = 1;
452 up_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300453
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -0300454 return ret;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300455}
456
457static int is_largepage_backed(struct kvm_vcpu *vcpu, gfn_t large_gfn)
458{
459 struct kvm_memory_slot *slot;
460
461 if (has_wrprotected_page(vcpu->kvm, large_gfn))
462 return 0;
463
464 if (!host_largepage_backed(vcpu->kvm, large_gfn))
465 return 0;
466
467 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
468 if (slot && slot->dirty_bitmap)
469 return 0;
470
471 return 1;
472}
473
474/*
Izik Eidus290fc382007-09-27 14:11:22 +0200475 * Take gfn and return the reverse mapping to it.
476 * Note: gfn must be unaliased before this function get called
477 */
478
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300479static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int lpage)
Izik Eidus290fc382007-09-27 14:11:22 +0200480{
481 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300482 unsigned long idx;
Izik Eidus290fc382007-09-27 14:11:22 +0200483
484 slot = gfn_to_memslot(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300485 if (!lpage)
486 return &slot->rmap[gfn - slot->base_gfn];
487
Joerg Roedelec04b262009-06-19 15:16:23 +0200488 idx = (gfn / KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL)) -
489 (slot->base_gfn / KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL));
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300490
Joerg Roedelec04b262009-06-19 15:16:23 +0200491 return &slot->lpage_info[0][idx].rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200492}
493
494/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800495 * Reverse mapping data structures:
496 *
Izik Eidus290fc382007-09-27 14:11:22 +0200497 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
498 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800499 *
Izik Eidus290fc382007-09-27 14:11:22 +0200500 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
501 * containing more mappings.
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300502 *
503 * Returns the number of rmap entries before the spte was added or zero if
504 * the spte was not added.
505 *
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800506 */
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300507static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn, int lpage)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800508{
Avi Kivity4db35312007-11-21 15:28:32 +0200509 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800510 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200511 unsigned long *rmapp;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300512 int i, count = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800513
Avi Kivity43a37952009-06-10 14:12:05 +0300514 if (!is_rmap_spte(*spte))
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300515 return count;
Izik Eidus290fc382007-09-27 14:11:22 +0200516 gfn = unalias_gfn(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200517 sp = page_header(__pa(spte));
518 sp->gfns[spte - sp->spt] = gfn;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300519 rmapp = gfn_to_rmap(vcpu->kvm, gfn, lpage);
Izik Eidus290fc382007-09-27 14:11:22 +0200520 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800521 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200522 *rmapp = (unsigned long)spte;
523 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800524 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800525 desc = mmu_alloc_rmap_desc(vcpu);
Avi Kivityd555c332009-06-10 14:24:23 +0300526 desc->sptes[0] = (u64 *)*rmapp;
527 desc->sptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200528 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800529 } else {
530 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200531 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivityd555c332009-06-10 14:24:23 +0300532 while (desc->sptes[RMAP_EXT-1] && desc->more) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800533 desc = desc->more;
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300534 count += RMAP_EXT;
535 }
Avi Kivityd555c332009-06-10 14:24:23 +0300536 if (desc->sptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800537 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800538 desc = desc->more;
539 }
Avi Kivityd555c332009-06-10 14:24:23 +0300540 for (i = 0; desc->sptes[i]; ++i)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800541 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300542 desc->sptes[i] = spte;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800543 }
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300544 return count;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800545}
546
Izik Eidus290fc382007-09-27 14:11:22 +0200547static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800548 struct kvm_rmap_desc *desc,
549 int i,
550 struct kvm_rmap_desc *prev_desc)
551{
552 int j;
553
Avi Kivityd555c332009-06-10 14:24:23 +0300554 for (j = RMAP_EXT - 1; !desc->sptes[j] && j > i; --j)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800555 ;
Avi Kivityd555c332009-06-10 14:24:23 +0300556 desc->sptes[i] = desc->sptes[j];
557 desc->sptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800558 if (j != 0)
559 return;
560 if (!prev_desc && !desc->more)
Avi Kivityd555c332009-06-10 14:24:23 +0300561 *rmapp = (unsigned long)desc->sptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800562 else
563 if (prev_desc)
564 prev_desc->more = desc->more;
565 else
Izik Eidus290fc382007-09-27 14:11:22 +0200566 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300567 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800568}
569
Izik Eidus290fc382007-09-27 14:11:22 +0200570static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800571{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800572 struct kvm_rmap_desc *desc;
573 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200574 struct kvm_mmu_page *sp;
Anthony Liguori35149e22008-04-02 14:46:56 -0500575 pfn_t pfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200576 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800577 int i;
578
Avi Kivity43a37952009-06-10 14:12:05 +0300579 if (!is_rmap_spte(*spte))
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800580 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200581 sp = page_header(__pa(spte));
Anthony Liguori35149e22008-04-02 14:46:56 -0500582 pfn = spte_to_pfn(*spte);
Sheng Yang7b523452008-04-25 21:13:50 +0800583 if (*spte & shadow_accessed_mask)
Anthony Liguori35149e22008-04-02 14:46:56 -0500584 kvm_set_pfn_accessed(pfn);
Izik Eidusb4231d62007-11-20 11:49:33 +0200585 if (is_writeble_pte(*spte))
Anthony Liguori35149e22008-04-02 14:46:56 -0500586 kvm_release_pfn_dirty(pfn);
Izik Eidusb4231d62007-11-20 11:49:33 +0200587 else
Anthony Liguori35149e22008-04-02 14:46:56 -0500588 kvm_release_pfn_clean(pfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300589 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], is_large_pte(*spte));
Izik Eidus290fc382007-09-27 14:11:22 +0200590 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800591 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
592 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200593 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800594 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200595 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800596 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
597 spte, *spte);
598 BUG();
599 }
Izik Eidus290fc382007-09-27 14:11:22 +0200600 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800601 } else {
602 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200603 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800604 prev_desc = NULL;
605 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300606 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i)
607 if (desc->sptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200608 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800609 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800610 prev_desc);
611 return;
612 }
613 prev_desc = desc;
614 desc = desc->more;
615 }
616 BUG();
617 }
618}
619
Izik Eidus98348e92007-10-16 14:42:30 +0200620static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800621{
Avi Kivity374cbac2007-01-05 16:36:43 -0800622 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200623 struct kvm_rmap_desc *prev_desc;
624 u64 *prev_spte;
625 int i;
626
627 if (!*rmapp)
628 return NULL;
629 else if (!(*rmapp & 1)) {
630 if (!spte)
631 return (u64 *)*rmapp;
632 return NULL;
633 }
634 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
635 prev_desc = NULL;
636 prev_spte = NULL;
637 while (desc) {
Avi Kivityd555c332009-06-10 14:24:23 +0300638 for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i) {
Izik Eidus98348e92007-10-16 14:42:30 +0200639 if (prev_spte == spte)
Avi Kivityd555c332009-06-10 14:24:23 +0300640 return desc->sptes[i];
641 prev_spte = desc->sptes[i];
Izik Eidus98348e92007-10-16 14:42:30 +0200642 }
643 desc = desc->more;
644 }
645 return NULL;
646}
647
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200648static int rmap_write_protect(struct kvm *kvm, u64 gfn)
Izik Eidus98348e92007-10-16 14:42:30 +0200649{
Izik Eidus290fc382007-09-27 14:11:22 +0200650 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800651 u64 *spte;
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800652 int write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800653
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500654 gfn = unalias_gfn(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300655 rmapp = gfn_to_rmap(kvm, gfn, 0);
Avi Kivity374cbac2007-01-05 16:36:43 -0800656
Izik Eidus98348e92007-10-16 14:42:30 +0200657 spte = rmap_next(kvm, rmapp, NULL);
658 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800659 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800660 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800661 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800662 if (is_writeble_pte(*spte)) {
Avi Kivityd555c332009-06-10 14:24:23 +0300663 __set_spte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800664 write_protected = 1;
665 }
Izik Eidus9647c142007-10-16 14:43:46 +0200666 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800667 }
Izik Eidus855149a2008-03-20 18:17:24 +0200668 if (write_protected) {
Anthony Liguori35149e22008-04-02 14:46:56 -0500669 pfn_t pfn;
Izik Eidus855149a2008-03-20 18:17:24 +0200670
671 spte = rmap_next(kvm, rmapp, NULL);
Anthony Liguori35149e22008-04-02 14:46:56 -0500672 pfn = spte_to_pfn(*spte);
673 kvm_set_pfn_dirty(pfn);
Izik Eidus855149a2008-03-20 18:17:24 +0200674 }
675
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300676 /* check for huge page mappings */
677 rmapp = gfn_to_rmap(kvm, gfn, 1);
678 spte = rmap_next(kvm, rmapp, NULL);
679 while (spte) {
680 BUG_ON(!spte);
681 BUG_ON(!(*spte & PT_PRESENT_MASK));
682 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
683 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
684 if (is_writeble_pte(*spte)) {
685 rmap_remove(kvm, spte);
686 --kvm->stat.lpages;
Avi Kivityd555c332009-06-10 14:24:23 +0300687 __set_spte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti6597ca02008-06-08 01:48:53 -0300688 spte = NULL;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300689 write_protected = 1;
690 }
691 spte = rmap_next(kvm, rmapp, spte);
692 }
693
Marcelo Tosattib1a36822008-12-01 22:32:03 -0200694 return write_protected;
Avi Kivity374cbac2007-01-05 16:36:43 -0800695}
696
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200697static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp)
698{
699 u64 *spte;
700 int need_tlb_flush = 0;
701
702 while ((spte = rmap_next(kvm, rmapp, NULL))) {
703 BUG_ON(!(*spte & PT_PRESENT_MASK));
704 rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
705 rmap_remove(kvm, spte);
Avi Kivityd555c332009-06-10 14:24:23 +0300706 __set_spte(spte, shadow_trap_nonpresent_pte);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200707 need_tlb_flush = 1;
708 }
709 return need_tlb_flush;
710}
711
712static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
713 int (*handler)(struct kvm *kvm, unsigned long *rmapp))
714{
715 int i;
716 int retval = 0;
717
718 /*
719 * If mmap_sem isn't taken, we can look the memslots with only
720 * the mmu_lock by skipping over the slots with userspace_addr == 0.
721 */
722 for (i = 0; i < kvm->nmemslots; i++) {
723 struct kvm_memory_slot *memslot = &kvm->memslots[i];
724 unsigned long start = memslot->userspace_addr;
725 unsigned long end;
726
727 /* mmu_lock protects userspace_addr */
728 if (!start)
729 continue;
730
731 end = start + (memslot->npages << PAGE_SHIFT);
732 if (hva >= start && hva < end) {
733 gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
Joerg Roedelec04b262009-06-19 15:16:23 +0200734 int idx = gfn_offset /
735 KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200736 retval |= handler(kvm, &memslot->rmap[gfn_offset]);
737 retval |= handler(kvm,
Joerg Roedelec04b262009-06-19 15:16:23 +0200738 &memslot->lpage_info[0][idx].rmap_pde);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200739 }
740 }
741
742 return retval;
743}
744
745int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
746{
747 return kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
748}
749
750static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp)
751{
752 u64 *spte;
753 int young = 0;
754
Sheng Yang534e38b2008-09-08 15:12:30 +0800755 /* always return old for EPT */
756 if (!shadow_accessed_mask)
757 return 0;
758
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200759 spte = rmap_next(kvm, rmapp, NULL);
760 while (spte) {
761 int _young;
762 u64 _spte = *spte;
763 BUG_ON(!(_spte & PT_PRESENT_MASK));
764 _young = _spte & PT_ACCESSED_MASK;
765 if (_young) {
766 young = 1;
767 clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
768 }
769 spte = rmap_next(kvm, rmapp, spte);
770 }
771 return young;
772}
773
Marcelo Tosatti53a27b32009-08-05 15:43:58 -0300774#define RMAP_RECYCLE_THRESHOLD 1000
775
776static void rmap_recycle(struct kvm_vcpu *vcpu, gfn_t gfn, int lpage)
777{
778 unsigned long *rmapp;
779
780 gfn = unalias_gfn(vcpu->kvm, gfn);
781 rmapp = gfn_to_rmap(vcpu->kvm, gfn, lpage);
782
783 kvm_unmap_rmapp(vcpu->kvm, rmapp);
784 kvm_flush_remote_tlbs(vcpu->kvm);
785}
786
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200787int kvm_age_hva(struct kvm *kvm, unsigned long hva)
788{
789 return kvm_handle_hva(kvm, hva, kvm_age_rmapp);
790}
791
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800792#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300793static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800794{
Avi Kivity139bdb22007-01-05 16:36:50 -0800795 u64 *pos;
796 u64 *end;
797
Avi Kivity47ad8e62007-05-06 15:50:58 +0300798 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivity3c915512008-05-20 16:21:13 +0300799 if (is_shadow_present_pte(*pos)) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800800 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800801 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800802 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800803 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800804 return 1;
805}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800806#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800807
Avi Kivity4db35312007-11-21 15:28:32 +0200808static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800809{
Avi Kivity4db35312007-11-21 15:28:32 +0200810 ASSERT(is_empty_shadow_page(sp->spt));
811 list_del(&sp->link);
812 __free_page(virt_to_page(sp->spt));
813 __free_page(virt_to_page(sp->gfns));
814 kfree(sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800815 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800816}
817
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800818static unsigned kvm_page_table_hashfn(gfn_t gfn)
819{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200820 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800821}
822
Avi Kivity25c0de22007-01-05 16:36:42 -0800823static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
824 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800825{
Avi Kivity4db35312007-11-21 15:28:32 +0200826 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800827
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800828 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
829 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
830 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200831 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800832 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -0200833 INIT_LIST_HEAD(&sp->oos_link);
Sheng Yang291f26b2008-10-16 17:30:57 +0800834 bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
Avi Kivity4db35312007-11-21 15:28:32 +0200835 sp->multimapped = 0;
836 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800837 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200838 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800839}
840
Avi Kivity714b93d2007-01-05 16:36:53 -0800841static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200842 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800843{
844 struct kvm_pte_chain *pte_chain;
845 struct hlist_node *node;
846 int i;
847
848 if (!parent_pte)
849 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200850 if (!sp->multimapped) {
851 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800852
853 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200854 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800855 return;
856 }
Avi Kivity4db35312007-11-21 15:28:32 +0200857 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800858 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200859 INIT_HLIST_HEAD(&sp->parent_ptes);
860 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800861 pte_chain->parent_ptes[0] = old;
862 }
Avi Kivity4db35312007-11-21 15:28:32 +0200863 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800864 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
865 continue;
866 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
867 if (!pte_chain->parent_ptes[i]) {
868 pte_chain->parent_ptes[i] = parent_pte;
869 return;
870 }
871 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800872 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800873 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200874 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800875 pte_chain->parent_ptes[0] = parent_pte;
876}
877
Avi Kivity4db35312007-11-21 15:28:32 +0200878static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800879 u64 *parent_pte)
880{
881 struct kvm_pte_chain *pte_chain;
882 struct hlist_node *node;
883 int i;
884
Avi Kivity4db35312007-11-21 15:28:32 +0200885 if (!sp->multimapped) {
886 BUG_ON(sp->parent_pte != parent_pte);
887 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800888 return;
889 }
Avi Kivity4db35312007-11-21 15:28:32 +0200890 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800891 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
892 if (!pte_chain->parent_ptes[i])
893 break;
894 if (pte_chain->parent_ptes[i] != parent_pte)
895 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800896 while (i + 1 < NR_PTE_CHAIN_ENTRIES
897 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800898 pte_chain->parent_ptes[i]
899 = pte_chain->parent_ptes[i + 1];
900 ++i;
901 }
902 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800903 if (i == 0) {
904 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300905 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200906 if (hlist_empty(&sp->parent_ptes)) {
907 sp->multimapped = 0;
908 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800909 }
910 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800911 return;
912 }
913 BUG();
914}
915
Marcelo Tosattiad8cfbe2008-09-23 13:18:36 -0300916
917static void mmu_parent_walk(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
918 mmu_parent_walk_fn fn)
919{
920 struct kvm_pte_chain *pte_chain;
921 struct hlist_node *node;
922 struct kvm_mmu_page *parent_sp;
923 int i;
924
925 if (!sp->multimapped && sp->parent_pte) {
926 parent_sp = page_header(__pa(sp->parent_pte));
927 fn(vcpu, parent_sp);
928 mmu_parent_walk(vcpu, parent_sp, fn);
929 return;
930 }
931 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
932 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
933 if (!pte_chain->parent_ptes[i])
934 break;
935 parent_sp = page_header(__pa(pte_chain->parent_ptes[i]));
936 fn(vcpu, parent_sp);
937 mmu_parent_walk(vcpu, parent_sp, fn);
938 }
939}
940
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300941static void kvm_mmu_update_unsync_bitmap(u64 *spte)
942{
943 unsigned int index;
944 struct kvm_mmu_page *sp = page_header(__pa(spte));
945
946 index = spte - sp->spt;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -0200947 if (!__test_and_set_bit(index, sp->unsync_child_bitmap))
948 sp->unsync_children++;
949 WARN_ON(!sp->unsync_children);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300950}
951
952static void kvm_mmu_update_parents_unsync(struct kvm_mmu_page *sp)
953{
954 struct kvm_pte_chain *pte_chain;
955 struct hlist_node *node;
956 int i;
957
958 if (!sp->parent_pte)
959 return;
960
961 if (!sp->multimapped) {
962 kvm_mmu_update_unsync_bitmap(sp->parent_pte);
963 return;
964 }
965
966 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
967 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
968 if (!pte_chain->parent_ptes[i])
969 break;
970 kvm_mmu_update_unsync_bitmap(pte_chain->parent_ptes[i]);
971 }
972}
973
974static int unsync_walk_fn(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
975{
Marcelo Tosatti0074ff62008-09-23 13:18:40 -0300976 kvm_mmu_update_parents_unsync(sp);
977 return 1;
978}
979
980static void kvm_mmu_mark_parents_unsync(struct kvm_vcpu *vcpu,
981 struct kvm_mmu_page *sp)
982{
983 mmu_parent_walk(vcpu, sp, unsync_walk_fn);
984 kvm_mmu_update_parents_unsync(sp);
985}
986
Avi Kivityd761a502008-05-29 14:55:03 +0300987static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
988 struct kvm_mmu_page *sp)
989{
990 int i;
991
992 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
993 sp->spt[i] = shadow_trap_nonpresent_pte;
994}
995
Marcelo Tosattie8bc2172008-09-23 13:18:33 -0300996static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
997 struct kvm_mmu_page *sp)
998{
999 return 1;
1000}
1001
Marcelo Tosattia7052892008-09-23 13:18:35 -03001002static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
1003{
1004}
1005
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001006#define KVM_PAGE_ARRAY_NR 16
1007
1008struct kvm_mmu_pages {
1009 struct mmu_page_and_offset {
1010 struct kvm_mmu_page *sp;
1011 unsigned int idx;
1012 } page[KVM_PAGE_ARRAY_NR];
1013 unsigned int nr;
1014};
1015
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001016#define for_each_unsync_children(bitmap, idx) \
1017 for (idx = find_first_bit(bitmap, 512); \
1018 idx < 512; \
1019 idx = find_next_bit(bitmap, 512, idx+1))
1020
Hannes Edercded19f2009-02-21 02:19:13 +01001021static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
1022 int idx)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001023{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001024 int i;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001025
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001026 if (sp->unsync)
1027 for (i=0; i < pvec->nr; i++)
1028 if (pvec->page[i].sp == sp)
1029 return 0;
1030
1031 pvec->page[pvec->nr].sp = sp;
1032 pvec->page[pvec->nr].idx = idx;
1033 pvec->nr++;
1034 return (pvec->nr == KVM_PAGE_ARRAY_NR);
1035}
1036
1037static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
1038 struct kvm_mmu_pages *pvec)
1039{
1040 int i, ret, nr_unsync_leaf = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001041
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001042 for_each_unsync_children(sp->unsync_child_bitmap, i) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001043 u64 ent = sp->spt[i];
1044
Marcelo Tosatti87917232008-12-22 18:49:30 -02001045 if (is_shadow_present_pte(ent) && !is_large_pte(ent)) {
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001046 struct kvm_mmu_page *child;
1047 child = page_header(ent & PT64_BASE_ADDR_MASK);
1048
1049 if (child->unsync_children) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001050 if (mmu_pages_add(pvec, child, i))
1051 return -ENOSPC;
1052
1053 ret = __mmu_unsync_walk(child, pvec);
1054 if (!ret)
1055 __clear_bit(i, sp->unsync_child_bitmap);
1056 else if (ret > 0)
1057 nr_unsync_leaf += ret;
1058 else
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001059 return ret;
1060 }
1061
1062 if (child->unsync) {
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001063 nr_unsync_leaf++;
1064 if (mmu_pages_add(pvec, child, i))
1065 return -ENOSPC;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001066 }
1067 }
1068 }
1069
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001070 if (find_first_bit(sp->unsync_child_bitmap, 512) == 512)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001071 sp->unsync_children = 0;
1072
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001073 return nr_unsync_leaf;
1074}
1075
1076static int mmu_unsync_walk(struct kvm_mmu_page *sp,
1077 struct kvm_mmu_pages *pvec)
1078{
1079 if (!sp->unsync_children)
1080 return 0;
1081
1082 mmu_pages_add(pvec, sp, 0);
1083 return __mmu_unsync_walk(sp, pvec);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001084}
1085
Avi Kivity4db35312007-11-21 15:28:32 +02001086static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001087{
1088 unsigned index;
1089 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001090 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001091 struct hlist_node *node;
1092
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001093 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001094 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001095 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001096 hlist_for_each_entry(sp, node, bucket, hash_link)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001097 if (sp->gfn == gfn && !sp->role.direct
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001098 && !sp->role.invalid) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001099 pgprintk("%s: found role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001100 __func__, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001101 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001102 }
1103 return NULL;
1104}
1105
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001106static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
1107{
1108 WARN_ON(!sp->unsync);
1109 sp->unsync = 0;
1110 --kvm->stat.mmu_unsync;
1111}
1112
1113static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp);
1114
1115static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1116{
1117 if (sp->role.glevels != vcpu->arch.mmu.root_level) {
1118 kvm_mmu_zap_page(vcpu->kvm, sp);
1119 return 1;
1120 }
1121
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001122 if (rmap_write_protect(vcpu->kvm, sp->gfn))
1123 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti0c0f40b2008-11-21 19:13:58 +01001124 kvm_unlink_unsync_page(vcpu->kvm, sp);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001125 if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
1126 kvm_mmu_zap_page(vcpu->kvm, sp);
1127 return 1;
1128 }
1129
1130 kvm_mmu_flush_tlb(vcpu);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001131 return 0;
1132}
1133
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001134struct mmu_page_path {
1135 struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
1136 unsigned int idx[PT64_ROOT_LEVEL-1];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001137};
1138
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001139#define for_each_sp(pvec, sp, parents, i) \
1140 for (i = mmu_pages_next(&pvec, &parents, -1), \
1141 sp = pvec.page[i].sp; \
1142 i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
1143 i = mmu_pages_next(&pvec, &parents, i))
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001144
Hannes Edercded19f2009-02-21 02:19:13 +01001145static int mmu_pages_next(struct kvm_mmu_pages *pvec,
1146 struct mmu_page_path *parents,
1147 int i)
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001148{
1149 int n;
1150
1151 for (n = i+1; n < pvec->nr; n++) {
1152 struct kvm_mmu_page *sp = pvec->page[n].sp;
1153
1154 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
1155 parents->idx[0] = pvec->page[n].idx;
1156 return n;
1157 }
1158
1159 parents->parent[sp->role.level-2] = sp;
1160 parents->idx[sp->role.level-1] = pvec->page[n].idx;
1161 }
1162
1163 return n;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001164}
1165
Hannes Edercded19f2009-02-21 02:19:13 +01001166static void mmu_pages_clear_parents(struct mmu_page_path *parents)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001167{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001168 struct kvm_mmu_page *sp;
1169 unsigned int level = 0;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001170
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001171 do {
1172 unsigned int idx = parents->idx[level];
1173
1174 sp = parents->parent[level];
1175 if (!sp)
1176 return;
1177
1178 --sp->unsync_children;
1179 WARN_ON((int)sp->unsync_children < 0);
1180 __clear_bit(idx, sp->unsync_child_bitmap);
1181 level++;
1182 } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
1183}
1184
1185static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
1186 struct mmu_page_path *parents,
1187 struct kvm_mmu_pages *pvec)
1188{
1189 parents->parent[parent->role.level-1] = NULL;
1190 pvec->nr = 0;
1191}
1192
1193static void mmu_sync_children(struct kvm_vcpu *vcpu,
1194 struct kvm_mmu_page *parent)
1195{
1196 int i;
1197 struct kvm_mmu_page *sp;
1198 struct mmu_page_path parents;
1199 struct kvm_mmu_pages pages;
1200
1201 kvm_mmu_pages_init(parent, &parents, &pages);
1202 while (mmu_unsync_walk(parent, &pages)) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001203 int protected = 0;
1204
1205 for_each_sp(pages, sp, parents, i)
1206 protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
1207
1208 if (protected)
1209 kvm_flush_remote_tlbs(vcpu->kvm);
1210
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001211 for_each_sp(pages, sp, parents, i) {
1212 kvm_sync_page(vcpu, sp);
1213 mmu_pages_clear_parents(&parents);
1214 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001215 cond_resched_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001216 kvm_mmu_pages_init(parent, &parents, &pages);
1217 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001218}
1219
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001220static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
1221 gfn_t gfn,
1222 gva_t gaddr,
1223 unsigned level,
Avi Kivityf6e2c022009-01-11 13:02:10 +02001224 int direct,
Avi Kivity41074d02007-12-09 17:00:02 +02001225 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001226 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001227{
1228 union kvm_mmu_page_role role;
1229 unsigned index;
1230 unsigned quadrant;
1231 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001232 struct kvm_mmu_page *sp;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001233 struct hlist_node *node, *tmp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001234
Avi Kivitya770f6f2008-12-21 19:20:09 +02001235 role = vcpu->arch.mmu.base_role;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001236 role.level = level;
Avi Kivityf6e2c022009-01-11 13:02:10 +02001237 role.direct = direct;
Avi Kivity41074d02007-12-09 17:00:02 +02001238 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001239 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001240 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
1241 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
1242 role.quadrant = quadrant;
1243 }
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001244 pgprintk("%s: looking gfn %lx role %x\n", __func__,
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001245 gfn, role.word);
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001246 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001247 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001248 hlist_for_each_entry_safe(sp, node, tmp, bucket, hash_link)
1249 if (sp->gfn == gfn) {
1250 if (sp->unsync)
1251 if (kvm_sync_page(vcpu, sp))
1252 continue;
1253
1254 if (sp->role.word != role.word)
1255 continue;
1256
Avi Kivity4db35312007-11-21 15:28:32 +02001257 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Marcelo Tosatti0074ff62008-09-23 13:18:40 -03001258 if (sp->unsync_children) {
1259 set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
1260 kvm_mmu_mark_parents_unsync(vcpu, sp);
1261 }
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001262 pgprintk("%s: found\n", __func__);
Avi Kivity4db35312007-11-21 15:28:32 +02001263 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001264 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +02001265 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +02001266 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
1267 if (!sp)
1268 return sp;
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001269 pgprintk("%s: adding gfn %lx role %x\n", __func__, gfn, role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001270 sp->gfn = gfn;
1271 sp->role = role;
1272 hlist_add_head(&sp->hash_link, bucket);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001273 if (!direct) {
Marcelo Tosattib1a36822008-12-01 22:32:03 -02001274 if (rmap_write_protect(vcpu->kvm, gfn))
1275 kvm_flush_remote_tlbs(vcpu->kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001276 account_shadowed(vcpu->kvm, gfn);
1277 }
Avi Kivity131d8272008-05-29 14:56:28 +03001278 if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
1279 vcpu->arch.mmu.prefetch_page(vcpu, sp);
1280 else
1281 nonpaging_prefetch_page(vcpu, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001282 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001283}
1284
Avi Kivity2d111232008-12-25 14:39:47 +02001285static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
1286 struct kvm_vcpu *vcpu, u64 addr)
1287{
1288 iterator->addr = addr;
1289 iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
1290 iterator->level = vcpu->arch.mmu.shadow_root_level;
1291 if (iterator->level == PT32E_ROOT_LEVEL) {
1292 iterator->shadow_addr
1293 = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
1294 iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
1295 --iterator->level;
1296 if (!iterator->shadow_addr)
1297 iterator->level = 0;
1298 }
1299}
1300
1301static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
1302{
1303 if (iterator->level < PT_PAGE_TABLE_LEVEL)
1304 return false;
Marcelo Tosatti4d889542009-06-11 12:07:41 -03001305
1306 if (iterator->level == PT_PAGE_TABLE_LEVEL)
1307 if (is_large_pte(*iterator->sptep))
1308 return false;
1309
Avi Kivity2d111232008-12-25 14:39:47 +02001310 iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
1311 iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
1312 return true;
1313}
1314
1315static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
1316{
1317 iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
1318 --iterator->level;
1319}
1320
Avi Kivity90cb0522007-07-17 13:04:56 +03001321static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +02001322 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001323{
Avi Kivity697fe2e2007-01-05 16:36:46 -08001324 unsigned i;
1325 u64 *pt;
1326 u64 ent;
1327
Avi Kivity4db35312007-11-21 15:28:32 +02001328 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001329
Avi Kivity697fe2e2007-01-05 16:36:46 -08001330 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1331 ent = pt[i];
1332
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001333 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001334 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001335 ent &= PT64_BASE_ADDR_MASK;
1336 mmu_page_remove_parent_pte(page_header(ent),
1337 &pt[i]);
1338 } else {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03001339 if (is_large_pte(ent))
1340 --kvm->stat.lpages;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001341 rmap_remove(kvm, &pt[i]);
1342 }
1343 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001344 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -08001345 }
Avi Kivitya4360362007-01-05 16:36:45 -08001346}
1347
Avi Kivity4db35312007-11-21 15:28:32 +02001348static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001349{
Avi Kivity4db35312007-11-21 15:28:32 +02001350 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001351}
1352
Avi Kivity12b7d282007-09-23 14:10:49 +02001353static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
1354{
1355 int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001356 struct kvm_vcpu *vcpu;
Avi Kivity12b7d282007-09-23 14:10:49 +02001357
Gleb Natapov988a2ca2009-06-09 15:56:29 +03001358 kvm_for_each_vcpu(i, vcpu, kvm)
1359 vcpu->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +02001360}
1361
Avi Kivity31aa2b42008-07-11 17:59:46 +03001362static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -08001363{
1364 u64 *parent_pte;
1365
Avi Kivity4db35312007-11-21 15:28:32 +02001366 while (sp->multimapped || sp->parent_pte) {
1367 if (!sp->multimapped)
1368 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -08001369 else {
1370 struct kvm_pte_chain *chain;
1371
Avi Kivity4db35312007-11-21 15:28:32 +02001372 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -08001373 struct kvm_pte_chain, link);
1374 parent_pte = chain->parent_ptes[0];
1375 }
Avi Kivity697fe2e2007-01-05 16:36:46 -08001376 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +02001377 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityd555c332009-06-10 14:24:23 +03001378 __set_spte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -08001379 }
Avi Kivity31aa2b42008-07-11 17:59:46 +03001380}
1381
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001382static int mmu_zap_unsync_children(struct kvm *kvm,
1383 struct kvm_mmu_page *parent)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001384{
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001385 int i, zapped = 0;
1386 struct mmu_page_path parents;
1387 struct kvm_mmu_pages pages;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001388
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001389 if (parent->role.level == PT_PAGE_TABLE_LEVEL)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001390 return 0;
Marcelo Tosatti60c8aec2008-12-01 22:32:02 -02001391
1392 kvm_mmu_pages_init(parent, &parents, &pages);
1393 while (mmu_unsync_walk(parent, &pages)) {
1394 struct kvm_mmu_page *sp;
1395
1396 for_each_sp(pages, sp, parents, i) {
1397 kvm_mmu_zap_page(kvm, sp);
1398 mmu_pages_clear_parents(&parents);
1399 }
1400 zapped += pages.nr;
1401 kvm_mmu_pages_init(parent, &parents, &pages);
1402 }
1403
1404 return zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001405}
1406
Marcelo Tosatti07385412008-09-23 13:18:37 -03001407static int kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity31aa2b42008-07-11 17:59:46 +03001408{
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001409 int ret;
Avi Kivity31aa2b42008-07-11 17:59:46 +03001410 ++kvm->stat.mmu_shadow_zapped;
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001411 ret = mmu_zap_unsync_children(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001412 kvm_mmu_page_unlink_children(kvm, sp);
Avi Kivity31aa2b42008-07-11 17:59:46 +03001413 kvm_mmu_unlink_parents(kvm, sp);
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001414 kvm_flush_remote_tlbs(kvm);
Avi Kivityf6e2c022009-01-11 13:02:10 +02001415 if (!sp->role.invalid && !sp->role.direct)
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001416 unaccount_shadowed(kvm, sp->gfn);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001417 if (sp->unsync)
1418 kvm_unlink_unsync_page(kvm, sp);
Avi Kivity4db35312007-11-21 15:28:32 +02001419 if (!sp->root_count) {
1420 hlist_del(&sp->hash_link);
1421 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001422 } else {
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001423 sp->role.invalid = 1;
Avi Kivity5b5c6a52008-07-11 18:07:26 +03001424 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001425 kvm_reload_remote_mmus(kvm);
1426 }
Avi Kivity12b7d282007-09-23 14:10:49 +02001427 kvm_mmu_reset_last_pte_updated(kvm);
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001428 return ret;
Avi Kivitya4360362007-01-05 16:36:45 -08001429}
1430
Izik Eidus82ce2c92007-10-02 18:52:55 +02001431/*
1432 * Changing the number of mmu pages allocated to the vm
1433 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
1434 */
1435void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
1436{
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001437 int used_pages;
1438
1439 used_pages = kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages;
1440 used_pages = max(0, used_pages);
1441
Izik Eidus82ce2c92007-10-02 18:52:55 +02001442 /*
1443 * If we set the number of mmu pages to be smaller be than the
1444 * number of actived pages , we must to free some mmu pages before we
1445 * change the value
1446 */
1447
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001448 if (used_pages > kvm_nr_mmu_pages) {
1449 while (used_pages > kvm_nr_mmu_pages) {
Izik Eidus82ce2c92007-10-02 18:52:55 +02001450 struct kvm_mmu_page *page;
1451
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001452 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +02001453 struct kvm_mmu_page, link);
1454 kvm_mmu_zap_page(kvm, page);
Marcelo Tosatti025dbbf2009-07-22 13:05:49 -03001455 used_pages--;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001456 }
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001457 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001458 }
1459 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001460 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
1461 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001462
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001463 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001464}
1465
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001466static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -08001467{
1468 unsigned index;
1469 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001470 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -08001471 struct hlist_node *node, *n;
1472 int r;
1473
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001474 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -08001475 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001476 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001477 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001478 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001479 if (sp->gfn == gfn && !sp->role.direct) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001480 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02001481 sp->role.word);
Avi Kivitya4360362007-01-05 16:36:45 -08001482 r = 1;
Marcelo Tosatti07385412008-09-23 13:18:37 -03001483 if (kvm_mmu_zap_page(kvm, sp))
1484 n = bucket->first;
Avi Kivitya4360362007-01-05 16:36:45 -08001485 }
1486 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001487}
1488
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001489static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001490{
Avi Kivity4677a3b2009-01-06 13:00:27 +02001491 unsigned index;
1492 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +02001493 struct kvm_mmu_page *sp;
Avi Kivity4677a3b2009-01-06 13:00:27 +02001494 struct hlist_node *node, *nn;
Avi Kivity97a0a012007-05-31 15:08:29 +03001495
Avi Kivity4677a3b2009-01-06 13:00:27 +02001496 index = kvm_page_table_hashfn(gfn);
1497 bucket = &kvm->arch.mmu_page_hash[index];
1498 hlist_for_each_entry_safe(sp, node, nn, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02001499 if (sp->gfn == gfn && !sp->role.direct
Avi Kivity4677a3b2009-01-06 13:00:27 +02001500 && !sp->role.invalid) {
1501 pgprintk("%s: zap %lx %x\n",
1502 __func__, gfn, sp->role.word);
1503 kvm_mmu_zap_page(kvm, sp);
1504 }
Avi Kivity97a0a012007-05-31 15:08:29 +03001505 }
1506}
1507
Avi Kivity38c335f2007-11-21 14:20:22 +02001508static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001509{
Avi Kivity38c335f2007-11-21 14:20:22 +02001510 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity4db35312007-11-21 15:28:32 +02001511 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001512
Sheng Yang291f26b2008-10-16 17:30:57 +08001513 __set_bit(slot, sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001514}
1515
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001516static void mmu_convert_notrap(struct kvm_mmu_page *sp)
1517{
1518 int i;
1519 u64 *pt = sp->spt;
1520
1521 if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
1522 return;
1523
1524 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1525 if (pt[i] == shadow_notrap_nonpresent_pte)
Avi Kivityd555c332009-06-10 14:24:23 +03001526 __set_spte(&pt[i], shadow_trap_nonpresent_pte);
Marcelo Tosatti6844dec2008-09-23 13:18:38 -03001527 }
1528}
1529
Avi Kivity039576c2007-03-20 12:46:50 +02001530struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1531{
Izik Eidus72dc67a2008-02-10 18:04:15 +02001532 struct page *page;
1533
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001534 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Avi Kivity039576c2007-03-20 12:46:50 +02001535
1536 if (gpa == UNMAPPED_GVA)
1537 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001538
Izik Eidus72dc67a2008-02-10 18:04:15 +02001539 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001540
1541 return page;
Avi Kivity039576c2007-03-20 12:46:50 +02001542}
1543
Sheng Yang74be52e2008-10-09 16:01:56 +08001544/*
1545 * The function is based on mtrr_type_lookup() in
1546 * arch/x86/kernel/cpu/mtrr/generic.c
1547 */
1548static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
1549 u64 start, u64 end)
1550{
1551 int i;
1552 u64 base, mask;
1553 u8 prev_match, curr_match;
1554 int num_var_ranges = KVM_NR_VAR_MTRR;
1555
1556 if (!mtrr_state->enabled)
1557 return 0xFF;
1558
1559 /* Make end inclusive end, instead of exclusive */
1560 end--;
1561
1562 /* Look in fixed ranges. Just return the type as per start */
1563 if (mtrr_state->have_fixed && (start < 0x100000)) {
1564 int idx;
1565
1566 if (start < 0x80000) {
1567 idx = 0;
1568 idx += (start >> 16);
1569 return mtrr_state->fixed_ranges[idx];
1570 } else if (start < 0xC0000) {
1571 idx = 1 * 8;
1572 idx += ((start - 0x80000) >> 14);
1573 return mtrr_state->fixed_ranges[idx];
1574 } else if (start < 0x1000000) {
1575 idx = 3 * 8;
1576 idx += ((start - 0xC0000) >> 12);
1577 return mtrr_state->fixed_ranges[idx];
1578 }
1579 }
1580
1581 /*
1582 * Look in variable ranges
1583 * Look of multiple ranges matching this address and pick type
1584 * as per MTRR precedence
1585 */
1586 if (!(mtrr_state->enabled & 2))
1587 return mtrr_state->def_type;
1588
1589 prev_match = 0xFF;
1590 for (i = 0; i < num_var_ranges; ++i) {
1591 unsigned short start_state, end_state;
1592
1593 if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
1594 continue;
1595
1596 base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
1597 (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
1598 mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
1599 (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
1600
1601 start_state = ((start & mask) == (base & mask));
1602 end_state = ((end & mask) == (base & mask));
1603 if (start_state != end_state)
1604 return 0xFE;
1605
1606 if ((start & mask) != (base & mask))
1607 continue;
1608
1609 curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
1610 if (prev_match == 0xFF) {
1611 prev_match = curr_match;
1612 continue;
1613 }
1614
1615 if (prev_match == MTRR_TYPE_UNCACHABLE ||
1616 curr_match == MTRR_TYPE_UNCACHABLE)
1617 return MTRR_TYPE_UNCACHABLE;
1618
1619 if ((prev_match == MTRR_TYPE_WRBACK &&
1620 curr_match == MTRR_TYPE_WRTHROUGH) ||
1621 (prev_match == MTRR_TYPE_WRTHROUGH &&
1622 curr_match == MTRR_TYPE_WRBACK)) {
1623 prev_match = MTRR_TYPE_WRTHROUGH;
1624 curr_match = MTRR_TYPE_WRTHROUGH;
1625 }
1626
1627 if (prev_match != curr_match)
1628 return MTRR_TYPE_UNCACHABLE;
1629 }
1630
1631 if (prev_match != 0xFF)
1632 return prev_match;
1633
1634 return mtrr_state->def_type;
1635}
1636
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001637u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
Sheng Yang74be52e2008-10-09 16:01:56 +08001638{
1639 u8 mtrr;
1640
1641 mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
1642 (gfn << PAGE_SHIFT) + PAGE_SIZE);
1643 if (mtrr == 0xfe || mtrr == 0xff)
1644 mtrr = MTRR_TYPE_WRBACK;
1645 return mtrr;
1646}
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001647EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
Sheng Yang74be52e2008-10-09 16:01:56 +08001648
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001649static int kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
1650{
1651 unsigned index;
1652 struct hlist_head *bucket;
1653 struct kvm_mmu_page *s;
1654 struct hlist_node *node, *n;
1655
1656 index = kvm_page_table_hashfn(sp->gfn);
1657 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
1658 /* don't unsync if pagetable is shadowed with multiple roles */
1659 hlist_for_each_entry_safe(s, node, n, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02001660 if (s->gfn != sp->gfn || s->role.direct)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001661 continue;
1662 if (s->role.word != sp->role.word)
1663 return 1;
1664 }
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001665 ++vcpu->kvm->stat.mmu_unsync;
1666 sp->unsync = 1;
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001667
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001668 kvm_mmu_mark_parents_unsync(vcpu, sp);
Marcelo Tosatti6cffe8c2008-12-01 22:32:04 -02001669
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001670 mmu_convert_notrap(sp);
1671 return 0;
1672}
1673
1674static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
1675 bool can_unsync)
1676{
1677 struct kvm_mmu_page *shadow;
1678
1679 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
1680 if (shadow) {
1681 if (shadow->role.level != PT_PAGE_TABLE_LEVEL)
1682 return 1;
1683 if (shadow->unsync)
1684 return 0;
Marcelo Tosatti582801a2008-09-23 13:18:41 -03001685 if (can_unsync && oos_shadow)
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001686 return kvm_unsync_page(vcpu, shadow);
1687 return 1;
1688 }
1689 return 0;
1690}
1691
Avi Kivityd555c332009-06-10 14:24:23 +03001692static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001693 unsigned pte_access, int user_fault,
1694 int write_fault, int dirty, int largepage,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001695 gfn_t gfn, pfn_t pfn, bool speculative,
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001696 bool can_unsync)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001697{
1698 u64 spte;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001699 int ret = 0;
Sheng Yang64d4d522008-10-09 16:01:57 +08001700
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001701 /*
1702 * We don't set the accessed bit, since we sometimes want to see
1703 * whether the guest actually used the pte (in order to detect
1704 * demand paging).
1705 */
Sheng Yang7b523452008-04-25 21:13:50 +08001706 spte = shadow_base_present_pte | shadow_dirty_mask;
Avi Kivity947da532008-03-18 11:05:52 +02001707 if (!speculative)
Avi Kivity3201b5d2008-08-27 20:01:04 +03001708 spte |= shadow_accessed_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001709 if (!dirty)
1710 pte_access &= ~ACC_WRITE_MASK;
Sheng Yang7b523452008-04-25 21:13:50 +08001711 if (pte_access & ACC_EXEC_MASK)
1712 spte |= shadow_x_mask;
1713 else
1714 spte |= shadow_nx_mask;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001715 if (pte_access & ACC_USER_MASK)
Sheng Yang7b523452008-04-25 21:13:50 +08001716 spte |= shadow_user_mask;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001717 if (largepage)
1718 spte |= PT_PAGE_SIZE_MASK;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08001719 if (tdp_enabled)
1720 spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
1721 kvm_is_mmio_pfn(pfn));
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001722
Anthony Liguori35149e22008-04-02 14:46:56 -05001723 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001724
1725 if ((pte_access & ACC_WRITE_MASK)
1726 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001727
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001728 if (largepage && has_wrprotected_page(vcpu->kvm, gfn)) {
1729 ret = 1;
1730 spte = shadow_trap_nonpresent_pte;
1731 goto set_pte;
1732 }
1733
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001734 spte |= PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001735
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001736 /*
1737 * Optimization: for pte sync, if spte was writable the hash
1738 * lookup is unnecessary (and expensive). Write protection
1739 * is responsibility of mmu_get_page / kvm_sync_page.
1740 * Same reasoning can be applied to dirty page accounting.
1741 */
Avi Kivityd555c332009-06-10 14:24:23 +03001742 if (!can_unsync && is_writeble_pte(*sptep))
Marcelo Tosattiecc55892008-11-25 15:58:07 +01001743 goto set_pte;
1744
Marcelo Tosatti4731d4c2008-09-23 13:18:39 -03001745 if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001746 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001747 __func__, gfn);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001748 ret = 1;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001749 pte_access &= ~ACC_WRITE_MASK;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001750 if (is_writeble_pte(spte))
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001751 spte &= ~PT_WRITABLE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001752 }
1753 }
1754
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001755 if (pte_access & ACC_WRITE_MASK)
1756 mark_page_dirty(vcpu->kvm, gfn);
1757
Marcelo Tosatti38187c82008-09-23 13:18:32 -03001758set_pte:
Avi Kivityd555c332009-06-10 14:24:23 +03001759 __set_spte(sptep, spte);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001760 return ret;
1761}
1762
Avi Kivityd555c332009-06-10 14:24:23 +03001763static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001764 unsigned pt_access, unsigned pte_access,
1765 int user_fault, int write_fault, int dirty,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001766 int *ptwrite, int largepage, gfn_t gfn,
1767 pfn_t pfn, bool speculative)
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001768{
1769 int was_rmapped = 0;
Avi Kivityd555c332009-06-10 14:24:23 +03001770 int was_writeble = is_writeble_pte(*sptep);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001771 int rmap_count;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001772
1773 pgprintk("%s: spte %llx access %x write_fault %d"
1774 " user_fault %d gfn %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001775 __func__, *sptep, pt_access,
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001776 write_fault, user_fault, gfn);
1777
Avi Kivityd555c332009-06-10 14:24:23 +03001778 if (is_rmap_spte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001779 /*
1780 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1781 * the parent of the now unreachable PTE.
1782 */
Avi Kivityd555c332009-06-10 14:24:23 +03001783 if (largepage && !is_large_pte(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001784 struct kvm_mmu_page *child;
Avi Kivityd555c332009-06-10 14:24:23 +03001785 u64 pte = *sptep;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001786
1787 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivityd555c332009-06-10 14:24:23 +03001788 mmu_page_remove_parent_pte(child, sptep);
1789 } else if (pfn != spte_to_pfn(*sptep)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001790 pgprintk("hfn old %lx new %lx\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001791 spte_to_pfn(*sptep), pfn);
1792 rmap_remove(vcpu->kvm, sptep);
Joerg Roedel6bed6b92009-02-18 14:08:59 +01001793 } else
1794 was_rmapped = 1;
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001795 }
Avi Kivityd555c332009-06-10 14:24:23 +03001796 if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001797 dirty, largepage, gfn, pfn, speculative, true)) {
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001798 if (write_fault)
1799 *ptwrite = 1;
Marcelo Tosattia378b4e2008-09-23 13:18:31 -03001800 kvm_x86_ops->tlb_flush(vcpu);
1801 }
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001802
Avi Kivityd555c332009-06-10 14:24:23 +03001803 pgprintk("%s: setting spte %llx\n", __func__, *sptep);
Marcelo Tosatti1e73f9d2008-09-23 13:18:30 -03001804 pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
Avi Kivityd555c332009-06-10 14:24:23 +03001805 is_large_pte(*sptep)? "2MB" : "4kB",
1806 is_present_pte(*sptep)?"RW":"R", gfn,
1807 *shadow_pte, sptep);
1808 if (!was_rmapped && is_large_pte(*sptep))
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001809 ++vcpu->kvm->stat.lpages;
1810
Avi Kivityd555c332009-06-10 14:24:23 +03001811 page_header_update_slot(vcpu->kvm, sptep, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001812 if (!was_rmapped) {
Avi Kivityd555c332009-06-10 14:24:23 +03001813 rmap_count = rmap_add(vcpu, sptep, gfn, largepage);
1814 if (!is_rmap_spte(*sptep))
Anthony Liguori35149e22008-04-02 14:46:56 -05001815 kvm_release_pfn_clean(pfn);
Marcelo Tosatti53a27b32009-08-05 15:43:58 -03001816 if (rmap_count > RMAP_RECYCLE_THRESHOLD)
1817 rmap_recycle(vcpu, gfn, largepage);
Izik Eidus75e68e62008-01-12 23:49:09 +02001818 } else {
1819 if (was_writeble)
Anthony Liguori35149e22008-04-02 14:46:56 -05001820 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001821 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001822 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001823 }
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001824 if (speculative) {
Avi Kivityd555c332009-06-10 14:24:23 +03001825 vcpu->arch.last_pte_updated = sptep;
Avi Kivity1b7fcd32008-05-15 13:51:35 +03001826 vcpu->arch.last_pte_gfn = gfn;
1827 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001828}
1829
Avi Kivity6aa8b732006-12-10 02:21:36 -08001830static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1831{
1832}
1833
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001834static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Avi Kivity6c41f422008-08-26 16:16:08 +03001835 int largepage, gfn_t gfn, pfn_t pfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001836{
Avi Kivity9f652d22008-12-25 14:54:25 +02001837 struct kvm_shadow_walk_iterator iterator;
1838 struct kvm_mmu_page *sp;
1839 int pt_write = 0;
1840 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001841
Avi Kivity9f652d22008-12-25 14:54:25 +02001842 for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
1843 if (iterator.level == PT_PAGE_TABLE_LEVEL
1844 || (largepage && iterator.level == PT_DIRECTORY_LEVEL)) {
1845 mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
1846 0, write, 1, &pt_write,
Marcelo Tosattic2d0ee42009-04-05 14:54:47 -03001847 largepage, gfn, pfn, false);
Avi Kivity9f652d22008-12-25 14:54:25 +02001848 ++vcpu->stat.pf_fixed;
1849 break;
1850 }
1851
1852 if (*iterator.sptep == shadow_trap_nonpresent_pte) {
1853 pseudo_gfn = (iterator.addr & PT64_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
1854 sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
1855 iterator.level - 1,
1856 1, ACC_ALL, iterator.sptep);
1857 if (!sp) {
1858 pgprintk("nonpaging_map: ENOMEM\n");
1859 kvm_release_pfn_clean(pfn);
1860 return -ENOMEM;
1861 }
1862
Avi Kivityd555c332009-06-10 14:24:23 +03001863 __set_spte(iterator.sptep,
1864 __pa(sp->spt)
1865 | PT_PRESENT_MASK | PT_WRITABLE_MASK
1866 | shadow_user_mask | shadow_x_mask);
Avi Kivity9f652d22008-12-25 14:54:25 +02001867 }
1868 }
1869 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001870}
1871
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001872static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1873{
1874 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001875 int largepage = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05001876 pfn_t pfn;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001877 unsigned long mmu_seq;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001878
Joerg Roedelec04b262009-06-19 15:16:23 +02001879 if (is_largepage_backed(vcpu, gfn &
1880 ~(KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL) - 1))) {
1881 gfn &= ~(KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL) - 1);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001882 largepage = 1;
1883 }
1884
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001885 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03001886 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05001887 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001888
Avi Kivityd196e342008-01-24 11:44:11 +02001889 /* mmio */
Anthony Liguori35149e22008-04-02 14:46:56 -05001890 if (is_error_pfn(pfn)) {
1891 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02001892 return 1;
1893 }
1894
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001895 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001896 if (mmu_notifier_retry(vcpu, mmu_seq))
1897 goto out_unlock;
Avi Kivityeb787d12007-12-31 15:27:49 +02001898 kvm_mmu_free_some_pages(vcpu);
Avi Kivity6c41f422008-08-26 16:16:08 +03001899 r = __direct_map(vcpu, v, write, largepage, gfn, pfn);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001900 spin_unlock(&vcpu->kvm->mmu_lock);
1901
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001902
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001903 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02001904
1905out_unlock:
1906 spin_unlock(&vcpu->kvm->mmu_lock);
1907 kvm_release_pfn_clean(pfn);
1908 return 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001909}
1910
1911
Avi Kivity17ac10a2007-01-05 16:36:40 -08001912static void mmu_free_roots(struct kvm_vcpu *vcpu)
1913{
1914 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02001915 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001916
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001917 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03001918 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001919 spin_lock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001920 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1921 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001922
Avi Kivity4db35312007-11-21 15:28:32 +02001923 sp = page_header(root);
1924 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001925 if (!sp->root_count && sp->role.invalid)
1926 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001927 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001928 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001929 return;
1930 }
Avi Kivity17ac10a2007-01-05 16:36:40 -08001931 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001932 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001933
Avi Kivity417726a2007-04-12 17:35:58 +03001934 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03001935 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001936 sp = page_header(root);
1937 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001938 if (!sp->root_count && sp->role.invalid)
1939 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03001940 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001941 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001942 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001943 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001944 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001945}
1946
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03001947static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
1948{
1949 int ret = 0;
1950
1951 if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
1952 set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
1953 ret = 1;
1954 }
1955
1956 return ret;
1957}
1958
1959static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
Avi Kivity17ac10a2007-01-05 16:36:40 -08001960{
1961 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001962 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02001963 struct kvm_mmu_page *sp;
Avi Kivityf6e2c022009-01-11 13:02:10 +02001964 int direct = 0;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001965 u64 pdptr;
Avi Kivity3bb65a22007-01-05 16:36:51 -08001966
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001967 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001968
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001969 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1970 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001971
1972 ASSERT(!VALID_PAGE(root));
Joerg Roedelfb72d162008-02-07 13:47:44 +01001973 if (tdp_enabled)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001974 direct = 1;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03001975 if (mmu_check_root(vcpu, root_gfn))
1976 return 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001977 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Avi Kivityf6e2c022009-01-11 13:02:10 +02001978 PT64_ROOT_LEVEL, direct,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001979 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001980 root = __pa(sp->spt);
1981 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001982 vcpu->arch.mmu.root_hpa = root;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03001983 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001984 }
Avi Kivityf6e2c022009-01-11 13:02:10 +02001985 direct = !is_paging(vcpu);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001986 if (tdp_enabled)
Avi Kivityf6e2c022009-01-11 13:02:10 +02001987 direct = 1;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001988 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001989 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001990
1991 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001992 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001993 pdptr = kvm_pdptr_read(vcpu, i);
Avi Kivity43a37952009-06-10 14:12:05 +03001994 if (!is_present_gpte(pdptr)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001995 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03001996 continue;
1997 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001998 root_gfn = pdptr >> PAGE_SHIFT;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001999 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002000 root_gfn = 0;
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002001 if (mmu_check_root(vcpu, root_gfn))
2002 return 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002003 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Avi Kivityf6e2c022009-01-11 13:02:10 +02002004 PT32_ROOT_LEVEL, direct,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02002005 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02002006 root = __pa(sp->spt);
2007 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002008 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002009 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002010 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002011 return 0;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002012}
2013
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002014static void mmu_sync_roots(struct kvm_vcpu *vcpu)
2015{
2016 int i;
2017 struct kvm_mmu_page *sp;
2018
2019 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2020 return;
2021 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
2022 hpa_t root = vcpu->arch.mmu.root_hpa;
2023 sp = page_header(root);
2024 mmu_sync_children(vcpu, sp);
2025 return;
2026 }
2027 for (i = 0; i < 4; ++i) {
2028 hpa_t root = vcpu->arch.mmu.pae_root[i];
2029
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002030 if (root && VALID_PAGE(root)) {
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002031 root &= PT64_BASE_ADDR_MASK;
2032 sp = page_header(root);
2033 mmu_sync_children(vcpu, sp);
2034 }
2035 }
2036}
2037
2038void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
2039{
2040 spin_lock(&vcpu->kvm->mmu_lock);
2041 mmu_sync_roots(vcpu);
2042 spin_unlock(&vcpu->kvm->mmu_lock);
2043}
2044
Avi Kivity6aa8b732006-12-10 02:21:36 -08002045static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
2046{
2047 return vaddr;
2048}
2049
2050static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02002051 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002052{
Avi Kivitye8332402007-12-09 18:43:00 +02002053 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08002054 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002055
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002056 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08002057 r = mmu_topup_memory_caches(vcpu);
2058 if (r)
2059 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08002060
Avi Kivity6aa8b732006-12-10 02:21:36 -08002061 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002062 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002063
Avi Kivitye8332402007-12-09 18:43:00 +02002064 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002065
Avi Kivitye8332402007-12-09 18:43:00 +02002066 return nonpaging_map(vcpu, gva & PAGE_MASK,
2067 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002068}
2069
Joerg Roedelfb72d162008-02-07 13:47:44 +01002070static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
2071 u32 error_code)
2072{
Anthony Liguori35149e22008-04-02 14:46:56 -05002073 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002074 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002075 int largepage = 0;
2076 gfn_t gfn = gpa >> PAGE_SHIFT;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002077 unsigned long mmu_seq;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002078
2079 ASSERT(vcpu);
2080 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
2081
2082 r = mmu_topup_memory_caches(vcpu);
2083 if (r)
2084 return r;
2085
Joerg Roedelec04b262009-06-19 15:16:23 +02002086 if (is_largepage_backed(vcpu, gfn &
2087 ~(KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL) - 1))) {
2088 gfn &= ~(KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL) - 1);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002089 largepage = 1;
2090 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002091 mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002092 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002093 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Anthony Liguori35149e22008-04-02 14:46:56 -05002094 if (is_error_pfn(pfn)) {
2095 kvm_release_pfn_clean(pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002096 return 1;
2097 }
2098 spin_lock(&vcpu->kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002099 if (mmu_notifier_retry(vcpu, mmu_seq))
2100 goto out_unlock;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002101 kvm_mmu_free_some_pages(vcpu);
2102 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Avi Kivity6c41f422008-08-26 16:16:08 +03002103 largepage, gfn, pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002104 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002105
2106 return r;
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002107
2108out_unlock:
2109 spin_unlock(&vcpu->kvm->mmu_lock);
2110 kvm_release_pfn_clean(pfn);
2111 return 0;
Joerg Roedelfb72d162008-02-07 13:47:44 +01002112}
2113
Avi Kivity6aa8b732006-12-10 02:21:36 -08002114static void nonpaging_free(struct kvm_vcpu *vcpu)
2115{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002116 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002117}
2118
2119static int nonpaging_init_context(struct kvm_vcpu *vcpu)
2120{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002121 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002122
2123 context->new_cr3 = nonpaging_new_cr3;
2124 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002125 context->gva_to_gpa = nonpaging_gva_to_gpa;
2126 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002127 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002128 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002129 context->invlpg = nonpaging_invlpg;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002130 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002131 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002132 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002133 return 0;
2134}
2135
Avi Kivityd835dfe2007-11-21 02:57:59 +02002136void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002137{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002138 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002139 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002140}
2141
2142static void paging_new_cr3(struct kvm_vcpu *vcpu)
2143{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002144 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08002145 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002146}
2147
Avi Kivity6aa8b732006-12-10 02:21:36 -08002148static void inject_page_fault(struct kvm_vcpu *vcpu,
2149 u64 addr,
2150 u32 err_code)
2151{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02002152 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002153}
2154
Avi Kivity6aa8b732006-12-10 02:21:36 -08002155static void paging_free(struct kvm_vcpu *vcpu)
2156{
2157 nonpaging_free(vcpu);
2158}
2159
Dong, Eddie82725b22009-03-30 16:21:08 +08002160static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level)
2161{
2162 int bit7;
2163
2164 bit7 = (gpte >> 7) & 1;
2165 return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0;
2166}
2167
Avi Kivity6aa8b732006-12-10 02:21:36 -08002168#define PTTYPE 64
2169#include "paging_tmpl.h"
2170#undef PTTYPE
2171
2172#define PTTYPE 32
2173#include "paging_tmpl.h"
2174#undef PTTYPE
2175
Dong, Eddie82725b22009-03-30 16:21:08 +08002176static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level)
2177{
2178 struct kvm_mmu *context = &vcpu->arch.mmu;
2179 int maxphyaddr = cpuid_maxphyaddr(vcpu);
2180 u64 exb_bit_rsvd = 0;
2181
2182 if (!is_nx(vcpu))
2183 exb_bit_rsvd = rsvd_bits(63, 63);
2184 switch (level) {
2185 case PT32_ROOT_LEVEL:
2186 /* no rsvd bits for 2 level 4K page table entries */
2187 context->rsvd_bits_mask[0][1] = 0;
2188 context->rsvd_bits_mask[0][0] = 0;
2189 if (is_cpuid_PSE36())
2190 /* 36bits PSE 4MB page */
2191 context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
2192 else
2193 /* 32 bits PSE 4MB page */
2194 context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
Avi Kivity29a4b932009-05-19 13:29:27 +03002195 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002196 break;
2197 case PT32E_ROOT_LEVEL:
Dong, Eddie20c466b2009-03-31 23:03:45 +08002198 context->rsvd_bits_mask[0][2] =
2199 rsvd_bits(maxphyaddr, 63) |
2200 rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002201 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002202 rsvd_bits(maxphyaddr, 62); /* PDE */
Dong, Eddie82725b22009-03-30 16:21:08 +08002203 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2204 rsvd_bits(maxphyaddr, 62); /* PTE */
2205 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
2206 rsvd_bits(maxphyaddr, 62) |
2207 rsvd_bits(13, 20); /* large page */
Avi Kivity29a4b932009-05-19 13:29:27 +03002208 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002209 break;
2210 case PT64_ROOT_LEVEL:
2211 context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
2212 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2213 context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
2214 rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
2215 context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002216 rsvd_bits(maxphyaddr, 51);
Dong, Eddie82725b22009-03-30 16:21:08 +08002217 context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
2218 rsvd_bits(maxphyaddr, 51);
2219 context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
2220 context->rsvd_bits_mask[1][2] = context->rsvd_bits_mask[0][2];
2221 context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
Sheng Yang4c26b4c2009-04-02 10:28:37 +08002222 rsvd_bits(maxphyaddr, 51) |
2223 rsvd_bits(13, 20); /* large page */
Avi Kivity29a4b932009-05-19 13:29:27 +03002224 context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[1][0];
Dong, Eddie82725b22009-03-30 16:21:08 +08002225 break;
2226 }
2227}
2228
Avi Kivity17ac10a2007-01-05 16:36:40 -08002229static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002230{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002231 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002232
2233 ASSERT(is_pae(vcpu));
2234 context->new_cr3 = paging_new_cr3;
2235 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002236 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02002237 context->prefetch_page = paging64_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002238 context->sync_page = paging64_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002239 context->invlpg = paging64_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002240 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002241 context->root_level = level;
2242 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002243 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002244 return 0;
2245}
2246
Avi Kivity17ac10a2007-01-05 16:36:40 -08002247static int paging64_init_context(struct kvm_vcpu *vcpu)
2248{
Dong, Eddie82725b22009-03-30 16:21:08 +08002249 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002250 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
2251}
2252
Avi Kivity6aa8b732006-12-10 02:21:36 -08002253static int paging32_init_context(struct kvm_vcpu *vcpu)
2254{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002255 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002256
Dong, Eddie82725b22009-03-30 16:21:08 +08002257 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002258 context->new_cr3 = paging_new_cr3;
2259 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002260 context->gva_to_gpa = paging32_gva_to_gpa;
2261 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02002262 context->prefetch_page = paging32_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002263 context->sync_page = paging32_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002264 context->invlpg = paging32_invlpg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002265 context->root_level = PT32_ROOT_LEVEL;
2266 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03002267 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002268 return 0;
2269}
2270
2271static int paging32E_init_context(struct kvm_vcpu *vcpu)
2272{
Dong, Eddie82725b22009-03-30 16:21:08 +08002273 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002274 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002275}
2276
Joerg Roedelfb72d162008-02-07 13:47:44 +01002277static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
2278{
2279 struct kvm_mmu *context = &vcpu->arch.mmu;
2280
2281 context->new_cr3 = nonpaging_new_cr3;
2282 context->page_fault = tdp_page_fault;
2283 context->free = nonpaging_free;
2284 context->prefetch_page = nonpaging_prefetch_page;
Marcelo Tosattie8bc2172008-09-23 13:18:33 -03002285 context->sync_page = nonpaging_sync_page;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002286 context->invlpg = nonpaging_invlpg;
Sheng Yang67253af2008-04-25 10:20:22 +08002287 context->shadow_root_level = kvm_x86_ops->get_tdp_level();
Joerg Roedelfb72d162008-02-07 13:47:44 +01002288 context->root_hpa = INVALID_PAGE;
2289
2290 if (!is_paging(vcpu)) {
2291 context->gva_to_gpa = nonpaging_gva_to_gpa;
2292 context->root_level = 0;
2293 } else if (is_long_mode(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002294 reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002295 context->gva_to_gpa = paging64_gva_to_gpa;
2296 context->root_level = PT64_ROOT_LEVEL;
2297 } else if (is_pae(vcpu)) {
Dong, Eddie82725b22009-03-30 16:21:08 +08002298 reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002299 context->gva_to_gpa = paging64_gva_to_gpa;
2300 context->root_level = PT32E_ROOT_LEVEL;
2301 } else {
Dong, Eddie82725b22009-03-30 16:21:08 +08002302 reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01002303 context->gva_to_gpa = paging32_gva_to_gpa;
2304 context->root_level = PT32_ROOT_LEVEL;
2305 }
2306
2307 return 0;
2308}
2309
2310static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002311{
Avi Kivitya770f6f2008-12-21 19:20:09 +02002312 int r;
2313
Avi Kivity6aa8b732006-12-10 02:21:36 -08002314 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002315 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002316
2317 if (!is_paging(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002318 r = nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08002319 else if (is_long_mode(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002320 r = paging64_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002321 else if (is_pae(vcpu))
Avi Kivitya770f6f2008-12-21 19:20:09 +02002322 r = paging32E_init_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002323 else
Avi Kivitya770f6f2008-12-21 19:20:09 +02002324 r = paging32_init_context(vcpu);
2325
2326 vcpu->arch.mmu.base_role.glevels = vcpu->arch.mmu.root_level;
2327
2328 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002329}
2330
Joerg Roedelfb72d162008-02-07 13:47:44 +01002331static int init_kvm_mmu(struct kvm_vcpu *vcpu)
2332{
Anthony Liguori35149e22008-04-02 14:46:56 -05002333 vcpu->arch.update_pte.pfn = bad_pfn;
2334
Joerg Roedelfb72d162008-02-07 13:47:44 +01002335 if (tdp_enabled)
2336 return init_kvm_tdp_mmu(vcpu);
2337 else
2338 return init_kvm_softmmu(vcpu);
2339}
2340
Avi Kivity6aa8b732006-12-10 02:21:36 -08002341static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
2342{
2343 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002344 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
2345 vcpu->arch.mmu.free(vcpu);
2346 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002347 }
2348}
2349
2350int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
2351{
Avi Kivity17c3ba92007-06-04 15:58:30 +03002352 destroy_kvm_mmu(vcpu);
2353 return init_kvm_mmu(vcpu);
2354}
Eddie Dong8668a3c2007-10-10 14:26:45 +08002355EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002356
2357int kvm_mmu_load(struct kvm_vcpu *vcpu)
2358{
Avi Kivity714b93d2007-01-05 16:36:53 -08002359 int r;
2360
Avi Kivitye2dec932007-01-05 16:36:54 -08002361 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002362 if (r)
2363 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002364 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02002365 kvm_mmu_free_some_pages(vcpu);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002366 r = mmu_alloc_roots(vcpu);
Marcelo Tosatti0ba73cd2008-09-23 13:18:34 -03002367 mmu_sync_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002368 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti8986ecc2009-05-12 18:55:45 -03002369 if (r)
2370 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002371 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03002372 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002373out:
2374 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002375}
Avi Kivity17c3ba92007-06-04 15:58:30 +03002376EXPORT_SYMBOL_GPL(kvm_mmu_load);
2377
2378void kvm_mmu_unload(struct kvm_vcpu *vcpu)
2379{
2380 mmu_free_roots(vcpu);
2381}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002382
Avi Kivity09072da2007-05-01 14:16:52 +03002383static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002384 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02002385 u64 *spte)
2386{
2387 u64 pte;
2388 struct kvm_mmu_page *child;
2389
2390 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02002391 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti776e6632009-06-10 12:27:03 -03002392 if (is_last_spte(pte, sp->role.level))
Izik Eidus290fc382007-09-27 14:11:22 +02002393 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002394 else {
2395 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03002396 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002397 }
2398 }
Avi Kivityd555c332009-06-10 14:24:23 +03002399 __set_spte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002400 if (is_large_pte(pte))
2401 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02002402}
2403
Avi Kivity00284252007-05-01 16:53:31 +03002404static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02002405 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03002406 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02002407 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03002408{
Marcelo Tosatti30945382008-06-11 20:32:40 -03002409 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
2410 if (!vcpu->arch.update_pte.largepage ||
2411 sp->role.glevels == PT32_ROOT_LEVEL) {
2412 ++vcpu->kvm->stat.mmu_pde_zapped;
2413 return;
2414 }
2415 }
Avi Kivity00284252007-05-01 16:53:31 +03002416
Avi Kivity4cee5762007-11-18 16:37:07 +02002417 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02002418 if (sp->role.glevels == PT32_ROOT_LEVEL)
Dong, Eddie489f1d62008-01-07 11:14:20 +02002419 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002420 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02002421 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03002422}
2423
Avi Kivity79539ce2007-11-21 02:06:21 +02002424static bool need_remote_flush(u64 old, u64 new)
2425{
2426 if (!is_shadow_present_pte(old))
2427 return false;
2428 if (!is_shadow_present_pte(new))
2429 return true;
2430 if ((old ^ new) & PT64_BASE_ADDR_MASK)
2431 return true;
2432 old ^= PT64_NX_MASK;
2433 new ^= PT64_NX_MASK;
2434 return (old & ~new & PT64_PERM_MASK) != 0;
2435}
2436
2437static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
2438{
2439 if (need_remote_flush(old, new))
2440 kvm_flush_remote_tlbs(vcpu->kvm);
2441 else
2442 kvm_mmu_flush_tlb(vcpu);
2443}
2444
Avi Kivity12b7d282007-09-23 14:10:49 +02002445static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
2446{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002447 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02002448
Sheng Yang7b523452008-04-25 21:13:50 +08002449 return !!(spte && (*spte & shadow_accessed_mask));
Avi Kivity12b7d282007-09-23 14:10:49 +02002450}
2451
Avi Kivityd7824ff2007-12-30 12:29:05 +02002452static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
2453 const u8 *new, int bytes)
2454{
2455 gfn_t gfn;
2456 int r;
2457 u64 gpte = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05002458 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002459
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002460 vcpu->arch.update_pte.largepage = 0;
2461
Avi Kivityd7824ff2007-12-30 12:29:05 +02002462 if (bytes != 4 && bytes != 8)
2463 return;
2464
2465 /*
2466 * Assume that the pte write on a page table of the same type
2467 * as the current vcpu paging mode. This is nearly always true
2468 * (might be false while changing modes). Note it is verified later
2469 * by update_pte().
2470 */
2471 if (is_pae(vcpu)) {
2472 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
2473 if ((bytes == 4) && (gpa % 4 == 0)) {
2474 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
2475 if (r)
2476 return;
2477 memcpy((void *)&gpte + (gpa % 8), new, 4);
2478 } else if ((bytes == 8) && (gpa % 8 == 0)) {
2479 memcpy((void *)&gpte, new, 8);
2480 }
2481 } else {
2482 if ((bytes == 4) && (gpa % 4 == 0))
2483 memcpy((void *)&gpte, new, 4);
2484 }
Avi Kivity43a37952009-06-10 14:12:05 +03002485 if (!is_present_gpte(gpte))
Avi Kivityd7824ff2007-12-30 12:29:05 +02002486 return;
2487 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002488
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002489 if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) {
Joerg Roedelec04b262009-06-19 15:16:23 +02002490 gfn &= ~(KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL) - 1);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03002491 vcpu->arch.update_pte.largepage = 1;
2492 }
Andrea Arcangelie930bff2008-07-25 16:24:52 +02002493 vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
Marcelo Tosatti4c2155c2008-09-16 20:54:47 -03002494 smp_rmb();
Anthony Liguori35149e22008-04-02 14:46:56 -05002495 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02002496
Anthony Liguori35149e22008-04-02 14:46:56 -05002497 if (is_error_pfn(pfn)) {
2498 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02002499 return;
2500 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02002501 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05002502 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002503}
2504
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002505static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2506{
2507 u64 *spte = vcpu->arch.last_pte_updated;
2508
2509 if (spte
2510 && vcpu->arch.last_pte_gfn == gfn
2511 && shadow_accessed_mask
2512 && !(*spte & shadow_accessed_mask)
2513 && is_shadow_present_pte(*spte))
2514 set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
2515}
2516
Avi Kivity09072da2007-05-01 14:16:52 +03002517void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002518 const u8 *new, int bytes,
2519 bool guest_initiated)
Avi Kivityda4a00f2007-01-05 16:36:44 -08002520{
Avi Kivity9b7a0322007-01-05 16:36:45 -08002521 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02002522 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002523 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002524 struct hlist_head *bucket;
2525 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002526 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002527 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002528 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002529 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002530 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002531 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03002532 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002533 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002534 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02002535 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02002536 int r;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002537
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002538 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02002539 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002540 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity1b7fcd32008-05-15 13:51:35 +03002541 kvm_mmu_access_page(vcpu, gfn);
Avi Kivityeb787d12007-12-31 15:27:49 +02002542 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02002543 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02002544 kvm_mmu_audit(vcpu, "pre pte write");
Marcelo Tosattiad218f82008-12-01 22:32:05 -02002545 if (guest_initiated) {
2546 if (gfn == vcpu->arch.last_pt_write_gfn
2547 && !last_updated_pte_accessed(vcpu)) {
2548 ++vcpu->arch.last_pt_write_count;
2549 if (vcpu->arch.last_pt_write_count >= 3)
2550 flooded = 1;
2551 } else {
2552 vcpu->arch.last_pt_write_gfn = gfn;
2553 vcpu->arch.last_pt_write_count = 1;
2554 vcpu->arch.last_pte_updated = NULL;
2555 }
Avi Kivity86a5ba02007-01-05 16:36:50 -08002556 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02002557 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002558 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02002559 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02002560 if (sp->gfn != gfn || sp->role.direct || sp->role.invalid)
Avi Kivity9b7a0322007-01-05 16:36:45 -08002561 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02002562 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002563 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03002564 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08002565 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002566 /*
2567 * Misaligned accesses are too much trouble to fix
2568 * up; also, they usually indicate a page is not used
2569 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08002570 *
2571 * If we're seeing too many writes to a page,
2572 * it may no longer be a page table, or we may be
2573 * forking, in which case it is better to unmap the
2574 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002575 */
2576 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02002577 gpa, bytes, sp->role.word);
Marcelo Tosatti07385412008-09-23 13:18:37 -03002578 if (kvm_mmu_zap_page(vcpu->kvm, sp))
2579 n = bucket->first;
Avi Kivity4cee5762007-11-18 16:37:07 +02002580 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08002581 continue;
2582 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002583 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02002584 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02002585 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02002586 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02002587 page_offset <<= 1; /* 32->64 */
2588 /*
2589 * A 32-bit pde maps 4MB while the shadow pdes map
2590 * only 2MB. So we need to double the offset again
2591 * and zap two pdes instead of one.
2592 */
2593 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03002594 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02002595 page_offset <<= 1;
2596 npte = 2;
2597 }
Avi Kivityfce06572007-05-01 16:44:05 +03002598 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002599 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02002600 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03002601 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002602 }
Avi Kivity4db35312007-11-21 15:28:32 +02002603 spte = &sp->spt[page_offset / sizeof(*spte)];
Dong, Eddie489f1d62008-01-07 11:14:20 +02002604 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
2605 gentry = 0;
2606 r = kvm_read_guest_atomic(vcpu->kvm,
2607 gpa & ~(u64)(pte_size - 1),
2608 &gentry, pte_size);
2609 new = (const void *)&gentry;
2610 if (r < 0)
2611 new = NULL;
2612 }
Avi Kivityac1b7142007-03-08 17:13:32 +02002613 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02002614 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02002615 mmu_pte_write_zap_pte(vcpu, sp, spte);
Dong, Eddie489f1d62008-01-07 11:14:20 +02002616 if (new)
2617 mmu_pte_write_new_pte(vcpu, sp, spte, new);
Avi Kivity79539ce2007-11-21 02:06:21 +02002618 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02002619 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08002620 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08002621 }
Avi Kivityc7addb92007-09-16 18:58:32 +02002622 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002623 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05002624 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
2625 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
2626 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02002627 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08002628}
2629
Avi Kivitya4360362007-01-05 16:36:45 -08002630int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
2631{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002632 gpa_t gpa;
2633 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08002634
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002635 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002636
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002637 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002638 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002639 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002640 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08002641}
Avi Kivity577bdc42008-07-19 08:57:05 +03002642EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
Avi Kivitya4360362007-01-05 16:36:45 -08002643
Avi Kivity22d95b12007-09-14 20:26:06 +03002644void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08002645{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002646 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
Avi Kivity4db35312007-11-21 15:28:32 +02002647 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08002648
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002649 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02002650 struct kvm_mmu_page, link);
2651 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02002652 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08002653 }
2654}
Avi Kivityebeace82007-01-05 16:36:47 -08002655
Avi Kivity30677142007-10-28 18:48:59 +02002656int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
2657{
2658 int r;
2659 enum emulation_result er;
2660
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002661 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02002662 if (r < 0)
2663 goto out;
2664
2665 if (!r) {
2666 r = 1;
2667 goto out;
2668 }
2669
Avi Kivityb733bfb2007-10-28 18:52:05 +02002670 r = mmu_topup_memory_caches(vcpu);
2671 if (r)
2672 goto out;
2673
Avi Kivity30677142007-10-28 18:48:59 +02002674 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02002675
2676 switch (er) {
2677 case EMULATE_DONE:
2678 return 1;
2679 case EMULATE_DO_MMIO:
2680 ++vcpu->stat.mmio_exits;
2681 return 0;
2682 case EMULATE_FAIL:
Avi Kivity3f5d18a2009-06-11 15:43:28 +03002683 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2684 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
2685 return 0;
Avi Kivity30677142007-10-28 18:48:59 +02002686 default:
2687 BUG();
2688 }
2689out:
Avi Kivity30677142007-10-28 18:48:59 +02002690 return r;
2691}
2692EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
2693
Marcelo Tosattia7052892008-09-23 13:18:35 -03002694void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
2695{
Marcelo Tosattia7052892008-09-23 13:18:35 -03002696 vcpu->arch.mmu.invlpg(vcpu, gva);
Marcelo Tosattia7052892008-09-23 13:18:35 -03002697 kvm_mmu_flush_tlb(vcpu);
2698 ++vcpu->stat.invlpg;
2699}
2700EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
2701
Joerg Roedel18552672008-02-07 13:47:41 +01002702void kvm_enable_tdp(void)
2703{
2704 tdp_enabled = true;
2705}
2706EXPORT_SYMBOL_GPL(kvm_enable_tdp);
2707
Joerg Roedel5f4cb662008-07-14 20:36:36 +02002708void kvm_disable_tdp(void)
2709{
2710 tdp_enabled = false;
2711}
2712EXPORT_SYMBOL_GPL(kvm_disable_tdp);
2713
Avi Kivity6aa8b732006-12-10 02:21:36 -08002714static void free_mmu_pages(struct kvm_vcpu *vcpu)
2715{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002716 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002717}
2718
2719static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
2720{
Avi Kivity17ac10a2007-01-05 16:36:40 -08002721 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002722 int i;
2723
2724 ASSERT(vcpu);
2725
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002726 if (vcpu->kvm->arch.n_requested_mmu_pages)
2727 vcpu->kvm->arch.n_free_mmu_pages =
2728 vcpu->kvm->arch.n_requested_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02002729 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002730 vcpu->kvm->arch.n_free_mmu_pages =
2731 vcpu->kvm->arch.n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002732 /*
2733 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
2734 * Therefore we need to allocate shadow page tables in the first
2735 * 4GB of memory, which happens to fit the DMA32 zone.
2736 */
2737 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
2738 if (!page)
2739 goto error_1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002740 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08002741 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002742 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08002743
Avi Kivity6aa8b732006-12-10 02:21:36 -08002744 return 0;
2745
2746error_1:
2747 free_mmu_pages(vcpu);
2748 return -ENOMEM;
2749}
2750
Ingo Molnar8018c272006-12-29 16:50:01 -08002751int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002752{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002753 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002754 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002755
Ingo Molnar8018c272006-12-29 16:50:01 -08002756 return alloc_mmu_pages(vcpu);
2757}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002758
Ingo Molnar8018c272006-12-29 16:50:01 -08002759int kvm_mmu_setup(struct kvm_vcpu *vcpu)
2760{
2761 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002762 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08002763
Ingo Molnar8018c272006-12-29 16:50:01 -08002764 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002765}
2766
2767void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
2768{
2769 ASSERT(vcpu);
2770
2771 destroy_kvm_mmu(vcpu);
2772 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08002773 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002774}
2775
Avi Kivity90cb0522007-07-17 13:04:56 +03002776void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002777{
Avi Kivity4db35312007-11-21 15:28:32 +02002778 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002779
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002780 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002781 int i;
2782 u64 *pt;
2783
Sheng Yang291f26b2008-10-16 17:30:57 +08002784 if (!test_bit(slot, sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002785 continue;
2786
Avi Kivity4db35312007-11-21 15:28:32 +02002787 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002788 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
2789 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02002790 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002791 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002792 }
Avi Kivity171d5952008-08-27 16:40:51 +03002793 kvm_flush_remote_tlbs(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002794}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002795
Avi Kivity90cb0522007-07-17 13:04:56 +03002796void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03002797{
Avi Kivity4db35312007-11-21 15:28:32 +02002798 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03002799
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002800 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002801 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Marcelo Tosatti07385412008-09-23 13:18:37 -03002802 if (kvm_mmu_zap_page(kvm, sp))
2803 node = container_of(kvm->arch.active_mmu_pages.next,
2804 struct kvm_mmu_page, link);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05002805 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03002806
Avi Kivity90cb0522007-07-17 13:04:56 +03002807 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03002808}
2809
Harvey Harrison8b2cf732008-04-27 12:14:13 -07002810static void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
Izik Eidus3ee16c82008-03-30 15:17:21 +03002811{
2812 struct kvm_mmu_page *page;
2813
2814 page = container_of(kvm->arch.active_mmu_pages.prev,
2815 struct kvm_mmu_page, link);
2816 kvm_mmu_zap_page(kvm, page);
2817}
2818
2819static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
2820{
2821 struct kvm *kvm;
2822 struct kvm *kvm_freed = NULL;
2823 int cache_count = 0;
2824
2825 spin_lock(&kvm_lock);
2826
2827 list_for_each_entry(kvm, &vm_list, vm_list) {
2828 int npages;
2829
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002830 if (!down_read_trylock(&kvm->slots_lock))
2831 continue;
Izik Eidus3ee16c82008-03-30 15:17:21 +03002832 spin_lock(&kvm->mmu_lock);
2833 npages = kvm->arch.n_alloc_mmu_pages -
2834 kvm->arch.n_free_mmu_pages;
2835 cache_count += npages;
2836 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
2837 kvm_mmu_remove_one_alloc_mmu_page(kvm);
2838 cache_count--;
2839 kvm_freed = kvm;
2840 }
2841 nr_to_scan--;
2842
2843 spin_unlock(&kvm->mmu_lock);
Marcelo Tosatti5a4c9282008-07-03 18:33:02 -03002844 up_read(&kvm->slots_lock);
Izik Eidus3ee16c82008-03-30 15:17:21 +03002845 }
2846 if (kvm_freed)
2847 list_move_tail(&kvm_freed->vm_list, &vm_list);
2848
2849 spin_unlock(&kvm_lock);
2850
2851 return cache_count;
2852}
2853
2854static struct shrinker mmu_shrinker = {
2855 .shrink = mmu_shrink,
2856 .seeks = DEFAULT_SEEKS * 10,
2857};
2858
Ingo Molnar2ddfd202008-05-22 10:37:48 +02002859static void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03002860{
2861 if (pte_chain_cache)
2862 kmem_cache_destroy(pte_chain_cache);
2863 if (rmap_desc_cache)
2864 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002865 if (mmu_page_header_cache)
2866 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002867}
2868
Izik Eidus3ee16c82008-03-30 15:17:21 +03002869void kvm_mmu_module_exit(void)
2870{
2871 mmu_destroy_caches();
2872 unregister_shrinker(&mmu_shrinker);
2873}
2874
Avi Kivityb5a33a72007-04-15 16:31:09 +03002875int kvm_mmu_module_init(void)
2876{
2877 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2878 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09002879 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002880 if (!pte_chain_cache)
2881 goto nomem;
2882 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2883 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09002884 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002885 if (!rmap_desc_cache)
2886 goto nomem;
2887
Avi Kivityd3d25b02007-05-30 12:34:53 +03002888 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2889 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09002890 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002891 if (!mmu_page_header_cache)
2892 goto nomem;
2893
Izik Eidus3ee16c82008-03-30 15:17:21 +03002894 register_shrinker(&mmu_shrinker);
2895
Avi Kivityb5a33a72007-04-15 16:31:09 +03002896 return 0;
2897
2898nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03002899 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03002900 return -ENOMEM;
2901}
2902
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08002903/*
2904 * Caculate mmu pages needed for kvm.
2905 */
2906unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
2907{
2908 int i;
2909 unsigned int nr_mmu_pages;
2910 unsigned int nr_pages = 0;
2911
2912 for (i = 0; i < kvm->nmemslots; i++)
2913 nr_pages += kvm->memslots[i].npages;
2914
2915 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2916 nr_mmu_pages = max(nr_mmu_pages,
2917 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2918
2919 return nr_mmu_pages;
2920}
2921
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002922static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2923 unsigned len)
2924{
2925 if (len > buffer->len)
2926 return NULL;
2927 return buffer->ptr;
2928}
2929
2930static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2931 unsigned len)
2932{
2933 void *ret;
2934
2935 ret = pv_mmu_peek_buffer(buffer, len);
2936 if (!ret)
2937 return ret;
2938 buffer->ptr += len;
2939 buffer->len -= len;
2940 buffer->processed += len;
2941 return ret;
2942}
2943
2944static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
2945 gpa_t addr, gpa_t value)
2946{
2947 int bytes = 8;
2948 int r;
2949
2950 if (!is_long_mode(vcpu) && !is_pae(vcpu))
2951 bytes = 4;
2952
2953 r = mmu_topup_memory_caches(vcpu);
2954 if (r)
2955 return r;
2956
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002957 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002958 return -EFAULT;
2959
2960 return 1;
2961}
2962
2963static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2964{
Avi Kivitya8cd0242009-05-24 22:15:25 +03002965 kvm_set_cr3(vcpu, vcpu->arch.cr3);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002966 return 1;
2967}
2968
2969static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
2970{
2971 spin_lock(&vcpu->kvm->mmu_lock);
2972 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
2973 spin_unlock(&vcpu->kvm->mmu_lock);
2974 return 1;
2975}
2976
2977static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
2978 struct kvm_pv_mmu_op_buffer *buffer)
2979{
2980 struct kvm_mmu_op_header *header;
2981
2982 header = pv_mmu_peek_buffer(buffer, sizeof *header);
2983 if (!header)
2984 return 0;
2985 switch (header->op) {
2986 case KVM_MMU_OP_WRITE_PTE: {
2987 struct kvm_mmu_op_write_pte *wpte;
2988
2989 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
2990 if (!wpte)
2991 return 0;
2992 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
2993 wpte->pte_val);
2994 }
2995 case KVM_MMU_OP_FLUSH_TLB: {
2996 struct kvm_mmu_op_flush_tlb *ftlb;
2997
2998 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
2999 if (!ftlb)
3000 return 0;
3001 return kvm_pv_mmu_flush_tlb(vcpu);
3002 }
3003 case KVM_MMU_OP_RELEASE_PT: {
3004 struct kvm_mmu_op_release_pt *rpt;
3005
3006 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
3007 if (!rpt)
3008 return 0;
3009 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
3010 }
3011 default: return 0;
3012 }
3013}
3014
3015int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
3016 gpa_t addr, unsigned long *ret)
3017{
3018 int r;
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003019 struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003020
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003021 buffer->ptr = buffer->buf;
3022 buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
3023 buffer->processed = 0;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003024
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003025 r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003026 if (r)
3027 goto out;
3028
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003029 while (buffer->len) {
3030 r = kvm_pv_mmu_op_one(vcpu, buffer);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003031 if (r < 0)
3032 goto out;
3033 if (r == 0)
3034 break;
3035 }
3036
3037 r = 1;
3038out:
Dave Hansen6ad18fb2008-08-11 10:01:49 -07003039 *ret = buffer->processed;
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05003040 return r;
3041}
3042
Marcelo Tosatti94d8b052009-06-11 12:07:42 -03003043int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
3044{
3045 struct kvm_shadow_walk_iterator iterator;
3046 int nr_sptes = 0;
3047
3048 spin_lock(&vcpu->kvm->mmu_lock);
3049 for_each_shadow_entry(vcpu, addr, iterator) {
3050 sptes[iterator.level-1] = *iterator.sptep;
3051 nr_sptes++;
3052 if (!is_shadow_present_pte(*iterator.sptep))
3053 break;
3054 }
3055 spin_unlock(&vcpu->kvm->mmu_lock);
3056
3057 return nr_sptes;
3058}
3059EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
3060
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003061#ifdef AUDIT
3062
3063static const char *audit_msg;
3064
3065static gva_t canonicalize(gva_t gva)
3066{
3067#ifdef CONFIG_X86_64
3068 gva = (long long)(gva << 16) >> 16;
3069#endif
3070 return gva;
3071}
3072
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003073
3074typedef void (*inspect_spte_fn) (struct kvm *kvm, struct kvm_mmu_page *sp,
3075 u64 *sptep);
3076
3077static void __mmu_spte_walk(struct kvm *kvm, struct kvm_mmu_page *sp,
3078 inspect_spte_fn fn)
3079{
3080 int i;
3081
3082 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3083 u64 ent = sp->spt[i];
3084
3085 if (is_shadow_present_pte(ent)) {
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003086 if (!is_last_spte(ent, sp->role.level)) {
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003087 struct kvm_mmu_page *child;
3088 child = page_header(ent & PT64_BASE_ADDR_MASK);
3089 __mmu_spte_walk(kvm, child, fn);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003090 } else
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003091 fn(kvm, sp, &sp->spt[i]);
3092 }
3093 }
3094}
3095
3096static void mmu_spte_walk(struct kvm_vcpu *vcpu, inspect_spte_fn fn)
3097{
3098 int i;
3099 struct kvm_mmu_page *sp;
3100
3101 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3102 return;
3103 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
3104 hpa_t root = vcpu->arch.mmu.root_hpa;
3105 sp = page_header(root);
3106 __mmu_spte_walk(vcpu->kvm, sp, fn);
3107 return;
3108 }
3109 for (i = 0; i < 4; ++i) {
3110 hpa_t root = vcpu->arch.mmu.pae_root[i];
3111
3112 if (root && VALID_PAGE(root)) {
3113 root &= PT64_BASE_ADDR_MASK;
3114 sp = page_header(root);
3115 __mmu_spte_walk(vcpu->kvm, sp, fn);
3116 }
3117 }
3118 return;
3119}
3120
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003121static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
3122 gva_t va, int level)
3123{
3124 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
3125 int i;
3126 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
3127
3128 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
3129 u64 ent = pt[i];
3130
Avi Kivityc7addb92007-09-16 18:58:32 +02003131 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003132 continue;
3133
3134 va = canonicalize(va);
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003135 if (is_shadow_present_pte(ent) && !is_last_spte(ent, level))
3136 audit_mappings_page(vcpu, ent, va, level - 1);
3137 else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003138 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
Jan Kiszka34382532009-04-25 12:43:21 +02003139 gfn_t gfn = gpa >> PAGE_SHIFT;
3140 pfn_t pfn = gfn_to_pfn(vcpu->kvm, gfn);
3141 hpa_t hpa = (hpa_t)pfn << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003142
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003143 if (is_error_pfn(pfn)) {
3144 kvm_release_pfn_clean(pfn);
3145 continue;
3146 }
3147
Avi Kivityc7addb92007-09-16 18:58:32 +02003148 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003149 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02003150 printk(KERN_ERR "xx audit error: (%s) levels %d"
3151 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003152 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04003153 va, gpa, hpa, ent,
3154 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02003155 else if (ent == shadow_notrap_nonpresent_pte
3156 && !is_error_hpa(hpa))
3157 printk(KERN_ERR "audit: (%s) notrap shadow,"
3158 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05003159 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02003160
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003161 }
3162 }
3163}
3164
3165static void audit_mappings(struct kvm_vcpu *vcpu)
3166{
Avi Kivity1ea252a2007-03-08 11:48:09 +02003167 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003168
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003169 if (vcpu->arch.mmu.root_level == 4)
3170 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003171 else
3172 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003173 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003174 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003175 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003176 i << 30,
3177 2);
3178}
3179
3180static int count_rmaps(struct kvm_vcpu *vcpu)
3181{
3182 int nmaps = 0;
3183 int i, j, k;
3184
3185 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
3186 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
3187 struct kvm_rmap_desc *d;
3188
3189 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02003190 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003191
Izik Eidus290fc382007-09-27 14:11:22 +02003192 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003193 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02003194 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003195 ++nmaps;
3196 continue;
3197 }
Izik Eidus290fc382007-09-27 14:11:22 +02003198 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003199 while (d) {
3200 for (k = 0; k < RMAP_EXT; ++k)
Avi Kivityd555c332009-06-10 14:24:23 +03003201 if (d->sptes[k])
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003202 ++nmaps;
3203 else
3204 break;
3205 d = d->more;
3206 }
3207 }
3208 }
3209 return nmaps;
3210}
3211
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003212void inspect_spte_has_rmap(struct kvm *kvm, struct kvm_mmu_page *sp, u64 *sptep)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003213{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003214 unsigned long *rmapp;
3215 struct kvm_mmu_page *rev_sp;
3216 gfn_t gfn;
3217
3218 if (*sptep & PT_WRITABLE_MASK) {
3219 rev_sp = page_header(__pa(sptep));
3220 gfn = rev_sp->gfns[sptep - rev_sp->spt];
3221
3222 if (!gfn_to_memslot(kvm, gfn)) {
3223 if (!printk_ratelimit())
3224 return;
3225 printk(KERN_ERR "%s: no memslot for gfn %ld\n",
3226 audit_msg, gfn);
3227 printk(KERN_ERR "%s: index %ld of sp (gfn=%lx)\n",
3228 audit_msg, sptep - rev_sp->spt,
3229 rev_sp->gfn);
3230 dump_stack();
3231 return;
3232 }
3233
Marcelo Tosatti2920d722009-06-10 12:27:08 -03003234 rmapp = gfn_to_rmap(kvm, rev_sp->gfns[sptep - rev_sp->spt],
3235 is_large_pte(*sptep));
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003236 if (!*rmapp) {
3237 if (!printk_ratelimit())
3238 return;
3239 printk(KERN_ERR "%s: no rmap for writable spte %llx\n",
3240 audit_msg, *sptep);
3241 dump_stack();
3242 }
3243 }
3244
3245}
3246
3247void audit_writable_sptes_have_rmaps(struct kvm_vcpu *vcpu)
3248{
3249 mmu_spte_walk(vcpu, inspect_spte_has_rmap);
3250}
3251
3252static void check_writable_mappings_rmap(struct kvm_vcpu *vcpu)
3253{
Avi Kivity4db35312007-11-21 15:28:32 +02003254 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003255 int i;
3256
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003257 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02003258 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003259
Avi Kivity4db35312007-11-21 15:28:32 +02003260 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003261 continue;
3262
3263 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
3264 u64 ent = pt[i];
3265
3266 if (!(ent & PT_PRESENT_MASK))
3267 continue;
3268 if (!(ent & PT_WRITABLE_MASK))
3269 continue;
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003270 inspect_spte_has_rmap(vcpu->kvm, sp, &pt[i]);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003271 }
3272 }
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003273 return;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003274}
3275
3276static void audit_rmap(struct kvm_vcpu *vcpu)
3277{
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003278 check_writable_mappings_rmap(vcpu);
3279 count_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003280}
3281
3282static void audit_write_protection(struct kvm_vcpu *vcpu)
3283{
Avi Kivity4db35312007-11-21 15:28:32 +02003284 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02003285 struct kvm_memory_slot *slot;
3286 unsigned long *rmapp;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003287 u64 *spte;
Izik Eidus290fc382007-09-27 14:11:22 +02003288 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003289
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08003290 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivityf6e2c022009-01-11 13:02:10 +02003291 if (sp->role.direct)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003292 continue;
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003293 if (sp->unsync)
3294 continue;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003295
Avi Kivity4db35312007-11-21 15:28:32 +02003296 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus28430992008-10-03 17:40:32 +03003297 slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02003298 rmapp = &slot->rmap[gfn - slot->base_gfn];
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003299
3300 spte = rmap_next(vcpu->kvm, rmapp, NULL);
3301 while (spte) {
3302 if (*spte & PT_WRITABLE_MASK)
3303 printk(KERN_ERR "%s: (%s) shadow page has "
3304 "writable mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003305 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02003306 sp->role.word);
Marcelo Tosattie58b0f92009-06-10 12:27:05 -03003307 spte = rmap_next(vcpu->kvm, rmapp, spte);
3308 }
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003309 }
3310}
3311
3312static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
3313{
3314 int olddbg = dbg;
3315
3316 dbg = 0;
3317 audit_msg = msg;
3318 audit_rmap(vcpu);
3319 audit_write_protection(vcpu);
Marcelo Tosatti2aaf65e2009-06-10 12:27:07 -03003320 if (strcmp("pre pte write", audit_msg) != 0)
3321 audit_mappings(vcpu);
Marcelo Tosatti08a37322009-06-10 12:27:04 -03003322 audit_writable_sptes_have_rmaps(vcpu);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08003323 dbg = olddbg;
3324}
3325
3326#endif