paging_tmpl.h 12.6 KB
Newer Older
Avi Kivity's avatar
Avi Kivity committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
 * Kernel-based Virtual Machine driver for Linux
 *
 * This module enables machines with Intel VT-x extensions to run virtual
 * machines without emulation or binary translation.
 *
 * MMU support
 *
 * Copyright (C) 2006 Qumranet, Inc.
 *
 * Authors:
 *   Yaniv Kamay  <yaniv@qumranet.com>
 *   Avi Kivity   <avi@qumranet.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2.  See
 * the COPYING file in the top-level directory.
 *
 */

/*
 * We need the mmu code to access both 32-bit and 64-bit guest ptes,
 * so the code in this file is compiled twice, once per pte size.
 */

#if PTTYPE == 64
	#define pt_element_t u64
	#define guest_walker guest_walker64
	#define FNAME(name) paging##64_##name
	#define PT_BASE_ADDR_MASK PT64_BASE_ADDR_MASK
	#define PT_DIR_BASE_ADDR_MASK PT64_DIR_BASE_ADDR_MASK
	#define PT_INDEX(addr, level) PT64_INDEX(addr, level)
	#define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
	#define PT_LEVEL_MASK(level) PT64_LEVEL_MASK(level)
34
	#define PT_LEVEL_BITS PT64_LEVEL_BITS
35
36
37
38
39
	#ifdef CONFIG_X86_64
	#define PT_MAX_FULL_LEVELS 4
	#else
	#define PT_MAX_FULL_LEVELS 2
	#endif
Avi Kivity's avatar
Avi Kivity committed
40
41
42
43
44
45
46
47
48
#elif PTTYPE == 32
	#define pt_element_t u32
	#define guest_walker guest_walker32
	#define FNAME(name) paging##32_##name
	#define PT_BASE_ADDR_MASK PT32_BASE_ADDR_MASK
	#define PT_DIR_BASE_ADDR_MASK PT32_DIR_BASE_ADDR_MASK
	#define PT_INDEX(addr, level) PT32_INDEX(addr, level)
	#define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
	#define PT_LEVEL_MASK(level) PT32_LEVEL_MASK(level)
49
	#define PT_LEVEL_BITS PT32_LEVEL_BITS
50
	#define PT_MAX_FULL_LEVELS 2
Avi Kivity's avatar
Avi Kivity committed
51
52
53
54
#else
	#error Invalid PTTYPE value
#endif

55
56
57
#define gpte_to_gfn FNAME(gpte_to_gfn)
#define gpte_to_gfn_pde FNAME(gpte_to_gfn_pde)

Avi Kivity's avatar
Avi Kivity committed
58
59
60
61
62
63
/*
 * The guest_walker structure emulates the behavior of the hardware page
 * table walker.
 */
struct guest_walker {
	int level;
64
	gfn_t table_gfn[PT_MAX_FULL_LEVELS];
65
	pt_element_t pte;
Avi Kivity's avatar
Avi Kivity committed
66
	pt_element_t inherited_ar;
67
	gfn_t gfn;
68
	u32 error_code;
Avi Kivity's avatar
Avi Kivity committed
69
70
};

71
72
73
74
75
76
77
78
79
80
static gfn_t gpte_to_gfn(pt_element_t gpte)
{
	return (gpte & PT_BASE_ADDR_MASK) >> PAGE_SHIFT;
}

