lcd-domains.c 38.2 KB
Newer Older
1
/**
2
 * lcd-domains.c - Main file for the LCD module
3 4 5
 *
 *
 * Authors:
6 7
 *   Anton Burtsev     <aburtsev@flux.utah.edu>
 *   Charlie Jacobsen  <charlesj@cs.utah.edu>
8 9 10 11 12 13 14 15 16
 */

#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/miscdevice.h>
#include <linux/compat.h>
#include <linux/fs.h>
17
#include <linux/slab.h>
18
#include <asm/uaccess.h>
19
#include <asm/pgtable.h>
20 21 22
#include <linux/kthread.h>
#include <linux/kmod.h>
#include <linux/mm.h>
23

24
#include <linux/lcd-domains.h>
25
#include <asm/lcd-domains-arch.h>
26 27
#include <lcd-domains/lcd-domains.h>
#include <lcd-domains/syscall.h>
28 29 30 31

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("LCD driver");

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#define LCD_PAGING_MEM_SIZE (4 * (1 << 20)) /* 4 MBs */

#define LCD_ERR(msg...) __lcd_err(__FILE__, __LINE__, msg)
static inline void __lcd_err(char *file, int lineno, char *fmt, ...)
{
	va_list args;
	printk(KERN_ERR "lcd-domains: %s:%d: error: ", file, lineno);
	va_start(args, fmt);
	vprintk(fmt, args);
	va_end(args);
}
#define LCD_MSG(msg...) __lcd_msg(__FILE__, __LINE__, msg)
static inline void __lcd_msg(char *file, int lineno, char *fmt, ...)
{
	va_list args;
	printk(KERN_ERR "lcd-domains: %s:%d: note: ", file, lineno);
	va_start(args, fmt);
	vprintk(fmt, args);
	va_end(args);
}
#define LCD_WARN(msg...) __lcd_warn(__FILE__, __LINE__, msg)
static inline void __lcd_warn(char *file, int lineno, char *fmt, ...)
{
	va_list args;
	printk(KERN_ERR "lcd-domains: %s:%d: warning: ", file, lineno);
	va_start(args, fmt);
	vprintk(fmt, args);
	va_end(args);
}

62 63
/* Guest Virtual -------------------------------------------------- */

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
static inline gpa_t pte_gpa(pte_t *pte)
{
	return __gpa(pte_pfn(*pte) << PAGE_SHIFT);
}
static inline gpa_t pmd_gpa(pmd_t *pmd_entry)
{
	return __gpa(pmd_pfn(*pmd_entry) << PAGE_SHIFT);
}
static inline gpa_t pud_gpa(pud_t *pud_entry)
{
	return __gpa(pud_pfn(*pud_entry) << PAGE_SHIFT);
}
static inline gpa_t pgd_gpa(pgd_t *pgd_entry)
{
	return __gpa(pgd_pfn(*pgd_entry) << PAGE_SHIFT);
}
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
static inline void set_pte_gpa(pte_t *pte, gpa_t gpa)
{
	set_pte(pte, __pte(gpa_val(gpa) | _KERNPG_TABLE));
}
static inline void set_pmd_gpa(pmd_t *entry, gpa_t gpa)
{
	set_pmd(entry, __pmd(gpa_val(gpa) | _KERNPG_TABLE));
}
static inline void set_pud_gpa(pud_t *entry, gpa_t gpa)
{
	set_pud(entry, __pud(gpa_val(gpa) | _KERNPG_TABLE));
}
static inline void set_pgd_gpa(pgd_t *entry, gpa_t gpa)
{
	set_pgd(entry, __pgd(gpa_val(gpa) | _KERNPG_TABLE));
}
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160

/**
 * Allocates a host physical page and guest physical
 * page (in the lcd's guest phys address space) for
 * storing a paging structure.
 */
static int lcd_mm_gva_alloc(struct lcd *lcd, gpa_t *ga_out, hpa_t *ha_out)
{
	hva_t hva;
	gpa_t gpa;
	hpa_t hpa;
	int ret;

	if (!lcd->gv.present) {
		printk(KERN_ERR "lcd_mm_gva_alloc: gv paging not present\n");
		ret = -EINVAL;
		goto fail1;
	}

	/*
	 * Check watermark, and bump it.
	 */
	if (gpa_val(lcd->gv.paging_mem_brk) >= 
		gpa_val(lcd->gv.paging_mem_top)) {
		printk(KERN_ERR "lcd_mm_gva_alloc: exhausted paging mem\n");
		ret = -ENOMEM;
		goto fail1;
	}
	gpa = lcd->gv.paging_mem_brk;
	lcd->gv.paging_mem_brk = gpa_add(lcd->gv.paging_mem_brk, PAGE_SIZE);

	/*
	 * Allocate a host physical page
	 */
	hva = __hva(__get_free_page(GFP_KERNEL));
	if (!hva_val(hva)) {
		printk(KERN_ERR "lcd_mm_gva_alloc: no host phys mem\n");
		ret = -ENOMEM;
		goto fail2;
	}
	memset(hva2va(hva), 0, PAGE_SIZE);
	hpa = hva2hpa(hva);

	/*
	 * Map in ept
	 */
	ret = lcd_arch_ept_map_range(lcd->lcd_arch, gpa, hpa, 1);
	if (ret) {
		printk(KERN_ERR "lcd_mm_gva_alloc: couldn't map gpa %lx to hpa %lx\n",
			gpa_val(gpa),
			hpa_val(hpa));
		goto fail3;
	}

	*ga_out = gpa;
	*ha_out = hpa;

	return 0;

fail3:
	free_page(hva_val(hva));
fail2:
fail1:
	return ret;
}
Charles Jacobsen's avatar
Charles Jacobsen committed
161

