]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - arch/x86/kvm/paging_tmpl.h
KVM: MMU: optimize to handle dirty bit
[mirror_ubuntu-zesty-kernel.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 gfn_t table_gfn[PT_MAX_FULL_LEVELS];
67 pt_element_t ptes[PT_MAX_FULL_LEVELS];
68 pt_element_t prefetch_ptes[PTE_PREFETCH_NUM];
69 gpa_t pte_gpa[PT_MAX_FULL_LEVELS];
70 unsigned pt_access;
71 unsigned pte_access;
72 gfn_t gfn;
73 struct x86_exception fault;
74 };
75
76 static gfn_t gpte_to_gfn_lvl(pt_element_t gpte, int lvl)
77 {
78 return (gpte & PT_LVL_ADDR_MASK(lvl)) >> PAGE_SHIFT;
79 }
80
81 static int FNAME(cmpxchg_gpte)(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
82 pt_element_t __user *ptep_user, unsigned index,
83 pt_element_t orig_pte, pt_element_t new_pte)
84 {
85 int npages;
86 pt_element_t ret;
87 pt_element_t *table;
88 struct page *page;
89
90 npages = get_user_pages_fast((unsigned long)ptep_user, 1, 1, &page);
91 /* Check if the user is doing something meaningless. */
92 if (unlikely(npages != 1))
93 return -EFAULT;
94
95 table = kmap_atomic(page, KM_USER0);
96 ret = CMPXCHG(&table[index], orig_pte, new_pte);
97 kunmap_atomic(table, KM_USER0);
98
99 kvm_release_page_dirty(page);
100
101 return (ret != orig_pte);
102 }
103
104 static unsigned FNAME(gpte_access)(struct kvm_vcpu *vcpu, pt_element_t gpte,
105 bool last)
106 {
107 unsigned access;
108
109 access = (gpte & (PT_WRITABLE_MASK | PT_USER_MASK)) | ACC_EXEC_MASK;
110 if (last && !is_dirty_gpte(gpte))
111 access &= ~ACC_WRITE_MASK;
112
113 #if PTTYPE == 64
114 if (vcpu->arch.mmu.nx)
115 access &= ~(gpte >> PT64_NX_SHIFT);
116 #endif
117 return access;
118 }
119
120 static bool FNAME(is_last_gpte)(struct guest_walker *walker,
121 struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
122 pt_element_t gpte)
123 {
124 if (walker->level == PT_PAGE_TABLE_LEVEL)
125 return true;
126
127 if ((walker->level == PT_DIRECTORY_LEVEL) && is_large_pte(gpte) &&
128 (PTTYPE == 64 || is_pse(vcpu)))
129 return true;
130
131 if ((walker->level == PT_PDPE_LEVEL) && is_large_pte(gpte) &&
132 (mmu->root_level == PT64_ROOT_LEVEL))
133 return true;
134
135 return false;
136 }
137
138 /*
139 * Fetch a guest pte for a guest virtual address
140 */
141 static int FNAME(walk_addr_generic)(struct guest_walker *walker,
142 struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
143 gva_t addr, u32 access)
144 {
145 pt_element_t pte;
146 pt_element_t __user *uninitialized_var(ptep_user);
147 gfn_t table_gfn;
148 unsigned index, pt_access, uninitialized_var(pte_access);
149 gpa_t pte_gpa;
150 bool eperm;
151 int offset;
152 const int write_fault = access & PFERR_WRITE_MASK;
153 const int user_fault = access & PFERR_USER_MASK;
154 const int fetch_fault = access & PFERR_FETCH_MASK;
155 u16 errcode = 0;
156
157 trace_kvm_mmu_pagetable_walk(addr, write_fault, user_fault,
158 fetch_fault);
159 retry_walk:
160 eperm = false;
161 walker->level = mmu->root_level;
162 pte = mmu->get_cr3(vcpu);
163
164 #if PTTYPE == 64
165 if (walker->level == PT32E_ROOT_LEVEL) {
166 pte = kvm_pdptr_read_mmu(vcpu, mmu, (addr >> 30) & 3);
167 trace_kvm_mmu_paging_element(pte, walker->level);
168 if (!is_present_gpte(pte))
169 goto error;
170 --walker->level;
171 }
172 #endif
173 ASSERT((!is_long_mode(vcpu) && is_pae(vcpu)) ||
174 (mmu->get_cr3(vcpu) & CR3_NONPAE_RESERVED_BITS) == 0);
175
176 pt_access = ACC_ALL;
177
178 for (;;) {
179 gfn_t real_gfn;
180 unsigned long host_addr;
181
182 index = PT_INDEX(addr, walker->level);
183
184 table_gfn = gpte_to_gfn(pte);
185 offset = index * sizeof(pt_element_t);
186 pte_gpa = gfn_to_gpa(table_gfn) + offset;
187 walker->table_gfn[walker->level - 1] = table_gfn;
188 walker->pte_gpa[walker->level - 1] = pte_gpa;
189
190 real_gfn = mmu->translate_gpa(vcpu, gfn_to_gpa(table_gfn),
191 PFERR_USER_MASK|PFERR_WRITE_MASK);
192 if (unlikely(real_gfn == UNMAPPED_GVA))
193 goto error;
194 real_gfn = gpa_to_gfn(real_gfn);
195
196 host_addr = gfn_to_hva(vcpu->kvm, real_gfn);
197 if (unlikely(kvm_is_error_hva(host_addr)))
198 goto error;
199
200 ptep_user = (pt_element_t __user *)((void *)host_addr + offset);
201 if (unlikely(__copy_from_user(&pte, ptep_user, sizeof(pte))))
202 goto error;
203
204 trace_kvm_mmu_paging_element(pte, walker->level);
205
206 if (unlikely(!is_present_gpte(pte)))
207 goto error;
208
209 if (unlikely(is_rsvd_bits_set(&vcpu->arch.mmu, pte,
210 walker->level))) {
211 errcode |= PFERR_RSVD_MASK | PFERR_PRESENT_MASK;
212 goto error;
213 }
214
215 if (!check_write_user_access(vcpu, write_fault, user_fault,
216 pte))
217 eperm = true;
218
219 #if PTTYPE == 64
220 if (unlikely(fetch_fault && (pte & PT64_NX_MASK)))
221 eperm = true;
222 #endif
223
224 if (!eperm && unlikely(!(pte & PT_ACCESSED_MASK))) {
225 int ret;
226 trace_kvm_mmu_set_accessed_bit(table_gfn, index,
227 sizeof(pte));
228 ret = FNAME(cmpxchg_gpte)(vcpu, mmu, ptep_user, index,
229 pte, pte|PT_ACCESSED_MASK);
230 if (unlikely(ret < 0))
231 goto error;
232 else if (ret)
233 goto retry_walk;
234
235 mark_page_dirty(vcpu->kvm, table_gfn);
236 pte |= PT_ACCESSED_MASK;
237 }
238
239 walker->ptes[walker->level - 1] = pte;
240
241 if (FNAME(is_last_gpte)(walker, vcpu, mmu, pte)) {
242 int lvl = walker->level;
243 gpa_t real_gpa;
244 gfn_t gfn;
245 u32 ac;
246
247 /* check if the kernel is fetching from user page */
248 if (unlikely(pte_access & PT_USER_MASK) &&
249 kvm_read_cr4_bits(vcpu, X86_CR4_SMEP))
250 if (fetch_fault && !user_fault)
251 eperm = true;
252
253 gfn = gpte_to_gfn_lvl(pte, lvl);
254 gfn += (addr & PT_LVL_OFFSET_MASK(lvl)) >> PAGE_SHIFT;
255
256 if (PTTYPE == 32 &&
257 walker->level == PT_DIRECTORY_LEVEL &&
258 is_cpuid_PSE36())
259 gfn += pse36_gfn_delta(pte);
260
261 ac = write_fault | fetch_fault | user_fault;
262
263 real_gpa = mmu->translate_gpa(vcpu, gfn_to_gpa(gfn),
264 ac);
265 if (real_gpa == UNMAPPED_GVA)
266 return 0;
267
268 walker->gfn = real_gpa >> PAGE_SHIFT;
269
270 break;
271 }
272
273 pt_access &= FNAME(gpte_access)(vcpu, pte, false);
274 --walker->level;
275 }
276
277 if (unlikely(eperm)) {
278 errcode |= PFERR_PRESENT_MASK;
279 goto error;
280 }
281
282 if (write_fault && unlikely(!is_dirty_gpte(pte))) {
283 int ret;
284
285 trace_kvm_mmu_set_dirty_bit(table_gfn, index, sizeof(pte));
286 ret = FNAME(cmpxchg_gpte)(vcpu, mmu, ptep_user, index,
287 pte, pte|PT_DIRTY_MASK);
288 if (unlikely(ret < 0))
289 goto error;
290 else if (ret)
291 goto retry_walk;
292
293 mark_page_dirty(vcpu->kvm, table_gfn);
294 pte |= PT_DIRTY_MASK;
295 walker->ptes[walker->level - 1] = pte;
296 }
297
298 pte_access = pt_access & FNAME(gpte_access)(vcpu, pte, true);
299 walker->pt_access = pt_access;
300 walker->pte_access = pte_access;
301 pgprintk("%s: pte %llx pte_access %x pt_access %x\n",
302 __func__, (u64)pte, pte_access, pt_access);
303 return 1;
304
305 error:
306 errcode |= write_fault | user_fault;
307 if (fetch_fault && (mmu->nx ||
308 kvm_read_cr4_bits(vcpu, X86_CR4_SMEP)))
309 errcode |= PFERR_FETCH_MASK;
310
311 walker->fault.vector = PF_VECTOR;
312 walker->fault.error_code_valid = true;
313 walker->fault.error_code = errcode;
314 walker->fault.address = addr;
315 walker->fault.nested_page_fault = mmu != vcpu->arch.walk_mmu;
316
317 trace_kvm_mmu_walker_error(walker->fault.error_code);
318 return 0;
319 }
320
321 static int FNAME(walk_addr)(struct guest_walker *walker,
322 struct kvm_vcpu *vcpu, gva_t addr, u32 access)
323 {
324 return FNAME(walk_addr_generic)(walker, vcpu, &vcpu->arch.mmu, addr,
325 access);
326 }
327
328 static int FNAME(walk_addr_nested)(struct guest_walker *walker,
329 struct kvm_vcpu *vcpu, gva_t addr,
330 u32 access)
331 {
332 return FNAME(walk_addr_generic)(walker, vcpu, &vcpu->arch.nested_mmu,
333 addr, access);
334 }
335
336 static bool FNAME(prefetch_invalid_gpte)(struct kvm_vcpu *vcpu,
337 struct kvm_mmu_page *sp, u64 *spte,
338 pt_element_t gpte)
339 {
340 u64 nonpresent = shadow_trap_nonpresent_pte;
341
342 if (is_rsvd_bits_set(&vcpu->arch.mmu, gpte, PT_PAGE_TABLE_LEVEL))
343 goto no_present;
344
345 if (!is_present_gpte(gpte)) {
346 if (!sp->unsync)
347 nonpresent = shadow_notrap_nonpresent_pte;
348 goto no_present;
349 }
350
351 if (!(gpte & PT_ACCESSED_MASK))
352 goto no_present;
353
354 return false;
355
356 no_present:
357 drop_spte(vcpu->kvm, spte, nonpresent);
358 return true;
359 }
360
361 static void FNAME(update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
362 u64 *spte, const void *pte)
363 {
364 pt_element_t gpte;
365 unsigned pte_access;
366 pfn_t pfn;
367
368 gpte = *(const pt_element_t *)pte;
369 if (FNAME(prefetch_invalid_gpte)(vcpu, sp, spte, gpte))
370 return;
371
372 pgprintk("%s: gpte %llx spte %p\n", __func__, (u64)gpte, spte);
373 pte_access = sp->role.access & FNAME(gpte_access)(vcpu, gpte, true);
374 pfn = gfn_to_pfn_atomic(vcpu->kvm, gpte_to_gfn(gpte));
375 if (is_error_pfn(pfn)) {
376 kvm_release_pfn_clean(pfn);
377 return;
378 }
379
380 /*
381 * we call mmu_set_spte() with host_writable = true because that
382 * vcpu->arch.update_pte.pfn was fetched from get_user_pages(write = 1).
383 */
384 mmu_set_spte(vcpu, spte, sp->role.access, pte_access, 0, 0,
385 NULL, PT_PAGE_TABLE_LEVEL,
386 gpte_to_gfn(gpte), pfn, true, true);
387 }
388
389 static bool FNAME(gpte_changed)(struct kvm_vcpu *vcpu,
390 struct guest_walker *gw, int level)
391 {
392 pt_element_t curr_pte;
393 gpa_t base_gpa, pte_gpa = gw->pte_gpa[level - 1];
394 u64 mask;
395 int r, index;
396
397 if (level == PT_PAGE_TABLE_LEVEL) {
398 mask = PTE_PREFETCH_NUM * sizeof(pt_element_t) - 1;
399 base_gpa = pte_gpa & ~mask;
400 index = (pte_gpa - base_gpa) / sizeof(pt_element_t);
401
402 r = kvm_read_guest_atomic(vcpu->kvm, base_gpa,
403 gw->prefetch_ptes, sizeof(gw->prefetch_ptes));
404 curr_pte = gw->prefetch_ptes[index];
405 } else
406 r = kvm_read_guest_atomic(vcpu->kvm, pte_gpa,
407 &curr_pte, sizeof(curr_pte));
408
409 return r || curr_pte != gw->ptes[level - 1];
410 }
411
412 static void FNAME(pte_prefetch)(struct kvm_vcpu *vcpu, struct guest_walker *gw,
413 u64 *sptep)
414 {
415 struct kvm_mmu_page *sp;
416 pt_element_t *gptep = gw->prefetch_ptes;
417 u64 *spte;
418 int i;
419
420 sp = page_header(__pa(sptep));
421
422 if (sp->role.level > PT_PAGE_TABLE_LEVEL)
423 return;
424
425 if (sp->role.direct)
426 return __direct_pte_prefetch(vcpu, sp, sptep);
427
428 i = (sptep - sp->spt) & ~(PTE_PREFETCH_NUM - 1);
429 spte = sp->spt + i;
430
431 for (i = 0; i < PTE_PREFETCH_NUM; i++, spte++) {
432 pt_element_t gpte;
433 unsigned pte_access;
434 gfn_t gfn;
435 pfn_t pfn;
436
437 if (spte == sptep)
438 continue;
439
440 if (*spte != shadow_trap_nonpresent_pte)
441 continue;
442
443 gpte = gptep[i];
444
445 if (FNAME(prefetch_invalid_gpte)(vcpu, sp, spte, gpte))
446 continue;
447
448 pte_access = sp->role.access & FNAME(gpte_access)(vcpu, gpte,
449 true);
450 gfn = gpte_to_gfn(gpte);
451 pfn = pte_prefetch_gfn_to_pfn(vcpu, gfn,
452 pte_access & ACC_WRITE_MASK);
453 if (is_error_pfn(pfn)) {
454 kvm_release_pfn_clean(pfn);
455 break;
456 }
457
458 mmu_set_spte(vcpu, spte, sp->role.access, pte_access, 0, 0,
459 NULL, PT_PAGE_TABLE_LEVEL, gfn,
460 pfn, true, true);
461 }
462 }
463
464 /*
465 * Fetch a shadow pte for a specific level in the paging hierarchy.
466 */
467 static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
468 struct guest_walker *gw,
469 int user_fault, int write_fault, int hlevel,
470 int *ptwrite, pfn_t pfn, bool map_writable,
471 bool prefault)
472 {
473 unsigned access = gw->pt_access;
474 struct kvm_mmu_page *sp = NULL;
475 int top_level;
476 unsigned direct_access;
477 struct kvm_shadow_walk_iterator it;
478
479 if (!is_present_gpte(gw->ptes[gw->level - 1]))
480 return NULL;
481
482 direct_access = gw->pt_access & gw->pte_access;
483
484 top_level = vcpu->arch.mmu.root_level;
485 if (top_level == PT32E_ROOT_LEVEL)
486 top_level = PT32_ROOT_LEVEL;
487 /*
488 * Verify that the top-level gpte is still there. Since the page
489 * is a root page, it is either write protected (and cannot be
490 * changed from now on) or it is invalid (in which case, we don't
491 * really care if it changes underneath us after this point).
492 */
493 if (FNAME(gpte_changed)(vcpu, gw, top_level))
494 goto out_gpte_changed;
495
496 for (shadow_walk_init(&it, vcpu, addr);
497 shadow_walk_okay(&it) && it.level > gw->level;
498 shadow_walk_next(&it)) {
499 gfn_t table_gfn;
500
501 drop_large_spte(vcpu, it.sptep);
502
503 sp = NULL;
504 if (!is_shadow_present_pte(*it.sptep)) {
505 table_gfn = gw->table_gfn[it.level - 2];
506 sp = kvm_mmu_get_page(vcpu, table_gfn, addr, it.level-1,
507 false, access, it.sptep);
508 }
509
510 /*
511 * Verify that the gpte in the page we've just write
512 * protected is still there.
513 */
514 if (FNAME(gpte_changed)(vcpu, gw, it.level - 1))
515 goto out_gpte_changed;
516
517 if (sp)
518 link_shadow_page(it.sptep, sp);
519 }
520
521 for (;
522 shadow_walk_okay(&it) && it.level > hlevel;
523 shadow_walk_next(&it)) {
524 gfn_t direct_gfn;
525
526 validate_direct_spte(vcpu, it.sptep, direct_access);
527
528 drop_large_spte(vcpu, it.sptep);
529
530 if (is_shadow_present_pte(*it.sptep))
531 continue;
532
533 direct_gfn = gw->gfn & ~(KVM_PAGES_PER_HPAGE(it.level) - 1);
534
535 sp = kvm_mmu_get_page(vcpu, direct_gfn, addr, it.level-1,
536 true, direct_access, it.sptep);
537 link_shadow_page(it.sptep, sp);
538 }
539
540 mmu_set_spte(vcpu, it.sptep, access, gw->pte_access & access,
541 user_fault, write_fault, ptwrite, it.level,
542 gw->gfn, pfn, prefault, map_writable);
543 FNAME(pte_prefetch)(vcpu, gw, it.sptep);
544
545 return it.sptep;
546
547 out_gpte_changed:
548 if (sp)
549 kvm_mmu_put_page(sp, it.sptep);
550 kvm_release_pfn_clean(pfn);
551 return NULL;
552 }
553
554 /*
555 * Page fault handler. There are several causes for a page fault:
556 * - there is no shadow pte for the guest pte
557 * - write access through a shadow pte marked read only so that we can set
558 * the dirty bit
559 * - write access to a shadow pte marked read only so we can update the page
560 * dirty bitmap, when userspace requests it
561 * - mmio access; in this case we will never install a present shadow pte
562 * - normal guest page fault due to the guest pte marked not present, not
563 * writable, or not executable
564 *
565 * Returns: 1 if we need to emulate the instruction, 0 otherwise, or
566 * a negative value on error.
567 */
568 static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr, u32 error_code,
569 bool prefault)
570 {
571 int write_fault = error_code & PFERR_WRITE_MASK;
572 int user_fault = error_code & PFERR_USER_MASK;
573 struct guest_walker walker;
574 u64 *sptep;
575 int write_pt = 0;
576 int r;
577 pfn_t pfn;
578 int level = PT_PAGE_TABLE_LEVEL;
579 int force_pt_level;
580 unsigned long mmu_seq;
581 bool map_writable;
582
583 pgprintk("%s: addr %lx err %x\n", __func__, addr, error_code);
584
585 r = mmu_topup_memory_caches(vcpu);
586 if (r)
587 return r;
588
589 /*
590 * Look up the guest pte for the faulting address.
591 */
592 r = FNAME(walk_addr)(&walker, vcpu, addr, error_code);
593
594 /*
595 * The page is not mapped by the guest. Let the guest handle it.
596 */
597 if (!r) {
598 pgprintk("%s: guest page fault\n", __func__);
599 if (!prefault) {
600 inject_page_fault(vcpu, &walker.fault);
601 /* reset fork detector */
602 vcpu->arch.last_pt_write_count = 0;
603 }
604 return 0;
605 }
606
607 if (walker.level >= PT_DIRECTORY_LEVEL)
608 force_pt_level = mapping_level_dirty_bitmap(vcpu, walker.gfn);
609 else
610 force_pt_level = 1;
611 if (!force_pt_level) {
612 level = min(walker.level, mapping_level(vcpu, walker.gfn));
613 walker.gfn = walker.gfn & ~(KVM_PAGES_PER_HPAGE(level) - 1);
614 }
615
616 mmu_seq = vcpu->kvm->mmu_notifier_seq;
617 smp_rmb();
618
619 if (try_async_pf(vcpu, prefault, walker.gfn, addr, &pfn, write_fault,
620 &map_writable))
621 return 0;
622
623 /* mmio */
624 if (is_error_pfn(pfn))
625 return kvm_handle_bad_page(vcpu, mmu_is_nested(vcpu) ? 0 :
626 addr, walker.pte_access, walker.gfn, pfn);
627 spin_lock(&vcpu->kvm->mmu_lock);
628 if (mmu_notifier_retry(vcpu, mmu_seq))
629 goto out_unlock;
630
631 trace_kvm_mmu_audit(vcpu, AUDIT_PRE_PAGE_FAULT);
632 kvm_mmu_free_some_pages(vcpu);
633 if (!force_pt_level)
634 transparent_hugepage_adjust(vcpu, &walker.gfn, &pfn, &level);
635 sptep = FNAME(fetch)(vcpu, addr, &walker, user_fault, write_fault,
636 level, &write_pt, pfn, map_writable, prefault);
637 (void)sptep;
638 pgprintk("%s: shadow pte %p %llx ptwrite %d\n", __func__,
639 sptep, *sptep, write_pt);
640
641 if (!write_pt)
642 vcpu->arch.last_pt_write_count = 0; /* reset fork detector */
643
644 ++vcpu->stat.pf_fixed;
645 trace_kvm_mmu_audit(vcpu, AUDIT_POST_PAGE_FAULT);
646 spin_unlock(&vcpu->kvm->mmu_lock);
647
648 return write_pt;
649
650 out_unlock:
651 spin_unlock(&vcpu->kvm->mmu_lock);
652 kvm_release_pfn_clean(pfn);
653 return 0;
654 }
655
656 static void FNAME(invlpg)(struct kvm_vcpu *vcpu, gva_t gva)
657 {
658 struct kvm_shadow_walk_iterator iterator;
659 struct kvm_mmu_page *sp;
660 gpa_t pte_gpa = -1;
661 int level;
662 u64 *sptep;
663 int need_flush = 0;
664
665 vcpu_clear_mmio_info(vcpu, gva);
666
667 spin_lock(&vcpu->kvm->mmu_lock);
668
669 for_each_shadow_entry(vcpu, gva, iterator) {
670 level = iterator.level;
671 sptep = iterator.sptep;
672
673 sp = page_header(__pa(sptep));
674 if (is_last_spte(*sptep, level)) {
675 int offset, shift;
676
677 if (!sp->unsync)
678 break;
679
680 shift = PAGE_SHIFT -
681 (PT_LEVEL_BITS - PT64_LEVEL_BITS) * level;
682 offset = sp->role.quadrant << shift;
683
684 pte_gpa = (sp->gfn << PAGE_SHIFT) + offset;
685 pte_gpa += (sptep - sp->spt) * sizeof(pt_element_t);
686
687 if (is_shadow_present_pte(*sptep)) {
688 if (is_large_pte(*sptep))
689 --vcpu->kvm->stat.lpages;
690 drop_spte(vcpu->kvm, sptep,
691 shadow_trap_nonpresent_pte);
692 need_flush = 1;
693 } else
694 __set_spte(sptep, shadow_trap_nonpresent_pte);
695 break;
696 }
697
698 if (!is_shadow_present_pte(*sptep) || !sp->unsync_children)
699 break;
700 }
701
702 if (need_flush)
703 kvm_flush_remote_tlbs(vcpu->kvm);
704
705 atomic_inc(&vcpu->kvm->arch.invlpg_counter);
706
707 spin_unlock(&vcpu->kvm->mmu_lock);
708
709 if (pte_gpa == -1)
710 return;
711
712 if (mmu_topup_memory_caches(vcpu))
713 return;
714 kvm_mmu_pte_write(vcpu, pte_gpa, NULL, sizeof(pt_element_t), 0);
715 }
716
717 static gpa_t FNAME(gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t vaddr, u32 access,
718 struct x86_exception *exception)
719 {
720 struct guest_walker walker;
721 gpa_t gpa = UNMAPPED_GVA;
722 int r;
723
724 r = FNAME(walk_addr)(&walker, vcpu, vaddr, access);
725
726 if (r) {
727 gpa = gfn_to_gpa(walker.gfn);
728 gpa |= vaddr & ~PAGE_MASK;
729 } else if (exception)
730 *exception = walker.fault;
731
732 return gpa;
733 }
734
735 static gpa_t FNAME(gva_to_gpa_nested)(struct kvm_vcpu *vcpu, gva_t vaddr,
736 u32 access,
737 struct x86_exception *exception)
738 {
739 struct guest_walker walker;
740 gpa_t gpa = UNMAPPED_GVA;
741 int r;
742
743 r = FNAME(walk_addr_nested)(&walker, vcpu, vaddr, access);
744
745 if (r) {
746 gpa = gfn_to_gpa(walker.gfn);
747 gpa |= vaddr & ~PAGE_MASK;
748 } else if (exception)
749 *exception = walker.fault;
750
751 return gpa;
752 }
753
754 static void FNAME(prefetch_page)(struct kvm_vcpu *vcpu,
755 struct kvm_mmu_page *sp)
756 {
757 int i, j, offset, r;
758 pt_element_t pt[256 / sizeof(pt_element_t)];
759 gpa_t pte_gpa;
760
761 if (sp->role.direct
762 || (PTTYPE == 32 && sp->role.level > PT_PAGE_TABLE_LEVEL)) {
763 nonpaging_prefetch_page(vcpu, sp);
764 return;
765 }
766
767 pte_gpa = gfn_to_gpa(sp->gfn);
768 if (PTTYPE == 32) {
769 offset = sp->role.quadrant << PT64_LEVEL_BITS;
770 pte_gpa += offset * sizeof(pt_element_t);
771 }
772
773 for (i = 0; i < PT64_ENT_PER_PAGE; i += ARRAY_SIZE(pt)) {
774 r = kvm_read_guest_atomic(vcpu->kvm, pte_gpa, pt, sizeof pt);
775 pte_gpa += ARRAY_SIZE(pt) * sizeof(pt_element_t);
776 for (j = 0; j < ARRAY_SIZE(pt); ++j)
777 if (r || is_present_gpte(pt[j]))
778 sp->spt[i+j] = shadow_trap_nonpresent_pte;
779 else
780 sp->spt[i+j] = shadow_notrap_nonpresent_pte;
781 }
782 }
783
784 /*
785 * Using the cached information from sp->gfns is safe because:
786 * - The spte has a reference to the struct page, so the pfn for a given gfn
787 * can't change unless all sptes pointing to it are nuked first.
788 *
789 * Note:
790 * We should flush all tlbs if spte is dropped even though guest is
791 * responsible for it. Since if we don't, kvm_mmu_notifier_invalidate_page
792 * and kvm_mmu_notifier_invalidate_range_start detect the mapping page isn't
793 * used by guest then tlbs are not flushed, so guest is allowed to access the
794 * freed pages.
795 * And we increase kvm->tlbs_dirty to delay tlbs flush in this case.
796 */
797 static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
798 {
799 int i, offset, nr_present;
800 bool host_writable;
801 gpa_t first_pte_gpa;
802
803 offset = nr_present = 0;
804
805 /* direct kvm_mmu_page can not be unsync. */
806 BUG_ON(sp->role.direct);
807
808 if (PTTYPE == 32)
809 offset = sp->role.quadrant << PT64_LEVEL_BITS;
810
811 first_pte_gpa = gfn_to_gpa(sp->gfn) + offset * sizeof(pt_element_t);
812
813 for (i = 0; i < PT64_ENT_PER_PAGE; i++) {
814 unsigned pte_access;
815 pt_element_t gpte;
816 gpa_t pte_gpa;
817 gfn_t gfn;
818
819 if (!is_shadow_present_pte(sp->spt[i]))
820 continue;
821
822 pte_gpa = first_pte_gpa + i * sizeof(pt_element_t);
823
824 if (kvm_read_guest_atomic(vcpu->kvm, pte_gpa, &gpte,
825 sizeof(pt_element_t)))
826 return -EINVAL;
827
828 gfn = gpte_to_gfn(gpte);
829
830 if (FNAME(prefetch_invalid_gpte)(vcpu, sp, &sp->spt[i], gpte)) {
831 vcpu->kvm->tlbs_dirty++;
832 continue;
833 }
834
835 if (gfn != sp->gfns[i]) {
836 drop_spte(vcpu->kvm, &sp->spt[i],
837 shadow_trap_nonpresent_pte);
838 vcpu->kvm->tlbs_dirty++;
839 continue;
840 }
841
842 nr_present++;
843 pte_access = sp->role.access & FNAME(gpte_access)(vcpu, gpte,
844 true);
845 host_writable = sp->spt[i] & SPTE_HOST_WRITEABLE;
846
847 set_spte(vcpu, &sp->spt[i], pte_access, 0, 0,
848 PT_PAGE_TABLE_LEVEL, gfn,
849 spte_to_pfn(sp->spt[i]), true, false,
850 host_writable);
851 }
852
853 return !nr_present;
854 }
855
856 #undef pt_element_t
857 #undef guest_walker
858 #undef FNAME
859 #undef PT_BASE_ADDR_MASK
860 #undef PT_INDEX
861 #undef PT_LVL_ADDR_MASK
862 #undef PT_LVL_OFFSET_MASK
863 #undef PT_LEVEL_BITS
864 #undef PT_MAX_FULL_LEVELS
865 #undef gpte_to_gfn
866 #undef gpte_to_gfn_lvl
867 #undef CMPXCHG