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