162 163 164 165 166 167
/**
 * Initializes guest virtual address space info in lcd, and
 * sets gva root pointer (e.g., x86 %cr3).
 *
 * Must be called before mapping any gva's.
 */
168 169
static int lcd_mm_gva_init(struct lcd *lcd, gpa_t gv_paging_mem_start,
			gpa_t gv_paging_mem_end)
170
{
171 172
	gpa_t gpa;
	hpa_t hpa;
173 174 175
	int ret;

	/*
Charles Jacobsen's avatar
Charles Jacobsen committed
176
	 * Set start / end
177
	 */
Charles Jacobsen's avatar
Charles Jacobsen committed
178 179 180
	lcd->gv.paging_mem_bot = gv_paging_mem_start;
	lcd->gv.paging_mem_brk = gv_paging_mem_start;
	lcd->gv.paging_mem_top = gv_paging_mem_end;
181 182 183 184 185 186

	/*
	 * Mark paging as present
	 */
	lcd->gv.present = 1;

187 188 189
	/*
	 * Alloc a page for the pgd
	 */
Charles Jacobsen's avatar
Charles Jacobsen committed
190 191 192
	ret = lcd_mm_gva_alloc(lcd, &gpa, &hpa);
	if (ret) {
		printk(KERN_ERR "lcd_mm_gva_init: error alloc'ing\n");
193
		goto fail1;
194 195
	}
	
196
	/*
197 198 199
	 * Store the root pointer
	 */
	lcd->gv.root = (pgd_t *)hpa2va(hpa);
Charles Jacobsen's avatar
Charles Jacobsen committed
200
	lcd_arch_set_gva_root(lcd->lcd_arch, gpa);
201

202
	return 0;
203 204 205 206 207

fail1:
	return ret;
}

208
static int lcd_mm_pt_destroy(struct lcd *lcd, pmd_t *pmd_entry)
209
{
210 211
	gpa_t gpa;
	hpa_t hpa;
212
	int ret;
Charles Jacobsen's avatar
Charles Jacobsen committed
213 214
	pte_t* pt;
	int i;
Charlie Jacobsen's avatar
Charlie Jacobsen committed
215
	
216 217 218
	/*
	 * Get hpa of page table, using gpa stored in pmd_entry.
	 */
219
	gpa = pmd_gpa(pmd_entry);
Charlie Jacobsen's avatar
Charlie Jacobsen committed
220
	ret = lcd_arch_ept_gpa_to_hpa(lcd->lcd_arch, gpa, &hpa);
221 222
	if (ret) {
		printk(KERN_ERR "lcd_mm_pt_destroy: error looking up gpa %lx\n",
223
			gpa_val(gpa));
224 225 226
		return ret;
	}

227 228 229 230 231 232 233
	pt = (pte_t *)hpa2va(hpa);

	/*
	 * Check for any potential memory leaks
	 */
	for (i = 0; i < PTRS_PER_PTE; i++) {
		if (pte_present(pt[i])) {
Charles Jacobsen's avatar
Charles Jacobsen committed
234
			printk(KERN_ERR "lcd_mm_pt_destroy: possible memory leak for gpa %lx (pt idx %d)\n",
235 236 237 238 239
				gpa_val(pte_gpa(&pt[i])), i);
			dump_stack();
		}
	}

240 241 242
	/*
	 * Unmap page table
	 */
243
	ret = lcd_arch_ept_unmap_range(lcd->lcd_arch, gpa, 1);
244 245 246 247
	if (ret) {
		printk(KERN_ERR "lcd_mm_pt_destroy: error unmapping pt\n");
		return ret;
	}
248 249 250 251

	/*
	 * Free page table
	 */
252
	free_page((unsigned long)pt);
253 254

	return 0;
255 256
}

