blob: 078a7f1ac34c186a97ab31d15562ba18461be818 [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
Anthony Liguori35149e22008-04-02 14:46:56 -0500243static pfn_t spte_to_pfn(u64 pte)
Avi Kivity0b49ea82008-03-23 15:06:23 +0200244{
Anthony Liguori35149e22008-04-02 14:46:56 -0500245 return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity0b49ea82008-03-23 15:06:23 +0200246}
247
Avi Kivityda9285212007-11-21 13:54:47 +0200248static gfn_t pse36_gfn_delta(u32 gpte)
249{
250 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
251
252 return (gpte & PT32_DIR_PSE36_MASK) << shift;
253}
254
Avi Kivitye663ee62007-05-31 15:46:04 +0300255static void set_shadow_pte(u64 *sptep, u64 spte)
256{
257#ifdef CONFIG_X86_64
258 set_64bit((unsigned long *)sptep, spte);
259#else
260 set_64bit((unsigned long long *)sptep, spte);
261#endif
262}
263
Avi Kivitye2dec932007-01-05 16:36:54 -0800264static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300265 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800266{
267 void *obj;
268
269 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800270 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800271 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300272 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800273 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800274 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800275 cache->objects[cache->nobjs++] = obj;
276 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800277 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800278}
279
280static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
281{
282 while (mc->nobjs)
283 kfree(mc->objects[--mc->nobjs]);
284}
285
Avi Kivityc1158e62007-07-20 08:18:27 +0300286static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300287 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300288{
289 struct page *page;
290
291 if (cache->nobjs >= min)
292 return 0;
293 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300294 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300295 if (!page)
296 return -ENOMEM;
297 set_page_private(page, 0);
298 cache->objects[cache->nobjs++] = page_address(page);
299 }
300 return 0;
301}
302
303static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
304{
305 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300306 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300307}
308
Avi Kivity8c438502007-04-16 11:53:17 +0300309static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
310{
311 int r;
312
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800313 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300314 pte_chain_cache, 4);
315 if (r)
316 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800317 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300318 rmap_desc_cache, 1);
319 if (r)
320 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800321 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300322 if (r)
323 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800324 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300325 mmu_page_header_cache, 4);
326out:
Avi Kivity8c438502007-04-16 11:53:17 +0300327 return r;
328}
329
Avi Kivity714b93d2007-01-05 16:36:53 -0800330static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
331{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800332 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
333 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
334 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
335 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800336}
337
338static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
339 size_t size)
340{
341 void *p;
342
343 BUG_ON(!mc->nobjs);
344 p = mc->objects[--mc->nobjs];
345 memset(p, 0, size);
346 return p;
347}
348
Avi Kivity714b93d2007-01-05 16:36:53 -0800349static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
350{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800351 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800352 sizeof(struct kvm_pte_chain));
353}
354
Avi Kivity90cb0522007-07-17 13:04:56 +0300355static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800356{
Avi Kivity90cb0522007-07-17 13:04:56 +0300357 kfree(pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800358}
359
360static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
361{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800362 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800363 sizeof(struct kvm_rmap_desc));
364}
365
Avi Kivity90cb0522007-07-17 13:04:56 +0300366static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800367{
Avi Kivity90cb0522007-07-17 13:04:56 +0300368 kfree(rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800369}
370
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800371/*
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300372 * Return the pointer to the largepage write count for a given
373 * gfn, handling slots that are not large page aligned.
374 */
375static int *slot_largepage_idx(gfn_t gfn, struct kvm_memory_slot *slot)
376{
377 unsigned long idx;
378
379 idx = (gfn / KVM_PAGES_PER_HPAGE) -
380 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
381 return &slot->lpage_info[idx].write_count;
382}
383
384static void account_shadowed(struct kvm *kvm, gfn_t gfn)
385{
386 int *write_count;
387
388 write_count = slot_largepage_idx(gfn, gfn_to_memslot(kvm, gfn));
389 *write_count += 1;
390 WARN_ON(*write_count > KVM_PAGES_PER_HPAGE);
391}
392
393static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
394{
395 int *write_count;
396
397 write_count = slot_largepage_idx(gfn, gfn_to_memslot(kvm, gfn));
398 *write_count -= 1;
399 WARN_ON(*write_count < 0);
400}
401
402static int has_wrprotected_page(struct kvm *kvm, gfn_t gfn)
403{
404 struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
405 int *largepage_idx;
406
407 if (slot) {
408 largepage_idx = slot_largepage_idx(gfn, slot);
409 return *largepage_idx;
410 }
411
412 return 1;
413}
414
415static int host_largepage_backed(struct kvm *kvm, gfn_t gfn)
416{
417 struct vm_area_struct *vma;
418 unsigned long addr;
419
420 addr = gfn_to_hva(kvm, gfn);
421 if (kvm_is_error_hva(addr))
422 return 0;
423
424 vma = find_vma(current->mm, addr);
425 if (vma && is_vm_hugetlb_page(vma))
426 return 1;
427
428 return 0;
429}
430
431static int is_largepage_backed(struct kvm_vcpu *vcpu, gfn_t large_gfn)
432{
433 struct kvm_memory_slot *slot;
434
435 if (has_wrprotected_page(vcpu->kvm, large_gfn))
436 return 0;
437
438 if (!host_largepage_backed(vcpu->kvm, large_gfn))
439 return 0;
440
441 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
442 if (slot && slot->dirty_bitmap)
443 return 0;
444
445 return 1;
446}
447
448/*
Izik Eidus290fc382007-09-27 14:11:22 +0200449 * Take gfn and return the reverse mapping to it.
450 * Note: gfn must be unaliased before this function get called
451 */
452
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300453static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int lpage)
Izik Eidus290fc382007-09-27 14:11:22 +0200454{
455 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300456 unsigned long idx;
Izik Eidus290fc382007-09-27 14:11:22 +0200457
458 slot = gfn_to_memslot(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300459 if (!lpage)
460 return &slot->rmap[gfn - slot->base_gfn];
461
462 idx = (gfn / KVM_PAGES_PER_HPAGE) -
463 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
464
465 return &slot->lpage_info[idx].rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200466}
467
468/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800469 * Reverse mapping data structures:
470 *
Izik Eidus290fc382007-09-27 14:11:22 +0200471 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
472 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800473 *
Izik Eidus290fc382007-09-27 14:11:22 +0200474 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
475 * containing more mappings.
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800476 */
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300477static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn, int lpage)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800478{
Avi Kivity4db35312007-11-21 15:28:32 +0200479 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800480 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200481 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800482 int i;
483
484 if (!is_rmap_pte(*spte))
485 return;
Izik Eidus290fc382007-09-27 14:11:22 +0200486 gfn = unalias_gfn(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200487 sp = page_header(__pa(spte));
488 sp->gfns[spte - sp->spt] = gfn;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300489 rmapp = gfn_to_rmap(vcpu->kvm, gfn, lpage);
Izik Eidus290fc382007-09-27 14:11:22 +0200490 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800491 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200492 *rmapp = (unsigned long)spte;
493 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800494 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800495 desc = mmu_alloc_rmap_desc(vcpu);
Izik Eidus290fc382007-09-27 14:11:22 +0200496 desc->shadow_ptes[0] = (u64 *)*rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800497 desc->shadow_ptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200498 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800499 } else {
500 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200501 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800502 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
503 desc = desc->more;
504 if (desc->shadow_ptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800505 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800506 desc = desc->more;
507 }
508 for (i = 0; desc->shadow_ptes[i]; ++i)
509 ;
510 desc->shadow_ptes[i] = spte;
511 }
512}
513
Izik Eidus290fc382007-09-27 14:11:22 +0200514static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800515 struct kvm_rmap_desc *desc,
516 int i,
517 struct kvm_rmap_desc *prev_desc)
518{
519 int j;
520
521 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
522 ;
523 desc->shadow_ptes[i] = desc->shadow_ptes[j];
Al Viro11718b4d2007-02-09 16:39:20 +0000524 desc->shadow_ptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800525 if (j != 0)
526 return;
527 if (!prev_desc && !desc->more)
Izik Eidus290fc382007-09-27 14:11:22 +0200528 *rmapp = (unsigned long)desc->shadow_ptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800529 else
530 if (prev_desc)
531 prev_desc->more = desc->more;
532 else
Izik Eidus290fc382007-09-27 14:11:22 +0200533 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300534 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800535}
536
Izik Eidus290fc382007-09-27 14:11:22 +0200537static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800538{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800539 struct kvm_rmap_desc *desc;
540 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200541 struct kvm_mmu_page *sp;
Anthony Liguori35149e22008-04-02 14:46:56 -0500542 pfn_t pfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200543 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800544 int i;
545
546 if (!is_rmap_pte(*spte))
547 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200548 sp = page_header(__pa(spte));
Anthony Liguori35149e22008-04-02 14:46:56 -0500549 pfn = spte_to_pfn(*spte);
Avi Kivityfcd6dba2008-04-03 12:02:21 +0300550 if (*spte & PT_ACCESSED_MASK)
Anthony Liguori35149e22008-04-02 14:46:56 -0500551 kvm_set_pfn_accessed(pfn);
Izik Eidusb4231d62007-11-20 11:49:33 +0200552 if (is_writeble_pte(*spte))
Anthony Liguori35149e22008-04-02 14:46:56 -0500553 kvm_release_pfn_dirty(pfn);
Izik Eidusb4231d62007-11-20 11:49:33 +0200554 else
Anthony Liguori35149e22008-04-02 14:46:56 -0500555 kvm_release_pfn_clean(pfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300556 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], is_large_pte(*spte));
Izik Eidus290fc382007-09-27 14:11:22 +0200557 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800558 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
559 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200560 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800561 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200562 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800563 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
564 spte, *spte);
565 BUG();
566 }
Izik Eidus290fc382007-09-27 14:11:22 +0200567 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800568 } else {
569 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200570 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800571 prev_desc = NULL;
572 while (desc) {
573 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
574 if (desc->shadow_ptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200575 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800576 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800577 prev_desc);
578 return;
579 }
580 prev_desc = desc;
581 desc = desc->more;
582 }
583 BUG();
584 }
585}
586
Izik Eidus98348e92007-10-16 14:42:30 +0200587static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800588{
Avi Kivity374cbac2007-01-05 16:36:43 -0800589 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200590 struct kvm_rmap_desc *prev_desc;
591 u64 *prev_spte;
592 int i;
593
594 if (!*rmapp)
595 return NULL;
596 else if (!(*rmapp & 1)) {
597 if (!spte)
598 return (u64 *)*rmapp;
599 return NULL;
600 }
601 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
602 prev_desc = NULL;
603 prev_spte = NULL;
604 while (desc) {
605 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) {
606 if (prev_spte == spte)
607 return desc->shadow_ptes[i];
608 prev_spte = desc->shadow_ptes[i];
609 }
610 desc = desc->more;
611 }
612 return NULL;
613}
614
615static void rmap_write_protect(struct kvm *kvm, u64 gfn)
616{
Izik Eidus290fc382007-09-27 14:11:22 +0200617 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800618 u64 *spte;
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800619 int write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800620
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500621 gfn = unalias_gfn(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300622 rmapp = gfn_to_rmap(kvm, gfn, 0);
Avi Kivity374cbac2007-01-05 16:36:43 -0800623
Izik Eidus98348e92007-10-16 14:42:30 +0200624 spte = rmap_next(kvm, rmapp, NULL);
625 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800626 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800627 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800628 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800629 if (is_writeble_pte(*spte)) {
Izik Eidus9647c142007-10-16 14:43:46 +0200630 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800631 write_protected = 1;
632 }
Izik Eidus9647c142007-10-16 14:43:46 +0200633 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800634 }
Izik Eidus855149a2008-03-20 18:17:24 +0200635 if (write_protected) {
Anthony Liguori35149e22008-04-02 14:46:56 -0500636 pfn_t pfn;
Izik Eidus855149a2008-03-20 18:17:24 +0200637
638 spte = rmap_next(kvm, rmapp, NULL);
Anthony Liguori35149e22008-04-02 14:46:56 -0500639 pfn = spte_to_pfn(*spte);
640 kvm_set_pfn_dirty(pfn);
Izik Eidus855149a2008-03-20 18:17:24 +0200641 }
642
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300643 /* check for huge page mappings */
644 rmapp = gfn_to_rmap(kvm, gfn, 1);
645 spte = rmap_next(kvm, rmapp, NULL);
646 while (spte) {
647 BUG_ON(!spte);
648 BUG_ON(!(*spte & PT_PRESENT_MASK));
649 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
650 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
651 if (is_writeble_pte(*spte)) {
652 rmap_remove(kvm, spte);
653 --kvm->stat.lpages;
654 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
655 write_protected = 1;
656 }
657 spte = rmap_next(kvm, rmapp, spte);
658 }
659
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800660 if (write_protected)
661 kvm_flush_remote_tlbs(kvm);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300662
663 account_shadowed(kvm, gfn);
Avi Kivity374cbac2007-01-05 16:36:43 -0800664}
665
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800666#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300667static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800668{
Avi Kivity139bdb22007-01-05 16:36:50 -0800669 u64 *pos;
670 u64 *end;
671
Avi Kivity47ad8e62007-05-06 15:50:58 +0300672 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivityd196e342008-01-24 11:44:11 +0200673 if (*pos != shadow_trap_nonpresent_pte) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800674 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800675 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800676 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800677 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800678 return 1;
679}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800680#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800681
Avi Kivity4db35312007-11-21 15:28:32 +0200682static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800683{
Avi Kivity4db35312007-11-21 15:28:32 +0200684 ASSERT(is_empty_shadow_page(sp->spt));
685 list_del(&sp->link);
686 __free_page(virt_to_page(sp->spt));
687 __free_page(virt_to_page(sp->gfns));
688 kfree(sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800689 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800690}
691
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800692static unsigned kvm_page_table_hashfn(gfn_t gfn)
693{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200694 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800695}
696
Avi Kivity25c0de22007-01-05 16:36:42 -0800697static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
698 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800699{
Avi Kivity4db35312007-11-21 15:28:32 +0200700 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800701
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800702 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
703 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
704 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200705 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800706 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Avi Kivity4db35312007-11-21 15:28:32 +0200707 ASSERT(is_empty_shadow_page(sp->spt));
708 sp->slot_bitmap = 0;
709 sp->multimapped = 0;
710 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800711 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200712 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800713}
714
Avi Kivity714b93d2007-01-05 16:36:53 -0800715static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200716 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800717{
718 struct kvm_pte_chain *pte_chain;
719 struct hlist_node *node;
720 int i;
721
722 if (!parent_pte)
723 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200724 if (!sp->multimapped) {
725 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800726
727 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200728 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800729 return;
730 }
Avi Kivity4db35312007-11-21 15:28:32 +0200731 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800732 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200733 INIT_HLIST_HEAD(&sp->parent_ptes);
734 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800735 pte_chain->parent_ptes[0] = old;
736 }
Avi Kivity4db35312007-11-21 15:28:32 +0200737 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800738 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
739 continue;
740 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
741 if (!pte_chain->parent_ptes[i]) {
742 pte_chain->parent_ptes[i] = parent_pte;
743 return;
744 }
745 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800746 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800747 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200748 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800749 pte_chain->parent_ptes[0] = parent_pte;
750}
751
Avi Kivity4db35312007-11-21 15:28:32 +0200752static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800753 u64 *parent_pte)
754{
755 struct kvm_pte_chain *pte_chain;
756 struct hlist_node *node;
757 int i;
758
Avi Kivity4db35312007-11-21 15:28:32 +0200759 if (!sp->multimapped) {
760 BUG_ON(sp->parent_pte != parent_pte);
761 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800762 return;
763 }
Avi Kivity4db35312007-11-21 15:28:32 +0200764 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800765 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
766 if (!pte_chain->parent_ptes[i])
767 break;
768 if (pte_chain->parent_ptes[i] != parent_pte)
769 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800770 while (i + 1 < NR_PTE_CHAIN_ENTRIES
771 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800772 pte_chain->parent_ptes[i]
773 = pte_chain->parent_ptes[i + 1];
774 ++i;
775 }
776 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800777 if (i == 0) {
778 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300779 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200780 if (hlist_empty(&sp->parent_ptes)) {
781 sp->multimapped = 0;
782 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800783 }
784 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800785 return;
786 }
787 BUG();
788}
789
Avi Kivity4db35312007-11-21 15:28:32 +0200790static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800791{
792 unsigned index;
793 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200794 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800795 struct hlist_node *node;
796
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800797 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200798 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800799 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200800 hlist_for_each_entry(sp, node, bucket, hash_link)
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500801 if (sp->gfn == gfn && !sp->role.metaphysical
802 && !sp->role.invalid) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800803 pgprintk("%s: found role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800804 __func__, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +0200805 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800806 }
807 return NULL;
808}
809
810static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
811 gfn_t gfn,
812 gva_t gaddr,
813 unsigned level,
814 int metaphysical,
Avi Kivity41074d02007-12-09 17:00:02 +0200815 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +0200816 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800817{
818 union kvm_mmu_page_role role;
819 unsigned index;
820 unsigned quadrant;
821 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200822 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800823 struct hlist_node *node;
824
825 role.word = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800826 role.glevels = vcpu->arch.mmu.root_level;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800827 role.level = level;
828 role.metaphysical = metaphysical;
Avi Kivity41074d02007-12-09 17:00:02 +0200829 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800830 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800831 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
832 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
833 role.quadrant = quadrant;
834 }
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800835 pgprintk("%s: looking gfn %lx role %x\n", __func__,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800836 gfn, role.word);
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200837 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800838 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200839 hlist_for_each_entry(sp, node, bucket, hash_link)
840 if (sp->gfn == gfn && sp->role.word == role.word) {
841 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800842 pgprintk("%s: found\n", __func__);
Avi Kivity4db35312007-11-21 15:28:32 +0200843 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800844 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +0200845 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +0200846 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
847 if (!sp)
848 return sp;
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800849 pgprintk("%s: adding gfn %lx role %x\n", __func__, gfn, role.word);
Avi Kivity4db35312007-11-21 15:28:32 +0200850 sp->gfn = gfn;
851 sp->role = role;
852 hlist_add_head(&sp->hash_link, bucket);
Avi Kivity374cbac2007-01-05 16:36:43 -0800853 if (!metaphysical)
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500854 rmap_write_protect(vcpu->kvm, gfn);
Marcelo Tosattibed1d1d2008-04-04 14:56:44 -0300855 vcpu->arch.mmu.prefetch_page(vcpu, sp);
Avi Kivity4db35312007-11-21 15:28:32 +0200856 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800857}
858
Avi Kivity90cb0522007-07-17 13:04:56 +0300859static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +0200860 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -0800861{
Avi Kivity697fe2e2007-01-05 16:36:46 -0800862 unsigned i;
863 u64 *pt;
864 u64 ent;
865
Avi Kivity4db35312007-11-21 15:28:32 +0200866 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800867
Avi Kivity4db35312007-11-21 15:28:32 +0200868 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800869 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Avi Kivityc7addb92007-09-16 18:58:32 +0200870 if (is_shadow_present_pte(pt[i]))
Izik Eidus290fc382007-09-27 14:11:22 +0200871 rmap_remove(kvm, &pt[i]);
Avi Kivityc7addb92007-09-16 18:58:32 +0200872 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800873 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300874 kvm_flush_remote_tlbs(kvm);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800875 return;
876 }
877
878 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
879 ent = pt[i];
880
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300881 if (is_shadow_present_pte(ent)) {
882 if (!is_large_pte(ent)) {
883 ent &= PT64_BASE_ADDR_MASK;
884 mmu_page_remove_parent_pte(page_header(ent),
885 &pt[i]);
886 } else {
887 --kvm->stat.lpages;
888 rmap_remove(kvm, &pt[i]);
889 }
890 }
Avi Kivityc7addb92007-09-16 18:58:32 +0200891 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800892 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300893 kvm_flush_remote_tlbs(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800894}
895
Avi Kivity4db35312007-11-21 15:28:32 +0200896static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800897{
Avi Kivity4db35312007-11-21 15:28:32 +0200898 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800899}
900
Avi Kivity12b7d282007-09-23 14:10:49 +0200901static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
902{
903 int i;
904
905 for (i = 0; i < KVM_MAX_VCPUS; ++i)
906 if (kvm->vcpus[i])
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800907 kvm->vcpus[i]->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +0200908}
909
Avi Kivity4db35312007-11-21 15:28:32 +0200910static void kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -0800911{
912 u64 *parent_pte;
913
Avi Kivity4cee5762007-11-18 16:37:07 +0200914 ++kvm->stat.mmu_shadow_zapped;
Avi Kivity4db35312007-11-21 15:28:32 +0200915 while (sp->multimapped || sp->parent_pte) {
916 if (!sp->multimapped)
917 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -0800918 else {
919 struct kvm_pte_chain *chain;
920
Avi Kivity4db35312007-11-21 15:28:32 +0200921 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -0800922 struct kvm_pte_chain, link);
923 parent_pte = chain->parent_ptes[0];
924 }
Avi Kivity697fe2e2007-01-05 16:36:46 -0800925 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +0200926 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityc7addb92007-09-16 18:58:32 +0200927 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800928 }
Avi Kivity4db35312007-11-21 15:28:32 +0200929 kvm_mmu_page_unlink_children(kvm, sp);
930 if (!sp->root_count) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300931 if (!sp->role.metaphysical)
932 unaccount_shadowed(kvm, sp->gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200933 hlist_del(&sp->hash_link);
934 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500935 } else {
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800936 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500937 sp->role.invalid = 1;
938 kvm_reload_remote_mmus(kvm);
939 }
Avi Kivity12b7d282007-09-23 14:10:49 +0200940 kvm_mmu_reset_last_pte_updated(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800941}
942
Izik Eidus82ce2c92007-10-02 18:52:55 +0200943/*
944 * Changing the number of mmu pages allocated to the vm
945 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
946 */
947void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
948{
949 /*
950 * If we set the number of mmu pages to be smaller be than the
951 * number of actived pages , we must to free some mmu pages before we
952 * change the value
953 */
954
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800955 if ((kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages) >
Izik Eidus82ce2c92007-10-02 18:52:55 +0200956 kvm_nr_mmu_pages) {
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800957 int n_used_mmu_pages = kvm->arch.n_alloc_mmu_pages
958 - kvm->arch.n_free_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200959
960 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
961 struct kvm_mmu_page *page;
962
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800963 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +0200964 struct kvm_mmu_page, link);
965 kvm_mmu_zap_page(kvm, page);
966 n_used_mmu_pages--;
967 }
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800968 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200969 }
970 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800971 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
972 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200973
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800974 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200975}
976
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500977static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -0800978{
979 unsigned index;
980 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200981 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -0800982 struct hlist_node *node, *n;
983 int r;
984
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800985 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -0800986 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200987 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800988 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200989 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
990 if (sp->gfn == gfn && !sp->role.metaphysical) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800991 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +0200992 sp->role.word);
993 kvm_mmu_zap_page(kvm, sp);
Avi Kivitya4360362007-01-05 16:36:45 -0800994 r = 1;
995 }
996 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800997}
998
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500999static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +03001000{
Avi Kivity4db35312007-11-21 15:28:32 +02001001 struct kvm_mmu_page *sp;
Avi Kivity97a0a012007-05-31 15:08:29 +03001002
Avi Kivity4db35312007-11-21 15:28:32 +02001003 while ((sp = kvm_mmu_lookup_page(kvm, gfn)) != NULL) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001004 pgprintk("%s: zap %lx %x\n", __func__, gfn, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +02001005 kvm_mmu_zap_page(kvm, sp);
Avi Kivity97a0a012007-05-31 15:08:29 +03001006 }
1007}
1008
Avi Kivity38c335f2007-11-21 14:20:22 +02001009static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001010{
Avi Kivity38c335f2007-11-21 14:20:22 +02001011 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity4db35312007-11-21 15:28:32 +02001012 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001013
Avi Kivity4db35312007-11-21 15:28:32 +02001014 __set_bit(slot, &sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001015}
1016
Avi Kivity039576c2007-03-20 12:46:50 +02001017struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1018{
Izik Eidus72dc67a2008-02-10 18:04:15 +02001019 struct page *page;
1020
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001021 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Avi Kivity039576c2007-03-20 12:46:50 +02001022
1023 if (gpa == UNMAPPED_GVA)
1024 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001025
1026 down_read(&current->mm->mmap_sem);
1027 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1028 up_read(&current->mm->mmap_sem);
1029
1030 return page;
Avi Kivity039576c2007-03-20 12:46:50 +02001031}
1032
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001033static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1034 unsigned pt_access, unsigned pte_access,
1035 int user_fault, int write_fault, int dirty,
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001036 int *ptwrite, int largepage, gfn_t gfn,
Anthony Liguori35149e22008-04-02 14:46:56 -05001037 pfn_t pfn, bool speculative)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001038{
1039 u64 spte;
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001040 int was_rmapped = 0;
Izik Eidus75e68e62008-01-12 23:49:09 +02001041 int was_writeble = is_writeble_pte(*shadow_pte);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001042
Avi Kivitybc750ba2007-12-09 18:39:41 +02001043 pgprintk("%s: spte %llx access %x write_fault %d"
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001044 " user_fault %d gfn %lx\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001045 __func__, *shadow_pte, pt_access,
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001046 write_fault, user_fault, gfn);
1047
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001048 if (is_rmap_pte(*shadow_pte)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001049 /*
1050 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1051 * the parent of the now unreachable PTE.
1052 */
1053 if (largepage && !is_large_pte(*shadow_pte)) {
1054 struct kvm_mmu_page *child;
1055 u64 pte = *shadow_pte;
1056
1057 child = page_header(pte & PT64_BASE_ADDR_MASK);
1058 mmu_page_remove_parent_pte(child, shadow_pte);
Anthony Liguori35149e22008-04-02 14:46:56 -05001059 } else if (pfn != spte_to_pfn(*shadow_pte)) {
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001060 pgprintk("hfn old %lx new %lx\n",
Anthony Liguori35149e22008-04-02 14:46:56 -05001061 spte_to_pfn(*shadow_pte), pfn);
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001062 rmap_remove(vcpu->kvm, shadow_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001063 } else {
1064 if (largepage)
1065 was_rmapped = is_large_pte(*shadow_pte);
1066 else
1067 was_rmapped = 1;
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001068 }
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001069 }
1070
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001071 /*
1072 * We don't set the accessed bit, since we sometimes want to see
1073 * whether the guest actually used the pte (in order to detect
1074 * demand paging).
1075 */
1076 spte = PT_PRESENT_MASK | PT_DIRTY_MASK;
Avi Kivity947da532008-03-18 11:05:52 +02001077 if (!speculative)
1078 pte_access |= PT_ACCESSED_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001079 if (!dirty)
1080 pte_access &= ~ACC_WRITE_MASK;
1081 if (!(pte_access & ACC_EXEC_MASK))
1082 spte |= PT64_NX_MASK;
1083
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001084 spte |= PT_PRESENT_MASK;
1085 if (pte_access & ACC_USER_MASK)
1086 spte |= PT_USER_MASK;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001087 if (largepage)
1088 spte |= PT_PAGE_SIZE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001089
Anthony Liguori35149e22008-04-02 14:46:56 -05001090 spte |= (u64)pfn << PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001091
1092 if ((pte_access & ACC_WRITE_MASK)
1093 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
1094 struct kvm_mmu_page *shadow;
1095
1096 spte |= PT_WRITABLE_MASK;
1097 if (user_fault) {
1098 mmu_unshadow(vcpu->kvm, gfn);
1099 goto unshadowed;
1100 }
1101
1102 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001103 if (shadow ||
1104 (largepage && has_wrprotected_page(vcpu->kvm, gfn))) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001105 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001106 __func__, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001107 pte_access &= ~ACC_WRITE_MASK;
1108 if (is_writeble_pte(spte)) {
1109 spte &= ~PT_WRITABLE_MASK;
1110 kvm_x86_ops->tlb_flush(vcpu);
1111 }
1112 if (write_fault)
1113 *ptwrite = 1;
1114 }
1115 }
1116
1117unshadowed:
1118
1119 if (pte_access & ACC_WRITE_MASK)
1120 mark_page_dirty(vcpu->kvm, gfn);
1121
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001122 pgprintk("%s: setting spte %llx\n", __func__, spte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001123 pgprintk("instantiating %s PTE (%s) at %d (%llx) addr %llx\n",
1124 (spte&PT_PAGE_SIZE_MASK)? "2MB" : "4kB",
1125 (spte&PT_WRITABLE_MASK)?"RW":"R", gfn, spte, shadow_pte);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001126 set_shadow_pte(shadow_pte, spte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001127 if (!was_rmapped && (spte & PT_PAGE_SIZE_MASK)
1128 && (spte & PT_PRESENT_MASK))
1129 ++vcpu->kvm->stat.lpages;
1130
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001131 page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
1132 if (!was_rmapped) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001133 rmap_add(vcpu, shadow_pte, gfn, largepage);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001134 if (!is_rmap_pte(*shadow_pte))
Anthony Liguori35149e22008-04-02 14:46:56 -05001135 kvm_release_pfn_clean(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001136 } else {
1137 if (was_writeble)
Anthony Liguori35149e22008-04-02 14:46:56 -05001138 kvm_release_pfn_dirty(pfn);
Izik Eidus75e68e62008-01-12 23:49:09 +02001139 else
Anthony Liguori35149e22008-04-02 14:46:56 -05001140 kvm_release_pfn_clean(pfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001141 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001142 if (!ptwrite || !*ptwrite)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001143 vcpu->arch.last_pte_updated = shadow_pte;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001144}
1145
Avi Kivity6aa8b732006-12-10 02:21:36 -08001146static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1147{
1148}
1149
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001150static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Anthony Liguori35149e22008-04-02 14:46:56 -05001151 int largepage, gfn_t gfn, pfn_t pfn,
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001152 int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001153{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001154 hpa_t table_addr = vcpu->arch.mmu.root_hpa;
Avi Kivitye8332402007-12-09 18:43:00 +02001155 int pt_write = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001156
1157 for (; ; level--) {
1158 u32 index = PT64_INDEX(v, level);
1159 u64 *table;
1160
1161 ASSERT(VALID_PAGE(table_addr));
1162 table = __va(table_addr);
1163
1164 if (level == 1) {
Avi Kivitye8332402007-12-09 18:43:00 +02001165 mmu_set_spte(vcpu, &table[index], ACC_ALL, ACC_ALL,
Anthony Liguori35149e22008-04-02 14:46:56 -05001166 0, write, 1, &pt_write, 0, gfn, pfn, false);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001167 return pt_write;
1168 }
1169
1170 if (largepage && level == 2) {
1171 mmu_set_spte(vcpu, &table[index], ACC_ALL, ACC_ALL,
Anthony Liguori35149e22008-04-02 14:46:56 -05001172 0, write, 1, &pt_write, 1, gfn, pfn, false);
Avi Kivityd196e342008-01-24 11:44:11 +02001173 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001174 }
1175
Avi Kivityc7addb92007-09-16 18:58:32 +02001176 if (table[index] == shadow_trap_nonpresent_pte) {
Avi Kivity25c0de22007-01-05 16:36:42 -08001177 struct kvm_mmu_page *new_table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001178 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001179
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001180 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
1181 >> PAGE_SHIFT;
1182 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
1183 v, level - 1,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001184 1, ACC_ALL, &table[index]);
Avi Kivity25c0de22007-01-05 16:36:42 -08001185 if (!new_table) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001186 pgprintk("nonpaging_map: ENOMEM\n");
Anthony Liguori35149e22008-04-02 14:46:56 -05001187 kvm_release_pfn_clean(pfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001188 return -ENOMEM;
1189 }
1190
Avi Kivity47ad8e62007-05-06 15:50:58 +03001191 table[index] = __pa(new_table->spt) | PT_PRESENT_MASK
Avi Kivity25c0de22007-01-05 16:36:42 -08001192 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001193 }
1194 table_addr = table[index] & PT64_BASE_ADDR_MASK;
1195 }
1196}
1197
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001198static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1199{
1200 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001201 int largepage = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05001202 pfn_t pfn;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001203
1204 down_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001205 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1206 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1207 largepage = 1;
1208 }
1209
Anthony Liguori35149e22008-04-02 14:46:56 -05001210 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001211 up_read(&current->mm->mmap_sem);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001212
Avi Kivityd196e342008-01-24 11:44:11 +02001213 /* mmio */
Anthony Liguori35149e22008-04-02 14:46:56 -05001214 if (is_error_pfn(pfn)) {
1215 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02001216 return 1;
1217 }
1218
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001219 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02001220 kvm_mmu_free_some_pages(vcpu);
Anthony Liguori35149e22008-04-02 14:46:56 -05001221 r = __direct_map(vcpu, v, write, largepage, gfn, pfn,
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001222 PT32E_ROOT_LEVEL);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001223 spin_unlock(&vcpu->kvm->mmu_lock);
1224
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001225
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001226 return r;
1227}
1228
1229
Avi Kivityc7addb92007-09-16 18:58:32 +02001230static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1231 struct kvm_mmu_page *sp)
1232{
1233 int i;
1234
1235 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1236 sp->spt[i] = shadow_trap_nonpresent_pte;
1237}
1238
Avi Kivity17ac10a2007-01-05 16:36:40 -08001239static void mmu_free_roots(struct kvm_vcpu *vcpu)
1240{
1241 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02001242 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001243
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001244 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03001245 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001246 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001247#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001248 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1249 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001250
Avi Kivity4db35312007-11-21 15:28:32 +02001251 sp = page_header(root);
1252 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001253 if (!sp->root_count && sp->role.invalid)
1254 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001255 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001256 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001257 return;
1258 }
1259#endif
1260 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001261 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001262
Avi Kivity417726a2007-04-12 17:35:58 +03001263 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03001264 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001265 sp = page_header(root);
1266 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001267 if (!sp->root_count && sp->role.invalid)
1268 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03001269 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001270 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001271 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001272 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001273 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001274}
1275
1276static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
1277{
1278 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001279 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02001280 struct kvm_mmu_page *sp;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001281 int metaphysical = 0;
Avi Kivity3bb65a22007-01-05 16:36:51 -08001282
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001283 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001284
1285#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001286 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1287 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001288
1289 ASSERT(!VALID_PAGE(root));
Joerg Roedelfb72d162008-02-07 13:47:44 +01001290 if (tdp_enabled)
1291 metaphysical = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001292 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001293 PT64_ROOT_LEVEL, metaphysical,
1294 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001295 root = __pa(sp->spt);
1296 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001297 vcpu->arch.mmu.root_hpa = root;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001298 return;
1299 }
1300#endif
Joerg Roedelfb72d162008-02-07 13:47:44 +01001301 metaphysical = !is_paging(vcpu);
1302 if (tdp_enabled)
1303 metaphysical = 1;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001304 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001305 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001306
1307 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001308 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
1309 if (!is_present_pte(vcpu->arch.pdptrs[i])) {
1310 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03001311 continue;
1312 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001313 root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT;
1314 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001315 root_gfn = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02001316 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001317 PT32_ROOT_LEVEL, metaphysical,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001318 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001319 root = __pa(sp->spt);
1320 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001321 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001322 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001323 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001324}
1325
Avi Kivity6aa8b732006-12-10 02:21:36 -08001326static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
1327{
1328 return vaddr;
1329}
1330
1331static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02001332 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001333{
Avi Kivitye8332402007-12-09 18:43:00 +02001334 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08001335 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001336
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001337 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08001338 r = mmu_topup_memory_caches(vcpu);
1339 if (r)
1340 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08001341
Avi Kivity6aa8b732006-12-10 02:21:36 -08001342 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001343 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001344
Avi Kivitye8332402007-12-09 18:43:00 +02001345 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001346
Avi Kivitye8332402007-12-09 18:43:00 +02001347 return nonpaging_map(vcpu, gva & PAGE_MASK,
1348 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001349}
1350
Joerg Roedelfb72d162008-02-07 13:47:44 +01001351static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
1352 u32 error_code)
1353{
Anthony Liguori35149e22008-04-02 14:46:56 -05001354 pfn_t pfn;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001355 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001356 int largepage = 0;
1357 gfn_t gfn = gpa >> PAGE_SHIFT;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001358
1359 ASSERT(vcpu);
1360 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
1361
1362 r = mmu_topup_memory_caches(vcpu);
1363 if (r)
1364 return r;
1365
1366 down_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001367 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1368 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1369 largepage = 1;
1370 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001371 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosatti3200f402008-03-29 20:17:59 -03001372 up_read(&current->mm->mmap_sem);
Anthony Liguori35149e22008-04-02 14:46:56 -05001373 if (is_error_pfn(pfn)) {
1374 kvm_release_pfn_clean(pfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001375 return 1;
1376 }
1377 spin_lock(&vcpu->kvm->mmu_lock);
1378 kvm_mmu_free_some_pages(vcpu);
1379 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Anthony Liguori35149e22008-04-02 14:46:56 -05001380 largepage, gfn, pfn, TDP_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001381 spin_unlock(&vcpu->kvm->mmu_lock);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001382
1383 return r;
1384}
1385
Avi Kivity6aa8b732006-12-10 02:21:36 -08001386static void nonpaging_free(struct kvm_vcpu *vcpu)
1387{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001388 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001389}
1390
1391static int nonpaging_init_context(struct kvm_vcpu *vcpu)
1392{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001393 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001394
1395 context->new_cr3 = nonpaging_new_cr3;
1396 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001397 context->gva_to_gpa = nonpaging_gva_to_gpa;
1398 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001399 context->prefetch_page = nonpaging_prefetch_page;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001400 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001401 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001402 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001403 return 0;
1404}
1405
Avi Kivityd835dfe2007-11-21 02:57:59 +02001406void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001407{
Avi Kivity1165f5f2007-04-19 17:27:43 +03001408 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001409 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001410}
1411
1412static void paging_new_cr3(struct kvm_vcpu *vcpu)
1413{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001414 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001415 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001416}
1417
Avi Kivity6aa8b732006-12-10 02:21:36 -08001418static void inject_page_fault(struct kvm_vcpu *vcpu,
1419 u64 addr,
1420 u32 err_code)
1421{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02001422 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001423}
1424
Avi Kivity6aa8b732006-12-10 02:21:36 -08001425static void paging_free(struct kvm_vcpu *vcpu)
1426{
1427 nonpaging_free(vcpu);
1428}
1429
1430#define PTTYPE 64
1431#include "paging_tmpl.h"
1432#undef PTTYPE
1433
1434#define PTTYPE 32
1435#include "paging_tmpl.h"
1436#undef PTTYPE
1437
Avi Kivity17ac10a2007-01-05 16:36:40 -08001438static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001439{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001440 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001441
1442 ASSERT(is_pae(vcpu));
1443 context->new_cr3 = paging_new_cr3;
1444 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001445 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02001446 context->prefetch_page = paging64_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001447 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001448 context->root_level = level;
1449 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001450 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001451 return 0;
1452}
1453
Avi Kivity17ac10a2007-01-05 16:36:40 -08001454static int paging64_init_context(struct kvm_vcpu *vcpu)
1455{
1456 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1457}
1458
Avi Kivity6aa8b732006-12-10 02:21:36 -08001459static int paging32_init_context(struct kvm_vcpu *vcpu)
1460{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001461 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001462
1463 context->new_cr3 = paging_new_cr3;
1464 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001465 context->gva_to_gpa = paging32_gva_to_gpa;
1466 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001467 context->prefetch_page = paging32_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001468 context->root_level = PT32_ROOT_LEVEL;
1469 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001470 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001471 return 0;
1472}
1473
1474static int paging32E_init_context(struct kvm_vcpu *vcpu)
1475{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001476 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001477}
1478
Joerg Roedelfb72d162008-02-07 13:47:44 +01001479static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
1480{
1481 struct kvm_mmu *context = &vcpu->arch.mmu;
1482
1483 context->new_cr3 = nonpaging_new_cr3;
1484 context->page_fault = tdp_page_fault;
1485 context->free = nonpaging_free;
1486 context->prefetch_page = nonpaging_prefetch_page;
1487 context->shadow_root_level = TDP_ROOT_LEVEL;
1488 context->root_hpa = INVALID_PAGE;
1489
1490 if (!is_paging(vcpu)) {
1491 context->gva_to_gpa = nonpaging_gva_to_gpa;
1492 context->root_level = 0;
1493 } else if (is_long_mode(vcpu)) {
1494 context->gva_to_gpa = paging64_gva_to_gpa;
1495 context->root_level = PT64_ROOT_LEVEL;
1496 } else if (is_pae(vcpu)) {
1497 context->gva_to_gpa = paging64_gva_to_gpa;
1498 context->root_level = PT32E_ROOT_LEVEL;
1499 } else {
1500 context->gva_to_gpa = paging32_gva_to_gpa;
1501 context->root_level = PT32_ROOT_LEVEL;
1502 }
1503
1504 return 0;
1505}
1506
1507static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001508{
1509 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001510 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001511
1512 if (!is_paging(vcpu))
1513 return nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08001514 else if (is_long_mode(vcpu))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001515 return paging64_init_context(vcpu);
1516 else if (is_pae(vcpu))
1517 return paging32E_init_context(vcpu);
1518 else
1519 return paging32_init_context(vcpu);
1520}
1521
Joerg Roedelfb72d162008-02-07 13:47:44 +01001522static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1523{
Anthony Liguori35149e22008-04-02 14:46:56 -05001524 vcpu->arch.update_pte.pfn = bad_pfn;
1525
Joerg Roedelfb72d162008-02-07 13:47:44 +01001526 if (tdp_enabled)
1527 return init_kvm_tdp_mmu(vcpu);
1528 else
1529 return init_kvm_softmmu(vcpu);
1530}
1531
Avi Kivity6aa8b732006-12-10 02:21:36 -08001532static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1533{
1534 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001535 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
1536 vcpu->arch.mmu.free(vcpu);
1537 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001538 }
1539}
1540
1541int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1542{
Avi Kivity17c3ba92007-06-04 15:58:30 +03001543 destroy_kvm_mmu(vcpu);
1544 return init_kvm_mmu(vcpu);
1545}
Eddie Dong8668a3c2007-10-10 14:26:45 +08001546EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001547
1548int kvm_mmu_load(struct kvm_vcpu *vcpu)
1549{
Avi Kivity714b93d2007-01-05 16:36:53 -08001550 int r;
1551
Avi Kivitye2dec932007-01-05 16:36:54 -08001552 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001553 if (r)
1554 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001555 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02001556 kvm_mmu_free_some_pages(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001557 mmu_alloc_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001558 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001559 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001560 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001561out:
1562 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001563}
Avi Kivity17c3ba92007-06-04 15:58:30 +03001564EXPORT_SYMBOL_GPL(kvm_mmu_load);
1565
1566void kvm_mmu_unload(struct kvm_vcpu *vcpu)
1567{
1568 mmu_free_roots(vcpu);
1569}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001570
Avi Kivity09072da2007-05-01 14:16:52 +03001571static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02001572 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02001573 u64 *spte)
1574{
1575 u64 pte;
1576 struct kvm_mmu_page *child;
1577
1578 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02001579 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001580 if (sp->role.level == PT_PAGE_TABLE_LEVEL ||
1581 is_large_pte(pte))
Izik Eidus290fc382007-09-27 14:11:22 +02001582 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001583 else {
1584 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03001585 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001586 }
1587 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001588 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001589 if (is_large_pte(pte))
1590 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02001591}
1592
Avi Kivity00284252007-05-01 16:53:31 +03001593static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02001594 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03001595 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02001596 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03001597{
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001598 if ((sp->role.level != PT_PAGE_TABLE_LEVEL)
1599 && !vcpu->arch.update_pte.largepage) {
Avi Kivity4cee5762007-11-18 16:37:07 +02001600 ++vcpu->kvm->stat.mmu_pde_zapped;
Avi Kivity00284252007-05-01 16:53:31 +03001601 return;
Avi Kivity4cee5762007-11-18 16:37:07 +02001602 }
Avi Kivity00284252007-05-01 16:53:31 +03001603
Avi Kivity4cee5762007-11-18 16:37:07 +02001604 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02001605 if (sp->role.glevels == PT32_ROOT_LEVEL)
Dong, Eddie489f1d62008-01-07 11:14:20 +02001606 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03001607 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02001608 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03001609}
1610
Avi Kivity79539ce2007-11-21 02:06:21 +02001611static bool need_remote_flush(u64 old, u64 new)
1612{
1613 if (!is_shadow_present_pte(old))
1614 return false;
1615 if (!is_shadow_present_pte(new))
1616 return true;
1617 if ((old ^ new) & PT64_BASE_ADDR_MASK)
1618 return true;
1619 old ^= PT64_NX_MASK;
1620 new ^= PT64_NX_MASK;
1621 return (old & ~new & PT64_PERM_MASK) != 0;
1622}
1623
1624static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
1625{
1626 if (need_remote_flush(old, new))
1627 kvm_flush_remote_tlbs(vcpu->kvm);
1628 else
1629 kvm_mmu_flush_tlb(vcpu);
1630}
1631
Avi Kivity12b7d282007-09-23 14:10:49 +02001632static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
1633{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001634 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02001635
1636 return !!(spte && (*spte & PT_ACCESSED_MASK));
1637}
1638
Avi Kivityd7824ff2007-12-30 12:29:05 +02001639static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1640 const u8 *new, int bytes)
1641{
1642 gfn_t gfn;
1643 int r;
1644 u64 gpte = 0;
Anthony Liguori35149e22008-04-02 14:46:56 -05001645 pfn_t pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02001646
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001647 vcpu->arch.update_pte.largepage = 0;
1648
Avi Kivityd7824ff2007-12-30 12:29:05 +02001649 if (bytes != 4 && bytes != 8)
1650 return;
1651
1652 /*
1653 * Assume that the pte write on a page table of the same type
1654 * as the current vcpu paging mode. This is nearly always true
1655 * (might be false while changing modes). Note it is verified later
1656 * by update_pte().
1657 */
1658 if (is_pae(vcpu)) {
1659 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
1660 if ((bytes == 4) && (gpa % 4 == 0)) {
1661 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
1662 if (r)
1663 return;
1664 memcpy((void *)&gpte + (gpa % 8), new, 4);
1665 } else if ((bytes == 8) && (gpa % 8 == 0)) {
1666 memcpy((void *)&gpte, new, 8);
1667 }
1668 } else {
1669 if ((bytes == 4) && (gpa % 4 == 0))
1670 memcpy((void *)&gpte, new, 4);
1671 }
1672 if (!is_present_pte(gpte))
1673 return;
1674 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001675
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001676 down_read(&current->mm->mmap_sem);
1677 if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) {
1678 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1679 vcpu->arch.update_pte.largepage = 1;
1680 }
Anthony Liguori35149e22008-04-02 14:46:56 -05001681 pfn = gfn_to_pfn(vcpu->kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001682 up_read(&current->mm->mmap_sem);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001683
Anthony Liguori35149e22008-04-02 14:46:56 -05001684 if (is_error_pfn(pfn)) {
1685 kvm_release_pfn_clean(pfn);
Avi Kivityd196e342008-01-24 11:44:11 +02001686 return;
1687 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02001688 vcpu->arch.update_pte.gfn = gfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001689 vcpu->arch.update_pte.pfn = pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02001690}
1691
Avi Kivity09072da2007-05-01 14:16:52 +03001692void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Shaohua Life5518812007-07-23 14:51:39 +08001693 const u8 *new, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001694{
Avi Kivity9b7a0322007-01-05 16:36:45 -08001695 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02001696 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001697 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001698 struct hlist_head *bucket;
1699 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02001700 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001701 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001702 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001703 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001704 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001705 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03001706 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001707 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001708 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02001709 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02001710 int r;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001711
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001712 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02001713 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001714 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02001715 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02001716 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02001717 kvm_mmu_audit(vcpu, "pre pte write");
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001718 if (gfn == vcpu->arch.last_pt_write_gfn
Avi Kivity12b7d282007-09-23 14:10:49 +02001719 && !last_updated_pte_accessed(vcpu)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001720 ++vcpu->arch.last_pt_write_count;
1721 if (vcpu->arch.last_pt_write_count >= 3)
Avi Kivity86a5ba02007-01-05 16:36:50 -08001722 flooded = 1;
1723 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001724 vcpu->arch.last_pt_write_gfn = gfn;
1725 vcpu->arch.last_pt_write_count = 1;
1726 vcpu->arch.last_pte_updated = NULL;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001727 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001728 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001729 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001730 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
1731 if (sp->gfn != gfn || sp->role.metaphysical)
Avi Kivity9b7a0322007-01-05 16:36:45 -08001732 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02001733 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001734 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03001735 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001736 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001737 /*
1738 * Misaligned accesses are too much trouble to fix
1739 * up; also, they usually indicate a page is not used
1740 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08001741 *
1742 * If we're seeing too many writes to a page,
1743 * it may no longer be a page table, or we may be
1744 * forking, in which case it is better to unmap the
1745 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001746 */
1747 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02001748 gpa, bytes, sp->role.word);
1749 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02001750 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001751 continue;
1752 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001753 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02001754 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02001755 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001756 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001757 page_offset <<= 1; /* 32->64 */
1758 /*
1759 * A 32-bit pde maps 4MB while the shadow pdes map
1760 * only 2MB. So we need to double the offset again
1761 * and zap two pdes instead of one.
1762 */
1763 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03001764 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02001765 page_offset <<= 1;
1766 npte = 2;
1767 }
Avi Kivityfce06572007-05-01 16:44:05 +03001768 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001769 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001770 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03001771 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001772 }
Avi Kivity4db35312007-11-21 15:28:32 +02001773 spte = &sp->spt[page_offset / sizeof(*spte)];
Dong, Eddie489f1d62008-01-07 11:14:20 +02001774 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
1775 gentry = 0;
1776 r = kvm_read_guest_atomic(vcpu->kvm,
1777 gpa & ~(u64)(pte_size - 1),
1778 &gentry, pte_size);
1779 new = (const void *)&gentry;
1780 if (r < 0)
1781 new = NULL;
1782 }
Avi Kivityac1b7142007-03-08 17:13:32 +02001783 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02001784 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02001785 mmu_pte_write_zap_pte(vcpu, sp, spte);
Dong, Eddie489f1d62008-01-07 11:14:20 +02001786 if (new)
1787 mmu_pte_write_new_pte(vcpu, sp, spte, new);
Avi Kivity79539ce2007-11-21 02:06:21 +02001788 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001789 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001790 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001791 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001792 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001793 spin_unlock(&vcpu->kvm->mmu_lock);
Anthony Liguori35149e22008-04-02 14:46:56 -05001794 if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
1795 kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
1796 vcpu->arch.update_pte.pfn = bad_pfn;
Avi Kivityd7824ff2007-12-30 12:29:05 +02001797 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08001798}
1799
Avi Kivitya4360362007-01-05 16:36:45 -08001800int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1801{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001802 gpa_t gpa;
1803 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08001804
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001805 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001806
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001807 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001808 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001809 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001810 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08001811}
1812
Avi Kivity22d95b12007-09-14 20:26:06 +03001813void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08001814{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001815 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
Avi Kivity4db35312007-11-21 15:28:32 +02001816 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08001817
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001818 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02001819 struct kvm_mmu_page, link);
1820 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02001821 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08001822 }
1823}
Avi Kivityebeace82007-01-05 16:36:47 -08001824
Avi Kivity30677142007-10-28 18:48:59 +02001825int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
1826{
1827 int r;
1828 enum emulation_result er;
1829
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001830 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02001831 if (r < 0)
1832 goto out;
1833
1834 if (!r) {
1835 r = 1;
1836 goto out;
1837 }
1838
Avi Kivityb733bfb2007-10-28 18:52:05 +02001839 r = mmu_topup_memory_caches(vcpu);
1840 if (r)
1841 goto out;
1842
Avi Kivity30677142007-10-28 18:48:59 +02001843 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02001844
1845 switch (er) {
1846 case EMULATE_DONE:
1847 return 1;
1848 case EMULATE_DO_MMIO:
1849 ++vcpu->stat.mmio_exits;
1850 return 0;
1851 case EMULATE_FAIL:
1852 kvm_report_emulation_failure(vcpu, "pagetable");
1853 return 1;
1854 default:
1855 BUG();
1856 }
1857out:
Avi Kivity30677142007-10-28 18:48:59 +02001858 return r;
1859}
1860EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
1861
Joerg Roedel18552672008-02-07 13:47:41 +01001862void kvm_enable_tdp(void)
1863{
1864 tdp_enabled = true;
1865}
1866EXPORT_SYMBOL_GPL(kvm_enable_tdp);
1867
Avi Kivity6aa8b732006-12-10 02:21:36 -08001868static void free_mmu_pages(struct kvm_vcpu *vcpu)
1869{
Avi Kivity4db35312007-11-21 15:28:32 +02001870 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001871
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001872 while (!list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
1873 sp = container_of(vcpu->kvm->arch.active_mmu_pages.next,
Avi Kivity4db35312007-11-21 15:28:32 +02001874 struct kvm_mmu_page, link);
1875 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivityf51234c2007-01-05 16:36:52 -08001876 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001877 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001878}
1879
1880static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
1881{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001882 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001883 int i;
1884
1885 ASSERT(vcpu);
1886
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001887 if (vcpu->kvm->arch.n_requested_mmu_pages)
1888 vcpu->kvm->arch.n_free_mmu_pages =
1889 vcpu->kvm->arch.n_requested_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001890 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001891 vcpu->kvm->arch.n_free_mmu_pages =
1892 vcpu->kvm->arch.n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001893 /*
1894 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1895 * Therefore we need to allocate shadow page tables in the first
1896 * 4GB of memory, which happens to fit the DMA32 zone.
1897 */
1898 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
1899 if (!page)
1900 goto error_1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001901 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001902 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001903 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001904
Avi Kivity6aa8b732006-12-10 02:21:36 -08001905 return 0;
1906
1907error_1:
1908 free_mmu_pages(vcpu);
1909 return -ENOMEM;
1910}
1911
Ingo Molnar8018c272006-12-29 16:50:01 -08001912int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001913{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001914 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001915 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001916
Ingo Molnar8018c272006-12-29 16:50:01 -08001917 return alloc_mmu_pages(vcpu);
1918}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001919
Ingo Molnar8018c272006-12-29 16:50:01 -08001920int kvm_mmu_setup(struct kvm_vcpu *vcpu)
1921{
1922 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001923 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08001924
Ingo Molnar8018c272006-12-29 16:50:01 -08001925 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001926}
1927
1928void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
1929{
1930 ASSERT(vcpu);
1931
1932 destroy_kvm_mmu(vcpu);
1933 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001934 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001935}
1936
Avi Kivity90cb0522007-07-17 13:04:56 +03001937void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001938{
Avi Kivity4db35312007-11-21 15:28:32 +02001939 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001940
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001941 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001942 int i;
1943 u64 *pt;
1944
Avi Kivity4db35312007-11-21 15:28:32 +02001945 if (!test_bit(slot, &sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001946 continue;
1947
Avi Kivity4db35312007-11-21 15:28:32 +02001948 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001949 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1950 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02001951 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001952 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001953 }
1954}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001955
Avi Kivity90cb0522007-07-17 13:04:56 +03001956void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03001957{
Avi Kivity4db35312007-11-21 15:28:32 +02001958 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03001959
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001960 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001961 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Avi Kivity4db35312007-11-21 15:28:32 +02001962 kvm_mmu_zap_page(kvm, sp);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001963 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03001964
Avi Kivity90cb0522007-07-17 13:04:56 +03001965 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03001966}
1967
Izik Eidus3ee16c82008-03-30 15:17:21 +03001968void kvm_mmu_remove_one_alloc_mmu_page(struct kvm *kvm)
1969{
1970 struct kvm_mmu_page *page;
1971
1972 page = container_of(kvm->arch.active_mmu_pages.prev,
1973 struct kvm_mmu_page, link);
1974 kvm_mmu_zap_page(kvm, page);
1975}
1976
1977static int mmu_shrink(int nr_to_scan, gfp_t gfp_mask)
1978{
1979 struct kvm *kvm;
1980 struct kvm *kvm_freed = NULL;
1981 int cache_count = 0;
1982
1983 spin_lock(&kvm_lock);
1984
1985 list_for_each_entry(kvm, &vm_list, vm_list) {
1986 int npages;
1987
1988 spin_lock(&kvm->mmu_lock);
1989 npages = kvm->arch.n_alloc_mmu_pages -
1990 kvm->arch.n_free_mmu_pages;
1991 cache_count += npages;
1992 if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
1993 kvm_mmu_remove_one_alloc_mmu_page(kvm);
1994 cache_count--;
1995 kvm_freed = kvm;
1996 }
1997 nr_to_scan--;
1998
1999 spin_unlock(&kvm->mmu_lock);
2000 }
2001 if (kvm_freed)
2002 list_move_tail(&kvm_freed->vm_list, &vm_list);
2003
2004 spin_unlock(&kvm_lock);
2005
2006 return cache_count;
2007}
2008
2009static struct shrinker mmu_shrinker = {
2010 .shrink = mmu_shrink,
2011 .seeks = DEFAULT_SEEKS * 10,
2012};
2013
2014void mmu_destroy_caches(void)
Avi Kivityb5a33a72007-04-15 16:31:09 +03002015{
2016 if (pte_chain_cache)
2017 kmem_cache_destroy(pte_chain_cache);
2018 if (rmap_desc_cache)
2019 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002020 if (mmu_page_header_cache)
2021 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002022}
2023
Izik Eidus3ee16c82008-03-30 15:17:21 +03002024void kvm_mmu_module_exit(void)
2025{
2026 mmu_destroy_caches();
2027 unregister_shrinker(&mmu_shrinker);
2028}
2029
Avi Kivityb5a33a72007-04-15 16:31:09 +03002030int kvm_mmu_module_init(void)
2031{
2032 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
2033 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09002034 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002035 if (!pte_chain_cache)
2036 goto nomem;
2037 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
2038 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09002039 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03002040 if (!rmap_desc_cache)
2041 goto nomem;
2042
Avi Kivityd3d25b02007-05-30 12:34:53 +03002043 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
2044 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09002045 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03002046 if (!mmu_page_header_cache)
2047 goto nomem;
2048
Izik Eidus3ee16c82008-03-30 15:17:21 +03002049 register_shrinker(&mmu_shrinker);
2050
Avi Kivityb5a33a72007-04-15 16:31:09 +03002051 return 0;
2052
2053nomem:
Izik Eidus3ee16c82008-03-30 15:17:21 +03002054 mmu_destroy_caches();
Avi Kivityb5a33a72007-04-15 16:31:09 +03002055 return -ENOMEM;
2056}
2057
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08002058/*
2059 * Caculate mmu pages needed for kvm.
2060 */
2061unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
2062{
2063 int i;
2064 unsigned int nr_mmu_pages;
2065 unsigned int nr_pages = 0;
2066
2067 for (i = 0; i < kvm->nmemslots; i++)
2068 nr_pages += kvm->memslots[i].npages;
2069
2070 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2071 nr_mmu_pages = max(nr_mmu_pages,
2072 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2073
2074 return nr_mmu_pages;
2075}
2076
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002077static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2078 unsigned len)
2079{
2080 if (len > buffer->len)
2081 return NULL;
2082 return buffer->ptr;
2083}
2084
2085static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2086 unsigned len)
2087{
2088 void *ret;
2089
2090 ret = pv_mmu_peek_buffer(buffer, len);
2091 if (!ret)
2092 return ret;
2093 buffer->ptr += len;
2094 buffer->len -= len;
2095 buffer->processed += len;
2096 return ret;
2097}
2098
2099static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
2100 gpa_t addr, gpa_t value)
2101{
2102 int bytes = 8;
2103 int r;
2104
2105 if (!is_long_mode(vcpu) && !is_pae(vcpu))
2106 bytes = 4;
2107
2108 r = mmu_topup_memory_caches(vcpu);
2109 if (r)
2110 return r;
2111
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002112 if (!emulator_write_phys(vcpu, addr, &value, bytes))
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002113 return -EFAULT;
2114
2115 return 1;
2116}
2117
2118static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2119{
2120 kvm_x86_ops->tlb_flush(vcpu);
2121 return 1;
2122}
2123
2124static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
2125{
2126 spin_lock(&vcpu->kvm->mmu_lock);
2127 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
2128 spin_unlock(&vcpu->kvm->mmu_lock);
2129 return 1;
2130}
2131
2132static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
2133 struct kvm_pv_mmu_op_buffer *buffer)
2134{
2135 struct kvm_mmu_op_header *header;
2136
2137 header = pv_mmu_peek_buffer(buffer, sizeof *header);
2138 if (!header)
2139 return 0;
2140 switch (header->op) {
2141 case KVM_MMU_OP_WRITE_PTE: {
2142 struct kvm_mmu_op_write_pte *wpte;
2143
2144 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
2145 if (!wpte)
2146 return 0;
2147 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
2148 wpte->pte_val);
2149 }
2150 case KVM_MMU_OP_FLUSH_TLB: {
2151 struct kvm_mmu_op_flush_tlb *ftlb;
2152
2153 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
2154 if (!ftlb)
2155 return 0;
2156 return kvm_pv_mmu_flush_tlb(vcpu);
2157 }
2158 case KVM_MMU_OP_RELEASE_PT: {
2159 struct kvm_mmu_op_release_pt *rpt;
2160
2161 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
2162 if (!rpt)
2163 return 0;
2164 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
2165 }
2166 default: return 0;
2167 }
2168}
2169
2170int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
2171 gpa_t addr, unsigned long *ret)
2172{
2173 int r;
2174 struct kvm_pv_mmu_op_buffer buffer;
2175
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002176 down_read(&current->mm->mmap_sem);
2177
2178 buffer.ptr = buffer.buf;
2179 buffer.len = min_t(unsigned long, bytes, sizeof buffer.buf);
2180 buffer.processed = 0;
2181
2182 r = kvm_read_guest(vcpu->kvm, addr, buffer.buf, buffer.len);
2183 if (r)
2184 goto out;
2185
2186 while (buffer.len) {
2187 r = kvm_pv_mmu_op_one(vcpu, &buffer);
2188 if (r < 0)
2189 goto out;
2190 if (r == 0)
2191 break;
2192 }
2193
2194 r = 1;
2195out:
2196 *ret = buffer.processed;
2197 up_read(&current->mm->mmap_sem);
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002198 return r;
2199}
2200
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002201#ifdef AUDIT
2202
2203static const char *audit_msg;
2204
2205static gva_t canonicalize(gva_t gva)
2206{
2207#ifdef CONFIG_X86_64
2208 gva = (long long)(gva << 16) >> 16;
2209#endif
2210 return gva;
2211}
2212
2213static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
2214 gva_t va, int level)
2215{
2216 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
2217 int i;
2218 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
2219
2220 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
2221 u64 ent = pt[i];
2222
Avi Kivityc7addb92007-09-16 18:58:32 +02002223 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002224 continue;
2225
2226 va = canonicalize(va);
Avi Kivityc7addb92007-09-16 18:58:32 +02002227 if (level > 1) {
2228 if (ent == shadow_notrap_nonpresent_pte)
2229 printk(KERN_ERR "audit: (%s) nontrapping pte"
2230 " in nonleaf level: levels %d gva %lx"
2231 " level %d pte %llx\n", audit_msg,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002232 vcpu->arch.mmu.root_level, va, level, ent);
Avi Kivityc7addb92007-09-16 18:58:32 +02002233
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002234 audit_mappings_page(vcpu, ent, va, level - 1);
Avi Kivityc7addb92007-09-16 18:58:32 +02002235 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002236 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05002237 hpa_t hpa = (hpa_t)gpa_to_pfn(vcpu, gpa) << PAGE_SHIFT;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002238
Avi Kivityc7addb92007-09-16 18:58:32 +02002239 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002240 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02002241 printk(KERN_ERR "xx audit error: (%s) levels %d"
2242 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002243 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04002244 va, gpa, hpa, ent,
2245 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02002246 else if (ent == shadow_notrap_nonpresent_pte
2247 && !is_error_hpa(hpa))
2248 printk(KERN_ERR "audit: (%s) notrap shadow,"
2249 " valid guest gva %lx\n", audit_msg, va);
Anthony Liguori35149e22008-04-02 14:46:56 -05002250 kvm_release_pfn_clean(pfn);
Avi Kivityc7addb92007-09-16 18:58:32 +02002251
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002252 }
2253 }
2254}
2255
2256static void audit_mappings(struct kvm_vcpu *vcpu)
2257{
Avi Kivity1ea252a2007-03-08 11:48:09 +02002258 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002259
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002260 if (vcpu->arch.mmu.root_level == 4)
2261 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002262 else
2263 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002264 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002265 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002266 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002267 i << 30,
2268 2);
2269}
2270
2271static int count_rmaps(struct kvm_vcpu *vcpu)
2272{
2273 int nmaps = 0;
2274 int i, j, k;
2275
2276 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
2277 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
2278 struct kvm_rmap_desc *d;
2279
2280 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02002281 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002282
Izik Eidus290fc382007-09-27 14:11:22 +02002283 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002284 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02002285 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002286 ++nmaps;
2287 continue;
2288 }
Izik Eidus290fc382007-09-27 14:11:22 +02002289 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002290 while (d) {
2291 for (k = 0; k < RMAP_EXT; ++k)
2292 if (d->shadow_ptes[k])
2293 ++nmaps;
2294 else
2295 break;
2296 d = d->more;
2297 }
2298 }
2299 }
2300 return nmaps;
2301}
2302
2303static int count_writable_mappings(struct kvm_vcpu *vcpu)
2304{
2305 int nmaps = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02002306 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002307 int i;
2308
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002309 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02002310 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002311
Avi Kivity4db35312007-11-21 15:28:32 +02002312 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002313 continue;
2314
2315 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
2316 u64 ent = pt[i];
2317
2318 if (!(ent & PT_PRESENT_MASK))
2319 continue;
2320 if (!(ent & PT_WRITABLE_MASK))
2321 continue;
2322 ++nmaps;
2323 }
2324 }
2325 return nmaps;
2326}
2327
2328static void audit_rmap(struct kvm_vcpu *vcpu)
2329{
2330 int n_rmap = count_rmaps(vcpu);
2331 int n_actual = count_writable_mappings(vcpu);
2332
2333 if (n_rmap != n_actual)
2334 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002335 __func__, audit_msg, n_rmap, n_actual);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002336}
2337
2338static void audit_write_protection(struct kvm_vcpu *vcpu)
2339{
Avi Kivity4db35312007-11-21 15:28:32 +02002340 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02002341 struct kvm_memory_slot *slot;
2342 unsigned long *rmapp;
2343 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002344
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002345 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02002346 if (sp->role.metaphysical)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002347 continue;
2348
Avi Kivity4db35312007-11-21 15:28:32 +02002349 slot = gfn_to_memslot(vcpu->kvm, sp->gfn);
2350 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02002351 rmapp = &slot->rmap[gfn - slot->base_gfn];
2352 if (*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002353 printk(KERN_ERR "%s: (%s) shadow page has writable"
2354 " mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002355 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02002356 sp->role.word);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002357 }
2358}
2359
2360static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
2361{
2362 int olddbg = dbg;
2363
2364 dbg = 0;
2365 audit_msg = msg;
2366 audit_rmap(vcpu);
2367 audit_write_protection(vcpu);
2368 audit_mappings(vcpu);
2369 dbg = olddbg;
2370}
2371
2372#endif