blob: f7cce443ca6ff7f7b917eb02564b9fcd1d2eb48c [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)
34 #define PT_PTE_COPY_MASK PT64_PTE_COPY_MASK
Avi Kivitycea0f0e2007-01-05 16:36:43 -080035 #ifdef CONFIG_X86_64
36 #define PT_MAX_FULL_LEVELS 4
37 #else
38 #define PT_MAX_FULL_LEVELS 2
39 #endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080040#elif PTTYPE == 32
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_PTE_COPY_MASK PT32_PTE_COPY_MASK
Avi Kivitycea0f0e2007-01-05 16:36:43 -080050 #define PT_MAX_FULL_LEVELS 2
Avi Kivity6aa8b732006-12-10 02:21:36 -080051#else
52 #error Invalid PTTYPE value
53#endif
54
55/*
56 * The guest_walker structure emulates the behavior of the hardware page
57 * table walker.
58 */
59struct guest_walker {
60 int level;
Avi Kivitycea0f0e2007-01-05 16:36:43 -080061 gfn_t table_gfn[PT_MAX_FULL_LEVELS];
Avi Kivity6aa8b732006-12-10 02:21:36 -080062 pt_element_t *table;
Avi Kivityac79c972007-01-05 16:36:40 -080063 pt_element_t *ptep;
Avi Kivity6aa8b732006-12-10 02:21:36 -080064 pt_element_t inherited_ar;
65};
66
Avi Kivityac79c972007-01-05 16:36:40 -080067/*
68 * Fetch a guest pte for a guest virtual address
69 */
70static void FNAME(walk_addr)(struct guest_walker *walker,
71 struct kvm_vcpu *vcpu, gva_t addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -080072{
73 hpa_t hpa;
74 struct kvm_memory_slot *slot;
Avi Kivityac79c972007-01-05 16:36:40 -080075 pt_element_t *ptep;
Avi Kivity1b0973b2007-01-05 16:36:41 -080076 pt_element_t root;
Avi Kivitycea0f0e2007-01-05 16:36:43 -080077 gfn_t table_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -080078
Avi Kivitycea0f0e2007-01-05 16:36:43 -080079 pgprintk("%s: addr %lx\n", __FUNCTION__, addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -080080 walker->level = vcpu->mmu.root_level;
Avi Kivity1b0973b2007-01-05 16:36:41 -080081 walker->table = NULL;
82 root = vcpu->cr3;
83#if PTTYPE == 64
84 if (!is_long_mode(vcpu)) {
85 walker->ptep = &vcpu->pdptrs[(addr >> 30) & 3];
86 root = *walker->ptep;
87 if (!(root & PT_PRESENT_MASK))
88 return;
89 --walker->level;
90 }
91#endif
Avi Kivitycea0f0e2007-01-05 16:36:43 -080092 table_gfn = (root & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
93 walker->table_gfn[walker->level - 1] = table_gfn;
94 pgprintk("%s: table_gfn[%d] %lx\n", __FUNCTION__,
95 walker->level - 1, table_gfn);
96 slot = gfn_to_memslot(vcpu->kvm, table_gfn);
Avi Kivity1b0973b2007-01-05 16:36:41 -080097 hpa = safe_gpa_to_hpa(vcpu, root & PT64_BASE_ADDR_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -080098 walker->table = kmap_atomic(pfn_to_page(hpa >> PAGE_SHIFT), KM_USER0);
99
Avi Kivitya9058ec2006-12-29 16:49:37 -0800100 ASSERT((!is_long_mode(vcpu) && is_pae(vcpu)) ||
Avi Kivity6aa8b732006-12-10 02:21:36 -0800101 (vcpu->cr3 & ~(PAGE_MASK | CR3_FLAGS_MASK)) == 0);
102
Avi Kivity6aa8b732006-12-10 02:21:36 -0800103 walker->inherited_ar = PT_USER_MASK | PT_WRITABLE_MASK;
Avi Kivityac79c972007-01-05 16:36:40 -0800104
105 for (;;) {
106 int index = PT_INDEX(addr, walker->level);
107 hpa_t paddr;
108
109 ptep = &walker->table[index];
110 ASSERT(((unsigned long)walker->table & PAGE_MASK) ==
111 ((unsigned long)ptep & PAGE_MASK));
112
Avi Kivity1b0973b2007-01-05 16:36:41 -0800113 if (is_present_pte(*ptep) && !(*ptep & PT_ACCESSED_MASK))
114 *ptep |= PT_ACCESSED_MASK;
Avi Kivityac79c972007-01-05 16:36:40 -0800115
116 if (!is_present_pte(*ptep) ||
117 walker->level == PT_PAGE_TABLE_LEVEL ||
118 (walker->level == PT_DIRECTORY_LEVEL &&
119 (*ptep & PT_PAGE_SIZE_MASK) &&
120 (PTTYPE == 64 || is_pse(vcpu))))
121 break;
122
123 if (walker->level != 3 || is_long_mode(vcpu))
124 walker->inherited_ar &= walker->table[index];
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800125 table_gfn = (*ptep & PT_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivityac79c972007-01-05 16:36:40 -0800126 paddr = safe_gpa_to_hpa(vcpu, *ptep & PT_BASE_ADDR_MASK);
127 kunmap_atomic(walker->table, KM_USER0);
128 walker->table = kmap_atomic(pfn_to_page(paddr >> PAGE_SHIFT),
129 KM_USER0);
130 --walker->level;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800131 walker->table_gfn[walker->level - 1 ] = table_gfn;
132 pgprintk("%s: table_gfn[%d] %lx\n", __FUNCTION__,
133 walker->level - 1, table_gfn);
Avi Kivityac79c972007-01-05 16:36:40 -0800134 }
135 walker->ptep = ptep;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800136}
137
138static void FNAME(release_walker)(struct guest_walker *walker)
139{
Avi Kivity1b0973b2007-01-05 16:36:41 -0800140 if (walker->table)
141 kunmap_atomic(walker->table, KM_USER0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800142}
143
144static void FNAME(set_pte)(struct kvm_vcpu *vcpu, u64 guest_pte,
145 u64 *shadow_pte, u64 access_bits)
146{
147 ASSERT(*shadow_pte == 0);
148 access_bits &= guest_pte;
149 *shadow_pte = (guest_pte & PT_PTE_COPY_MASK);
150 set_pte_common(vcpu, shadow_pte, guest_pte & PT_BASE_ADDR_MASK,
151 guest_pte & PT_DIRTY_MASK, access_bits);
152}
153
154static void FNAME(set_pde)(struct kvm_vcpu *vcpu, u64 guest_pde,
155 u64 *shadow_pte, u64 access_bits,
156 int index)
157{
158 gpa_t gaddr;
159
160 ASSERT(*shadow_pte == 0);
161 access_bits &= guest_pde;
162 gaddr = (guest_pde & PT_DIR_BASE_ADDR_MASK) + PAGE_SIZE * index;
163 if (PTTYPE == 32 && is_cpuid_PSE36())
164 gaddr |= (guest_pde & PT32_DIR_PSE36_MASK) <<
165 (32 - PT32_DIR_PSE36_SHIFT);
Avi Kivity8c7bb722006-12-13 00:34:02 -0800166 *shadow_pte = guest_pde & PT_PTE_COPY_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800167 set_pte_common(vcpu, shadow_pte, gaddr,
168 guest_pde & PT_DIRTY_MASK, access_bits);
169}
170
171/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800172 * Fetch a shadow pte for a specific level in the paging hierarchy.
173 */
174static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
175 struct guest_walker *walker)
176{
177 hpa_t shadow_addr;
178 int level;
179 u64 *prev_shadow_ent = NULL;
Avi Kivityac79c972007-01-05 16:36:40 -0800180 pt_element_t *guest_ent = walker->ptep;
181
182 if (!is_present_pte(*guest_ent))
183 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800184
185 shadow_addr = vcpu->mmu.root_hpa;
186 level = vcpu->mmu.shadow_root_level;
Avi Kivityaef3d3f2007-01-05 16:36:41 -0800187 if (level == PT32E_ROOT_LEVEL) {
188 shadow_addr = vcpu->mmu.pae_root[(addr >> 30) & 3];
189 shadow_addr &= PT64_BASE_ADDR_MASK;
190 --level;
191 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800192
193 for (; ; level--) {
194 u32 index = SHADOW_PT_INDEX(addr, level);
195 u64 *shadow_ent = ((u64 *)__va(shadow_addr)) + index;
Avi Kivity25c0de22007-01-05 16:36:42 -0800196 struct kvm_mmu_page *shadow_page;
Avi Kivity8c7bb722006-12-13 00:34:02 -0800197 u64 shadow_pte;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800198 int metaphysical;
199 gfn_t table_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800200
201 if (is_present_pte(*shadow_ent) || is_io_pte(*shadow_ent)) {
202 if (level == PT_PAGE_TABLE_LEVEL)
203 return shadow_ent;
204 shadow_addr = *shadow_ent & PT64_BASE_ADDR_MASK;
205 prev_shadow_ent = shadow_ent;
206 continue;
207 }
208
Avi Kivity6aa8b732006-12-10 02:21:36 -0800209 if (level == PT_PAGE_TABLE_LEVEL) {
210
211 if (walker->level == PT_DIRECTORY_LEVEL) {
212 if (prev_shadow_ent)
213 *prev_shadow_ent |= PT_SHADOW_PS_MARK;
214 FNAME(set_pde)(vcpu, *guest_ent, shadow_ent,
215 walker->inherited_ar,
216 PT_INDEX(addr, PT_PAGE_TABLE_LEVEL));
217 } else {
218 ASSERT(walker->level == PT_PAGE_TABLE_LEVEL);
219 FNAME(set_pte)(vcpu, *guest_ent, shadow_ent, walker->inherited_ar);
220 }
221 return shadow_ent;
222 }
223
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800224 if (level - 1 == PT_PAGE_TABLE_LEVEL
225 && walker->level == PT_DIRECTORY_LEVEL) {
226 metaphysical = 1;
227 table_gfn = (*guest_ent & PT_BASE_ADDR_MASK)
228 >> PAGE_SHIFT;
229 } else {
230 metaphysical = 0;
231 table_gfn = walker->table_gfn[level - 2];
232 }
233 shadow_page = kvm_mmu_get_page(vcpu, table_gfn, addr, level-1,
234 metaphysical, shadow_ent);
Avi Kivity25c0de22007-01-05 16:36:42 -0800235 if (!shadow_page)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800236 return ERR_PTR(-ENOMEM);
Avi Kivity25c0de22007-01-05 16:36:42 -0800237 shadow_addr = shadow_page->page_hpa;
Avi Kivityaef3d3f2007-01-05 16:36:41 -0800238 shadow_pte = shadow_addr | PT_PRESENT_MASK | PT_ACCESSED_MASK
239 | PT_WRITABLE_MASK | PT_USER_MASK;
Avi Kivity8c7bb722006-12-13 00:34:02 -0800240 *shadow_ent = shadow_pte;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800241 prev_shadow_ent = shadow_ent;
242 }
243}
244
245/*
246 * The guest faulted for write. We need to
247 *
248 * - check write permissions
249 * - update the guest pte dirty bit
250 * - update our own dirty page tracking structures
251 */
252static int FNAME(fix_write_pf)(struct kvm_vcpu *vcpu,
253 u64 *shadow_ent,
254 struct guest_walker *walker,
255 gva_t addr,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800256 int user,
257 int *write_pt)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800258{
259 pt_element_t *guest_ent;
260 int writable_shadow;
261 gfn_t gfn;
262
263 if (is_writeble_pte(*shadow_ent))
264 return 0;
265
266 writable_shadow = *shadow_ent & PT_SHADOW_WRITABLE_MASK;
267 if (user) {
268 /*
269 * User mode access. Fail if it's a kernel page or a read-only
270 * page.
271 */
272 if (!(*shadow_ent & PT_SHADOW_USER_MASK) || !writable_shadow)
273 return 0;
274 ASSERT(*shadow_ent & PT_USER_MASK);
275 } else
276 /*
277 * Kernel mode access. Fail if it's a read-only page and
278 * supervisor write protection is enabled.
279 */
280 if (!writable_shadow) {
281 if (is_write_protection(vcpu))
282 return 0;
283 *shadow_ent &= ~PT_USER_MASK;
284 }
285
Avi Kivityac79c972007-01-05 16:36:40 -0800286 guest_ent = walker->ptep;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800287
288 if (!is_present_pte(*guest_ent)) {
289 *shadow_ent = 0;
290 return 0;
291 }
292
293 gfn = (*guest_ent & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800294 if (kvm_mmu_lookup_page(vcpu, gfn)) {
295 pgprintk("%s: found shadow page for %lx, marking ro\n",
296 __FUNCTION__, gfn);
297 *write_pt = 1;
298 return 0;
299 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800300 mark_page_dirty(vcpu->kvm, gfn);
301 *shadow_ent |= PT_WRITABLE_MASK;
302 *guest_ent |= PT_DIRTY_MASK;
Avi Kivitycd4a4e52007-01-05 16:36:38 -0800303 rmap_add(vcpu->kvm, shadow_ent);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800304
305 return 1;
306}
307
308/*
309 * Page fault handler. There are several causes for a page fault:
310 * - there is no shadow pte for the guest pte
311 * - write access through a shadow pte marked read only so that we can set
312 * the dirty bit
313 * - write access to a shadow pte marked read only so we can update the page
314 * dirty bitmap, when userspace requests it
315 * - mmio access; in this case we will never install a present shadow pte
316 * - normal guest page fault due to the guest pte marked not present, not
317 * writable, or not executable
318 *
319 * Returns: 1 if we need to emulate the instruction, 0 otherwise
320 */
321static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr,
322 u32 error_code)
323{
324 int write_fault = error_code & PFERR_WRITE_MASK;
325 int pte_present = error_code & PFERR_PRESENT_MASK;
326 int user_fault = error_code & PFERR_USER_MASK;
327 struct guest_walker walker;
328 u64 *shadow_pte;
329 int fixed;
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800330 int write_pt = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800331
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800332 pgprintk("%s: addr %lx err %x\n", __FUNCTION__, addr, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800333 /*
334 * Look up the shadow pte for the faulting address.
335 */
336 for (;;) {
Avi Kivityac79c972007-01-05 16:36:40 -0800337 FNAME(walk_addr)(&walker, vcpu, addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800338 shadow_pte = FNAME(fetch)(vcpu, addr, &walker);
339 if (IS_ERR(shadow_pte)) { /* must be -ENOMEM */
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800340 printk("%s: oom\n", __FUNCTION__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800341 nonpaging_flush(vcpu);
342 FNAME(release_walker)(&walker);
343 continue;
344 }
345 break;
346 }
347
348 /*
349 * The page is not mapped by the guest. Let the guest handle it.
350 */
351 if (!shadow_pte) {
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800352 pgprintk("%s: not mapped\n", __FUNCTION__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800353 inject_page_fault(vcpu, addr, error_code);
354 FNAME(release_walker)(&walker);
355 return 0;
356 }
357
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800358 pgprintk("%s: shadow pte %p %llx\n", __FUNCTION__,
359 shadow_pte, *shadow_pte);
360
Avi Kivity6aa8b732006-12-10 02:21:36 -0800361 /*
362 * Update the shadow pte.
363 */
364 if (write_fault)
365 fixed = FNAME(fix_write_pf)(vcpu, shadow_pte, &walker, addr,
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800366 user_fault, &write_pt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800367 else
368 fixed = fix_read_pf(shadow_pte);
369
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800370 pgprintk("%s: updated shadow pte %p %llx\n", __FUNCTION__,
371 shadow_pte, *shadow_pte);
372
Avi Kivity6aa8b732006-12-10 02:21:36 -0800373 FNAME(release_walker)(&walker);
374
375 /*
376 * mmio: emulate if accessible, otherwise its a guest fault.
377 */
378 if (is_io_pte(*shadow_pte)) {
379 if (may_access(*shadow_pte, write_fault, user_fault))
380 return 1;
381 pgprintk("%s: io work, no access\n", __FUNCTION__);
382 inject_page_fault(vcpu, addr,
383 error_code | PFERR_PRESENT_MASK);
384 return 0;
385 }
386
387 /*
388 * pte not present, guest page fault.
389 */
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800390 if (pte_present && !fixed && !write_pt) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800391 inject_page_fault(vcpu, addr, error_code);
392 return 0;
393 }
394
395 ++kvm_stat.pf_fixed;
396
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800397 return write_pt;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800398}
399
400static gpa_t FNAME(gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t vaddr)
401{
402 struct guest_walker walker;
403 pt_element_t guest_pte;
404 gpa_t gpa;
405
Avi Kivityac79c972007-01-05 16:36:40 -0800406 FNAME(walk_addr)(&walker, vcpu, vaddr);
407 guest_pte = *walker.ptep;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800408 FNAME(release_walker)(&walker);
409
410 if (!is_present_pte(guest_pte))
411 return UNMAPPED_GVA;
412
413 if (walker.level == PT_DIRECTORY_LEVEL) {
414 ASSERT((guest_pte & PT_PAGE_SIZE_MASK));
415 ASSERT(PTTYPE == 64 || is_pse(vcpu));
416
417 gpa = (guest_pte & PT_DIR_BASE_ADDR_MASK) | (vaddr &
418 (PT_LEVEL_MASK(PT_PAGE_TABLE_LEVEL) | ~PAGE_MASK));
419
420 if (PTTYPE == 32 && is_cpuid_PSE36())
421 gpa |= (guest_pte & PT32_DIR_PSE36_MASK) <<
422 (32 - PT32_DIR_PSE36_SHIFT);
423 } else {
424 gpa = (guest_pte & PT_BASE_ADDR_MASK);
425 gpa |= (vaddr & ~PAGE_MASK);
426 }
427
428 return gpa;
429}
430
431#undef pt_element_t
432#undef guest_walker
433#undef FNAME
434#undef PT_BASE_ADDR_MASK
435#undef PT_INDEX
436#undef SHADOW_PT_INDEX
437#undef PT_LEVEL_MASK
438#undef PT_PTE_COPY_MASK
439#undef PT_NON_PTE_COPY_MASK
440#undef PT_DIR_BASE_ADDR_MASK
Avi Kivitycea0f0e2007-01-05 16:36:43 -0800441#undef PT_MAX_FULL_LEVELS