257
static int lcd_mm_pmd_destroy(struct lcd *lcd, pud_t *pud_entry)
258 259
{
	pmd_t *pmd;
260 261
	gpa_t gpa;
	hpa_t hpa;
262
	int i;
263
	int ret;
264 265 266 267

	/*
	 * Get hpa of pmd, using gpa stored in pud_entry.
	 */
268
	gpa = pud_gpa(pud_entry);
Charlie Jacobsen's avatar
Charlie Jacobsen committed
269
	ret = lcd_arch_ept_gpa_to_hpa(lcd->lcd_arch, gpa, &hpa);
270 271
	if (ret) {
		printk(KERN_ERR "lcd_mm_pmd_destroy: error looking up gpa %lx\n",
272
			gpa_val(gpa));
273 274 275
		return ret;
	}

276
	pmd = (pmd_t *)hpa2va(hpa);
277 278 279 280 281

	/*
	 * Free all present page tables
	 */
	for (i = 0; i < PTRS_PER_PMD; i++) {
Charlie Jacobsen's avatar
Charlie Jacobsen committed
282 283 284 285 286 287
		if (pmd_present(pmd[i])) {
			ret = lcd_mm_pt_destroy(lcd, &pmd[i]);
			if (ret) {
				printk(KERN_ERR "lcd_mm_pmd_destroy: error destroying child pt\n");
				return ret;
			}
Charles Jacobsen's avatar
Charles Jacobsen committed
288
		}
289 290 291 292 293
	}

	/*
	 * Unmap pmd
	 */
294
	ret = lcd_arch_ept_unmap_range(lcd->lcd_arch, gpa, 1);
295 296 297 298
	if (ret) {
		printk(KERN_ERR "lcd_mm_pmd_destroy: error unmapping pmd\n");
		return ret;
	}
299 300 301 302

	/*
	 * Free pmd
	 */
303
	free_page((unsigned long)pmd);
304 305

	return 0;
306 307
}

308
static int lcd_mm_pud_destroy(struct lcd *lcd, pgd_t *pgd_entry)
309 310
{
	pud_t *pud;
311 312
	gpa_t gpa;
	hpa_t hpa;
313
	int i;
314
	int ret;
315 316 317 318

	/*
	 * Get hpa of pud, using gpa stored in pgd_entry.
	 */
319
	gpa = pgd_gpa(pgd_entry);
Charlie Jacobsen's avatar
Charlie Jacobsen committed
320
	ret = lcd_arch_ept_gpa_to_hpa(lcd->lcd_arch, gpa, &hpa);
321 322
	if (ret) {
		printk(KERN_ERR "lcd_mm_pud_destroy: error looking up gpa %lx\n",
323
			gpa_val(gpa));
324 325 326
		return ret;
	}

327
	pud = (pud_t *)hpa2va(hpa);
328 329 330 331 332

	/*
	 * Destroy all present pmd's
	 */
	for (i = 0; i < PTRS_PER_PUD; i++) {
Charlie Jacobsen's avatar
Charlie Jacobsen committed
333 334 335 336 337 338
		if (pud_present(pud[i])) {
			ret = lcd_mm_pmd_destroy(lcd, &pud[i]);
			if (ret) {
				printk(KERN_ERR "lcd_mm_pud_destroy: error destroying child pmd\n");
				return ret;
			}
Charles Jacobsen's avatar
Charles Jacobsen committed
339
		}
340 341 342 343 344
	}

	/*
	 * Unmap pud
	 */
345
	ret = lcd_arch_ept_unmap_range(lcd->lcd_arch, gpa, 1);
346 347 348 349

	/*
	 * Free pud
	 */
350
	free_page((unsigned long)pud);
351 352

	return 0;
353 354
}

355 356 357 358 359 360 361 362 363
/**
 * Unmaps guest virtual paging structures in lcd's ept, and
 * frees host physical memory associated with paging structures.
 *
 * Note! Does not free host physical memory mapped (via guest
 * physical addresses) by gva; just the paging structures themselves.
 */
static void lcd_mm_gva_destroy(struct lcd *lcd)
{
364 365 366 367
	pgd_t *pgd;
	int i;
	int ret;

368
	pgd = lcd->gv.root;
369
	
370 371 372 373 374
	/*
	 * Free all present pud's
	 */
	for (i = 0; i < PTRS_PER_PGD; i++) {
		if (pgd_present(pgd[i])) {
375
			ret = lcd_mm_pud_destroy(lcd, &pgd[i]);
376 377 378 379 380
			if (ret) {
				printk(KERN_ERR "lcd_mm_gva_destroy: error freeing pud at idx %d\n",
					i);
				return;
			}
381
		}
382 383 384 385 386
	}

	/*
	 * Unmap in ept
	 */
387 388
	ret = lcd_arch_ept_unmap_range(lcd->lcd_arch, 
				lcd->gv.paging_mem_bot, 1);
389 390 391 392 393 394 395 396
	if (ret) {
		printk(KERN_ERR "lcd_mm_gva_destroy: error unmapping pgd\n");
		return;
	}

	/*
	 * Free pgd
	 */
397 398
	free_page((unsigned long)pgd);
	lcd->gv.root = NULL;
399 400 401 402 403

	/*
	 * Mark as invalid
	 */
	lcd->gv.present = 0;
404 405
}

406 407 408 409 410

/**
 * Get host virtual address of pte
 * for gva and pmd_entry.
 */
