Merge tag 'v3.10.72' into linux-linaro-lsk
[firefly-linux-kernel-4.4.55.git] / arch / x86 / kvm / paging_tmpl.h
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  * Copyright 2010 Red Hat, Inc. and/or its affiliates.
11  *
12  * Authors:
13  *   Yaniv Kamay  <yaniv@qumranet.com>
14  *   Avi Kivity   <avi@qumranet.com>
15  *
16  * This work is licensed under the terms of the GNU GPL, version 2.  See
17  * the COPYING file in the top-level directory.
18  *
19  */
20
21 /*
22  * We need the mmu code to access both 32-bit and 64-bit guest ptes,
23  * so the code in this file is compiled twice, once per pte size.
24  */
25
26 #if PTTYPE == 64
27         #define pt_element_t u64
28         #define guest_walker guest_walker64
29         #define FNAME(name) paging##64_##name
30         #define PT_BASE_ADDR_MASK PT64_BASE_ADDR_MASK
31         #define PT_LVL_ADDR_MASK(lvl) PT64_LVL_ADDR_MASK(lvl)
32         #define PT_LVL_OFFSET_MASK(lvl) PT64_LVL_OFFSET_MASK(lvl)
33         #define PT_INDEX(addr, level) PT64_INDEX(addr, level)
34         #define PT_LEVEL_BITS PT64_LEVEL_BITS
35         #ifdef CONFIG_X86_64
36         #define PT_MAX_FULL_LEVELS 4
37         #define CMPXCHG cmpxchg
38         #else
39         #define CMPXCHG cmpxchg64
40         #define PT_MAX_FULL_LEVELS 2
41         #endif
42 #elif PTTYPE == 32
43         #define pt_element_t u32
44         #define guest_walker guest_walker32
45         #define FNAME(name) paging##32_##name
46         #define PT_BASE_ADDR_MASK PT32_BASE_ADDR_MASK
47         #define PT_LVL_ADDR_MASK(lvl) PT32_LVL_ADDR_MASK(lvl)
48         #define PT_LVL_OFFSET_MASK(lvl) PT32_LVL_OFFSET_MASK(lvl)
49         #define PT_INDEX(addr, level) PT32_INDEX(addr, level)
50         #define PT_LEVEL_BITS PT32_LEVEL_BITS
51         #define PT_MAX_FULL_LEVELS 2
52         #define CMPXCHG cmpxchg
53 #else
54         #error Invalid PTTYPE value
55 #endif
56
57 #define gpte_to_gfn_lvl FNAME(gpte_to_gfn_lvl)
58 #define gpte_to_gfn(pte) gpte_to_gfn_lvl((pte), PT_PAGE_TABLE_LEVEL)
59
60 /*
61  * The guest_walker structure emulates the behavior of the hardware page
62  * table walker.
63  */
64 struct guest_walker {
65         int level;
66         unsigned max_level;
67         gfn_t table_gfn[PT_MAX_FULL_LEVELS];
68         pt_element_t ptes[PT_MAX_FULL_LEVELS];
69         pt_element_t prefetch_ptes[PTE_PREFETCH_NUM];
70         gpa_t pte_gpa[PT_MAX_FULL_LEVELS];
71         pt_element_t __user *ptep_user[PT_MAX_FULL_LEVELS];
72         bool pte_writable[PT_MAX_FULL_LEVELS];
73         unsigned pt_access;
74         unsigned pte_access;
75         gfn_t gfn;
76         struct x86_exception fault;
77 };
78
79 static gfn_t gpte_to_gfn_lvl(pt_element_t gpte, int lvl)
80 {
81         return (gpte & PT_LVL_ADDR_MASK(lvl)) >> PAGE_SHIFT;
82 }
83
84 static int FNAME(cmpxchg_gpte)(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
85                                pt_element_t __user *ptep_user, unsigned index,
86                                pt_element_t orig_pte, pt_element_t new_pte)
87 {
88         int npages;
89         pt_element_t ret;
90         pt_element_t *table;
91         struct page *page;
92
93         npages = get_user_pages_fast((unsigned long)ptep_user, 1, 1, &page);
94         /* Check if the user is doing something meaningless. */
95         if (unlikely(npages != 1))
96                 return -EFAULT;
97
98         table = kmap_atomic(page);
99         ret = CMPXCHG(&table[index], orig_pte, new_pte);
100         kunmap_atomic(table);
101
102         kvm_release_page_dirty(page);
103
104         return (ret != orig_pte);
105 }
106
107 static int FNAME(update_accessed_dirty_bits)(struct kvm_vcpu *vcpu,
108                                              struct kvm_mmu *mmu,
109                                              struct guest_walker *walker,
110                                              int write_fault)
111 {
112         unsigned level, index;
113         pt_element_t pte, orig_pte;
114         pt_element_t __user *ptep_user;
115         gfn_t table_gfn;
116         int ret;
117
118         for (level = walker->max_level; level >= walker->level; --level) {
119                 pte = orig_pte = walker->ptes[level - 1];
120                 table_gfn = walker->table_gfn[level - 1];
121                 ptep_user = walker->ptep_user[level - 1];
122                 index = offset_in_page(ptep_user) / sizeof(pt_element_t);
123                 if (!(pte & PT_ACCESSED_MASK)) {
124                         trace_kvm_mmu_set_accessed_bit(table_gfn, index, sizeof(pte));
125                         pte |= PT_ACCESSED_MASK;
126                 }
127                 if (level == walker->level && write_fault && !is_dirty_gpte(pte)) {
128                         trace_kvm_mmu_set_dirty_bit(table_gfn, index, sizeof(pte));
129                         pte |= PT_DIRTY_MASK;
130                 }
131                 if (pte == orig_pte)
132                         continue;
133
134                 /*
135                  * If the slot is read-only, simply do not process the accessed
136                  * and dirty bits.  This is the correct thing to do if the slot
137                  * is ROM, and page tables in read-as-ROM/write-as-MMIO slots
138                  * are only supported if the accessed and dirty bits are already
139                  * set in the ROM (so that MMIO writes are never needed).
140                  *
141                  * Note that NPT does not allow this at all and faults, since
142                  * it always wants nested page table entries for the guest
143                  * page tables to be writable.  And EPT works but will simply
144                  * overwrite the read-only memory to set the accessed and dirty
145                  * bits.
146                  */
147                 if (unlikely(!walker->pte_writable[level - 1]))
148                         continue;
149
150                 ret = FNAME(cmpxchg_gpte)(vcpu, mmu, ptep_user, index, orig_pte, pte);
151                 if (ret)
152                         return ret;
153
154                 mark_page_dirty(vcpu->kvm, table_gfn);
155                 walker->ptes[level] = pte;
156         }
157         return 0;
158 }
159
160 /*
161  * Fetch a guest pte for a guest virtual address
162  */
163 static int FNAME(walk_addr_generic)(struct guest_walker *walker,
164                                     struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
165                                     gva_t addr, u32 access)
166 {
167         int ret;
168         pt_element_t pte;
169         pt_element_t __user *uninitialized_var(ptep_user);
170         gfn_t table_gfn;
171         unsigned index, pt_access, pte_access, accessed_dirty;
172         gpa_t pte_gpa;
173         int offset;
174         const int write_fault = access & PFERR_WRITE_MASK;
175         const int user_fault  = access & PFERR_USER_MASK;
176         const int fetch_fault = access & PFERR_FETCH_MASK;
177         u16 errcode = 0;
178         gpa_t real_gpa;
179         gfn_t gfn;
180
181         trace_kvm_mmu_pagetable_walk(addr, access);
182 retry_walk:
183         walker->level = mmu->root_level;
184         pte           = mmu->get_cr3(vcpu);
185
186 #if PTTYPE == 64
187         if (walker->level == PT32E_ROOT_LEVEL) {
188                 pte = mmu->get_pdptr(vcpu, (addr >> 30) & 3);
189                 trace_kvm_mmu_paging_element(pte, walker->level);
190                 if (!is_present_gpte(pte))
191                         goto error;
192                 --walker->level;
193         }
194 #endif
195         walker->max_level = walker->level;
196         ASSERT((!is_long_mode(vcpu) && is_pae(vcpu)) ||
197                (mmu->get_cr3(vcpu) & CR3_NONPAE_RESERVED_BITS) == 0);
198
199         accessed_dirty = PT_ACCESSED_MASK;
200         pt_access = pte_access = ACC_ALL;
201         ++walker->level;
202
203         do {
204                 gfn_t real_gfn;
205                 unsigned long host_addr;
206
207                 pt_access &= pte_access;
208                 --walker->level;
209
210                 index = PT_INDEX(addr, walker->level);
211
212                 table_gfn = gpte_to_gfn(pte);
213                 offset    = index * sizeof(pt_element_t);
214                 pte_gpa   = gfn_to_gpa(table_gfn) + offset;
215                 walker->table_gfn[walker->level - 1] = table_gfn;
216                 walker->pte_gpa[walker->level - 1] = pte_gpa;
217
218                 real_gfn = mmu->translate_gpa(vcpu, gfn_to_gpa(table_gfn),
219                                               PFERR_USER_MASK|PFERR_WRITE_MASK);
220                 if (unlikely(real_gfn == UNMAPPED_GVA))
221                         goto error;
222                 real_gfn = gpa_to_gfn(real_gfn);
223
224                 host_addr = gfn_to_hva_prot(vcpu->kvm, real_gfn,
225                                             &walker->pte_writable[walker->level - 1]);
226                 if (unlikely(kvm_is_error_hva(host_addr)))
227                         goto error;
228
229                 ptep_user = (pt_element_t __user *)((void *)host_addr + offset);
230                 if (unlikely(__copy_from_user(&pte, ptep_user, sizeof(pte))))
231                         goto error;
232                 walker->ptep_user[walker->level - 1] = ptep_user;
233
234                 trace_kvm_mmu_paging_element(pte, walker->level);
235
236                 if (unlikely(!is_present_gpte(pte)))
237                         goto error;
238
239                 if (unlikely(is_rsvd_bits_set(&vcpu->arch.mmu, pte,
240                                               walker->level))) {
241                         errcode |= PFERR_RSVD_MASK | PFERR_PRESENT_MASK;
242                         goto error;
243                 }
244
245                 accessed_dirty &= pte;
246                 pte_access = pt_access & gpte_access(vcpu, pte);
247
248                 walker->ptes[walker->level - 1] = pte;
249         } while (!is_last_gpte(mmu, walker->level, pte));
250
251         if (unlikely(permission_fault(mmu, pte_access, access))) {
252                 errcode |= PFERR_PRESENT_MASK;
253                 goto error;
254         }
255
256         gfn = gpte_to_gfn_lvl(pte, walker->level);
257         gfn += (addr & PT_LVL_OFFSET_MASK(walker->level)) >> PAGE_SHIFT;
258
259         if (PTTYPE == 32 && walker->level == PT_DIRECTORY_LEVEL && is_cpuid_PSE36())
260                 gfn += pse36_gfn_delta(pte);
261
262         real_gpa = mmu->translate_gpa(vcpu, gfn_to_gpa(gfn), access);
263         if (real_gpa == UNMAPPED_GVA)
264                 return 0;
265
266         walker->gfn = real_gpa >> PAGE_SHIFT;
267
268         if (!write_fault)
269                 protect_clean_gpte(&pte_access, pte);
270         else
271                 /*
272                  * On a write fault, fold the dirty bit into accessed_dirty by
273                  * shifting it one place right.
274                  */
275                 accessed_dirty &= pte >> (PT_DIRTY_SHIFT - PT_ACCESSED_SHIFT);
276
277         if (unlikely(!accessed_dirty)) {
278                 ret = FNAME(update_accessed_dirty_bits)(vcpu, mmu, walker, write_fault);
279                 if (unlikely(ret < 0))
280                         goto error;
281                 else if (ret)
282                         goto retry_walk;
283         }
284
285         walker->pt_access = pt_access;
286         walker->pte_access = pte_access;
287         pgprintk("%s: pte %llx pte_access %x pt_access %x\n",
288                  __func__, (u64)pte, pte_access, pt_access);
289         return 1;
290
291 error:
292         errcode |= write_fault | user_fault;
293         if (fetch_fault && (mmu->nx ||
294                             kvm_read_cr4_bits(vcpu, X86_CR4_SMEP)))
295                 errcode |= PFERR_FETCH_MASK;
296
297         walker->fault.vector = PF_VECTOR;
298         walker->fault.error_code_valid = true;
299         walker->fault.error_code = errcode;
300         walker->fault.address = addr;
301         walker->fault.nested_page_fault = mmu != vcpu->arch.walk_mmu;
302
303         trace_kvm_mmu_walker_error(walker->fault.error_code);
304         return 0;
305 }
306
307 static int FNAME(walk_addr)(struct guest_walker *walker,
308                             struct kvm_vcpu *vcpu, gva_t addr, u32 access)
309 {
310         return FNAME(walk_addr_generic)(walker, vcpu, &vcpu->arch.mmu, addr,
311                                         access);
312 }
313
314 static int FNAME(walk_addr_nested)(struct guest_walker *walker,
315                                    struct kvm_vcpu *vcpu, gva_t addr,
316                                    u32 access)
317 {
318         return FNAME(walk_addr_generic)(walker, vcpu, &vcpu->arch.nested_mmu,
319                                         addr, access);
320 }
321
322 static bool
323 FNAME(prefetch_gpte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
324                      u64 *spte, pt_element_t gpte, bool no_dirty_log)
325 {
326         unsigned pte_access;
327         gfn_t gfn;
328         pfn_t pfn;
329
330         if (prefetch_invalid_gpte(vcpu, sp, spte, gpte))
331                 return false;
332
333         pgprintk("%s: gpte %llx spte %p\n", __func__, (u64)gpte, spte);
334
335         gfn = gpte_to_gfn(gpte);
336         pte_access = sp->role.access & gpte_access(vcpu, gpte);
337         protect_clean_gpte(&pte_access, gpte);
338         pfn = pte_prefetch_gfn_to_pfn(vcpu, gfn,
339                         no_dirty_log && (pte_access & ACC_WRITE_MASK));
340         if (is_error_pfn(pfn))
341                 return false;
342
343         /*
344          * we call mmu_set_spte() with host_writable = true because
345          * pte_prefetch_gfn_to_pfn always gets a writable pfn.
346          */
347         mmu_set_spte(vcpu, spte, pte_access, 0, NULL, PT_PAGE_TABLE_LEVEL,
348                      gfn, pfn, true, true);
349
350         return true;
351 }
352
353 static void FNAME(update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
354                               u64 *spte, const void *pte)
355 {
356         pt_element_t gpte = *(const pt_element_t *)pte;
357
358         FNAME(prefetch_gpte)(vcpu, sp, spte, gpte, false);
359 }
360
361 static bool FNAME(gpte_changed)(struct kvm_vcpu *vcpu,
362                                 struct guest_walker *gw, int level)
363 {
364         pt_element_t curr_pte;
365         gpa_t base_gpa, pte_gpa = gw->pte_gpa[level - 1];
366         u64 mask;
367         int r, index;
368
369         if (level == PT_PAGE_TABLE_LEVEL) {
370                 mask = PTE_PREFETCH_NUM * sizeof(pt_element_t) - 1;
371                 base_gpa = pte_gpa & ~mask;
372                 index = (pte_gpa - base_gpa) / sizeof(pt_element_t);
373
374                 r = kvm_read_guest_atomic(vcpu->kvm, base_gpa,
375                                 gw->prefetch_ptes, sizeof(gw->prefetch_ptes));
376                 curr_pte = gw->prefetch_ptes[index];
377         } else
378                 r = kvm_read_guest_atomic(vcpu->kvm, pte_gpa,
379                                   &curr_pte, sizeof(curr_pte));
380
381         return r || curr_pte != gw->ptes[level - 1];
382 }
383
384 static void FNAME(pte_prefetch)(struct kvm_vcpu *vcpu, struct guest_walker *gw,
385                                 u64 *sptep)
386 {
387         struct kvm_mmu_page *sp;
388         pt_element_t *gptep = gw->prefetch_ptes;
389         u64 *spte;
390         int i;
391
392         sp = page_header(__pa(sptep));
393
394         if (sp->role.level > PT_PAGE_TABLE_LEVEL)
395                 return;
396
397         if (sp->role.direct)
398                 return __direct_pte_prefetch(vcpu, sp, sptep);
399
400         i = (sptep - sp->spt) & ~(PTE_PREFETCH_NUM - 1);
401         spte = sp->spt + i;
402
403         for (i = 0; i < PTE_PREFETCH_NUM; i++, spte++) {
404                 if (spte == sptep)
405                         continue;
406
407                 if (is_shadow_present_pte(*spte))
408                         continue;
409
410                 if (!FNAME(prefetch_gpte)(vcpu, sp, spte, gptep[i], true))
411                         break;
412         }
413 }
414
415 /*
416  * Fetch a shadow pte for a specific level in the paging hierarchy.
417  * If the guest tries to write a write-protected page, we need to
418  * emulate this operation, return 1 to indicate this case.
419  */
420 static int FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
421                          struct guest_walker *gw,
422                          int write_fault, int hlevel,
423                          pfn_t pfn, bool map_writable, bool prefault)
424 {
425         struct kvm_mmu_page *sp = NULL;
426         struct kvm_shadow_walk_iterator it;
427         unsigned direct_access, access = gw->pt_access;
428         int top_level, emulate = 0;
429
430         direct_access = gw->pte_access;
431
432         top_level = vcpu->arch.mmu.root_level;
433         if (top_level == PT32E_ROOT_LEVEL)
434                 top_level = PT32_ROOT_LEVEL;
435         /*
436          * Verify that the top-level gpte is still there.  Since the page
437          * is a root page, it is either write protected (and cannot be
438          * changed from now on) or it is invalid (in which case, we don't
439          * really care if it changes underneath us after this point).
440          */
441         if (FNAME(gpte_changed)(vcpu, gw, top_level))
442                 goto out_gpte_changed;
443
444         if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
445                 goto out_gpte_changed;
446
447         for (shadow_walk_init(&it, vcpu, addr);
448              shadow_walk_okay(&it) && it.level > gw->level;
449              shadow_walk_next(&it)) {
450                 gfn_t table_gfn;
451
452                 clear_sp_write_flooding_count(it.sptep);
453                 drop_large_spte(vcpu, it.sptep);
454
455                 sp = NULL;
456                 if (!is_shadow_present_pte(*it.sptep)) {
457                         table_gfn = gw->table_gfn[it.level - 2];
458                         sp = kvm_mmu_get_page(vcpu, table_gfn, addr, it.level-1,
459                                               false, access, it.sptep);
460                 }
461
462                 /*
463                  * Verify that the gpte in the page we've just write
464                  * protected is still there.
465                  */
466                 if (FNAME(gpte_changed)(vcpu, gw, it.level - 1))
467                         goto out_gpte_changed;
468
469                 if (sp)
470                         link_shadow_page(it.sptep, sp);
471         }
472
473         for (;
474              shadow_walk_okay(&it) && it.level > hlevel;
475              shadow_walk_next(&it)) {
476                 gfn_t direct_gfn;
477
478                 clear_sp_write_flooding_count(it.sptep);
479                 validate_direct_spte(vcpu, it.sptep, direct_access);
480
481                 drop_large_spte(vcpu, it.sptep);
482
483                 if (is_shadow_present_pte(*it.sptep))
484                         continue;
485
486                 direct_gfn = gw->gfn & ~(KVM_PAGES_PER_HPAGE(it.level) - 1);
487
488                 sp = kvm_mmu_get_page(vcpu, direct_gfn, addr, it.level-1,
489                                       true, direct_access, it.sptep);
490                 link_shadow_page(it.sptep, sp);
491         }
492
493         clear_sp_write_flooding_count(it.sptep);
494         mmu_set_spte(vcpu, it.sptep, gw->pte_access, write_fault, &emulate,
495                      it.level, gw->gfn, pfn, prefault, map_writable);
496         FNAME(pte_prefetch)(vcpu, gw, it.sptep);
497
498         return emulate;
499
500 out_gpte_changed:
501         if (sp)
502                 kvm_mmu_put_page(sp, it.sptep);
503         kvm_release_pfn_clean(pfn);
504         return 0;
505 }
506
507  /*
508  * To see whether the mapped gfn can write its page table in the current
509  * mapping.
510  *
511  * It is the helper function of FNAME(page_fault). When guest uses large page
512  * size to map the writable gfn which is used as current page table, we should
513  * force kvm to use small page size to map it because new shadow page will be
514  * created when kvm establishes shadow page table that stop kvm using large
515  * page size. Do it early can avoid unnecessary #PF and emulation.
516  *
517  * @write_fault_to_shadow_pgtable will return true if the fault gfn is
518  * currently used as its page table.
519  *
520  * Note: the PDPT page table is not checked for PAE-32 bit guest. It is ok
521  * since the PDPT is always shadowed, that means, we can not use large page
522  * size to map the gfn which is used as PDPT.
523  */
524 static bool
525 FNAME(is_self_change_mapping)(struct kvm_vcpu *vcpu,
526                               struct guest_walker *walker, int user_fault,
527                               bool *write_fault_to_shadow_pgtable)
528 {
529         int level;
530         gfn_t mask = ~(KVM_PAGES_PER_HPAGE(walker->level) - 1);
531         bool self_changed = false;
532
533         if (!(walker->pte_access & ACC_WRITE_MASK ||
534               (!is_write_protection(vcpu) && !user_fault)))
535                 return false;
536
537         for (level = walker->level; level <= walker->max_level; level++) {
538                 gfn_t gfn = walker->gfn ^ walker->table_gfn[level - 1];
539
540                 self_changed |= !(gfn & mask);
541                 *write_fault_to_shadow_pgtable |= !gfn;
542         }
543
544         return self_changed;
545 }
546
547 /*
548  * Page fault handler.  There are several causes for a page fault:
549  *   - there is no shadow pte for the guest pte
550  *   - write access through a shadow pte marked read only so that we can set
551  *     the dirty bit
552  *   - write access to a shadow pte marked read only so we can update the page
553  *     dirty bitmap, when userspace requests it
554  *   - mmio access; in this case we will never install a present shadow pte
555  *   - normal guest page fault due to the guest pte marked not present, not
556  *     writable, or not executable
557  *
558  *  Returns: 1 if we need to emulate the instruction, 0 otherwise, or
559  *           a negative value on error.
560  */
561 static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr, u32 error_code,
562                              bool prefault)
563 {
564         int write_fault = error_code & PFERR_WRITE_MASK;
565         int user_fault = error_code & PFERR_USER_MASK;
566         struct guest_walker walker;
567         int r;
568         pfn_t pfn;
569         int level = PT_PAGE_TABLE_LEVEL;
570         int force_pt_level;
571         unsigned long mmu_seq;
572         bool map_writable, is_self_change_mapping;
573
574         pgprintk("%s: addr %lx err %x\n", __func__, addr, error_code);
575
576         if (unlikely(error_code & PFERR_RSVD_MASK))
577                 return handle_mmio_page_fault(vcpu, addr, error_code,
578                                               mmu_is_nested(vcpu));
579
580         r = mmu_topup_memory_caches(vcpu);
581         if (r)
582                 return r;
583
584         /*
585          * Look up the guest pte for the faulting address.
586          */
587         r = FNAME(walk_addr)(&walker, vcpu, addr, error_code);
588
589         /*
590          * The page is not mapped by the guest.  Let the guest handle it.
591          */
592         if (!r) {
593                 pgprintk("%s: guest page fault\n", __func__);
594                 if (!prefault)
595                         inject_page_fault(vcpu, &walker.fault);
596
597                 return 0;
598         }
599
600         vcpu->arch.write_fault_to_shadow_pgtable = false;
601
602         is_self_change_mapping = FNAME(is_self_change_mapping)(vcpu,
603               &walker, user_fault, &vcpu->arch.write_fault_to_shadow_pgtable);
604
605         if (walker.level >= PT_DIRECTORY_LEVEL)
606                 force_pt_level = mapping_level_dirty_bitmap(vcpu, walker.gfn)
607                    || is_self_change_mapping;
608         else
609                 force_pt_level = 1;
610         if (!force_pt_level) {
611                 level = min(walker.level, mapping_level(vcpu, walker.gfn));
612                 walker.gfn = walker.gfn & ~(KVM_PAGES_PER_HPAGE(level) - 1);
613         }
614
615         mmu_seq = vcpu->kvm->mmu_notifier_seq;
616         smp_rmb();
617
618         if (try_async_pf(vcpu, prefault, walker.gfn, addr, &pfn, write_fault,
619                          &map_writable))
620                 return 0;
621
622         if (handle_abnormal_pfn(vcpu, mmu_is_nested(vcpu) ? 0 : addr,
623                                 walker.gfn, pfn, walker.pte_access, &r))
624                 return r;
625
626         /*
627          * Do not change pte_access if the pfn is a mmio page, otherwise
628          * we will cache the incorrect access into mmio spte.
629          */
630         if (write_fault && !(walker.pte_access & ACC_WRITE_MASK) &&
631              !is_write_protection(vcpu) && !user_fault &&
632               !is_noslot_pfn(pfn)) {
633                 walker.pte_access |= ACC_WRITE_MASK;
634                 walker.pte_access &= ~ACC_USER_MASK;
635
636                 /*
637                  * If we converted a user page to a kernel page,
638                  * so that the kernel can write to it when cr0.wp=0,
639                  * then we should prevent the kernel from executing it
640                  * if SMEP is enabled.
641                  */
642                 if (kvm_read_cr4_bits(vcpu, X86_CR4_SMEP))
643                         walker.pte_access &= ~ACC_EXEC_MASK;
644         }
645
646         spin_lock(&vcpu->kvm->mmu_lock);
647         if (mmu_notifier_retry(vcpu->kvm, mmu_seq))
648                 goto out_unlock;
649
650         kvm_mmu_audit(vcpu, AUDIT_PRE_PAGE_FAULT);
651         make_mmu_pages_available(vcpu);
652         if (!force_pt_level)
653                 transparent_hugepage_adjust(vcpu, &walker.gfn, &pfn, &level);
654         r = FNAME(fetch)(vcpu, addr, &walker, write_fault,
655                          level, pfn, map_writable, prefault);
656         ++vcpu->stat.pf_fixed;
657         kvm_mmu_audit(vcpu, AUDIT_POST_PAGE_FAULT);
658         spin_unlock(&vcpu->kvm->mmu_lock);
659
660         return r;
661
662 out_unlock:
663         spin_unlock(&vcpu->kvm->mmu_lock);
664         kvm_release_pfn_clean(pfn);
665         return 0;
666 }
667
668 static gpa_t FNAME(get_level1_sp_gpa)(struct kvm_mmu_page *sp)
669 {
670         int offset = 0;
671
672         WARN_ON(sp->role.level != PT_PAGE_TABLE_LEVEL);
673
674         if (PTTYPE == 32)
675                 offset = sp->role.quadrant << PT64_LEVEL_BITS;
676
677         return gfn_to_gpa(sp->gfn) + offset * sizeof(pt_element_t);
678 }
679
680 static void FNAME(invlpg)(struct kvm_vcpu *vcpu, gva_t gva)
681 {
682         struct kvm_shadow_walk_iterator iterator;
683         struct kvm_mmu_page *sp;
684         int level;
685         u64 *sptep;
686
687         vcpu_clear_mmio_info(vcpu, gva);
688
689         /*
690          * No need to check return value here, rmap_can_add() can
691          * help us to skip pte prefetch later.
692          */
693         mmu_topup_memory_caches(vcpu);
694
695         if (!VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
696                 WARN_ON(1);
697                 return;
698         }
699
700         spin_lock(&vcpu->kvm->mmu_lock);
701         for_each_shadow_entry(vcpu, gva, iterator) {
702                 level = iterator.level;
703                 sptep = iterator.sptep;
704
705                 sp = page_header(__pa(sptep));
706                 if (is_last_spte(*sptep, level)) {
707                         pt_element_t gpte;
708                         gpa_t pte_gpa;
709
710                         if (!sp->unsync)
711                                 break;
712
713                         pte_gpa = FNAME(get_level1_sp_gpa)(sp);
714                         pte_gpa += (sptep - sp->spt) * sizeof(pt_element_t);
715
716                         if (mmu_page_zap_pte(vcpu->kvm, sp, sptep))
717                                 kvm_flush_remote_tlbs(vcpu->kvm);
718
719                         if (!rmap_can_add(vcpu))
720                                 break;
721
722                         if (kvm_read_guest_atomic(vcpu->kvm, pte_gpa, &gpte,
723                                                   sizeof(pt_element_t)))
724                                 break;
725
726                         FNAME(update_pte)(vcpu, sp, sptep, &gpte);
727                 }
728
729                 if (!is_shadow_present_pte(*sptep) || !sp->unsync_children)
730                         break;
731         }
732         spin_unlock(&vcpu->kvm->mmu_lock);
733 }
734
735 static gpa_t FNAME(gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t vaddr, u32 access,
736                                struct x86_exception *exception)
737 {
738         struct guest_walker walker;
739         gpa_t gpa = UNMAPPED_GVA;
740         int r;
741
742         r = FNAME(walk_addr)(&walker, vcpu, vaddr, access);
743
744         if (r) {
745                 gpa = gfn_to_gpa(walker.gfn);
746                 gpa |= vaddr & ~PAGE_MASK;
747         } else if (exception)
748                 *exception = walker.fault;
749
750         return gpa;
751 }
752
753 static gpa_t FNAME(gva_to_gpa_nested)(struct kvm_vcpu *vcpu, gva_t vaddr,
754                                       u32 access,
755                                       struct x86_exception *exception)
756 {
757         struct guest_walker walker;
758         gpa_t gpa = UNMAPPED_GVA;
759         int r;
760
761         r = FNAME(walk_addr_nested)(&walker, vcpu, vaddr, access);
762
763         if (r) {
764                 gpa = gfn_to_gpa(walker.gfn);
765                 gpa |= vaddr & ~PAGE_MASK;
766         } else if (exception)
767                 *exception = walker.fault;
768
769         return gpa;
770 }
771
772 /*
773  * Using the cached information from sp->gfns is safe because:
774  * - The spte has a reference to the struct page, so the pfn for a given gfn
775  *   can't change unless all sptes pointing to it are nuked first.
776  *
777  * Note:
778  *   We should flush all tlbs if spte is dropped even though guest is
779  *   responsible for it. Since if we don't, kvm_mmu_notifier_invalidate_page
780  *   and kvm_mmu_notifier_invalidate_range_start detect the mapping page isn't
781  *   used by guest then tlbs are not flushed, so guest is allowed to access the
782  *   freed pages.
783  *   And we increase kvm->tlbs_dirty to delay tlbs flush in this case.
784  */
785 static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
786 {
787         int i, nr_present = 0;
788         bool host_writable;
789         gpa_t first_pte_gpa;
790
791         /* direct kvm_mmu_page can not be unsync. */
792         BUG_ON(sp->role.direct);
793
794         first_pte_gpa = FNAME(get_level1_sp_gpa)(sp);
795
796         for (i = 0; i < PT64_ENT_PER_PAGE; i++) {
797                 unsigned pte_access;
798                 pt_element_t gpte;
799                 gpa_t pte_gpa;
800                 gfn_t gfn;
801
802                 if (!sp->spt[i])
803                         continue;
804
805                 pte_gpa = first_pte_gpa + i * sizeof(pt_element_t);
806
807                 if (kvm_read_guest_atomic(vcpu->kvm, pte_gpa, &gpte,
808                                           sizeof(pt_element_t)))
809                         return -EINVAL;
810
811                 if (prefetch_invalid_gpte(vcpu, sp, &sp->spt[i], gpte)) {
812                         vcpu->kvm->tlbs_dirty++;
813                         continue;
814                 }
815
816                 gfn = gpte_to_gfn(gpte);
817                 pte_access = sp->role.access;
818                 pte_access &= gpte_access(vcpu, gpte);
819                 protect_clean_gpte(&pte_access, gpte);
820
821                 if (sync_mmio_spte(&sp->spt[i], gfn, pte_access, &nr_present))
822                         continue;
823
824                 if (gfn != sp->gfns[i]) {
825                         drop_spte(vcpu->kvm, &sp->spt[i]);
826                         vcpu->kvm->tlbs_dirty++;
827                         continue;
828                 }
829
830                 nr_present++;
831
832                 host_writable = sp->spt[i] & SPTE_HOST_WRITEABLE;
833
834                 set_spte(vcpu, &sp->spt[i], pte_access,
835                          PT_PAGE_TABLE_LEVEL, gfn,
836                          spte_to_pfn(sp->spt[i]), true, false,
837                          host_writable);
838         }
839
840         return !nr_present;
841 }
842
843 #undef pt_element_t
844 #undef guest_walker
845 #undef FNAME
846 #undef PT_BASE_ADDR_MASK
847 #undef PT_INDEX
848 #undef PT_LVL_ADDR_MASK
849 #undef PT_LVL_OFFSET_MASK
850 #undef PT_LEVEL_BITS
851 #undef PT_MAX_FULL_LEVELS
852 #undef gpte_to_gfn
853 #undef gpte_to_gfn_lvl
854 #undef CMPXCHG