Commit | Line | Data |
---|---|---|
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. | |
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 | ||
20 | /* | |
21 | * We need the mmu code to access both 32-bit and 64-bit guest ptes, | |
22 | * so the code in this file is compiled twice, once per pte size. | |
23 | */ | |
24 | ||
25 | #if PTTYPE == 64 | |
26 | #define pt_element_t u64 | |
27 | #define guest_walker guest_walker64 | |
28 | #define FNAME(name) paging##64_##name | |
29 | #define PT_BASE_ADDR_MASK PT64_BASE_ADDR_MASK | |
30 | #define PT_DIR_BASE_ADDR_MASK PT64_DIR_BASE_ADDR_MASK | |
31 | #define PT_INDEX(addr, level) PT64_INDEX(addr, level) | |
6aa8b732 | 32 | #define PT_LEVEL_MASK(level) PT64_LEVEL_MASK(level) |
c7addb90 | 33 | #define PT_LEVEL_BITS PT64_LEVEL_BITS |
cea0f0e7 AK |
34 | #ifdef CONFIG_X86_64 |
35 | #define PT_MAX_FULL_LEVELS 4 | |
b3e4e63f | 36 | #define CMPXCHG cmpxchg |
cea0f0e7 | 37 | #else |
b3e4e63f | 38 | #define CMPXCHG cmpxchg64 |
cea0f0e7 AK |
39 | #define PT_MAX_FULL_LEVELS 2 |
40 | #endif | |
6aa8b732 AK |
41 | #elif PTTYPE == 32 |
42 | #define pt_element_t u32 | |
43 | #define guest_walker guest_walker32 | |
44 | #define FNAME(name) paging##32_##name | |
45 | #define PT_BASE_ADDR_MASK PT32_BASE_ADDR_MASK | |
46 | #define PT_DIR_BASE_ADDR_MASK PT32_DIR_BASE_ADDR_MASK | |
47 | #define PT_INDEX(addr, level) PT32_INDEX(addr, level) | |
6aa8b732 | 48 | #define PT_LEVEL_MASK(level) PT32_LEVEL_MASK(level) |
c7addb90 | 49 | #define PT_LEVEL_BITS PT32_LEVEL_BITS |
cea0f0e7 | 50 | #define PT_MAX_FULL_LEVELS 2 |
b3e4e63f | 51 | #define CMPXCHG cmpxchg |
6aa8b732 AK |
52 | #else |
53 | #error Invalid PTTYPE value | |
54 | #endif | |
55 | ||
5fb07ddb AK |
56 | #define gpte_to_gfn FNAME(gpte_to_gfn) |
57 | #define gpte_to_gfn_pde FNAME(gpte_to_gfn_pde) | |
58 | ||
6aa8b732 AK |
59 | /* |
60 | * The guest_walker structure emulates the behavior of the hardware page | |
61 | * table walker. | |
62 | */ | |
63 | struct guest_walker { | |
64 | int level; | |
cea0f0e7 | 65 | gfn_t table_gfn[PT_MAX_FULL_LEVELS]; |
7819026e MT |
66 | pt_element_t ptes[PT_MAX_FULL_LEVELS]; |
67 | gpa_t pte_gpa[PT_MAX_FULL_LEVELS]; | |
fe135d2c AK |
68 | unsigned pt_access; |
69 | unsigned pte_access; | |
815af8d4 | 70 | gfn_t gfn; |
7993ba43 | 71 | u32 error_code; |
6aa8b732 AK |
72 | }; |
73 | ||
5fb07ddb AK |
74 | static gfn_t gpte_to_gfn(pt_element_t gpte) |
75 | { | |
76 | return (gpte & PT_BASE_ADDR_MASK) >> PAGE_SHIFT; | |
77 | } | |
78 | ||
79 | static gfn_t gpte_to_gfn_pde(pt_element_t gpte) | |
80 | { | |
81 | return (gpte & PT_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT; | |
82 | } | |
83 | ||
b3e4e63f MT |
84 | static bool FNAME(cmpxchg_gpte)(struct kvm *kvm, |
85 | gfn_t table_gfn, unsigned index, | |
86 | pt_element_t orig_pte, pt_element_t new_pte) | |
87 | { | |
88 | pt_element_t ret; | |
89 | pt_element_t *table; | |
90 | struct page *page; | |
91 | ||
92 | page = gfn_to_page(kvm, table_gfn); | |
72dc67a6 | 93 | |
b3e4e63f | 94 | table = kmap_atomic(page, KM_USER0); |
b3e4e63f | 95 | ret = CMPXCHG(&table[index], orig_pte, new_pte); |
b3e4e63f MT |
96 | kunmap_atomic(table, KM_USER0); |
97 | ||
98 | kvm_release_page_dirty(page); | |
99 | ||
100 | return (ret != orig_pte); | |
101 | } | |
102 | ||
bedbe4ee AK |
103 | static unsigned FNAME(gpte_access)(struct kvm_vcpu *vcpu, pt_element_t gpte) |
104 | { | |
105 | unsigned access; | |
106 | ||
107 | access = (gpte & (PT_WRITABLE_MASK | PT_USER_MASK)) | ACC_EXEC_MASK; | |
108 | #if PTTYPE == 64 | |
109 | if (is_nx(vcpu)) | |
110 | access &= ~(gpte >> PT64_NX_SHIFT); | |
111 | #endif | |
112 | return access; | |
113 | } | |
114 | ||
ac79c978 AK |
115 | /* |
116 | * Fetch a guest pte for a guest virtual address | |
117 | */ | |
7993ba43 AK |
118 | static int FNAME(walk_addr)(struct guest_walker *walker, |
119 | struct kvm_vcpu *vcpu, gva_t addr, | |
73b1087e | 120 | int write_fault, int user_fault, int fetch_fault) |
6aa8b732 | 121 | { |
42bf3f0a | 122 | pt_element_t pte; |
cea0f0e7 | 123 | gfn_t table_gfn; |
fe135d2c | 124 | unsigned index, pt_access, pte_access; |
42bf3f0a | 125 | gpa_t pte_gpa; |
82725b20 | 126 | int rsvd_fault = 0; |
6aa8b732 | 127 | |
07420171 AK |
128 | trace_kvm_mmu_pagetable_walk(addr, write_fault, user_fault, |
129 | fetch_fault); | |
b3e4e63f | 130 | walk: |
ad312c7c ZX |
131 | walker->level = vcpu->arch.mmu.root_level; |
132 | pte = vcpu->arch.cr3; | |
1b0973bd AK |
133 | #if PTTYPE == 64 |
134 | if (!is_long_mode(vcpu)) { | |
6de4f3ad | 135 | pte = kvm_pdptr_read(vcpu, (addr >> 30) & 3); |
07420171 | 136 | trace_kvm_mmu_paging_element(pte, walker->level); |
43a3795a | 137 | if (!is_present_gpte(pte)) |
7993ba43 | 138 | goto not_present; |
1b0973bd AK |
139 | --walker->level; |
140 | } | |
141 | #endif | |
a9058ecd | 142 | ASSERT((!is_long_mode(vcpu) && is_pae(vcpu)) || |
24993d53 | 143 | (vcpu->arch.cr3 & CR3_NONPAE_RESERVED_BITS) == 0); |
6aa8b732 | 144 | |
fe135d2c | 145 | pt_access = ACC_ALL; |
ac79c978 AK |
146 | |
147 | for (;;) { | |
42bf3f0a | 148 | index = PT_INDEX(addr, walker->level); |
ac79c978 | 149 | |
5fb07ddb | 150 | table_gfn = gpte_to_gfn(pte); |
1755fbcc | 151 | pte_gpa = gfn_to_gpa(table_gfn); |
ec8d4eae | 152 | pte_gpa += index * sizeof(pt_element_t); |
42bf3f0a | 153 | walker->table_gfn[walker->level - 1] = table_gfn; |
7819026e | 154 | walker->pte_gpa[walker->level - 1] = pte_gpa; |
42bf3f0a | 155 | |
ec8d4eae | 156 | kvm_read_guest(vcpu->kvm, pte_gpa, &pte, sizeof(pte)); |
07420171 | 157 | trace_kvm_mmu_paging_element(pte, walker->level); |
42bf3f0a | 158 | |
43a3795a | 159 | if (!is_present_gpte(pte)) |
7993ba43 AK |
160 | goto not_present; |
161 | ||
82725b20 DE |
162 | rsvd_fault = is_rsvd_bits_set(vcpu, pte, walker->level); |
163 | if (rsvd_fault) | |
164 | goto access_error; | |
165 | ||
42bf3f0a | 166 | if (write_fault && !is_writeble_pte(pte)) |
7993ba43 AK |
167 | if (user_fault || is_write_protection(vcpu)) |
168 | goto access_error; | |
169 | ||
42bf3f0a | 170 | if (user_fault && !(pte & PT_USER_MASK)) |
7993ba43 AK |
171 | goto access_error; |
172 | ||
73b1087e | 173 | #if PTTYPE == 64 |
42bf3f0a | 174 | if (fetch_fault && is_nx(vcpu) && (pte & PT64_NX_MASK)) |
73b1087e AK |
175 | goto access_error; |
176 | #endif | |
177 | ||
42bf3f0a | 178 | if (!(pte & PT_ACCESSED_MASK)) { |
07420171 AK |
179 | trace_kvm_mmu_set_accessed_bit(table_gfn, index, |
180 | sizeof(pte)); | |
bf3f8e86 | 181 | mark_page_dirty(vcpu->kvm, table_gfn); |
b3e4e63f MT |
182 | if (FNAME(cmpxchg_gpte)(vcpu->kvm, table_gfn, |
183 | index, pte, pte|PT_ACCESSED_MASK)) | |
184 | goto walk; | |
42bf3f0a | 185 | pte |= PT_ACCESSED_MASK; |
bf3f8e86 | 186 | } |
815af8d4 | 187 | |
bedbe4ee | 188 | pte_access = pt_access & FNAME(gpte_access)(vcpu, pte); |
fe135d2c | 189 | |
7819026e MT |
190 | walker->ptes[walker->level - 1] = pte; |
191 | ||
815af8d4 | 192 | if (walker->level == PT_PAGE_TABLE_LEVEL) { |
5fb07ddb | 193 | walker->gfn = gpte_to_gfn(pte); |
815af8d4 AK |
194 | break; |
195 | } | |
196 | ||
197 | if (walker->level == PT_DIRECTORY_LEVEL | |
42bf3f0a | 198 | && (pte & PT_PAGE_SIZE_MASK) |
815af8d4 | 199 | && (PTTYPE == 64 || is_pse(vcpu))) { |
5fb07ddb | 200 | walker->gfn = gpte_to_gfn_pde(pte); |
815af8d4 | 201 | walker->gfn += PT_INDEX(addr, PT_PAGE_TABLE_LEVEL); |
da928521 AK |
202 | if (PTTYPE == 32 && is_cpuid_PSE36()) |
203 | walker->gfn += pse36_gfn_delta(pte); | |
ac79c978 | 204 | break; |
815af8d4 | 205 | } |
ac79c978 | 206 | |
fe135d2c | 207 | pt_access = pte_access; |
ac79c978 AK |
208 | --walker->level; |
209 | } | |
42bf3f0a | 210 | |
43a3795a | 211 | if (write_fault && !is_dirty_gpte(pte)) { |
b3e4e63f MT |
212 | bool ret; |
213 | ||
07420171 | 214 | trace_kvm_mmu_set_dirty_bit(table_gfn, index, sizeof(pte)); |
42bf3f0a | 215 | mark_page_dirty(vcpu->kvm, table_gfn); |
b3e4e63f MT |
216 | ret = FNAME(cmpxchg_gpte)(vcpu->kvm, table_gfn, index, pte, |
217 | pte|PT_DIRTY_MASK); | |
218 | if (ret) | |
219 | goto walk; | |
42bf3f0a | 220 | pte |= PT_DIRTY_MASK; |
7819026e | 221 | walker->ptes[walker->level - 1] = pte; |
42bf3f0a AK |
222 | } |
223 | ||
fe135d2c AK |
224 | walker->pt_access = pt_access; |
225 | walker->pte_access = pte_access; | |
226 | pgprintk("%s: pte %llx pte_access %x pt_access %x\n", | |
b8688d51 | 227 | __func__, (u64)pte, pt_access, pte_access); |
7993ba43 AK |
228 | return 1; |
229 | ||
230 | not_present: | |
231 | walker->error_code = 0; | |
232 | goto err; | |
233 | ||
234 | access_error: | |
235 | walker->error_code = PFERR_PRESENT_MASK; | |
236 | ||
237 | err: | |
238 | if (write_fault) | |
239 | walker->error_code |= PFERR_WRITE_MASK; | |
240 | if (user_fault) | |
241 | walker->error_code |= PFERR_USER_MASK; | |
73b1087e AK |
242 | if (fetch_fault) |
243 | walker->error_code |= PFERR_FETCH_MASK; | |
82725b20 DE |
244 | if (rsvd_fault) |
245 | walker->error_code |= PFERR_RSVD_MASK; | |
07420171 | 246 | trace_kvm_mmu_walker_error(walker->error_code); |
fe551881 | 247 | return 0; |
6aa8b732 AK |
248 | } |
249 | ||
0028425f | 250 | static void FNAME(update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *page, |
489f1d65 | 251 | u64 *spte, const void *pte) |
0028425f AK |
252 | { |
253 | pt_element_t gpte; | |
41074d07 | 254 | unsigned pte_access; |
35149e21 | 255 | pfn_t pfn; |
05da4558 | 256 | int largepage = vcpu->arch.update_pte.largepage; |
0028425f | 257 | |
0028425f | 258 | gpte = *(const pt_element_t *)pte; |
c7addb90 | 259 | if (~gpte & (PT_PRESENT_MASK | PT_ACCESSED_MASK)) { |
43a3795a | 260 | if (!is_present_gpte(gpte)) |
d555c333 | 261 | __set_spte(spte, shadow_notrap_nonpresent_pte); |
c7addb90 AK |
262 | return; |
263 | } | |
b8688d51 | 264 | pgprintk("%s: gpte %llx spte %p\n", __func__, (u64)gpte, spte); |
41074d07 | 265 | pte_access = page->role.access & FNAME(gpte_access)(vcpu, gpte); |
d7824fff AK |
266 | if (gpte_to_gfn(gpte) != vcpu->arch.update_pte.gfn) |
267 | return; | |
35149e21 AL |
268 | pfn = vcpu->arch.update_pte.pfn; |
269 | if (is_error_pfn(pfn)) | |
d7824fff | 270 | return; |
e930bffe AA |
271 | if (mmu_notifier_retry(vcpu, vcpu->arch.update_pte.mmu_seq)) |
272 | return; | |
35149e21 | 273 | kvm_get_pfn(pfn); |
1c4f1fd6 | 274 | mmu_set_spte(vcpu, spte, page->role.access, pte_access, 0, 0, |
6cffe8ca | 275 | gpte & PT_DIRTY_MASK, NULL, largepage, |
c2d0ee46 | 276 | gpte_to_gfn(gpte), pfn, true); |
0028425f AK |
277 | } |
278 | ||
6aa8b732 AK |
279 | /* |
280 | * Fetch a shadow pte for a specific level in the paging hierarchy. | |
281 | */ | |
e7a04c99 AK |
282 | static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr, |
283 | struct guest_walker *gw, | |
284 | int user_fault, int write_fault, int largepage, | |
285 | int *ptwrite, pfn_t pfn) | |
6aa8b732 | 286 | { |
abb9e0b8 AK |
287 | unsigned access = gw->pt_access; |
288 | struct kvm_mmu_page *shadow_page; | |
bde89223 | 289 | u64 spte, *sptep = NULL; |
f6e2c02b | 290 | int direct; |
abb9e0b8 AK |
291 | gfn_t table_gfn; |
292 | int r; | |
e7a04c99 | 293 | int level; |
abb9e0b8 | 294 | pt_element_t curr_pte; |
e7a04c99 | 295 | struct kvm_shadow_walk_iterator iterator; |
abb9e0b8 | 296 | |
43a3795a | 297 | if (!is_present_gpte(gw->ptes[gw->level - 1])) |
e7a04c99 | 298 | return NULL; |
6aa8b732 | 299 | |
e7a04c99 AK |
300 | for_each_shadow_entry(vcpu, addr, iterator) { |
301 | level = iterator.level; | |
302 | sptep = iterator.sptep; | |
303 | if (level == PT_PAGE_TABLE_LEVEL | |
304 | || (largepage && level == PT_DIRECTORY_LEVEL)) { | |
305 | mmu_set_spte(vcpu, sptep, access, | |
306 | gw->pte_access & access, | |
307 | user_fault, write_fault, | |
308 | gw->ptes[gw->level-1] & PT_DIRTY_MASK, | |
309 | ptwrite, largepage, | |
e7a04c99 AK |
310 | gw->gfn, pfn, false); |
311 | break; | |
312 | } | |
6aa8b732 | 313 | |
e7a04c99 AK |
314 | if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep)) |
315 | continue; | |
abb9e0b8 | 316 | |
e7a04c99 | 317 | if (is_large_pte(*sptep)) { |
c5bc2242 | 318 | rmap_remove(vcpu->kvm, sptep); |
d555c333 | 319 | __set_spte(sptep, shadow_trap_nonpresent_pte); |
e7a04c99 | 320 | kvm_flush_remote_tlbs(vcpu->kvm); |
7819026e | 321 | } |
ef0197e8 | 322 | |
e7a04c99 AK |
323 | if (level == PT_DIRECTORY_LEVEL |
324 | && gw->level == PT_DIRECTORY_LEVEL) { | |
f6e2c02b | 325 | direct = 1; |
43a3795a | 326 | if (!is_dirty_gpte(gw->ptes[level - 1])) |
e7a04c99 AK |
327 | access &= ~ACC_WRITE_MASK; |
328 | table_gfn = gpte_to_gfn(gw->ptes[level - 1]); | |
329 | } else { | |
f6e2c02b | 330 | direct = 0; |
e7a04c99 AK |
331 | table_gfn = gw->table_gfn[level - 2]; |
332 | } | |
333 | shadow_page = kvm_mmu_get_page(vcpu, table_gfn, addr, level-1, | |
f6e2c02b AK |
334 | direct, access, sptep); |
335 | if (!direct) { | |
e7a04c99 AK |
336 | r = kvm_read_guest_atomic(vcpu->kvm, |
337 | gw->pte_gpa[level - 2], | |
338 | &curr_pte, sizeof(curr_pte)); | |
339 | if (r || curr_pte != gw->ptes[level - 2]) { | |
340 | kvm_mmu_put_page(shadow_page, sptep); | |
341 | kvm_release_pfn_clean(pfn); | |
342 | sptep = NULL; | |
343 | break; | |
344 | } | |
345 | } | |
abb9e0b8 | 346 | |
e7a04c99 AK |
347 | spte = __pa(shadow_page->spt) |
348 | | PT_PRESENT_MASK | PT_ACCESSED_MASK | |
349 | | PT_WRITABLE_MASK | PT_USER_MASK; | |
350 | *sptep = spte; | |
351 | } | |
050e6499 | 352 | |
e7a04c99 | 353 | return sptep; |
6aa8b732 AK |
354 | } |
355 | ||
6aa8b732 AK |
356 | /* |
357 | * Page fault handler. There are several causes for a page fault: | |
358 | * - there is no shadow pte for the guest pte | |
359 | * - write access through a shadow pte marked read only so that we can set | |
360 | * the dirty bit | |
361 | * - write access to a shadow pte marked read only so we can update the page | |
362 | * dirty bitmap, when userspace requests it | |
363 | * - mmio access; in this case we will never install a present shadow pte | |
364 | * - normal guest page fault due to the guest pte marked not present, not | |
365 | * writable, or not executable | |
366 | * | |
e2dec939 AK |
367 | * Returns: 1 if we need to emulate the instruction, 0 otherwise, or |
368 | * a negative value on error. | |
6aa8b732 AK |
369 | */ |
370 | static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr, | |
371 | u32 error_code) | |
372 | { | |
373 | int write_fault = error_code & PFERR_WRITE_MASK; | |
6aa8b732 | 374 | int user_fault = error_code & PFERR_USER_MASK; |
73b1087e | 375 | int fetch_fault = error_code & PFERR_FETCH_MASK; |
6aa8b732 | 376 | struct guest_walker walker; |
d555c333 | 377 | u64 *sptep; |
cea0f0e7 | 378 | int write_pt = 0; |
e2dec939 | 379 | int r; |
35149e21 | 380 | pfn_t pfn; |
05da4558 | 381 | int largepage = 0; |
e930bffe | 382 | unsigned long mmu_seq; |
6aa8b732 | 383 | |
b8688d51 | 384 | pgprintk("%s: addr %lx err %x\n", __func__, addr, error_code); |
37a7d8b0 | 385 | kvm_mmu_audit(vcpu, "pre page fault"); |
714b93da | 386 | |
e2dec939 AK |
387 | r = mmu_topup_memory_caches(vcpu); |
388 | if (r) | |
389 | return r; | |
714b93da | 390 | |
6aa8b732 | 391 | /* |
a8b876b1 | 392 | * Look up the guest pte for the faulting address. |
6aa8b732 | 393 | */ |
73b1087e AK |
394 | r = FNAME(walk_addr)(&walker, vcpu, addr, write_fault, user_fault, |
395 | fetch_fault); | |
6aa8b732 AK |
396 | |
397 | /* | |
398 | * The page is not mapped by the guest. Let the guest handle it. | |
399 | */ | |
7993ba43 | 400 | if (!r) { |
b8688d51 | 401 | pgprintk("%s: guest page fault\n", __func__); |
7993ba43 | 402 | inject_page_fault(vcpu, addr, walker.error_code); |
ad312c7c | 403 | vcpu->arch.last_pt_write_count = 0; /* reset fork detector */ |
6aa8b732 AK |
404 | return 0; |
405 | } | |
406 | ||
05da4558 MT |
407 | if (walker.level == PT_DIRECTORY_LEVEL) { |
408 | gfn_t large_gfn; | |
ec04b260 | 409 | large_gfn = walker.gfn & |
d25797b2 JR |
410 | ~(KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL) - 1); |
411 | if (mapping_level(vcpu, large_gfn) == PT_DIRECTORY_LEVEL) { | |
05da4558 MT |
412 | walker.gfn = large_gfn; |
413 | largepage = 1; | |
414 | } | |
415 | } | |
e930bffe | 416 | mmu_seq = vcpu->kvm->mmu_notifier_seq; |
4c2155ce | 417 | smp_rmb(); |
35149e21 | 418 | pfn = gfn_to_pfn(vcpu->kvm, walker.gfn); |
d7824fff | 419 | |
d196e343 | 420 | /* mmio */ |
35149e21 | 421 | if (is_error_pfn(pfn)) { |
ebb0e626 | 422 | pgprintk("gfn %lx is mmio\n", walker.gfn); |
35149e21 | 423 | kvm_release_pfn_clean(pfn); |
d196e343 AK |
424 | return 1; |
425 | } | |
426 | ||
aaee2c94 | 427 | spin_lock(&vcpu->kvm->mmu_lock); |
e930bffe AA |
428 | if (mmu_notifier_retry(vcpu, mmu_seq)) |
429 | goto out_unlock; | |
eb787d10 | 430 | kvm_mmu_free_some_pages(vcpu); |
d555c333 AK |
431 | sptep = FNAME(fetch)(vcpu, addr, &walker, user_fault, write_fault, |
432 | largepage, &write_pt, pfn); | |
05da4558 | 433 | |
b8688d51 | 434 | pgprintk("%s: shadow pte %p %llx ptwrite %d\n", __func__, |
d555c333 | 435 | sptep, *sptep, write_pt); |
cea0f0e7 | 436 | |
a25f7e1f | 437 | if (!write_pt) |
ad312c7c | 438 | vcpu->arch.last_pt_write_count = 0; /* reset fork detector */ |
a25f7e1f | 439 | |
1165f5fe | 440 | ++vcpu->stat.pf_fixed; |
37a7d8b0 | 441 | kvm_mmu_audit(vcpu, "post page fault (fixed)"); |
aaee2c94 | 442 | spin_unlock(&vcpu->kvm->mmu_lock); |
6aa8b732 | 443 | |
cea0f0e7 | 444 | return write_pt; |
e930bffe AA |
445 | |
446 | out_unlock: | |
447 | spin_unlock(&vcpu->kvm->mmu_lock); | |
448 | kvm_release_pfn_clean(pfn); | |
449 | return 0; | |
6aa8b732 AK |
450 | } |
451 | ||
a461930b | 452 | static void FNAME(invlpg)(struct kvm_vcpu *vcpu, gva_t gva) |
a7052897 | 453 | { |
a461930b AK |
454 | struct kvm_shadow_walk_iterator iterator; |
455 | pt_element_t gpte; | |
456 | gpa_t pte_gpa = -1; | |
457 | int level; | |
458 | u64 *sptep; | |
4539b358 | 459 | int need_flush = 0; |
a461930b AK |
460 | |
461 | spin_lock(&vcpu->kvm->mmu_lock); | |
a7052897 | 462 | |
a461930b AK |
463 | for_each_shadow_entry(vcpu, gva, iterator) { |
464 | level = iterator.level; | |
465 | sptep = iterator.sptep; | |
ad218f85 | 466 | |
a461930b AK |
467 | /* FIXME: properly handle invlpg on large guest pages */ |
468 | if (level == PT_PAGE_TABLE_LEVEL || | |
469 | ((level == PT_DIRECTORY_LEVEL) && is_large_pte(*sptep))) { | |
470 | struct kvm_mmu_page *sp = page_header(__pa(sptep)); | |
ad218f85 | 471 | |
a461930b AK |
472 | pte_gpa = (sp->gfn << PAGE_SHIFT); |
473 | pte_gpa += (sptep - sp->spt) * sizeof(pt_element_t); | |
474 | ||
475 | if (is_shadow_present_pte(*sptep)) { | |
476 | rmap_remove(vcpu->kvm, sptep); | |
477 | if (is_large_pte(*sptep)) | |
478 | --vcpu->kvm->stat.lpages; | |
4539b358 | 479 | need_flush = 1; |
a461930b | 480 | } |
d555c333 | 481 | __set_spte(sptep, shadow_trap_nonpresent_pte); |
a461930b | 482 | break; |
87917239 | 483 | } |
a7052897 | 484 | |
a461930b AK |
485 | if (!is_shadow_present_pte(*sptep)) |
486 | break; | |
487 | } | |
a7052897 | 488 | |
4539b358 AA |
489 | if (need_flush) |
490 | kvm_flush_remote_tlbs(vcpu->kvm); | |
ad218f85 | 491 | spin_unlock(&vcpu->kvm->mmu_lock); |
a461930b AK |
492 | |
493 | if (pte_gpa == -1) | |
ad218f85 | 494 | return; |
a461930b | 495 | if (kvm_read_guest_atomic(vcpu->kvm, pte_gpa, &gpte, |
ad218f85 MT |
496 | sizeof(pt_element_t))) |
497 | return; | |
43a3795a | 498 | if (is_present_gpte(gpte) && (gpte & PT_ACCESSED_MASK)) { |
ad218f85 MT |
499 | if (mmu_topup_memory_caches(vcpu)) |
500 | return; | |
a461930b | 501 | kvm_mmu_pte_write(vcpu, pte_gpa, (const u8 *)&gpte, |
ad218f85 MT |
502 | sizeof(pt_element_t), 0); |
503 | } | |
a7052897 MT |
504 | } |
505 | ||
6aa8b732 AK |
506 | static gpa_t FNAME(gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t vaddr) |
507 | { | |
508 | struct guest_walker walker; | |
e119d117 AK |
509 | gpa_t gpa = UNMAPPED_GVA; |
510 | int r; | |
6aa8b732 | 511 | |
e119d117 | 512 | r = FNAME(walk_addr)(&walker, vcpu, vaddr, 0, 0, 0); |
6aa8b732 | 513 | |
e119d117 | 514 | if (r) { |
1755fbcc | 515 | gpa = gfn_to_gpa(walker.gfn); |
e119d117 | 516 | gpa |= vaddr & ~PAGE_MASK; |
6aa8b732 AK |
517 | } |
518 | ||
519 | return gpa; | |
520 | } | |
521 | ||
c7addb90 AK |
522 | static void FNAME(prefetch_page)(struct kvm_vcpu *vcpu, |
523 | struct kvm_mmu_page *sp) | |
524 | { | |
eab9f71f AK |
525 | int i, j, offset, r; |
526 | pt_element_t pt[256 / sizeof(pt_element_t)]; | |
527 | gpa_t pte_gpa; | |
c7addb90 | 528 | |
f6e2c02b | 529 | if (sp->role.direct |
e5a4c8ca | 530 | || (PTTYPE == 32 && sp->role.level > PT_PAGE_TABLE_LEVEL)) { |
c7addb90 AK |
531 | nonpaging_prefetch_page(vcpu, sp); |
532 | return; | |
533 | } | |
534 | ||
eab9f71f AK |
535 | pte_gpa = gfn_to_gpa(sp->gfn); |
536 | if (PTTYPE == 32) { | |
e5a4c8ca | 537 | offset = sp->role.quadrant << PT64_LEVEL_BITS; |
eab9f71f AK |
538 | pte_gpa += offset * sizeof(pt_element_t); |
539 | } | |
7ec54588 | 540 | |
eab9f71f AK |
541 | for (i = 0; i < PT64_ENT_PER_PAGE; i += ARRAY_SIZE(pt)) { |
542 | r = kvm_read_guest_atomic(vcpu->kvm, pte_gpa, pt, sizeof pt); | |
543 | pte_gpa += ARRAY_SIZE(pt) * sizeof(pt_element_t); | |
544 | for (j = 0; j < ARRAY_SIZE(pt); ++j) | |
43a3795a | 545 | if (r || is_present_gpte(pt[j])) |
eab9f71f AK |
546 | sp->spt[i+j] = shadow_trap_nonpresent_pte; |
547 | else | |
548 | sp->spt[i+j] = shadow_notrap_nonpresent_pte; | |
7ec54588 | 549 | } |
c7addb90 AK |
550 | } |
551 | ||
e8bc217a MT |
552 | /* |
553 | * Using the cached information from sp->gfns is safe because: | |
554 | * - The spte has a reference to the struct page, so the pfn for a given gfn | |
555 | * can't change unless all sptes pointing to it are nuked first. | |
556 | * - Alias changes zap the entire shadow cache. | |
557 | */ | |
558 | static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp) | |
559 | { | |
560 | int i, offset, nr_present; | |
561 | ||
562 | offset = nr_present = 0; | |
563 | ||
564 | if (PTTYPE == 32) | |
565 | offset = sp->role.quadrant << PT64_LEVEL_BITS; | |
566 | ||
567 | for (i = 0; i < PT64_ENT_PER_PAGE; i++) { | |
568 | unsigned pte_access; | |
569 | pt_element_t gpte; | |
570 | gpa_t pte_gpa; | |
571 | gfn_t gfn = sp->gfns[i]; | |
572 | ||
573 | if (!is_shadow_present_pte(sp->spt[i])) | |
574 | continue; | |
575 | ||
576 | pte_gpa = gfn_to_gpa(sp->gfn); | |
577 | pte_gpa += (i+offset) * sizeof(pt_element_t); | |
578 | ||
579 | if (kvm_read_guest_atomic(vcpu->kvm, pte_gpa, &gpte, | |
580 | sizeof(pt_element_t))) | |
581 | return -EINVAL; | |
582 | ||
43a3795a | 583 | if (gpte_to_gfn(gpte) != gfn || !is_present_gpte(gpte) || |
e8bc217a MT |
584 | !(gpte & PT_ACCESSED_MASK)) { |
585 | u64 nonpresent; | |
586 | ||
587 | rmap_remove(vcpu->kvm, &sp->spt[i]); | |
43a3795a | 588 | if (is_present_gpte(gpte)) |
e8bc217a MT |
589 | nonpresent = shadow_trap_nonpresent_pte; |
590 | else | |
591 | nonpresent = shadow_notrap_nonpresent_pte; | |
d555c333 | 592 | __set_spte(&sp->spt[i], nonpresent); |
e8bc217a MT |
593 | continue; |
594 | } | |
595 | ||
596 | nr_present++; | |
597 | pte_access = sp->role.access & FNAME(gpte_access)(vcpu, gpte); | |
598 | set_spte(vcpu, &sp->spt[i], pte_access, 0, 0, | |
43a3795a | 599 | is_dirty_gpte(gpte), 0, gfn, |
4731d4c7 | 600 | spte_to_pfn(sp->spt[i]), true, false); |
e8bc217a MT |
601 | } |
602 | ||
603 | return !nr_present; | |
604 | } | |
605 | ||
6aa8b732 AK |
606 | #undef pt_element_t |
607 | #undef guest_walker | |
608 | #undef FNAME | |
609 | #undef PT_BASE_ADDR_MASK | |
610 | #undef PT_INDEX | |
6aa8b732 | 611 | #undef PT_LEVEL_MASK |
6aa8b732 | 612 | #undef PT_DIR_BASE_ADDR_MASK |
c7addb90 | 613 | #undef PT_LEVEL_BITS |
cea0f0e7 | 614 | #undef PT_MAX_FULL_LEVELS |
5fb07ddb AK |
615 | #undef gpte_to_gfn |
616 | #undef gpte_to_gfn_pde | |
b3e4e63f | 617 | #undef CMPXCHG |