blob: c89bf230af67bf4e3be4c974738a766eb870b2f6 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * MMU support
8 *
9 * Copyright (C) 2006 Qumranet, Inc.
10 *
11 * Authors:
12 * Yaniv Kamay <yaniv@qumranet.com>
13 * Avi Kivity <avi@qumranet.com>
14 *
15 * This work is licensed under the terms of the GNU GPL, version 2. See
16 * the COPYING file in the top-level directory.
17 *
18 */
Avi Kivity6aa8b732006-12-10 02:21:36 -080019
20#include "vmx.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080021#include "mmu.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080022
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Avi Kivitye4956062007-06-28 14:15:57 -040024#include <linux/types.h>
25#include <linux/string.h>
26#include <linux/mm.h>
27#include <linux/highmem.h>
28#include <linux/module.h>
Izik Eidus448353c2007-11-26 14:08:14 +020029#include <linux/swap.h>
Marcelo Tosatti05da4552008-02-23 11:44:30 -030030#include <linux/hugetlb.h>
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050031#include <linux/compiler.h>
Avi Kivitye4956062007-06-28 14:15:57 -040032
33#include <asm/page.h>
34#include <asm/cmpxchg.h>
Avi Kivity4e542372007-11-21 14:08:40 +020035#include <asm/io.h>
Avi Kivitye4956062007-06-28 14:15:57 -040036
Joerg Roedel18552672008-02-07 13:47:41 +010037/*
38 * When setting this variable to true it enables Two-Dimensional-Paging
39 * where the hardware walks 2 page tables:
40 * 1. the guest-virtual to guest-physical
41 * 2. while doing 1. it walks guest-physical to host-physical
42 * If the hardware supports that we don't need to do shadow paging.
43 */
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050044bool tdp_enabled = false;
Joerg Roedel18552672008-02-07 13:47:41 +010045
Avi Kivity37a7d8b2007-01-05 16:36:56 -080046#undef MMU_DEBUG
47
48#undef AUDIT
49
50#ifdef AUDIT
51static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
52#else
53static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
54#endif
55
56#ifdef MMU_DEBUG
57
58#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
59#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
60
61#else
62
63#define pgprintk(x...) do { } while (0)
64#define rmap_printk(x...) do { } while (0)
65
66#endif
67
68#if defined(MMU_DEBUG) || defined(AUDIT)
69static int dbg = 1;
70#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080071
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080072#ifndef MMU_DEBUG
73#define ASSERT(x) do { } while (0)
74#else
Avi Kivity6aa8b732006-12-10 02:21:36 -080075#define ASSERT(x) \
76 if (!(x)) { \
77 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
78 __FILE__, __LINE__, #x); \
79 }
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080080#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080081
Avi Kivitycea0f0e2007-01-05 16:36:43 -080082#define PT64_PT_BITS 9
83#define PT64_ENT_PER_PAGE (1 << PT64_PT_BITS)
84#define PT32_PT_BITS 10
85#define PT32_ENT_PER_PAGE (1 << PT32_PT_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -080086
87#define PT_WRITABLE_SHIFT 1
88
89#define PT_PRESENT_MASK (1ULL << 0)
90#define PT_WRITABLE_MASK (1ULL << PT_WRITABLE_SHIFT)
91#define PT_USER_MASK (1ULL << 2)
92#define PT_PWT_MASK (1ULL << 3)
93#define PT_PCD_MASK (1ULL << 4)
94#define PT_ACCESSED_MASK (1ULL << 5)
95#define PT_DIRTY_MASK (1ULL << 6)
96#define PT_PAGE_SIZE_MASK (1ULL << 7)
97#define PT_PAT_MASK (1ULL << 7)
98#define PT_GLOBAL_MASK (1ULL << 8)
Avi Kivityfe135d22007-12-09 16:15:46 +020099#define PT64_NX_SHIFT 63
100#define PT64_NX_MASK (1ULL << PT64_NX_SHIFT)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800101
102#define PT_PAT_SHIFT 7
103#define PT_DIR_PAT_SHIFT 12
104#define PT_DIR_PAT_MASK (1ULL << PT_DIR_PAT_SHIFT)
105
106#define PT32_DIR_PSE36_SIZE 4
107#define PT32_DIR_PSE36_SHIFT 13
Mike Dayd77c26f2007-10-08 09:02:08 -0400108#define PT32_DIR_PSE36_MASK \
109 (((1ULL << PT32_DIR_PSE36_SIZE) - 1) << PT32_DIR_PSE36_SHIFT)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800110
111
Avi Kivity6aa8b732006-12-10 02:21:36 -0800112#define PT_FIRST_AVAIL_BITS_SHIFT 9
113#define PT64_SECOND_AVAIL_BITS_SHIFT 52
114
Avi Kivity6aa8b732006-12-10 02:21:36 -0800115#define VALID_PAGE(x) ((x) != INVALID_PAGE)
116
117#define PT64_LEVEL_BITS 9
118
119#define PT64_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400120 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800121
122#define PT64_LEVEL_MASK(level) \
123 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
124
125#define PT64_INDEX(address, level)\
126 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
127
128
129#define PT32_LEVEL_BITS 10
130
131#define PT32_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400132 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800133
134#define PT32_LEVEL_MASK(level) \
135 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
136
137#define PT32_INDEX(address, level)\
138 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
139
140
Avi Kivity27aba762007-03-09 13:04:31 +0200141#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800142#define PT64_DIR_BASE_ADDR_MASK \
143 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
144
145#define PT32_BASE_ADDR_MASK PAGE_MASK
146#define PT32_DIR_BASE_ADDR_MASK \
147 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
148
Avi Kivity79539ce2007-11-21 02:06:21 +0200149#define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
150 | PT64_NX_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800151
152#define PFERR_PRESENT_MASK (1U << 0)
153#define PFERR_WRITE_MASK (1U << 1)
154#define PFERR_USER_MASK (1U << 2)
Avi Kivity73b10872007-01-26 00:56:41 -0800155#define PFERR_FETCH_MASK (1U << 4)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800156
157#define PT64_ROOT_LEVEL 4
158#define PT32_ROOT_LEVEL 2
159#define PT32E_ROOT_LEVEL 3
160
161#define PT_DIRECTORY_LEVEL 2
162#define PT_PAGE_TABLE_LEVEL 1
163
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800164#define RMAP_EXT 4
165
Avi Kivityfe135d22007-12-09 16:15:46 +0200166#define ACC_EXEC_MASK 1
167#define ACC_WRITE_MASK PT_WRITABLE_MASK
168#define ACC_USER_MASK PT_USER_MASK
169#define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
170
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -0500171struct kvm_pv_mmu_op_buffer {
172 void *ptr;
173 unsigned len;
174 unsigned processed;
175 char buf[512] __aligned(sizeof(long));
176};
177
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800178struct kvm_rmap_desc {
179 u64 *shadow_ptes[RMAP_EXT];
180 struct kvm_rmap_desc *more;
181};
182
Avi Kivityb5a33a72007-04-15 16:31:09 +0300183static struct kmem_cache *pte_chain_cache;
184static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300185static struct kmem_cache *mmu_page_header_cache;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300186
Avi Kivityc7addb92007-09-16 18:58:32 +0200187static u64 __read_mostly shadow_trap_nonpresent_pte;
188static u64 __read_mostly shadow_notrap_nonpresent_pte;
189
190void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
191{
192 shadow_trap_nonpresent_pte = trap_pte;
193 shadow_notrap_nonpresent_pte = notrap_pte;
194}
195EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
196
Avi Kivity6aa8b732006-12-10 02:21:36 -0800197static int is_write_protection(struct kvm_vcpu *vcpu)
198{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800199 return vcpu->arch.cr0 & X86_CR0_WP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800200}
201
202static int is_cpuid_PSE36(void)
203{
204 return 1;
205}
206
Avi Kivity73b10872007-01-26 00:56:41 -0800207static int is_nx(struct kvm_vcpu *vcpu)
208{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800209 return vcpu->arch.shadow_efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800210}
211
Avi Kivity6aa8b732006-12-10 02:21:36 -0800212static int is_present_pte(unsigned long pte)
213{
214 return pte & PT_PRESENT_MASK;
215}
216
Avi Kivityc7addb92007-09-16 18:58:32 +0200217static int is_shadow_present_pte(u64 pte)
218{
Avi Kivityc7addb92007-09-16 18:58:32 +0200219 return pte != shadow_trap_nonpresent_pte
220 && pte != shadow_notrap_nonpresent_pte;
221}
222
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300223static int is_large_pte(u64 pte)
224{
225 return pte & PT_PAGE_SIZE_MASK;
226}
227
Avi Kivity6aa8b732006-12-10 02:21:36 -0800228static int is_writeble_pte(unsigned long pte)
229{
230 return pte & PT_WRITABLE_MASK;
231}
232
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200233static int is_dirty_pte(unsigned long pte)
234{
235 return pte & PT_DIRTY_MASK;
236}
237
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800238static int is_rmap_pte(u64 pte)
239{
Avi Kivity4b1a80f2008-03-23 12:18:19 +0200240 return is_shadow_present_pte(pte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800241}
242
Avi Kivity0b49ea82008-03-23 15:06:23 +0200243static struct page *spte_to_page(u64 pte)
244{
245 hfn_t hfn = (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
246
247 return pfn_to_page(hfn);
248}
249
Avi Kivityda9285212007-11-21 13:54:47 +0200250static gfn_t pse36_gfn_delta(u32 gpte)
251{
252 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
253
254 return (gpte & PT32_DIR_PSE36_MASK) << shift;
255}
256
Avi Kivitye663ee62007-05-31 15:46:04 +0300257static void set_shadow_pte(u64 *sptep, u64 spte)
258{
259#ifdef CONFIG_X86_64
260 set_64bit((unsigned long *)sptep, spte);
261#else
262 set_64bit((unsigned long long *)sptep, spte);
263#endif
264}
265
Avi Kivitye2dec932007-01-05 16:36:54 -0800266static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300267 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800268{
269 void *obj;
270
271 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800272 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800273 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300274 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800275 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800276 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800277 cache->objects[cache->nobjs++] = obj;
278 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800279 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800280}
281
282static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
283{
284 while (mc->nobjs)
285 kfree(mc->objects[--mc->nobjs]);
286}
287
Avi Kivityc1158e62007-07-20 08:18:27 +0300288static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300289 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300290{
291 struct page *page;
292
293 if (cache->nobjs >= min)
294 return 0;
295 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300296 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300297 if (!page)
298 return -ENOMEM;
299 set_page_private(page, 0);
300 cache->objects[cache->nobjs++] = page_address(page);
301 }
302 return 0;
303}
304
305static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
306{
307 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300308 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300309}
310
Avi Kivity8c438502007-04-16 11:53:17 +0300311static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
312{
313 int r;
314
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800315 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300316 pte_chain_cache, 4);
317 if (r)
318 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800319 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300320 rmap_desc_cache, 1);
321 if (r)
322 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800323 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300324 if (r)
325 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800326 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300327 mmu_page_header_cache, 4);
328out:
Avi Kivity8c438502007-04-16 11:53:17 +0300329 return r;
330}
331
Avi Kivity714b93d2007-01-05 16:36:53 -0800332static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
333{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800334 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
335 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
336 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
337 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800338}
339
340static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
341 size_t size)
342{
343 void *p;
344
345 BUG_ON(!mc->nobjs);
346 p = mc->objects[--mc->nobjs];
347 memset(p, 0, size);
348 return p;
349}
350
Avi Kivity714b93d2007-01-05 16:36:53 -0800351static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
352{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800353 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800354 sizeof(struct kvm_pte_chain));
355}
356
Avi Kivity90cb0522007-07-17 13:04:56 +0300357static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800358{
Avi Kivity90cb0522007-07-17 13:04:56 +0300359 kfree(pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800360}
361
362static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
363{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800364 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800365 sizeof(struct kvm_rmap_desc));
366}
367
Avi Kivity90cb0522007-07-17 13:04:56 +0300368static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800369{
Avi Kivity90cb0522007-07-17 13:04:56 +0300370 kfree(rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800371}
372
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800373/*
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300374 * Return the pointer to the largepage write count for a given
375 * gfn, handling slots that are not large page aligned.
376 */
377static int *slot_largepage_idx(gfn_t gfn, struct kvm_memory_slot *slot)
378{
379 unsigned long idx;
380
381 idx = (gfn / KVM_PAGES_PER_HPAGE) -
382 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
383 return &slot->lpage_info[idx].write_count;
384}
385
386static void account_shadowed(struct kvm *kvm, gfn_t gfn)
387{
388 int *write_count;
389
390 write_count = slot_largepage_idx(gfn, gfn_to_memslot(kvm, gfn));
391 *write_count += 1;
392 WARN_ON(*write_count > KVM_PAGES_PER_HPAGE);
393}
394
395static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
396{
397 int *write_count;
398
399 write_count = slot_largepage_idx(gfn, gfn_to_memslot(kvm, gfn));
400 *write_count -= 1;
401 WARN_ON(*write_count < 0);
402}
403
404static int has_wrprotected_page(struct kvm *kvm, gfn_t gfn)
405{
406 struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
407 int *largepage_idx;
408
409 if (slot) {
410 largepage_idx = slot_largepage_idx(gfn, slot);
411 return *largepage_idx;
412 }
413
414 return 1;
415}
416
417static int host_largepage_backed(struct kvm *kvm, gfn_t gfn)
418{
419 struct vm_area_struct *vma;
420 unsigned long addr;
421
422 addr = gfn_to_hva(kvm, gfn);
423 if (kvm_is_error_hva(addr))
424 return 0;
425
426 vma = find_vma(current->mm, addr);
427 if (vma && is_vm_hugetlb_page(vma))
428 return 1;
429
430 return 0;
431}
432
433static int is_largepage_backed(struct kvm_vcpu *vcpu, gfn_t large_gfn)
434{
435 struct kvm_memory_slot *slot;
436
437 if (has_wrprotected_page(vcpu->kvm, large_gfn))
438 return 0;
439
440 if (!host_largepage_backed(vcpu->kvm, large_gfn))
441 return 0;
442
443 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
444 if (slot && slot->dirty_bitmap)
445 return 0;
446
447 return 1;
448}
449
450/*
Izik Eidus290fc382007-09-27 14:11:22 +0200451 * Take gfn and return the reverse mapping to it.
452 * Note: gfn must be unaliased before this function get called
453 */
454
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300455static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int lpage)
Izik Eidus290fc382007-09-27 14:11:22 +0200456{
457 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300458 unsigned long idx;
Izik Eidus290fc382007-09-27 14:11:22 +0200459
460 slot = gfn_to_memslot(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300461 if (!lpage)
462 return &slot->rmap[gfn - slot->base_gfn];
463
464 idx = (gfn / KVM_PAGES_PER_HPAGE) -
465 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
466
467 return &slot->lpage_info[idx].rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200468}
469
470/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800471 * Reverse mapping data structures:
472 *
Izik Eidus290fc382007-09-27 14:11:22 +0200473 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
474 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800475 *
Izik Eidus290fc382007-09-27 14:11:22 +0200476 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
477 * containing more mappings.
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800478 */
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300479static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn, int lpage)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800480{
Avi Kivity4db35312007-11-21 15:28:32 +0200481 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800482 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200483 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800484 int i;
485
486 if (!is_rmap_pte(*spte))
487 return;
Izik Eidus290fc382007-09-27 14:11:22 +0200488 gfn = unalias_gfn(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200489 sp = page_header(__pa(spte));
490 sp->gfns[spte - sp->spt] = gfn;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300491 rmapp = gfn_to_rmap(vcpu->kvm, gfn, lpage);
Izik Eidus290fc382007-09-27 14:11:22 +0200492 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800493 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200494 *rmapp = (unsigned long)spte;
495 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800496 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800497 desc = mmu_alloc_rmap_desc(vcpu);
Izik Eidus290fc382007-09-27 14:11:22 +0200498 desc->shadow_ptes[0] = (u64 *)*rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800499 desc->shadow_ptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200500 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800501 } else {
502 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200503 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800504 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
505 desc = desc->more;
506 if (desc->shadow_ptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800507 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800508 desc = desc->more;
509 }
510 for (i = 0; desc->shadow_ptes[i]; ++i)
511 ;
512 desc->shadow_ptes[i] = spte;
513 }
514}
515
Izik Eidus290fc382007-09-27 14:11:22 +0200516static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800517 struct kvm_rmap_desc *desc,
518 int i,
519 struct kvm_rmap_desc *prev_desc)
520{
521 int j;
522
523 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
524 ;
525 desc->shadow_ptes[i] = desc->shadow_ptes[j];
Al Viro11718b4d2007-02-09 16:39:20 +0000526 desc->shadow_ptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800527 if (j != 0)
528 return;
529 if (!prev_desc && !desc->more)
Izik Eidus290fc382007-09-27 14:11:22 +0200530 *rmapp = (unsigned long)desc->shadow_ptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800531 else
532 if (prev_desc)
533 prev_desc->more = desc->more;
534 else
Izik Eidus290fc382007-09-27 14:11:22 +0200535 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300536 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800537}
538
Izik Eidus290fc382007-09-27 14:11:22 +0200539static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800540{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800541 struct kvm_rmap_desc *desc;
542 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200543 struct kvm_mmu_page *sp;
Avi Kivity76c35c62007-11-21 15:32:41 +0200544 struct page *page;
Izik Eidus290fc382007-09-27 14:11:22 +0200545 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800546 int i;
547
548 if (!is_rmap_pte(*spte))
549 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200550 sp = page_header(__pa(spte));
Avi Kivity0b49ea82008-03-23 15:06:23 +0200551 page = spte_to_page(*spte);
Avi Kivityfcd6dba2008-04-03 12:02:21 +0300552 if (*spte & PT_ACCESSED_MASK)
553 mark_page_accessed(page);
Izik Eidusb4231d62007-11-20 11:49:33 +0200554 if (is_writeble_pte(*spte))
Avi Kivity76c35c62007-11-21 15:32:41 +0200555 kvm_release_page_dirty(page);
Izik Eidusb4231d62007-11-20 11:49:33 +0200556 else
Avi Kivity76c35c62007-11-21 15:32:41 +0200557 kvm_release_page_clean(page);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300558 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], is_large_pte(*spte));
Izik Eidus290fc382007-09-27 14:11:22 +0200559 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800560 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
561 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200562 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800563 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200564 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800565 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
566 spte, *spte);
567 BUG();
568 }
Izik Eidus290fc382007-09-27 14:11:22 +0200569 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800570 } else {
571 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200572 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800573 prev_desc = NULL;
574 while (desc) {
575 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
576 if (desc->shadow_ptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200577 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800578 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800579 prev_desc);
580 return;
581 }
582 prev_desc = desc;
583 desc = desc->more;
584 }
585 BUG();
586 }
587}
588
Izik Eidus98348e92007-10-16 14:42:30 +0200589static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800590{
Avi Kivity374cbac2007-01-05 16:36:43 -0800591 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200592 struct kvm_rmap_desc *prev_desc;
593 u64 *prev_spte;
594 int i;
595
596 if (!*rmapp)
597 return NULL;
598 else if (!(*rmapp & 1)) {
599 if (!spte)
600 return (u64 *)*rmapp;
601 return NULL;
602 }
603 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
604 prev_desc = NULL;
605 prev_spte = NULL;
606 while (desc) {
607 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) {
608 if (prev_spte == spte)
609 return desc->shadow_ptes[i];
610 prev_spte = desc->shadow_ptes[i];
611 }
612 desc = desc->more;
613 }
614 return NULL;
615}
616
617static void rmap_write_protect(struct kvm *kvm, u64 gfn)
618{
Izik Eidus290fc382007-09-27 14:11:22 +0200619 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800620 u64 *spte;
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800621 int write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800622
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500623 gfn = unalias_gfn(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300624 rmapp = gfn_to_rmap(kvm, gfn, 0);
Avi Kivity374cbac2007-01-05 16:36:43 -0800625
Izik Eidus98348e92007-10-16 14:42:30 +0200626 spte = rmap_next(kvm, rmapp, NULL);
627 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800628 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800629 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800630 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800631 if (is_writeble_pte(*spte)) {
Izik Eidus9647c142007-10-16 14:43:46 +0200632 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800633 write_protected = 1;
634 }
Izik Eidus9647c142007-10-16 14:43:46 +0200635 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800636 }
Izik Eidus855149a2008-03-20 18:17:24 +0200637 if (write_protected) {
638 struct page *page;
639
640 spte = rmap_next(kvm, rmapp, NULL);
Avi Kivity0b49ea82008-03-23 15:06:23 +0200641 page = spte_to_page(*spte);
Izik Eidus855149a2008-03-20 18:17:24 +0200642 SetPageDirty(page);
643 }
644
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300645 /* check for huge page mappings */
646 rmapp = gfn_to_rmap(kvm, gfn, 1);
647 spte = rmap_next(kvm, rmapp, NULL);
648 while (spte) {
649 BUG_ON(!spte);
650 BUG_ON(!(*spte & PT_PRESENT_MASK));
651 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
652 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
653 if (is_writeble_pte(*spte)) {
654 rmap_remove(kvm, spte);
655 --kvm->stat.lpages;
656 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
657 write_protected = 1;
658 }
659 spte = rmap_next(kvm, rmapp, spte);
660 }
661
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800662 if (write_protected)
663 kvm_flush_remote_tlbs(kvm);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300664
665 account_shadowed(kvm, gfn);
Avi Kivity374cbac2007-01-05 16:36:43 -0800666}
667
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800668#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300669static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800670{
Avi Kivity139bdb22007-01-05 16:36:50 -0800671 u64 *pos;
672 u64 *end;
673
Avi Kivity47ad8e62007-05-06 15:50:58 +0300674 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivityd196e342008-01-24 11:44:11 +0200675 if (*pos != shadow_trap_nonpresent_pte) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800676 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800677 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800678 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800679 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800680 return 1;
681}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800682#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800683
Avi Kivity4db35312007-11-21 15:28:32 +0200684static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800685{
Avi Kivity4db35312007-11-21 15:28:32 +0200686 ASSERT(is_empty_shadow_page(sp->spt));
687 list_del(&sp->link);
688 __free_page(virt_to_page(sp->spt));
689 __free_page(virt_to_page(sp->gfns));
690 kfree(sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800691 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800692}
693
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800694static unsigned kvm_page_table_hashfn(gfn_t gfn)
695{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200696 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800697}
698
Avi Kivity25c0de22007-01-05 16:36:42 -0800699static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
700 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800701{
Avi Kivity4db35312007-11-21 15:28:32 +0200702 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800703
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800704 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
705 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
706 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200707 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800708 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Avi Kivity4db35312007-11-21 15:28:32 +0200709 ASSERT(is_empty_shadow_page(sp->spt));
710 sp->slot_bitmap = 0;
711 sp->multimapped = 0;
712 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800713 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200714 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800715}
716
Avi Kivity714b93d2007-01-05 16:36:53 -0800717static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200718 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800719{
720 struct kvm_pte_chain *pte_chain;
721 struct hlist_node *node;
722 int i;
723
724 if (!parent_pte)
725 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200726 if (!sp->multimapped) {
727 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800728
729 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200730 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800731 return;
732 }
Avi Kivity4db35312007-11-21 15:28:32 +0200733 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800734 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200735 INIT_HLIST_HEAD(&sp->parent_ptes);
736 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800737 pte_chain->parent_ptes[0] = old;
738 }
Avi Kivity4db35312007-11-21 15:28:32 +0200739 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800740 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
741 continue;
742 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
743 if (!pte_chain->parent_ptes[i]) {
744 pte_chain->parent_ptes[i] = parent_pte;
745 return;
746 }
747 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800748 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800749 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200750 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800751 pte_chain->parent_ptes[0] = parent_pte;
752}
753
Avi Kivity4db35312007-11-21 15:28:32 +0200754static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800755 u64 *parent_pte)
756{
757 struct kvm_pte_chain *pte_chain;
758 struct hlist_node *node;
759 int i;
760
Avi Kivity4db35312007-11-21 15:28:32 +0200761 if (!sp->multimapped) {
762 BUG_ON(sp->parent_pte != parent_pte);
763 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800764 return;
765 }
Avi Kivity4db35312007-11-21 15:28:32 +0200766 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800767 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
768 if (!pte_chain->parent_ptes[i])
769 break;
770 if (pte_chain->parent_ptes[i] != parent_pte)
771 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800772 while (i + 1 < NR_PTE_CHAIN_ENTRIES
773 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800774 pte_chain->parent_ptes[i]
775 = pte_chain->parent_ptes[i + 1];
776 ++i;
777 }
778 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800779 if (i == 0) {
780 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300781 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200782 if (hlist_empty(&sp->parent_ptes)) {
783 sp->multimapped = 0;
784 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800785 }
786 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800787 return;
788 }
789 BUG();
790}
791
Avi Kivity4db35312007-11-21 15:28:32 +0200792static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800793{
794 unsigned index;
795 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200796 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800797 struct hlist_node *node;
798
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800799 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200800 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800801 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200802 hlist_for_each_entry(sp, node, bucket, hash_link)
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500803 if (sp->gfn == gfn && !sp->role.metaphysical
804 && !sp->role.invalid) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800805 pgprintk("%s: found role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800806 __func__, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +0200807 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800808 }
809 return NULL;
810}
811
812static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
813 gfn_t gfn,
814 gva_t gaddr,
815 unsigned level,
816 int metaphysical,
Avi Kivity41074d02007-12-09 17:00:02 +0200817 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +0200818 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800819{
820 union kvm_mmu_page_role role;
821 unsigned index;
822 unsigned quadrant;
823 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200824 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800825 struct hlist_node *node;
826
827 role.word = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800828 role.glevels = vcpu->arch.mmu.root_level;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800829 role.level = level;
830 role.metaphysical = metaphysical;
Avi Kivity41074d02007-12-09 17:00:02 +0200831 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800832 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800833 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
834 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
835 role.quadrant = quadrant;
836 }
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800837 pgprintk("%s: looking gfn %lx role %x\n", __func__,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800838 gfn, role.word);
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200839 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800840 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200841 hlist_for_each_entry(sp, node, bucket, hash_link)
842 if (sp->gfn == gfn && sp->role.word == role.word) {
843 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800844 pgprintk("%s: found\n", __func__);
Avi Kivity4db35312007-11-21 15:28:32 +0200845 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800846 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +0200847 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +0200848 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
849 if (!sp)
850 return sp;
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800851 pgprintk("%s: adding gfn %lx role %x\n", __func__, gfn, role.word);
Avi Kivity4db35312007-11-21 15:28:32 +0200852 sp->gfn = gfn;
853 sp->role = role;
854 hlist_add_head(&sp->hash_link, bucket);
Avi Kivity374cbac2007-01-05 16:36:43 -0800855 if (!metaphysical)
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500856 rmap_write_protect(vcpu->kvm, gfn);
Marcelo Tosattibed1d1d2008-04-04 14:56:44 -0300857 vcpu->arch.mmu.prefetch_page(vcpu, sp);
Avi Kivity4db35312007-11-21 15:28:32 +0200858 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800859}
860
Avi Kivity90cb0522007-07-17 13:04:56 +0300861static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +0200862 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -0800863{
Avi Kivity697fe2e2007-01-05 16:36:46 -0800864 unsigned i;
865 u64 *pt;
866 u64 ent;
867
Avi Kivity4db35312007-11-21 15:28:32 +0200868 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800869
Avi Kivity4db35312007-11-21 15:28:32 +0200870 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800871 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Avi Kivityc7addb92007-09-16 18:58:32 +0200872 if (is_shadow_present_pte(pt[i]))
Izik Eidus290fc382007-09-27 14:11:22 +0200873 rmap_remove(kvm, &pt[i]);
Avi Kivityc7addb92007-09-16 18:58:32 +0200874 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800875 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300876 kvm_flush_remote_tlbs(kvm);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800877 return;
878 }
879
880 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
881 ent = pt[i];
882
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300883 if (is_shadow_present_pte(ent)) {
884 if (!is_large_pte(ent)) {
885 ent &= PT64_BASE_ADDR_MASK;
886 mmu_page_remove_parent_pte(page_header(ent),
887 &pt[i]);
888 } else {
889 --kvm->stat.lpages;
890 rmap_remove(kvm, &pt[i]);
891 }
892 }
Avi Kivityc7addb92007-09-16 18:58:32 +0200893 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800894 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300895 kvm_flush_remote_tlbs(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800896}
897
Avi Kivity4db35312007-11-21 15:28:32 +0200898static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800899{
Avi Kivity4db35312007-11-21 15:28:32 +0200900 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800901}
902
Avi Kivity12b7d282007-09-23 14:10:49 +0200903static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
904{
905 int i;
906
907 for (i = 0; i < KVM_MAX_VCPUS; ++i)
908 if (kvm->vcpus[i])
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800909 kvm->vcpus[i]->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +0200910}
911
Avi Kivity4db35312007-11-21 15:28:32 +0200912static void kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -0800913{
914 u64 *parent_pte;
915
Avi Kivity4cee5762007-11-18 16:37:07 +0200916 ++kvm->stat.mmu_shadow_zapped;
Avi Kivity4db35312007-11-21 15:28:32 +0200917 while (sp->multimapped || sp->parent_pte) {
918 if (!sp->multimapped)
919 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -0800920 else {
921 struct kvm_pte_chain *chain;
922
Avi Kivity4db35312007-11-21 15:28:32 +0200923 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -0800924 struct kvm_pte_chain, link);
925 parent_pte = chain->parent_ptes[0];
926 }
Avi Kivity697fe2e2007-01-05 16:36:46 -0800927 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +0200928 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityc7addb92007-09-16 18:58:32 +0200929 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800930 }
Avi Kivity4db35312007-11-21 15:28:32 +0200931 kvm_mmu_page_unlink_children(kvm, sp);
932 if (!sp->root_count) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300933 if (!sp->role.metaphysical)
934 unaccount_shadowed(kvm, sp->gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200935 hlist_del(&sp->hash_link);
936 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500937 } else {
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800938 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500939 sp->role.invalid = 1;
940 kvm_reload_remote_mmus(kvm);
941 }
Avi Kivity12b7d282007-09-23 14:10:49 +0200942 kvm_mmu_reset_last_pte_updated(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800943}
944
Izik Eidus82ce2c92007-10-02 18:52:55 +0200945/*
946 * Changing the number of mmu pages allocated to the vm
947 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
948 */
949void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
950{
951 /*
952 * If we set the number of mmu pages to be smaller be than the
953 * number of actived pages , we must to free some mmu pages before we
954 * change the value
955 */
956
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800957 if ((kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages) >
Izik Eidus82ce2c92007-10-02 18:52:55 +0200958 kvm_nr_mmu_pages) {
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800959 int n_used_mmu_pages = kvm->arch.n_alloc_mmu_pages
960 - kvm->arch.n_free_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200961
962 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
963 struct kvm_mmu_page *page;
964
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800965 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +0200966 struct kvm_mmu_page, link);
967 kvm_mmu_zap_page(kvm, page);
968 n_used_mmu_pages--;
969 }
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800970 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200971 }
972 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800973 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
974 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200975
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800976 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200977}
978
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500979static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -0800980{
981 unsigned index;
982 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200983 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -0800984 struct hlist_node *node, *n;
985 int r;
986
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800987 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -0800988 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200989 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800990 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200991 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
992 if (sp->gfn == gfn && !sp->role.metaphysical) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800993 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +0200994 sp->role.word);
995 kvm_mmu_zap_page(kvm, sp);
Avi Kivitya4360362007-01-05 16:36:45 -0800996 r = 1;
997 }
998 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800999}
1000
Anthony Liguorif67a46f2007-10-10 19:25:50 -05001001static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001002{
Avi Kivity4db35312007-11-21 15:28:32 +02001003 struct kvm_mmu_page *sp;
Avi Kivity97a0a012007-05-31 15:08:29 +03001004
Avi Kivity4db35312007-11-21 15:28:32 +02001005 while ((sp = kvm_mmu_lookup_page(kvm, gfn)) != NULL) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001006 pgprintk("%s: zap %lx %x\n", __func__, gfn, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001007 kvm_mmu_zap_page(kvm, sp);
Avi Kivity97a0a012007-05-31 15:08:29 +03001008 }
1009}
1010
Avi Kivity38c335f2007-11-21 14:20:22 +02001011static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001012{
Avi Kivity38c335f2007-11-21 14:20:22 +02001013 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity4db35312007-11-21 15:28:32 +02001014 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001015
Avi Kivity4db35312007-11-21 15:28:32 +02001016 __set_bit(slot, &sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001017}
1018
Avi Kivity039576c2007-03-20 12:46:50 +02001019struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1020{
Izik Eidus72dc67a2008-02-10 18:04:15 +02001021 struct page *page;
1022
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001023 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Avi Kivity039576c2007-03-20 12:46:50 +02001024
1025 if (gpa == UNMAPPED_GVA)
1026 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001027
1028 down_read(&current->mm->mmap_sem);
1029 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1030 up_read(&current->mm->mmap_sem);
1031
1032 return page;
Avi Kivity039576c2007-03-20 12:46:50 +02001033}
1034
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001035static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1036 unsigned pt_access, unsigned pte_access,
1037 int user_fault, int write_fault, int dirty,
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001038 int *ptwrite, int largepage, gfn_t gfn,
Avi Kivity947da532008-03-18 11:05:52 +02001039 struct page *page, bool speculative)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001040{
1041 u64 spte;
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001042 int was_rmapped = 0;
Izik Eidus75e68e62008-01-12 23:49:09 +02001043 int was_writeble = is_writeble_pte(*shadow_pte);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001044
Avi Kivitybc750ba2007-12-09 18:39:41 +02001045 pgprintk("%s: spte %llx access %x write_fault %d"
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001046 " user_fault %d gfn %lx\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001047 __func__, *shadow_pte, pt_access,
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001048 write_fault, user_fault, gfn);
1049
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001050 if (is_rmap_pte(*shadow_pte)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001051 /*
1052 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1053 * the parent of the now unreachable PTE.
1054 */
1055 if (largepage && !is_large_pte(*shadow_pte)) {
1056 struct kvm_mmu_page *child;
1057 u64 pte = *shadow_pte;
1058
1059 child = page_header(pte & PT64_BASE_ADDR_MASK);
1060 mmu_page_remove_parent_pte(child, shadow_pte);
Avi Kivity0b49ea82008-03-23 15:06:23 +02001061 } else if (page != spte_to_page(*shadow_pte)) {
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001062 pgprintk("hfn old %lx new %lx\n",
Avi Kivity0b49ea82008-03-23 15:06:23 +02001063 page_to_pfn(spte_to_page(*shadow_pte)),
1064 page_to_pfn(page));
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001065 rmap_remove(vcpu->kvm, shadow_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001066 } else {
1067 if (largepage)
1068 was_rmapped = is_large_pte(*shadow_pte);
1069 else
1070 was_rmapped = 1;
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001071 }
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001072 }
1073
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001074 /*
1075 * We don't set the accessed bit, since we sometimes want to see
1076 * whether the guest actually used the pte (in order to detect
1077 * demand paging).
1078 */
1079 spte = PT_PRESENT_MASK | PT_DIRTY_MASK;
Avi Kivity947da532008-03-18 11:05:52 +02001080 if (!speculative)
1081 pte_access |= PT_ACCESSED_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001082 if (!dirty)
1083 pte_access &= ~ACC_WRITE_MASK;
1084 if (!(pte_access & ACC_EXEC_MASK))
1085 spte |= PT64_NX_MASK;
1086
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001087 spte |= PT_PRESENT_MASK;
1088 if (pte_access & ACC_USER_MASK)
1089 spte |= PT_USER_MASK;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001090 if (largepage)
1091 spte |= PT_PAGE_SIZE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001092
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001093 spte |= page_to_phys(page);
1094
1095 if ((pte_access & ACC_WRITE_MASK)
1096 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
1097 struct kvm_mmu_page *shadow;
1098
1099 spte |= PT_WRITABLE_MASK;
1100 if (user_fault) {
1101 mmu_unshadow(vcpu->kvm, gfn);
1102 goto unshadowed;
1103 }
1104
1105 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001106 if (shadow ||
1107 (largepage && has_wrprotected_page(vcpu->kvm, gfn))) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001108 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001109 __func__, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001110 pte_access &= ~ACC_WRITE_MASK;
1111 if (is_writeble_pte(spte)) {
1112 spte &= ~PT_WRITABLE_MASK;
1113 kvm_x86_ops->tlb_flush(vcpu);
1114 }
1115 if (write_fault)
1116 *ptwrite = 1;
1117 }
1118 }
1119
1120unshadowed:
1121
1122 if (pte_access & ACC_WRITE_MASK)
1123 mark_page_dirty(vcpu->kvm, gfn);
1124
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001125 pgprintk("%s: setting spte %llx\n", __func__, spte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001126 pgprintk("instantiating %s PTE (%s) at %d (%llx) addr %llx\n",
1127 (spte&PT_PAGE_SIZE_MASK)? "2MB" : "4kB",
1128 (spte&PT_WRITABLE_MASK)?"RW":"R", gfn, spte, shadow_pte);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001129 set_shadow_pte(shadow_pte, spte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001130 if (!was_rmapped && (spte & PT_PAGE_SIZE_MASK)
1131 && (spte & PT_PRESENT_MASK))
1132 ++vcpu->kvm->stat.lpages;
1133
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001134 page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
1135 if (!was_rmapped) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001136 rmap_add(vcpu, shadow_pte, gfn, largepage);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001137 if (!is_rmap_pte(*shadow_pte))
1138 kvm_release_page_clean(page);
Izik Eidus75e68e62008-01-12 23:49:09 +02001139 } else {
1140 if (was_writeble)
1141 kvm_release_page_dirty(page);
1142 else
1143 kvm_release_page_clean(page);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001144 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001145 if (!ptwrite || !*ptwrite)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001146 vcpu->arch.last_pte_updated = shadow_pte;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001147}
1148
Avi Kivity6aa8b732006-12-10 02:21:36 -08001149static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1150{
1151}
1152
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001153static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001154 int largepage, gfn_t gfn, struct page *page,
1155 int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001156{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001157 hpa_t table_addr = vcpu->arch.mmu.root_hpa;
Avi Kivitye8332402007-12-09 18:43:00 +02001158 int pt_write = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001159
1160 for (; ; level--) {
1161 u32 index = PT64_INDEX(v, level);
1162 u64 *table;
1163
1164 ASSERT(VALID_PAGE(table_addr));
1165 table = __va(table_addr);
1166
1167 if (level == 1) {
Avi Kivitye8332402007-12-09 18:43:00 +02001168 mmu_set_spte(vcpu, &table[index], ACC_ALL, ACC_ALL,
Avi Kivity947da532008-03-18 11:05:52 +02001169 0, write, 1, &pt_write, 0, gfn, page, false);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001170 return pt_write;
1171 }
1172
1173 if (largepage && level == 2) {
1174 mmu_set_spte(vcpu, &table[index], ACC_ALL, ACC_ALL,
Avi Kivity947da532008-03-18 11:05:52 +02001175 0, write, 1, &pt_write, 1, gfn, page, false);
Avi Kivityd196e342008-01-24 11:44:11 +02001176 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001177 }
1178
Avi Kivityc7addb92007-09-16 18:58:32 +02001179 if (table[index] == shadow_trap_nonpresent_pte) {
Avi Kivity25c0de22007-01-05 16:36:42 -08001180 struct kvm_mmu_page *new_table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001181 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001182
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001183 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
1184 >> PAGE_SHIFT;
1185 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
1186 v, level - 1,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001187 1, ACC_ALL, &table[index]);
Avi Kivity25c0de22007-01-05 16:36:42 -08001188 if (!new_table) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001189 pgprintk("nonpaging_map: ENOMEM\n");
Avi Kivityd7824ff2007-12-30 12:29:05 +02001190 kvm_release_page_clean(page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001191 return -ENOMEM;
1192 }
1193
Avi Kivity47ad8e62007-05-06 15:50:58 +03001194 table[index] = __pa(new_table->spt) | PT_PRESENT_MASK
Avi Kivity25c0de22007-01-05 16:36:42 -08001195 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001196 }
1197 table_addr = table[index] & PT64_BASE_ADDR_MASK;
1198 }
1199}
1200
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001201static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1202{
1203 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001204 int largepage = 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001205
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001206 struct page *page;
1207
1208 down_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001209 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1210 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1211 largepage = 1;
1212 }
1213
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001214 page = gfn_to_page(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001215 up_read(&current->mm->mmap_sem);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001216
Avi Kivityd196e342008-01-24 11:44:11 +02001217 /* mmio */
1218 if (is_error_page(page)) {
1219 kvm_release_page_clean(page);
Avi Kivityd196e342008-01-24 11:44:11 +02001220 return 1;
1221 }
1222
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001223 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02001224 kvm_mmu_free_some_pages(vcpu);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001225 r = __direct_map(vcpu, v, write, largepage, gfn, page,
1226 PT32E_ROOT_LEVEL);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001227 spin_unlock(&vcpu->kvm->mmu_lock);
1228
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001229
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001230 return r;
1231}
1232
1233
Avi Kivityc7addb92007-09-16 18:58:32 +02001234static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1235 struct kvm_mmu_page *sp)
1236{
1237 int i;
1238
1239 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1240 sp->spt[i] = shadow_trap_nonpresent_pte;
1241}
1242
Avi Kivity17ac10a2007-01-05 16:36:40 -08001243static void mmu_free_roots(struct kvm_vcpu *vcpu)
1244{
1245 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02001246 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001247
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001248 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03001249 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001250 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001251#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001252 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1253 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001254
Avi Kivity4db35312007-11-21 15:28:32 +02001255 sp = page_header(root);
1256 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001257 if (!sp->root_count && sp->role.invalid)
1258 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001259 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001260 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001261 return;
1262 }
1263#endif
1264 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001265 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001266
Avi Kivity417726a2007-04-12 17:35:58 +03001267 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03001268 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001269 sp = page_header(root);
1270 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001271 if (!sp->root_count && sp->role.invalid)
1272 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03001273 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001274 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001275 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001276 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001277 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001278}
1279
1280static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
1281{
1282 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001283 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02001284 struct kvm_mmu_page *sp;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001285 int metaphysical = 0;
Avi Kivity3bb65a22007-01-05 16:36:51 -08001286
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001287 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001288
1289#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001290 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1291 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001292
1293 ASSERT(!VALID_PAGE(root));
Joerg Roedelfb72d162008-02-07 13:47:44 +01001294 if (tdp_enabled)
1295 metaphysical = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001296 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001297 PT64_ROOT_LEVEL, metaphysical,
1298 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001299 root = __pa(sp->spt);
1300 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001301 vcpu->arch.mmu.root_hpa = root;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001302 return;
1303 }
1304#endif
Joerg Roedelfb72d162008-02-07 13:47:44 +01001305 metaphysical = !is_paging(vcpu);
1306 if (tdp_enabled)
1307 metaphysical = 1;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001308 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001309 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001310
1311 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001312 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
1313 if (!is_present_pte(vcpu->arch.pdptrs[i])) {
1314 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03001315 continue;
1316 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001317 root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT;
1318 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001319 root_gfn = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02001320 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001321 PT32_ROOT_LEVEL, metaphysical,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001322 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001323 root = __pa(sp->spt);
1324 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001325 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001326 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001327 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001328}
1329
Avi Kivity6aa8b732006-12-10 02:21:36 -08001330static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
1331{
1332 return vaddr;
1333}
1334
1335static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02001336 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001337{
Avi Kivitye8332402007-12-09 18:43:00 +02001338 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08001339 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001340
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001341 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08001342 r = mmu_topup_memory_caches(vcpu);
1343 if (r)
1344 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08001345
Avi Kivity6aa8b732006-12-10 02:21:36 -08001346 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001347 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001348
Avi Kivitye8332402007-12-09 18:43:00 +02001349 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001350
Avi Kivitye8332402007-12-09 18:43:00 +02001351 return nonpaging_map(vcpu, gva & PAGE_MASK,
1352 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001353}
1354
Joerg Roedelfb72d162008-02-07 13:47:44 +01001355static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
1356 u32 error_code)
1357{
1358 struct page *page;
1359 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001360 int largepage = 0;
1361 gfn_t gfn = gpa >> PAGE_SHIFT;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001362
1363 ASSERT(vcpu);
1364 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
1365
1366 r = mmu_topup_memory_caches(vcpu);
1367 if (r)
1368 return r;
1369
1370 down_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001371 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1372 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1373 largepage = 1;
1374 }
1375 page = gfn_to_page(vcpu->kvm, gfn);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03001376 up_read(&current->mm->mmap_sem);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001377 if (is_error_page(page)) {
1378 kvm_release_page_clean(page);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001379 return 1;
1380 }
1381 spin_lock(&vcpu->kvm->mmu_lock);
1382 kvm_mmu_free_some_pages(vcpu);
1383 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001384 largepage, gfn, page, TDP_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001385 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001386
1387 return r;
1388}
1389
Avi Kivity6aa8b732006-12-10 02:21:36 -08001390static void nonpaging_free(struct kvm_vcpu *vcpu)
1391{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001392 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001393}
1394
1395static int nonpaging_init_context(struct kvm_vcpu *vcpu)
1396{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001397 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001398
1399 context->new_cr3 = nonpaging_new_cr3;
1400 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001401 context->gva_to_gpa = nonpaging_gva_to_gpa;
1402 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001403 context->prefetch_page = nonpaging_prefetch_page;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001404 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001405 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001406 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001407 return 0;
1408}
1409
Avi Kivityd835dfe2007-11-21 02:57:59 +02001410void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001411{
Avi Kivity1165f5f2007-04-19 17:27:43 +03001412 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001413 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001414}
1415
1416static void paging_new_cr3(struct kvm_vcpu *vcpu)
1417{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001418 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001419 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001420}
1421
Avi Kivity6aa8b732006-12-10 02:21:36 -08001422static void inject_page_fault(struct kvm_vcpu *vcpu,
1423 u64 addr,
1424 u32 err_code)
1425{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02001426 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001427}
1428
Avi Kivity6aa8b732006-12-10 02:21:36 -08001429static void paging_free(struct kvm_vcpu *vcpu)
1430{
1431 nonpaging_free(vcpu);
1432}
1433
1434#define PTTYPE 64
1435#include "paging_tmpl.h"
1436#undef PTTYPE
1437
1438#define PTTYPE 32
1439#include "paging_tmpl.h"
1440#undef PTTYPE
1441
Avi Kivity17ac10a2007-01-05 16:36:40 -08001442static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001443{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001444 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001445
1446 ASSERT(is_pae(vcpu));
1447 context->new_cr3 = paging_new_cr3;
1448 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001449 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02001450 context->prefetch_page = paging64_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001451 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001452 context->root_level = level;
1453 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001454 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001455 return 0;
1456}
1457
Avi Kivity17ac10a2007-01-05 16:36:40 -08001458static int paging64_init_context(struct kvm_vcpu *vcpu)
1459{
1460 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1461}
1462
Avi Kivity6aa8b732006-12-10 02:21:36 -08001463static int paging32_init_context(struct kvm_vcpu *vcpu)
1464{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001465 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001466
1467 context->new_cr3 = paging_new_cr3;
1468 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001469 context->gva_to_gpa = paging32_gva_to_gpa;
1470 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001471 context->prefetch_page = paging32_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001472 context->root_level = PT32_ROOT_LEVEL;
1473 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001474 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001475 return 0;
1476}
1477
1478static int paging32E_init_context(struct kvm_vcpu *vcpu)
1479{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001480 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001481}
1482
Joerg Roedelfb72d162008-02-07 13:47:44 +01001483static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
1484{
1485 struct kvm_mmu *context = &vcpu->arch.mmu;
1486
1487 context->new_cr3 = nonpaging_new_cr3;
1488 context->page_fault = tdp_page_fault;
1489 context->free = nonpaging_free;
1490 context->prefetch_page = nonpaging_prefetch_page;
1491 context->shadow_root_level = TDP_ROOT_LEVEL;
1492 context->root_hpa = INVALID_PAGE;
1493
1494 if (!is_paging(vcpu)) {
1495 context->gva_to_gpa = nonpaging_gva_to_gpa;
1496 context->root_level = 0;
1497 } else if (is_long_mode(vcpu)) {
1498 context->gva_to_gpa = paging64_gva_to_gpa;
1499 context->root_level = PT64_ROOT_LEVEL;
1500 } else if (is_pae(vcpu)) {
1501 context->gva_to_gpa = paging64_gva_to_gpa;
1502 context->root_level = PT32E_ROOT_LEVEL;
1503 } else {
1504 context->gva_to_gpa = paging32_gva_to_gpa;
1505 context->root_level = PT32_ROOT_LEVEL;
1506 }
1507
1508 return 0;
1509}
1510
1511static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001512{
1513 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001514 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001515
1516 if (!is_paging(vcpu))
1517 return nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08001518 else if (is_long_mode(vcpu))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001519 return paging64_init_context(vcpu);
1520 else if (is_pae(vcpu))
1521 return paging32E_init_context(vcpu);
1522 else
1523 return paging32_init_context(vcpu);
1524}
1525
Joerg Roedelfb72d162008-02-07 13:47:44 +01001526static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1527{
1528 if (tdp_enabled)
1529 return init_kvm_tdp_mmu(vcpu);
1530 else
1531 return init_kvm_softmmu(vcpu);
1532}
1533
Avi Kivity6aa8b732006-12-10 02:21:36 -08001534static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1535{
1536 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001537 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
1538 vcpu->arch.mmu.free(vcpu);
1539 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001540 }
1541}
1542
1543int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1544{
Avi Kivity17c3ba92007-06-04 15:58:30 +03001545 destroy_kvm_mmu(vcpu);
1546 return init_kvm_mmu(vcpu);
1547}
Eddie Dong8668a3c2007-10-10 14:26:45 +08001548EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001549
1550int kvm_mmu_load(struct kvm_vcpu *vcpu)
1551{
Avi Kivity714b93d2007-01-05 16:36:53 -08001552 int r;
1553
Avi Kivitye2dec932007-01-05 16:36:54 -08001554 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001555 if (r)
1556 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001557 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02001558 kvm_mmu_free_some_pages(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001559 mmu_alloc_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001560 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001561 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001562 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001563out:
1564 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001565}
Avi Kivity17c3ba92007-06-04 15:58:30 +03001566EXPORT_SYMBOL_GPL(kvm_mmu_load);
1567
1568void kvm_mmu_unload(struct kvm_vcpu *vcpu)
1569{
1570 mmu_free_roots(vcpu);
1571}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001572
Avi Kivity09072da2007-05-01 14:16:52 +03001573static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02001574 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02001575 u64 *spte)
1576{
1577 u64 pte;
1578 struct kvm_mmu_page *child;
1579
1580 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02001581 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001582 if (sp->role.level == PT_PAGE_TABLE_LEVEL ||
1583 is_large_pte(pte))
Izik Eidus290fc382007-09-27 14:11:22 +02001584 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001585 else {
1586 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03001587 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001588 }
1589 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001590 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001591 if (is_large_pte(pte))
1592 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02001593}
1594
Avi Kivity00284252007-05-01 16:53:31 +03001595static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02001596 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03001597 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02001598 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03001599{
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001600 if ((sp->role.level != PT_PAGE_TABLE_LEVEL)
1601 && !vcpu->arch.update_pte.largepage) {
Avi Kivity4cee5762007-11-18 16:37:07 +02001602 ++vcpu->kvm->stat.mmu_pde_zapped;
Avi Kivity00284252007-05-01 16:53:31 +03001603 return;
Avi Kivity4cee5762007-11-18 16:37:07 +02001604 }
Avi Kivity00284252007-05-01 16:53:31 +03001605
Avi Kivity4cee5762007-11-18 16:37:07 +02001606 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02001607 if (sp->role.glevels == PT32_ROOT_LEVEL)
Dong, Eddie489f1d62008-01-07 11:14:20 +02001608 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03001609 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02001610 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03001611}
1612
Avi Kivity79539ce2007-11-21 02:06:21 +02001613static bool need_remote_flush(u64 old, u64 new)
1614{
1615 if (!is_shadow_present_pte(old))
1616 return false;
1617 if (!is_shadow_present_pte(new))
1618 return true;
1619 if ((old ^ new) & PT64_BASE_ADDR_MASK)
1620 return true;
1621 old ^= PT64_NX_MASK;
1622 new ^= PT64_NX_MASK;
1623 return (old & ~new & PT64_PERM_MASK) != 0;
1624}
1625
1626static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
1627{
1628 if (need_remote_flush(old, new))
1629 kvm_flush_remote_tlbs(vcpu->kvm);
1630 else
1631 kvm_mmu_flush_tlb(vcpu);
1632}
1633
Avi Kivity12b7d282007-09-23 14:10:49 +02001634static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
1635{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001636 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02001637
1638 return !!(spte && (*spte & PT_ACCESSED_MASK));
1639}
1640
Avi Kivityd7824ff2007-12-30 12:29:05 +02001641static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1642 const u8 *new, int bytes)
1643{
1644 gfn_t gfn;
1645 int r;
1646 u64 gpte = 0;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001647 struct page *page;
Avi Kivityd7824ff2007-12-30 12:29:05 +02001648
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001649 vcpu->arch.update_pte.largepage = 0;
1650
Avi Kivityd7824ff2007-12-30 12:29:05 +02001651 if (bytes != 4 && bytes != 8)
1652 return;
1653
1654 /*
1655 * Assume that the pte write on a page table of the same type
1656 * as the current vcpu paging mode. This is nearly always true
1657 * (might be false while changing modes). Note it is verified later
1658 * by update_pte().
1659 */
1660 if (is_pae(vcpu)) {
1661 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
1662 if ((bytes == 4) && (gpa % 4 == 0)) {
1663 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
1664 if (r)
1665 return;
1666 memcpy((void *)&gpte + (gpa % 8), new, 4);
1667 } else if ((bytes == 8) && (gpa % 8 == 0)) {
1668 memcpy((void *)&gpte, new, 8);
1669 }
1670 } else {
1671 if ((bytes == 4) && (gpa % 4 == 0))
1672 memcpy((void *)&gpte, new, 4);
1673 }
1674 if (!is_present_pte(gpte))
1675 return;
1676 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001677
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001678 down_read(&current->mm->mmap_sem);
1679 if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) {
1680 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1681 vcpu->arch.update_pte.largepage = 1;
1682 }
Izik Eidus72dc67a2008-02-10 18:04:15 +02001683 page = gfn_to_page(vcpu->kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001684 up_read(&current->mm->mmap_sem);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001685
Avi Kivityd196e342008-01-24 11:44:11 +02001686 if (is_error_page(page)) {
1687 kvm_release_page_clean(page);
1688 return;
1689 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02001690 vcpu->arch.update_pte.gfn = gfn;
Avi Kivitye48bb492008-03-23 14:21:08 +02001691 vcpu->arch.update_pte.page = page;
Avi Kivityd7824ff2007-12-30 12:29:05 +02001692}
1693
Avi Kivity09072da2007-05-01 14:16:52 +03001694void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Shaohua Life5518812007-07-23 14:51:39 +08001695 const u8 *new, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001696{
Avi Kivity9b7a0322007-01-05 16:36:45 -08001697 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02001698 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001699 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001700 struct hlist_head *bucket;
1701 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02001702 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001703 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001704 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001705 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001706 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001707 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03001708 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001709 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001710 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02001711 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02001712 int r;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001713
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001714 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02001715 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001716 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02001717 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02001718 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02001719 kvm_mmu_audit(vcpu, "pre pte write");
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001720 if (gfn == vcpu->arch.last_pt_write_gfn
Avi Kivity12b7d282007-09-23 14:10:49 +02001721 && !last_updated_pte_accessed(vcpu)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001722 ++vcpu->arch.last_pt_write_count;
1723 if (vcpu->arch.last_pt_write_count >= 3)
Avi Kivity86a5ba02007-01-05 16:36:50 -08001724 flooded = 1;
1725 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001726 vcpu->arch.last_pt_write_gfn = gfn;
1727 vcpu->arch.last_pt_write_count = 1;
1728 vcpu->arch.last_pte_updated = NULL;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001729 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001730 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001731 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001732 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
1733 if (sp->gfn != gfn || sp->role.metaphysical)
Avi Kivity9b7a0322007-01-05 16:36:45 -08001734 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02001735 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001736 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03001737 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001738 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001739 /*
1740 * Misaligned accesses are too much trouble to fix
1741 * up; also, they usually indicate a page is not used
1742 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08001743 *
1744 * If we're seeing too many writes to a page,
1745 * it may no longer be a page table, or we may be
1746 * forking, in which case it is better to unmap the
1747 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001748 */
1749 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02001750 gpa, bytes, sp->role.word);
1751 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02001752 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001753 continue;
1754 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001755 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02001756 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02001757 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001758 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001759 page_offset <<= 1; /* 32->64 */
1760 /*
1761 * A 32-bit pde maps 4MB while the shadow pdes map
1762 * only 2MB. So we need to double the offset again
1763 * and zap two pdes instead of one.
1764 */
1765 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03001766 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02001767 page_offset <<= 1;
1768 npte = 2;
1769 }
Avi Kivityfce06572007-05-01 16:44:05 +03001770 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001771 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001772 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03001773 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001774 }
Avi Kivity4db35312007-11-21 15:28:32 +02001775 spte = &sp->spt[page_offset / sizeof(*spte)];
Dong, Eddie489f1d62008-01-07 11:14:20 +02001776 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
1777 gentry = 0;
1778 r = kvm_read_guest_atomic(vcpu->kvm,
1779 gpa & ~(u64)(pte_size - 1),
1780 &gentry, pte_size);
1781 new = (const void *)&gentry;
1782 if (r < 0)
1783 new = NULL;
1784 }
Avi Kivityac1b7142007-03-08 17:13:32 +02001785 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02001786 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02001787 mmu_pte_write_zap_pte(vcpu, sp, spte);
Dong, Eddie489f1d62008-01-07 11:14:20 +02001788 if (new)
1789 mmu_pte_write_new_pte(vcpu, sp, spte, new);
Avi Kivity79539ce2007-11-21 02:06:21 +02001790 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001791 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001792 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001793 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001794 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001795 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivityd7824ff2007-12-30 12:29:05 +02001796 if (vcpu->arch.update_pte.page) {
1797 kvm_release_page_clean(vcpu->arch.update_pte.page);
1798 vcpu->arch.update_pte.page = NULL;
1799 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08001800}
1801
Avi Kivitya4360362007-01-05 16:36:45 -08001802int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1803{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001804 gpa_t gpa;
1805 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08001806
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001807 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001808
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001809 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001810 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001811 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001812 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08001813}
1814
Avi Kivity22d95b12007-09-14 20:26:06 +03001815void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08001816{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001817 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
Avi Kivity4db35312007-11-21 15:28:32 +02001818 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08001819
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001820 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02001821 struct kvm_mmu_page, link);
1822 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02001823 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08001824 }
1825}
Avi Kivityebeace82007-01-05 16:36:47 -08001826
Avi Kivity30677142007-10-28 18:48:59 +02001827int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
1828{
1829 int r;
1830 enum emulation_result er;
1831
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001832 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02001833 if (r < 0)
1834 goto out;
1835
1836 if (!r) {
1837 r = 1;
1838 goto out;
1839 }
1840
Avi Kivityb733bfb2007-10-28 18:52:05 +02001841 r = mmu_topup_memory_caches(vcpu);
1842 if (r)
1843 goto out;
1844
Avi Kivity30677142007-10-28 18:48:59 +02001845 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02001846
1847 switch (er) {
1848 case EMULATE_DONE:
1849 return 1;
1850 case EMULATE_DO_MMIO:
1851 ++vcpu->stat.mmio_exits;
1852 return 0;
1853 case EMULATE_FAIL:
1854 kvm_report_emulation_failure(vcpu, "pagetable");
1855 return 1;
1856 default:
1857 BUG();
1858 }
1859out:
Avi Kivity30677142007-10-28 18:48:59 +02001860 return r;
1861}
1862EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
1863
Joerg Roedel18552672008-02-07 13:47:41 +01001864void kvm_enable_tdp(void)
1865{
1866 tdp_enabled = true;
1867}
1868EXPORT_SYMBOL_GPL(kvm_enable_tdp);
1869
Avi Kivity6aa8b732006-12-10 02:21:36 -08001870static void free_mmu_pages(struct kvm_vcpu *vcpu)
1871{
Avi Kivity4db35312007-11-21 15:28:32 +02001872 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001873
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001874 while (!list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
1875 sp = container_of(vcpu->kvm->arch.active_mmu_pages.next,
Avi Kivity4db35312007-11-21 15:28:32 +02001876 struct kvm_mmu_page, link);
1877 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivityf51234c2007-01-05 16:36:52 -08001878 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001879 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001880}
1881
1882static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
1883{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001884 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001885 int i;
1886
1887 ASSERT(vcpu);
1888
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001889 if (vcpu->kvm->arch.n_requested_mmu_pages)
1890 vcpu->kvm->arch.n_free_mmu_pages =
1891 vcpu->kvm->arch.n_requested_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001892 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001893 vcpu->kvm->arch.n_free_mmu_pages =
1894 vcpu->kvm->arch.n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001895 /*
1896 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1897 * Therefore we need to allocate shadow page tables in the first
1898 * 4GB of memory, which happens to fit the DMA32 zone.
1899 */
1900 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
1901 if (!page)
1902 goto error_1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001903 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001904 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001905 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001906
Avi Kivity6aa8b732006-12-10 02:21:36 -08001907 return 0;
1908
1909error_1:
1910 free_mmu_pages(vcpu);
1911 return -ENOMEM;
1912}
1913
Ingo Molnar8018c272006-12-29 16:50:01 -08001914int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001915{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001916 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001917 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001918
Ingo Molnar8018c272006-12-29 16:50:01 -08001919 return alloc_mmu_pages(vcpu);
1920}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001921
Ingo Molnar8018c272006-12-29 16:50:01 -08001922int kvm_mmu_setup(struct kvm_vcpu *vcpu)
1923{
1924 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001925 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08001926
Ingo Molnar8018c272006-12-29 16:50:01 -08001927 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001928}
1929
1930void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
1931{
1932 ASSERT(vcpu);
1933
1934 destroy_kvm_mmu(vcpu);
1935 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001936 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001937}
1938
Avi Kivity90cb0522007-07-17 13:04:56 +03001939void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001940{
Avi Kivity4db35312007-11-21 15:28:32 +02001941 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001942
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001943 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001944 int i;
1945 u64 *pt;
1946
Avi Kivity4db35312007-11-21 15:28:32 +02001947 if (!test_bit(slot, &sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001948 continue;
1949
Avi Kivity4db35312007-11-21 15:28:32 +02001950 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001951 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1952 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02001953 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001954 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001955 }
1956}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001957
Avi Kivity90cb0522007-07-17 13:04:56 +03001958void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03001959{
Avi Kivity4db35312007-11-21 15:28:32 +02001960 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03001961
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001962 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001963 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Avi Kivity4db35312007-11-21 15:28:32 +02001964 kvm_mmu_zap_page(kvm, sp);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001965 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03001966
Avi Kivity90cb0522007-07-17 13:04:56 +03001967 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03001968}
1969
Izik Eidus3ee16c82008-03-30 15:17:21 +03001970void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
1971{
1972 struct kvm_mmu_page *page;
1973
1974 page = container_of(kvm->arch.active_mmu_pages.prev,
1975 struct kvm_mmu_page, link);
1976 kvm_mmu_zap_page(kvm, page);
1977}
1978
1979static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
1980{
1981 struct kvm *kvm;
1982 struct kvm *kvm_freed = NULL;
1983 int cache_count = 0;
1984
1985 spin_lock(&kvm_lock);
1986
1987 list_for_each_entry(kvm, &vm_list, vm_list) {
1988 int npages;
1989
1990 spin_lock(&kvm->mmu_lock);
1991 npages = kvm->arch.n_alloc_mmu_pages -
1992 kvm->arch.n_free_mmu_pages;
1993 cache_count += npages;
1994 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
1995 kvm_mmu_remove_one_alloc_mmu_page(kvm);
1996 cache_count--;
1997 kvm_freed = kvm;
1998 }
1999 nr_to_scan--;
2000
2001 spin_unlock(&kvm->mmu_lock);
2002 }
2003 if (kvm_freed)
2004 list_move_tail(&kvm_freed->vm_list, &vm_list);
2005
2006 spin_unlock(&kvm_lock);
2007
2008 return cache_count;
2009}
2010
2011static struct shrinker mmu_shrinker = {
2012 .shrink = mmu_shrink,
2013 .seeks = DEFAULT_SEEKS * 10,
2014};
2015
2016void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03002017{
2018 if (pte_chain_cache)
2019 kmem_cache_destroy(pte_chain_cache);
2020 if (rmap_desc_cache)
2021 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002022 if (mmu_page_header_cache)
2023 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002024}
2025
Izik Eidus3ee16c82008-03-30 15:17:21 +03002026void kvm_mmu_module_exit(void)
2027{
2028 mmu_destroy_caches();
2029 unregister_shrinker(&mmu_shrinker);
2030}
2031
Avi Kivityb5a33a72007-04-15 16:31:09 +03002032int kvm_mmu_module_init(void)
2033{
2034 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2035 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09002036 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002037 if (!pte_chain_cache)
2038 goto nomem;
2039 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2040 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09002041 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002042 if (!rmap_desc_cache)
2043 goto nomem;
2044
Avi Kivityd3d25b02007-05-30 12:34:53 +03002045 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2046 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09002047 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002048 if (!mmu_page_header_cache)
2049 goto nomem;
2050
Izik Eidus3ee16c82008-03-30 15:17:21 +03002051 register_shrinker(&mmu_shrinker);
2052
Avi Kivityb5a33a72007-04-15 16:31:09 +03002053 return 0;
2054
2055nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03002056 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03002057 return -ENOMEM;
2058}
2059
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08002060/*
2061 * Caculate mmu pages needed for kvm.
2062 */
2063unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
2064{
2065 int i;
2066 unsigned int nr_mmu_pages;
2067 unsigned int nr_pages = 0;
2068
2069 for (i = 0; i < kvm->nmemslots; i++)
2070 nr_pages += kvm->memslots[i].npages;
2071
2072 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2073 nr_mmu_pages = max(nr_mmu_pages,
2074 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2075
2076 return nr_mmu_pages;
2077}
2078
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002079static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2080 unsigned len)
2081{
2082 if (len > buffer->len)
2083 return NULL;
2084 return buffer->ptr;
2085}
2086
2087static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2088 unsigned len)
2089{
2090 void *ret;
2091
2092 ret = pv_mmu_peek_buffer(buffer, len);
2093 if (!ret)
2094 return ret;
2095 buffer->ptr += len;
2096 buffer->len -= len;
2097 buffer->processed += len;
2098 return ret;
2099}
2100
2101static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
2102 gpa_t addr, gpa_t value)
2103{
2104 int bytes = 8;
2105 int r;
2106
2107 if (!is_long_mode(vcpu) && !is_pae(vcpu))
2108 bytes = 4;
2109
2110 r = mmu_topup_memory_caches(vcpu);
2111 if (r)
2112 return r;
2113
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002114 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002115 return -EFAULT;
2116
2117 return 1;
2118}
2119
2120static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2121{
2122 kvm_x86_ops->tlb_flush(vcpu);
2123 return 1;
2124}
2125
2126static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
2127{
2128 spin_lock(&vcpu->kvm->mmu_lock);
2129 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
2130 spin_unlock(&vcpu->kvm->mmu_lock);
2131 return 1;
2132}
2133
2134static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
2135 struct kvm_pv_mmu_op_buffer *buffer)
2136{
2137 struct kvm_mmu_op_header *header;
2138
2139 header = pv_mmu_peek_buffer(buffer, sizeof *header);
2140 if (!header)
2141 return 0;
2142 switch (header->op) {
2143 case KVM_MMU_OP_WRITE_PTE: {
2144 struct kvm_mmu_op_write_pte *wpte;
2145
2146 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
2147 if (!wpte)
2148 return 0;
2149 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
2150 wpte->pte_val);
2151 }
2152 case KVM_MMU_OP_FLUSH_TLB: {
2153 struct kvm_mmu_op_flush_tlb *ftlb;
2154
2155 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
2156 if (!ftlb)
2157 return 0;
2158 return kvm_pv_mmu_flush_tlb(vcpu);
2159 }
2160 case KVM_MMU_OP_RELEASE_PT: {
2161 struct kvm_mmu_op_release_pt *rpt;
2162
2163 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
2164 if (!rpt)
2165 return 0;
2166 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
2167 }
2168 default: return 0;
2169 }
2170}
2171
2172int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
2173 gpa_t addr, unsigned long *ret)
2174{
2175 int r;
2176 struct kvm_pv_mmu_op_buffer buffer;
2177
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002178 down_read(&current->mm->mmap_sem);
2179
2180 buffer.ptr = buffer.buf;
2181 buffer.len = min_t(unsigned long, bytes, sizeof buffer.buf);
2182 buffer.processed = 0;
2183
2184 r = kvm_read_guest(vcpu->kvm, addr, buffer.buf, buffer.len);
2185 if (r)
2186 goto out;
2187
2188 while (buffer.len) {
2189 r = kvm_pv_mmu_op_one(vcpu, &buffer);
2190 if (r < 0)
2191 goto out;
2192 if (r == 0)
2193 break;
2194 }
2195
2196 r = 1;
2197out:
2198 *ret = buffer.processed;
2199 up_read(&current->mm->mmap_sem);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002200 return r;
2201}
2202
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002203#ifdef AUDIT
2204
2205static const char *audit_msg;
2206
2207static gva_t canonicalize(gva_t gva)
2208{
2209#ifdef CONFIG_X86_64
2210 gva = (long long)(gva << 16) >> 16;
2211#endif
2212 return gva;
2213}
2214
2215static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
2216 gva_t va, int level)
2217{
2218 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
2219 int i;
2220 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
2221
2222 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
2223 u64 ent = pt[i];
2224
Avi Kivityc7addb92007-09-16 18:58:32 +02002225 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002226 continue;
2227
2228 va = canonicalize(va);
Avi Kivityc7addb92007-09-16 18:58:32 +02002229 if (level > 1) {
2230 if (ent == shadow_notrap_nonpresent_pte)
2231 printk(KERN_ERR "audit: (%s) nontrapping pte"
2232 " in nonleaf level: levels %d gva %lx"
2233 " level %d pte %llx\n", audit_msg,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002234 vcpu->arch.mmu.root_level, va, level, ent);
Avi Kivityc7addb92007-09-16 18:58:32 +02002235
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002236 audit_mappings_page(vcpu, ent, va, level - 1);
Avi Kivityc7addb92007-09-16 18:58:32 +02002237 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002238 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
Avi Kivity1d28f5f2007-11-21 15:01:44 +02002239 struct page *page = gpa_to_page(vcpu, gpa);
2240 hpa_t hpa = page_to_phys(page);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002241
Avi Kivityc7addb92007-09-16 18:58:32 +02002242 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002243 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02002244 printk(KERN_ERR "xx audit error: (%s) levels %d"
2245 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002246 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04002247 va, gpa, hpa, ent,
2248 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02002249 else if (ent == shadow_notrap_nonpresent_pte
2250 && !is_error_hpa(hpa))
2251 printk(KERN_ERR "audit: (%s) notrap shadow,"
2252 " valid guest gva %lx\n", audit_msg, va);
Izik Eidusb4231d62007-11-20 11:49:33 +02002253 kvm_release_page_clean(page);
Avi Kivityc7addb92007-09-16 18:58:32 +02002254
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002255 }
2256 }
2257}
2258
2259static void audit_mappings(struct kvm_vcpu *vcpu)
2260{
Avi Kivity1ea252a2007-03-08 11:48:09 +02002261 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002262
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002263 if (vcpu->arch.mmu.root_level == 4)
2264 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002265 else
2266 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002267 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002268 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002269 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002270 i << 30,
2271 2);
2272}
2273
2274static int count_rmaps(struct kvm_vcpu *vcpu)
2275{
2276 int nmaps = 0;
2277 int i, j, k;
2278
2279 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
2280 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
2281 struct kvm_rmap_desc *d;
2282
2283 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02002284 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002285
Izik Eidus290fc382007-09-27 14:11:22 +02002286 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002287 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02002288 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002289 ++nmaps;
2290 continue;
2291 }
Izik Eidus290fc382007-09-27 14:11:22 +02002292 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002293 while (d) {
2294 for (k = 0; k < RMAP_EXT; ++k)
2295 if (d->shadow_ptes[k])
2296 ++nmaps;
2297 else
2298 break;
2299 d = d->more;
2300 }
2301 }
2302 }
2303 return nmaps;
2304}
2305
2306static int count_writable_mappings(struct kvm_vcpu *vcpu)
2307{
2308 int nmaps = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02002309 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002310 int i;
2311
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002312 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02002313 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002314
Avi Kivity4db35312007-11-21 15:28:32 +02002315 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002316 continue;
2317
2318 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
2319 u64 ent = pt[i];
2320
2321 if (!(ent & PT_PRESENT_MASK))
2322 continue;
2323 if (!(ent & PT_WRITABLE_MASK))
2324 continue;
2325 ++nmaps;
2326 }
2327 }
2328 return nmaps;
2329}
2330
2331static void audit_rmap(struct kvm_vcpu *vcpu)
2332{
2333 int n_rmap = count_rmaps(vcpu);
2334 int n_actual = count_writable_mappings(vcpu);
2335
2336 if (n_rmap != n_actual)
2337 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002338 __func__, audit_msg, n_rmap, n_actual);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002339}
2340
2341static void audit_write_protection(struct kvm_vcpu *vcpu)
2342{
Avi Kivity4db35312007-11-21 15:28:32 +02002343 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02002344 struct kvm_memory_slot *slot;
2345 unsigned long *rmapp;
2346 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002347
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002348 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02002349 if (sp->role.metaphysical)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002350 continue;
2351
Avi Kivity4db35312007-11-21 15:28:32 +02002352 slot = gfn_to_memslot(vcpu->kvm, sp->gfn);
2353 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02002354 rmapp = &slot->rmap[gfn - slot->base_gfn];
2355 if (*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002356 printk(KERN_ERR "%s: (%s) shadow page has writable"
2357 " mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002358 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02002359 sp->role.word);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002360 }
2361}
2362
2363static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
2364{
2365 int olddbg = dbg;
2366
2367 dbg = 0;
2368 audit_msg = msg;
2369 audit_rmap(vcpu);
2370 audit_write_protection(vcpu);
2371 audit_mappings(vcpu);
2372 dbg = olddbg;
2373}
2374
2375#endif