411
static int lcd_mm_gva_lookup_pte(struct lcd *lcd, gva_t gva, pmd_t *pmd_entry,
412 413 414
				pte_t **pte_out)
{
	int ret;
415 416
	gpa_t gpa;
	hpa_t hpa;
417 418 419 420 421
	pte_t *entry;

	/*
	 * Get hpa of page table, using gpa stored in pmd_entry.
	 */
422
	gpa = pmd_gpa(pmd_entry);
Charles Jacobsen's avatar
Charles Jacobsen committed
423
	ret = lcd_arch_ept_gpa_to_hpa(lcd->lcd_arch, gpa, &hpa);
424
	if (ret) {
425
		printk(KERN_ERR "lcd_mm_gva_lookup_pte: error looking up gpa %lx\n",
426
			gpa_val(gpa));
427 428 429 430 431
		return ret;
	}
	/*
	 * Look up entry in page table
	 */
432
	entry = ((pte_t *)hpa2va(hpa)) + pte_index(gva_val(gva));
433 434 435 436 437 438 439 440 441
	
	*pte_out = entry;
	return 0;
}

/**
 * Look up pte for the page frame containing gva,
 * using the page table referenced by pmd_entry.
 */
442
static int lcd_mm_gva_walk_pt(struct lcd *lcd, gva_t gva, pmd_t *pmd_entry,
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
			pte_t **pte_out)
{
	int ret;
	pte_t *entry;

	ret = lcd_mm_gva_lookup_pte(lcd, gva, pmd_entry, &entry);
	if (ret) {
		printk(KERN_ERR "lcd_mm_gva_walk_pt: error looking up pte\n");
		return ret;
	}

	*pte_out = entry;

	return 0;
}

/**
 * Get host virtual address of pmd entry
 * for gva and pud_entry.
 */
463
static int lcd_mm_gva_lookup_pmd(struct lcd *lcd, gva_t gva, pud_t *pud_entry,
464 465 466
				pmd_t **pmd_out)
{
	int ret;
467 468
	gpa_t gpa;
	hpa_t hpa;
469 470 471 472 473
	pmd_t *entry;

	/*
	 * Get hpa of pmd, using gpa stored in pud_entry.
	 */
474
	gpa = pud_gpa(pud_entry);
Charles Jacobsen's avatar
Charles Jacobsen committed
475
	ret = lcd_arch_ept_gpa_to_hpa(lcd->lcd_arch, gpa, &hpa);
476 477
	if (ret) {
		printk(KERN_ERR "lcd_mm_gva_lookup_pmd: error looking up gpa %lx\n",
478
			gpa_val(gpa));
479 480 481 482 483
		return ret;
	}
	/*
	 * Look up entry in pmd
	 */
484
	entry = ((pmd_t *)hpa2va(hpa)) + pmd_index(gva_val(gva));
485 486 487 488 489 490 491 492 493
	
	*pmd_out = entry;
	return 0;
}

/**
 * Look up pmd entry for the page table for gva,
 * using the pmd referenced by pud_entry.
 */
494
static int lcd_mm_gva_walk_pmd(struct lcd *lcd, gva_t gva, pud_t *pud_entry,
495 496 497 498
				pmd_t **pmd_out)
{
	int ret;
	pmd_t *entry;
499 500
	gpa_t gpa;
	hpa_t hpa;
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520

	ret = lcd_mm_gva_lookup_pmd(lcd, gva, pud_entry, &entry);
	if (ret) {
		printk(KERN_ERR "lcd_mm_gva_walk_pmd: error looking up pmd\n");
		return ret;
	}

	if (!pmd_present(*entry)) {
		/*
		 * Alloc and map a page table
		 */
		ret = lcd_mm_gva_alloc(lcd, &gpa, &hpa);
		if (ret) {
			printk(KERN_ERR "lcd_mm_gva_walk_pmd: error alloc'ing\n");
			return ret;
		}

		/*
		 * Map *guest physical* address into pud entry
		 */
521 522
		set_pmd_gpa(entry, gpa);

523 524 525 526 527 528 529 530 531 532 533 534

	}

	*pmd_out = entry;

	return 0;
}

/**
 * Get host virtual address of pud entry
 * for gva and pgd_entry.
 */
535
static int lcd_mm_gva_lookup_pud(struct lcd *lcd, gva_t gva, pgd_t *pgd_entry,
536 537 538
				pud_t **pud_out)
{
	int ret;
539 540
	gpa_t gpa;
	hpa_t hpa;
541 542 543 544 545
	pud_t *entry;

	/*
	 * Get hpa of pud, using gpa stored in pgd_entry.
	 */
546
	gpa = pgd_gpa(pgd_entry);
Charles Jacobsen's avatar
Charles Jacobsen committed
547
	ret = lcd_arch_ept_gpa_to_hpa(lcd->lcd_arch, gpa, &hpa);
548 549
	if (ret) {
		printk(KERN_ERR "lcd_mm_gva_lookup_pud: error looking up gpa %lx\n",
550
			gpa_val(gpa));
551 552 553 554 555
		return ret;
	}
	/*
	 * Look up entry in pud
	 */
556
	entry = ((pud_t *)hpa2va(hpa)) + pud_index(gva_val(gva));
557 558 559 560 561 562 563 564 565
	
	*pud_out = entry;
	return 0;
}