static gfn_t gpte_to_gfn_pde(pt_element_t gpte)
{
	return (gpte & PT_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
}

81
82
83
/*
 * Fetch a guest pte for a guest virtual address
 */
84
85
static int FNAME(walk_addr)(struct guest_walker *walker,
			    struct kvm_vcpu *vcpu, gva_t addr,
86
			    int write_fault, int user_fault, int fetch_fault)
Avi Kivity's avatar
Avi Kivity committed
87
{
88
	pt_element_t pte;
89
	gfn_t table_gfn;
90
91
	unsigned index;
	gpa_t pte_gpa;
Avi Kivity's avatar
Avi Kivity committed
92

93
	pgprintk("%s: addr %lx\n", __FUNCTION__, addr);
Avi Kivity's avatar
Avi Kivity committed
94
	walker->level = vcpu->mmu.root_level;
95
	pte = vcpu->cr3;
96
97
#if PTTYPE == 64
	if (!is_long_mode(vcpu)) {
98
99
		pte = vcpu->pdptrs[(addr >> 30) & 3];
		if (!is_present_pte(pte))
100
			goto not_present;
101
102
103
		--walker->level;
	}
#endif
104
	ASSERT((!is_long_mode(vcpu) && is_pae(vcpu)) ||
105
	       (vcpu->cr3 & CR3_NONPAE_RESERVED_BITS) == 0);
Avi Kivity's avatar
Avi Kivity committed
106
107

	walker->inherited_ar = PT_USER_MASK | PT_WRITABLE_MASK;
108
109

	for (;;) {
110
		index = PT_INDEX(addr, walker->level);
111

112
		table_gfn = gpte_to_gfn(pte);
Avi Kivity's avatar
Avi Kivity committed
113
		pte_gpa = gfn_to_gpa(table_gfn);
114
		pte_gpa += index * sizeof(pt_element_t);
115
116
117
118
		walker->table_gfn[walker->level - 1] = table_gfn;
		pgprintk("%s: table_gfn[%d] %lx\n", __FUNCTION__,
			 walker->level - 1, table_gfn);

119
		kvm_read_guest(vcpu->kvm, pte_gpa, &pte, sizeof(pte));
120
121

		if (!is_present_pte(pte))
122
123
			goto not_present;

124
		if (write_fault && !is_writeble_pte(pte))
125
126
127
			if (user_fault || is_write_protection(vcpu))
				goto access_error;

128
		if (user_fault && !(pte & PT_USER_MASK))
129
130
			goto access_error;

131
#if PTTYPE == 64
132
		if (fetch_fault && is_nx(vcpu) && (pte & PT64_NX_MASK))
133
134
135
			goto access_error;
#endif

136
		if (!(pte & PT_ACCESSED_MASK)) {
137
			mark_page_dirty(vcpu->kvm, table_gfn);
138
			pte |= PT_ACCESSED_MASK;
139
			kvm_write_guest(vcpu->kvm, pte_gpa, &pte, sizeof(pte));
140
		}
141
142

		if (walker->level == PT_PAGE_TABLE_LEVEL) {
143
			walker->gfn = gpte_to_gfn(pte);
144
145
146
147
			break;
		}

		if (walker->level == PT_DIRECTORY_LEVEL
148
		    && (pte & PT_PAGE_SIZE_MASK)
149
		    && (PTTYPE == 64 || is_pse(vcpu))) {
150
			walker->gfn = gpte_to_gfn_pde(pte);
151
			walker->gfn += PT_INDEX(addr, PT_PAGE_TABLE_LEVEL);
152
153
			if (PTTYPE == 32 && is_cpuid_PSE36())
				walker->gfn += pse36_gfn_delta(pte);
154
			break;
155
		}
156

157
		walker->inherited_ar &= pte;
158
159
		--walker->level;
	}
160
161
162
163

	if (write_fault && !is_dirty_pte(pte)) {
		mark_page_dirty(vcpu->kvm, table_gfn);
		pte |= PT_DIRTY_MASK;
164
		kvm_write_guest(vcpu->kvm, pte_gpa, &pte, sizeof(pte));
165
166
167
168
169
		kvm_mmu_pte_write(vcpu, pte_gpa, (u8 *)&pte, sizeof(pte));
	}

	walker->pte = pte;
	pgprintk("%s: pte %llx\n", __FUNCTION__, (u64)pte);
170
171
172
173
174
175
176
177
178
179
180
181
182
183
	return 1;

not_present:
	walker->error_code = 0;
	goto err;

access_error:
	walker->error_code = PFERR_PRESENT_MASK;

err:
	if (write_fault)
		walker->error_code |= PFERR_WRITE_MASK;
	if (user_fault)
		walker->error_code |= PFERR_USER_MASK;
184
185
	if (fetch_fault)
		walker->error_code |= PFERR_FETCH_MASK;
186
	return 0;
Avi Kivity's avatar
Avi Kivity committed
187
188
}

189
190
191
192
193
static void FNAME(set_pte)(struct kvm_vcpu *vcpu, pt_element_t gpte,
			   u64 *shadow_pte, u64 access_bits,
			   int user_fault, int write_fault,
			   int *ptwrite, struct guest_walker *walker,
			   gfn_t gfn)
194
{
195
	int dirty = gpte & PT_DIRTY_MASK;
196
197
	u64 spte;
	int was_rmapped = is_rmap_pte(*shadow_pte);
Izik Eidus's avatar
Izik Eidus committed
198
	struct page *page;
199
200
201

	pgprintk("%s: spte %llx gpte %llx access %llx write_fault %d"
		 " user_fault %d gfn %lx\n",
202
		 __FUNCTION__, *shadow_pte, (u64)gpte, access_bits,
203
204
		 write_fault, user_fault, gfn);

205
	access_bits &= gpte;
206
207
208
209
210
211
	/*
	 * We don't set the accessed bit, since we sometimes want to see
	 * whether the guest actually used the pte (in order to detect
	 * demand paging).
	 */
	spte = PT_PRESENT_MASK | PT_DIRTY_MASK;
212
	spte |= gpte & PT64_NX_MASK;
213
214
215
	if (!dirty)
		access_bits &= ~PT_WRITABLE_MASK;

216
	page = gfn_to_page(vcpu->kvm, gfn);
Izik Eidus's avatar
Izik Eidus committed
217

218
	spte |= PT_PRESENT_MASK;
219
	if (access_bits & PT_USER_MASK)
220
		spte |= PT_USER_MASK;
221

222
	if (is_error_page(page)) {
223
224
		set_shadow_pte(shadow_pte,
			       shadow_trap_nonpresent_pte | PT_SHADOW_IO_MARK);
Izik Eidus's avatar
Izik Eidus committed
225
		kvm_release_page_clean(page);
226
227
228
		return;
	}

229
	spte |= page_to_phys(page);
230

231
232
	if ((access_bits & PT_WRITABLE_MASK)
	    || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
233
234
		struct kvm_mmu_page *shadow;

235
		spte |= PT_WRITABLE_MASK;
236
		if (user_fault) {
237
			mmu_unshadow(vcpu->kvm, gfn);
238
239
240
			goto unshadowed;
		}

241
		shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
242
243
244
245
		if (shadow) {
			pgprintk("%s: found shadow page for %lx, marking ro\n",
				 __FUNCTION__, gfn);
			access_bits &= ~PT_WRITABLE_MASK;
246
247
			if (is_writeble_pte(spte)) {
				spte &= ~PT_WRITABLE_MASK;
248
				kvm_x86_ops->tlb_flush(vcpu);
249
			}
250
251
			if (write_fault)
				*ptwrite = 1;
252
253
254
		}
	}

255
256
unshadowed:

257
	if (access_bits & PT_WRITABLE_MASK)
258
		mark_page_dirty(vcpu->kvm, gfn);
259

260
	pgprintk("%s: setting spte %llx\n", __FUNCTION__, spte);
261
	set_shadow_pte(shadow_pte, spte);
262
	page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
263
	if (!was_rmapped) {
264
		rmap_add(vcpu, shadow_pte, gfn);
Izik Eidus's avatar
Izik Eidus committed
265
		if (!is_rmap_pte(*shadow_pte))
266
			kvm_release_page_clean(page);
267
268
	}
	else
Izik Eidus's avatar
Izik Eidus committed
269
		kvm_release_page_clean(page);
270
271
	if (!ptwrite || !*ptwrite)
		vcpu->last_pte_updated = shadow_pte;
272
273
}

274
static void FNAME(update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *page,
275
276
			      u64 *spte, const void *pte, int bytes,
			      int offset_in_pte)
277
278
279
280
{
	pt_element_t gpte;

	gpte = *(const pt_element_t *)pte;
281
282
283
284
285
286
	if (~gpte & (PT_PRESENT_MASK | PT_ACCESSED_MASK)) {
		if (!offset_in_pte && !is_present_pte(gpte))
			set_shadow_pte(spte, shadow_notrap_nonpresent_pte);
		return;
	}
	if (bytes < sizeof(pt_element_t))
287
288
		return;
	pgprintk("%s: gpte %llx spte %p\n", __FUNCTION__, (u64)gpte, spte);
289
	FNAME(set_pte)(vcpu, gpte, spte, PT_USER_MASK | PT_WRITABLE_MASK, 0,
290
		       0, NULL, NULL, gpte_to_gfn(gpte));
291
292
}

Avi Kivity's avatar
Avi Kivity committed
293
294
295
296
/*
 * Fetch a shadow pte for a specific level in the paging hierarchy.
 */
static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
297
298
			 struct guest_walker *walker,
			 int user_fault, int write_fault, int *ptwrite)
