blob: 211fef83be5da898432d312c73ff34eff887ce0c [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
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)
Avi Kivityc7addb92007-09-16 18:58:32 +020034 #define PT_LEVEL_BITS PT64_LEVEL_BITS
Avi Kivitycea0f0e2007-01-05 16:36:43 -080035 #ifdef CONFIG_X86_64
36 #define PT_MAX_FULL_LEVELS 4
Marcelo Tosattib3e4e632007-12-07 07:56:58 -050037 #define CMPXCHG cmpxchg
Avi Kivitycea0f0e2007-01-05 16:36:43 -080038 #else
Marcelo Tosattib3e4e632007-12-07 07:56:58 -050039 #define CMPXCHG cmpxchg64
Avi Kivitycea0f0e2007-01-05 16:36:43 -080040 #define PT_MAX_FULL_LEVELS 2
41 #endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080042#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)
Avi Kivityc7addb92007-09-16 18:58:32 +020051 #define PT_LEVEL_BITS PT32_LEVEL_BITS
Avi Kivitycea0f0e2007-01-05 16:36:43 -080052 #define PT_MAX_FULL_LEVELS 2
Marcelo Tosattib3e4e632007-12-07 07:56:58 -050053 #define CMPXCHG cmpxchg
Avi Kivity6aa8b732006-12-10 02:21:36 -080054#else
55 #error Invalid PTTYPE value
56#endif
57
Avi Kivity5fb07dd2007-11-21 12:35:07 +020058#define gpte_to_gfn FNAME(gpte_to_gfn)
59#define gpte_to_gfn_pde FNAME(gpte_to_gfn_pde)
60
Avi Kivity6aa8b732006-12-10 02:21:36 -080061/*
62 * The guest_walker structure emulates the behavior of the hardware page
63 * table walker.
64 */
65struct guest_walker {
66 int level;
Avi Kivitycea0f0e2007-01-05 16:36:43 -080067 gfn_t table_gfn[PT_MAX_FULL_LEVELS];
Shaohua Life5518812007-07-23 14:51:39 +080068 pt_element_t pte;
Avi Kivityfe135d22007-12-09 16:15:46 +020069 unsigned pt_access;
70 unsigned pte_access;
Avi Kivity815af8d2007-01-05 16:36:44 -080071 gfn_t gfn;
Avi Kivity7993ba42007-01-26 00:56:41 -080072 u32 error_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -080073};
74
Avi Kivity5fb07dd2007-11-21 12:35:07 +020075static gfn_t gpte_to_gfn(pt_element_t gpte)
76{
77 return (gpte & PT_BASE_ADDR_MASK) >> PAGE_SHIFT;
78}
79
80static gfn_t gpte_to_gfn_pde(pt_element_t gpte)
81{
82 return (gpte & PT_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
83}
84
Marcelo Tosattib3e4e632007-12-07 07:56:58 -050085static 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
Avi Kivitybedbe4e2007-12-09 16:52:56 +0200105static unsigned FNAME(gpte_access)(struct kvm_vcpu *vcpu, pt_element_t gpte)
106{
107 unsigned access;
108
109 access = (gpte & (PT_WRITABLE_MASK | PT_USER_MASK)) | ACC_EXEC_MASK;
110#if PTTYPE == 64
111 if (is_nx(vcpu))
112 access &= ~(gpte >> PT64_NX_SHIFT);
113#endif
114 return access;
115}
116
Avi Kivityac79c972007-01-05 16:36:40 -0800117/*
118 * Fetch a guest pte for a guest virtual address
119 */
Avi Kivity7993ba42007-01-26 00:56:41 -0800120static int FNAME(walk_addr)(struct guest_walker *walker,
121 struct kvm_vcpu *vcpu, gva_t addr,
Avi Kivity73b10872007-01-26 00:56:41 -0800122 int write_fault, int user_fault, int fetch_fault)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800123{
Avi Kivity42bf3f02007-10-17 12:18:47 +0200124 pt_element_t pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800125 gfn_t table_gfn;
Avi Kivityfe135d22007-12-09 16:15:46 +0200126 unsigned index, pt_access, pte_access;
Avi Kivity42bf3f02007-10-17 12:18:47 +0200127 gpa_t pte_gpa;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800128
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800129 pgprintk("%s: addr %lx\n", __FUNCTION__, addr);
Marcelo Tosattib3e4e632007-12-07 07:56:58 -0500130walk:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800131 walker->level = vcpu->mmu.root_level;
Avi Kivity42bf3f02007-10-17 12:18:47 +0200132 pte = vcpu->cr3;
Avi Kivity1b0973b2007-01-05 16:36:41 -0800133#if PTTYPE == 64
134 if (!is_long_mode(vcpu)) {
Avi Kivity42bf3f02007-10-17 12:18:47 +0200135 pte = vcpu->pdptrs[(addr >> 30) & 3];
136 if (!is_present_pte(pte))
Avi Kivity7993ba42007-01-26 00:56:41 -0800137 goto not_present;
Avi Kivity1b0973b2007-01-05 16:36:41 -0800138 --walker->level;
139 }
140#endif
Avi Kivitya9058ec2006-12-29 16:49:37 -0800141 ASSERT((!is_long_mode(vcpu) && is_pae(vcpu)) ||
Rusty Russellf802a302007-07-17 23:32:55 +1000142 (vcpu->cr3 & CR3_NONPAE_RESERVED_BITS) == 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800143
Avi Kivityfe135d22007-12-09 16:15:46 +0200144 pt_access = ACC_ALL;
Avi Kivityac79c972007-01-05 16:36:40 -0800145
146 for (;;) {
Avi Kivity42bf3f02007-10-17 12:18:47 +0200147 index = PT_INDEX(addr, walker->level);
Avi Kivityac79c972007-01-05 16:36:40 -0800148
Avi Kivity5fb07dd2007-11-21 12:35:07 +0200149 table_gfn = gpte_to_gfn(pte);
Avi Kivity1755fbc2007-11-21 14:44:45 +0200150 pte_gpa = gfn_to_gpa(table_gfn);
Izik Eidusec8d4ea2007-11-19 11:28:19 +0200151 pte_gpa += index * sizeof(pt_element_t);
Avi Kivity42bf3f02007-10-17 12:18:47 +0200152 walker->table_gfn[walker->level - 1] = table_gfn;
153 pgprintk("%s: table_gfn[%d] %lx\n", __FUNCTION__,
154 walker->level - 1, table_gfn);
Avi Kivityac79c972007-01-05 16:36:40 -0800155
Izik Eidusec8d4ea2007-11-19 11:28:19 +0200156 kvm_read_guest(vcpu->kvm, pte_gpa, &pte, sizeof(pte));
Avi Kivity42bf3f02007-10-17 12:18:47 +0200157
158 if (!is_present_pte(pte))
Avi Kivity7993ba42007-01-26 00:56:41 -0800159 goto not_present;
160
Avi Kivity42bf3f02007-10-17 12:18:47 +0200161 if (write_fault && !is_writeble_pte(pte))
Avi Kivity7993ba42007-01-26 00:56:41 -0800162 if (user_fault || is_write_protection(vcpu))
163 goto access_error;
164
Avi Kivity42bf3f02007-10-17 12:18:47 +0200165 if (user_fault && !(pte & PT_USER_MASK))
Avi Kivity7993ba42007-01-26 00:56:41 -0800166 goto access_error;
167
Avi Kivity73b10872007-01-26 00:56:41 -0800168#if PTTYPE == 64
Avi Kivity42bf3f02007-10-17 12:18:47 +0200169 if (fetch_fault && is_nx(vcpu) && (pte & PT64_NX_MASK))
Avi Kivity73b10872007-01-26 00:56:41 -0800170 goto access_error;
171#endif
172
Avi Kivity42bf3f02007-10-17 12:18:47 +0200173 if (!(pte & PT_ACCESSED_MASK)) {
Avi Kivitybf3f8e82007-02-19 14:37:46 +0200174 mark_page_dirty(vcpu->kvm, table_gfn);
Marcelo Tosattib3e4e632007-12-07 07:56:58 -0500175 if (FNAME(cmpxchg_gpte)(vcpu->kvm, table_gfn,
176 index, pte, pte|PT_ACCESSED_MASK))
177 goto walk;
Avi Kivity42bf3f02007-10-17 12:18:47 +0200178 pte |= PT_ACCESSED_MASK;
Avi Kivitybf3f8e82007-02-19 14:37:46 +0200179 }
Avi Kivityac79c972007-01-05 16:36:40 -0800180
Avi Kivitybedbe4e2007-12-09 16:52:56 +0200181 pte_access = pt_access & FNAME(gpte_access)(vcpu, pte);
Avi Kivityfe135d22007-12-09 16:15:46 +0200182
Avi Kivity815af8d2007-01-05 16:36:44 -0800183 if (walker->level == PT_PAGE_TABLE_LEVEL) {
Avi Kivity5fb07dd2007-11-21 12:35:07 +0200184 walker->gfn = gpte_to_gfn(pte);
Avi Kivity815af8d2007-01-05 16:36:44 -0800185 break;
186 }
187
188 if (walker->level == PT_DIRECTORY_LEVEL
Avi Kivity42bf3f02007-10-17 12:18:47 +0200189 && (pte & PT_PAGE_SIZE_MASK)
Avi Kivity815af8d2007-01-05 16:36:44 -0800190 && (PTTYPE == 64 || is_pse(vcpu))) {
Avi Kivity5fb07dd2007-11-21 12:35:07 +0200191 walker->gfn = gpte_to_gfn_pde(pte);
Avi Kivity815af8d2007-01-05 16:36:44 -0800192 walker->gfn += PT_INDEX(addr, PT_PAGE_TABLE_LEVEL);
Avi Kivityda9285212007-11-21 13:54:47 +0200193 if (PTTYPE == 32 && is_cpuid_PSE36())
194 walker->gfn += pse36_gfn_delta(pte);
Avi Kivity815af8d2007-01-05 16:36:44 -0800195 break;
196 }
197
Avi Kivityfe135d22007-12-09 16:15:46 +0200198 pt_access = pte_access;
Avi Kivityac79c972007-01-05 16:36:40 -0800199 --walker->level;
200 }
Avi Kivity42bf3f02007-10-17 12:18:47 +0200201
202 if (write_fault && !is_dirty_pte(pte)) {
Marcelo Tosattib3e4e632007-12-07 07:56:58 -0500203 bool ret;
204
Avi Kivity42bf3f02007-10-17 12:18:47 +0200205 mark_page_dirty(vcpu->kvm, table_gfn);
Marcelo Tosattib3e4e632007-12-07 07:56:58 -0500206 ret = FNAME(cmpxchg_gpte)(vcpu->kvm, table_gfn, index, pte,
207 pte|PT_DIRTY_MASK);
208 if (ret)
209 goto walk;
Avi Kivity42bf3f02007-10-17 12:18:47 +0200210 pte |= PT_DIRTY_MASK;
Avi Kivity42bf3f02007-10-17 12:18:47 +0200211 kvm_mmu_pte_write(vcpu, pte_gpa, (u8 *)&pte, sizeof(pte));
212 }
213
214 walker->pte = pte;
Avi Kivityfe135d22007-12-09 16:15:46 +0200215 walker->pt_access = pt_access;
216 walker->pte_access = pte_access;
217 pgprintk("%s: pte %llx pte_access %x pt_access %x\n",
218 __FUNCTION__, (u64)pte, pt_access, pte_access);
Avi Kivity7993ba42007-01-26 00:56:41 -0800219 return 1;
220
221not_present:
222 walker->error_code = 0;
223 goto err;
224
225access_error:
226 walker->error_code = PFERR_PRESENT_MASK;
227
228err:
229 if (write_fault)
230 walker->error_code |= PFERR_WRITE_MASK;
231 if (user_fault)
232 walker->error_code |= PFERR_USER_MASK;
Avi Kivity73b10872007-01-26 00:56:41 -0800233 if (fetch_fault)
234 walker->error_code |= PFERR_FETCH_MASK;
Shaohua Life5518812007-07-23 14:51:39 +0800235 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800236}
237
Avi Kivity230c9a82007-11-21 14:16:30 +0200238static void FNAME(set_pte)(struct kvm_vcpu *vcpu, pt_element_t gpte,
Avi Kivityfe135d22007-12-09 16:15:46 +0200239 u64 *shadow_pte, unsigned pt_access,
240 unsigned pte_access,
Avi Kivity230c9a82007-11-21 14:16:30 +0200241 int user_fault, int write_fault,
242 int *ptwrite, struct guest_walker *walker,
243 gfn_t gfn)
Avi Kivitye60d75e2007-05-30 19:31:17 +0300244{
Shaohua Life5518812007-07-23 14:51:39 +0800245 int dirty = gpte & PT_DIRTY_MASK;
Avi Kivityc7addb92007-09-16 18:58:32 +0200246 u64 spte;
247 int was_rmapped = is_rmap_pte(*shadow_pte);
Izik Eidusb238f7b2007-11-20 12:02:12 +0200248 struct page *page;
Avi Kivity97a0a012007-05-31 15:08:29 +0300249
Avi Kivityfe135d22007-12-09 16:15:46 +0200250 pgprintk("%s: spte %llx gpte %llx access %x write_fault %d"
Avi Kivity97a0a012007-05-31 15:08:29 +0300251 " user_fault %d gfn %lx\n",
Avi Kivityfe135d22007-12-09 16:15:46 +0200252 __FUNCTION__, *shadow_pte, (u64)gpte, pt_access,
Avi Kivity97a0a012007-05-31 15:08:29 +0300253 write_fault, user_fault, gfn);
254
Avi Kivity12b7d282007-09-23 14:10:49 +0200255 /*
256 * We don't set the accessed bit, since we sometimes want to see
257 * whether the guest actually used the pte (in order to detect
258 * demand paging).
259 */
260 spte = PT_PRESENT_MASK | PT_DIRTY_MASK;
Shaohua Life5518812007-07-23 14:51:39 +0800261 spte |= gpte & PT64_NX_MASK;
Avi Kivitye60d75e2007-05-30 19:31:17 +0300262 if (!dirty)
Avi Kivityfe135d22007-12-09 16:15:46 +0200263 pte_access &= ~ACC_WRITE_MASK;
Avi Kivity8d87a032007-12-09 16:37:36 +0200264 if (!(pte_access & ACC_EXEC_MASK))
265 spte |= PT64_NX_MASK;
Avi Kivitye60d75e2007-05-30 19:31:17 +0300266
Avi Kivity4e542372007-11-21 14:08:40 +0200267 page = gfn_to_page(vcpu->kvm, gfn);
Izik Eidusb238f7b2007-11-20 12:02:12 +0200268
Avi Kivity0d551bb2007-05-31 15:23:35 +0300269 spte |= PT_PRESENT_MASK;
Avi Kivityfe135d22007-12-09 16:15:46 +0200270 if (pte_access & ACC_USER_MASK)
Avi Kivity0d551bb2007-05-31 15:23:35 +0300271 spte |= PT_USER_MASK;
Avi Kivitye60d75e2007-05-30 19:31:17 +0300272
Avi Kivity4e542372007-11-21 14:08:40 +0200273 if (is_error_page(page)) {
Avi Kivityc7addb92007-09-16 18:58:32 +0200274 set_shadow_pte(shadow_pte,
275 shadow_trap_nonpresent_pte | PT_SHADOW_IO_MARK);
Izik Eidusb238f7b2007-11-20 12:02:12 +0200276 kvm_release_page_clean(page);
Avi Kivitye60d75e2007-05-30 19:31:17 +0300277 return;
278 }
279
Avi Kivity4e542372007-11-21 14:08:40 +0200280 spte |= page_to_phys(page);
Avi Kivitye60d75e2007-05-30 19:31:17 +0300281
Avi Kivityfe135d22007-12-09 16:15:46 +0200282 if ((pte_access & ACC_WRITE_MASK)
Avi Kivity97a0a012007-05-31 15:08:29 +0300283 || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
Avi Kivitye60d75e2007-05-30 19:31:17 +0300284 struct kvm_mmu_page *shadow;
285
Avi Kivity0d551bb2007-05-31 15:23:35 +0300286 spte |= PT_WRITABLE_MASK;
Avi Kivity97a0a012007-05-31 15:08:29 +0300287 if (user_fault) {
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500288 mmu_unshadow(vcpu->kvm, gfn);
Avi Kivity97a0a012007-05-31 15:08:29 +0300289 goto unshadowed;
290 }
291
Anthony Liguorif67a46f2007-10-10 19:25:50 -0500292 shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
Avi Kivitye60d75e2007-05-30 19:31:17 +0300293 if (shadow) {
294 pgprintk("%s: found shadow page for %lx, marking ro\n",
295 __FUNCTION__, gfn);
Avi Kivityfe135d22007-12-09 16:15:46 +0200296 pte_access &= ~ACC_WRITE_MASK;
Avi Kivity0d551bb2007-05-31 15:23:35 +0300297 if (is_writeble_pte(spte)) {
298 spte &= ~PT_WRITABLE_MASK;
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +0300299 kvm_x86_ops->tlb_flush(vcpu);
Avi Kivitye60d75e2007-05-30 19:31:17 +0300300 }
Avi Kivity97a0a012007-05-31 15:08:29 +0300301 if (write_fault)
302 *ptwrite = 1;
Avi Kivitye60d75e2007-05-30 19:31:17 +0300303 }
304 }
305
Avi Kivity97a0a012007-05-31 15:08:29 +0300306unshadowed:
307
Avi Kivityfe135d22007-12-09 16:15:46 +0200308 if (pte_access & ACC_WRITE_MASK)
Avi Kivity4e542372007-11-21 14:08:40 +0200309 mark_page_dirty(vcpu->kvm, gfn);
Avi Kivitye60d75e2007-05-30 19:31:17 +0300310
Avi Kivityc7addb92007-09-16 18:58:32 +0200311 pgprintk("%s: setting spte %llx\n", __FUNCTION__, spte);
Avi Kivitye663ee62007-05-31 15:46:04 +0300312 set_shadow_pte(shadow_pte, spte);
Avi Kivity38c335f2007-11-21 14:20:22 +0200313 page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200314 if (!was_rmapped) {
Avi Kivity4e542372007-11-21 14:08:40 +0200315 rmap_add(vcpu, shadow_pte, gfn);
Izik Eidusb238f7b2007-11-20 12:02:12 +0200316 if (!is_rmap_pte(*shadow_pte))
Izik Eidusb4231d62007-11-20 11:49:33 +0200317 kvm_release_page_clean(page);
Izik Eidus8a7ae052007-10-18 11:09:33 +0200318 }
319 else
Izik Eidusb238f7b2007-11-20 12:02:12 +0200320 kvm_release_page_clean(page);
Avi Kivity12b7d282007-09-23 14:10:49 +0200321 if (!ptwrite || !*ptwrite)
322 vcpu->last_pte_updated = shadow_pte;
Avi Kivitye60d75e2007-05-30 19:31:17 +0300323}
324
Avi Kivity00284252007-05-01 16:53:31 +0300325static void FNAME(update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *page,
Avi Kivityc7addb92007-09-16 18:58:32 +0200326 u64 *spte, const void *pte, int bytes,
327 int offset_in_pte)
Avi Kivity00284252007-05-01 16:53:31 +0300328{
329 pt_element_t gpte;
Avi Kivity41074d02007-12-09 17:00:02 +0200330 unsigned pte_access;
Avi Kivity00284252007-05-01 16:53:31 +0300331
Avi Kivity00284252007-05-01 16:53:31 +0300332 gpte = *(const pt_element_t *)pte;
Avi Kivityc7addb92007-09-16 18:58:32 +0200333 if (~gpte & (PT_PRESENT_MASK | PT_ACCESSED_MASK)) {
334 if (!offset_in_pte && !is_present_pte(gpte))
335 set_shadow_pte(spte, shadow_notrap_nonpresent_pte);
336 return;
337 }
338 if (bytes < sizeof(pt_element_t))
Avi Kivity00284252007-05-01 16:53:31 +0300339 return;
340 pgprintk("%s: gpte %llx spte %p\n", __FUNCTION__, (u64)gpte, spte);
Avi Kivity41074d02007-12-09 17:00:02 +0200341 pte_access = page->role.access & FNAME(gpte_access)(vcpu, gpte);
342 FNAME(set_pte)(vcpu, gpte, spte, page->role.access, pte_access,
Avi Kivityfe135d22007-12-09 16:15:46 +0200343 0, 0, NULL, NULL, gpte_to_gfn(gpte));
Avi Kivity00284252007-05-01 16:53:31 +0300344}
345
Avi Kivity6aa8b732006-12-10 02:21:36 -0800346/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800347 * Fetch a shadow pte for a specific level in the paging hierarchy.
348 */
349static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
Avi Kivity97a0a012007-05-31 15:08:29 +0300350 struct guest_walker *walker,
351 int user_fault, int write_fault, int *ptwrite)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800352{
353 hpa_t shadow_addr;
354 int level;
Avi Kivityef0197e2007-05-30 14:21:51 +0300355 u64 *shadow_ent;
Avi Kivityfe135d22007-12-09 16:15:46 +0200356 unsigned access = walker->pt_access;
Avi Kivityac79c972007-01-05 16:36:40 -0800357
Shaohua Life5518812007-07-23 14:51:39 +0800358 if (!is_present_pte(walker->pte))
Avi Kivityac79c972007-01-05 16:36:40 -0800359 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800360
361 shadow_addr = vcpu->mmu.root_hpa;
362 level = vcpu->mmu.shadow_root_level;
Avi Kivityaef3d3f2007-01-05 16:36:41 -0800363 if (level == PT32E_ROOT_LEVEL) {
364 shadow_addr = vcpu->mmu.pae_root[(addr >> 30) & 3];
365 shadow_addr &= PT64_BASE_ADDR_MASK;
366 --level;
367 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800368
369 for (; ; level--) {
370 u32 index = SHADOW_PT_INDEX(addr, level);
Avi Kivity25c0de22007-01-05 16:36:42 -0800371 struct kvm_mmu_page *shadow_page;
Avi Kivity8c7bb722006-12-13 00:34:02 -0800372 u64 shadow_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800373 int metaphysical;
374 gfn_t table_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800375
Avi Kivityef0197e2007-05-30 14:21:51 +0300376 shadow_ent = ((u64 *)__va(shadow_addr)) + index;
Avi Kivityc7addb92007-09-16 18:58:32 +0200377 if (is_shadow_present_pte(*shadow_ent)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800378 if (level == PT_PAGE_TABLE_LEVEL)
Avi Kivity97a0a012007-05-31 15:08:29 +0300379 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800380 shadow_addr = *shadow_ent & PT64_BASE_ADDR_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800381 continue;
382 }
383
Avi Kivityef0197e2007-05-30 14:21:51 +0300384 if (level == PT_PAGE_TABLE_LEVEL)
385 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800386
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800387 if (level - 1 == PT_PAGE_TABLE_LEVEL
388 && walker->level == PT_DIRECTORY_LEVEL) {
389 metaphysical = 1;
Avi Kivitycc70e732007-10-11 15:12:24 +0200390 if (!is_dirty_pte(walker->pte))
Avi Kivityfe135d22007-12-09 16:15:46 +0200391 access &= ~ACC_WRITE_MASK;
Avi Kivity5fb07dd2007-11-21 12:35:07 +0200392 table_gfn = gpte_to_gfn(walker->pte);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800393 } else {
394 metaphysical = 0;
395 table_gfn = walker->table_gfn[level - 2];
396 }
397 shadow_page = kvm_mmu_get_page(vcpu, table_gfn, addr, level-1,
Avi Kivityfe135d22007-12-09 16:15:46 +0200398 metaphysical, access,
Avi Kivityd28c6cfb2007-03-23 09:55:25 +0200399 shadow_ent);
Avi Kivity47ad8e62007-05-06 15:50:58 +0300400 shadow_addr = __pa(shadow_page->spt);
Avi Kivityaef3d3f2007-01-05 16:36:41 -0800401 shadow_pte = shadow_addr | PT_PRESENT_MASK | PT_ACCESSED_MASK
402 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity8c7bb722006-12-13 00:34:02 -0800403 *shadow_ent = shadow_pte;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800404 }
Avi Kivityef0197e2007-05-30 14:21:51 +0300405
Avi Kivity050e6492007-11-21 14:11:49 +0200406 FNAME(set_pte)(vcpu, walker->pte, shadow_ent,
Avi Kivityfe135d22007-12-09 16:15:46 +0200407 access, walker->pte_access & access,
408 user_fault, write_fault,
Avi Kivity050e6492007-11-21 14:11:49 +0200409 ptwrite, walker, walker->gfn);
410
Avi Kivityef0197e2007-05-30 14:21:51 +0300411 return shadow_ent;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800412}
413
414/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800415 * Page fault handler. There are several causes for a page fault:
416 * - there is no shadow pte for the guest pte
417 * - write access through a shadow pte marked read only so that we can set
418 * the dirty bit
419 * - write access to a shadow pte marked read only so we can update the page
420 * dirty bitmap, when userspace requests it
421 * - mmio access; in this case we will never install a present shadow pte
422 * - normal guest page fault due to the guest pte marked not present, not
423 * writable, or not executable
424 *
Avi Kivitye2dec932007-01-05 16:36:54 -0800425 * Returns: 1 if we need to emulate the instruction, 0 otherwise, or
426 * a negative value on error.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800427 */
428static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr,
429 u32 error_code)
430{
431 int write_fault = error_code & PFERR_WRITE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800432 int user_fault = error_code & PFERR_USER_MASK;
Avi Kivity73b10872007-01-26 00:56:41 -0800433 int fetch_fault = error_code & PFERR_FETCH_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800434 struct guest_walker walker;
435 u64 *shadow_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800436 int write_pt = 0;
Avi Kivitye2dec932007-01-05 16:36:54 -0800437 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800438
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800439 pgprintk("%s: addr %lx err %x\n", __FUNCTION__, addr, error_code);
Avi Kivity37a7d8b2007-01-05 16:36:56 -0800440 kvm_mmu_audit(vcpu, "pre page fault");
Avi Kivity714b93d2007-01-05 16:36:53 -0800441
Avi Kivitye2dec932007-01-05 16:36:54 -0800442 r = mmu_topup_memory_caches(vcpu);
443 if (r)
444 return r;
Avi Kivity714b93d2007-01-05 16:36:53 -0800445
Avi Kivity6aa8b732006-12-10 02:21:36 -0800446 /*
447 * Look up the shadow pte for the faulting address.
448 */
Avi Kivity73b10872007-01-26 00:56:41 -0800449 r = FNAME(walk_addr)(&walker, vcpu, addr, write_fault, user_fault,
450 fetch_fault);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800451
452 /*
453 * The page is not mapped by the guest. Let the guest handle it.
454 */
Avi Kivity7993ba42007-01-26 00:56:41 -0800455 if (!r) {
456 pgprintk("%s: guest page fault\n", __FUNCTION__);
457 inject_page_fault(vcpu, addr, walker.error_code);
Avi Kivitya25f7e12007-04-30 17:05:38 +0300458 vcpu->last_pt_write_count = 0; /* reset fork detector */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800459 return 0;
460 }
461
Avi Kivity97a0a012007-05-31 15:08:29 +0300462 shadow_pte = FNAME(fetch)(vcpu, addr, &walker, user_fault, write_fault,
463 &write_pt);
464 pgprintk("%s: shadow pte %p %llx ptwrite %d\n", __FUNCTION__,
465 shadow_pte, *shadow_pte, write_pt);
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800466
Avi Kivitya25f7e12007-04-30 17:05:38 +0300467 if (!write_pt)
468 vcpu->last_pt_write_count = 0; /* reset fork detector */
469
Avi Kivity6aa8b732006-12-10 02:21:36 -0800470 /*
471 * mmio: emulate if accessible, otherwise its a guest fault.
472 */
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200473 if (is_io_pte(*shadow_pte))
Avi Kivity7993ba42007-01-26 00:56:41 -0800474 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800475
Avi Kivity1165f5f2007-04-19 17:27:43 +0300476 ++vcpu->stat.pf_fixed;
Avi Kivity37a7d8b2007-01-05 16:36:56 -0800477 kvm_mmu_audit(vcpu, "post page fault (fixed)");
Avi Kivity6aa8b732006-12-10 02:21:36 -0800478
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800479 return write_pt;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800480}
481
482static gpa_t FNAME(gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t vaddr)
483{
484 struct guest_walker walker;
Avi Kivitye119d112007-02-12 00:54:36 -0800485 gpa_t gpa = UNMAPPED_GVA;
486 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800487
Avi Kivitye119d112007-02-12 00:54:36 -0800488 r = FNAME(walk_addr)(&walker, vcpu, vaddr, 0, 0, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800489
Avi Kivitye119d112007-02-12 00:54:36 -0800490 if (r) {
Avi Kivity1755fbc2007-11-21 14:44:45 +0200491 gpa = gfn_to_gpa(walker.gfn);
Avi Kivitye119d112007-02-12 00:54:36 -0800492 gpa |= vaddr & ~PAGE_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800493 }
494
495 return gpa;
496}
497
Avi Kivityc7addb92007-09-16 18:58:32 +0200498static void FNAME(prefetch_page)(struct kvm_vcpu *vcpu,
499 struct kvm_mmu_page *sp)
500{
Avi Kivitye5a4c8c2007-11-20 21:39:54 +0200501 int i, offset = 0;
Avi Kivityc7addb92007-09-16 18:58:32 +0200502 pt_element_t *gpt;
Izik Eidus8a7ae052007-10-18 11:09:33 +0200503 struct page *page;
Avi Kivityc7addb92007-09-16 18:58:32 +0200504
Avi Kivitye5a4c8c2007-11-20 21:39:54 +0200505 if (sp->role.metaphysical
506 || (PTTYPE == 32 && sp->role.level > PT_PAGE_TABLE_LEVEL)) {
Avi Kivityc7addb92007-09-16 18:58:32 +0200507 nonpaging_prefetch_page(vcpu, sp);
508 return;
509 }
510
Avi Kivitye5a4c8c2007-11-20 21:39:54 +0200511 if (PTTYPE == 32)
512 offset = sp->role.quadrant << PT64_LEVEL_BITS;
Izik Eidus8a7ae052007-10-18 11:09:33 +0200513 page = gfn_to_page(vcpu->kvm, sp->gfn);
514 gpt = kmap_atomic(page, KM_USER0);
Avi Kivityc7addb92007-09-16 18:58:32 +0200515 for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
Avi Kivitye5a4c8c2007-11-20 21:39:54 +0200516 if (is_present_pte(gpt[offset + i]))
Avi Kivityc7addb92007-09-16 18:58:32 +0200517 sp->spt[i] = shadow_trap_nonpresent_pte;
518 else
519 sp->spt[i] = shadow_notrap_nonpresent_pte;
520 kunmap_atomic(gpt, KM_USER0);
Izik Eidusb4231d62007-11-20 11:49:33 +0200521 kvm_release_page_clean(page);
Avi Kivityc7addb92007-09-16 18:58:32 +0200522}
523
Avi Kivity6aa8b732006-12-10 02:21:36 -0800524#undef pt_element_t
525#undef guest_walker
526#undef FNAME
527#undef PT_BASE_ADDR_MASK
528#undef PT_INDEX
529#undef SHADOW_PT_INDEX
530#undef PT_LEVEL_MASK
Avi Kivity6aa8b732006-12-10 02:21:36 -0800531#undef PT_DIR_BASE_ADDR_MASK
Avi Kivityc7addb92007-09-16 18:58:32 +0200532#undef PT_LEVEL_BITS
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800533#undef PT_MAX_FULL_LEVELS
Avi Kivity5fb07dd2007-11-21 12:35:07 +0200534#undef gpte_to_gfn
535#undef gpte_to_gfn_pde
Marcelo Tosattib3e4e632007-12-07 07:56:58 -0500536#undef CMPXCHG