/**
 * Look up pud entry for the pmd for gva, using
 * the pud referenced by pgd_entry.
 */
566
static int lcd_mm_gva_walk_pud(struct lcd *lcd, gva_t gva, pgd_t *pgd_entry,
567 568 569 570
				pud_t **pud_out)
{
	int ret;
	pud_t *entry;
571 572
	gpa_t gpa;
	hpa_t hpa;
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592

	ret = lcd_mm_gva_lookup_pud(lcd, gva, pgd_entry, &entry);
	if (ret) {
		printk(KERN_ERR "lcd_mm_gva_walk_pud: error looking up pud\n");
		return ret;
	}

	if (!pud_present(*entry)) {
		/*
		 * Alloc and map a pmd
		 */
		ret = lcd_mm_gva_alloc(lcd, &gpa, &hpa);
		if (ret) {
			printk(KERN_ERR "lcd_mm_gva_walk_pud: error alloc'ing\n");
			return ret;
		}

		/*
		 * Map *guest physical* address into pud entry
		 */
593
		set_pud_gpa(entry, gpa);
594 595 596 597 598 599 600 601 602 603
	}

	*pud_out = entry;

	return 0;
}

/**
 * Look up pgd entry for the pud for gva.
 */
604
static int lcd_mm_gva_walk_pgd(struct lcd *lcd, gva_t gva, pgd_t **pgd_out)
605 606 607
{
	int ret;
	pgd_t *entry;
608 609
	gpa_t gpa;
	hpa_t hpa;
610

611
	entry = lcd->gv.root + pgd_index(gva_val(gva));
612 613 614 615 616 617
	if (!pgd_present(*entry)) {
		/*
		 * Alloc and map a pud
		 */
		ret = lcd_mm_gva_alloc(lcd, &gpa, &hpa);
		if (ret) {
618
			printk(KERN_ERR "lcd_mm_gva_walk_pgd: error alloc'ing\n");
619 620
			return ret;
		}
Charles Jacobsen's avatar
Charles Jacobsen committed
621

622 623 624
		/*
		 * Map *guest physical* address into pgd entry
		 */
625
		set_pgd_gpa(entry, gpa);
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
	}

	*pgd_out = entry;

	return 0;
}

/**
 * Look up the page table entry for guest virtual
 * address gva, using the pgd pointed to by root_hva.
 *
 * Paging data structures are allocated along the
 * way.
 *
 * Hierarchy: pgd -> pud -> pmd -> page table -> page frame
 *
642 643 644 645 646 647 648 649 650 651 652 653
 * For concreteness, on Intel 64-bit, IA-32e paging
 * is used, and
 *
 *    pgd = pml4
 *    pud = pdpt
 *    pmd = page directory
 *
 * with the `standard' 512 entries per paging structure.
 * (No huge pages are used for the initial guest
 * virtual address space. But the intra-lcd code can
 * change that if it wishes.)
 *
654 655 656 657 658 659 660 661 662
 * Since guest physical addresses (rather than 
 * host physical addresses) are stored in the paging
 * structures, we can't use some of the most benefical
 * macros that allow for pud- and pmd-folding
 * (e.g., pud_offset). C'est la vie ... We could define
 * some macros that do the same thing, later ...
 *
 * Punchline: Arch must have 4 paging levels.
 */
663
static int lcd_mm_gva_walk(struct lcd *lcd, gva_t gva, pte_t **pte_out)
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
{
	int ret;
	pgd_t *pgd;
	pud_t *pud;
	pmd_t *pmd;

	/*
	 * Get pgd entry for pud
	 */
	ret = lcd_mm_gva_walk_pgd(lcd, gva, &pgd);
	if (ret) {
		printk(KERN_ERR "lcd_mm_gva_walk: err walking pgd\n");
		return ret;
	}

	/*
	 * Get pud entry for pmd
	 */
	ret = lcd_mm_gva_walk_pud(lcd, gva, pgd, &pud);
	if (ret) {
		printk(KERN_ERR "lcd_mm_gva_walk: err walking pud\n");
		return ret;
	}

	/*
	 * Get pmd entry for page table
	 */
	ret = lcd_mm_gva_walk_pmd(lcd, gva, pud, &pmd);
	if (ret) {
		printk(KERN_ERR "lcd_mm_gva_walk: err walking pmd\n");
		return ret;
	}

	/*
	 * Finally, get page table entry
	 */
	return lcd_mm_gva_walk_pt(lcd, gva, pmd, pte_out);
}

703
static void lcd_mm_gva_set(pte_t *pte, gpa_t gpa)
704
{
705
	set_pte_gpa(pte, gpa);
Charlie Jacobsen's avatar
Charlie Jacobsen committed
706
}
707

Charlie Jacobsen's avatar
Charlie Jacobsen committed
708 709 710
static void lcd_mm_gva_unset(pte_t *pte)
{
	*pte = __pte(0);
711 712
}

713
static gpa_t lcd_mm_gva_get(pte_t *pte)
Charles Jacobsen's avatar
Charles Jacobsen committed
714
{
715
	return pte_gpa(pte);
Charles Jacobsen's avatar
Charles Jacobsen committed
716
}
717

