paging_tmpl.h 13.4 KB
Newer Older
A
Avi Kivity 已提交
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
	#ifdef CONFIG_X86_64
	#define PT_MAX_FULL_LEVELS 4
37
	#define CMPXCHG cmpxchg
38
	#else
39
	#define CMPXCHG cmpxchg64
40 41
	#define PT_MAX_FULL_LEVELS 2
	#endif
A
Avi Kivity 已提交
42 43 44 45 46 47 48 49 50
#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)
51
	#define PT_LEVEL_BITS PT32_LEVEL_BITS
52
	#define PT_MAX_FULL_LEVELS 2
53
	#define CMPXCHG cmpxchg
A
Avi Kivity 已提交
54 55 56 57
#else
	#error Invalid PTTYPE value
#endif

58 59 60
#define gpte_to_gfn FNAME(gpte_to_gfn)
#define gpte_to_gfn_pde FNAME(gpte_to_gfn_pde)

A
Avi Kivity 已提交
61 62 63 64 65 66
/*
 * The guest_walker structure emulates the behavior of the hardware page
 * table walker.
 */
struct guest_walker {
	int level;
67
	gfn_t table_gfn[PT_MAX_FULL_LEVELS];
68
	pt_element_t pte;
69 70
	unsigned pt_access;
	unsigned pte_access;
71
	gfn_t gfn;
72
	u32 error_code;
A
Avi Kivity 已提交
73 74
};

75 76 77 78 79 80 81 82 83 84
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;
}

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
static bool FNAME(cmpxchg_gpte)(struct kvm *kvm,
			 gfn_t table_gfn, unsigned index,
			 pt_element_t orig_pte, pt_element_t new_pte)
{
	pt_element_t ret;
	pt_element_t *table;
	struct page *page;

	page = gfn_to_page(kvm, table_gfn);
	table = kmap_atomic(page, KM_USER0);

	ret = CMPXCHG(&table[index], orig_pte, new_pte);

	kunmap_atomic(table, KM_USER0);

	kvm_release_page_dirty(page);

	return (ret != orig_pte);
}

105 106 107 108 109 110 111 112 113 114 115 116
static unsigned FNAME(gpte_access)(struct kvm_vcpu *vcpu, pt_element_t gpte)
{
	unsigned access;

	access = (gpte & (PT_WRITABLE_MASK | PT_USER_MASK)) | ACC_EXEC_MASK;
#if PTTYPE == 64
	if (is_nx(vcpu))
		access &= ~(gpte >> PT64_NX_SHIFT);
#endif
	return access;
}

117 118 119
/*
 * Fetch a guest pte for a guest virtual address
 */
120 121
static int FNAME(walk_addr)(struct guest_walker *walker,
			    struct kvm_vcpu *vcpu, gva_t addr,
122
			    int write_fault, int user_fault, int fetch_fault)