Avi Kivity's avatar
Avi Kivity committed
299
300
301
{
	hpa_t shadow_addr;
	int level;
302
	u64 *shadow_ent;
Avi Kivity's avatar
Avi Kivity committed
303
	u64 *prev_shadow_ent = NULL;
304

305
	if (!is_present_pte(walker->pte))
306
		return NULL;
Avi Kivity's avatar
Avi Kivity committed
307
308
309

	shadow_addr = vcpu->mmu.root_hpa;
	level = vcpu->mmu.shadow_root_level;
310
311
312
313
314
	if (level == PT32E_ROOT_LEVEL) {
		shadow_addr = vcpu->mmu.pae_root[(addr >> 30) & 3];
		shadow_addr &= PT64_BASE_ADDR_MASK;
		--level;
	}
Avi Kivity's avatar
Avi Kivity committed
315
316
317

	for (; ; level--) {
		u32 index = SHADOW_PT_INDEX(addr, level);
318
		struct kvm_mmu_page *shadow_page;
319
		u64 shadow_pte;
320
321
		int metaphysical;
		gfn_t table_gfn;
322
		unsigned hugepage_access = 0;
Avi Kivity's avatar
Avi Kivity committed
323

324
		shadow_ent = ((u64 *)__va(shadow_addr)) + index;
325
		if (is_shadow_present_pte(*shadow_ent)) {
Avi Kivity's avatar
Avi Kivity committed
326
			if (level == PT_PAGE_TABLE_LEVEL)
327
				break;
Avi Kivity's avatar
Avi Kivity committed
328
329
330
331
332
			shadow_addr = *shadow_ent & PT64_BASE_ADDR_MASK;
			prev_shadow_ent = shadow_ent;
			continue;
		}

333
334
		if (level == PT_PAGE_TABLE_LEVEL)
			break;
Avi Kivity's avatar
Avi Kivity committed
335

336
337
338
		if (level - 1 == PT_PAGE_TABLE_LEVEL
		    && walker->level == PT_DIRECTORY_LEVEL) {
			metaphysical = 1;
339
			hugepage_access = walker->pte;
340
			hugepage_access &= PT_USER_MASK | PT_WRITABLE_MASK;
341
342
			if (!is_dirty_pte(walker->pte))
				hugepage_access &= ~PT_WRITABLE_MASK;
343
			hugepage_access >>= PT_WRITABLE_SHIFT;
344
			if (walker->pte & PT64_NX_MASK)
345
				hugepage_access |= (1 << 2);
346
			table_gfn = gpte_to_gfn(walker->pte);
347
348
349
350
351
		} else {
			metaphysical = 0;
			table_gfn = walker->table_gfn[level - 2];
		}
		shadow_page = kvm_mmu_get_page(vcpu, table_gfn, addr, level-1,
352
353
					       metaphysical, hugepage_access,
					       shadow_ent);
354
		shadow_addr = __pa(shadow_page->spt);
355
356
		shadow_pte = shadow_addr | PT_PRESENT_MASK | PT_ACCESSED_MASK
			| PT_WRITABLE_MASK | PT_USER_MASK;
357
		*shadow_ent = shadow_pte;
Avi Kivity's avatar
Avi Kivity committed
358
359
		prev_shadow_ent = shadow_ent;
	}
360

Avi Kivity's avatar
Avi Kivity committed
361
362
363
364
	FNAME(set_pte)(vcpu, walker->pte, shadow_ent,
		       walker->inherited_ar, user_fault, write_fault,
		       ptwrite, walker, walker->gfn);

365
	return shadow_ent;
Avi Kivity's avatar
Avi Kivity committed
366
367
368
369
370
371
372
373
374
375
376
377
378
}