718 719 720 721
/**
 * Simple routine combining walk and set. Never
 * overwrites.
 */
722
static int lcd_mm_gva_map(struct lcd *lcd, gva_t gva, gpa_t gpa)
723 724 725 726 727 728 729 730 731 732 733
{
	int ret;
	pte_t *pte;

	ret = lcd_mm_gva_walk(lcd, gva, &pte);
	if (ret) {
		printk(KERN_ERR "lcd_mm_gva_map: error getting pte\n");
		return ret;
	}

	if (pte_present(*pte)) {
734
		printk(KERN_ERR "lcd_mm_gva_map: remap gva %lx to gpa %lx (was %lx)\n",
735 736
			gva_val(gva), gpa_val(gpa),
			gpa_val(lcd_mm_gva_get(pte)));
737 738 739 740 741 742 743 744
		return -EINVAL;
	}

	lcd_mm_gva_set(pte, gpa);

	return 0;
}

Charlie Jacobsen's avatar
Charlie Jacobsen committed
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
/**
 * Simple routine combining walk and set. Never
 * overwrites.
 */
static int lcd_mm_gva_unmap(struct lcd *lcd, gva_t gva)
{
	int ret;
	pte_t *pte;

	ret = lcd_mm_gva_walk(lcd, gva, &pte);
	if (ret) {
		printk(KERN_ERR "lcd_mm_gva_unmap: error getting pte\n");
		return ret;
	}

	if (!pte_present(*pte)) {
		printk(KERN_ERR "lcd_mm_gva_unmap: no mapping for gva %lx\n",
			gva_val(gva));
		return -EINVAL;
	}

	lcd_mm_gva_unset(pte);

	return 0;
}

771 772 773 774
/**
 * Simple routine combining walk and get. Never
 * overwrites.
 */
775
static int lcd_mm_gva_to_gpa(struct lcd *lcd, gva_t gva, gpa_t *gpa)
776 777 778 779 780 781 782 783 784 785
{
	int ret;
	pte_t *pte;

	ret = lcd_mm_gva_walk(lcd, gva, &pte);
	if (ret) {
		printk(KERN_ERR "lcd_mm_gva_to_gpa: error getting pte\n");
		return ret;
	}

786
	*gpa = lcd_mm_gva_get(pte);
787 788 789

	return 0;
}
790

791 792 793 794 795 796 797 798 799 800 801 802 803
/**
 * Maps 
 *
 *    gva_start --> gva_start + npages * PAGE_SIZE
 *
 * to
 *
 *    gpa_start --> gpa_start + npages * PAGE_SIZE
 *
 * in lcd's guest virtual paging tables.
 *
 * Note! Call lcd_mm_gva_init before mapping any gva's.
 */
804 805
static int lcd_mm_gva_map_range(struct lcd *lcd, gva_t gva_start, 
				gpa_t gpa_start, unsigned long npages)
806
{
Charlie Jacobsen's avatar
Charlie Jacobsen committed
807 808
	unsigned long off;
	unsigned long len;
809 810 811

	len = npages * PAGE_SIZE;
	for (off = 0; off < len; off += PAGE_SIZE) {
812
		if (lcd_mm_gva_map(lcd,
813
					/* gva */
814
					gva_add(gva_start, off),
815
					/* gpa */
816
					gpa_add(gpa_start, off))) {
817
			printk(KERN_ERR "lcd_mm_gva_map_range: error mapping gva %lx to gpa %lx\n",
818 819
				gva_val(gva_add(gva_start,off)),
				gpa_val(gpa_add(gpa_start,off)));
820 821 822 823 824
			return -EIO;
		}
	}

	return 0;
825 826
}

Charlie Jacobsen's avatar
Charlie Jacobsen committed
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
/**
 * Unmaps 
 *
 *    gva_start --> gva_start + npages * PAGE_SIZE
 *
 * in lcd's guest virtual paging tables.
 */
static int lcd_mm_gva_unmap_range(struct lcd *lcd, gva_t gva_start, 
				unsigned long npages)
{
	unsigned long off;
	unsigned long len;

	len = npages * PAGE_SIZE;
	for (off = 0; off < len; off += PAGE_SIZE) {
		if (lcd_mm_gva_unmap(lcd, gva_add(gva_start, off))) {
			printk(KERN_ERR "lcd_mm_gva_unmap_range: error unmapping gva %lx\n",
				gva_val(gva_add(gva_start,off)));
			return -EIO;
		}
	}

	return 0;
}

852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
/* lcd create / destroy ---------------------------------------- */

