2 * Kernel-based Virtual Machine driver for Linux
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
9 * Copyright (C) 2006 Qumranet, Inc.
12 * Yaniv Kamay <yaniv@qumranet.com>
13 * Avi Kivity <avi@qumranet.com>
15 * This work is licensed under the terms of the GNU GPL, version 2. See
16 * the COPYING file in the top-level directory.
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.
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
36 #define PT_MAX_FULL_LEVELS 4
38 #define PT_MAX_FULL_LEVELS 2
41 #define pt_element_t u32
42 #define guest_walker guest_walker32
43 #define FNAME(name) paging##32_##name
44 #define PT_BASE_ADDR_MASK PT32_BASE_ADDR_MASK
45 #define PT_DIR_BASE_ADDR_MASK PT32_DIR_BASE_ADDR_MASK
46 #define PT_INDEX(addr, level) PT32_INDEX(addr, level)
47 #define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
48 #define PT_LEVEL_MASK(level) PT32_LEVEL_MASK(level)
49 #define PT_LEVEL_BITS PT32_LEVEL_BITS
50 #define PT_MAX_FULL_LEVELS 2
52 #error Invalid PTTYPE value
56 * The guest_walker structure emulates the behavior of the hardware page
61 gfn_t table_gfn[PT_MAX_FULL_LEVELS];
67 pt_element_t inherited_ar;
72 static void FNAME(update_dirty_bit)(struct kvm_vcpu *vcpu,
77 if (write_fault && !is_dirty_pte(*ptep)) {
78 mark_page_dirty(vcpu->kvm, table_gfn);
79 *ptep |= PT_DIRTY_MASK;
84 * Fetch a guest pte for a guest virtual address
86 static int FNAME(walk_addr)(struct guest_walker *walker,
87 struct kvm_vcpu *vcpu, gva_t addr,
88 int write_fault, int user_fault, int fetch_fault)
91 struct kvm_memory_slot *slot;
96 pgprintk("%s: addr %lx\n", __FUNCTION__, addr);
97 walker->level = vcpu->mmu.root_level;
103 if (!is_long_mode(vcpu)) {
104 walker->ptep = &vcpu->pdptrs[(addr >> 30) & 3];
105 root = *walker->ptep;
107 if (!(root & PT_PRESENT_MASK))
112 table_gfn = (root & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
113 walker->table_gfn[walker->level - 1] = table_gfn;
114 pgprintk("%s: table_gfn[%d] %lx\n", __FUNCTION__,
115 walker->level - 1, table_gfn);
116 slot = gfn_to_memslot(vcpu->kvm, table_gfn);
117 hpa = safe_gpa_to_hpa(vcpu->kvm, root & PT64_BASE_ADDR_MASK);
118 walker->page = pfn_to_page(hpa >> PAGE_SHIFT);
119 walker->table = kmap_atomic(walker->page, KM_USER0);
121 ASSERT((!is_long_mode(vcpu) && is_pae(vcpu)) ||
122 (vcpu->cr3 & CR3_NONPAE_RESERVED_BITS) == 0);
124 walker->inherited_ar = PT_USER_MASK | PT_WRITABLE_MASK;
127 int index = PT_INDEX(addr, walker->level);
130 ptep = &walker->table[index];
131 walker->index = index;
132 ASSERT(((unsigned long)walker->table & PAGE_MASK) ==
133 ((unsigned long)ptep & PAGE_MASK));
135 if (!is_present_pte(*ptep))
138 if (write_fault && !is_writeble_pte(*ptep))
139 if (user_fault || is_write_protection(vcpu))
142 if (user_fault && !(*ptep & PT_USER_MASK))
146 if (fetch_fault && is_nx(vcpu) && (*ptep & PT64_NX_MASK))
150 if (!(*ptep & PT_ACCESSED_MASK)) {
151 mark_page_dirty(vcpu->kvm, table_gfn);
152 *ptep |= PT_ACCESSED_MASK;
155 if (walker->level == PT_PAGE_TABLE_LEVEL) {
156 walker->gfn = (*ptep & PT_BASE_ADDR_MASK)
158 FNAME(update_dirty_bit)(vcpu, write_fault, ptep,
163 if (walker->level == PT_DIRECTORY_LEVEL
164 && (*ptep & PT_PAGE_SIZE_MASK)
165 && (PTTYPE == 64 || is_pse(vcpu))) {
166 walker->gfn = (*ptep & PT_DIR_BASE_ADDR_MASK)
168 walker->gfn += PT_INDEX(addr, PT_PAGE_TABLE_LEVEL);
169 FNAME(update_dirty_bit)(vcpu, write_fault, ptep,
174 walker->inherited_ar &= walker->table[index];
175 table_gfn = (*ptep & PT_BASE_ADDR_MASK) >> PAGE_SHIFT;
176 kunmap_atomic(walker->table, KM_USER0);
177 paddr = safe_gpa_to_hpa(vcpu->kvm, table_gfn << PAGE_SHIFT);
178 walker->page = pfn_to_page(paddr >> PAGE_SHIFT);
179 walker->table = kmap_atomic(walker->page, KM_USER0);
181 walker->table_gfn[walker->level - 1] = table_gfn;
182 pgprintk("%s: table_gfn[%d] %lx\n", __FUNCTION__,
183 walker->level - 1, table_gfn);
189 kunmap_atomic(walker->table, KM_USER0);
190 pgprintk("%s: pte %llx\n", __FUNCTION__, (u64)*ptep);
194 walker->error_code = 0;
198 walker->error_code = PFERR_PRESENT_MASK;
202 walker->error_code |= PFERR_WRITE_MASK;
204 walker->error_code |= PFERR_USER_MASK;
206 walker->error_code |= PFERR_FETCH_MASK;
208 kunmap_atomic(walker->table, KM_USER0);
212 static void FNAME(set_pte_common)(struct kvm_vcpu *vcpu,
220 struct guest_walker *walker,
224 int dirty = gpte & PT_DIRTY_MASK;
226 int was_rmapped = is_rmap_pte(*shadow_pte);
228 pgprintk("%s: spte %llx gpte %llx access %llx write_fault %d"
229 " user_fault %d gfn %lx\n",
230 __FUNCTION__, *shadow_pte, (u64)gpte, access_bits,
231 write_fault, user_fault, gfn);
234 * We don't set the accessed bit, since we sometimes want to see
235 * whether the guest actually used the pte (in order to detect
238 spte = PT_PRESENT_MASK | PT_DIRTY_MASK;
239 spte |= gpte & PT64_NX_MASK;
241 access_bits &= ~PT_WRITABLE_MASK;
243 paddr = gpa_to_hpa(vcpu->kvm, gaddr & PT64_BASE_ADDR_MASK);
245 spte |= PT_PRESENT_MASK;
246 if (access_bits & PT_USER_MASK)
247 spte |= PT_USER_MASK;
249 if (is_error_hpa(paddr)) {
250 set_shadow_pte(shadow_pte,
251 shadow_trap_nonpresent_pte | PT_SHADOW_IO_MARK);
257 if ((access_bits & PT_WRITABLE_MASK)
258 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
259 struct kvm_mmu_page *shadow;
261 spte |= PT_WRITABLE_MASK;
263 mmu_unshadow(vcpu->kvm, gfn);
267 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
269 pgprintk("%s: found shadow page for %lx, marking ro\n",
271 access_bits &= ~PT_WRITABLE_MASK;
272 if (is_writeble_pte(spte)) {
273 spte &= ~PT_WRITABLE_MASK;
274 kvm_x86_ops->tlb_flush(vcpu);
283 if (access_bits & PT_WRITABLE_MASK)
284 mark_page_dirty(vcpu->kvm, gaddr >> PAGE_SHIFT);
286 pgprintk("%s: setting spte %llx\n", __FUNCTION__, spte);
287 set_shadow_pte(shadow_pte, spte);
288 page_header_update_slot(vcpu->kvm, shadow_pte, gaddr);
290 rmap_add(vcpu, shadow_pte, (gaddr & PT64_BASE_ADDR_MASK)
292 if (!ptwrite || !*ptwrite)
293 vcpu->last_pte_updated = shadow_pte;
296 static void FNAME(set_pte)(struct kvm_vcpu *vcpu, pt_element_t gpte,
297 u64 *shadow_pte, u64 access_bits,
298 int user_fault, int write_fault, int *ptwrite,
299 struct guest_walker *walker, gfn_t gfn)
302 FNAME(set_pte_common)(vcpu, shadow_pte, gpte & PT_BASE_ADDR_MASK,
303 gpte, access_bits, user_fault, write_fault,
304 ptwrite, walker, gfn);
307 static void FNAME(update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *page,
308 u64 *spte, const void *pte, int bytes,
313 gpte = *(const pt_element_t *)pte;
314 if (~gpte & (PT_PRESENT_MASK | PT_ACCESSED_MASK)) {
315 if (!offset_in_pte && !is_present_pte(gpte))
316 set_shadow_pte(spte, shadow_notrap_nonpresent_pte);
319 if (bytes < sizeof(pt_element_t))
321 pgprintk("%s: gpte %llx spte %p\n", __FUNCTION__, (u64)gpte, spte);
322 FNAME(set_pte)(vcpu, gpte, spte, PT_USER_MASK | PT_WRITABLE_MASK, 0,
324 (gpte & PT_BASE_ADDR_MASK) >> PAGE_SHIFT);
327 static void FNAME(set_pde)(struct kvm_vcpu *vcpu, pt_element_t gpde,
328 u64 *shadow_pte, u64 access_bits,
329 int user_fault, int write_fault, int *ptwrite,
330 struct guest_walker *walker, gfn_t gfn)
335 gaddr = (gpa_t)gfn << PAGE_SHIFT;
336 if (PTTYPE == 32 && is_cpuid_PSE36())
337 gaddr |= (gpde & PT32_DIR_PSE36_MASK) <<
338 (32 - PT32_DIR_PSE36_SHIFT);
339 FNAME(set_pte_common)(vcpu, shadow_pte, gaddr,
340 gpde, access_bits, user_fault, write_fault,
341 ptwrite, walker, gfn);
345 * Fetch a shadow pte for a specific level in the paging hierarchy.
347 static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
348 struct guest_walker *walker,
349 int user_fault, int write_fault, int *ptwrite)
354 u64 *prev_shadow_ent = NULL;
356 if (!is_present_pte(walker->pte))
359 shadow_addr = vcpu->mmu.root_hpa;
360 level = vcpu->mmu.shadow_root_level;
361 if (level == PT32E_ROOT_LEVEL) {
362 shadow_addr = vcpu->mmu.pae_root[(addr >> 30) & 3];
363 shadow_addr &= PT64_BASE_ADDR_MASK;
368 u32 index = SHADOW_PT_INDEX(addr, level);
369 struct kvm_mmu_page *shadow_page;
373 unsigned hugepage_access = 0;
375 shadow_ent = ((u64 *)__va(shadow_addr)) + index;
376 if (is_shadow_present_pte(*shadow_ent)) {
377 if (level == PT_PAGE_TABLE_LEVEL)
379 shadow_addr = *shadow_ent & PT64_BASE_ADDR_MASK;
380 prev_shadow_ent = shadow_ent;
384 if (level == PT_PAGE_TABLE_LEVEL)
387 if (level - 1 == PT_PAGE_TABLE_LEVEL
388 && walker->level == PT_DIRECTORY_LEVEL) {
390 hugepage_access = walker->pte;
391 hugepage_access &= PT_USER_MASK | PT_WRITABLE_MASK;
392 if (!is_dirty_pte(walker->pte))
393 hugepage_access &= ~PT_WRITABLE_MASK;
394 hugepage_access >>= PT_WRITABLE_SHIFT;
395 if (walker->pte & PT64_NX_MASK)
396 hugepage_access |= (1 << 2);
397 table_gfn = (walker->pte & PT_BASE_ADDR_MASK)
401 table_gfn = walker->table_gfn[level - 2];
403 shadow_page = kvm_mmu_get_page(vcpu, table_gfn, addr, level-1,
404 metaphysical, hugepage_access,
406 shadow_addr = __pa(shadow_page->spt);
407 shadow_pte = shadow_addr | PT_PRESENT_MASK | PT_ACCESSED_MASK
408 | PT_WRITABLE_MASK | PT_USER_MASK;
409 *shadow_ent = shadow_pte;
410 prev_shadow_ent = shadow_ent;
413 if (walker->level == PT_DIRECTORY_LEVEL) {
414 FNAME(set_pde)(vcpu, walker->pte, shadow_ent,
415 walker->inherited_ar, user_fault, write_fault,
416 ptwrite, walker, walker->gfn);
418 ASSERT(walker->level == PT_PAGE_TABLE_LEVEL);
419 FNAME(set_pte)(vcpu, walker->pte, shadow_ent,
420 walker->inherited_ar, user_fault, write_fault,
421 ptwrite, walker, walker->gfn);
427 * Page fault handler. There are several causes for a page fault:
428 * - there is no shadow pte for the guest pte
429 * - write access through a shadow pte marked read only so that we can set
431 * - write access to a shadow pte marked read only so we can update the page
432 * dirty bitmap, when userspace requests it
433 * - mmio access; in this case we will never install a present shadow pte
434 * - normal guest page fault due to the guest pte marked not present, not
435 * writable, or not executable
437 * Returns: 1 if we need to emulate the instruction, 0 otherwise, or
438 * a negative value on error.
440 static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr,
443 int write_fault = error_code & PFERR_WRITE_MASK;
444 int user_fault = error_code & PFERR_USER_MASK;
445 int fetch_fault = error_code & PFERR_FETCH_MASK;
446 struct guest_walker walker;
451 pgprintk("%s: addr %lx err %x\n", __FUNCTION__, addr, error_code);
452 kvm_mmu_audit(vcpu, "pre page fault");
454 r = mmu_topup_memory_caches(vcpu);
459 * Look up the shadow pte for the faulting address.
461 r = FNAME(walk_addr)(&walker, vcpu, addr, write_fault, user_fault,
465 * The page is not mapped by the guest. Let the guest handle it.
468 pgprintk("%s: guest page fault\n", __FUNCTION__);
469 inject_page_fault(vcpu, addr, walker.error_code);
470 vcpu->last_pt_write_count = 0; /* reset fork detector */
474 shadow_pte = FNAME(fetch)(vcpu, addr, &walker, user_fault, write_fault,
476 pgprintk("%s: shadow pte %p %llx ptwrite %d\n", __FUNCTION__,
477 shadow_pte, *shadow_pte, write_pt);
480 vcpu->last_pt_write_count = 0; /* reset fork detector */
483 * mmio: emulate if accessible, otherwise its a guest fault.
485 if (is_io_pte(*shadow_pte))
488 ++vcpu->stat.pf_fixed;
489 kvm_mmu_audit(vcpu, "post page fault (fixed)");
494 static gpa_t FNAME(gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t vaddr)
496 struct guest_walker walker;
497 gpa_t gpa = UNMAPPED_GVA;
500 r = FNAME(walk_addr)(&walker, vcpu, vaddr, 0, 0, 0);
503 gpa = (gpa_t)walker.gfn << PAGE_SHIFT;
504 gpa |= vaddr & ~PAGE_MASK;
510 static void FNAME(prefetch_page)(struct kvm_vcpu *vcpu,
511 struct kvm_mmu_page *sp)
516 if (sp->role.metaphysical || PTTYPE == 32) {
517 nonpaging_prefetch_page(vcpu, sp);
521 gpt = kmap_atomic(gfn_to_page(vcpu->kvm, sp->gfn), KM_USER0);
522 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
523 if (is_present_pte(gpt[i]))
524 sp->spt[i] = shadow_trap_nonpresent_pte;
526 sp->spt[i] = shadow_notrap_nonpresent_pte;
527 kunmap_atomic(gpt, KM_USER0);
533 #undef PT_BASE_ADDR_MASK
535 #undef SHADOW_PT_INDEX
537 #undef PT_DIR_BASE_ADDR_MASK
539 #undef PT_MAX_FULL_LEVELS