/*
 * Page fault handler.  There are several causes for a page fault:
 *   - there is no shadow pte for the guest pte
 *   - write access through a shadow pte marked read only so that we can set
 *     the dirty bit
 *   - write access to a shadow pte marked read only so we can update the page
 *     dirty bitmap, when userspace requests it
 *   - mmio access; in this case we will never install a present shadow pte
 *   - normal guest page fault due to the guest pte marked not present, not
 *     writable, or not executable
 *
379
380
 *  Returns: 1 if we need to emulate the instruction, 0 otherwise, or
 *           a negative value on error.
Avi Kivity's avatar
Avi Kivity committed
381
382
383
384
385
386
 */
static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr,
			       u32 error_code)
{
	int write_fault = error_code & PFERR_WRITE_MASK;
	int user_fault = error_code & PFERR_USER_MASK;
387
	int fetch_fault = error_code & PFERR_FETCH_MASK;
Avi Kivity's avatar
Avi Kivity committed
388
389
	struct guest_walker walker;
	u64 *shadow_pte;
390
	int write_pt = 0;
391
	int r;
Avi Kivity's avatar
Avi Kivity committed
392

393
	pgprintk("%s: addr %lx err %x\n", __FUNCTION__, addr, error_code);
394
	kvm_mmu_audit(vcpu, "pre page fault");
395

396
397
398
	r = mmu_topup_memory_caches(vcpu);
	if (r)
		return r;
399

Avi Kivity's avatar
Avi Kivity committed
400
401
402
	/*
	 * Look up the shadow pte for the faulting address.
	 */
403
404
	r = FNAME(walk_addr)(&walker, vcpu, addr, write_fault, user_fault,
			     fetch_fault);
Avi Kivity's avatar
Avi Kivity committed
405
406
407
408

	/*
	 * The page is not mapped by the guest.  Let the guest handle it.
	 */
409
410
411
	if (!r) {
		pgprintk("%s: guest page fault\n", __FUNCTION__);
		inject_page_fault(vcpu, addr, walker.error_code);
412
		vcpu->last_pt_write_count = 0; /* reset fork detector */
Avi Kivity's avatar
Avi Kivity committed
413
414
415
		return 0;
	}

416
417
418
419
	shadow_pte = FNAME(fetch)(vcpu, addr, &walker, user_fault, write_fault,
				  &write_pt);
	pgprintk("%s: shadow pte %p %llx ptwrite %d\n", __FUNCTION__,
		 shadow_pte, *shadow_pte, write_pt);
420

421
422
423
	if (!write_pt)
		vcpu->last_pt_write_count = 0; /* reset fork detector */

Avi Kivity's avatar
Avi Kivity committed
424
425
426
	/*
	 * mmio: emulate if accessible, otherwise its a guest fault.
	 */
Avi Kivity's avatar
Avi Kivity committed
427
	if (is_io_pte(*shadow_pte))
428
		return 1;
Avi Kivity's avatar
Avi Kivity committed
429

Avi Kivity's avatar
Avi Kivity committed
430
	++vcpu->stat.pf_fixed;
431
	kvm_mmu_audit(vcpu, "post page fault (fixed)");
Avi Kivity's avatar
Avi Kivity committed
432

433
	return write_pt;
Avi Kivity's avatar
Avi Kivity committed
434
435
436
437
438
}