A
Avi Kivity 已提交
123
{
124
	pt_element_t pte;
125
	gfn_t table_gfn;
126
	unsigned index, pt_access, pte_access;
127
	gpa_t pte_gpa;
A
Avi Kivity 已提交
128

129
	pgprintk("%s: addr %lx\n", __FUNCTION__, addr);
130
walk:
A
Avi Kivity 已提交
131
	walker->level = vcpu->mmu.root_level;
132
	pte = vcpu->cr3;
133 134
#if PTTYPE == 64
	if (!is_long_mode(vcpu)) {
135 136
		pte = vcpu->pdptrs[(addr >> 30) & 3];
		if (!is_present_pte(pte))
137
			goto not_present;
138 139 140
		--walker->level;
	}
#endif
A
Avi Kivity 已提交
141
	ASSERT((!is_long_mode(vcpu) && is_pae(vcpu)) ||
142
	       (vcpu->cr3 & CR3_NONPAE_RESERVED_BITS) == 0);
A
Avi Kivity 已提交
143

144
	pt_access = ACC_ALL;
145 146

	for (;;) {
147
		index = PT_INDEX(addr, walker->level);
148

149
		table_gfn = gpte_to_gfn(pte);
A
Avi Kivity 已提交
150
		pte_gpa = gfn_to_gpa(table_gfn);
151
		pte_gpa += index * sizeof(pt_element_t);
152 153 154 155
		walker->table_gfn[walker->level - 1] = table_gfn;
		pgprintk("%s: table_gfn[%d] %lx\n", __FUNCTION__,
			 walker->level - 1, table_gfn);

156
		kvm_read_guest(vcpu->kvm, pte_gpa, &pte, sizeof(pte));
157 158

		if (!is_present_pte(pte))
159 160
			goto not_present;

161
		if (write_fault && !is_writeble_pte(pte))
162 163 164
			if (user_fault || is_write_protection(vcpu))
				goto access_error;

165
		if (user_fault && !(pte & PT_USER_MASK))
166 167
			goto access_error;

168
#if PTTYPE == 64
169
		if (fetch_fault && is_nx(vcpu) && (pte & PT64_NX_MASK))
170 171 172
			goto access_error;
#endif

173
		if (!(pte & PT_ACCESSED_MASK)) {
174
			mark_page_dirty(vcpu->kvm, table_gfn);
175 176 177
			if (FNAME(cmpxchg_gpte)(vcpu->kvm, table_gfn,
			    index, pte, pte|PT_ACCESSED_MASK))
				goto walk;
178
			pte |= PT_ACCESSED_MASK;
179
		}
180

181
		pte_access = pt_access & FNAME(gpte_access)(vcpu, pte);
182

183
		if (walker->level == PT_PAGE_TABLE_LEVEL) {
184
			walker->gfn = gpte_to_gfn(pte);
185 186 187 188
			break;
		}

		if (walker->level == PT_DIRECTORY_LEVEL
189
		    && (pte & PT_PAGE_SIZE_MASK)
190
		    && (PTTYPE == 64 || is_pse(vcpu))) {
191
			walker->gfn = gpte_to_gfn_pde(pte);
192
			walker->gfn += PT_INDEX(addr, PT_PAGE_TABLE_LEVEL);
193 194
			if (PTTYPE == 32 && is_cpuid_PSE36())
				walker->gfn += pse36_gfn_delta(pte);
195
			break;
196
		}
197

198
		pt_access = pte_access;
199 200
		--walker->level;
	}
201 202

	if (write_fault && !is_dirty_pte(pte)) {
203 204
		bool ret;

205
		mark_page_dirty(vcpu->kvm, table_gfn);
206 207 208 209
		ret = FNAME(cmpxchg_gpte)(vcpu->kvm, table_gfn, index, pte,
			    pte|PT_DIRTY_MASK);
		if (ret)
			goto walk;
210 211 212 213 214
		pte |= PT_DIRTY_MASK;
		kvm_mmu_pte_write(vcpu, pte_gpa, (u8 *)&pte, sizeof(pte));
	}

	walker->pte = pte;
215 216 217 218
	walker->pt_access = pt_access;
	walker->pte_access = pte_access;
	pgprintk("%s: pte %llx pte_access %x pt_access %x\n",
		 __FUNCTION__, (u64)pte, pt_access, pte_access);
219 220 221 222 223 224 225 226 227 228 229 230 231 232
	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;
233 234
	if (fetch_fault)
		walker->error_code |= PFERR_FETCH_MASK;
235
	return 0;
A
Avi Kivity 已提交
236 237
}

238
static void FNAME(set_pte)(struct kvm_vcpu *vcpu, pt_element_t gpte,
239 240
			   u64 *shadow_pte, unsigned pt_access,
			   unsigned pte_access,
241 242 243
			   int user_fault, int write_fault,
			   int *ptwrite, struct guest_walker *walker,
			   gfn_t gfn)
