KVM: MMU: Simplify calculation of pte access
[powerpc.git] / drivers / 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  *
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)
32         #define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
33         #define PT_LEVEL_MASK(level) PT64_LEVEL_MASK(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_DIR_BASE_ADDR_MASK PT32_DIR_BASE_ADDR_MASK
48         #define PT_INDEX(addr, level) PT32_INDEX(addr, level)
49         #define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
50         #define PT_LEVEL_MASK(level) PT32_LEVEL_MASK(level)
51         #define PT_LEVEL_BITS PT32_LEVEL_BITS
52         #define PT_MAX_FULL_LEVELS 2
53         #define CMPXCHG cmpxchg
54 #else
55         #error Invalid PTTYPE value
56 #endif
57
58 #define gpte_to_gfn FNAME(gpte_to_gfn)
59 #define gpte_to_gfn_pde FNAME(gpte_to_gfn_pde)
60
61 /*
62  * The guest_walker structure emulates the behavior of the hardware page
63  * table walker.
64  */
65 struct guest_walker {
66         int level;
67         gfn_t table_gfn[PT_MAX_FULL_LEVELS];
68         pt_element_t pte;
69         unsigned pt_access;
70         unsigned pte_access;
71         gfn_t gfn;
72         u32 error_code;
73 };
74
75 static gfn_t gpte_to_gfn(pt_element_t gpte)
76 {
77         return (gpte & PT_BASE_ADDR_MASK) >> PAGE_SHIFT;
78 }
79
80 static gfn_t gpte_to_gfn_pde(pt_element_t gpte)
81 {
82         return (gpte & PT_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
83 }
84
85 static bool FNAME(cmpxchg_gpte)(struct kvm *kvm,
86                          gfn_t table_gfn, unsigned index,
87                          pt_element_t orig_pte, pt_element_t new_pte)
88 {
89         pt_element_t ret;
90         pt_element_t *table;
91         struct page *page;
92
93         page = gfn_to_page(kvm, table_gfn);
94         table = kmap_atomic(page, KM_USER0);
95
96         ret = CMPXCHG(&table[index], orig_pte, new_pte);
97
98         kunmap_atomic(table, KM_USER0);
99
100         kvm_release_page_dirty(page);
101
102         return (ret != orig_pte);
103 }
104
105 /*
106  * Fetch a guest pte for a guest virtual address
107  */
108 static int FNAME(walk_addr)(struct guest_walker *walker,
109                             struct kvm_vcpu *vcpu, gva_t addr,
110                             int write_fault, int user_fault, int fetch_fault)
111 {
112         pt_element_t pte;
113         gfn_t table_gfn;
114         unsigned index, pt_access, pte_access;
115         gpa_t pte_gpa;
116
117         pgprintk("%s: addr %lx\n", __FUNCTION__, addr);
118 walk:
119         walker->level = vcpu->mmu.root_level;
120         pte = vcpu->cr3;
121 #if PTTYPE == 64
122         if (!is_long_mode(vcpu)) {
123                 pte = vcpu->pdptrs[(addr >> 30) & 3];
124                 if (!is_present_pte(pte))
125                         goto not_present;
126                 --walker->level;
127         }
128 #endif
129         ASSERT((!is_long_mode(vcpu) && is_pae(vcpu)) ||
130                (vcpu->cr3 & CR3_NONPAE_RESERVED_BITS) == 0);
131
132         pt_access = ACC_ALL;
133
134         for (;;) {
135                 index = PT_INDEX(addr, walker->level);
136
137                 table_gfn = gpte_to_gfn(pte);
138                 pte_gpa = gfn_to_gpa(table_gfn);
139                 pte_gpa += index * sizeof(pt_element_t);
140                 walker->table_gfn[walker->level - 1] = table_gfn;
141                 pgprintk("%s: table_gfn[%d] %lx\n", __FUNCTION__,
142                          walker->level - 1, table_gfn);
143
144                 kvm_read_guest(vcpu->kvm, pte_gpa, &pte, sizeof(pte));
145
146                 if (!is_present_pte(pte))
147                         goto not_present;
148
149                 if (write_fault && !is_writeble_pte(pte))
150                         if (user_fault || is_write_protection(vcpu))
151                                 goto access_error;
152
153                 if (user_fault && !(pte & PT_USER_MASK))
154                         goto access_error;
155
156 #if PTTYPE == 64
157                 if (fetch_fault && is_nx(vcpu) && (pte & PT64_NX_MASK))
158                         goto access_error;
159 #endif
160
161                 if (!(pte & PT_ACCESSED_MASK)) {
162                         mark_page_dirty(vcpu->kvm, table_gfn);
163                         if (FNAME(cmpxchg_gpte)(vcpu->kvm, table_gfn,
164                             index, pte, pte|PT_ACCESSED_MASK))
165                                 goto walk;
166                         pte |= PT_ACCESSED_MASK;
167                 }
168
169                 pte_access = pte & (PT_WRITABLE_MASK | PT_USER_MASK);
170                 pte_access |= ACC_EXEC_MASK;
171 #if PTTYPE == 64
172                 if (is_nx(vcpu))
173                         pte_access &= ~(pte >> PT64_NX_SHIFT);
174 #endif
175                 pte_access &= pt_access;
176
177                 if (walker->level == PT_PAGE_TABLE_LEVEL) {
178                         walker->gfn = gpte_to_gfn(pte);
179                         break;
180                 }
181
182                 if (walker->level == PT_DIRECTORY_LEVEL
183                     && (pte & PT_PAGE_SIZE_MASK)
184                     && (PTTYPE == 64 || is_pse(vcpu))) {
185                         walker->gfn = gpte_to_gfn_pde(pte);
186                         walker->gfn += PT_INDEX(addr, PT_PAGE_TABLE_LEVEL);
187                         if (PTTYPE == 32 && is_cpuid_PSE36())
188                                 walker->gfn += pse36_gfn_delta(pte);
189                         break;
190                 }
191
192                 pt_access = pte_access;
193                 --walker->level;
194         }
195
196         if (write_fault && !is_dirty_pte(pte)) {
197                 bool ret;
198
199                 mark_page_dirty(vcpu->kvm, table_gfn);
200                 ret = FNAME(cmpxchg_gpte)(vcpu->kvm, table_gfn, index, pte,
201                             pte|PT_DIRTY_MASK);
202                 if (ret)
203                         goto walk;
204                 pte |= PT_DIRTY_MASK;
205                 kvm_mmu_pte_write(vcpu, pte_gpa, (u8 *)&pte, sizeof(pte));
206         }
207
208         walker->pte = pte;
209         walker->pt_access = pt_access;
210         walker->pte_access = pte_access;
211         pgprintk("%s: pte %llx pte_access %x pt_access %x\n",
212                  __FUNCTION__, (u64)pte, pt_access, pte_access);
213         return 1;
214
215 not_present:
216         walker->error_code = 0;
217         goto err;
218
219 access_error:
220         walker->error_code = PFERR_PRESENT_MASK;
221
222 err:
223         if (write_fault)
224                 walker->error_code |= PFERR_WRITE_MASK;
225         if (user_fault)
226                 walker->error_code |= PFERR_USER_MASK;
227         if (fetch_fault)
228                 walker->error_code |= PFERR_FETCH_MASK;
229         return 0;
230 }
231
232 static void FNAME(set_pte)(struct kvm_vcpu *vcpu, pt_element_t gpte,
233                            u64 *shadow_pte, unsigned pt_access,
234                            unsigned pte_access,
235                            int user_fault, int write_fault,
236                            int *ptwrite, struct guest_walker *walker,
237                            gfn_t gfn)
238 {
239         int dirty = gpte & PT_DIRTY_MASK;
240         u64 spte;
241         int was_rmapped = is_rmap_pte(*shadow_pte);
242         struct page *page;
243
244         pgprintk("%s: spte %llx gpte %llx access %x write_fault %d"
245                  " user_fault %d gfn %lx\n",
246                  __FUNCTION__, *shadow_pte, (u64)gpte, pt_access,
247                  write_fault, user_fault, gfn);
248
249         /*
250          * We don't set the accessed bit, since we sometimes want to see
251          * whether the guest actually used the pte (in order to detect
252          * demand paging).
253          */
254         spte = PT_PRESENT_MASK | PT_DIRTY_MASK;
255         spte |= gpte & PT64_NX_MASK;
256         if (!dirty)
257                 pte_access &= ~ACC_WRITE_MASK;
258
259         page = gfn_to_page(vcpu->kvm, gfn);
260
261         spte |= PT_PRESENT_MASK;
262         if (pte_access & ACC_USER_MASK)
263                 spte |= PT_USER_MASK;
264
265         if (is_error_page(page)) {
266                 set_shadow_pte(shadow_pte,
267                                shadow_trap_nonpresent_pte | PT_SHADOW_IO_MARK);
268                 kvm_release_page_clean(page);
269                 return;
270         }
271
272         spte |= page_to_phys(page);
273
274         if ((pte_access & ACC_WRITE_MASK)
275             || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
276                 struct kvm_mmu_page *shadow;
277
278                 spte |= PT_WRITABLE_MASK;
279                 if (user_fault) {
280                         mmu_unshadow(vcpu->kvm, gfn);
281                         goto unshadowed;
282                 }
283
284                 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
285                 if (shadow) {
286                         pgprintk("%s: found shadow page for %lx, marking ro\n",
287                                  __FUNCTION__, gfn);
288                         pte_access &= ~ACC_WRITE_MASK;
289                         if (is_writeble_pte(spte)) {
290                                 spte &= ~PT_WRITABLE_MASK;
291                                 kvm_x86_ops->tlb_flush(vcpu);
292                         }
293                         if (write_fault)
294                                 *ptwrite = 1;
295                 }
296         }
297
298 unshadowed:
299
300         if (pte_access & ACC_WRITE_MASK)
301                 mark_page_dirty(vcpu->kvm, gfn);
302
303         pgprintk("%s: setting spte %llx\n", __FUNCTION__, spte);
304         set_shadow_pte(shadow_pte, spte);
305         page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
306         if (!was_rmapped) {
307                 rmap_add(vcpu, shadow_pte, gfn);
308                 if (!is_rmap_pte(*shadow_pte))
309                         kvm_release_page_clean(page);
310         }
311         else
312                 kvm_release_page_clean(page);
313         if (!ptwrite || !*ptwrite)
314                 vcpu->last_pte_updated = shadow_pte;
315 }
316
317 static void FNAME(update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *page,
318                               u64 *spte, const void *pte, int bytes,
319                               int offset_in_pte)
320 {
321         pt_element_t gpte;
322
323         gpte = *(const pt_element_t *)pte;
324         if (~gpte & (PT_PRESENT_MASK | PT_ACCESSED_MASK)) {
325                 if (!offset_in_pte && !is_present_pte(gpte))
326                         set_shadow_pte(spte, shadow_notrap_nonpresent_pte);
327                 return;
328         }
329         if (bytes < sizeof(pt_element_t))
330                 return;
331         pgprintk("%s: gpte %llx spte %p\n", __FUNCTION__, (u64)gpte, spte);
332         FNAME(set_pte)(vcpu, gpte, spte, ACC_ALL, ACC_ALL,
333                        0, 0, NULL, NULL, gpte_to_gfn(gpte));
334 }
335
336 /*
337  * Fetch a shadow pte for a specific level in the paging hierarchy.
338  */
339 static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
340                          struct guest_walker *walker,
341                          int user_fault, int write_fault, int *ptwrite)
342 {
343         hpa_t shadow_addr;
344         int level;
345         u64 *shadow_ent;
346         unsigned access = walker->pt_access;
347
348         if (!is_present_pte(walker->pte))
349                 return NULL;
350
351         shadow_addr = vcpu->mmu.root_hpa;
352         level = vcpu->mmu.shadow_root_level;
353         if (level == PT32E_ROOT_LEVEL) {
354                 shadow_addr = vcpu->mmu.pae_root[(addr >> 30) & 3];
355                 shadow_addr &= PT64_BASE_ADDR_MASK;
356                 --level;
357         }
358
359         for (; ; level--) {
360                 u32 index = SHADOW_PT_INDEX(addr, level);
361                 struct kvm_mmu_page *shadow_page;
362                 u64 shadow_pte;
363                 int metaphysical;
364                 gfn_t table_gfn;
365
366                 shadow_ent = ((u64 *)__va(shadow_addr)) + index;
367                 if (is_shadow_present_pte(*shadow_ent)) {
368                         if (level == PT_PAGE_TABLE_LEVEL)
369                                 break;
370                         shadow_addr = *shadow_ent & PT64_BASE_ADDR_MASK;
371                         continue;
372                 }
373
374                 if (level == PT_PAGE_TABLE_LEVEL)
375                         break;
376
377                 if (level - 1 == PT_PAGE_TABLE_LEVEL
378                     && walker->level == PT_DIRECTORY_LEVEL) {
379                         metaphysical = 1;
380                         if (!is_dirty_pte(walker->pte))
381                                 access &= ~ACC_WRITE_MASK;
382                         table_gfn = gpte_to_gfn(walker->pte);
383                 } else {
384                         metaphysical = 0;
385                         table_gfn = walker->table_gfn[level - 2];
386                 }
387                 shadow_page = kvm_mmu_get_page(vcpu, table_gfn, addr, level-1,
388                                                metaphysical, access,
389                                                shadow_ent);
390                 shadow_addr = __pa(shadow_page->spt);
391                 shadow_pte = shadow_addr | PT_PRESENT_MASK | PT_ACCESSED_MASK
392                         | PT_WRITABLE_MASK | PT_USER_MASK;
393                 *shadow_ent = shadow_pte;
394         }
395
396         FNAME(set_pte)(vcpu, walker->pte, shadow_ent,
397                        access, walker->pte_access & access,
398                        user_fault, write_fault,
399                        ptwrite, walker, walker->gfn);
400
401         return shadow_ent;
402 }
403
404 /*
405  * Page fault handler.  There are several causes for a page fault:
406  *   - there is no shadow pte for the guest pte
407  *   - write access through a shadow pte marked read only so that we can set
408  *     the dirty bit
409  *   - write access to a shadow pte marked read only so we can update the page
410  *     dirty bitmap, when userspace requests it
411  *   - mmio access; in this case we will never install a present shadow pte
412  *   - normal guest page fault due to the guest pte marked not present, not
413  *     writable, or not executable
414  *
415  *  Returns: 1 if we need to emulate the instruction, 0 otherwise, or
416  *           a negative value on error.
417  */
418 static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr,
419                                u32 error_code)
420 {
421         int write_fault = error_code & PFERR_WRITE_MASK;
422         int user_fault = error_code & PFERR_USER_MASK;
423         int fetch_fault = error_code & PFERR_FETCH_MASK;
424         struct guest_walker walker;
425         u64 *shadow_pte;
426         int write_pt = 0;
427         int r;
428
429         pgprintk("%s: addr %lx err %x\n", __FUNCTION__, addr, error_code);
430         kvm_mmu_audit(vcpu, "pre page fault");
431
432         r = mmu_topup_memory_caches(vcpu);
433         if (r)
434                 return r;
435
436         /*
437          * Look up the shadow pte for the faulting address.
438          */
439         r = FNAME(walk_addr)(&walker, vcpu, addr, write_fault, user_fault,
440                              fetch_fault);
441
442         /*
443          * The page is not mapped by the guest.  Let the guest handle it.
444          */
445         if (!r) {
446                 pgprintk("%s: guest page fault\n", __FUNCTION__);
447                 inject_page_fault(vcpu, addr, walker.error_code);
448                 vcpu->last_pt_write_count = 0; /* reset fork detector */
449                 return 0;
450         }
451
452         shadow_pte = FNAME(fetch)(vcpu, addr, &walker, user_fault, write_fault,
453                                   &write_pt);
454         pgprintk("%s: shadow pte %p %llx ptwrite %d\n", __FUNCTION__,
455                  shadow_pte, *shadow_pte, write_pt);
456
457         if (!write_pt)
458                 vcpu->last_pt_write_count = 0; /* reset fork detector */
459
460         /*
461          * mmio: emulate if accessible, otherwise its a guest fault.
462          */
463         if (is_io_pte(*shadow_pte))
464                 return 1;
465
466         ++vcpu->stat.pf_fixed;
467         kvm_mmu_audit(vcpu, "post page fault (fixed)");
468
469         return write_pt;
470 }
471
472 static gpa_t FNAME(gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t vaddr)
473 {
474         struct guest_walker walker;
475         gpa_t gpa = UNMAPPED_GVA;
476         int r;
477
478         r = FNAME(walk_addr)(&walker, vcpu, vaddr, 0, 0, 0);
479
480         if (r) {
481                 gpa = gfn_to_gpa(walker.gfn);
482                 gpa |= vaddr & ~PAGE_MASK;
483         }
484
485         return gpa;
486 }
487
488 static void FNAME(prefetch_page)(struct kvm_vcpu *vcpu,
489                                  struct kvm_mmu_page *sp)
490 {
491         int i, offset = 0;
492         pt_element_t *gpt;
493         struct page *page;
494
495         if (sp->role.metaphysical
496             || (PTTYPE == 32 && sp->role.level > PT_PAGE_TABLE_LEVEL)) {
497                 nonpaging_prefetch_page(vcpu, sp);
498                 return;
499         }
500
501         if (PTTYPE == 32)
502                 offset = sp->role.quadrant << PT64_LEVEL_BITS;
503         page = gfn_to_page(vcpu->kvm, sp->gfn);
504         gpt = kmap_atomic(page, KM_USER0);
505         for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
506                 if (is_present_pte(gpt[offset + i]))
507                         sp->spt[i] = shadow_trap_nonpresent_pte;
508                 else
509                         sp->spt[i] = shadow_notrap_nonpresent_pte;
510         kunmap_atomic(gpt, KM_USER0);
511         kvm_release_page_clean(page);
512 }
513
514 #undef pt_element_t
515 #undef guest_walker
516 #undef FNAME
517 #undef PT_BASE_ADDR_MASK
518 #undef PT_INDEX
519 #undef SHADOW_PT_INDEX
520 #undef PT_LEVEL_MASK
521 #undef PT_DIR_BASE_ADDR_MASK
522 #undef PT_LEVEL_BITS
523 #undef PT_MAX_FULL_LEVELS
524 #undef gpte_to_gfn
525 #undef gpte_to_gfn_pde
526 #undef CMPXCHG