static gpa_t FNAME(gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t vaddr)
{
	struct guest_walker walker;
Avi Kivity's avatar
Avi Kivity committed
439
440
	gpa_t gpa = UNMAPPED_GVA;
	int r;
Avi Kivity's avatar
Avi Kivity committed
441

Avi Kivity's avatar
Avi Kivity committed
442
	r = FNAME(walk_addr)(&walker, vcpu, vaddr, 0, 0, 0);
Avi Kivity's avatar
Avi Kivity committed
443

Avi Kivity's avatar
Avi Kivity committed
444
	if (r) {
Avi Kivity's avatar
Avi Kivity committed
445
		gpa = gfn_to_gpa(walker.gfn);
Avi Kivity's avatar
Avi Kivity committed
446
		gpa |= vaddr & ~PAGE_MASK;
Avi Kivity's avatar
Avi Kivity committed
447
448
449
450
451
	}

	return gpa;
}

452
453
454
static void FNAME(prefetch_page)(struct kvm_vcpu *vcpu,
				 struct kvm_mmu_page *sp)
{
455
	int i, offset = 0;
456
	pt_element_t *gpt;
457
	struct page *page;
458

459
460
	if (sp->role.metaphysical
	    || (PTTYPE == 32 && sp->role.level > PT_PAGE_TABLE_LEVEL)) {
461
462
463
464
		nonpaging_prefetch_page(vcpu, sp);
		return;
	}

465
466
	if (PTTYPE == 32)
		offset = sp->role.quadrant << PT64_LEVEL_BITS;
467
468
	page = gfn_to_page(vcpu->kvm, sp->gfn);
	gpt = kmap_atomic(page, KM_USER0);
469
	for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
470
		if (is_present_pte(gpt[offset + i]))
471
472
473
474
			sp->spt[i] = shadow_trap_nonpresent_pte;
		else
			sp->spt[i] = shadow_notrap_nonpresent_pte;
	kunmap_atomic(gpt, KM_USER0);
475
	kvm_release_page_clean(page);
476
477
}

Avi Kivity's avatar
Avi Kivity committed
478
479
480
481
482
483
484
485
#undef pt_element_t
#undef guest_walker
#undef FNAME
#undef PT_BASE_ADDR_MASK
#undef PT_INDEX
#undef SHADOW_PT_INDEX
#undef PT_LEVEL_MASK
#undef PT_DIR_BASE_ADDR_MASK
486
#undef PT_LEVEL_BITS
487
#undef PT_MAX_FULL_LEVELS
488
489
#undef gpte_to_gfn
#undef gpte_to_gfn_pde