244
{
245
	int dirty = gpte & PT_DIRTY_MASK;
246 247
	u64 spte;
	int was_rmapped = is_rmap_pte(*shadow_pte);
I
Izik Eidus 已提交
248
	struct page *page;
249

250
	pgprintk("%s: spte %llx gpte %llx access %x write_fault %d"
251
		 " user_fault %d gfn %lx\n",
252
		 __FUNCTION__, *shadow_pte, (u64)gpte, pt_access,
253 254
		 write_fault, user_fault, gfn);

255 256 257 258 259 260
	/*
	 * 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;
261
	spte |= gpte & PT64_NX_MASK;
262
	if (!dirty)
263
		pte_access &= ~ACC_WRITE_MASK;
264 265
	if (!(pte_access & ACC_EXEC_MASK))
		spte |= PT64_NX_MASK;
266

267
	page = gfn_to_page(vcpu->kvm, gfn);
I
Izik Eidus 已提交
268

A
Avi Kivity 已提交
269
	spte |= PT_PRESENT_MASK;
270
	if (pte_access & ACC_USER_MASK)
A
Avi Kivity 已提交
271
		spte |= PT_USER_MASK;
272

273
	if (is_error_page(page)) {
274 275
		set_shadow_pte(shadow_pte,
			       shadow_trap_nonpresent_pte | PT_SHADOW_IO_MARK);
I
Izik Eidus 已提交
276
		kvm_release_page_clean(page);
277 278 279
		return;
	}

280
	spte |= page_to_phys(page);
281

282
	if ((pte_access & ACC_WRITE_MASK)
283
	    || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
284 285
		struct kvm_mmu_page *shadow;

A
Avi Kivity 已提交
286
		spte |= PT_WRITABLE_MASK;
287
		if (user_fault) {
288
			mmu_unshadow(vcpu->kvm, gfn);
289 290 291
			goto unshadowed;
		}

292
		shadow = kvm_mmu_lookup_page(vcpu->kvm, gfn);
293 294 295
		if (shadow) {
			pgprintk("%s: found shadow page for %lx, marking ro\n",
				 __FUNCTION__, gfn);
296
			pte_access &= ~ACC_WRITE_MASK;
A
Avi Kivity 已提交
297 298
			if (is_writeble_pte(spte)) {
				spte &= ~PT_WRITABLE_MASK;
299
				kvm_x86_ops->tlb_flush(vcpu);
300
			}
301 302
			if (write_fault)
				*ptwrite = 1;
303 304 305
		}
	}

306 307
unshadowed:

308
	if (pte_access & ACC_WRITE_MASK)
309
		mark_page_dirty(vcpu->kvm, gfn);
310

311
	pgprintk("%s: setting spte %llx\n", __FUNCTION__, spte);
312
	set_shadow_pte(shadow_pte, spte);
313
	page_header_update_slot(vcpu->kvm, shadow_pte, gfn);
314
	if (!was_rmapped) {
315
		rmap_add(vcpu, shadow_pte, gfn);
I
Izik Eidus 已提交
316
		if (!is_rmap_pte(*shadow_pte))
317
			kvm_release_page_clean(page);
318 319
	}
	else
I
Izik Eidus 已提交
320
		kvm_release_page_clean(page);
321 322
	if (!ptwrite || !*ptwrite)
		vcpu->last_pte_updated = shadow_pte;
323 324
}

325
static void FNAME(update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *page,
326 327
			      u64 *spte, const void *pte, int bytes,
			      int offset_in_pte)
328 329 330 331
{
	pt_element_t gpte;

	gpte = *(const pt_element_t *)pte;
332 333 334 335 336 337
	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))
338 339
		return;
	pgprintk("%s: gpte %llx spte %p\n", __FUNCTION__, (u64)gpte, spte);
340 341
	FNAME(set_pte)(vcpu, gpte, spte, ACC_ALL, ACC_ALL,
		       0, 0, NULL, NULL, gpte_to_gfn(gpte));
342 343
}

A
Avi Kivity 已提交
344 345 346 347
/*
 * Fetch a shadow pte for a specific level in the paging hierarchy.
 */
static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
348 349
			 struct guest_walker *walker,
			 int user_fault, int write_fault, int *ptwrite)
