[PATCH] KVM: MMU: oom handling
[linux-2.6.git] / drivers / kvm / mmu.c
1 /*
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  */
19 #include <linux/types.h>
20 #include <linux/string.h>
21 #include <asm/page.h>
22 #include <linux/mm.h>
23 #include <linux/highmem.h>
24 #include <linux/module.h>
25
26 #include "vmx.h"
27 #include "kvm.h"
28
29 #define pgprintk(x...) do { printk(x); } while (0)
30 #define rmap_printk(x...) do { printk(x); } while (0)
31
32 #define ASSERT(x)                                                       \
33         if (!(x)) {                                                     \
34                 printk(KERN_WARNING "assertion failed %s:%d: %s\n",     \
35                        __FILE__, __LINE__, #x);                         \
36         }
37
38 #define PT64_PT_BITS 9
39 #define PT64_ENT_PER_PAGE (1 << PT64_PT_BITS)
40 #define PT32_PT_BITS 10
41 #define PT32_ENT_PER_PAGE (1 << PT32_PT_BITS)
42
43 #define PT_WRITABLE_SHIFT 1
44
45 #define PT_PRESENT_MASK (1ULL << 0)
46 #define PT_WRITABLE_MASK (1ULL << PT_WRITABLE_SHIFT)
47 #define PT_USER_MASK (1ULL << 2)
48 #define PT_PWT_MASK (1ULL << 3)
49 #define PT_PCD_MASK (1ULL << 4)
50 #define PT_ACCESSED_MASK (1ULL << 5)
51 #define PT_DIRTY_MASK (1ULL << 6)
52 #define PT_PAGE_SIZE_MASK (1ULL << 7)
53 #define PT_PAT_MASK (1ULL << 7)
54 #define PT_GLOBAL_MASK (1ULL << 8)
55 #define PT64_NX_MASK (1ULL << 63)
56
57 #define PT_PAT_SHIFT 7
58 #define PT_DIR_PAT_SHIFT 12
59 #define PT_DIR_PAT_MASK (1ULL << PT_DIR_PAT_SHIFT)
60
61 #define PT32_DIR_PSE36_SIZE 4
62 #define PT32_DIR_PSE36_SHIFT 13
63 #define PT32_DIR_PSE36_MASK (((1ULL << PT32_DIR_PSE36_SIZE) - 1) << PT32_DIR_PSE36_SHIFT)
64
65
66 #define PT32_PTE_COPY_MASK \
67         (PT_PRESENT_MASK | PT_ACCESSED_MASK | PT_DIRTY_MASK | PT_GLOBAL_MASK)
68
69 #define PT64_PTE_COPY_MASK (PT64_NX_MASK | PT32_PTE_COPY_MASK)
70
71 #define PT_FIRST_AVAIL_BITS_SHIFT 9
72 #define PT64_SECOND_AVAIL_BITS_SHIFT 52
73
74 #define PT_SHADOW_PS_MARK (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
75 #define PT_SHADOW_IO_MARK (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
76
77 #define PT_SHADOW_WRITABLE_SHIFT (PT_FIRST_AVAIL_BITS_SHIFT + 1)
78 #define PT_SHADOW_WRITABLE_MASK (1ULL << PT_SHADOW_WRITABLE_SHIFT)
79
80 #define PT_SHADOW_USER_SHIFT (PT_SHADOW_WRITABLE_SHIFT + 1)
81 #define PT_SHADOW_USER_MASK (1ULL << (PT_SHADOW_USER_SHIFT))
82
83 #define PT_SHADOW_BITS_OFFSET (PT_SHADOW_WRITABLE_SHIFT - PT_WRITABLE_SHIFT)
84
85 #define VALID_PAGE(x) ((x) != INVALID_PAGE)
86
87 #define PT64_LEVEL_BITS 9
88
89 #define PT64_LEVEL_SHIFT(level) \
90                 ( PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS )
91
92 #define PT64_LEVEL_MASK(level) \
93                 (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
94
95 #define PT64_INDEX(address, level)\
96         (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
97
98
99 #define PT32_LEVEL_BITS 10
100
101 #define PT32_LEVEL_SHIFT(level) \
102                 ( PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS )
103
104 #define PT32_LEVEL_MASK(level) \
105                 (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
106
107 #define PT32_INDEX(address, level)\
108         (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
109
110
111 #define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & PAGE_MASK)
112 #define PT64_DIR_BASE_ADDR_MASK \
113         (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
114
115 #define PT32_BASE_ADDR_MASK PAGE_MASK
116 #define PT32_DIR_BASE_ADDR_MASK \
117         (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
118
119
120 #define PFERR_PRESENT_MASK (1U << 0)
121 #define PFERR_WRITE_MASK (1U << 1)
122 #define PFERR_USER_MASK (1U << 2)
123
124 #define PT64_ROOT_LEVEL 4
125 #define PT32_ROOT_LEVEL 2
126 #define PT32E_ROOT_LEVEL 3
127
128 #define PT_DIRECTORY_LEVEL 2
129 #define PT_PAGE_TABLE_LEVEL 1
130
131 #define RMAP_EXT 4
132
133 struct kvm_rmap_desc {
134         u64 *shadow_ptes[RMAP_EXT];
135         struct kvm_rmap_desc *more;
136 };
137
138 static int is_write_protection(struct kvm_vcpu *vcpu)
139 {
140         return vcpu->cr0 & CR0_WP_MASK;
141 }
142
143 static int is_cpuid_PSE36(void)
144 {
145         return 1;
146 }
147
148 static int is_present_pte(unsigned long pte)
149 {
150         return pte & PT_PRESENT_MASK;
151 }
152
153 static int is_writeble_pte(unsigned long pte)
154 {
155         return pte & PT_WRITABLE_MASK;
156 }
157
158 static int is_io_pte(unsigned long pte)
159 {
160         return pte & PT_SHADOW_IO_MARK;
161 }
162
163 static int is_rmap_pte(u64 pte)
164 {
165         return (pte & (PT_WRITABLE_MASK | PT_PRESENT_MASK))
166                 == (PT_WRITABLE_MASK | PT_PRESENT_MASK);
167 }
168
169 /*
170  * Reverse mapping data structures:
171  *
172  * If page->private bit zero is zero, then page->private points to the
173  * shadow page table entry that points to page_address(page).
174  *
175  * If page->private bit zero is one, (then page->private & ~1) points
176  * to a struct kvm_rmap_desc containing more mappings.
177  */
178 static void rmap_add(struct kvm *kvm, u64 *spte)
179 {
180         struct page *page;
181         struct kvm_rmap_desc *desc;
182         int i;
183
184         if (!is_rmap_pte(*spte))
185                 return;
186         page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT);
187         if (!page->private) {
188                 rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
189                 page->private = (unsigned long)spte;
190         } else if (!(page->private & 1)) {
191                 rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
192                 desc = kzalloc(sizeof *desc, GFP_NOWAIT);
193                 if (!desc)
194                         BUG(); /* FIXME: return error */
195                 desc->shadow_ptes[0] = (u64 *)page->private;
196                 desc->shadow_ptes[1] = spte;
197                 page->private = (unsigned long)desc | 1;
198         } else {
199                 rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
200                 desc = (struct kvm_rmap_desc *)(page->private & ~1ul);
201                 while (desc->shadow_ptes[RMAP_EXT-1] && desc->more)
202                         desc = desc->more;
203                 if (desc->shadow_ptes[RMAP_EXT-1]) {
204                         desc->more = kzalloc(sizeof *desc->more, GFP_NOWAIT);
205                         if (!desc->more)
206                                 BUG(); /* FIXME: return error */
207                         desc = desc->more;
208                 }
209                 for (i = 0; desc->shadow_ptes[i]; ++i)
210                         ;
211                 desc->shadow_ptes[i] = spte;
212         }
213 }
214
215 static void rmap_desc_remove_entry(struct page *page,
216                                    struct kvm_rmap_desc *desc,
217                                    int i,
218                                    struct kvm_rmap_desc *prev_desc)
219 {
220         int j;
221
222         for (j = RMAP_EXT - 1; !desc->shadow_ptes[j] && j > i; --j)
223                 ;
224         desc->shadow_ptes[i] = desc->shadow_ptes[j];
225         desc->shadow_ptes[j] = 0;
226         if (j != 0)
227                 return;
228         if (!prev_desc && !desc->more)
229                 page->private = (unsigned long)desc->shadow_ptes[0];
230         else
231                 if (prev_desc)
232                         prev_desc->more = desc->more;
233                 else
234                         page->private = (unsigned long)desc->more | 1;
235         kfree(desc);
236 }
237
238 static void rmap_remove(struct kvm *kvm, u64 *spte)
239 {
240         struct page *page;
241         struct kvm_rmap_desc *desc;
242         struct kvm_rmap_desc *prev_desc;
243         int i;
244
245         if (!is_rmap_pte(*spte))
246                 return;
247         page = pfn_to_page((*spte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT);
248         if (!page->private) {
249                 printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
250                 BUG();
251         } else if (!(page->private & 1)) {
252                 rmap_printk("rmap_remove:  %p %llx 1->0\n", spte, *spte);
253                 if ((u64 *)page->private != spte) {
254                         printk(KERN_ERR "rmap_remove:  %p %llx 1->BUG\n",
255                                spte, *spte);
256                         BUG();
257                 }
258                 page->private = 0;
259         } else {
260                 rmap_printk("rmap_remove:  %p %llx many->many\n", spte, *spte);
261                 desc = (struct kvm_rmap_desc *)(page->private & ~1ul);
262                 prev_desc = NULL;
263                 while (desc) {
264                         for (i = 0; i < RMAP_EXT && desc->shadow_ptes[i]; ++i)
265                                 if (desc->shadow_ptes[i] == spte) {
266                                         rmap_desc_remove_entry(page, desc, i,
267                                                                prev_desc);
268                                         return;
269                                 }
270                         prev_desc = desc;
271                         desc = desc->more;
272                 }
273                 BUG();
274         }
275 }
276
277 static void rmap_write_protect(struct kvm *kvm, u64 gfn)
278 {
279         struct page *page;
280         struct kvm_memory_slot *slot;
281         struct kvm_rmap_desc *desc;
282         u64 *spte;
283
284         slot = gfn_to_memslot(kvm, gfn);
285         BUG_ON(!slot);
286         page = gfn_to_page(slot, gfn);
287
288         while (page->private) {
289                 if (!(page->private & 1))
290                         spte = (u64 *)page->private;
291                 else {
292                         desc = (struct kvm_rmap_desc *)(page->private & ~1ul);
293                         spte = desc->shadow_ptes[0];
294                 }
295                 BUG_ON(!spte);
296                 BUG_ON((*spte & PT64_BASE_ADDR_MASK) !=
297                        page_to_pfn(page) << PAGE_SHIFT);
298                 BUG_ON(!(*spte & PT_PRESENT_MASK));
299                 BUG_ON(!(*spte & PT_WRITABLE_MASK));
300                 rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
301                 rmap_remove(kvm, spte);
302                 *spte &= ~(u64)PT_WRITABLE_MASK;
303         }
304 }
305
306 static void kvm_mmu_free_page(struct kvm_vcpu *vcpu, hpa_t page_hpa)
307 {
308         struct kvm_mmu_page *page_head = page_header(page_hpa);
309
310         list_del(&page_head->link);
311         page_head->page_hpa = page_hpa;
312         list_add(&page_head->link, &vcpu->free_pages);
313         ++vcpu->kvm->n_free_mmu_pages;
314 }
315
316 static int is_empty_shadow_page(hpa_t page_hpa)
317 {
318         u32 *pos;
319         u32 *end;
320         for (pos = __va(page_hpa), end = pos + PAGE_SIZE / sizeof(u32);
321                       pos != end; pos++)
322                 if (*pos != 0)
323                         return 0;
324         return 1;
325 }
326
327 static unsigned kvm_page_table_hashfn(gfn_t gfn)
328 {
329         return gfn;
330 }
331
332 static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
333                                                u64 *parent_pte)
334 {
335         struct kvm_mmu_page *page;
336
337         if (list_empty(&vcpu->free_pages))
338                 return NULL;
339
340         page = list_entry(vcpu->free_pages.next, struct kvm_mmu_page, link);
341         list_del(&page->link);
342         list_add(&page->link, &vcpu->kvm->active_mmu_pages);
343         ASSERT(is_empty_shadow_page(page->page_hpa));
344         page->slot_bitmap = 0;
345         page->global = 1;
346         page->multimapped = 0;
347         page->parent_pte = parent_pte;
348         --vcpu->kvm->n_free_mmu_pages;
349         return page;
350 }
351
352 static void mmu_page_add_parent_pte(struct kvm_mmu_page *page, u64 *parent_pte)
353 {
354         struct kvm_pte_chain *pte_chain;
355         struct hlist_node *node;
356         int i;
357
358         if (!parent_pte)
359                 return;
360         if (!page->multimapped) {
361                 u64 *old = page->parent_pte;
362
363                 if (!old) {
364                         page->parent_pte = parent_pte;
365                         return;
366                 }
367                 page->multimapped = 1;
368                 pte_chain = kzalloc(sizeof(struct kvm_pte_chain), GFP_NOWAIT);
369                 BUG_ON(!pte_chain);
370                 INIT_HLIST_HEAD(&page->parent_ptes);
371                 hlist_add_head(&pte_chain->link, &page->parent_ptes);
372                 pte_chain->parent_ptes[0] = old;
373         }
374         hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link) {
375                 if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
376                         continue;
377                 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
378                         if (!pte_chain->parent_ptes[i]) {
379                                 pte_chain->parent_ptes[i] = parent_pte;
380                                 return;
381                         }
382         }
383         pte_chain = kzalloc(sizeof(struct kvm_pte_chain), GFP_NOWAIT);
384         BUG_ON(!pte_chain);
385         hlist_add_head(&pte_chain->link, &page->parent_ptes);
386         pte_chain->parent_ptes[0] = parent_pte;
387 }
388
389 static void mmu_page_remove_parent_pte(struct kvm_mmu_page *page,
390                                        u64 *parent_pte)
391 {
392         struct kvm_pte_chain *pte_chain;
393         struct hlist_node *node;
394         int i;
395
396         if (!page->multimapped) {
397                 BUG_ON(page->parent_pte != parent_pte);
398                 page->parent_pte = NULL;
399                 return;
400         }
401         hlist_for_each_entry(pte_chain, node, &page->parent_ptes, link)
402                 for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
403                         if (!pte_chain->parent_ptes[i])
404                                 break;
405                         if (pte_chain->parent_ptes[i] != parent_pte)
406                                 continue;
407                         while (i + 1 < NR_PTE_CHAIN_ENTRIES
408                                 && pte_chain->parent_ptes[i + 1]) {
409                                 pte_chain->parent_ptes[i]
410                                         = pte_chain->parent_ptes[i + 1];
411                                 ++i;
412                         }
413                         pte_chain->parent_ptes[i] = NULL;
414                         if (i == 0) {
415                                 hlist_del(&pte_chain->link);
416                                 kfree(pte_chain);
417                                 if (hlist_empty(&page->parent_ptes)) {
418                                         page->multimapped = 0;
419                                         page->parent_pte = NULL;
420                                 }
421                         }
422                         return;
423                 }
424         BUG();
425 }
426
427 static struct kvm_mmu_page *kvm_mmu_lookup_page(struct kvm_vcpu *vcpu,
428                                                 gfn_t gfn)
429 {
430         unsigned index;
431         struct hlist_head *bucket;
432         struct kvm_mmu_page *page;
433         struct hlist_node *node;
434
435         pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
436         index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
437         bucket = &vcpu->kvm->mmu_page_hash[index];
438         hlist_for_each_entry(page, node, bucket, hash_link)
439                 if (page->gfn == gfn && !page->role.metaphysical) {
440                         pgprintk("%s: found role %x\n",
441                                  __FUNCTION__, page->role.word);
442                         return page;
443                 }
444         return NULL;
445 }
446
447 static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
448                                              gfn_t gfn,
449                                              gva_t gaddr,
450                                              unsigned level,
451                                              int metaphysical,
452                                              u64 *parent_pte)
453 {
454         union kvm_mmu_page_role role;
455         unsigned index;
456         unsigned quadrant;
457         struct hlist_head *bucket;
458         struct kvm_mmu_page *page;
459         struct hlist_node *node;
460
461         role.word = 0;
462         role.glevels = vcpu->mmu.root_level;
463         role.level = level;
464         role.metaphysical = metaphysical;
465         if (vcpu->mmu.root_level <= PT32_ROOT_LEVEL) {
466                 quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
467                 quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
468                 role.quadrant = quadrant;
469         }
470         pgprintk("%s: looking gfn %lx role %x\n", __FUNCTION__,
471                  gfn, role.word);
472         index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
473         bucket = &vcpu->kvm->mmu_page_hash[index];
474         hlist_for_each_entry(page, node, bucket, hash_link)
475                 if (page->gfn == gfn && page->role.word == role.word) {
476                         mmu_page_add_parent_pte(page, parent_pte);
477                         pgprintk("%s: found\n", __FUNCTION__);
478                         return page;
479                 }
480         page = kvm_mmu_alloc_page(vcpu, parent_pte);
481         if (!page)
482                 return page;
483         pgprintk("%s: adding gfn %lx role %x\n", __FUNCTION__, gfn, role.word);
484         page->gfn = gfn;
485         page->role = role;
486         hlist_add_head(&page->hash_link, bucket);
487         if (!metaphysical)
488                 rmap_write_protect(vcpu->kvm, gfn);
489         return page;
490 }
491
492 static void kvm_mmu_page_unlink_children(struct kvm_vcpu *vcpu,
493                                          struct kvm_mmu_page *page)
494 {
495         unsigned i;
496         u64 *pt;
497         u64 ent;
498
499         pt = __va(page->page_hpa);
500
501         if (page->role.level == PT_PAGE_TABLE_LEVEL) {
502                 for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
503                         if (pt[i] & PT_PRESENT_MASK)
504                                 rmap_remove(vcpu->kvm, &pt[i]);
505                         pt[i] = 0;
506                 }
507                 return;
508         }
509
510         for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
511                 ent = pt[i];
512
513                 pt[i] = 0;
514                 if (!(ent & PT_PRESENT_MASK))
515                         continue;
516                 ent &= PT64_BASE_ADDR_MASK;
517                 mmu_page_remove_parent_pte(page_header(ent), &pt[i]);
518         }
519 }
520
521 static void kvm_mmu_put_page(struct kvm_vcpu *vcpu,
522                              struct kvm_mmu_page *page,
523                              u64 *parent_pte)
524 {
525         mmu_page_remove_parent_pte(page, parent_pte);
526 }
527
528 static void kvm_mmu_zap_page(struct kvm_vcpu *vcpu,
529                              struct kvm_mmu_page *page)
530 {
531         u64 *parent_pte;
532
533         while (page->multimapped || page->parent_pte) {
534                 if (!page->multimapped)
535                         parent_pte = page->parent_pte;
536                 else {
537                         struct kvm_pte_chain *chain;
538
539                         chain = container_of(page->parent_ptes.first,
540                                              struct kvm_pte_chain, link);
541                         parent_pte = chain->parent_ptes[0];
542                 }
543                 BUG_ON(!parent_pte);
544                 kvm_mmu_put_page(vcpu, page, parent_pte);
545                 *parent_pte = 0;
546         }
547         kvm_mmu_page_unlink_children(vcpu, page);
548         hlist_del(&page->hash_link);
549         kvm_mmu_free_page(vcpu, page->page_hpa);
550 }
551
552 static int kvm_mmu_unprotect_page(struct kvm_vcpu *vcpu, gfn_t gfn)
553 {
554         unsigned index;
555         struct hlist_head *bucket;
556         struct kvm_mmu_page *page;
557         struct hlist_node *node, *n;
558         int r;
559
560         pgprintk("%s: looking for gfn %lx\n", __FUNCTION__, gfn);
561         r = 0;
562         index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
563         bucket = &vcpu->kvm->mmu_page_hash[index];
564         hlist_for_each_entry_safe(page, node, n, bucket, hash_link)
565                 if (page->gfn == gfn && !page->role.metaphysical) {
566                         pgprintk("%s: gfn %lx role %x\n", __FUNCTION__, gfn,
567                                  page->role.word);
568                         kvm_mmu_zap_page(vcpu, page);
569                         r = 1;
570                 }
571         return r;
572 }
573
574 static void page_header_update_slot(struct kvm *kvm, void *pte, gpa_t gpa)
575 {
576         int slot = memslot_id(kvm, gfn_to_memslot(kvm, gpa >> PAGE_SHIFT));
577         struct kvm_mmu_page *page_head = page_header(__pa(pte));
578
579         __set_bit(slot, &page_head->slot_bitmap);
580 }
581
582 hpa_t safe_gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa)
583 {
584         hpa_t hpa = gpa_to_hpa(vcpu, gpa);
585
586         return is_error_hpa(hpa) ? bad_page_address | (gpa & ~PAGE_MASK): hpa;
587 }
588
589 hpa_t gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa)
590 {
591         struct kvm_memory_slot *slot;
592         struct page *page;
593
594         ASSERT((gpa & HPA_ERR_MASK) == 0);
595         slot = gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT);
596         if (!slot)
597                 return gpa | HPA_ERR_MASK;
598         page = gfn_to_page(slot, gpa >> PAGE_SHIFT);
599         return ((hpa_t)page_to_pfn(page) << PAGE_SHIFT)
600                 | (gpa & (PAGE_SIZE-1));
601 }
602
603 hpa_t gva_to_hpa(struct kvm_vcpu *vcpu, gva_t gva)
604 {
605         gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
606
607         if (gpa == UNMAPPED_GVA)
608                 return UNMAPPED_GVA;
609         return gpa_to_hpa(vcpu, gpa);
610 }
611
612
613 static void release_pt_page_64(struct kvm_vcpu *vcpu, hpa_t page_hpa,
614                                int level)
615 {
616         u64 *pos;
617         u64 *end;
618
619         ASSERT(vcpu);
620         ASSERT(VALID_PAGE(page_hpa));
621         ASSERT(level <= PT64_ROOT_LEVEL && level > 0);
622
623         for (pos = __va(page_hpa), end = pos + PT64_ENT_PER_PAGE;
624              pos != end; pos++) {
625                 u64 current_ent = *pos;
626
627                 if (is_present_pte(current_ent)) {
628                         if (level != 1)
629                                 release_pt_page_64(vcpu,
630                                                   current_ent &
631                                                   PT64_BASE_ADDR_MASK,
632                                                   level - 1);
633                         else
634                                 rmap_remove(vcpu->kvm, pos);
635                 }
636                 *pos = 0;
637         }
638         kvm_mmu_free_page(vcpu, page_hpa);
639 }
640
641 static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
642 {
643 }
644
645 static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, hpa_t p)
646 {
647         int level = PT32E_ROOT_LEVEL;
648         hpa_t table_addr = vcpu->mmu.root_hpa;
649
650         for (; ; level--) {
651                 u32 index = PT64_INDEX(v, level);
652                 u64 *table;
653                 u64 pte;
654
655                 ASSERT(VALID_PAGE(table_addr));
656                 table = __va(table_addr);
657
658                 if (level == 1) {
659                         pte = table[index];
660                         if (is_present_pte(pte) && is_writeble_pte(pte))
661                                 return 0;
662                         mark_page_dirty(vcpu->kvm, v >> PAGE_SHIFT);
663                         page_header_update_slot(vcpu->kvm, table, v);
664                         table[index] = p | PT_PRESENT_MASK | PT_WRITABLE_MASK |
665                                                                 PT_USER_MASK;
666                         rmap_add(vcpu->kvm, &table[index]);
667                         return 0;
668                 }
669
670                 if (table[index] == 0) {
671                         struct kvm_mmu_page *new_table;
672                         gfn_t pseudo_gfn;
673
674                         pseudo_gfn = (v & PT64_DIR_BASE_ADDR_MASK)
675                                 >> PAGE_SHIFT;
676                         new_table = kvm_mmu_get_page(vcpu, pseudo_gfn,
677                                                      v, level - 1,
678                                                      1, &table[index]);
679                         if (!new_table) {
680                                 pgprintk("nonpaging_map: ENOMEM\n");
681                                 return -ENOMEM;
682                         }
683
684                         table[index] = new_table->page_hpa | PT_PRESENT_MASK
685                                 | PT_WRITABLE_MASK | PT_USER_MASK;
686                 }
687                 table_addr = table[index] & PT64_BASE_ADDR_MASK;
688         }
689 }
690
691 static void mmu_free_roots(struct kvm_vcpu *vcpu)
692 {
693         int i;
694
695 #ifdef CONFIG_X86_64
696         if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
697                 hpa_t root = vcpu->mmu.root_hpa;
698
699                 ASSERT(VALID_PAGE(root));
700                 vcpu->mmu.root_hpa = INVALID_PAGE;
701                 return;
702         }
703 #endif
704         for (i = 0; i < 4; ++i) {
705                 hpa_t root = vcpu->mmu.pae_root[i];
706
707                 ASSERT(VALID_PAGE(root));
708                 root &= PT64_BASE_ADDR_MASK;
709                 vcpu->mmu.pae_root[i] = INVALID_PAGE;
710         }
711         vcpu->mmu.root_hpa = INVALID_PAGE;
712 }
713
714 static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
715 {
716         int i;
717         gfn_t root_gfn;
718         root_gfn = vcpu->cr3 >> PAGE_SHIFT;
719
720 #ifdef CONFIG_X86_64
721         if (vcpu->mmu.shadow_root_level == PT64_ROOT_LEVEL) {
722                 hpa_t root = vcpu->mmu.root_hpa;
723
724                 ASSERT(!VALID_PAGE(root));
725                 root = kvm_mmu_get_page(vcpu, root_gfn, 0,
726                                         PT64_ROOT_LEVEL, 0, NULL)->page_hpa;
727                 vcpu->mmu.root_hpa = root;
728                 return;
729         }
730 #endif
731         for (i = 0; i < 4; ++i) {
732                 hpa_t root = vcpu->mmu.pae_root[i];
733
734                 ASSERT(!VALID_PAGE(root));
735                 if (vcpu->mmu.root_level == PT32E_ROOT_LEVEL)
736                         root_gfn = vcpu->pdptrs[i] >> PAGE_SHIFT;
737                 else if (vcpu->mmu.root_level == 0)
738                         root_gfn = 0;
739                 root = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
740                                         PT32_ROOT_LEVEL, !is_paging(vcpu),
741                                         NULL)->page_hpa;
742                 vcpu->mmu.pae_root[i] = root | PT_PRESENT_MASK;
743         }
744         vcpu->mmu.root_hpa = __pa(vcpu->mmu.pae_root);
745 }
746
747 static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr)
748 {
749         return vaddr;
750 }
751
752 static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
753                                u32 error_code)
754 {
755         gpa_t addr = gva;
756         hpa_t paddr;
757
758         ASSERT(vcpu);
759         ASSERT(VALID_PAGE(vcpu->mmu.root_hpa));
760
761
762         paddr = gpa_to_hpa(vcpu , addr & PT64_BASE_ADDR_MASK);
763
764         if (is_error_hpa(paddr))
765                 return 1;
766
767         return nonpaging_map(vcpu, addr & PAGE_MASK, paddr);
768 }
769
770 static void nonpaging_inval_page(struct kvm_vcpu *vcpu, gva_t addr)
771 {
772 }
773
774 static void nonpaging_free(struct kvm_vcpu *vcpu)
775 {
776         mmu_free_roots(vcpu);
777 }
778
779 static int nonpaging_init_context(struct kvm_vcpu *vcpu)
780 {
781         struct kvm_mmu *context = &vcpu->mmu;
782
783         context->new_cr3 = nonpaging_new_cr3;
784         context->page_fault = nonpaging_page_fault;
785         context->inval_page = nonpaging_inval_page;
786         context->gva_to_gpa = nonpaging_gva_to_gpa;
787         context->free = nonpaging_free;
788         context->root_level = 0;
789         context->shadow_root_level = PT32E_ROOT_LEVEL;
790         mmu_alloc_roots(vcpu);
791         ASSERT(VALID_PAGE(context->root_hpa));
792         kvm_arch_ops->set_cr3(vcpu, context->root_hpa);
793         return 0;
794 }
795
796 static void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
797 {
798         ++kvm_stat.tlb_flush;
799         kvm_arch_ops->tlb_flush(vcpu);
800 }
801
802 static void paging_new_cr3(struct kvm_vcpu *vcpu)
803 {
804         pgprintk("%s: cr3 %lx\n", __FUNCTION__, vcpu->cr3);
805         mmu_free_roots(vcpu);
806         mmu_alloc_roots(vcpu);
807         kvm_mmu_flush_tlb(vcpu);
808         kvm_arch_ops->set_cr3(vcpu, vcpu->mmu.root_hpa);
809 }
810
811 static void mark_pagetable_nonglobal(void *shadow_pte)
812 {
813         page_header(__pa(shadow_pte))->global = 0;
814 }
815
816 static inline void set_pte_common(struct kvm_vcpu *vcpu,
817                              u64 *shadow_pte,
818                              gpa_t gaddr,
819                              int dirty,
820                              u64 access_bits,
821                              gfn_t gfn)
822 {
823         hpa_t paddr;
824
825         *shadow_pte |= access_bits << PT_SHADOW_BITS_OFFSET;
826         if (!dirty)
827                 access_bits &= ~PT_WRITABLE_MASK;
828
829         paddr = gpa_to_hpa(vcpu, gaddr & PT64_BASE_ADDR_MASK);
830
831         *shadow_pte |= access_bits;
832
833         if (!(*shadow_pte & PT_GLOBAL_MASK))
834                 mark_pagetable_nonglobal(shadow_pte);
835
836         if (is_error_hpa(paddr)) {
837                 *shadow_pte |= gaddr;
838                 *shadow_pte |= PT_SHADOW_IO_MARK;
839                 *shadow_pte &= ~PT_PRESENT_MASK;
840                 return;
841         }
842
843         *shadow_pte |= paddr;
844
845         if (access_bits & PT_WRITABLE_MASK) {
846                 struct kvm_mmu_page *shadow;
847
848                 shadow = kvm_mmu_lookup_page(vcpu, gfn);
849                 if (shadow) {
850                         pgprintk("%s: found shadow page for %lx, marking ro\n",
851                                  __FUNCTION__, gfn);
852                         access_bits &= ~PT_WRITABLE_MASK;
853                         *shadow_pte &= ~PT_WRITABLE_MASK;
854                 }
855         }
856
857         if (access_bits & PT_WRITABLE_MASK)
858                 mark_page_dirty(vcpu->kvm, gaddr >> PAGE_SHIFT);
859
860         page_header_update_slot(vcpu->kvm, shadow_pte, gaddr);
861         rmap_add(vcpu->kvm, shadow_pte);
862 }
863
864 static void inject_page_fault(struct kvm_vcpu *vcpu,
865                               u64 addr,
866                               u32 err_code)
867 {
868         kvm_arch_ops->inject_page_fault(vcpu, addr, err_code);
869 }
870
871 static inline int fix_read_pf(u64 *shadow_ent)
872 {
873         if ((*shadow_ent & PT_SHADOW_USER_MASK) &&
874             !(*shadow_ent & PT_USER_MASK)) {
875                 /*
876                  * If supervisor write protect is disabled, we shadow kernel
877                  * pages as user pages so we can trap the write access.
878                  */
879                 *shadow_ent |= PT_USER_MASK;
880                 *shadow_ent &= ~PT_WRITABLE_MASK;
881
882                 return 1;
883
884         }
885         return 0;
886 }
887
888 static int may_access(u64 pte, int write, int user)
889 {
890
891         if (user && !(pte & PT_USER_MASK))
892                 return 0;
893         if (write && !(pte & PT_WRITABLE_MASK))
894                 return 0;
895         return 1;
896 }
897
898 /*
899  * Remove a shadow pte.
900  */
901 static void paging_inval_page(struct kvm_vcpu *vcpu, gva_t addr)
902 {
903         hpa_t page_addr = vcpu->mmu.root_hpa;
904         int level = vcpu->mmu.shadow_root_level;
905
906         ++kvm_stat.invlpg;
907
908         for (; ; level--) {
909                 u32 index = PT64_INDEX(addr, level);
910                 u64 *table = __va(page_addr);
911
912                 if (level == PT_PAGE_TABLE_LEVEL ) {
913                         rmap_remove(vcpu->kvm, &table[index]);
914                         table[index] = 0;
915                         return;
916                 }
917
918                 if (!is_present_pte(table[index]))
919                         return;
920
921                 page_addr = table[index] & PT64_BASE_ADDR_MASK;
922
923                 if (level == PT_DIRECTORY_LEVEL &&
924                           (table[index] & PT_SHADOW_PS_MARK)) {
925                         table[index] = 0;
926                         release_pt_page_64(vcpu, page_addr, PT_PAGE_TABLE_LEVEL);
927
928                         kvm_arch_ops->tlb_flush(vcpu);
929                         return;
930                 }
931         }
932 }
933
934 static void paging_free(struct kvm_vcpu *vcpu)
935 {
936         nonpaging_free(vcpu);
937 }
938
939 #define PTTYPE 64
940 #include "paging_tmpl.h"
941 #undef PTTYPE
942
943 #define PTTYPE 32
944 #include "paging_tmpl.h"
945 #undef PTTYPE
946
947 static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
948 {
949         struct kvm_mmu *context = &vcpu->mmu;
950
951         ASSERT(is_pae(vcpu));
952         context->new_cr3 = paging_new_cr3;
953         context->page_fault = paging64_page_fault;
954         context->inval_page = paging_inval_page;
955         context->gva_to_gpa = paging64_gva_to_gpa;
956         context->free = paging_free;
957         context->root_level = level;
958         context->shadow_root_level = level;
959         mmu_alloc_roots(vcpu);
960         ASSERT(VALID_PAGE(context->root_hpa));
961         kvm_arch_ops->set_cr3(vcpu, context->root_hpa |
962                     (vcpu->cr3 & (CR3_PCD_MASK | CR3_WPT_MASK)));
963         return 0;
964 }
965
966 static int paging64_init_context(struct kvm_vcpu *vcpu)
967 {
968         return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
969 }
970
971 static int paging32_init_context(struct kvm_vcpu *vcpu)
972 {
973         struct kvm_mmu *context = &vcpu->mmu;
974
975         context->new_cr3 = paging_new_cr3;
976         context->page_fault = paging32_page_fault;
977         context->inval_page = paging_inval_page;
978         context->gva_to_gpa = paging32_gva_to_gpa;
979         context->free = paging_free;
980         context->root_level = PT32_ROOT_LEVEL;
981         context->shadow_root_level = PT32E_ROOT_LEVEL;
982         mmu_alloc_roots(vcpu);
983         ASSERT(VALID_PAGE(context->root_hpa));
984         kvm_arch_ops->set_cr3(vcpu, context->root_hpa |
985                     (vcpu->cr3 & (CR3_PCD_MASK | CR3_WPT_MASK)));
986         return 0;
987 }
988
989 static int paging32E_init_context(struct kvm_vcpu *vcpu)
990 {
991         return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
992 }
993
994 static int init_kvm_mmu(struct kvm_vcpu *vcpu)
995 {
996         ASSERT(vcpu);
997         ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
998
999         if (!is_paging(vcpu))
1000                 return nonpaging_init_context(vcpu);
1001         else if (is_long_mode(vcpu))
1002                 return paging64_init_context(vcpu);
1003         else if (is_pae(vcpu))
1004                 return paging32E_init_context(vcpu);
1005         else
1006                 return paging32_init_context(vcpu);
1007 }
1008
1009 static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
1010 {
1011         ASSERT(vcpu);
1012         if (VALID_PAGE(vcpu->mmu.root_hpa)) {
1013                 vcpu->mmu.free(vcpu);
1014                 vcpu->mmu.root_hpa = INVALID_PAGE;
1015         }
1016 }
1017
1018 int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
1019 {
1020         destroy_kvm_mmu(vcpu);
1021         return init_kvm_mmu(vcpu);
1022 }
1023
1024 void kvm_mmu_pre_write(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes)
1025 {
1026         gfn_t gfn = gpa >> PAGE_SHIFT;
1027         struct kvm_mmu_page *page;
1028         struct kvm_mmu_page *child;
1029         struct hlist_node *node;
1030         struct hlist_head *bucket;
1031         unsigned index;
1032         u64 *spte;
1033         u64 pte;
1034         unsigned offset = offset_in_page(gpa);
1035         unsigned page_offset;
1036         int level;
1037
1038         pgprintk("%s: gpa %llx bytes %d\n", __FUNCTION__, gpa, bytes);
1039         index = kvm_page_table_hashfn(gfn) % KVM_NUM_MMU_PAGES;
1040         bucket = &vcpu->kvm->mmu_page_hash[index];
1041         hlist_for_each_entry(page, node, bucket, hash_link) {
1042                 if (page->gfn != gfn || page->role.metaphysical)
1043                         continue;
1044                 page_offset = offset;
1045                 level = page->role.level;
1046                 if (page->role.glevels == PT32_ROOT_LEVEL) {
1047                         page_offset <<= 1;          /* 32->64 */
1048                         page_offset &= ~PAGE_MASK;
1049                 }
1050                 spte = __va(page->page_hpa);
1051                 spte += page_offset / sizeof(*spte);
1052                 pte = *spte;
1053                 if (is_present_pte(pte)) {
1054                         if (level == PT_PAGE_TABLE_LEVEL)
1055                                 rmap_remove(vcpu->kvm, spte);
1056                         else {
1057                                 child = page_header(pte & PT64_BASE_ADDR_MASK);
1058                                 mmu_page_remove_parent_pte(child, spte);
1059                         }
1060                 }
1061                 *spte = 0;
1062         }
1063 }
1064
1065 void kvm_mmu_post_write(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes)
1066 {
1067 }
1068
1069 int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
1070 {
1071         gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, gva);
1072
1073         return kvm_mmu_unprotect_page(vcpu, gpa >> PAGE_SHIFT);
1074 }
1075
1076 void kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
1077 {
1078         while (vcpu->kvm->n_free_mmu_pages < KVM_REFILL_PAGES) {
1079                 struct kvm_mmu_page *page;
1080
1081                 page = container_of(vcpu->kvm->active_mmu_pages.prev,
1082                                     struct kvm_mmu_page, link);
1083                 kvm_mmu_zap_page(vcpu, page);
1084         }
1085 }
1086 EXPORT_SYMBOL_GPL(kvm_mmu_free_some_pages);
1087
1088 static void free_mmu_pages(struct kvm_vcpu *vcpu)
1089 {
1090         while (!list_empty(&vcpu->free_pages)) {
1091                 struct kvm_mmu_page *page;
1092
1093                 page = list_entry(vcpu->free_pages.next,
1094                                   struct kvm_mmu_page, link);
1095                 list_del(&page->link);
1096                 __free_page(pfn_to_page(page->page_hpa >> PAGE_SHIFT));
1097                 page->page_hpa = INVALID_PAGE;
1098         }
1099         free_page((unsigned long)vcpu->mmu.pae_root);
1100 }
1101
1102 static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
1103 {
1104         struct page *page;
1105         int i;
1106
1107         ASSERT(vcpu);
1108
1109         for (i = 0; i < KVM_NUM_MMU_PAGES; i++) {
1110                 struct kvm_mmu_page *page_header = &vcpu->page_header_buf[i];
1111
1112                 INIT_LIST_HEAD(&page_header->link);
1113                 if ((page = alloc_page(GFP_KERNEL)) == NULL)
1114                         goto error_1;
1115                 page->private = (unsigned long)page_header;
1116                 page_header->page_hpa = (hpa_t)page_to_pfn(page) << PAGE_SHIFT;
1117                 memset(__va(page_header->page_hpa), 0, PAGE_SIZE);
1118                 list_add(&page_header->link, &vcpu->free_pages);
1119                 ++vcpu->kvm->n_free_mmu_pages;
1120         }
1121
1122         /*
1123          * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
1124          * Therefore we need to allocate shadow page tables in the first
1125          * 4GB of memory, which happens to fit the DMA32 zone.
1126          */
1127         page = alloc_page(GFP_KERNEL | __GFP_DMA32);
1128         if (!page)
1129                 goto error_1;
1130         vcpu->mmu.pae_root = page_address(page);
1131         for (i = 0; i < 4; ++i)
1132                 vcpu->mmu.pae_root[i] = INVALID_PAGE;
1133
1134         return 0;
1135
1136 error_1:
1137         free_mmu_pages(vcpu);
1138         return -ENOMEM;
1139 }
1140
1141 int kvm_mmu_create(struct kvm_vcpu *vcpu)
1142 {
1143         ASSERT(vcpu);
1144         ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1145         ASSERT(list_empty(&vcpu->free_pages));
1146
1147         return alloc_mmu_pages(vcpu);
1148 }
1149
1150 int kvm_mmu_setup(struct kvm_vcpu *vcpu)
1151 {
1152         ASSERT(vcpu);
1153         ASSERT(!VALID_PAGE(vcpu->mmu.root_hpa));
1154         ASSERT(!list_empty(&vcpu->free_pages));
1155
1156         return init_kvm_mmu(vcpu);
1157 }
1158
1159 void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
1160 {
1161         ASSERT(vcpu);
1162
1163         destroy_kvm_mmu(vcpu);
1164         free_mmu_pages(vcpu);
1165 }
1166
1167 void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
1168 {
1169         struct kvm_mmu_page *page;
1170
1171         list_for_each_entry(page, &kvm->active_mmu_pages, link) {
1172                 int i;
1173                 u64 *pt;
1174
1175                 if (!test_bit(slot, &page->slot_bitmap))
1176                         continue;
1177
1178                 pt = __va(page->page_hpa);
1179                 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
1180                         /* avoid RMW */
1181                         if (pt[i] & PT_WRITABLE_MASK) {
1182                                 rmap_remove(kvm, &pt[i]);
1183                                 pt[i] &= ~PT_WRITABLE_MASK;
1184                         }
1185         }
1186 }