blob: c478ee25de6606ddee52e11c88dd612cfc47b484 [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>
Avi Kivitye4956062007-06-28 14:15:57 -040030
31#include <asm/page.h>
32#include <asm/cmpxchg.h>
Avi Kivity4e542372007-11-21 14:08:40 +020033#include <asm/io.h>
Avi Kivitye4956062007-06-28 14:15:57 -040034
Avi Kivity37a7d8b2007-01-05 16:36:56 -080035#undef MMU_DEBUG
36
37#undef AUDIT
38
39#ifdef AUDIT
40static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
41#else
42static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
43#endif
44
45#ifdef MMU_DEBUG
46
47#define pgprintk(x...) do { if (dbg) printk(x); } while (0)
48#define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
49
50#else
51
52#define pgprintk(x...) do { } while (0)
53#define rmap_printk(x...) do { } while (0)
54
55#endif
56
57#if defined(MMU_DEBUG) || defined(AUDIT)
58static int dbg = 1;
59#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080060
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080061#ifndef MMU_DEBUG
62#define ASSERT(x) do { } while (0)
63#else
Avi Kivity6aa8b732006-12-10 02:21:36 -080064#define ASSERT(x) \
65 if (!(x)) { \
66 printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
67 __FILE__, __LINE__, #x); \
68 }
Yaozu Dongd6c69ee2007-04-25 14:17:25 +080069#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080070
Avi Kivitycea0f0e2007-01-05 16:36:43 -080071#define PT64_PT_BITS 9
72#define PT64_ENT_PER_PAGE (1 << PT64_PT_BITS)
73#define PT32_PT_BITS 10
74#define PT32_ENT_PER_PAGE (1 << PT32_PT_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -080075
76#define PT_WRITABLE_SHIFT 1
77
78#define PT_PRESENT_MASK (1ULL << 0)
79#define PT_WRITABLE_MASK (1ULL << PT_WRITABLE_SHIFT)
80#define PT_USER_MASK (1ULL << 2)
81#define PT_PWT_MASK (1ULL << 3)
82#define PT_PCD_MASK (1ULL << 4)
83#define PT_ACCESSED_MASK (1ULL << 5)
84#define PT_DIRTY_MASK (1ULL << 6)
85#define PT_PAGE_SIZE_MASK (1ULL << 7)
86#define PT_PAT_MASK (1ULL << 7)
87#define PT_GLOBAL_MASK (1ULL << 8)
Avi Kivityfe135d22007-12-09 16:15:46 +020088#define PT64_NX_SHIFT 63
89#define PT64_NX_MASK (1ULL << PT64_NX_SHIFT)
Avi Kivity6aa8b732006-12-10 02:21:36 -080090
91#define PT_PAT_SHIFT 7
92#define PT_DIR_PAT_SHIFT 12
93#define PT_DIR_PAT_MASK (1ULL << PT_DIR_PAT_SHIFT)
94
95#define PT32_DIR_PSE36_SIZE 4
96#define PT32_DIR_PSE36_SHIFT 13
Mike Dayd77c26f2007-10-08 09:02:08 -040097#define PT32_DIR_PSE36_MASK \
98 (((1ULL << PT32_DIR_PSE36_SIZE) - 1) << PT32_DIR_PSE36_SHIFT)
Avi Kivity6aa8b732006-12-10 02:21:36 -080099
100
Avi Kivity6aa8b732006-12-10 02:21:36 -0800101#define PT_FIRST_AVAIL_BITS_SHIFT 9
102#define PT64_SECOND_AVAIL_BITS_SHIFT 52
103
Avi Kivity6aa8b732006-12-10 02:21:36 -0800104#define PT_SHADOW_IO_MARK (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
105
Avi Kivity6aa8b732006-12-10 02:21:36 -0800106#define VALID_PAGE(x) ((x) != INVALID_PAGE)
107
108#define PT64_LEVEL_BITS 9
109
110#define PT64_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400111 (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800112
113#define PT64_LEVEL_MASK(level) \
114 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
115
116#define PT64_INDEX(address, level)\
117 (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
118
119
120#define PT32_LEVEL_BITS 10
121
122#define PT32_LEVEL_SHIFT(level) \
Mike Dayd77c26f2007-10-08 09:02:08 -0400123 (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800124
125#define PT32_LEVEL_MASK(level) \
126 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
127
128#define PT32_INDEX(address, level)\
129 (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
130
131
Avi Kivity27aba762007-03-09 13:04:31 +0200132#define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800133#define PT64_DIR_BASE_ADDR_MASK \
134 (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
135
136#define PT32_BASE_ADDR_MASK PAGE_MASK
137#define PT32_DIR_BASE_ADDR_MASK \
138 (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
139
Avi Kivity79539ce2007-11-21 02:06:21 +0200140#define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
141 | PT64_NX_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800142
143#define PFERR_PRESENT_MASK (1U << 0)
144#define PFERR_WRITE_MASK (1U << 1)
145#define PFERR_USER_MASK (1U << 2)
Avi Kivity73b10872007-01-26 00:56:41 -0800146#define PFERR_FETCH_MASK (1U << 4)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800147
148#define PT64_ROOT_LEVEL 4
149#define PT32_ROOT_LEVEL 2
150#define PT32E_ROOT_LEVEL 3
151
152#define PT_DIRECTORY_LEVEL 2
153#define PT_PAGE_TABLE_LEVEL 1
154
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800155#define RMAP_EXT 4
156
Avi Kivityfe135d22007-12-09 16:15:46 +0200157#define ACC_EXEC_MASK 1
158#define ACC_WRITE_MASK PT_WRITABLE_MASK
159#define ACC_USER_MASK PT_USER_MASK
160#define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
161
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800162struct kvm_rmap_desc {
163 u64 *shadow_ptes[RMAP_EXT];
164 struct kvm_rmap_desc *more;
165};
166
Avi Kivityb5a33a72007-04-15 16:31:09 +0300167static struct kmem_cache *pte_chain_cache;
168static struct kmem_cache *rmap_desc_cache;
Avi Kivityd3d25b02007-05-30 12:34:53 +0300169static struct kmem_cache *mmu_page_header_cache;
Avi Kivityb5a33a72007-04-15 16:31:09 +0300170
Avi Kivityc7addb92007-09-16 18:58:32 +0200171static u64 __read_mostly shadow_trap_nonpresent_pte;
172static u64 __read_mostly shadow_notrap_nonpresent_pte;
173
174void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
175{
176 shadow_trap_nonpresent_pte = trap_pte;
177 shadow_notrap_nonpresent_pte = notrap_pte;
178}
179EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
180
Avi Kivity6aa8b732006-12-10 02:21:36 -0800181static int is_write_protection(struct kvm_vcpu *vcpu)
182{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800183 return vcpu->arch.cr0 & X86_CR0_WP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800184}
185
186static int is_cpuid_PSE36(void)
187{
188 return 1;
189}
190
Avi Kivity73b10872007-01-26 00:56:41 -0800191static int is_nx(struct kvm_vcpu *vcpu)
192{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800193 return vcpu->arch.shadow_efer & EFER_NX;
Avi Kivity73b10872007-01-26 00:56:41 -0800194}
195
Avi Kivity6aa8b732006-12-10 02:21:36 -0800196static int is_present_pte(unsigned long pte)
197{
198 return pte & PT_PRESENT_MASK;
199}
200
Avi Kivityc7addb92007-09-16 18:58:32 +0200201static int is_shadow_present_pte(u64 pte)
202{
203 pte &= ~PT_SHADOW_IO_MARK;
204 return pte != shadow_trap_nonpresent_pte
205 && pte != shadow_notrap_nonpresent_pte;
206}
207
Avi Kivity6aa8b732006-12-10 02:21:36 -0800208static int is_writeble_pte(unsigned long pte)
209{
210 return pte & PT_WRITABLE_MASK;
211}
212
Avi Kivitye3c5e7ec2007-10-11 12:32:30 +0200213static int is_dirty_pte(unsigned long pte)
214{
215 return pte & PT_DIRTY_MASK;
216}
217
Avi Kivity6aa8b732006-12-10 02:21:36 -0800218static int is_io_pte(unsigned long pte)
219{
220 return pte & PT_SHADOW_IO_MARK;
221}
222
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800223static int is_rmap_pte(u64 pte)
224{
Izik Eidus9647c142007-10-16 14:43:46 +0200225 return pte != shadow_trap_nonpresent_pte
226 && pte != shadow_notrap_nonpresent_pte;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800227}
228
Avi Kivityda928522007-11-21 13:54:47 +0200229static gfn_t pse36_gfn_delta(u32 gpte)
230{
231 int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
232
233 return (gpte & PT32_DIR_PSE36_MASK) << shift;
234}
235
Avi Kivitye663ee62007-05-31 15:46:04 +0300236static void set_shadow_pte(u64 *sptep, u64 spte)
237{
238#ifdef CONFIG_X86_64
239 set_64bit((unsigned long *)sptep, spte);
240#else
241 set_64bit((unsigned long long *)sptep, spte);
242#endif
243}
244
Avi Kivitye2dec932007-01-05 16:36:54 -0800245static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300246 struct kmem_cache *base_cache, int min)
Avi Kivity714b93d2007-01-05 16:36:53 -0800247{
248 void *obj;
249
250 if (cache->nobjs >= min)
Avi Kivitye2dec932007-01-05 16:36:54 -0800251 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800252 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300253 obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
Avi Kivity714b93d2007-01-05 16:36:53 -0800254 if (!obj)
Avi Kivitye2dec932007-01-05 16:36:54 -0800255 return -ENOMEM;
Avi Kivity714b93d2007-01-05 16:36:53 -0800256 cache->objects[cache->nobjs++] = obj;
257 }
Avi Kivitye2dec932007-01-05 16:36:54 -0800258 return 0;
Avi Kivity714b93d2007-01-05 16:36:53 -0800259}
260
261static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
262{
263 while (mc->nobjs)
264 kfree(mc->objects[--mc->nobjs]);
265}
266
Avi Kivityc1158e62007-07-20 08:18:27 +0300267static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300268 int min)
Avi Kivityc1158e62007-07-20 08:18:27 +0300269{
270 struct page *page;
271
272 if (cache->nobjs >= min)
273 return 0;
274 while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
Avi Kivity2e3e5882007-09-10 11:28:17 +0300275 page = alloc_page(GFP_KERNEL);
Avi Kivityc1158e62007-07-20 08:18:27 +0300276 if (!page)
277 return -ENOMEM;
278 set_page_private(page, 0);
279 cache->objects[cache->nobjs++] = page_address(page);
280 }
281 return 0;
282}
283
284static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
285{
286 while (mc->nobjs)
Avi Kivityc4d198d2007-07-21 09:06:46 +0300287 free_page((unsigned long)mc->objects[--mc->nobjs]);
Avi Kivityc1158e62007-07-20 08:18:27 +0300288}
289
Avi Kivity8c438502007-04-16 11:53:17 +0300290static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
291{
292 int r;
293
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800294 r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300295 pte_chain_cache, 4);
296 if (r)
297 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800298 r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300299 rmap_desc_cache, 1);
300 if (r)
301 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800302 r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
Avi Kivity2e3e5882007-09-10 11:28:17 +0300303 if (r)
304 goto out;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800305 r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
Avi Kivity2e3e5882007-09-10 11:28:17 +0300306 mmu_page_header_cache, 4);
307out:
Avi Kivity8c438502007-04-16 11:53:17 +0300308 return r;
309}
310
Avi Kivity714b93d2007-01-05 16:36:53 -0800311static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
312{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800313 mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache);
314 mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache);
315 mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
316 mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache);
Avi Kivity714b93d2007-01-05 16:36:53 -0800317}
318
319static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
320 size_t size)
321{
322 void *p;
323
324 BUG_ON(!mc->nobjs);
325 p = mc->objects[--mc->nobjs];
326 memset(p, 0, size);
327 return p;
328}
329
Avi Kivity714b93d2007-01-05 16:36:53 -0800330static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
331{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800332 return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800333 sizeof(struct kvm_pte_chain));
334}
335
Avi Kivity90cb0522007-07-17 13:04:56 +0300336static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
Avi Kivity714b93d2007-01-05 16:36:53 -0800337{
Avi Kivity90cb0522007-07-17 13:04:56 +0300338 kfree(pc);
Avi Kivity714b93d2007-01-05 16:36:53 -0800339}
340
341static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
342{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800343 return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
Avi Kivity714b93d2007-01-05 16:36:53 -0800344 sizeof(struct kvm_rmap_desc));
345}
346
Avi Kivity90cb0522007-07-17 13:04:56 +0300347static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
Avi Kivity714b93d2007-01-05 16:36:53 -0800348{
Avi Kivity90cb0522007-07-17 13:04:56 +0300349 kfree(rd);
Avi Kivity714b93d2007-01-05 16:36:53 -0800350}
351
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800352/*
Izik Eidus290fc382007-09-27 14:11:22 +0200353 * Take gfn and return the reverse mapping to it.
354 * Note: gfn must be unaliased before this function get called
355 */
356
357static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn)
358{
359 struct kvm_memory_slot *slot;
360
361 slot = gfn_to_memslot(kvm, gfn);
362 return &slot->rmap[gfn - slot->base_gfn];
363}
364
365/*
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800366 * Reverse mapping data structures:
367 *
Izik Eidus290fc382007-09-27 14:11:22 +0200368 * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
369 * that points to page_address(page).
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800370 *
Izik Eidus290fc382007-09-27 14:11:22 +0200371 * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
372 * containing more mappings.
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800373 */
Izik Eidus290fc382007-09-27 14:11:22 +0200374static void rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800375{
Avi Kivity4db35312007-11-21 15:28:32 +0200376 struct kvm_mmu_page *sp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800377 struct kvm_rmap_desc *desc;
Izik Eidus290fc382007-09-27 14:11:22 +0200378 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800379 int i;
380
381 if (!is_rmap_pte(*spte))
382 return;
Izik Eidus290fc382007-09-27 14:11:22 +0200383 gfn = unalias_gfn(vcpu->kvm, gfn);
Avi Kivity4db35312007-11-21 15:28:32 +0200384 sp = page_header(__pa(spte));
385 sp->gfns[spte - sp->spt] = gfn;
Izik Eidus290fc382007-09-27 14:11:22 +0200386 rmapp = gfn_to_rmap(vcpu->kvm, gfn);
387 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800388 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200389 *rmapp = (unsigned long)spte;
390 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800391 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
Avi Kivity714b93d2007-01-05 16:36:53 -0800392 desc = mmu_alloc_rmap_desc(vcpu);
Izik Eidus290fc382007-09-27 14:11:22 +0200393 desc->shadow_ptes[0] = (u64 *)*rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800394 desc->shadow_ptes[1] = spte;
Izik Eidus290fc382007-09-27 14:11:22 +0200395 *rmapp = (unsigned long)desc | 1;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800396 } else {
397 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200398 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800399 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
400 desc = desc->more;
401 if (desc->shadow_ptes[RMAP_EXT-1]) {
Avi Kivity714b93d2007-01-05 16:36:53 -0800402 desc->more = mmu_alloc_rmap_desc(vcpu);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800403 desc = desc->more;
404 }
405 for (i = 0; desc->shadow_ptes[i]; ++i)
406 ;
407 desc->shadow_ptes[i] = spte;
408 }
409}
410
Izik Eidus290fc382007-09-27 14:11:22 +0200411static void rmap_desc_remove_entry(unsigned long *rmapp,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800412 struct kvm_rmap_desc *desc,
413 int i,
414 struct kvm_rmap_desc *prev_desc)
415{
416 int j;
417
418 for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
419 ;
420 desc->shadow_ptes[i] = desc->shadow_ptes[j];
Al Viro11718b4d2007-02-09 16:39:20 +0000421 desc->shadow_ptes[j] = NULL;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800422 if (j != 0)
423 return;
424 if (!prev_desc && !desc->more)
Izik Eidus290fc382007-09-27 14:11:22 +0200425 *rmapp = (unsigned long)desc->shadow_ptes[0];
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800426 else
427 if (prev_desc)
428 prev_desc->more = desc->more;
429 else
Izik Eidus290fc382007-09-27 14:11:22 +0200430 *rmapp = (unsigned long)desc->more | 1;
Avi Kivity90cb0522007-07-17 13:04:56 +0300431 mmu_free_rmap_desc(desc);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800432}
433
Izik Eidus290fc382007-09-27 14:11:22 +0200434static void rmap_remove(struct kvm *kvm, u64 *spte)
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800435{
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800436 struct kvm_rmap_desc *desc;
437 struct kvm_rmap_desc *prev_desc;
Avi Kivity4db35312007-11-21 15:28:32 +0200438 struct kvm_mmu_page *sp;
Avi Kivity76c35c62007-11-21 15:32:41 +0200439 struct page *page;
Izik Eidus290fc382007-09-27 14:11:22 +0200440 unsigned long *rmapp;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800441 int i;
442
443 if (!is_rmap_pte(*spte))
444 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200445 sp = page_header(__pa(spte));
Avi Kivity76c35c62007-11-21 15:32:41 +0200446 page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT);
Izik Eidus448353c2007-11-26 14:08:14 +0200447 mark_page_accessed(page);
Izik Eidusb4231d62007-11-20 11:49:33 +0200448 if (is_writeble_pte(*spte))
Avi Kivity76c35c62007-11-21 15:32:41 +0200449 kvm_release_page_dirty(page);
Izik Eidusb4231d62007-11-20 11:49:33 +0200450 else
Avi Kivity76c35c62007-11-21 15:32:41 +0200451 kvm_release_page_clean(page);
Avi Kivity4db35312007-11-21 15:28:32 +0200452 rmapp = gfn_to_rmap(kvm, sp->gfns[spte - sp->spt]);
Izik Eidus290fc382007-09-27 14:11:22 +0200453 if (!*rmapp) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800454 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
455 BUG();
Izik Eidus290fc382007-09-27 14:11:22 +0200456 } else if (!(*rmapp & 1)) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800457 rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200458 if ((u64 *)*rmapp != spte) {
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800459 printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
460 spte, *spte);
461 BUG();
462 }
Izik Eidus290fc382007-09-27 14:11:22 +0200463 *rmapp = 0;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800464 } else {
465 rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
Izik Eidus290fc382007-09-27 14:11:22 +0200466 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800467 prev_desc = NULL;
468 while (desc) {
469 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
470 if (desc->shadow_ptes[i] == spte) {
Izik Eidus290fc382007-09-27 14:11:22 +0200471 rmap_desc_remove_entry(rmapp,
Avi Kivity714b93d2007-01-05 16:36:53 -0800472 desc, i,
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800473 prev_desc);
474 return;
475 }
476 prev_desc = desc;
477 desc = desc->more;
478 }
479 BUG();
480 }
481}
482
Izik Eidus98348e92007-10-16 14:42:30 +0200483static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
Avi Kivity374cbac2007-01-05 16:36:43 -0800484{
Avi Kivity374cbac2007-01-05 16:36:43 -0800485 struct kvm_rmap_desc *desc;
Izik Eidus98348e92007-10-16 14:42:30 +0200486 struct kvm_rmap_desc *prev_desc;
487 u64 *prev_spte;
488 int i;
489
490 if (!*rmapp)
491 return NULL;
492 else if (!(*rmapp & 1)) {
493 if (!spte)
494 return (u64 *)*rmapp;
495 return NULL;
496 }
497 desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
498 prev_desc = NULL;
499 prev_spte = NULL;
500 while (desc) {
501 for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i) {
502 if (prev_spte == spte)
503 return desc->shadow_ptes[i];
504 prev_spte = desc->shadow_ptes[i];
505 }
506 desc = desc->more;
507 }
508 return NULL;
509}
510
511static void rmap_write_protect(struct kvm *kvm, u64 gfn)
512{
Izik Eidus290fc382007-09-27 14:11:22 +0200513 unsigned long *rmapp;
Avi Kivity374cbac2007-01-05 16:36:43 -0800514 u64 *spte;
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800515 int write_protected = 0;
Avi Kivity374cbac2007-01-05 16:36:43 -0800516
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500517 gfn = unalias_gfn(kvm, gfn);
518 rmapp = gfn_to_rmap(kvm, gfn);
Avi Kivity374cbac2007-01-05 16:36:43 -0800519
Izik Eidus98348e92007-10-16 14:42:30 +0200520 spte = rmap_next(kvm, rmapp, NULL);
521 while (spte) {
Avi Kivity374cbac2007-01-05 16:36:43 -0800522 BUG_ON(!spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800523 BUG_ON(!(*spte & PT_PRESENT_MASK));
Avi Kivity374cbac2007-01-05 16:36:43 -0800524 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800525 if (is_writeble_pte(*spte)) {
Izik Eidus9647c142007-10-16 14:43:46 +0200526 set_shadow_pte(spte, *spte & ~PT_WRITABLE_MASK);
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800527 write_protected = 1;
528 }
Izik Eidus9647c142007-10-16 14:43:46 +0200529 spte = rmap_next(kvm, rmapp, spte);
Avi Kivity374cbac2007-01-05 16:36:43 -0800530 }
Eddie Dongcaa5b8a2007-12-18 06:08:27 +0800531 if (write_protected)
532 kvm_flush_remote_tlbs(kvm);
Avi Kivity374cbac2007-01-05 16:36:43 -0800533}
534
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800535#ifdef MMU_DEBUG
Avi Kivity47ad8e62007-05-06 15:50:58 +0300536static int is_empty_shadow_page(u64 *spt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800537{
Avi Kivity139bdb22007-01-05 16:36:50 -0800538 u64 *pos;
539 u64 *end;
540
Avi Kivity47ad8e62007-05-06 15:50:58 +0300541 for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
Avi Kivityc7addb92007-09-16 18:58:32 +0200542 if ((*pos & ~PT_SHADOW_IO_MARK) != shadow_trap_nonpresent_pte) {
Avi Kivity139bdb22007-01-05 16:36:50 -0800543 printk(KERN_ERR "%s: %p %llx\n", __FUNCTION__,
544 pos, *pos);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800545 return 0;
Avi Kivity139bdb22007-01-05 16:36:50 -0800546 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800547 return 1;
548}
Yaozu Dongd6c69ee2007-04-25 14:17:25 +0800549#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800550
Avi Kivity4db35312007-11-21 15:28:32 +0200551static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivity260746c2007-01-05 16:36:49 -0800552{
Avi Kivity4db35312007-11-21 15:28:32 +0200553 ASSERT(is_empty_shadow_page(sp->spt));
554 list_del(&sp->link);
555 __free_page(virt_to_page(sp->spt));
556 __free_page(virt_to_page(sp->gfns));
557 kfree(sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800558 ++kvm->arch.n_free_mmu_pages;
Avi Kivity260746c2007-01-05 16:36:49 -0800559}
560
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800561static unsigned kvm_page_table_hashfn(gfn_t gfn)
562{
563 return gfn;
564}
565
Avi Kivity25c0de22007-01-05 16:36:42 -0800566static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
567 u64 *parent_pte)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800568{
Avi Kivity4db35312007-11-21 15:28:32 +0200569 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800570
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800571 sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
572 sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
573 sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
Avi Kivity4db35312007-11-21 15:28:32 +0200574 set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800575 list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
Avi Kivity4db35312007-11-21 15:28:32 +0200576 ASSERT(is_empty_shadow_page(sp->spt));
577 sp->slot_bitmap = 0;
578 sp->multimapped = 0;
579 sp->parent_pte = parent_pte;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800580 --vcpu->kvm->arch.n_free_mmu_pages;
Avi Kivity4db35312007-11-21 15:28:32 +0200581 return sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800582}
583
Avi Kivity714b93d2007-01-05 16:36:53 -0800584static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +0200585 struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800586{
587 struct kvm_pte_chain *pte_chain;
588 struct hlist_node *node;
589 int i;
590
591 if (!parent_pte)
592 return;
Avi Kivity4db35312007-11-21 15:28:32 +0200593 if (!sp->multimapped) {
594 u64 *old = sp->parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800595
596 if (!old) {
Avi Kivity4db35312007-11-21 15:28:32 +0200597 sp->parent_pte = parent_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800598 return;
599 }
Avi Kivity4db35312007-11-21 15:28:32 +0200600 sp->multimapped = 1;
Avi Kivity714b93d2007-01-05 16:36:53 -0800601 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivity4db35312007-11-21 15:28:32 +0200602 INIT_HLIST_HEAD(&sp->parent_ptes);
603 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800604 pte_chain->parent_ptes[0] = old;
605 }
Avi Kivity4db35312007-11-21 15:28:32 +0200606 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800607 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
608 continue;
609 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
610 if (!pte_chain->parent_ptes[i]) {
611 pte_chain->parent_ptes[i] = parent_pte;
612 return;
613 }
614 }
Avi Kivity714b93d2007-01-05 16:36:53 -0800615 pte_chain = mmu_alloc_pte_chain(vcpu);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800616 BUG_ON(!pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200617 hlist_add_head(&pte_chain->link, &sp->parent_ptes);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800618 pte_chain->parent_ptes[0] = parent_pte;
619}
620
Avi Kivity4db35312007-11-21 15:28:32 +0200621static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800622 u64 *parent_pte)
623{
624 struct kvm_pte_chain *pte_chain;
625 struct hlist_node *node;
626 int i;
627
Avi Kivity4db35312007-11-21 15:28:32 +0200628 if (!sp->multimapped) {
629 BUG_ON(sp->parent_pte != parent_pte);
630 sp->parent_pte = NULL;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800631 return;
632 }
Avi Kivity4db35312007-11-21 15:28:32 +0200633 hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800634 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
635 if (!pte_chain->parent_ptes[i])
636 break;
637 if (pte_chain->parent_ptes[i] != parent_pte)
638 continue;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800639 while (i + 1 < NR_PTE_CHAIN_ENTRIES
640 && pte_chain->parent_ptes[i + 1]) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800641 pte_chain->parent_ptes[i]
642 = pte_chain->parent_ptes[i + 1];
643 ++i;
644 }
645 pte_chain->parent_ptes[i] = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800646 if (i == 0) {
647 hlist_del(&pte_chain->link);
Avi Kivity90cb0522007-07-17 13:04:56 +0300648 mmu_free_pte_chain(pte_chain);
Avi Kivity4db35312007-11-21 15:28:32 +0200649 if (hlist_empty(&sp->parent_ptes)) {
650 sp->multimapped = 0;
651 sp->parent_pte = NULL;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800652 }
653 }
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800654 return;
655 }
656 BUG();
657}
658
Avi Kivity4db35312007-11-21 15:28:32 +0200659static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800660{
661 unsigned index;
662 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200663 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800664 struct hlist_node *node;
665
666 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
667 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800668 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200669 hlist_for_each_entry(sp, node, bucket, hash_link)
670 if (sp->gfn == gfn && !sp->role.metaphysical) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800671 pgprintk("%s: found role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +0200672 __FUNCTION__, sp->role.word);
673 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800674 }
675 return NULL;
676}
677
678static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
679 gfn_t gfn,
680 gva_t gaddr,
681 unsigned level,
682 int metaphysical,
Avi Kivity41074d02007-12-09 17:00:02 +0200683 unsigned access,
Marcelo Tosatti78190262007-12-11 19:12:27 -0500684 u64 *parent_pte,
685 bool *new_page)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800686{
687 union kvm_mmu_page_role role;
688 unsigned index;
689 unsigned quadrant;
690 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200691 struct kvm_mmu_page *sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800692 struct hlist_node *node;
693
694 role.word = 0;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800695 role.glevels = vcpu->arch.mmu.root_level;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800696 role.level = level;
697 role.metaphysical = metaphysical;
Avi Kivity41074d02007-12-09 17:00:02 +0200698 role.access = access;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800699 if (vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800700 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
701 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
702 role.quadrant = quadrant;
703 }
704 pgprintk("%s: looking gfn %lx role %x\n", __FUNCTION__,
705 gfn, role.word);
706 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800707 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200708 hlist_for_each_entry(sp, node, bucket, hash_link)
709 if (sp->gfn == gfn && sp->role.word == role.word) {
710 mmu_page_add_parent_pte(vcpu, sp, parent_pte);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800711 pgprintk("%s: found\n", __FUNCTION__);
Avi Kivity4db35312007-11-21 15:28:32 +0200712 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800713 }
Avi Kivitydfc5aa02007-12-18 19:47:18 +0200714 ++vcpu->kvm->stat.mmu_cache_miss;
Avi Kivity4db35312007-11-21 15:28:32 +0200715 sp = kvm_mmu_alloc_page(vcpu, parent_pte);
716 if (!sp)
717 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800718 pgprintk("%s: adding gfn %lx role %x\n", __FUNCTION__, gfn, role.word);
Avi Kivity4db35312007-11-21 15:28:32 +0200719 sp->gfn = gfn;
720 sp->role = role;
721 hlist_add_head(&sp->hash_link, bucket);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800722 vcpu->arch.mmu.prefetch_page(vcpu, sp);
Avi Kivity374cbac2007-01-05 16:36:43 -0800723 if (!metaphysical)
Anthony Liguori4a4c9922007-10-10 20:08:41 -0500724 rmap_write_protect(vcpu->kvm, gfn);
Marcelo Tosatti78190262007-12-11 19:12:27 -0500725 if (new_page)
726 *new_page = 1;
Avi Kivity4db35312007-11-21 15:28:32 +0200727 return sp;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800728}
729
Avi Kivity90cb0522007-07-17 13:04:56 +0300730static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Avi Kivity4db35312007-11-21 15:28:32 +0200731 struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -0800732{
Avi Kivity697fe2e2007-01-05 16:36:46 -0800733 unsigned i;
734 u64 *pt;
735 u64 ent;
736
Avi Kivity4db35312007-11-21 15:28:32 +0200737 pt = sp->spt;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800738
Avi Kivity4db35312007-11-21 15:28:32 +0200739 if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800740 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
Avi Kivityc7addb92007-09-16 18:58:32 +0200741 if (is_shadow_present_pte(pt[i]))
Izik Eidus290fc382007-09-27 14:11:22 +0200742 rmap_remove(kvm, &pt[i]);
Avi Kivityc7addb92007-09-16 18:58:32 +0200743 pt[i] = shadow_trap_nonpresent_pte;
Avi Kivity697fe2e2007-01-05 16:36:46 -0800744 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300745 kvm_flush_remote_tlbs(kvm);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800746 return;
747 }
748
749 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
750 ent = pt[i];
751
Avi Kivityc7addb92007-09-16 18:58:32 +0200752 pt[i] = shadow_trap_nonpresent_pte;
753 if (!is_shadow_present_pte(ent))
Avi Kivity697fe2e2007-01-05 16:36:46 -0800754 continue;
755 ent &= PT64_BASE_ADDR_MASK;
Avi Kivity90cb0522007-07-17 13:04:56 +0300756 mmu_page_remove_parent_pte(page_header(ent), &pt[i]);
Avi Kivity697fe2e2007-01-05 16:36:46 -0800757 }
Avi Kivity90cb0522007-07-17 13:04:56 +0300758 kvm_flush_remote_tlbs(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800759}
760
Avi Kivity4db35312007-11-21 15:28:32 +0200761static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800762{
Avi Kivity4db35312007-11-21 15:28:32 +0200763 mmu_page_remove_parent_pte(sp, parent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800764}
765
Avi Kivity12b7d282007-09-23 14:10:49 +0200766static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
767{
768 int i;
769
770 for (i = 0; i < KVM_MAX_VCPUS; ++i)
771 if (kvm->vcpus[i])
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800772 kvm->vcpus[i]->arch.last_pte_updated = NULL;
Avi Kivity12b7d282007-09-23 14:10:49 +0200773}
774
Avi Kivity4db35312007-11-21 15:28:32 +0200775static void kvm_mmu_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Avi Kivitya4360362007-01-05 16:36:45 -0800776{
777 u64 *parent_pte;
778
Avi Kivity4cee5762007-11-18 16:37:07 +0200779 ++kvm->stat.mmu_shadow_zapped;
Avi Kivity4db35312007-11-21 15:28:32 +0200780 while (sp->multimapped || sp->parent_pte) {
781 if (!sp->multimapped)
782 parent_pte = sp->parent_pte;
Avi Kivitya4360362007-01-05 16:36:45 -0800783 else {
784 struct kvm_pte_chain *chain;
785
Avi Kivity4db35312007-11-21 15:28:32 +0200786 chain = container_of(sp->parent_ptes.first,
Avi Kivitya4360362007-01-05 16:36:45 -0800787 struct kvm_pte_chain, link);
788 parent_pte = chain->parent_ptes[0];
789 }
Avi Kivity697fe2e2007-01-05 16:36:46 -0800790 BUG_ON(!parent_pte);
Avi Kivity4db35312007-11-21 15:28:32 +0200791 kvm_mmu_put_page(sp, parent_pte);
Avi Kivityc7addb92007-09-16 18:58:32 +0200792 set_shadow_pte(parent_pte, shadow_trap_nonpresent_pte);
Avi Kivitya4360362007-01-05 16:36:45 -0800793 }
Avi Kivity4db35312007-11-21 15:28:32 +0200794 kvm_mmu_page_unlink_children(kvm, sp);
795 if (!sp->root_count) {
796 hlist_del(&sp->hash_link);
797 kvm_mmu_free_page(kvm, sp);
Avi Kivity36868f72007-03-26 19:31:52 +0200798 } else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800799 list_move(&sp->link, &kvm->arch.active_mmu_pages);
Avi Kivity12b7d282007-09-23 14:10:49 +0200800 kvm_mmu_reset_last_pte_updated(kvm);
Avi Kivitya4360362007-01-05 16:36:45 -0800801}
802
Izik Eidus82ce2c92007-10-02 18:52:55 +0200803/*
804 * Changing the number of mmu pages allocated to the vm
805 * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
806 */
807void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
808{
809 /*
810 * If we set the number of mmu pages to be smaller be than the
811 * number of actived pages , we must to free some mmu pages before we
812 * change the value
813 */
814
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800815 if ((kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages) >
Izik Eidus82ce2c92007-10-02 18:52:55 +0200816 kvm_nr_mmu_pages) {
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800817 int n_used_mmu_pages = kvm->arch.n_alloc_mmu_pages
818 - kvm->arch.n_free_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200819
820 while (n_used_mmu_pages > kvm_nr_mmu_pages) {
821 struct kvm_mmu_page *page;
822
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800823 page = container_of(kvm->arch.active_mmu_pages.prev,
Izik Eidus82ce2c92007-10-02 18:52:55 +0200824 struct kvm_mmu_page, link);
825 kvm_mmu_zap_page(kvm, page);
826 n_used_mmu_pages--;
827 }
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800828 kvm->arch.n_free_mmu_pages = 0;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200829 }
830 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800831 kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
832 - kvm->arch.n_alloc_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200833
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800834 kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +0200835}
836
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500837static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
Avi Kivitya4360362007-01-05 16:36:45 -0800838{
839 unsigned index;
840 struct hlist_head *bucket;
Avi Kivity4db35312007-11-21 15:28:32 +0200841 struct kvm_mmu_page *sp;
Avi Kivitya4360362007-01-05 16:36:45 -0800842 struct hlist_node *node, *n;
843 int r;
844
845 pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
846 r = 0;
847 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +0800848 bucket = &kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +0200849 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link)
850 if (sp->gfn == gfn && !sp->role.metaphysical) {
Avi Kivity697fe2e2007-01-05 16:36:46 -0800851 pgprintk("%s: gfn %lx role %x\n", __FUNCTION__, gfn,
Avi Kivity4db35312007-11-21 15:28:32 +0200852 sp->role.word);
853 kvm_mmu_zap_page(kvm, sp);
Avi Kivitya4360362007-01-05 16:36:45 -0800854 r = 1;
855 }
856 return r;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800857}
858
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500859static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
Avi Kivity97a0a012007-05-31 15:08:29 +0300860{
Avi Kivity4db35312007-11-21 15:28:32 +0200861 struct kvm_mmu_page *sp;
Avi Kivity97a0a012007-05-31 15:08:29 +0300862
Avi Kivity4db35312007-11-21 15:28:32 +0200863 while ((sp = kvm_mmu_lookup_page(kvm, gfn)) != NULL) {
864 pgprintk("%s: zap %lx %x\n", __FUNCTION__, gfn, sp->role.word);
865 kvm_mmu_zap_page(kvm, sp);
Avi Kivity97a0a012007-05-31 15:08:29 +0300866 }
867}
868
Avi Kivity38c335f2007-11-21 14:20:22 +0200869static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800870{
Avi Kivity38c335f2007-11-21 14:20:22 +0200871 int slot = memslot_id(kvm, gfn_to_memslot(kvm, gfn));
Avi Kivity4db35312007-11-21 15:28:32 +0200872 struct kvm_mmu_page *sp = page_header(__pa(pte));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800873
Avi Kivity4db35312007-11-21 15:28:32 +0200874 __set_bit(slot, &sp->slot_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800875}
876
Avi Kivity039576c2007-03-20 12:46:50 +0200877struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva)
878{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800879 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
Avi Kivity039576c2007-03-20 12:46:50 +0200880
881 if (gpa == UNMAPPED_GVA)
882 return NULL;
Avi Kivity1d28f5f2007-11-21 15:01:44 +0200883 return gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Avi Kivity039576c2007-03-20 12:46:50 +0200884}
885
Avi Kivity1c4f1fd2007-12-09 17:40:31 +0200886static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
887 unsigned pt_access, unsigned pte_access,
888 int user_fault, int write_fault, int dirty,
Avi Kivityd7824ff2007-12-30 12:29:05 +0200889 int *ptwrite, gfn_t gfn, struct page *page)
Avi Kivity1c4f1fd2007-12-09 17:40:31 +0200890{
891 u64 spte;
892 int was_rmapped = is_rmap_pte(*shadow_pte);
Avi Kivity1c4f1fd2007-12-09 17:40:31 +0200893
Avi Kivitybc750ba2007-12-09 18:39:41 +0200894 pgprintk("%s: spte %llx access %x write_fault %d"
Avi Kivity1c4f1fd2007-12-09 17:40:31 +0200895 " user_fault %d gfn %lx\n",
Avi Kivitybc750ba2007-12-09 18:39:41 +0200896 __FUNCTION__, *shadow_pte, pt_access,
Avi Kivity1c4f1fd2007-12-09 17:40:31 +0200897 write_fault, user_fault, gfn);
898
899 /*
900 * We don't set the accessed bit, since we sometimes want to see
901 * whether the guest actually used the pte (in order to detect
902 * demand paging).
903 */
904 spte = PT_PRESENT_MASK | PT_DIRTY_MASK;
905 if (!dirty)
906 pte_access &= ~ACC_WRITE_MASK;
907 if (!(pte_access & ACC_EXEC_MASK))
908 spte |= PT64_NX_MASK;
909
Avi Kivity1c4f1fd2007-12-09 17:40:31 +0200910 spte |= PT_PRESENT_MASK;
911 if (pte_access & ACC_USER_MASK)
912 spte |= PT_USER_MASK;
913
914 if (is_error_page(page)) {
915 set_shadow_pte(shadow_pte,
916 shadow_trap_nonpresent_pte | PT_SHADOW_IO_MARK);
917 kvm_release_page_clean(page);
918 return;
919 }
920
921 spte |= page_to_phys(page);
922
923 if ((pte_access & ACC_WRITE_MASK)
924 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
925 struct kvm_mmu_page *shadow;
926
927 spte |= PT_WRITABLE_MASK;
928 if (user_fault) {
929 mmu_unshadow(vcpu->kvm, gfn);
930 goto unshadowed;
931 }
932
933 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
934 if (shadow) {
935 pgprintk("%s: found shadow page for %lx, marking ro\n",
936 __FUNCTION__, gfn);
937 pte_access &= ~ACC_WRITE_MASK;
938 if (is_writeble_pte(spte)) {
939 spte &= ~PT_WRITABLE_MASK;
940 kvm_x86_ops->tlb_flush(vcpu);
941 }
942 if (write_fault)
943 *ptwrite = 1;
944 }
945 }
946
947unshadowed:
948
949 if (pte_access & ACC_WRITE_MASK)
950 mark_page_dirty(vcpu->kvm, gfn);
951
952 pgprintk("%s: setting spte %llx\n", __FUNCTION__, spte);
953 set_shadow_pte(shadow_pte, spte);
954 page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
955 if (!was_rmapped) {
956 rmap_add(vcpu, shadow_pte, gfn);
957 if (!is_rmap_pte(*shadow_pte))
958 kvm_release_page_clean(page);
959 }
960 else
961 kvm_release_page_clean(page);
962 if (!ptwrite || !*ptwrite)
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800963 vcpu->arch.last_pte_updated = shadow_pte;
Avi Kivity1c4f1fd2007-12-09 17:40:31 +0200964}
965
Avi Kivity6aa8b732006-12-10 02:21:36 -0800966static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
967{
968}
969
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -0500970static int __nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write,
971 gfn_t gfn, struct page *page)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800972{
973 int level = PT32E_ROOT_LEVEL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800974 hpa_t table_addr = vcpu->arch.mmu.root_hpa;
Avi Kivitye8332402007-12-09 18:43:00 +0200975 int pt_write = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800976
977 for (; ; level--) {
978 u32 index = PT64_INDEX(v, level);
979 u64 *table;
980
981 ASSERT(VALID_PAGE(table_addr));
982 table = __va(table_addr);
983
984 if (level == 1) {
Avi Kivitye8332402007-12-09 18:43:00 +0200985 mmu_set_spte(vcpu, &table[index], ACC_ALL, ACC_ALL,
Avi Kivityd7824ff2007-12-30 12:29:05 +0200986 0, write, 1, &pt_write, gfn, page);
Avi Kivitye8332402007-12-09 18:43:00 +0200987 return pt_write || is_io_pte(table[index]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800988 }
989
Avi Kivityc7addb92007-09-16 18:58:32 +0200990 if (table[index] == shadow_trap_nonpresent_pte) {
Avi Kivity25c0de22007-01-05 16:36:42 -0800991 struct kvm_mmu_page *new_table;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800992 gfn_t pseudo_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800993
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800994 pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
995 >> PAGE_SHIFT;
996 new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
997 v, level - 1,
Marcelo Tosatti78190262007-12-11 19:12:27 -0500998 1, ACC_ALL, &table[index],
999 NULL);
Avi Kivity25c0de22007-01-05 16:36:42 -08001000 if (!new_table) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001001 pgprintk("nonpaging_map: ENOMEM\n");
Avi Kivityd7824ff2007-12-30 12:29:05 +02001002 kvm_release_page_clean(page);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001003 return -ENOMEM;
1004 }
1005
Avi Kivity47ad8e62007-05-06 15:50:58 +03001006 table[index] = __pa(new_table->spt) | PT_PRESENT_MASK
Avi Kivity25c0de22007-01-05 16:36:42 -08001007 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001008 }
1009 table_addr = table[index] & PT64_BASE_ADDR_MASK;
1010 }
1011}
1012
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001013static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
1014{
1015 int r;
1016
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001017 struct page *page;
1018
1019 down_read(&current->mm->mmap_sem);
1020 page = gfn_to_page(vcpu->kvm, gfn);
1021
1022 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02001023 kvm_mmu_free_some_pages(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001024 r = __nonpaging_map(vcpu, v, write, gfn, page);
1025 spin_unlock(&vcpu->kvm->mmu_lock);
1026
1027 up_read(&current->mm->mmap_sem);
1028
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001029 return r;
1030}
1031
1032
Avi Kivityc7addb92007-09-16 18:58:32 +02001033static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
1034 struct kvm_mmu_page *sp)
1035{
1036 int i;
1037
1038 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1039 sp->spt[i] = shadow_trap_nonpresent_pte;
1040}
1041
Avi Kivity17ac10a2007-01-05 16:36:40 -08001042static void mmu_free_roots(struct kvm_vcpu *vcpu)
1043{
1044 int i;
Avi Kivity4db35312007-11-21 15:28:32 +02001045 struct kvm_mmu_page *sp;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001046
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001047 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
Avi Kivity7b53aa52007-06-05 12:17:03 +03001048 return;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001049 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001050#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001051 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1052 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001053
Avi Kivity4db35312007-11-21 15:28:32 +02001054 sp = page_header(root);
1055 --sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001056 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001057 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001058 return;
1059 }
1060#endif
1061 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001062 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001063
Avi Kivity417726a2007-04-12 17:35:58 +03001064 if (root) {
Avi Kivity417726a2007-04-12 17:35:58 +03001065 root &= PT64_BASE_ADDR_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001066 sp = page_header(root);
1067 --sp->root_count;
Avi Kivity417726a2007-04-12 17:35:58 +03001068 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001069 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001070 }
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001071 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001072 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001073}
1074
1075static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
1076{
1077 int i;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001078 gfn_t root_gfn;
Avi Kivity4db35312007-11-21 15:28:32 +02001079 struct kvm_mmu_page *sp;
Avi Kivity3bb65a22007-01-05 16:36:51 -08001080
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001081 root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001082
1083#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001084 if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
1085 hpa_t root = vcpu->arch.mmu.root_hpa;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001086
1087 ASSERT(!VALID_PAGE(root));
Avi Kivity4db35312007-11-21 15:28:32 +02001088 sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
Marcelo Tosatti78190262007-12-11 19:12:27 -05001089 PT64_ROOT_LEVEL, 0, ACC_ALL, NULL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001090 root = __pa(sp->spt);
1091 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001092 vcpu->arch.mmu.root_hpa = root;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001093 return;
1094 }
1095#endif
1096 for (i = 0; i < 4; ++i) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001097 hpa_t root = vcpu->arch.mmu.pae_root[i];
Avi Kivity17ac10a2007-01-05 16:36:40 -08001098
1099 ASSERT(!VALID_PAGE(root));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001100 if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
1101 if (!is_present_pte(vcpu->arch.pdptrs[i])) {
1102 vcpu->arch.mmu.pae_root[i] = 0;
Avi Kivity417726a2007-04-12 17:35:58 +03001103 continue;
1104 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001105 root_gfn = vcpu->arch.pdptrs[i] >> PAGE_SHIFT;
1106 } else if (vcpu->arch.mmu.root_level == 0)
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001107 root_gfn = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02001108 sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
1109 PT32_ROOT_LEVEL, !is_paging(vcpu),
Marcelo Tosatti78190262007-12-11 19:12:27 -05001110 ACC_ALL, NULL, NULL);
Avi Kivity4db35312007-11-21 15:28:32 +02001111 root = __pa(sp->spt);
1112 ++sp->root_count;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001113 vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001114 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001115 vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001116}
1117
Avi Kivity6aa8b732006-12-10 02:21:36 -08001118static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
1119{
1120 return vaddr;
1121}
1122
1123static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
Avi Kivity3f3e7122007-11-21 14:54:16 +02001124 u32 error_code)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001125{
Avi Kivitye8332402007-12-09 18:43:00 +02001126 gfn_t gfn;
Avi Kivitye2dec932007-01-05 16:36:54 -08001127 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001128
Avi Kivitye8332402007-12-09 18:43:00 +02001129 pgprintk("%s: gva %lx error %x\n", __FUNCTION__, gva, error_code);
Avi Kivitye2dec932007-01-05 16:36:54 -08001130 r = mmu_topup_memory_caches(vcpu);
1131 if (r)
1132 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -08001133
Avi Kivity6aa8b732006-12-10 02:21:36 -08001134 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001135 ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001136
Avi Kivitye8332402007-12-09 18:43:00 +02001137 gfn = gva >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001138
Avi Kivitye8332402007-12-09 18:43:00 +02001139 return nonpaging_map(vcpu, gva & PAGE_MASK,
1140 error_code & PFERR_WRITE_MASK, gfn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001141}
1142
Avi Kivity6aa8b732006-12-10 02:21:36 -08001143static void nonpaging_free(struct kvm_vcpu *vcpu)
1144{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001145 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001146}
1147
1148static int nonpaging_init_context(struct kvm_vcpu *vcpu)
1149{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001150 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001151
1152 context->new_cr3 = nonpaging_new_cr3;
1153 context->page_fault = nonpaging_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001154 context->gva_to_gpa = nonpaging_gva_to_gpa;
1155 context->free = nonpaging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001156 context->prefetch_page = nonpaging_prefetch_page;
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001157 context->root_level = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001158 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001159 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001160 return 0;
1161}
1162
Avi Kivityd835dfe2007-11-21 02:57:59 +02001163void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001164{
Avi Kivity1165f5f2007-04-19 17:27:43 +03001165 ++vcpu->stat.tlb_flush;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03001166 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001167}
1168
1169static void paging_new_cr3(struct kvm_vcpu *vcpu)
1170{
Avi Kivity374cbac2007-01-05 16:36:43 -08001171 pgprintk("%s: cr3 %lx\n", __FUNCTION__, vcpu->cr3);
Avi Kivitycea0f0e2007-01-05 16:36:43 -08001172 mmu_free_roots(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001173}
1174
Avi Kivity6aa8b732006-12-10 02:21:36 -08001175static void inject_page_fault(struct kvm_vcpu *vcpu,
1176 u64 addr,
1177 u32 err_code)
1178{
Avi Kivityc3c91fe2007-11-25 14:04:58 +02001179 kvm_inject_page_fault(vcpu, addr, err_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001180}
1181
Avi Kivity6aa8b732006-12-10 02:21:36 -08001182static void paging_free(struct kvm_vcpu *vcpu)
1183{
1184 nonpaging_free(vcpu);
1185}
1186
1187#define PTTYPE 64
1188#include "paging_tmpl.h"
1189#undef PTTYPE
1190
1191#define PTTYPE 32
1192#include "paging_tmpl.h"
1193#undef PTTYPE
1194
Avi Kivity17ac10a2007-01-05 16:36:40 -08001195static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001196{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001197 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001198
1199 ASSERT(is_pae(vcpu));
1200 context->new_cr3 = paging_new_cr3;
1201 context->page_fault = paging64_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001202 context->gva_to_gpa = paging64_gva_to_gpa;
Avi Kivityc7addb92007-09-16 18:58:32 +02001203 context->prefetch_page = paging64_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001204 context->free = paging_free;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001205 context->root_level = level;
1206 context->shadow_root_level = level;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001207 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001208 return 0;
1209}
1210
Avi Kivity17ac10a2007-01-05 16:36:40 -08001211static int paging64_init_context(struct kvm_vcpu *vcpu)
1212{
1213 return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
1214}
1215
Avi Kivity6aa8b732006-12-10 02:21:36 -08001216static int paging32_init_context(struct kvm_vcpu *vcpu)
1217{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001218 struct kvm_mmu *context = &vcpu->arch.mmu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001219
1220 context->new_cr3 = paging_new_cr3;
1221 context->page_fault = paging32_page_fault;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001222 context->gva_to_gpa = paging32_gva_to_gpa;
1223 context->free = paging_free;
Avi Kivityc7addb92007-09-16 18:58:32 +02001224 context->prefetch_page = paging32_prefetch_page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001225 context->root_level = PT32_ROOT_LEVEL;
1226 context->shadow_root_level = PT32E_ROOT_LEVEL;
Avi Kivity17c3ba92007-06-04 15:58:30 +03001227 context->root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001228 return 0;
1229}
1230
1231static int paging32E_init_context(struct kvm_vcpu *vcpu)
1232{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001233 return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001234}
1235
1236static int init_kvm_mmu(struct kvm_vcpu *vcpu)
1237{
1238 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001239 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001240
1241 if (!is_paging(vcpu))
1242 return nonpaging_init_context(vcpu);
Avi Kivitya9058ec2006-12-29 16:49:37 -08001243 else if (is_long_mode(vcpu))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001244 return paging64_init_context(vcpu);
1245 else if (is_pae(vcpu))
1246 return paging32E_init_context(vcpu);
1247 else
1248 return paging32_init_context(vcpu);
1249}
1250
1251static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1252{
1253 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001254 if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
1255 vcpu->arch.mmu.free(vcpu);
1256 vcpu->arch.mmu.root_hpa = INVALID_PAGE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001257 }
1258}
1259
1260int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1261{
Avi Kivity17c3ba92007-06-04 15:58:30 +03001262 destroy_kvm_mmu(vcpu);
1263 return init_kvm_mmu(vcpu);
1264}
Eddie Dong8668a3c2007-10-10 14:26:45 +08001265EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001266
1267int kvm_mmu_load(struct kvm_vcpu *vcpu)
1268{
Avi Kivity714b93d2007-01-05 16:36:53 -08001269 int r;
1270
Avi Kivitye2dec932007-01-05 16:36:54 -08001271 r = mmu_topup_memory_caches(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001272 if (r)
1273 goto out;
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001274 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02001275 kvm_mmu_free_some_pages(vcpu);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001276 mmu_alloc_roots(vcpu);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001277 spin_unlock(&vcpu->kvm->mmu_lock);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001278 kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
Avi Kivity17c3ba92007-06-04 15:58:30 +03001279 kvm_mmu_flush_tlb(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001280out:
1281 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001282}
Avi Kivity17c3ba92007-06-04 15:58:30 +03001283EXPORT_SYMBOL_GPL(kvm_mmu_load);
1284
1285void kvm_mmu_unload(struct kvm_vcpu *vcpu)
1286{
1287 mmu_free_roots(vcpu);
1288}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001289
Avi Kivity09072da2007-05-01 14:16:52 +03001290static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02001291 struct kvm_mmu_page *sp,
Avi Kivityac1b7142007-03-08 17:13:32 +02001292 u64 *spte)
1293{
1294 u64 pte;
1295 struct kvm_mmu_page *child;
1296
1297 pte = *spte;
Avi Kivityc7addb92007-09-16 18:58:32 +02001298 if (is_shadow_present_pte(pte)) {
Avi Kivity4db35312007-11-21 15:28:32 +02001299 if (sp->role.level == PT_PAGE_TABLE_LEVEL)
Izik Eidus290fc382007-09-27 14:11:22 +02001300 rmap_remove(vcpu->kvm, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001301 else {
1302 child = page_header(pte & PT64_BASE_ADDR_MASK);
Avi Kivity90cb0522007-07-17 13:04:56 +03001303 mmu_page_remove_parent_pte(child, spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001304 }
1305 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001306 set_shadow_pte(spte, shadow_trap_nonpresent_pte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001307}
1308
Avi Kivity00284252007-05-01 16:53:31 +03001309static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
Avi Kivity4db35312007-11-21 15:28:32 +02001310 struct kvm_mmu_page *sp,
Avi Kivity00284252007-05-01 16:53:31 +03001311 u64 *spte,
Avi Kivityc7addb92007-09-16 18:58:32 +02001312 const void *new, int bytes,
1313 int offset_in_pte)
Avi Kivity00284252007-05-01 16:53:31 +03001314{
Avi Kivity4db35312007-11-21 15:28:32 +02001315 if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
Avi Kivity4cee5762007-11-18 16:37:07 +02001316 ++vcpu->kvm->stat.mmu_pde_zapped;
Avi Kivity00284252007-05-01 16:53:31 +03001317 return;
Avi Kivity4cee5762007-11-18 16:37:07 +02001318 }
Avi Kivity00284252007-05-01 16:53:31 +03001319
Avi Kivity4cee5762007-11-18 16:37:07 +02001320 ++vcpu->kvm->stat.mmu_pte_updated;
Avi Kivity4db35312007-11-21 15:28:32 +02001321 if (sp->role.glevels == PT32_ROOT_LEVEL)
1322 paging32_update_pte(vcpu, sp, spte, new, bytes, offset_in_pte);
Avi Kivity00284252007-05-01 16:53:31 +03001323 else
Avi Kivity4db35312007-11-21 15:28:32 +02001324 paging64_update_pte(vcpu, sp, spte, new, bytes, offset_in_pte);
Avi Kivity00284252007-05-01 16:53:31 +03001325}
1326
Avi Kivity79539ce2007-11-21 02:06:21 +02001327static bool need_remote_flush(u64 old, u64 new)
1328{
1329 if (!is_shadow_present_pte(old))
1330 return false;
1331 if (!is_shadow_present_pte(new))
1332 return true;
1333 if ((old ^ new) & PT64_BASE_ADDR_MASK)
1334 return true;
1335 old ^= PT64_NX_MASK;
1336 new ^= PT64_NX_MASK;
1337 return (old & ~new & PT64_PERM_MASK) != 0;
1338}
1339
1340static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, u64 old, u64 new)
1341{
1342 if (need_remote_flush(old, new))
1343 kvm_flush_remote_tlbs(vcpu->kvm);
1344 else
1345 kvm_mmu_flush_tlb(vcpu);
1346}
1347
Avi Kivity12b7d282007-09-23 14:10:49 +02001348static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
1349{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001350 u64 *spte = vcpu->arch.last_pte_updated;
Avi Kivity12b7d282007-09-23 14:10:49 +02001351
1352 return !!(spte && (*spte & PT_ACCESSED_MASK));
1353}
1354
Avi Kivityd7824ff2007-12-30 12:29:05 +02001355static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
1356 const u8 *new, int bytes)
1357{
1358 gfn_t gfn;
1359 int r;
1360 u64 gpte = 0;
1361
1362 if (bytes != 4 && bytes != 8)
1363 return;
1364
1365 /*
1366 * Assume that the pte write on a page table of the same type
1367 * as the current vcpu paging mode. This is nearly always true
1368 * (might be false while changing modes). Note it is verified later
1369 * by update_pte().
1370 */
1371 if (is_pae(vcpu)) {
1372 /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
1373 if ((bytes == 4) && (gpa % 4 == 0)) {
1374 r = kvm_read_guest(vcpu->kvm, gpa & ~(u64)7, &gpte, 8);
1375 if (r)
1376 return;
1377 memcpy((void *)&gpte + (gpa % 8), new, 4);
1378 } else if ((bytes == 8) && (gpa % 8 == 0)) {
1379 memcpy((void *)&gpte, new, 8);
1380 }
1381 } else {
1382 if ((bytes == 4) && (gpa % 4 == 0))
1383 memcpy((void *)&gpte, new, 4);
1384 }
1385 if (!is_present_pte(gpte))
1386 return;
1387 gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
1388 vcpu->arch.update_pte.gfn = gfn;
1389 vcpu->arch.update_pte.page = gfn_to_page(vcpu->kvm, gfn);
1390}
1391
Avi Kivity09072da2007-05-01 14:16:52 +03001392void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
Shaohua Life551882007-07-23 14:51:39 +08001393 const u8 *new, int bytes)
Avi Kivityda4a00f2007-01-05 16:36:44 -08001394{
Avi Kivity9b7a0322007-01-05 16:36:45 -08001395 gfn_t gfn = gpa >> PAGE_SHIFT;
Avi Kivity4db35312007-11-21 15:28:32 +02001396 struct kvm_mmu_page *sp;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001397 struct hlist_node *node, *n;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001398 struct hlist_head *bucket;
1399 unsigned index;
Avi Kivity79539ce2007-11-21 02:06:21 +02001400 u64 entry;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001401 u64 *spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001402 unsigned offset = offset_in_page(gpa);
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001403 unsigned pte_size;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001404 unsigned page_offset;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001405 unsigned misaligned;
Avi Kivityfce06572007-05-01 16:44:05 +03001406 unsigned quadrant;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001407 int level;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001408 int flooded = 0;
Avi Kivityac1b7142007-03-08 17:13:32 +02001409 int npte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001410
Avi Kivityda4a00f2007-01-05 16:36:44 -08001411 pgprintk("%s: gpa %llx bytes %d\n", __FUNCTION__, gpa, bytes);
Avi Kivityd7824ff2007-12-30 12:29:05 +02001412 mmu_guess_page_from_pte_write(vcpu, gpa, new, bytes);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001413 spin_lock(&vcpu->kvm->mmu_lock);
Avi Kivityeb787d12007-12-31 15:27:49 +02001414 kvm_mmu_free_some_pages(vcpu);
Avi Kivity4cee5762007-11-18 16:37:07 +02001415 ++vcpu->kvm->stat.mmu_pte_write;
Avi Kivityc7addb92007-09-16 18:58:32 +02001416 kvm_mmu_audit(vcpu, "pre pte write");
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001417 if (gfn == vcpu->arch.last_pt_write_gfn
Avi Kivity12b7d282007-09-23 14:10:49 +02001418 && !last_updated_pte_accessed(vcpu)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001419 ++vcpu->arch.last_pt_write_count;
1420 if (vcpu->arch.last_pt_write_count >= 3)
Avi Kivity86a5ba02007-01-05 16:36:50 -08001421 flooded = 1;
1422 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001423 vcpu->arch.last_pt_write_gfn = gfn;
1424 vcpu->arch.last_pt_write_count = 1;
1425 vcpu->arch.last_pte_updated = NULL;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001426 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001427 index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001428 bucket = &vcpu->kvm->arch.mmu_page_hash[index];
Avi Kivity4db35312007-11-21 15:28:32 +02001429 hlist_for_each_entry_safe(sp, node, n, bucket, hash_link) {
1430 if (sp->gfn != gfn || sp->role.metaphysical)
Avi Kivity9b7a0322007-01-05 16:36:45 -08001431 continue;
Avi Kivity4db35312007-11-21 15:28:32 +02001432 pte_size = sp->role.glevels == PT32_ROOT_LEVEL ? 4 : 8;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001433 misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
Avi Kivitye925c5b2007-04-30 14:47:02 +03001434 misaligned |= bytes < 4;
Avi Kivity86a5ba02007-01-05 16:36:50 -08001435 if (misaligned || flooded) {
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001436 /*
1437 * Misaligned accesses are too much trouble to fix
1438 * up; also, they usually indicate a page is not used
1439 * as a page table.
Avi Kivity86a5ba02007-01-05 16:36:50 -08001440 *
1441 * If we're seeing too many writes to a page,
1442 * it may no longer be a page table, or we may be
1443 * forking, in which case it is better to unmap the
1444 * page.
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001445 */
1446 pgprintk("misaligned: gpa %llx bytes %d role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02001447 gpa, bytes, sp->role.word);
1448 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02001449 ++vcpu->kvm->stat.mmu_flooded;
Avi Kivity0e7bc4b2007-01-05 16:36:48 -08001450 continue;
1451 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001452 page_offset = offset;
Avi Kivity4db35312007-11-21 15:28:32 +02001453 level = sp->role.level;
Avi Kivityac1b7142007-03-08 17:13:32 +02001454 npte = 1;
Avi Kivity4db35312007-11-21 15:28:32 +02001455 if (sp->role.glevels == PT32_ROOT_LEVEL) {
Avi Kivityac1b7142007-03-08 17:13:32 +02001456 page_offset <<= 1; /* 32->64 */
1457 /*
1458 * A 32-bit pde maps 4MB while the shadow pdes map
1459 * only 2MB. So we need to double the offset again
1460 * and zap two pdes instead of one.
1461 */
1462 if (level == PT32_ROOT_LEVEL) {
Avi Kivity6b8d0f92007-04-18 11:18:18 +03001463 page_offset &= ~7; /* kill rounding error */
Avi Kivityac1b7142007-03-08 17:13:32 +02001464 page_offset <<= 1;
1465 npte = 2;
1466 }
Avi Kivityfce06572007-05-01 16:44:05 +03001467 quadrant = page_offset >> PAGE_SHIFT;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001468 page_offset &= ~PAGE_MASK;
Avi Kivity4db35312007-11-21 15:28:32 +02001469 if (quadrant != sp->role.quadrant)
Avi Kivityfce06572007-05-01 16:44:05 +03001470 continue;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001471 }
Avi Kivity4db35312007-11-21 15:28:32 +02001472 spte = &sp->spt[page_offset / sizeof(*spte)];
Avi Kivityac1b7142007-03-08 17:13:32 +02001473 while (npte--) {
Avi Kivity79539ce2007-11-21 02:06:21 +02001474 entry = *spte;
Avi Kivity4db35312007-11-21 15:28:32 +02001475 mmu_pte_write_zap_pte(vcpu, sp, spte);
1476 mmu_pte_write_new_pte(vcpu, sp, spte, new, bytes,
Avi Kivityc7addb92007-09-16 18:58:32 +02001477 page_offset & (pte_size - 1));
Avi Kivity79539ce2007-11-21 02:06:21 +02001478 mmu_pte_write_flush_tlb(vcpu, entry, *spte);
Avi Kivityac1b7142007-03-08 17:13:32 +02001479 ++spte;
Avi Kivity9b7a0322007-01-05 16:36:45 -08001480 }
Avi Kivity9b7a0322007-01-05 16:36:45 -08001481 }
Avi Kivityc7addb92007-09-16 18:58:32 +02001482 kvm_mmu_audit(vcpu, "post pte write");
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001483 spin_unlock(&vcpu->kvm->mmu_lock);
Avi Kivityd7824ff2007-12-30 12:29:05 +02001484 if (vcpu->arch.update_pte.page) {
1485 kvm_release_page_clean(vcpu->arch.update_pte.page);
1486 vcpu->arch.update_pte.page = NULL;
1487 }
Avi Kivityda4a00f2007-01-05 16:36:44 -08001488}
1489
Avi Kivitya4360362007-01-05 16:36:45 -08001490int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1491{
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001492 gpa_t gpa;
1493 int r;
Avi Kivitya4360362007-01-05 16:36:45 -08001494
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001495 down_read(&current->mm->mmap_sem);
1496 gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, gva);
1497 up_read(&current->mm->mmap_sem);
1498
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001499 spin_lock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001500 r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001501 spin_unlock(&vcpu->kvm->mmu_lock);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001502 return r;
Avi Kivitya4360362007-01-05 16:36:45 -08001503}
1504
Avi Kivity22d95b12007-09-14 20:26:06 +03001505void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
Avi Kivityebeace82007-01-05 16:36:47 -08001506{
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001507 while (vcpu->kvm->arch.n_free_mmu_pages < KVM_REFILL_PAGES) {
Avi Kivity4db35312007-11-21 15:28:32 +02001508 struct kvm_mmu_page *sp;
Avi Kivityebeace82007-01-05 16:36:47 -08001509
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001510 sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
Avi Kivity4db35312007-11-21 15:28:32 +02001511 struct kvm_mmu_page, link);
1512 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivity4cee5762007-11-18 16:37:07 +02001513 ++vcpu->kvm->stat.mmu_recycled;
Avi Kivityebeace82007-01-05 16:36:47 -08001514 }
1515}
Avi Kivityebeace82007-01-05 16:36:47 -08001516
Avi Kivity30677142007-10-28 18:48:59 +02001517int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
1518{
1519 int r;
1520 enum emulation_result er;
1521
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001522 r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
Avi Kivity30677142007-10-28 18:48:59 +02001523 if (r < 0)
1524 goto out;
1525
1526 if (!r) {
1527 r = 1;
1528 goto out;
1529 }
1530
Avi Kivityb733bfb2007-10-28 18:52:05 +02001531 r = mmu_topup_memory_caches(vcpu);
1532 if (r)
1533 goto out;
1534
Avi Kivity30677142007-10-28 18:48:59 +02001535 er = emulate_instruction(vcpu, vcpu->run, cr2, error_code, 0);
Avi Kivity30677142007-10-28 18:48:59 +02001536
1537 switch (er) {
1538 case EMULATE_DONE:
1539 return 1;
1540 case EMULATE_DO_MMIO:
1541 ++vcpu->stat.mmio_exits;
1542 return 0;
1543 case EMULATE_FAIL:
1544 kvm_report_emulation_failure(vcpu, "pagetable");
1545 return 1;
1546 default:
1547 BUG();
1548 }
1549out:
Avi Kivity30677142007-10-28 18:48:59 +02001550 return r;
1551}
1552EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
1553
Avi Kivity6aa8b732006-12-10 02:21:36 -08001554static void free_mmu_pages(struct kvm_vcpu *vcpu)
1555{
Avi Kivity4db35312007-11-21 15:28:32 +02001556 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001557
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001558 while (!list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
1559 sp = container_of(vcpu->kvm->arch.active_mmu_pages.next,
Avi Kivity4db35312007-11-21 15:28:32 +02001560 struct kvm_mmu_page, link);
1561 kvm_mmu_zap_page(vcpu->kvm, sp);
Avi Kivityf51234c2007-01-05 16:36:52 -08001562 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001563 free_page((unsigned long)vcpu->arch.mmu.pae_root);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001564}
1565
1566static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
1567{
Avi Kivity17ac10a2007-01-05 16:36:40 -08001568 struct page *page;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001569 int i;
1570
1571 ASSERT(vcpu);
1572
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001573 if (vcpu->kvm->arch.n_requested_mmu_pages)
1574 vcpu->kvm->arch.n_free_mmu_pages =
1575 vcpu->kvm->arch.n_requested_mmu_pages;
Izik Eidus82ce2c92007-10-02 18:52:55 +02001576 else
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001577 vcpu->kvm->arch.n_free_mmu_pages =
1578 vcpu->kvm->arch.n_alloc_mmu_pages;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001579 /*
1580 * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1581 * Therefore we need to allocate shadow page tables in the first
1582 * 4GB of memory, which happens to fit the DMA32 zone.
1583 */
1584 page = alloc_page(GFP_KERNEL | __GFP_DMA32);
1585 if (!page)
1586 goto error_1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001587 vcpu->arch.mmu.pae_root = page_address(page);
Avi Kivity17ac10a2007-01-05 16:36:40 -08001588 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001589 vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
Avi Kivity17ac10a2007-01-05 16:36:40 -08001590
Avi Kivity6aa8b732006-12-10 02:21:36 -08001591 return 0;
1592
1593error_1:
1594 free_mmu_pages(vcpu);
1595 return -ENOMEM;
1596}
1597
Ingo Molnar8018c272006-12-29 16:50:01 -08001598int kvm_mmu_create(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001599{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001600 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001601 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001602
Ingo Molnar8018c272006-12-29 16:50:01 -08001603 return alloc_mmu_pages(vcpu);
1604}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001605
Ingo Molnar8018c272006-12-29 16:50:01 -08001606int kvm_mmu_setup(struct kvm_vcpu *vcpu)
1607{
1608 ASSERT(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001609 ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
Avi Kivity2c264952006-12-22 01:05:28 -08001610
Ingo Molnar8018c272006-12-29 16:50:01 -08001611 return init_kvm_mmu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001612}
1613
1614void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
1615{
1616 ASSERT(vcpu);
1617
1618 destroy_kvm_mmu(vcpu);
1619 free_mmu_pages(vcpu);
Avi Kivity714b93d2007-01-05 16:36:53 -08001620 mmu_free_memory_caches(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001621}
1622
Avi Kivity90cb0522007-07-17 13:04:56 +03001623void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001624{
Avi Kivity4db35312007-11-21 15:28:32 +02001625 struct kvm_mmu_page *sp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001626
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001627 list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001628 int i;
1629 u64 *pt;
1630
Avi Kivity4db35312007-11-21 15:28:32 +02001631 if (!test_bit(slot, &sp->slot_bitmap))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001632 continue;
1633
Avi Kivity4db35312007-11-21 15:28:32 +02001634 pt = sp->spt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001635 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1636 /* avoid RMW */
Izik Eidus9647c142007-10-16 14:43:46 +02001637 if (pt[i] & PT_WRITABLE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001638 pt[i] &= ~PT_WRITABLE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001639 }
1640}
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001641
Avi Kivity90cb0522007-07-17 13:04:56 +03001642void kvm_mmu_zap_all(struct kvm *kvm)
Dor Laore0fa8262007-03-30 13:06:33 +03001643{
Avi Kivity4db35312007-11-21 15:28:32 +02001644 struct kvm_mmu_page *sp, *node;
Dor Laore0fa8262007-03-30 13:06:33 +03001645
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001646 spin_lock(&kvm->mmu_lock);
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001647 list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
Avi Kivity4db35312007-11-21 15:28:32 +02001648 kvm_mmu_zap_page(kvm, sp);
Marcelo Tosattiaaee2c92007-12-20 19:18:26 -05001649 spin_unlock(&kvm->mmu_lock);
Dor Laore0fa8262007-03-30 13:06:33 +03001650
Avi Kivity90cb0522007-07-17 13:04:56 +03001651 kvm_flush_remote_tlbs(kvm);
Dor Laore0fa8262007-03-30 13:06:33 +03001652}
1653
Avi Kivityb5a33a72007-04-15 16:31:09 +03001654void kvm_mmu_module_exit(void)
1655{
1656 if (pte_chain_cache)
1657 kmem_cache_destroy(pte_chain_cache);
1658 if (rmap_desc_cache)
1659 kmem_cache_destroy(rmap_desc_cache);
Avi Kivityd3d25b02007-05-30 12:34:53 +03001660 if (mmu_page_header_cache)
1661 kmem_cache_destroy(mmu_page_header_cache);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001662}
1663
1664int kvm_mmu_module_init(void)
1665{
1666 pte_chain_cache = kmem_cache_create("kvm_pte_chain",
1667 sizeof(struct kvm_pte_chain),
Paul Mundt20c2df82007-07-20 10:11:58 +09001668 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001669 if (!pte_chain_cache)
1670 goto nomem;
1671 rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
1672 sizeof(struct kvm_rmap_desc),
Paul Mundt20c2df82007-07-20 10:11:58 +09001673 0, 0, NULL);
Avi Kivityb5a33a72007-04-15 16:31:09 +03001674 if (!rmap_desc_cache)
1675 goto nomem;
1676
Avi Kivityd3d25b02007-05-30 12:34:53 +03001677 mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
1678 sizeof(struct kvm_mmu_page),
Paul Mundt20c2df82007-07-20 10:11:58 +09001679 0, 0, NULL);
Avi Kivityd3d25b02007-05-30 12:34:53 +03001680 if (!mmu_page_header_cache)
1681 goto nomem;
1682
Avi Kivityb5a33a72007-04-15 16:31:09 +03001683 return 0;
1684
1685nomem:
1686 kvm_mmu_module_exit();
1687 return -ENOMEM;
1688}
1689
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08001690/*
1691 * Caculate mmu pages needed for kvm.
1692 */
1693unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
1694{
1695 int i;
1696 unsigned int nr_mmu_pages;
1697 unsigned int nr_pages = 0;
1698
1699 for (i = 0; i < kvm->nmemslots; i++)
1700 nr_pages += kvm->memslots[i].npages;
1701
1702 nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
1703 nr_mmu_pages = max(nr_mmu_pages,
1704 (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
1705
1706 return nr_mmu_pages;
1707}
1708
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001709#ifdef AUDIT
1710
1711static const char *audit_msg;
1712
1713static gva_t canonicalize(gva_t gva)
1714{
1715#ifdef CONFIG_X86_64
1716 gva = (long long)(gva << 16) >> 16;
1717#endif
1718 return gva;
1719}
1720
1721static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
1722 gva_t va, int level)
1723{
1724 u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
1725 int i;
1726 gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
1727
1728 for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
1729 u64 ent = pt[i];
1730
Avi Kivityc7addb92007-09-16 18:58:32 +02001731 if (ent == shadow_trap_nonpresent_pte)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001732 continue;
1733
1734 va = canonicalize(va);
Avi Kivityc7addb92007-09-16 18:58:32 +02001735 if (level > 1) {
1736 if (ent == shadow_notrap_nonpresent_pte)
1737 printk(KERN_ERR "audit: (%s) nontrapping pte"
1738 " in nonleaf level: levels %d gva %lx"
1739 " level %d pte %llx\n", audit_msg,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001740 vcpu->arch.mmu.root_level, va, level, ent);
Avi Kivityc7addb92007-09-16 18:58:32 +02001741
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001742 audit_mappings_page(vcpu, ent, va, level - 1);
Avi Kivityc7addb92007-09-16 18:58:32 +02001743 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001744 gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, va);
Avi Kivity1d28f5f2007-11-21 15:01:44 +02001745 struct page *page = gpa_to_page(vcpu, gpa);
1746 hpa_t hpa = page_to_phys(page);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001747
Avi Kivityc7addb92007-09-16 18:58:32 +02001748 if (is_shadow_present_pte(ent)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001749 && (ent & PT64_BASE_ADDR_MASK) != hpa)
Avi Kivityc7addb92007-09-16 18:58:32 +02001750 printk(KERN_ERR "xx audit error: (%s) levels %d"
1751 " gva %lx gpa %llx hpa %llx ent %llx %d\n",
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001752 audit_msg, vcpu->arch.mmu.root_level,
Mike Dayd77c26f2007-10-08 09:02:08 -04001753 va, gpa, hpa, ent,
1754 is_shadow_present_pte(ent));
Avi Kivityc7addb92007-09-16 18:58:32 +02001755 else if (ent == shadow_notrap_nonpresent_pte
1756 && !is_error_hpa(hpa))
1757 printk(KERN_ERR "audit: (%s) notrap shadow,"
1758 " valid guest gva %lx\n", audit_msg, va);
Izik Eidusb4231d62007-11-20 11:49:33 +02001759 kvm_release_page_clean(page);
Avi Kivityc7addb92007-09-16 18:58:32 +02001760
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001761 }
1762 }
1763}
1764
1765static void audit_mappings(struct kvm_vcpu *vcpu)
1766{
Avi Kivity1ea252a2007-03-08 11:48:09 +02001767 unsigned i;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001768
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001769 if (vcpu->arch.mmu.root_level == 4)
1770 audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001771 else
1772 for (i = 0; i < 4; ++i)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001773 if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001774 audit_mappings_page(vcpu,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001775 vcpu->arch.mmu.pae_root[i],
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001776 i << 30,
1777 2);
1778}
1779
1780static int count_rmaps(struct kvm_vcpu *vcpu)
1781{
1782 int nmaps = 0;
1783 int i, j, k;
1784
1785 for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
1786 struct kvm_memory_slot *m = &vcpu->kvm->memslots[i];
1787 struct kvm_rmap_desc *d;
1788
1789 for (j = 0; j < m->npages; ++j) {
Izik Eidus290fc382007-09-27 14:11:22 +02001790 unsigned long *rmapp = &m->rmap[j];
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001791
Izik Eidus290fc382007-09-27 14:11:22 +02001792 if (!*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001793 continue;
Izik Eidus290fc382007-09-27 14:11:22 +02001794 if (!(*rmapp & 1)) {
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001795 ++nmaps;
1796 continue;
1797 }
Izik Eidus290fc382007-09-27 14:11:22 +02001798 d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001799 while (d) {
1800 for (k = 0; k < RMAP_EXT; ++k)
1801 if (d->shadow_ptes[k])
1802 ++nmaps;
1803 else
1804 break;
1805 d = d->more;
1806 }
1807 }
1808 }
1809 return nmaps;
1810}
1811
1812static int count_writable_mappings(struct kvm_vcpu *vcpu)
1813{
1814 int nmaps = 0;
Avi Kivity4db35312007-11-21 15:28:32 +02001815 struct kvm_mmu_page *sp;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001816 int i;
1817
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001818 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02001819 u64 *pt = sp->spt;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001820
Avi Kivity4db35312007-11-21 15:28:32 +02001821 if (sp->role.level != PT_PAGE_TABLE_LEVEL)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001822 continue;
1823
1824 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
1825 u64 ent = pt[i];
1826
1827 if (!(ent & PT_PRESENT_MASK))
1828 continue;
1829 if (!(ent & PT_WRITABLE_MASK))
1830 continue;
1831 ++nmaps;
1832 }
1833 }
1834 return nmaps;
1835}
1836
1837static void audit_rmap(struct kvm_vcpu *vcpu)
1838{
1839 int n_rmap = count_rmaps(vcpu);
1840 int n_actual = count_writable_mappings(vcpu);
1841
1842 if (n_rmap != n_actual)
1843 printk(KERN_ERR "%s: (%s) rmap %d actual %d\n",
1844 __FUNCTION__, audit_msg, n_rmap, n_actual);
1845}
1846
1847static void audit_write_protection(struct kvm_vcpu *vcpu)
1848{
Avi Kivity4db35312007-11-21 15:28:32 +02001849 struct kvm_mmu_page *sp;
Izik Eidus290fc382007-09-27 14:11:22 +02001850 struct kvm_memory_slot *slot;
1851 unsigned long *rmapp;
1852 gfn_t gfn;
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001853
Zhang Xiantaof05e70a2007-12-14 10:01:48 +08001854 list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
Avi Kivity4db35312007-11-21 15:28:32 +02001855 if (sp->role.metaphysical)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001856 continue;
1857
Avi Kivity4db35312007-11-21 15:28:32 +02001858 slot = gfn_to_memslot(vcpu->kvm, sp->gfn);
1859 gfn = unalias_gfn(vcpu->kvm, sp->gfn);
Izik Eidus290fc382007-09-27 14:11:22 +02001860 rmapp = &slot->rmap[gfn - slot->base_gfn];
1861 if (*rmapp)
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001862 printk(KERN_ERR "%s: (%s) shadow page has writable"
1863 " mappings: gfn %lx role %x\n",
Avi Kivity4db35312007-11-21 15:28:32 +02001864 __FUNCTION__, audit_msg, sp->gfn,
1865 sp->role.word);
Avi Kivity37a7d8b2007-01-05 16:36:56 -08001866 }
1867}
1868
1869static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
1870{
1871 int olddbg = dbg;
1872
1873 dbg = 0;
1874 audit_msg = msg;
1875 audit_rmap(vcpu);
1876 audit_write_protection(vcpu);
1877 audit_mappings(vcpu);
1878 dbg = olddbg;
1879}
1880
1881#endif