paging_tmpl.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /*
  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. * We need the mmu code to access both 32-bit and 64-bit guest ptes,
  21. * so the code in this file is compiled twice, once per pte size.
  22. */
  23. #if PTTYPE == 64
  24. #define pt_element_t u64
  25. #define guest_walker guest_walker64
  26. #define FNAME(name) paging##64_##name
  27. #define PT_BASE_ADDR_MASK PT64_BASE_ADDR_MASK
  28. #define PT_DIR_BASE_ADDR_MASK PT64_DIR_BASE_ADDR_MASK
  29. #define PT_INDEX(addr, level) PT64_INDEX(addr, level)
  30. #define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
  31. #define PT_LEVEL_MASK(level) PT64_LEVEL_MASK(level)
  32. #ifdef CONFIG_X86_64
  33. #define PT_MAX_FULL_LEVELS 4
  34. #else
  35. #define PT_MAX_FULL_LEVELS 2
  36. #endif
  37. #elif PTTYPE == 32
  38. #define pt_element_t u32
  39. #define guest_walker guest_walker32
  40. #define FNAME(name) paging##32_##name
  41. #define PT_BASE_ADDR_MASK PT32_BASE_ADDR_MASK
  42. #define PT_DIR_BASE_ADDR_MASK PT32_DIR_BASE_ADDR_MASK
  43. #define PT_INDEX(addr, level) PT32_INDEX(addr, level)
  44. #define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
  45. #define PT_LEVEL_MASK(level) PT32_LEVEL_MASK(level)
  46. #define PT_MAX_FULL_LEVELS 2
  47. #else
  48. #error Invalid PTTYPE value
  49. #endif
  50. /*
  51. * The guest_walker structure emulates the behavior of the hardware page
  52. * table walker.
  53. */
  54. struct guest_walker {
  55. int level;
  56. gfn_t table_gfn[PT_MAX_FULL_LEVELS];
  57. pt_element_t *table;
  58. pt_element_t *ptep;
  59. pt_element_t inherited_ar;
  60. gfn_t gfn;
  61. u32 error_code;
  62. };
  63. /*
  64. * Fetch a guest pte for a guest virtual address
  65. */
  66. static int FNAME(walk_addr)(struct guest_walker *walker,
  67. struct kvm_vcpu *vcpu, gva_t addr,
  68. int write_fault, int user_fault, int fetch_fault)
  69. {
  70. hpa_t hpa;
  71. struct kvm_memory_slot *slot;
  72. pt_element_t *ptep;
  73. pt_element_t root;
  74. gfn_t table_gfn;
  75. pgprintk("%s: addr %lx\n", __FUNCTION__, addr);
  76. walker->level = vcpu->mmu.root_level;
  77. walker->table = NULL;
  78. root = vcpu->cr3;
  79. #if PTTYPE == 64
  80. if (!is_long_mode(vcpu)) {
  81. walker->ptep = &vcpu->pdptrs[(addr >> 30) & 3];
  82. root = *walker->ptep;
  83. if (!(root & PT_PRESENT_MASK))
  84. goto not_present;
  85. --walker->level;
  86. }
  87. #endif
  88. table_gfn = (root & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
  89. walker->table_gfn[walker->level - 1] = table_gfn;
  90. pgprintk("%s: table_gfn[%d] %lx\n", __FUNCTION__,
  91. walker->level - 1, table_gfn);
  92. slot = gfn_to_memslot(vcpu->kvm, table_gfn);
  93. hpa = safe_gpa_to_hpa(vcpu, root & PT64_BASE_ADDR_MASK);
  94. walker->table = kmap_atomic(pfn_to_page(hpa >> PAGE_SHIFT), KM_USER0);
  95. ASSERT((!is_long_mode(vcpu) && is_pae(vcpu)) ||
  96. (vcpu->cr3 & ~(PAGE_MASK | CR3_FLAGS_MASK)) == 0);
  97. walker->inherited_ar = PT_USER_MASK | PT_WRITABLE_MASK;
  98. for (;;) {
  99. int index = PT_INDEX(addr, walker->level);
  100. hpa_t paddr;
  101. ptep = &walker->table[index];
  102. ASSERT(((unsigned long)walker->table & PAGE_MASK) ==
  103. ((unsigned long)ptep & PAGE_MASK));
  104. if (!is_present_pte(*ptep))
  105. goto not_present;
  106. if (write_fault && !is_writeble_pte(*ptep))
  107. if (user_fault || is_write_protection(vcpu))
  108. goto access_error;
  109. if (user_fault && !(*ptep & PT_USER_MASK))
  110. goto access_error;
  111. #if PTTYPE == 64
  112. if (fetch_fault && is_nx(vcpu) && (*ptep & PT64_NX_MASK))
  113. goto access_error;
  114. #endif
  115. if (!(*ptep & PT_ACCESSED_MASK)) {
  116. mark_page_dirty(vcpu->kvm, table_gfn);
  117. *ptep |= PT_ACCESSED_MASK;
  118. }
  119. if (walker->level == PT_PAGE_TABLE_LEVEL) {
  120. walker->gfn = (*ptep & PT_BASE_ADDR_MASK)
  121. >> PAGE_SHIFT;
  122. break;
  123. }
  124. if (walker->level == PT_DIRECTORY_LEVEL
  125. && (*ptep & PT_PAGE_SIZE_MASK)
  126. && (PTTYPE == 64 || is_pse(vcpu))) {
  127. walker->gfn = (*ptep & PT_DIR_BASE_ADDR_MASK)
  128. >> PAGE_SHIFT;
  129. walker->gfn += PT_INDEX(addr, PT_PAGE_TABLE_LEVEL);
  130. break;
  131. }
  132. walker->inherited_ar &= walker->table[index];
  133. table_gfn = (*ptep & PT_BASE_ADDR_MASK) >> PAGE_SHIFT;
  134. paddr = safe_gpa_to_hpa(vcpu, *ptep & PT_BASE_ADDR_MASK);
  135. kunmap_atomic(walker->table, KM_USER0);
  136. walker->table = kmap_atomic(pfn_to_page(paddr >> PAGE_SHIFT),
  137. KM_USER0);
  138. --walker->level;
  139. walker->table_gfn[walker->level - 1 ] = table_gfn;
  140. pgprintk("%s: table_gfn[%d] %lx\n", __FUNCTION__,
  141. walker->level - 1, table_gfn);
  142. }
  143. walker->ptep = ptep;
  144. pgprintk("%s: pte %llx\n", __FUNCTION__, (u64)*ptep);
  145. return 1;
  146. not_present:
  147. walker->error_code = 0;
  148. goto err;
  149. access_error:
  150. walker->error_code = PFERR_PRESENT_MASK;
  151. err:
  152. if (write_fault)
  153. walker->error_code |= PFERR_WRITE_MASK;
  154. if (user_fault)
  155. walker->error_code |= PFERR_USER_MASK;
  156. if (fetch_fault)
  157. walker->error_code |= PFERR_FETCH_MASK;
  158. return 0;
  159. }
  160. static void FNAME(release_walker)(struct guest_walker *walker)
  161. {
  162. if (walker->table)
  163. kunmap_atomic(walker->table, KM_USER0);
  164. }
  165. static void FNAME(mark_pagetable_dirty)(struct kvm *kvm,
  166. struct guest_walker *walker)
  167. {
  168. mark_page_dirty(kvm, walker->table_gfn[walker->level - 1]);
  169. }
  170. static void FNAME(set_pte_common)(struct kvm_vcpu *vcpu,
  171. u64 *shadow_pte,
  172. gpa_t gaddr,
  173. pt_element_t *gpte,
  174. u64 access_bits,
  175. int user_fault,
  176. int write_fault,
  177. int *ptwrite,
  178. struct guest_walker *walker,
  179. gfn_t gfn)
  180. {
  181. hpa_t paddr;
  182. int dirty = *gpte & PT_DIRTY_MASK;
  183. u64 spte = *shadow_pte;
  184. int was_rmapped = is_rmap_pte(spte);
  185. pgprintk("%s: spte %llx gpte %llx access %llx write_fault %d"
  186. " user_fault %d gfn %lx\n",
  187. __FUNCTION__, spte, (u64)*gpte, access_bits,
  188. write_fault, user_fault, gfn);
  189. if (write_fault && !dirty) {
  190. *gpte |= PT_DIRTY_MASK;
  191. dirty = 1;
  192. FNAME(mark_pagetable_dirty)(vcpu->kvm, walker);
  193. }
  194. spte |= PT_PRESENT_MASK | PT_ACCESSED_MASK | PT_DIRTY_MASK;
  195. spte |= *gpte & PT64_NX_MASK;
  196. if (!dirty)
  197. access_bits &= ~PT_WRITABLE_MASK;
  198. paddr = gpa_to_hpa(vcpu, gaddr & PT64_BASE_ADDR_MASK);
  199. spte |= PT_PRESENT_MASK;
  200. if (access_bits & PT_USER_MASK)
  201. spte |= PT_USER_MASK;
  202. if (is_error_hpa(paddr)) {
  203. spte |= gaddr;
  204. spte |= PT_SHADOW_IO_MARK;
  205. spte &= ~PT_PRESENT_MASK;
  206. set_shadow_pte(shadow_pte, spte);
  207. return;
  208. }
  209. spte |= paddr;
  210. if ((access_bits & PT_WRITABLE_MASK)
  211. || (write_fault && !is_write_protection(vcpu) && !user_fault)) {
  212. struct kvm_mmu_page *shadow;
  213. spte |= PT_WRITABLE_MASK;
  214. if (user_fault) {
  215. mmu_unshadow(vcpu, gfn);
  216. goto unshadowed;
  217. }
  218. shadow = kvm_mmu_lookup_page(vcpu, gfn);
  219. if (shadow) {
  220. pgprintk("%s: found shadow page for %lx, marking ro\n",
  221. __FUNCTION__, gfn);
  222. access_bits &= ~PT_WRITABLE_MASK;
  223. if (is_writeble_pte(spte)) {
  224. spte &= ~PT_WRITABLE_MASK;
  225. kvm_arch_ops->tlb_flush(vcpu);
  226. }
  227. if (write_fault)
  228. *ptwrite = 1;
  229. }
  230. }
  231. unshadowed:
  232. if (access_bits & PT_WRITABLE_MASK)
  233. mark_page_dirty(vcpu->kvm, gaddr >> PAGE_SHIFT);
  234. set_shadow_pte(shadow_pte, spte);
  235. page_header_update_slot(vcpu->kvm, shadow_pte, gaddr);
  236. if (!was_rmapped)
  237. rmap_add(vcpu, shadow_pte);
  238. }
  239. static void FNAME(set_pte)(struct kvm_vcpu *vcpu, pt_element_t *gpte,
  240. u64 *shadow_pte, u64 access_bits,
  241. int user_fault, int write_fault, int *ptwrite,
  242. struct guest_walker *walker, gfn_t gfn)
  243. {
  244. access_bits &= *gpte;
  245. FNAME(set_pte_common)(vcpu, shadow_pte, *gpte & PT_BASE_ADDR_MASK,
  246. gpte, access_bits, user_fault, write_fault,
  247. ptwrite, walker, gfn);
  248. }
  249. static void FNAME(update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *page,
  250. u64 *spte, const void *pte, int bytes)
  251. {
  252. pt_element_t gpte;
  253. if (bytes < sizeof(pt_element_t))
  254. return;
  255. gpte = *(const pt_element_t *)pte;
  256. if (~gpte & (PT_PRESENT_MASK | PT_ACCESSED_MASK))
  257. return;
  258. pgprintk("%s: gpte %llx spte %p\n", __FUNCTION__, (u64)gpte, spte);
  259. FNAME(set_pte)(vcpu, &gpte, spte, PT_USER_MASK | PT_WRITABLE_MASK, 0,
  260. 0, NULL, NULL,
  261. (gpte & PT_BASE_ADDR_MASK) >> PAGE_SHIFT);
  262. }
  263. static void FNAME(set_pde)(struct kvm_vcpu *vcpu, pt_element_t *gpde,
  264. u64 *shadow_pte, u64 access_bits,
  265. int user_fault, int write_fault, int *ptwrite,
  266. struct guest_walker *walker, gfn_t gfn)
  267. {
  268. gpa_t gaddr;
  269. access_bits &= *gpde;
  270. gaddr = (gpa_t)gfn << PAGE_SHIFT;
  271. if (PTTYPE == 32 && is_cpuid_PSE36())
  272. gaddr |= (*gpde & PT32_DIR_PSE36_MASK) <<
  273. (32 - PT32_DIR_PSE36_SHIFT);
  274. FNAME(set_pte_common)(vcpu, shadow_pte, gaddr,
  275. gpde, access_bits, user_fault, write_fault,
  276. ptwrite, walker, gfn);
  277. }
  278. /*
  279. * Fetch a shadow pte for a specific level in the paging hierarchy.
  280. */
  281. static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
  282. struct guest_walker *walker,
  283. int user_fault, int write_fault, int *ptwrite)
  284. {
  285. hpa_t shadow_addr;
  286. int level;
  287. u64 *shadow_ent;
  288. u64 *prev_shadow_ent = NULL;
  289. pt_element_t *guest_ent = walker->ptep;
  290. if (!is_present_pte(*guest_ent))
  291. return NULL;
  292. shadow_addr = vcpu->mmu.root_hpa;
  293. level = vcpu->mmu.shadow_root_level;
  294. if (level == PT32E_ROOT_LEVEL) {
  295. shadow_addr = vcpu->mmu.pae_root[(addr >> 30) & 3];
  296. shadow_addr &= PT64_BASE_ADDR_MASK;
  297. --level;
  298. }
  299. for (; ; level--) {
  300. u32 index = SHADOW_PT_INDEX(addr, level);
  301. struct kvm_mmu_page *shadow_page;
  302. u64 shadow_pte;
  303. int metaphysical;
  304. gfn_t table_gfn;
  305. unsigned hugepage_access = 0;
  306. shadow_ent = ((u64 *)__va(shadow_addr)) + index;
  307. if (is_present_pte(*shadow_ent) || is_io_pte(*shadow_ent)) {
  308. if (level == PT_PAGE_TABLE_LEVEL)
  309. break;
  310. shadow_addr = *shadow_ent & PT64_BASE_ADDR_MASK;
  311. prev_shadow_ent = shadow_ent;
  312. continue;
  313. }
  314. if (level == PT_PAGE_TABLE_LEVEL)
  315. break;
  316. if (level - 1 == PT_PAGE_TABLE_LEVEL
  317. && walker->level == PT_DIRECTORY_LEVEL) {
  318. metaphysical = 1;
  319. hugepage_access = *guest_ent;
  320. hugepage_access &= PT_USER_MASK | PT_WRITABLE_MASK;
  321. hugepage_access >>= PT_WRITABLE_SHIFT;
  322. table_gfn = (*guest_ent & PT_BASE_ADDR_MASK)
  323. >> PAGE_SHIFT;
  324. } else {
  325. metaphysical = 0;
  326. table_gfn = walker->table_gfn[level - 2];
  327. }
  328. shadow_page = kvm_mmu_get_page(vcpu, table_gfn, addr, level-1,
  329. metaphysical, hugepage_access,
  330. shadow_ent);
  331. shadow_addr = __pa(shadow_page->spt);
  332. shadow_pte = shadow_addr | PT_PRESENT_MASK | PT_ACCESSED_MASK
  333. | PT_WRITABLE_MASK | PT_USER_MASK;
  334. *shadow_ent = shadow_pte;
  335. prev_shadow_ent = shadow_ent;
  336. }
  337. if (walker->level == PT_DIRECTORY_LEVEL) {
  338. FNAME(set_pde)(vcpu, guest_ent, shadow_ent,
  339. walker->inherited_ar, user_fault, write_fault,
  340. ptwrite, walker, walker->gfn);
  341. } else {
  342. ASSERT(walker->level == PT_PAGE_TABLE_LEVEL);
  343. FNAME(set_pte)(vcpu, guest_ent, shadow_ent,
  344. walker->inherited_ar, user_fault, write_fault,
  345. ptwrite, walker, walker->gfn);
  346. }
  347. return shadow_ent;
  348. }
  349. /*
  350. * Page fault handler. There are several causes for a page fault:
  351. * - there is no shadow pte for the guest pte
  352. * - write access through a shadow pte marked read only so that we can set
  353. * the dirty bit
  354. * - write access to a shadow pte marked read only so we can update the page
  355. * dirty bitmap, when userspace requests it
  356. * - mmio access; in this case we will never install a present shadow pte
  357. * - normal guest page fault due to the guest pte marked not present, not
  358. * writable, or not executable
  359. *
  360. * Returns: 1 if we need to emulate the instruction, 0 otherwise, or
  361. * a negative value on error.
  362. */
  363. static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr,
  364. u32 error_code)
  365. {
  366. int write_fault = error_code & PFERR_WRITE_MASK;
  367. int user_fault = error_code & PFERR_USER_MASK;
  368. int fetch_fault = error_code & PFERR_FETCH_MASK;
  369. struct guest_walker walker;
  370. u64 *shadow_pte;
  371. int write_pt = 0;
  372. int r;
  373. pgprintk("%s: addr %lx err %x\n", __FUNCTION__, addr, error_code);
  374. kvm_mmu_audit(vcpu, "pre page fault");
  375. r = mmu_topup_memory_caches(vcpu);
  376. if (r)
  377. return r;
  378. /*
  379. * Look up the shadow pte for the faulting address.
  380. */
  381. r = FNAME(walk_addr)(&walker, vcpu, addr, write_fault, user_fault,
  382. fetch_fault);
  383. /*
  384. * The page is not mapped by the guest. Let the guest handle it.
  385. */
  386. if (!r) {
  387. pgprintk("%s: guest page fault\n", __FUNCTION__);
  388. inject_page_fault(vcpu, addr, walker.error_code);
  389. FNAME(release_walker)(&walker);
  390. vcpu->last_pt_write_count = 0; /* reset fork detector */
  391. return 0;
  392. }
  393. shadow_pte = FNAME(fetch)(vcpu, addr, &walker, user_fault, write_fault,
  394. &write_pt);
  395. pgprintk("%s: shadow pte %p %llx ptwrite %d\n", __FUNCTION__,
  396. shadow_pte, *shadow_pte, write_pt);
  397. FNAME(release_walker)(&walker);
  398. if (!write_pt)
  399. vcpu->last_pt_write_count = 0; /* reset fork detector */
  400. /*
  401. * mmio: emulate if accessible, otherwise its a guest fault.
  402. */
  403. if (is_io_pte(*shadow_pte))
  404. return 1;
  405. ++vcpu->stat.pf_fixed;
  406. kvm_mmu_audit(vcpu, "post page fault (fixed)");
  407. return write_pt;
  408. }
  409. static gpa_t FNAME(gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t vaddr)
  410. {
  411. struct guest_walker walker;
  412. gpa_t gpa = UNMAPPED_GVA;
  413. int r;
  414. r = FNAME(walk_addr)(&walker, vcpu, vaddr, 0, 0, 0);
  415. if (r) {
  416. gpa = (gpa_t)walker.gfn << PAGE_SHIFT;
  417. gpa |= vaddr & ~PAGE_MASK;
  418. }
  419. FNAME(release_walker)(&walker);
  420. return gpa;
  421. }
  422. #undef pt_element_t
  423. #undef guest_walker
  424. #undef FNAME
  425. #undef PT_BASE_ADDR_MASK
  426. #undef PT_INDEX
  427. #undef SHADOW_PT_INDEX
  428. #undef PT_LEVEL_MASK
  429. #undef PT_DIR_BASE_ADDR_MASK
  430. #undef PT_MAX_FULL_LEVELS