blob: 072e9422c9145ab682296b0a239002211e2fc399 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * MMU support
8 *
9 * Copyright (C) 2006 Qumranet, Inc.
10 *
11 * Authors:
12 * Yaniv Kamay <yaniv@qumranet.com>
13 * Avi Kivity <avi@qumranet.com>
14 *
15 * This work is licensed under the terms of the GNU GPL, version 2. See
16 * the COPYING file in the top-level directory.
17 *
18 */
Avi Kivity6aa8b732006-12-10 02:21:36 -080019
20#include "vmx.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080021#include "mmu.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080022
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Avi Kivitye4956062007-06-28 14:15:57 -040024#include <linux/types.h>
25#include <linux/string.h>
26#include <linux/mm.h>
27#include <linux/highmem.h>
28#include <linux/module.h>
Izik Eidus448353c2007-11-26 14:08:14 +020029#include <linux/swap.h>
Marcelo Tosatti05da4552008-02-23 11:44:30 -030030#include <linux/hugetlb.h>
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050031#include <linux/compiler.h>
Avi Kivitye4956062007-06-28 14:15:57 -040032
33#include <asm/page.h>
34#include <asm/cmpxchg.h>
Avi Kivity4e542372007-11-21 14:08:40 +020035#include <asm/io.h>
Avi Kivitye4956062007-06-28 14:15:57 -040036
Joerg Roedel18552672008-02-07 13:47:41 +010037/*
38 * When setting this variable to true it enables Two-Dimensional-Paging
39 * where the hardware walks 2 page tables:
40 * 1. the guest-virtual to guest-physical
41 * 2. while doing 1. it walks guest-physical to host-physical
42 * If the hardware supports that we don't need to do shadow paging.
43 */
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050044bool tdp_enabled = false;
Joerg Roedel18552672008-02-07 13:47:41 +010045
Avi Kivity37a7d8b2007-01-05 16:36:56 -080046#undef MMU_DEBUG
47
48#undef AUDIT
49
50#ifdef AUDIT
51static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
52#else
53static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
54#endif
55
56#ifdef MMU_DEBUG
57
58#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
59#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
60
61#else
62
63#define pgprintk(x...) do { } while (0)
64#define rmap_printk(x...) do { } while (0)
65
66#endif
67
68#if defined(MMU_DEBUG) || defined(AUDIT)
69static int dbg = 1;
70#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080071
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080072#ifndef MMU_DEBUG
73#define ASSERT(x) do { } while (0)
74#else
Avi Kivity6aa8b732006-12-10 02:21:36 -080075#define ASSERT(x) \
76 if (!(x)) { \
77 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
78 __FILE__, __LINE__, #x); \
79 }
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080080#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080081
Avi Kivitycea0f0e2007-01-05 16:36:43 -080082#define PT64_PT_BITS 9
83#define PT64_ENT_PER_PAGE (1 << PT64_PT_BITS)
84#define PT32_PT_BITS 10
85#define PT32_ENT_PER_PAGE (1 << PT32_PT_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -080086
87#define PT_WRITABLE_SHIFT 1
88
89#define PT_PRESENT_MASK (1ULL << 0)
90#define PT_WRITABLE_MASK (1ULL << PT_WRITABLE_SHIFT)
91#define PT_USER_MASK (1ULL << 2)
92#define PT_PWT_MASK (1ULL << 3)
93#define PT_PCD_MASK (1ULL << 4)
94#define PT_ACCESSED_MASK (1ULL << 5)
95#define PT_DIRTY_MASK (1ULL << 6)
96#define PT_PAGE_SIZE_MASK (1ULL << 7)
97#define PT_PAT_MASK (1ULL << 7)
98#define PT_GLOBAL_MASK (1ULL << 8)
Avi Kivityfe135d22007-12-09 16:15:46 +020099#define PT64_NX_SHIFT 63
100#define PT64_NX_MASK (1ULL << PT64_NX_SHIFT)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800101
102#define PT_PAT_SHIFT 7
103#define PT_DIR_PAT_SHIFT 12
104#define PT_DIR_PAT_MASK (1ULL << PT_DIR_PAT_SHIFT)
105
106#define PT32_DIR_PSE36_SIZE 4
107#define PT32_DIR_PSE36_SHIFT 13
Mike Dayd77c26f2007-10-08 09:02:08 -0400108#define PT32_DIR_PSE36_MASK \
109 (((1ULL << PT32_DIR_PSE36_SIZE) - 1) << PT32_DIR_PSE36_SHIFT)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800110
111
Avi Kivity6aa8b732006-12-10 02:21:36 -0800112#define PT_FIRST_AVAIL_BITS_SHIFT 9
113#define PT64_SECOND_AVAIL_BITS_SHIFT 52
114
Avi Kivity6aa8b732006-12-10 02:21:36 -0800115#define VALID_PAGE(x) ((x) != INVALID_PAGE)
116
117#define PT64_LEVEL_BITS 9
118
119#define PT64_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400120 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800121
122#define PT64_LEVEL_MASK(level) \
123 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
124
125#define PT64_INDEX(address, level)\
126 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
127
128
129#define PT32_LEVEL_BITS 10
130
131#define PT32_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400132 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800133
134#define PT32_LEVEL_MASK(level) \
135 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
136
137#define PT32_INDEX(address, level)\
138 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
139
140
Avi Kivity27aba762007-03-09 13:04:31 +0200141#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800142#define PT64_DIR_BASE_ADDR_MASK \
143 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
144
145#define PT32_BASE_ADDR_MASK PAGE_MASK
146#define PT32_DIR_BASE_ADDR_MASK \
147 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
148
Avi Kivity79539ce2007-11-21 02:06:21 +0200149#define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
150 | PT64_NX_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800151
152#define PFERR_PRESENT_MASK (1U << 0)
153#define PFERR_WRITE_MASK (1U << 1)
154#define PFERR_USER_MASK (1U << 2)
Avi Kivity73b10872007-01-26 00:56:41 -0800155#define PFERR_FETCH_MASK (1U << 4)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800156
157#define PT64_ROOT_LEVEL 4
158#define PT32_ROOT_LEVEL 2
159#define PT32E_ROOT_LEVEL 3
160
161#define PT_DIRECTORY_LEVEL 2
162#define PT_PAGE_TABLE_LEVEL 1
163
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800164#define RMAP_EXT 4
165
Avi Kivityfe135d22007-12-09 16:15:46 +0200166#define ACC_EXEC_MASK 1
167#define ACC_WRITE_MASK PT_WRITABLE_MASK
168#define ACC_USER_MASK PT_USER_MASK
169#define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
170
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -0500171struct kvm_pv_mmu_op_buffer {
172 void *ptr;
173 unsigned len;
174 unsigned processed;
175 char buf[512] __aligned(sizeof(long));
176};
177
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800178struct kvm_rmap_desc {
179 u64 *shadow_ptes[RMAP_EXT];
180 struct kvm_rmap_desc *more;
181};
182
Avi Kivityb5a33a72007-04-15 16:31:09 +0300183static struct kmem_cache *pte_chain_cache;
184static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300185static struct kmem_cache *mmu_page_header_cache;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300186
Avi Kivityc7addb92007-09-16 18:58:32 +0200187static u64 __read_mostly shadow_trap_nonpresent_pte;
188static u64 __read_mostly shadow_notrap_nonpresent_pte;
189
190void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
191{
192 shadow_trap_nonpresent_pte = trap_pte;
193 shadow_notrap_nonpresent_pte = notrap_pte;
194}
195EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
196
Avi Kivity6aa8b732006-12-10 02:21:36 -0800197static int is_write_protection(struct kvm_vcpu *vcpu)
198{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800199 return vcpu->arch.cr0 & X86_CR0_WP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800200}
201
202static int is_cpuid_PSE36(void)
203{
204 return 1;
205}
206
Avi Kivity73b10872007-01-26 00:56:41 -0800207static int is_nx(struct kvm_vcpu *vcpu)
208{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800209 return vcpu->arch.shadow_efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800210}
211
Avi Kivity6aa8b732006-12-10 02:21:36 -0800212static int is_present_pte(unsigned long pte)
213{
214 return pte & PT_PRESENT_MASK;
215}
216
Avi Kivityc7addb92007-09-16 18:58:32 +0200217static int is_shadow_present_pte(u64 pte)
218{
Avi Kivityc7addb92007-09-16 18:58:32 +0200219 return pte != shadow_trap_nonpresent_pte
220 && pte != shadow_notrap_nonpresent_pte;
221}
222
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300223static int is_large_pte(u64 pte)
224{
225 return pte & PT_PAGE_SIZE_MASK;
226}
227
Avi Kivity6aa8b732006-12-10 02:21:36 -0800228static int is_writeble_pte(unsigned long pte)
229{
230 return pte & PT_WRITABLE_MASK;
231}
232
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200233static int is_dirty_pte(unsigned long pte)
234{
235 return pte & PT_DIRTY_MASK;
236}
237
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800238static int is_rmap_pte(u64 pte)
239{
Avi Kivity4b1a80f2008-03-23 12:18:19 +0200240 return is_shadow_present_pte(pte);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800241}
242
Avi Kivityda928522007-11-21 13:54:47 +0200243static gfn_t pse36_gfn_delta(u32 gpte)
244{
245 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
246
247 return (gpte & PT32_DIR_PSE36_MASK) << shift;
248}
249
Avi Kivitye663ee62007-05-31 15:46:04 +0300250static void set_shadow_pte(u64 *sptep, u64 spte)
251{
252#ifdef CONFIG_X86_64
253 set_64bit((unsigned long *)sptep, spte);
254#else
255 set_64bit((unsigned long long *)sptep, spte);
256#endif
257}
258
Avi Kivitye2dec932007-01-05 16:36:54 -0800259static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300260 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800261{
262 void *obj;
263
264 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800265 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800266 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300267 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800268 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800269 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800270 cache->objects[cache->nobjs++] = obj;
271 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800272 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800273}
274
275static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
276{
277 while (mc->nobjs)
278 kfree(mc->objects[--mc->nobjs]);
279}
280
Avi Kivityc1158e62007-07-20 08:18:27 +0300281static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300282 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300283{
284 struct page *page;
285
286 if (cache->nobjs >= min)
287 return 0;
288 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300289 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300290 if (!page)
291 return -ENOMEM;
292 set_page_private(page, 0);
293 cache->objects[cache->nobjs++] = page_address(page);
294 }
295 return 0;
296}
297
298static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
299{
300 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300301 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300302}
303
Avi Kivity8c438502007-04-16 11:53:17 +0300304static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
305{
306 int r;
307
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800308 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300309 pte_chain_cache, 4);
310 if (r)
311 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800312 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300313 rmap_desc_cache, 1);
314 if (r)
315 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800316 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300317 if (r)
318 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800319 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300320 mmu_page_header_cache, 4);
321out:
Avi Kivity8c438502007-04-16 11:53:17 +0300322 return r;
323}
324
Avi Kivity714b93d2007-01-05 16:36:53 -0800325static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
326{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800327 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
328 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
329 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
330 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800331}
332
333static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
334 size_t size)
335{
336 void *p;
337
338 BUG_ON(!mc->nobjs);
339 p = mc->objects[--mc->nobjs];
340 memset(p, 0, size);
341 return p;
342}
343
Avi Kivity714b93d2007-01-05 16:36:53 -0800344static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
345{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800346 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800347 sizeof(struct kvm_pte_chain));
348}
349
Avi Kivity90cb0522007-07-17 13:04:56 +0300350static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800351{
Avi Kivity90cb0522007-07-17 13:04:56 +0300352 kfree(pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800353}
354
355static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
356{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800357 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800358 sizeof(struct kvm_rmap_desc));
359}
360
Avi Kivity90cb0522007-07-17 13:04:56 +0300361static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800362{
Avi Kivity90cb0522007-07-17 13:04:56 +0300363 kfree(rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800364}
365
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800366/*
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300367 * Return the pointer to the largepage write count for a given
368 * gfn, handling slots that are not large page aligned.
369 */
370static int *slot_largepage_idx(gfn_t gfn, struct kvm_memory_slot *slot)
371{
372 unsigned long idx;
373
374 idx = (gfn / KVM_PAGES_PER_HPAGE) -
375 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
376 return &slot->lpage_info[idx].write_count;
377}
378
379static void account_shadowed(struct kvm *kvm, gfn_t gfn)
380{
381 int *write_count;
382
383 write_count = slot_largepage_idx(gfn, gfn_to_memslot(kvm, gfn));
384 *write_count += 1;
385 WARN_ON(*write_count > KVM_PAGES_PER_HPAGE);
386}
387
388static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
389{
390 int *write_count;
391
392 write_count = slot_largepage_idx(gfn, gfn_to_memslot(kvm, gfn));
393 *write_count -= 1;
394 WARN_ON(*write_count < 0);
395}
396
397static int has_wrprotected_page(struct kvm *kvm, gfn_t gfn)
398{
399 struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
400 int *largepage_idx;
401
402 if (slot) {
403 largepage_idx = slot_largepage_idx(gfn, slot);
404 return *largepage_idx;
405 }
406
407 return 1;
408}
409
410static int host_largepage_backed(struct kvm *kvm, gfn_t gfn)
411{
412 struct vm_area_struct *vma;
413 unsigned long addr;
414
415 addr = gfn_to_hva(kvm, gfn);
416 if (kvm_is_error_hva(addr))
417 return 0;
418
419 vma = find_vma(current->mm, addr);
420 if (vma && is_vm_hugetlb_page(vma))
421 return 1;
422
423 return 0;
424}
425
426static int is_largepage_backed(struct kvm_vcpu *vcpu, gfn_t large_gfn)
427{
428 struct kvm_memory_slot *slot;
429
430 if (has_wrprotected_page(vcpu->kvm, large_gfn))
431 return 0;
432
433 if (!host_largepage_backed(vcpu->kvm, large_gfn))
434 return 0;
435
436 slot = gfn_to_memslot(vcpu->kvm, large_gfn);
437 if (slot && slot->dirty_bitmap)
438 return 0;
439
440 return 1;
441}
442
443/*
Izik Eidus290fc382007-09-27 14:11:22 +0200444 * Take gfn and return the reverse mapping to it.
445 * Note: gfn must be unaliased before this function get called
446 */
447
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300448static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int lpage)
Izik Eidus290fc382007-09-27 14:11:22 +0200449{
450 struct kvm_memory_slot *slot;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300451 unsigned long idx;
Izik Eidus290fc382007-09-27 14:11:22 +0200452
453 slot = gfn_to_memslot(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300454 if (!lpage)
455 return &slot->rmap[gfn - slot->base_gfn];
456
457 idx = (gfn / KVM_PAGES_PER_HPAGE) -
458 (slot->base_gfn / KVM_PAGES_PER_HPAGE);
459
460 return &slot->lpage_info[idx].rmap_pde;
Izik Eidus290fc382007-09-27 14:11:22 +0200461}
462
463/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800464 * Reverse mapping data structures:
465 *
Izik Eidus290fc382007-09-27 14:11:22 +0200466 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
467 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800468 *
Izik Eidus290fc382007-09-27 14:11:22 +0200469 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
470 * containing more mappings.
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800471 */
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300472static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn, int lpage)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800473{
Avi Kivity4db35312007-11-21 15:28:32 +0200474 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800475 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200476 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800477 int i;
478
479 if (!is_rmap_pte(*spte))
480 return;
Izik Eidus290fc382007-09-27 14:11:22 +0200481 gfn = unalias_gfn(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200482 sp = page_header(__pa(spte));
483 sp->gfns[spte - sp->spt] = gfn;
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300484 rmapp = gfn_to_rmap(vcpu->kvm, gfn, lpage);
Izik Eidus290fc382007-09-27 14:11:22 +0200485 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800486 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200487 *rmapp = (unsigned long)spte;
488 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800489 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800490 desc = mmu_alloc_rmap_desc(vcpu);
Izik Eidus290fc382007-09-27 14:11:22 +0200491 desc->shadow_ptes[0] = (u64 *)*rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800492 desc->shadow_ptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200493 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800494 } else {
495 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200496 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800497 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
498 desc = desc->more;
499 if (desc->shadow_ptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800500 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800501 desc = desc->more;
502 }
503 for (i = 0; desc->shadow_ptes[i]; ++i)
504 ;
505 desc->shadow_ptes[i] = spte;
506 }
507}
508
Izik Eidus290fc382007-09-27 14:11:22 +0200509static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800510 struct kvm_rmap_desc *desc,
511 int i,
512 struct kvm_rmap_desc *prev_desc)
513{
514 int j;
515
516 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
517 ;
518 desc->shadow_ptes[i] = desc->shadow_ptes[j];
Al Viro11718b4d2007-02-09 16:39:20 +0000519 desc->shadow_ptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800520 if (j != 0)
521 return;
522 if (!prev_desc && !desc->more)
Izik Eidus290fc382007-09-27 14:11:22 +0200523 *rmapp = (unsigned long)desc->shadow_ptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800524 else
525 if (prev_desc)
526 prev_desc->more = desc->more;
527 else
Izik Eidus290fc382007-09-27 14:11:22 +0200528 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300529 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800530}
531
Izik Eidus290fc382007-09-27 14:11:22 +0200532static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800533{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800534 struct kvm_rmap_desc *desc;
535 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200536 struct kvm_mmu_page *sp;
Avi Kivity76c35c62007-11-21 15:32:41 +0200537 struct page *page;
Izik Eidus290fc382007-09-27 14:11:22 +0200538 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800539 int i;
540
541 if (!is_rmap_pte(*spte))
542 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200543 sp = page_header(__pa(spte));
Avi Kivity76c35c62007-11-21 15:32:41 +0200544 page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT);
Izik Eidus448353c2007-11-26 14:08:14 +0200545 mark_page_accessed(page);
Izik Eidusb4231d62007-11-20 11:49:33 +0200546 if (is_writeble_pte(*spte))
Avi Kivity76c35c62007-11-21 15:32:41 +0200547 kvm_release_page_dirty(page);
Izik Eidusb4231d62007-11-20 11:49:33 +0200548 else
Avi Kivity76c35c62007-11-21 15:32:41 +0200549 kvm_release_page_clean(page);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300550 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt], is_large_pte(*spte));
Izik Eidus290fc382007-09-27 14:11:22 +0200551 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800552 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
553 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200554 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800555 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200556 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800557 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
558 spte, *spte);
559 BUG();
560 }
Izik Eidus290fc382007-09-27 14:11:22 +0200561 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800562 } else {
563 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200564 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800565 prev_desc = NULL;
566 while (desc) {
567 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
568 if (desc->shadow_ptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200569 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800570 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800571 prev_desc);
572 return;
573 }
574 prev_desc = desc;
575 desc = desc->more;
576 }
577 BUG();
578 }
579}
580
Izik Eidus98348e92007-10-16 14:42:30 +0200581static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800582{
Avi Kivity374cbac2007-01-05 16:36:43 -0800583 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200584 struct kvm_rmap_desc *prev_desc;
585 u64 *prev_spte;
586 int i;
587
588 if (!*rmapp)
589 return NULL;
590 else if (!(*rmapp & 1)) {
591 if (!spte)
592 return (u64 *)*rmapp;
593 return NULL;
594 }
595 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
596 prev_desc = NULL;
597 prev_spte = NULL;
598 while (desc) {
599 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) {
600 if (prev_spte == spte)
601 return desc->shadow_ptes[i];
602 prev_spte = desc->shadow_ptes[i];
603 }
604 desc = desc->more;
605 }
606 return NULL;
607}
608
609static void rmap_write_protect(struct kvm *kvm, u64 gfn)
610{
Izik Eidus290fc382007-09-27 14:11:22 +0200611 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800612 u64 *spte;
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800613 int write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800614
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500615 gfn = unalias_gfn(kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300616 rmapp = gfn_to_rmap(kvm, gfn, 0);
Avi Kivity374cbac2007-01-05 16:36:43 -0800617
Izik Eidus98348e92007-10-16 14:42:30 +0200618 spte = rmap_next(kvm, rmapp, NULL);
619 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800620 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800621 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800622 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800623 if (is_writeble_pte(*spte)) {
Izik Eidus9647c142007-10-16 14:43:46 +0200624 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800625 write_protected = 1;
626 }
Izik Eidus9647c142007-10-16 14:43:46 +0200627 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800628 }
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300629 /* check for huge page mappings */
630 rmapp = gfn_to_rmap(kvm, gfn, 1);
631 spte = rmap_next(kvm, rmapp, NULL);
632 while (spte) {
633 BUG_ON(!spte);
634 BUG_ON(!(*spte & PT_PRESENT_MASK));
635 BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
636 pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
637 if (is_writeble_pte(*spte)) {
638 rmap_remove(kvm, spte);
639 --kvm->stat.lpages;
640 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
641 write_protected = 1;
642 }
643 spte = rmap_next(kvm, rmapp, spte);
644 }
645
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800646 if (write_protected)
647 kvm_flush_remote_tlbs(kvm);
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300648
649 account_shadowed(kvm, gfn);
Avi Kivity374cbac2007-01-05 16:36:43 -0800650}
651
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800652#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300653static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800654{
Avi Kivity139bdb22007-01-05 16:36:50 -0800655 u64 *pos;
656 u64 *end;
657
Avi Kivity47ad8e62007-05-06 15:50:58 +0300658 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivityd196e342008-01-24 11:44:11 +0200659 if (*pos != shadow_trap_nonpresent_pte) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800660 printk(KERN_ERR "%s: %p %llx\n", __func__,
Avi Kivity139bdb22007-01-05 16:36:50 -0800661 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800662 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800663 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800664 return 1;
665}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800666#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800667
Avi Kivity4db35312007-11-21 15:28:32 +0200668static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800669{
Avi Kivity4db35312007-11-21 15:28:32 +0200670 ASSERT(is_empty_shadow_page(sp->spt));
671 list_del(&sp->link);
672 __free_page(virt_to_page(sp->spt));
673 __free_page(virt_to_page(sp->gfns));
674 kfree(sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800675 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800676}
677
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800678static unsigned kvm_page_table_hashfn(gfn_t gfn)
679{
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200680 return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800681}
682
Avi Kivity25c0de22007-01-05 16:36:42 -0800683static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
684 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800685{
Avi Kivity4db35312007-11-21 15:28:32 +0200686 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800687
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800688 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
689 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
690 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200691 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800692 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Avi Kivity4db35312007-11-21 15:28:32 +0200693 ASSERT(is_empty_shadow_page(sp->spt));
694 sp->slot_bitmap = 0;
695 sp->multimapped = 0;
696 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800697 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200698 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800699}
700
Avi Kivity714b93d2007-01-05 16:36:53 -0800701static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200702 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800703{
704 struct kvm_pte_chain *pte_chain;
705 struct hlist_node *node;
706 int i;
707
708 if (!parent_pte)
709 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200710 if (!sp->multimapped) {
711 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800712
713 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200714 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800715 return;
716 }
Avi Kivity4db35312007-11-21 15:28:32 +0200717 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800718 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200719 INIT_HLIST_HEAD(&sp->parent_ptes);
720 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800721 pte_chain->parent_ptes[0] = old;
722 }
Avi Kivity4db35312007-11-21 15:28:32 +0200723 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800724 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
725 continue;
726 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
727 if (!pte_chain->parent_ptes[i]) {
728 pte_chain->parent_ptes[i] = parent_pte;
729 return;
730 }
731 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800732 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800733 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200734 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800735 pte_chain->parent_ptes[0] = parent_pte;
736}
737
Avi Kivity4db35312007-11-21 15:28:32 +0200738static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800739 u64 *parent_pte)
740{
741 struct kvm_pte_chain *pte_chain;
742 struct hlist_node *node;
743 int i;
744
Avi Kivity4db35312007-11-21 15:28:32 +0200745 if (!sp->multimapped) {
746 BUG_ON(sp->parent_pte != parent_pte);
747 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800748 return;
749 }
Avi Kivity4db35312007-11-21 15:28:32 +0200750 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800751 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
752 if (!pte_chain->parent_ptes[i])
753 break;
754 if (pte_chain->parent_ptes[i] != parent_pte)
755 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800756 while (i + 1 < NR_PTE_CHAIN_ENTRIES
757 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800758 pte_chain->parent_ptes[i]
759 = pte_chain->parent_ptes[i + 1];
760 ++i;
761 }
762 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800763 if (i == 0) {
764 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300765 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200766 if (hlist_empty(&sp->parent_ptes)) {
767 sp->multimapped = 0;
768 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800769 }
770 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800771 return;
772 }
773 BUG();
774}
775
Avi Kivity4db35312007-11-21 15:28:32 +0200776static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800777{
778 unsigned index;
779 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200780 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800781 struct hlist_node *node;
782
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800783 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200784 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800785 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200786 hlist_for_each_entry(sp, node, bucket, hash_link)
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500787 if (sp->gfn == gfn && !sp->role.metaphysical
788 && !sp->role.invalid) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800789 pgprintk("%s: found role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800790 __func__, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +0200791 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800792 }
793 return NULL;
794}
795
796static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
797 gfn_t gfn,
798 gva_t gaddr,
799 unsigned level,
800 int metaphysical,
Avi Kivity41074d02007-12-09 17:00:02 +0200801 unsigned access,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +0200802 u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800803{
804 union kvm_mmu_page_role role;
805 unsigned index;
806 unsigned quadrant;
807 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200808 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800809 struct hlist_node *node;
810
811 role.word = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800812 role.glevels = vcpu->arch.mmu.root_level;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800813 role.level = level;
814 role.metaphysical = metaphysical;
Avi Kivity41074d02007-12-09 17:00:02 +0200815 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800816 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800817 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
818 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
819 role.quadrant = quadrant;
820 }
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800821 pgprintk("%s: looking gfn %lx role %x\n", __func__,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800822 gfn, role.word);
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200823 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800824 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200825 hlist_for_each_entry(sp, node, bucket, hash_link)
826 if (sp->gfn == gfn && sp->role.word == role.word) {
827 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800828 pgprintk("%s: found\n", __func__);
Avi Kivity4db35312007-11-21 15:28:32 +0200829 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800830 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +0200831 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +0200832 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
833 if (!sp)
834 return sp;
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800835 pgprintk("%s: adding gfn %lx role %x\n", __func__, gfn, role.word);
Avi Kivity4db35312007-11-21 15:28:32 +0200836 sp->gfn = gfn;
837 sp->role = role;
838 hlist_add_head(&sp->hash_link, bucket);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800839 vcpu->arch.mmu.prefetch_page(vcpu, sp);
Avi Kivity374cbac2007-01-05 16:36:43 -0800840 if (!metaphysical)
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500841 rmap_write_protect(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200842 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800843}
844
Avi Kivity90cb0522007-07-17 13:04:56 +0300845static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +0200846 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -0800847{
Avi Kivity697fe2e2007-01-05 16:36:46 -0800848 unsigned i;
849 u64 *pt;
850 u64 ent;
851
Avi Kivity4db35312007-11-21 15:28:32 +0200852 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800853
Avi Kivity4db35312007-11-21 15:28:32 +0200854 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800855 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Avi Kivityc7addb92007-09-16 18:58:32 +0200856 if (is_shadow_present_pte(pt[i]))
Izik Eidus290fc382007-09-27 14:11:22 +0200857 rmap_remove(kvm, &pt[i]);
Avi Kivityc7addb92007-09-16 18:58:32 +0200858 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800859 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300860 kvm_flush_remote_tlbs(kvm);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800861 return;
862 }
863
864 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
865 ent = pt[i];
866
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300867 if (is_shadow_present_pte(ent)) {
868 if (!is_large_pte(ent)) {
869 ent &= PT64_BASE_ADDR_MASK;
870 mmu_page_remove_parent_pte(page_header(ent),
871 &pt[i]);
872 } else {
873 --kvm->stat.lpages;
874 rmap_remove(kvm, &pt[i]);
875 }
876 }
Avi Kivityc7addb92007-09-16 18:58:32 +0200877 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800878 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300879 kvm_flush_remote_tlbs(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800880}
881
Avi Kivity4db35312007-11-21 15:28:32 +0200882static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800883{
Avi Kivity4db35312007-11-21 15:28:32 +0200884 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800885}
886
Avi Kivity12b7d282007-09-23 14:10:49 +0200887static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
888{
889 int i;
890
891 for (i = 0; i < KVM_MAX_VCPUS; ++i)
892 if (kvm->vcpus[i])
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800893 kvm->vcpus[i]->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +0200894}
895
Avi Kivity4db35312007-11-21 15:28:32 +0200896static void kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -0800897{
898 u64 *parent_pte;
899
Avi Kivity4cee5762007-11-18 16:37:07 +0200900 ++kvm->stat.mmu_shadow_zapped;
Avi Kivity4db35312007-11-21 15:28:32 +0200901 while (sp->multimapped || sp->parent_pte) {
902 if (!sp->multimapped)
903 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -0800904 else {
905 struct kvm_pte_chain *chain;
906
Avi Kivity4db35312007-11-21 15:28:32 +0200907 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -0800908 struct kvm_pte_chain, link);
909 parent_pte = chain->parent_ptes[0];
910 }
Avi Kivity697fe2e2007-01-05 16:36:46 -0800911 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +0200912 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityc7addb92007-09-16 18:58:32 +0200913 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800914 }
Avi Kivity4db35312007-11-21 15:28:32 +0200915 kvm_mmu_page_unlink_children(kvm, sp);
916 if (!sp->root_count) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -0300917 if (!sp->role.metaphysical)
918 unaccount_shadowed(kvm, sp->gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200919 hlist_del(&sp->hash_link);
920 kvm_mmu_free_page(kvm, sp);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500921 } else {
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800922 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500923 sp->role.invalid = 1;
924 kvm_reload_remote_mmus(kvm);
925 }
Avi Kivity12b7d282007-09-23 14:10:49 +0200926 kvm_mmu_reset_last_pte_updated(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800927}
928
Izik Eidus82ce2c92007-10-02 18:52:55 +0200929/*
930 * Changing the number of mmu pages allocated to the vm
931 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
932 */
933void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
934{
935 /*
936 * If we set the number of mmu pages to be smaller be than the
937 * number of actived pages , we must to free some mmu pages before we
938 * change the value
939 */
940
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800941 if ((kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages) >
Izik Eidus82ce2c92007-10-02 18:52:55 +0200942 kvm_nr_mmu_pages) {
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800943 int n_used_mmu_pages = kvm->arch.n_alloc_mmu_pages
944 - kvm->arch.n_free_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200945
946 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
947 struct kvm_mmu_page *page;
948
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800949 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +0200950 struct kvm_mmu_page, link);
951 kvm_mmu_zap_page(kvm, page);
952 n_used_mmu_pages--;
953 }
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800954 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200955 }
956 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800957 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
958 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200959
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800960 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200961}
962
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500963static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -0800964{
965 unsigned index;
966 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200967 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -0800968 struct hlist_node *node, *n;
969 int r;
970
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800971 pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
Avi Kivitya4360362007-01-05 16:36:45 -0800972 r = 0;
Dong, Eddie1ae0a132008-01-07 13:20:25 +0200973 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800974 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200975 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
976 if (sp->gfn == gfn && !sp->role.metaphysical) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800977 pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +0200978 sp->role.word);
979 kvm_mmu_zap_page(kvm, sp);
Avi Kivitya4360362007-01-05 16:36:45 -0800980 r = 1;
981 }
982 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800983}
984
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500985static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +0300986{
Avi Kivity4db35312007-11-21 15:28:32 +0200987 struct kvm_mmu_page *sp;
Avi Kivity97a0a012007-05-31 15:08:29 +0300988
Avi Kivity4db35312007-11-21 15:28:32 +0200989 while ((sp = kvm_mmu_lookup_page(kvm, gfn)) != NULL) {
Harvey Harrisonb8688d52008-03-03 12:59:56 -0800990 pgprintk("%s: zap %lx %x\n", __func__, gfn, sp->role.word);
Avi Kivity4db35312007-11-21 15:28:32 +0200991 kvm_mmu_zap_page(kvm, sp);
Avi Kivity97a0a012007-05-31 15:08:29 +0300992 }
993}
994
Avi Kivity38c335f2007-11-21 14:20:22 +0200995static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800996{
Avi Kivity38c335f2007-11-21 14:20:22 +0200997 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity4db35312007-11-21 15:28:32 +0200998 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800999
Avi Kivity4db35312007-11-21 15:28:32 +02001000 __set_bit(slot, &sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001001}
1002
Avi Kivity039576c2007-03-20 12:46:50 +02001003struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
1004{
Izik Eidus72dc67a2008-02-10 18:04:15 +02001005 struct page *page;
1006
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001007 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Avi Kivity039576c2007-03-20 12:46:50 +02001008
1009 if (gpa == UNMAPPED_GVA)
1010 return NULL;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001011
1012 down_read(&current->mm->mmap_sem);
1013 page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
1014 up_read(&current->mm->mmap_sem);
1015
1016 return page;
Avi Kivity039576c2007-03-20 12:46:50 +02001017}
1018
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001019static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
1020 unsigned pt_access, unsigned pte_access,
1021 int user_fault, int write_fault, int dirty,
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001022 int *ptwrite, int largepage, gfn_t gfn,
1023 struct page *page)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001024{
1025 u64 spte;
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001026 int was_rmapped = 0;
Izik Eidus75e68e62008-01-12 23:49:09 +02001027 int was_writeble = is_writeble_pte(*shadow_pte);
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001028 hfn_t host_pfn = (*shadow_pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001029
Avi Kivitybc750ba2007-12-09 18:39:41 +02001030 pgprintk("%s: spte %llx access %x write_fault %d"
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001031 " user_fault %d gfn %lx\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001032 __func__, *shadow_pte, pt_access,
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001033 write_fault, user_fault, gfn);
1034
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001035 if (is_rmap_pte(*shadow_pte)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001036 /*
1037 * If we overwrite a PTE page pointer with a 2MB PMD, unlink
1038 * the parent of the now unreachable PTE.
1039 */
1040 if (largepage && !is_large_pte(*shadow_pte)) {
1041 struct kvm_mmu_page *child;
1042 u64 pte = *shadow_pte;
1043
1044 child = page_header(pte & PT64_BASE_ADDR_MASK);
1045 mmu_page_remove_parent_pte(child, shadow_pte);
1046 } else if (host_pfn != page_to_pfn(page)) {
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001047 pgprintk("hfn old %lx new %lx\n",
1048 host_pfn, page_to_pfn(page));
1049 rmap_remove(vcpu->kvm, shadow_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001050 } else {
1051 if (largepage)
1052 was_rmapped = is_large_pte(*shadow_pte);
1053 else
1054 was_rmapped = 1;
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001055 }
Marcelo Tosatti15aaa812008-03-17 10:08:18 -03001056 }
1057
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001058 /*
1059 * We don't set the accessed bit, since we sometimes want to see
1060 * whether the guest actually used the pte (in order to detect
1061 * demand paging).
1062 */
1063 spte = PT_PRESENT_MASK | PT_DIRTY_MASK;
1064 if (!dirty)
1065 pte_access &= ~ACC_WRITE_MASK;
1066 if (!(pte_access & ACC_EXEC_MASK))
1067 spte |= PT64_NX_MASK;
1068
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001069 spte |= PT_PRESENT_MASK;
1070 if (pte_access & ACC_USER_MASK)
1071 spte |= PT_USER_MASK;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001072 if (largepage)
1073 spte |= PT_PAGE_SIZE_MASK;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001074
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001075 spte |= page_to_phys(page);
1076
1077 if ((pte_access & ACC_WRITE_MASK)
1078 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
1079 struct kvm_mmu_page *shadow;
1080
1081 spte |= PT_WRITABLE_MASK;
1082 if (user_fault) {
1083 mmu_unshadow(vcpu->kvm, gfn);
1084 goto unshadowed;
1085 }
1086
1087 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001088 if (shadow ||
1089 (largepage && has_wrprotected_page(vcpu->kvm, gfn))) {
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001090 pgprintk("%s: found shadow page for %lx, marking ro\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001091 __func__, gfn);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001092 pte_access &= ~ACC_WRITE_MASK;
1093 if (is_writeble_pte(spte)) {
1094 spte &= ~PT_WRITABLE_MASK;
1095 kvm_x86_ops->tlb_flush(vcpu);
1096 }
1097 if (write_fault)
1098 *ptwrite = 1;
1099 }
1100 }
1101
1102unshadowed:
1103
1104 if (pte_access & ACC_WRITE_MASK)
1105 mark_page_dirty(vcpu->kvm, gfn);
1106
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001107 pgprintk("%s: setting spte %llx\n", __func__, spte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001108 pgprintk("instantiating %s PTE (%s) at %d (%llx) addr %llx\n",
1109 (spte&PT_PAGE_SIZE_MASK)? "2MB" : "4kB",
1110 (spte&PT_WRITABLE_MASK)?"RW":"R", gfn, spte, shadow_pte);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001111 set_shadow_pte(shadow_pte, spte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001112 if (!was_rmapped && (spte & PT_PAGE_SIZE_MASK)
1113 && (spte & PT_PRESENT_MASK))
1114 ++vcpu->kvm->stat.lpages;
1115
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001116 page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
1117 if (!was_rmapped) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001118 rmap_add(vcpu, shadow_pte, gfn, largepage);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001119 if (!is_rmap_pte(*shadow_pte))
1120 kvm_release_page_clean(page);
Izik Eidus75e68e62008-01-12 23:49:09 +02001121 } else {
1122 if (was_writeble)
1123 kvm_release_page_dirty(page);
1124 else
1125 kvm_release_page_clean(page);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001126 }
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001127 if (!ptwrite || !*ptwrite)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001128 vcpu->arch.last_pte_updated = shadow_pte;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +02001129}
1130
Avi Kivity6aa8b732006-12-10 02:21:36 -08001131static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
1132{
1133}
1134
Joerg Roedel4d9976b2008-02-07 13:47:42 +01001135static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001136 int largepage, gfn_t gfn, struct page *page,
1137 int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001138{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001139 hpa_t table_addr = vcpu->arch.mmu.root_hpa;
Avi Kivitye8332402007-12-09 18:43:00 +02001140 int pt_write = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001141
1142 for (; ; level--) {
1143 u32 index = PT64_INDEX(v, level);
1144 u64 *table;
1145
1146 ASSERT(VALID_PAGE(table_addr));
1147 table = __va(table_addr);
1148
1149 if (level == 1) {
Avi Kivitye8332402007-12-09 18:43:00 +02001150 mmu_set_spte(vcpu, &table[index], ACC_ALL, ACC_ALL,
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001151 0, write, 1, &pt_write, 0, gfn, page);
1152 return pt_write;
1153 }
1154
1155 if (largepage && level == 2) {
1156 mmu_set_spte(vcpu, &table[index], ACC_ALL, ACC_ALL,
1157 0, write, 1, &pt_write, 1, gfn, page);
Avi Kivityd196e342008-01-24 11:44:11 +02001158 return pt_write;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001159 }
1160
Avi Kivityc7addb92007-09-16 18:58:32 +02001161 if (table[index] == shadow_trap_nonpresent_pte) {
Avi Kivity25c0de22007-01-05 16:36:42 -08001162 struct kvm_mmu_page *new_table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001163 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001164
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001165 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
1166 >> PAGE_SHIFT;
1167 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
1168 v, level - 1,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001169 1, ACC_ALL, &table[index]);
Avi Kivity25c0de22007-01-05 16:36:42 -08001170 if (!new_table) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001171 pgprintk("nonpaging_map: ENOMEM\n");
Avi Kivityd7824ff2007-12-30 12:29:05 +02001172 kvm_release_page_clean(page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001173 return -ENOMEM;
1174 }
1175
Avi Kivity47ad8e62007-05-06 15:50:58 +03001176 table[index] = __pa(new_table->spt) | PT_PRESENT_MASK
Avi Kivity25c0de22007-01-05 16:36:42 -08001177 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001178 }
1179 table_addr = table[index] & PT64_BASE_ADDR_MASK;
1180 }
1181}
1182
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001183static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1184{
1185 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001186 int largepage = 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001187
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001188 struct page *page;
1189
Izik Eidus72dc67a2008-02-10 18:04:15 +02001190 down_read(&vcpu->kvm->slots_lock);
1191
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001192 down_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001193 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1194 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1195 largepage = 1;
1196 }
1197
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001198 page = gfn_to_page(vcpu->kvm, gfn);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001199 up_read(&current->mm->mmap_sem);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001200
Avi Kivityd196e342008-01-24 11:44:11 +02001201 /* mmio */
1202 if (is_error_page(page)) {
1203 kvm_release_page_clean(page);
1204 up_read(&vcpu->kvm->slots_lock);
1205 return 1;
1206 }
1207
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001208 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02001209 kvm_mmu_free_some_pages(vcpu);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001210 r = __direct_map(vcpu, v, write, largepage, gfn, page,
1211 PT32E_ROOT_LEVEL);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001212 spin_unlock(&vcpu->kvm->mmu_lock);
1213
Izik Eidus72dc67a2008-02-10 18:04:15 +02001214 up_read(&vcpu->kvm->slots_lock);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001215
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001216 return r;
1217}
1218
1219
Avi Kivityc7addb92007-09-16 18:58:32 +02001220static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1221 struct kvm_mmu_page *sp)
1222{
1223 int i;
1224
1225 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1226 sp->spt[i] = shadow_trap_nonpresent_pte;
1227}
1228
Avi Kivity17ac10a2007-01-05 16:36:40 -08001229static void mmu_free_roots(struct kvm_vcpu *vcpu)
1230{
1231 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02001232 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001233
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001234 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03001235 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001236 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001237#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001238 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1239 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001240
Avi Kivity4db35312007-11-21 15:28:32 +02001241 sp = page_header(root);
1242 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001243 if (!sp->root_count && sp->role.invalid)
1244 kvm_mmu_zap_page(vcpu->kvm, sp);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001245 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001246 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001247 return;
1248 }
1249#endif
1250 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001251 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001252
Avi Kivity417726a2007-04-12 17:35:58 +03001253 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03001254 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001255 sp = page_header(root);
1256 --sp->root_count;
Marcelo Tosatti2e53d632008-02-20 14:47:24 -05001257 if (!sp->root_count && sp->role.invalid)
1258 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity417726a2007-04-12 17:35:58 +03001259 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001260 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001261 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001262 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001263 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001264}
1265
1266static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
1267{
1268 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001269 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02001270 struct kvm_mmu_page *sp;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001271 int metaphysical = 0;
Avi Kivity3bb65a22007-01-05 16:36:51 -08001272
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001273 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001274
1275#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001276 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1277 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001278
1279 ASSERT(!VALID_PAGE(root));
Joerg Roedelfb72d162008-02-07 13:47:44 +01001280 if (tdp_enabled)
1281 metaphysical = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001282 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001283 PT64_ROOT_LEVEL, metaphysical,
1284 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001285 root = __pa(sp->spt);
1286 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001287 vcpu->arch.mmu.root_hpa = root;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001288 return;
1289 }
1290#endif
Joerg Roedelfb72d162008-02-07 13:47:44 +01001291 metaphysical = !is_paging(vcpu);
1292 if (tdp_enabled)
1293 metaphysical = 1;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001294 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001295 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001296
1297 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001298 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
1299 if (!is_present_pte(vcpu->arch.pdptrs[i])) {
1300 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03001301 continue;
1302 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001303 root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT;
1304 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001305 root_gfn = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02001306 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
Joerg Roedelfb72d162008-02-07 13:47:44 +01001307 PT32_ROOT_LEVEL, metaphysical,
Avi Kivityf7d9c7b2008-02-26 22:12:10 +02001308 ACC_ALL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001309 root = __pa(sp->spt);
1310 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001311 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001312 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001313 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001314}
1315
Avi Kivity6aa8b732006-12-10 02:21:36 -08001316static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
1317{
1318 return vaddr;
1319}
1320
1321static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02001322 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001323{
Avi Kivitye8332402007-12-09 18:43:00 +02001324 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08001325 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001326
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001327 pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08001328 r = mmu_topup_memory_caches(vcpu);
1329 if (r)
1330 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08001331
Avi Kivity6aa8b732006-12-10 02:21:36 -08001332 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001333 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001334
Avi Kivitye8332402007-12-09 18:43:00 +02001335 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001336
Avi Kivitye8332402007-12-09 18:43:00 +02001337 return nonpaging_map(vcpu, gva & PAGE_MASK,
1338 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001339}
1340
Joerg Roedelfb72d162008-02-07 13:47:44 +01001341static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
1342 u32 error_code)
1343{
1344 struct page *page;
1345 int r;
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001346 int largepage = 0;
1347 gfn_t gfn = gpa >> PAGE_SHIFT;
Joerg Roedelfb72d162008-02-07 13:47:44 +01001348
1349 ASSERT(vcpu);
1350 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
1351
1352 r = mmu_topup_memory_caches(vcpu);
1353 if (r)
1354 return r;
1355
1356 down_read(&current->mm->mmap_sem);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001357 if (is_largepage_backed(vcpu, gfn & ~(KVM_PAGES_PER_HPAGE-1))) {
1358 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1359 largepage = 1;
1360 }
1361 page = gfn_to_page(vcpu->kvm, gfn);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001362 if (is_error_page(page)) {
1363 kvm_release_page_clean(page);
1364 up_read(&current->mm->mmap_sem);
1365 return 1;
1366 }
1367 spin_lock(&vcpu->kvm->mmu_lock);
1368 kvm_mmu_free_some_pages(vcpu);
1369 r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001370 largepage, gfn, page, TDP_ROOT_LEVEL);
Joerg Roedelfb72d162008-02-07 13:47:44 +01001371 spin_unlock(&vcpu->kvm->mmu_lock);
1372 up_read(&current->mm->mmap_sem);
1373
1374 return r;
1375}
1376
Avi Kivity6aa8b732006-12-10 02:21:36 -08001377static void nonpaging_free(struct kvm_vcpu *vcpu)
1378{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001379 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001380}
1381
1382static int nonpaging_init_context(struct kvm_vcpu *vcpu)
1383{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001384 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001385
1386 context->new_cr3 = nonpaging_new_cr3;
1387 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001388 context->gva_to_gpa = nonpaging_gva_to_gpa;
1389 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001390 context->prefetch_page = nonpaging_prefetch_page;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001391 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001392 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001393 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001394 return 0;
1395}
1396
Avi Kivityd835dfe2007-11-21 02:57:59 +02001397void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001398{
Avi Kivity1165f5f2007-04-19 17:27:43 +03001399 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001400 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001401}
1402
1403static void paging_new_cr3(struct kvm_vcpu *vcpu)
1404{
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001405 pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001406 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001407}
1408
Avi Kivity6aa8b732006-12-10 02:21:36 -08001409static void inject_page_fault(struct kvm_vcpu *vcpu,
1410 u64 addr,
1411 u32 err_code)
1412{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02001413 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001414}
1415
Avi Kivity6aa8b732006-12-10 02:21:36 -08001416static void paging_free(struct kvm_vcpu *vcpu)
1417{
1418 nonpaging_free(vcpu);
1419}
1420
1421#define PTTYPE 64
1422#include "paging_tmpl.h"
1423#undef PTTYPE
1424
1425#define PTTYPE 32
1426#include "paging_tmpl.h"
1427#undef PTTYPE
1428
Avi Kivity17ac10a2007-01-05 16:36:40 -08001429static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001430{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001431 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001432
1433 ASSERT(is_pae(vcpu));
1434 context->new_cr3 = paging_new_cr3;
1435 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001436 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02001437 context->prefetch_page = paging64_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001438 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001439 context->root_level = level;
1440 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001441 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001442 return 0;
1443}
1444
Avi Kivity17ac10a2007-01-05 16:36:40 -08001445static int paging64_init_context(struct kvm_vcpu *vcpu)
1446{
1447 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1448}
1449
Avi Kivity6aa8b732006-12-10 02:21:36 -08001450static int paging32_init_context(struct kvm_vcpu *vcpu)
1451{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001452 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001453
1454 context->new_cr3 = paging_new_cr3;
1455 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001456 context->gva_to_gpa = paging32_gva_to_gpa;
1457 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001458 context->prefetch_page = paging32_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001459 context->root_level = PT32_ROOT_LEVEL;
1460 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001461 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001462 return 0;
1463}
1464
1465static int paging32E_init_context(struct kvm_vcpu *vcpu)
1466{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001467 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001468}
1469
Joerg Roedelfb72d162008-02-07 13:47:44 +01001470static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
1471{
1472 struct kvm_mmu *context = &vcpu->arch.mmu;
1473
1474 context->new_cr3 = nonpaging_new_cr3;
1475 context->page_fault = tdp_page_fault;
1476 context->free = nonpaging_free;
1477 context->prefetch_page = nonpaging_prefetch_page;
1478 context->shadow_root_level = TDP_ROOT_LEVEL;
1479 context->root_hpa = INVALID_PAGE;
1480
1481 if (!is_paging(vcpu)) {
1482 context->gva_to_gpa = nonpaging_gva_to_gpa;
1483 context->root_level = 0;
1484 } else if (is_long_mode(vcpu)) {
1485 context->gva_to_gpa = paging64_gva_to_gpa;
1486 context->root_level = PT64_ROOT_LEVEL;
1487 } else if (is_pae(vcpu)) {
1488 context->gva_to_gpa = paging64_gva_to_gpa;
1489 context->root_level = PT32E_ROOT_LEVEL;
1490 } else {
1491 context->gva_to_gpa = paging32_gva_to_gpa;
1492 context->root_level = PT32_ROOT_LEVEL;
1493 }
1494
1495 return 0;
1496}
1497
1498static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001499{
1500 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001501 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001502
1503 if (!is_paging(vcpu))
1504 return nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08001505 else if (is_long_mode(vcpu))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001506 return paging64_init_context(vcpu);
1507 else if (is_pae(vcpu))
1508 return paging32E_init_context(vcpu);
1509 else
1510 return paging32_init_context(vcpu);
1511}
1512
Joerg Roedelfb72d162008-02-07 13:47:44 +01001513static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1514{
1515 if (tdp_enabled)
1516 return init_kvm_tdp_mmu(vcpu);
1517 else
1518 return init_kvm_softmmu(vcpu);
1519}
1520
Avi Kivity6aa8b732006-12-10 02:21:36 -08001521static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1522{
1523 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001524 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
1525 vcpu->arch.mmu.free(vcpu);
1526 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001527 }
1528}
1529
1530int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1531{
Avi Kivity17c3ba92007-06-04 15:58:30 +03001532 destroy_kvm_mmu(vcpu);
1533 return init_kvm_mmu(vcpu);
1534}
Eddie Dong8668a3c2007-10-10 14:26:45 +08001535EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001536
1537int kvm_mmu_load(struct kvm_vcpu *vcpu)
1538{
Avi Kivity714b93d2007-01-05 16:36:53 -08001539 int r;
1540
Avi Kivitye2dec932007-01-05 16:36:54 -08001541 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001542 if (r)
1543 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001544 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02001545 kvm_mmu_free_some_pages(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001546 mmu_alloc_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001547 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001548 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001549 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001550out:
1551 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001552}
Avi Kivity17c3ba92007-06-04 15:58:30 +03001553EXPORT_SYMBOL_GPL(kvm_mmu_load);
1554
1555void kvm_mmu_unload(struct kvm_vcpu *vcpu)
1556{
1557 mmu_free_roots(vcpu);
1558}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001559
Avi Kivity09072da2007-05-01 14:16:52 +03001560static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02001561 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02001562 u64 *spte)
1563{
1564 u64 pte;
1565 struct kvm_mmu_page *child;
1566
1567 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02001568 if (is_shadow_present_pte(pte)) {
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001569 if (sp->role.level == PT_PAGE_TABLE_LEVEL ||
1570 is_large_pte(pte))
Izik Eidus290fc382007-09-27 14:11:22 +02001571 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001572 else {
1573 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03001574 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001575 }
1576 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001577 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001578 if (is_large_pte(pte))
1579 --vcpu->kvm->stat.lpages;
Avi Kivityac1b7142007-03-08 17:13:32 +02001580}
1581
Avi Kivity00284252007-05-01 16:53:31 +03001582static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02001583 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03001584 u64 *spte,
Dong, Eddie489f1d62008-01-07 11:14:20 +02001585 const void *new)
Avi Kivity00284252007-05-01 16:53:31 +03001586{
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001587 if ((sp->role.level != PT_PAGE_TABLE_LEVEL)
1588 && !vcpu->arch.update_pte.largepage) {
Avi Kivity4cee5762007-11-18 16:37:07 +02001589 ++vcpu->kvm->stat.mmu_pde_zapped;
Avi Kivity00284252007-05-01 16:53:31 +03001590 return;
Avi Kivity4cee5762007-11-18 16:37:07 +02001591 }
Avi Kivity00284252007-05-01 16:53:31 +03001592
Avi Kivity4cee5762007-11-18 16:37:07 +02001593 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02001594 if (sp->role.glevels == PT32_ROOT_LEVEL)
Dong, Eddie489f1d62008-01-07 11:14:20 +02001595 paging32_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03001596 else
Dong, Eddie489f1d62008-01-07 11:14:20 +02001597 paging64_update_pte(vcpu, sp, spte, new);
Avi Kivity00284252007-05-01 16:53:31 +03001598}
1599
Avi Kivity79539ce2007-11-21 02:06:21 +02001600static bool need_remote_flush(u64 old, u64 new)
1601{
1602 if (!is_shadow_present_pte(old))
1603 return false;
1604 if (!is_shadow_present_pte(new))
1605 return true;
1606 if ((old ^ new) & PT64_BASE_ADDR_MASK)
1607 return true;
1608 old ^= PT64_NX_MASK;
1609 new ^= PT64_NX_MASK;
1610 return (old & ~new & PT64_PERM_MASK) != 0;
1611}
1612
1613static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
1614{
1615 if (need_remote_flush(old, new))
1616 kvm_flush_remote_tlbs(vcpu->kvm);
1617 else
1618 kvm_mmu_flush_tlb(vcpu);
1619}
1620
Avi Kivity12b7d282007-09-23 14:10:49 +02001621static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
1622{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001623 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02001624
1625 return !!(spte && (*spte & PT_ACCESSED_MASK));
1626}
1627
Avi Kivityd7824ff2007-12-30 12:29:05 +02001628static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1629 const u8 *new, int bytes)
1630{
1631 gfn_t gfn;
1632 int r;
1633 u64 gpte = 0;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001634 struct page *page;
Avi Kivityd7824ff2007-12-30 12:29:05 +02001635
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001636 vcpu->arch.update_pte.largepage = 0;
1637
Avi Kivityd7824ff2007-12-30 12:29:05 +02001638 if (bytes != 4 && bytes != 8)
1639 return;
1640
1641 /*
1642 * Assume that the pte write on a page table of the same type
1643 * as the current vcpu paging mode. This is nearly always true
1644 * (might be false while changing modes). Note it is verified later
1645 * by update_pte().
1646 */
1647 if (is_pae(vcpu)) {
1648 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
1649 if ((bytes == 4) && (gpa % 4 == 0)) {
1650 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
1651 if (r)
1652 return;
1653 memcpy((void *)&gpte + (gpa % 8), new, 4);
1654 } else if ((bytes == 8) && (gpa % 8 == 0)) {
1655 memcpy((void *)&gpte, new, 8);
1656 }
1657 } else {
1658 if ((bytes == 4) && (gpa % 4 == 0))
1659 memcpy((void *)&gpte, new, 4);
1660 }
1661 if (!is_present_pte(gpte))
1662 return;
1663 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001664
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001665 down_read(&current->mm->mmap_sem);
1666 if (is_large_pte(gpte) && is_largepage_backed(vcpu, gfn)) {
1667 gfn &= ~(KVM_PAGES_PER_HPAGE-1);
1668 vcpu->arch.update_pte.largepage = 1;
1669 }
Izik Eidus72dc67a2008-02-10 18:04:15 +02001670 page = gfn_to_page(vcpu->kvm, gfn);
Marcelo Tosatti05da4552008-02-23 11:44:30 -03001671 up_read(&current->mm->mmap_sem);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001672
Avi Kivityd196e342008-01-24 11:44:11 +02001673 if (is_error_page(page)) {
1674 kvm_release_page_clean(page);
1675 return;
1676 }
Avi Kivityd7824ff2007-12-30 12:29:05 +02001677 vcpu->arch.update_pte.gfn = gfn;
Avi Kivitye48bb492008-03-23 14:21:08 +02001678 vcpu->arch.update_pte.page = page;
Avi Kivityd7824ff2007-12-30 12:29:05 +02001679}
1680
Avi Kivity09072da2007-05-01 14:16:52 +03001681void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Shaohua Life551882007-07-23 14:51:39 +08001682 const u8 *new, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001683{
Avi Kivity9b7a0322007-01-05 16:36:45 -08001684 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02001685 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001686 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001687 struct hlist_head *bucket;
1688 unsigned index;
Dong, Eddie489f1d62008-01-07 11:14:20 +02001689 u64 entry, gentry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001690 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001691 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001692 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001693 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001694 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03001695 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001696 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001697 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02001698 int npte;
Dong, Eddie489f1d62008-01-07 11:14:20 +02001699 int r;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001700
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001701 pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02001702 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001703 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02001704 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02001705 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02001706 kvm_mmu_audit(vcpu, "pre pte write");
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001707 if (gfn == vcpu->arch.last_pt_write_gfn
Avi Kivity12b7d282007-09-23 14:10:49 +02001708 && !last_updated_pte_accessed(vcpu)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001709 ++vcpu->arch.last_pt_write_count;
1710 if (vcpu->arch.last_pt_write_count >= 3)
Avi Kivity86a5ba02007-01-05 16:36:50 -08001711 flooded = 1;
1712 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001713 vcpu->arch.last_pt_write_gfn = gfn;
1714 vcpu->arch.last_pt_write_count = 1;
1715 vcpu->arch.last_pte_updated = NULL;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001716 }
Dong, Eddie1ae0a132008-01-07 13:20:25 +02001717 index = kvm_page_table_hashfn(gfn);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001718 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001719 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
1720 if (sp->gfn != gfn || sp->role.metaphysical)
Avi Kivity9b7a0322007-01-05 16:36:45 -08001721 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02001722 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001723 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03001724 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001725 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001726 /*
1727 * Misaligned accesses are too much trouble to fix
1728 * up; also, they usually indicate a page is not used
1729 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08001730 *
1731 * If we're seeing too many writes to a page,
1732 * it may no longer be a page table, or we may be
1733 * forking, in which case it is better to unmap the
1734 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001735 */
1736 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02001737 gpa, bytes, sp->role.word);
1738 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02001739 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001740 continue;
1741 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001742 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02001743 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02001744 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001745 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001746 page_offset <<= 1; /* 32->64 */
1747 /*
1748 * A 32-bit pde maps 4MB while the shadow pdes map
1749 * only 2MB. So we need to double the offset again
1750 * and zap two pdes instead of one.
1751 */
1752 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03001753 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02001754 page_offset <<= 1;
1755 npte = 2;
1756 }
Avi Kivityfce06572007-05-01 16:44:05 +03001757 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001758 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001759 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03001760 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001761 }
Avi Kivity4db35312007-11-21 15:28:32 +02001762 spte = &sp->spt[page_offset / sizeof(*spte)];
Dong, Eddie489f1d62008-01-07 11:14:20 +02001763 if ((gpa & (pte_size - 1)) || (bytes < pte_size)) {
1764 gentry = 0;
1765 r = kvm_read_guest_atomic(vcpu->kvm,
1766 gpa & ~(u64)(pte_size - 1),
1767 &gentry, pte_size);
1768 new = (const void *)&gentry;
1769 if (r < 0)
1770 new = NULL;
1771 }
Avi Kivityac1b7142007-03-08 17:13:32 +02001772 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02001773 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02001774 mmu_pte_write_zap_pte(vcpu, sp, spte);
Dong, Eddie489f1d62008-01-07 11:14:20 +02001775 if (new)
1776 mmu_pte_write_new_pte(vcpu, sp, spte, new);
Avi Kivity79539ce2007-11-21 02:06:21 +02001777 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001778 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001779 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001780 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001781 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001782 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivityd7824ff2007-12-30 12:29:05 +02001783 if (vcpu->arch.update_pte.page) {
1784 kvm_release_page_clean(vcpu->arch.update_pte.page);
1785 vcpu->arch.update_pte.page = NULL;
1786 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08001787}
1788
Avi Kivitya4360362007-01-05 16:36:45 -08001789int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1790{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001791 gpa_t gpa;
1792 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08001793
Izik Eidus72dc67a2008-02-10 18:04:15 +02001794 down_read(&vcpu->kvm->slots_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001795 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001796 up_read(&vcpu->kvm->slots_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001797
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001798 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001799 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001800 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001801 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08001802}
1803
Avi Kivity22d95b12007-09-14 20:26:06 +03001804void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08001805{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001806 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
Avi Kivity4db35312007-11-21 15:28:32 +02001807 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08001808
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001809 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02001810 struct kvm_mmu_page, link);
1811 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02001812 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08001813 }
1814}
Avi Kivityebeace82007-01-05 16:36:47 -08001815
Avi Kivity30677142007-10-28 18:48:59 +02001816int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
1817{
1818 int r;
1819 enum emulation_result er;
1820
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001821 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02001822 if (r < 0)
1823 goto out;
1824
1825 if (!r) {
1826 r = 1;
1827 goto out;
1828 }
1829
Avi Kivityb733bfb2007-10-28 18:52:05 +02001830 r = mmu_topup_memory_caches(vcpu);
1831 if (r)
1832 goto out;
1833
Avi Kivity30677142007-10-28 18:48:59 +02001834 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02001835
1836 switch (er) {
1837 case EMULATE_DONE:
1838 return 1;
1839 case EMULATE_DO_MMIO:
1840 ++vcpu->stat.mmio_exits;
1841 return 0;
1842 case EMULATE_FAIL:
1843 kvm_report_emulation_failure(vcpu, "pagetable");
1844 return 1;
1845 default:
1846 BUG();
1847 }
1848out:
Avi Kivity30677142007-10-28 18:48:59 +02001849 return r;
1850}
1851EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
1852
Joerg Roedel18552672008-02-07 13:47:41 +01001853void kvm_enable_tdp(void)
1854{
1855 tdp_enabled = true;
1856}
1857EXPORT_SYMBOL_GPL(kvm_enable_tdp);
1858
Avi Kivity6aa8b732006-12-10 02:21:36 -08001859static void free_mmu_pages(struct kvm_vcpu *vcpu)
1860{
Avi Kivity4db35312007-11-21 15:28:32 +02001861 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001862
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001863 while (!list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
1864 sp = container_of(vcpu->kvm->arch.active_mmu_pages.next,
Avi Kivity4db35312007-11-21 15:28:32 +02001865 struct kvm_mmu_page, link);
1866 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivityf51234c2007-01-05 16:36:52 -08001867 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001868 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001869}
1870
1871static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
1872{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001873 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001874 int i;
1875
1876 ASSERT(vcpu);
1877
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001878 if (vcpu->kvm->arch.n_requested_mmu_pages)
1879 vcpu->kvm->arch.n_free_mmu_pages =
1880 vcpu->kvm->arch.n_requested_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001881 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001882 vcpu->kvm->arch.n_free_mmu_pages =
1883 vcpu->kvm->arch.n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001884 /*
1885 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1886 * Therefore we need to allocate shadow page tables in the first
1887 * 4GB of memory, which happens to fit the DMA32 zone.
1888 */
1889 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
1890 if (!page)
1891 goto error_1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001892 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001893 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001894 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001895
Avi Kivity6aa8b732006-12-10 02:21:36 -08001896 return 0;
1897
1898error_1:
1899 free_mmu_pages(vcpu);
1900 return -ENOMEM;
1901}
1902
Ingo Molnar8018c272006-12-29 16:50:01 -08001903int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001904{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001905 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001906 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001907
Ingo Molnar8018c272006-12-29 16:50:01 -08001908 return alloc_mmu_pages(vcpu);
1909}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001910
Ingo Molnar8018c272006-12-29 16:50:01 -08001911int kvm_mmu_setup(struct kvm_vcpu *vcpu)
1912{
1913 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001914 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08001915
Ingo Molnar8018c272006-12-29 16:50:01 -08001916 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001917}
1918
1919void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
1920{
1921 ASSERT(vcpu);
1922
1923 destroy_kvm_mmu(vcpu);
1924 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001925 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001926}
1927
Avi Kivity90cb0522007-07-17 13:04:56 +03001928void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001929{
Avi Kivity4db35312007-11-21 15:28:32 +02001930 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001931
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001932 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001933 int i;
1934 u64 *pt;
1935
Avi Kivity4db35312007-11-21 15:28:32 +02001936 if (!test_bit(slot, &sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001937 continue;
1938
Avi Kivity4db35312007-11-21 15:28:32 +02001939 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001940 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1941 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02001942 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001943 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001944 }
1945}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001946
Avi Kivity90cb0522007-07-17 13:04:56 +03001947void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03001948{
Avi Kivity4db35312007-11-21 15:28:32 +02001949 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03001950
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001951 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001952 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Avi Kivity4db35312007-11-21 15:28:32 +02001953 kvm_mmu_zap_page(kvm, sp);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001954 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03001955
Avi Kivity90cb0522007-07-17 13:04:56 +03001956 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03001957}
1958
Avi Kivityb5a33a72007-04-15 16:31:09 +03001959void kvm_mmu_module_exit(void)
1960{
1961 if (pte_chain_cache)
1962 kmem_cache_destroy(pte_chain_cache);
1963 if (rmap_desc_cache)
1964 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03001965 if (mmu_page_header_cache)
1966 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001967}
1968
1969int kvm_mmu_module_init(void)
1970{
1971 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
1972 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09001973 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001974 if (!pte_chain_cache)
1975 goto nomem;
1976 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
1977 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09001978 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001979 if (!rmap_desc_cache)
1980 goto nomem;
1981
Avi Kivityd3d25b02007-05-30 12:34:53 +03001982 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
1983 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09001984 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03001985 if (!mmu_page_header_cache)
1986 goto nomem;
1987
Avi Kivityb5a33a72007-04-15 16:31:09 +03001988 return 0;
1989
1990nomem:
1991 kvm_mmu_module_exit();
1992 return -ENOMEM;
1993}
1994
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08001995/*
1996 * Caculate mmu pages needed for kvm.
1997 */
1998unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
1999{
2000 int i;
2001 unsigned int nr_mmu_pages;
2002 unsigned int nr_pages = 0;
2003
2004 for (i = 0; i < kvm->nmemslots; i++)
2005 nr_pages += kvm->memslots[i].npages;
2006
2007 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
2008 nr_mmu_pages = max(nr_mmu_pages,
2009 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
2010
2011 return nr_mmu_pages;
2012}
2013
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -05002014static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2015 unsigned len)
2016{
2017 if (len > buffer->len)
2018 return NULL;
2019 return buffer->ptr;
2020}
2021
2022static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
2023 unsigned len)
2024{
2025 void *ret;
2026
2027 ret = pv_mmu_peek_buffer(buffer, len);
2028 if (!ret)
2029 return ret;
2030 buffer->ptr += len;
2031 buffer->len -= len;
2032 buffer->processed += len;
2033 return ret;
2034}
2035
2036static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
2037 gpa_t addr, gpa_t value)
2038{
2039 int bytes = 8;
2040 int r;
2041
2042 if (!is_long_mode(vcpu) && !is_pae(vcpu))
2043 bytes = 4;
2044
2045 r = mmu_topup_memory_caches(vcpu);
2046 if (r)
2047 return r;
2048
2049 if (!__emulator_write_phys(vcpu, addr, &value, bytes))
2050 return -EFAULT;
2051
2052 return 1;
2053}
2054
2055static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
2056{
2057 kvm_x86_ops->tlb_flush(vcpu);
2058 return 1;
2059}
2060
2061static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
2062{
2063 spin_lock(&vcpu->kvm->mmu_lock);
2064 mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
2065 spin_unlock(&vcpu->kvm->mmu_lock);
2066 return 1;
2067}
2068
2069static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
2070 struct kvm_pv_mmu_op_buffer *buffer)
2071{
2072 struct kvm_mmu_op_header *header;
2073
2074 header = pv_mmu_peek_buffer(buffer, sizeof *header);
2075 if (!header)
2076 return 0;
2077 switch (header->op) {
2078 case KVM_MMU_OP_WRITE_PTE: {
2079 struct kvm_mmu_op_write_pte *wpte;
2080
2081 wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
2082 if (!wpte)
2083 return 0;
2084 return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
2085 wpte->pte_val);
2086 }
2087 case KVM_MMU_OP_FLUSH_TLB: {
2088 struct kvm_mmu_op_flush_tlb *ftlb;
2089
2090 ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
2091 if (!ftlb)
2092 return 0;
2093 return kvm_pv_mmu_flush_tlb(vcpu);
2094 }
2095 case KVM_MMU_OP_RELEASE_PT: {
2096 struct kvm_mmu_op_release_pt *rpt;
2097
2098 rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
2099 if (!rpt)
2100 return 0;
2101 return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
2102 }
2103 default: return 0;
2104 }
2105}
2106
2107int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
2108 gpa_t addr, unsigned long *ret)
2109{
2110 int r;
2111 struct kvm_pv_mmu_op_buffer buffer;
2112
2113 down_read(&vcpu->kvm->slots_lock);
2114 down_read(&current->mm->mmap_sem);
2115
2116 buffer.ptr = buffer.buf;
2117 buffer.len = min_t(unsigned long, bytes, sizeof buffer.buf);
2118 buffer.processed = 0;
2119
2120 r = kvm_read_guest(vcpu->kvm, addr, buffer.buf, buffer.len);
2121 if (r)
2122 goto out;
2123
2124 while (buffer.len) {
2125 r = kvm_pv_mmu_op_one(vcpu, &buffer);
2126 if (r < 0)
2127 goto out;
2128 if (r == 0)
2129 break;
2130 }
2131
2132 r = 1;
2133out:
2134 *ret = buffer.processed;
2135 up_read(&current->mm->mmap_sem);
2136 up_read(&vcpu->kvm->slots_lock);
2137 return r;
2138}
2139
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002140#ifdef AUDIT
2141
2142static const char *audit_msg;
2143
2144static gva_t canonicalize(gva_t gva)
2145{
2146#ifdef CONFIG_X86_64
2147 gva = (long long)(gva << 16) >> 16;
2148#endif
2149 return gva;
2150}
2151
2152static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
2153 gva_t va, int level)
2154{
2155 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
2156 int i;
2157 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
2158
2159 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
2160 u64 ent = pt[i];
2161
Avi Kivityc7addb92007-09-16 18:58:32 +02002162 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002163 continue;
2164
2165 va = canonicalize(va);
Avi Kivityc7addb92007-09-16 18:58:32 +02002166 if (level > 1) {
2167 if (ent == shadow_notrap_nonpresent_pte)
2168 printk(KERN_ERR "audit: (%s) nontrapping pte"
2169 " in nonleaf level: levels %d gva %lx"
2170 " level %d pte %llx\n", audit_msg,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002171 vcpu->arch.mmu.root_level, va, level, ent);
Avi Kivityc7addb92007-09-16 18:58:32 +02002172
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002173 audit_mappings_page(vcpu, ent, va, level - 1);
Avi Kivityc7addb92007-09-16 18:58:32 +02002174 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002175 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
Avi Kivity1d28f5f2007-11-21 15:01:44 +02002176 struct page *page = gpa_to_page(vcpu, gpa);
2177 hpa_t hpa = page_to_phys(page);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002178
Avi Kivityc7addb92007-09-16 18:58:32 +02002179 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002180 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02002181 printk(KERN_ERR "xx audit error: (%s) levels %d"
2182 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002183 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04002184 va, gpa, hpa, ent,
2185 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02002186 else if (ent == shadow_notrap_nonpresent_pte
2187 && !is_error_hpa(hpa))
2188 printk(KERN_ERR "audit: (%s) notrap shadow,"
2189 " valid guest gva %lx\n", audit_msg, va);
Izik Eidusb4231d62007-11-20 11:49:33 +02002190 kvm_release_page_clean(page);
Avi Kivityc7addb92007-09-16 18:58:32 +02002191
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002192 }
2193 }
2194}
2195
2196static void audit_mappings(struct kvm_vcpu *vcpu)
2197{
Avi Kivity1ea252a2007-03-08 11:48:09 +02002198 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002199
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002200 if (vcpu->arch.mmu.root_level == 4)
2201 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002202 else
2203 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002204 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002205 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002206 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002207 i << 30,
2208 2);
2209}
2210
2211static int count_rmaps(struct kvm_vcpu *vcpu)
2212{
2213 int nmaps = 0;
2214 int i, j, k;
2215
2216 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
2217 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
2218 struct kvm_rmap_desc *d;
2219
2220 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02002221 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002222
Izik Eidus290fc382007-09-27 14:11:22 +02002223 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002224 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02002225 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002226 ++nmaps;
2227 continue;
2228 }
Izik Eidus290fc382007-09-27 14:11:22 +02002229 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002230 while (d) {
2231 for (k = 0; k < RMAP_EXT; ++k)
2232 if (d->shadow_ptes[k])
2233 ++nmaps;
2234 else
2235 break;
2236 d = d->more;
2237 }
2238 }
2239 }
2240 return nmaps;
2241}
2242
2243static int count_writable_mappings(struct kvm_vcpu *vcpu)
2244{
2245 int nmaps = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02002246 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002247 int i;
2248
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002249 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02002250 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002251
Avi Kivity4db35312007-11-21 15:28:32 +02002252 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002253 continue;
2254
2255 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
2256 u64 ent = pt[i];
2257
2258 if (!(ent & PT_PRESENT_MASK))
2259 continue;
2260 if (!(ent & PT_WRITABLE_MASK))
2261 continue;
2262 ++nmaps;
2263 }
2264 }
2265 return nmaps;
2266}
2267
2268static void audit_rmap(struct kvm_vcpu *vcpu)
2269{
2270 int n_rmap = count_rmaps(vcpu);
2271 int n_actual = count_writable_mappings(vcpu);
2272
2273 if (n_rmap != n_actual)
2274 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002275 __func__, audit_msg, n_rmap, n_actual);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002276}
2277
2278static void audit_write_protection(struct kvm_vcpu *vcpu)
2279{
Avi Kivity4db35312007-11-21 15:28:32 +02002280 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02002281 struct kvm_memory_slot *slot;
2282 unsigned long *rmapp;
2283 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002284
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08002285 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02002286 if (sp->role.metaphysical)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002287 continue;
2288
Avi Kivity4db35312007-11-21 15:28:32 +02002289 slot = gfn_to_memslot(vcpu->kvm, sp->gfn);
2290 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02002291 rmapp = &slot->rmap[gfn - slot->base_gfn];
2292 if (*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002293 printk(KERN_ERR "%s: (%s) shadow page has writable"
2294 " mappings: gfn %lx role %x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002295 __func__, audit_msg, sp->gfn,
Avi Kivity4db35312007-11-21 15:28:32 +02002296 sp->role.word);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08002297 }
2298}
2299
2300static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
2301{
2302 int olddbg = dbg;
2303
2304 dbg = 0;
2305 audit_msg = msg;
2306 audit_rmap(vcpu);
2307 audit_write_protection(vcpu);
2308 audit_mappings(vcpu);
2309 dbg = olddbg;
2310}
2311
2312#endif