A
Avi Kivity 已提交
350 351 352
{
	hpa_t shadow_addr;
	int level;
353
	u64 *shadow_ent;
354
	unsigned access = walker->pt_access;
355

356
	if (!is_present_pte(walker->pte))
357
		return NULL;
A
Avi Kivity 已提交
358 359 360

	shadow_addr = vcpu->mmu.root_hpa;
	level = vcpu->mmu.shadow_root_level;
361 362 363 364 365
	if (level == PT32E_ROOT_LEVEL) {
		shadow_addr = vcpu->mmu.pae_root[(addr >> 30) & 3];
		shadow_addr &= PT64_BASE_ADDR_MASK;
		--level;
	}
A
Avi Kivity 已提交
366 367 368

	for (; ; level--) {
		u32 index = SHADOW_PT_INDEX(addr, level);
369
		struct kvm_mmu_page *shadow_page;
370
		u64 shadow_pte;
371 372
		int metaphysical;
		gfn_t table_gfn;
A
Avi Kivity 已提交
373

374
		shadow_ent = ((u64 *)__va(shadow_addr)) + index;
375
		if (is_shadow_present_pte(*shadow_ent)) {
A
Avi Kivity 已提交
376
			if (level == PT_PAGE_TABLE_LEVEL)
377
				break;
A
Avi Kivity 已提交
378 379 380 381
			shadow_addr = *shadow_ent & PT64_BASE_ADDR_MASK;
			continue;
		}

382 383
		if (level == PT_PAGE_TABLE_LEVEL)
			break;
A
Avi Kivity 已提交
384

385 386 387
		if (level - 1 == PT_PAGE_TABLE_LEVEL
		    && walker->level == PT_DIRECTORY_LEVEL) {
			metaphysical = 1;
388
			if (!is_dirty_pte(walker->pte))
389
				access &= ~ACC_WRITE_MASK;
390
			table_gfn = gpte_to_gfn(walker->pte);
391 392 393 394 395
		} else {
			metaphysical = 0;
			table_gfn = walker->table_gfn[level - 2];
		}
		shadow_page = kvm_mmu_get_page(vcpu, table_gfn, addr, level-1,
396
					       metaphysical, access,
397
					       shadow_ent);
398
		shadow_addr = __pa(shadow_page->spt);
399 400
		shadow_pte = shadow_addr | PT_PRESENT_MASK | PT_ACCESSED_MASK
			| PT_WRITABLE_MASK | PT_USER_MASK;
401
		*shadow_ent = shadow_pte;
A
Avi Kivity 已提交
402
	}
403

A
Avi Kivity 已提交
404
	FNAME(set_pte)(vcpu, walker->pte, shadow_ent,
405 406
		       access, walker->pte_access & access,
		       user_fault, write_fault,
A
Avi Kivity 已提交
407 408
		       ptwrite, walker, walker->gfn);

409
	return shadow_ent;
A
Avi Kivity 已提交
410 411 412 413 414 415 416 417 418 419 420 421 422
}

/*
 * 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
 *
423 424
 *  Returns: 1 if we need to emulate the instruction, 0 otherwise, or
 *           a negative value on error.
A
Avi Kivity 已提交
425 426 427 428 429 430
 */
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;
431
	int fetch_fault = error_code & PFERR_FETCH_MASK;
A
Avi Kivity 已提交
432 433
	struct guest_walker walker;
	u64 *shadow_pte;
434
	int write_pt = 0;
435
	int r;
A
Avi Kivity 已提交
436