static int lcd_create(struct lcd **lcd_out)
{
	struct lcd *lcd;
	int r;

	/*
	 * Alloc and init lcd
	 */
	lcd = (struct lcd *)kmalloc(sizeof(*lcd), GFP_KERNEL);
	if (!lcd) {
		printk(KERN_ERR "lcd_create: error alloc'ing lcd\n");
		r = -ENOMEM;
		goto fail1;
	}
868
	memset(lcd, 0, sizeof(*lcd)); /* sets status to unformed */
869

870 871 872 873 874 875 876 877 878
	lcd->lcd_arch = lcd_arch_create();
	if(!lcd->lcd_arch) {
		printk(KERN_ERR "lcd_create: error creating lcd_arch\n");
		r = -ENOMEM;
		goto fail2;
	}

	*lcd_out = lcd;

Charles Jacobsen's avatar
Charles Jacobsen committed
879 880
	return 0;

881 882 883 884 885 886 887 888 889
fail2:
	kfree(lcd);
fail1:
	return r;

}

static void lcd_destroy(struct lcd *lcd)
{
890 891 892 893 894 895
	/*
	 * Order is important ...
	 */
	if (lcd->gv.present)
		lcd_mm_gva_destroy(lcd);
	lcd_arch_destroy(lcd->lcd_arch);
896 897 898 899
	kfree(lcd);
}


900
/* BLOBs -------------------------------------------------- */
901 902 903 904

static int lcd_do_run_blob_once(struct lcd *lcd)
{
	int r;
905
	int syscall_id;
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927

	r = lcd_arch_run(lcd->lcd_arch);
	if (r < 0) {
		printk(KERN_ERR "lcd_do_run_blob_once: error running blob\n");
		goto out;
	}

	switch(r) {
	case LCD_ARCH_STATUS_PAGE_FAULT:
		/*
		 * Paging shouldn't be needed for blob (blob shouldn't
		 * use instructions that access memory, for now)
		 */
		printk(KERN_ERR "lcd_run_blob: page fault\n");
		r = -EIO;
		goto out;
		break;
	case LCD_ARCH_STATUS_EXT_INTR:
		/*
		 * Continue
		 */
		printk(KERN_ERR "lcd_run_blob: got external intr\n");
928
		r = -EIO;
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
		goto out;
	case LCD_ARCH_STATUS_EPT_FAULT:
		/*
		 * EPT should everything mapped the blob needs, so
		 * there's a problem. Quit.
		 */
		printk(KERN_ERR "lcd_run_blob: ept fault\n");
		r = -EIO;
		goto out;
	case LCD_ARCH_STATUS_CR3_ACCESS:
		/*
		 * %cr3 shouldn't be accessed for simple blobs (for
		 * now). Quit.
		 */
		printk(KERN_ERR "lcd_run_blob: cr3 access\n");
		r = -EIO;
		goto out;
	case LCD_ARCH_STATUS_SYSCALL:
		/*
		 * Only allow yield syscalls for now
		 */
		syscall_id = LCD_ARCH_GET_SYSCALL_NUM(lcd->lcd_arch);
		printk(KERN_ERR "lcd_run_blob: handling syscall %d\n",
			syscall_id);
		if (syscall_id != LCD_SYSCALL_YIELD) {
			printk(KERN_ERR "lcd_run_blob: unexpected syscall id %d\n",
				syscall_id);
			r = -EIO;
			goto out;
		} else {
			printk(KERN_ERR "lcd_run_blob: lcd yielded, exiting lcd...\n");
960
			r = -EIO;
961 962 963 964 965 966 967 968 969 970 971
			goto out;
		}
	}

out:
	return r;
}

static int lcd_do_run_blob(struct lcd *lcd)
{
	int r;
972

973 974 975 976 977 978 979
	while (1) {
		r = lcd_do_run_blob_once(lcd);
		if (r)
			return r;
	}
}

980 981
static int lcd_init_blob(struct lcd *lcd, unsigned char *blob,
			unsigned int blob_order)
