paging_tmpl.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. #define PT_LEVEL_BITS PT64_LEVEL_BITS
  33. #ifdef CONFIG_X86_64
  34. #define PT_MAX_FULL_LEVELS 4
  35. #define CMPXCHG cmpxchg
  36. #else
  37. #define CMPXCHG cmpxchg64
  38. #define PT_MAX_FULL_LEVELS 2
  39. #endif
  40. #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_LEVEL_BITS PT32_LEVEL_BITS
  50. #define PT_MAX_FULL_LEVELS 2
  51. #define CMPXCHG cmpxchg
  52. #else
  53. #error Invalid PTTYPE value
  54. #endif
  55. #define gpte_to_gfn FNAME(gpte_to_gfn)
  56. #define gpte_to_gfn_pde FNAME(gpte_to_gfn_pde)
  57. /*
  58. * The guest_walker structure emulates the behavior of the hardware page
  59. * table walker.
  60. */
  61. struct guest_walker {
  62. int level;
  63. gfn_t table_gfn[PT_MAX_FULL_LEVELS];
  64. pt_element_t ptes[PT_MAX_FULL_LEVELS];
  65. gpa_t pte_gpa[PT_MAX_FULL_LEVELS];
  66. unsigned pt_access;
  67. unsigned pte_access;
  68. gfn_t gfn;
  69. u32 error_code;
  70. };
  71. static gfn_t gpte_to_gfn(pt_element_t gpte)
  72. {
  73. return (gpte & PT_BASE_ADDR_MASK) >> PAGE_SHIFT;
  74. }
  75. static gfn_t gpte_to_gfn_pde(pt_element_t gpte)
  76. {
  77. return (gpte & PT_DIR_BASE_ADDR_MASK) >> PAGE_SHIFT;
  78. }
  79. static bool FNAME(cmpxchg_gpte)(struct kvm *kvm,
  80. gfn_t table_gfn, unsigned index,
  81. pt_element_t orig_pte, pt_element_t new_pte)
  82. {
  83. pt_element_t ret;
  84. pt_element_t *table;
  85. struct page *page;
  86. page = gfn_to_page(kvm, table_gfn);
  87. table = kmap_atomic(page, KM_USER0);
  88. ret = CMPXCHG(&table[index], orig_pte, new_pte);
  89. kunmap_atomic(table, KM_USER0);
  90. kvm_release_page_dirty(page);
  91. return (ret != orig_pte);
  92. }
  93. static unsigned FNAME(gpte_access)(struct kvm_vcpu *vcpu, pt_element_t gpte)
  94. {
  95. unsigned access;
  96. access = (gpte & (PT_WRITABLE_MASK | PT_USER_MASK)) | ACC_EXEC_MASK;
  97. #if PTTYPE == 64
  98. if (is_nx(vcpu))
  99. access &= ~(gpte >> PT64_NX_SHIFT);
  100. #endif
  101. return access;
  102. }
  103. /*
  104. * Fetch a guest pte for a guest virtual address
  105. */
  106. static int FNAME(walk_addr)(struct guest_walker *walker,
  107. struct kvm_vcpu *vcpu, gva_t addr,
  108. int write_fault, int user_fault, int fetch_fault)
  109. {
  110. pt_element_t pte;
  111. gfn_t table_gfn;
  112. unsigned index, pt_access, pte_access;
  113. gpa_t pte_gpa;
  114. pgprintk("%s: addr %lx\n", __FUNCTION__, addr);
  115. walk:
  116. walker->level = vcpu->arch.mmu.root_level;
  117. pte = vcpu->arch.cr3;
  118. #if PTTYPE == 64
  119. if (!is_long_mode(vcpu)) {
  120. pte = vcpu->arch.pdptrs[(addr >> 30) & 3];
  121. if (!is_present_pte(pte))
  122. goto not_present;
  123. --walker->level;
  124. }
  125. #endif
  126. ASSERT((!is_long_mode(vcpu) && is_pae(vcpu)) ||
  127. (vcpu->cr3 & CR3_NONPAE_RESERVED_BITS) == 0);
  128. pt_access = ACC_ALL;
  129. for (;;) {
  130. index = PT_INDEX(addr, walker->level);
  131. table_gfn = gpte_to_gfn(pte);
  132. pte_gpa = gfn_to_gpa(table_gfn);
  133. pte_gpa += index * sizeof(pt_element_t);
  134. walker->table_gfn[walker->level - 1] = table_gfn;
  135. walker->pte_gpa[walker->level - 1] = pte_gpa;
  136. pgprintk("%s: table_gfn[%d] %lx\n", __FUNCTION__,
  137. walker->level - 1, table_gfn);
  138. kvm_read_guest(vcpu->kvm, pte_gpa, &pte, sizeof(pte));
  139. if (!is_present_pte(pte))
  140. goto not_present;
  141. if (write_fault && !is_writeble_pte(pte))
  142. if (user_fault || is_write_protection(vcpu))
  143. goto access_error;
  144. if (user_fault && !(pte & PT_USER_MASK))
  145. goto access_error;
  146. #if PTTYPE == 64
  147. if (fetch_fault && is_nx(vcpu) && (pte & PT64_NX_MASK))
  148. goto access_error;
  149. #endif
  150. if (!(pte & PT_ACCESSED_MASK)) {
  151. mark_page_dirty(vcpu->kvm, table_gfn);
  152. if (FNAME(cmpxchg_gpte)(vcpu->kvm, table_gfn,
  153. index, pte, pte|PT_ACCESSED_MASK))
  154. goto walk;
  155. pte |= PT_ACCESSED_MASK;
  156. }
  157. pte_access = pt_access & FNAME(gpte_access)(vcpu, pte);
  158. walker->ptes[walker->level - 1] = pte;
  159. if (walker->level == PT_PAGE_TABLE_LEVEL) {
  160. walker->gfn = gpte_to_gfn(pte);
  161. break;
  162. }
  163. if (walker->level == PT_DIRECTORY_LEVEL
  164. && (pte & PT_PAGE_SIZE_MASK)
  165. && (PTTYPE == 64 || is_pse(vcpu))) {
  166. walker->gfn = gpte_to_gfn_pde(pte);
  167. walker->gfn += PT_INDEX(addr, PT_PAGE_TABLE_LEVEL);
  168. if (PTTYPE == 32 && is_cpuid_PSE36())
  169. walker->gfn += pse36_gfn_delta(pte);
  170. break;
  171. }
  172. pt_access = pte_access;
  173. --walker->level;
  174. }
  175. if (write_fault && !is_dirty_pte(pte)) {
  176. bool ret;
  177. mark_page_dirty(vcpu->kvm, table_gfn);
  178. ret = FNAME(cmpxchg_gpte)(vcpu->kvm, table_gfn, index, pte,
  179. pte|PT_DIRTY_MASK);
  180. if (ret)
  181. goto walk;
  182. pte |= PT_DIRTY_MASK;
  183. kvm_mmu_pte_write(vcpu, pte_gpa, (u8 *)&pte, sizeof(pte));
  184. walker->ptes[walker->level - 1] = pte;
  185. }
  186. walker->pt_access = pt_access;
  187. walker->pte_access = pte_access;
  188. pgprintk("%s: pte %llx pte_access %x pt_access %x\n",
  189. __FUNCTION__, (u64)pte, pt_access, pte_access);
  190. return 1;
  191. not_present:
  192. walker->error_code = 0;
  193. goto err;
  194. access_error:
  195. walker->error_code = PFERR_PRESENT_MASK;
  196. err:
  197. if (write_fault)
  198. walker->error_code |= PFERR_WRITE_MASK;
  199. if (user_fault)
  200. walker->error_code |= PFERR_USER_MASK;
  201. if (fetch_fault)
  202. walker->error_code |= PFERR_FETCH_MASK;
  203. return 0;
  204. }
  205. static void FNAME(update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *page,
  206. u64 *spte, const void *pte, int bytes,
  207. int offset_in_pte)
  208. {
  209. pt_element_t gpte;
  210. unsigned pte_access;
  211. struct page *npage;
  212. gpte = *(const pt_element_t *)pte;
  213. if (~gpte & (PT_PRESENT_MASK | PT_ACCESSED_MASK)) {
  214. if (!offset_in_pte && !is_present_pte(gpte))
  215. set_shadow_pte(spte, shadow_notrap_nonpresent_pte);
  216. return;
  217. }
  218. if (bytes < sizeof(pt_element_t))
  219. return;
  220. pgprintk("%s: gpte %llx spte %p\n", __FUNCTION__, (u64)gpte, spte);
  221. pte_access = page->role.access & FNAME(gpte_access)(vcpu, gpte);
  222. if (gpte_to_gfn(gpte) != vcpu->arch.update_pte.gfn)
  223. return;
  224. npage = vcpu->arch.update_pte.page;
  225. if (!npage)
  226. return;
  227. get_page(npage);
  228. mmu_set_spte(vcpu, spte, page->role.access, pte_access, 0, 0,
  229. gpte & PT_DIRTY_MASK, NULL, gpte_to_gfn(gpte), npage);
  230. }
  231. /*
  232. * Fetch a shadow pte for a specific level in the paging hierarchy.
  233. */
  234. static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
  235. struct guest_walker *walker,
  236. int user_fault, int write_fault, int *ptwrite,
  237. struct page *page)
  238. {
  239. hpa_t shadow_addr;
  240. int level;
  241. u64 *shadow_ent;
  242. unsigned access = walker->pt_access;
  243. if (!is_present_pte(walker->ptes[walker->level - 1]))
  244. return NULL;
  245. shadow_addr = vcpu->arch.mmu.root_hpa;
  246. level = vcpu->arch.mmu.shadow_root_level;
  247. if (level == PT32E_ROOT_LEVEL) {
  248. shadow_addr = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
  249. shadow_addr &= PT64_BASE_ADDR_MASK;
  250. --level;
  251. }
  252. for (; ; level--) {
  253. u32 index = SHADOW_PT_INDEX(addr, level);
  254. struct kvm_mmu_page *shadow_page;
  255. u64 shadow_pte;
  256. int metaphysical;
  257. gfn_t table_gfn;
  258. bool new_page = 0;
  259. shadow_ent = ((u64 *)__va(shadow_addr)) + index;
  260. if (level == PT_PAGE_TABLE_LEVEL)
  261. break;
  262. if (is_shadow_present_pte(*shadow_ent)) {
  263. shadow_addr = *shadow_ent & PT64_BASE_ADDR_MASK;
  264. continue;
  265. }
  266. if (level - 1 == PT_PAGE_TABLE_LEVEL
  267. && walker->level == PT_DIRECTORY_LEVEL) {
  268. metaphysical = 1;
  269. if (!is_dirty_pte(walker->ptes[level - 1]))
  270. access &= ~ACC_WRITE_MASK;
  271. table_gfn = gpte_to_gfn(walker->ptes[level - 1]);
  272. } else {
  273. metaphysical = 0;
  274. table_gfn = walker->table_gfn[level - 2];
  275. }
  276. shadow_page = kvm_mmu_get_page(vcpu, table_gfn, addr, level-1,
  277. metaphysical, access,
  278. shadow_ent, &new_page);
  279. if (new_page && !metaphysical) {
  280. int r;
  281. pt_element_t curr_pte;
  282. r = kvm_read_guest_atomic(vcpu->kvm,
  283. walker->pte_gpa[level - 2],
  284. &curr_pte, sizeof(curr_pte));
  285. if (r || curr_pte != walker->ptes[level - 2]) {
  286. kvm_release_page_clean(page);
  287. return NULL;
  288. }
  289. }
  290. shadow_addr = __pa(shadow_page->spt);
  291. shadow_pte = shadow_addr | PT_PRESENT_MASK | PT_ACCESSED_MASK
  292. | PT_WRITABLE_MASK | PT_USER_MASK;
  293. *shadow_ent = shadow_pte;
  294. }
  295. mmu_set_spte(vcpu, shadow_ent, access, walker->pte_access & access,
  296. user_fault, write_fault,
  297. walker->ptes[walker->level-1] & PT_DIRTY_MASK,
  298. ptwrite, walker->gfn, page);
  299. return shadow_ent;
  300. }
  301. /*
  302. * Page fault handler. There are several causes for a page fault:
  303. * - there is no shadow pte for the guest pte
  304. * - write access through a shadow pte marked read only so that we can set
  305. * the dirty bit
  306. * - write access to a shadow pte marked read only so we can update the page
  307. * dirty bitmap, when userspace requests it
  308. * - mmio access; in this case we will never install a present shadow pte
  309. * - normal guest page fault due to the guest pte marked not present, not
  310. * writable, or not executable
  311. *
  312. * Returns: 1 if we need to emulate the instruction, 0 otherwise, or
  313. * a negative value on error.
  314. */
  315. static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr,
  316. u32 error_code)
  317. {
  318. int write_fault = error_code & PFERR_WRITE_MASK;
  319. int user_fault = error_code & PFERR_USER_MASK;
  320. int fetch_fault = error_code & PFERR_FETCH_MASK;
  321. struct guest_walker walker;
  322. u64 *shadow_pte;
  323. int write_pt = 0;
  324. int r;
  325. struct page *page;
  326. pgprintk("%s: addr %lx err %x\n", __FUNCTION__, addr, error_code);
  327. kvm_mmu_audit(vcpu, "pre page fault");
  328. r = mmu_topup_memory_caches(vcpu);
  329. if (r)
  330. return r;
  331. down_read(&current->mm->mmap_sem);
  332. /*
  333. * Look up the shadow pte for the faulting address.
  334. */
  335. r = FNAME(walk_addr)(&walker, vcpu, addr, write_fault, user_fault,
  336. fetch_fault);
  337. /*
  338. * The page is not mapped by the guest. Let the guest handle it.
  339. */
  340. if (!r) {
  341. pgprintk("%s: guest page fault\n", __FUNCTION__);
  342. inject_page_fault(vcpu, addr, walker.error_code);
  343. vcpu->arch.last_pt_write_count = 0; /* reset fork detector */
  344. up_read(&current->mm->mmap_sem);
  345. return 0;
  346. }
  347. page = gfn_to_page(vcpu->kvm, walker.gfn);
  348. spin_lock(&vcpu->kvm->mmu_lock);
  349. kvm_mmu_free_some_pages(vcpu);
  350. shadow_pte = FNAME(fetch)(vcpu, addr, &walker, user_fault, write_fault,
  351. &write_pt, page);
  352. pgprintk("%s: shadow pte %p %llx ptwrite %d\n", __FUNCTION__,
  353. shadow_pte, *shadow_pte, write_pt);
  354. if (!write_pt)
  355. vcpu->arch.last_pt_write_count = 0; /* reset fork detector */
  356. /*
  357. * mmio: emulate if accessible, otherwise its a guest fault.
  358. */
  359. if (shadow_pte && is_io_pte(*shadow_pte)) {
  360. spin_unlock(&vcpu->kvm->mmu_lock);
  361. up_read(&current->mm->mmap_sem);
  362. return 1;
  363. }
  364. ++vcpu->stat.pf_fixed;
  365. kvm_mmu_audit(vcpu, "post page fault (fixed)");
  366. spin_unlock(&vcpu->kvm->mmu_lock);
  367. up_read(&current->mm->mmap_sem);
  368. return write_pt;
  369. }
  370. static gpa_t FNAME(gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t vaddr)
  371. {
  372. struct guest_walker walker;
  373. gpa_t gpa = UNMAPPED_GVA;
  374. int r;
  375. r = FNAME(walk_addr)(&walker, vcpu, vaddr, 0, 0, 0);
  376. if (r) {
  377. gpa = gfn_to_gpa(walker.gfn);
  378. gpa |= vaddr & ~PAGE_MASK;
  379. }
  380. return gpa;
  381. }
  382. static void FNAME(prefetch_page)(struct kvm_vcpu *vcpu,
  383. struct kvm_mmu_page *sp)
  384. {
  385. int i, offset = 0, r = 0;
  386. pt_element_t pt;
  387. if (sp->role.metaphysical
  388. || (PTTYPE == 32 && sp->role.level > PT_PAGE_TABLE_LEVEL)) {
  389. nonpaging_prefetch_page(vcpu, sp);
  390. return;
  391. }
  392. if (PTTYPE == 32)
  393. offset = sp->role.quadrant << PT64_LEVEL_BITS;
  394. for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
  395. gpa_t pte_gpa = gfn_to_gpa(sp->gfn);
  396. pte_gpa += (i+offset) * sizeof(pt_element_t);
  397. r = kvm_read_guest_atomic(vcpu->kvm, pte_gpa, &pt,
  398. sizeof(pt_element_t));
  399. if (r || is_present_pte(pt))
  400. sp->spt[i] = shadow_trap_nonpresent_pte;
  401. else
  402. sp->spt[i] = shadow_notrap_nonpresent_pte;
  403. }
  404. }
  405. #undef pt_element_t
  406. #undef guest_walker
  407. #undef FNAME
  408. #undef PT_BASE_ADDR_MASK
  409. #undef PT_INDEX
  410. #undef SHADOW_PT_INDEX
  411. #undef PT_LEVEL_MASK
  412. #undef PT_DIR_BASE_ADDR_MASK
  413. #undef PT_LEVEL_BITS
  414. #undef PT_MAX_FULL_LEVELS
  415. #undef gpte_to_gfn
  416. #undef gpte_to_gfn_pde
  417. #undef CMPXCHG