437
	pgprintk("%s: addr %lx err %x\n", __FUNCTION__, addr, error_code);
438
	kvm_mmu_audit(vcpu, "pre page fault");
439

440 441 442
	r = mmu_topup_memory_caches(vcpu);
	if (r)
		return r;
443

A
Avi Kivity 已提交
444 445 446
	/*
	 * Look up the shadow pte for the faulting address.
	 */
447 448
	r = FNAME(walk_addr)(&walker, vcpu, addr, write_fault, user_fault,
			     fetch_fault);
A
Avi Kivity 已提交
449 450 451 452

	/*
	 * The page is not mapped by the guest.  Let the guest handle it.
	 */
453 454 455
	if (!r) {
		pgprintk("%s: guest page fault\n", __FUNCTION__);
		inject_page_fault(vcpu, addr, walker.error_code);
456
		vcpu->last_pt_write_count = 0; /* reset fork detector */
A
Avi Kivity 已提交
457 458 459
		return 0;
	}

460 461 462 463
	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);
464

465 466 467
	if (!write_pt)
		vcpu->last_pt_write_count = 0; /* reset fork detector */

A
Avi Kivity 已提交
468 469 470
	/*
	 * mmio: emulate if accessible, otherwise its a guest fault.
	 */
A
Avi Kivity 已提交
471
	if (is_io_pte(*shadow_pte))
472
		return 1;
A
Avi Kivity 已提交
473

A
Avi Kivity 已提交
474
	++vcpu->stat.pf_fixed;
475
	kvm_mmu_audit(vcpu, "post page fault (fixed)");
A
Avi Kivity 已提交
476

477
	return write_pt;
A
Avi Kivity 已提交
478 479 480 481 482
}

static gpa_t FNAME(gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t vaddr)
{
	struct guest_walker walker;
A
Avi Kivity 已提交
483 484
	gpa_t gpa = UNMAPPED_GVA;
	int r;
A
Avi Kivity 已提交
485

A
Avi Kivity 已提交
486
	r = FNAME(walk_addr)(&walker, vcpu, vaddr, 0, 0, 0);
A
Avi Kivity 已提交
487

A
Avi Kivity 已提交
488
	if (r) {
A
Avi Kivity 已提交
489
		gpa = gfn_to_gpa(walker.gfn);
A
Avi Kivity 已提交
490
		gpa |= vaddr & ~PAGE_MASK;
A
Avi Kivity 已提交
491 492 493 494 495
	}

	return gpa;
}

496 497 498
static void FNAME(prefetch_page)(struct kvm_vcpu *vcpu,
				 struct kvm_mmu_page *sp)
{
499
	int i, offset = 0;
500
	pt_element_t *gpt;
501
	struct page *page;
502

503 504
	if (sp->role.metaphysical
	    || (PTTYPE == 32 && sp->role.level > PT_PAGE_TABLE_LEVEL)) {
505 506 507 508
		nonpaging_prefetch_page(vcpu, sp);
		return;
	}

509 510
	if (PTTYPE == 32)
		offset = sp->role.quadrant << PT64_LEVEL_BITS;
511 512
	page = gfn_to_page(vcpu->kvm, sp->gfn);
	gpt = kmap_atomic(page, KM_USER0);
513
	for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
514
		if (is_present_pte(gpt[offset + i]))
515 516 517 518
			sp->spt[i] = shadow_trap_nonpresent_pte;
		else
			sp->spt[i] = shadow_notrap_nonpresent_pte;
	kunmap_atomic(gpt, KM_USER0);
519
	kvm_release_page_clean(page);
520 521
}

A
Avi Kivity 已提交
522 523 524 525 526 527 528 529
#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
530
#undef PT_LEVEL_BITS
531
#undef PT_MAX_FULL_LEVELS
532 533
#undef gpte_to_gfn
#undef gpte_to_gfn_pde
534
#undef CMPXCHG