982
{
983
	int r;
984 985
	unsigned long paging_mem_size;
	unsigned long npages;
986

987
	/*
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
	 * (initial)
	 * Blob Memory Layout
	 * ==================
	 *
	 * The layout below reflects the guest physical *and* virtual memory
	 * layout with the exception that not all paging memory is mapped in
	 * in the guest physical address space (for efficiency). 
	 *
	 * Guest physical addresses are mapped one-to-one to the same guest 
	 * virtual addresses.
	 *
	 * All allocated guest physical memory--including the arch-dependent 
	 * region, guest virtual page tables, and the lcd's code--is mapped
	 * in the guest virtual address space.
	 *
	 *                   +---------------------------+
	 *                   |                           |
	 *                   :                           :
	 *                   :      Free / Unmapped      :
	 *                   |                           |
	 *                   +---------------------------+
	 *                   |           Blob            | (max 16 pgs)	 
	 * blob entry------> +---------------------------+ 
	 *                   |       Guest Virtual       | (4 MBs)
	 *                   |       Paging Memory       |
	 * LCD_ARCH_FREE---> +---------------------------+
	 *                   |                           |
	 *                   :   Reserved Arch Memory    :
	 *                   |                           |
	 *                   +---------------------------+ 0x0000 0000 0000 0000
1018
	 */
1019 1020 1021 1022 1023 1024 1025
	
	paging_mem_size = 4 * (1 << 20); /* 4 MBs */

	/*
	 * Initialize guest virtual paging
	 */
	r = lcd_mm_gva_init(lcd, LCD_ARCH_FREE, 
Charles Jacobsen's avatar
Charles Jacobsen committed
1026
			gpa_add(LCD_ARCH_FREE, paging_mem_size));
1027 1028 1029 1030 1031 1032 1033 1034
	if (r) {
		printk(KERN_ERR "lcd_init_blob: error setting up gva\n");
		goto fail1;
	}

	/*
	 * Map blob in guest physical, after paging mem
	 */
1035
	r = lcd_arch_ept_map_range(lcd->lcd_arch, 
Charles Jacobsen's avatar
Charles Jacobsen committed
1036
				gpa_add(LCD_ARCH_FREE, paging_mem_size), 
1037 1038
				va2hpa(blob),
				(1 << blob_order));
1039 1040 1041
	if (r) {
		printk(KERN_ERR "lcd_init_blob: error mapping blob in gpa\n");
		goto fail2;
1042 1043 1044
	}

	/*
1045
	 * Map gpa from 0 to top of blob in lcd's gva
1046
	 */
Charles Jacobsen's avatar
Charles Jacobsen committed
1047
	npages = (gpa_val(LCD_ARCH_FREE) + paging_mem_size) >> PAGE_SHIFT;
1048
	npages += (1 << blob_order);
1049
	r = lcd_mm_gva_map_range(lcd, 
1050 1051 1052 1053 1054 1055
				/* gva start */
				__gva(0), 
				/* gpa start */
				__gpa(0), 
				/* num pages */
				npages);
1056 1057 1058 1059 1060 1061 1062 1063 1064
	if (r) {
		printk(KERN_ERR "lcd_init_blob: error setting up initial gva\n");
		goto fail3;
	}

	/*
	 * Initialize program counter to blob entry point (just after
	 * guest virtual paging mem).
	 */
1065
	r = lcd_arch_set_pc(lcd->lcd_arch, 
1066 1067
			__gva(gpa_val(gpa_add(LCD_ARCH_FREE, 
							paging_mem_size))));
1068 1069 1070
	if (r) {
		printk(KERN_ERR "lcd_init_blob: error setting prgm counter\n");
		goto fail4;
1071
	}
1072

1073 1074 1075 1076 1077 1078
	r = lcd_do_run_blob(lcd);
	if (r) {
		printk(KERN_ERR "lcd_init_blob: error running blob: err %d\n",
			r);
		goto fail4;
	}
1079

1080 1081 1082
	r = 0;
	goto done;
done:
1083
fail4:
Charles Jacobsen's avatar
Charles Jacobsen committed
1084
	lcd_mm_gva_unmap_range(lcd, __gva(0), npages);
1085
fail3:
1086
	lcd_arch_ept_unmap_range(lcd->lcd_arch, 
Charles Jacobsen's avatar
Charles Jacobsen committed
1087
				gpa_add(LCD_ARCH_FREE, paging_mem_size), 
1088
				(1 << blob_order));
1089
fail2:
1090
	lcd_mm_gva_destroy(lcd);
1091 1092
fail1:
	return r;
1093 1094 1095 1096 1097 1098 1099
}

static int lcd_run_blob(struct lcd_blob_info *bi)
{
	struct lcd *lcd;
	int r;
	unsigned char *blob;
1100

1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
	/*
	 * Sanity check blob order
	 */
	if (bi->blob_order > 4) {
		printk(KERN_ERR "lcd_run_blob: blob is bigger than 16 pgs\n");
		r = -EINVAL;
		goto fail1;
	}
	
	/*
	 * Load blob mem
	 */
	blob = (unsigned char *)__get_free_pages(GFP_KERNEL, bi->blob_order);
	if (!blob) {
		printk(KERN_ERR "lcd_run_blob: couldn't alloc mem for blob\n");
		r = -ENOMEM;
		goto fail2;
	}

	/*
	 * Copy blob
	 */
1123
	r = copy_from_user(blob, (void __user *)bi->blob, 
1124
			(1 << bi->blob_order) * PAGE_SIZE);
1125 1126 1127 1128 1129 1130 1131 1132
	if (r) {
		printk(KERN_ERR "lcd_run_blob: error copying blob\n");
		goto fail3;
	}

	/*
	 * Alloc and init lcd
	 */
1133 1134 1135
	r = lcd_create(&lcd);
	if (r) {
		printk(KERN_ERR "lcd_run_blob: error creating lcd\n");
1136 1137 1138 1139
		goto fail4;
	}

	/*
1140
	 * Initialize lcd for blob
1141
	 */
1142
	r = lcd_init_blob(lcd, blob, bi->blob_order);
1143 1144 1145
	if (r) {
		printk(KERN_ERR "lcd_run_blob: error loading blob in lcd\n");
		r = -EIO;
1146
		goto fail5;
1147 1148 1149
	}


1150 1151
	r = 0;
	goto done;
1152 1153
done:
fail5:
1154
	lcd_destroy(lcd);
1155 1156
fail4:
fail3:
1157
	free_pages((unsigned long)blob, bi->blob_order);
1158 1159 1160 1161 1162
fail2:
fail1:
	return r;
}

1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571