paging_tmpl.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  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. * Copyright 2010 Red Hat, Inc. and/or its affiliates.
  11. *
  12. * Authors:
  13. * Yaniv Kamay <yaniv@qumranet.com>
  14. * Avi Kivity <avi@qumranet.com>
  15. *
  16. * This work is licensed under the terms of the GNU GPL, version 2. See
  17. * the COPYING file in the top-level directory.
  18. *
  19. */
  20. /*
  21. * We need the mmu code to access both 32-bit and 64-bit guest ptes,
  22. * so the code in this file is compiled twice, once per pte size.
  23. */
  24. #if PTTYPE == 64
  25. #define pt_element_t u64
  26. #define guest_walker guest_walker64
  27. #define FNAME(name) paging##64_##name
  28. #define PT_BASE_ADDR_MASK PT64_BASE_ADDR_MASK
  29. #define PT_LVL_ADDR_MASK(lvl) PT64_LVL_ADDR_MASK(lvl)
  30. #define PT_LVL_OFFSET_MASK(lvl) PT64_LVL_OFFSET_MASK(lvl)
  31. #define PT_INDEX(addr, level) PT64_INDEX(addr, 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_LVL_ADDR_MASK(lvl) PT32_LVL_ADDR_MASK(lvl)
  46. #define PT_LVL_OFFSET_MASK(lvl) PT32_LVL_OFFSET_MASK(lvl)
  47. #define PT_INDEX(addr, level) PT32_INDEX(addr, level)
  48. #define PT_LEVEL_BITS PT32_LEVEL_BITS
  49. #define PT_MAX_FULL_LEVELS 2
  50. #define CMPXCHG cmpxchg
  51. #else
  52. #error Invalid PTTYPE value
  53. #endif
  54. #define gpte_to_gfn_lvl FNAME(gpte_to_gfn_lvl)
  55. #define gpte_to_gfn(pte) gpte_to_gfn_lvl((pte), PT_PAGE_TABLE_LEVEL)
  56. /*
  57. * The guest_walker structure emulates the behavior of the hardware page
  58. * table walker.
  59. */
  60. struct guest_walker {
  61. int level;
  62. gfn_t table_gfn[PT_MAX_FULL_LEVELS];
  63. pt_element_t ptes[PT_MAX_FULL_LEVELS];
  64. pt_element_t prefetch_ptes[PTE_PREFETCH_NUM];
  65. gpa_t pte_gpa[PT_MAX_FULL_LEVELS];
  66. unsigned pt_access;
  67. unsigned pte_access;
  68. gfn_t gfn;
  69. struct x86_exception fault;
  70. };
  71. static gfn_t gpte_to_gfn_lvl(pt_element_t gpte, int lvl)
  72. {
  73. return (gpte & PT_LVL_ADDR_MASK(lvl)) >> PAGE_SHIFT;
  74. }
  75. static int FNAME(cmpxchg_gpte)(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
  76. gfn_t table_gfn, unsigned index,
  77. pt_element_t orig_pte, pt_element_t new_pte)
  78. {
  79. pt_element_t ret;
  80. pt_element_t *table;
  81. struct page *page;
  82. gpa_t gpa;
  83. gpa = mmu->translate_gpa(vcpu, table_gfn << PAGE_SHIFT,
  84. PFERR_USER_MASK|PFERR_WRITE_MASK);
  85. if (gpa == UNMAPPED_GVA)
  86. return -EFAULT;
  87. page = gfn_to_page(vcpu->kvm, gpa_to_gfn(gpa));
  88. table = kmap_atomic(page, KM_USER0);
  89. ret = CMPXCHG(&table[index], orig_pte, new_pte);
  90. kunmap_atomic(table, KM_USER0);
  91. kvm_release_page_dirty(page);
  92. return (ret != orig_pte);
  93. }
  94. static unsigned FNAME(gpte_access)(struct kvm_vcpu *vcpu, pt_element_t gpte)
  95. {
  96. unsigned access;
  97. access = (gpte & (PT_WRITABLE_MASK | PT_USER_MASK)) | ACC_EXEC_MASK;
  98. #if PTTYPE == 64
  99. if (vcpu->arch.mmu.nx)
  100. access &= ~(gpte >> PT64_NX_SHIFT);
  101. #endif
  102. return access;
  103. }
  104. /*
  105. * Fetch a guest pte for a guest virtual address
  106. */
  107. static int FNAME(walk_addr_generic)(struct guest_walker *walker,
  108. struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
  109. gva_t addr, u32 access)
  110. {
  111. pt_element_t pte;
  112. pt_element_t __user *ptep_user;
  113. gfn_t table_gfn;
  114. unsigned index, pt_access, uninitialized_var(pte_access);
  115. gpa_t pte_gpa;
  116. bool eperm, present, rsvd_fault;
  117. int offset, write_fault, user_fault, fetch_fault;
  118. write_fault = access & PFERR_WRITE_MASK;
  119. user_fault = access & PFERR_USER_MASK;
  120. fetch_fault = access & PFERR_FETCH_MASK;
  121. trace_kvm_mmu_pagetable_walk(addr, write_fault, user_fault,
  122. fetch_fault);
  123. walk:
  124. present = true;
  125. eperm = rsvd_fault = false;
  126. walker->level = mmu->root_level;
  127. pte = mmu->get_cr3(vcpu);
  128. #if PTTYPE == 64
  129. if (walker->level == PT32E_ROOT_LEVEL) {
  130. pte = kvm_pdptr_read_mmu(vcpu, mmu, (addr >> 30) & 3);
  131. trace_kvm_mmu_paging_element(pte, walker->level);
  132. if (!is_present_gpte(pte)) {
  133. present = false;
  134. goto error;
  135. }
  136. --walker->level;
  137. }
  138. #endif
  139. ASSERT((!is_long_mode(vcpu) && is_pae(vcpu)) ||
  140. (mmu->get_cr3(vcpu) & CR3_NONPAE_RESERVED_BITS) == 0);
  141. pt_access = ACC_ALL;
  142. for (;;) {
  143. gfn_t real_gfn;
  144. unsigned long host_addr;
  145. index = PT_INDEX(addr, walker->level);
  146. table_gfn = gpte_to_gfn(pte);
  147. offset = index * sizeof(pt_element_t);
  148. pte_gpa = gfn_to_gpa(table_gfn) + offset;
  149. walker->table_gfn[walker->level - 1] = table_gfn;
  150. walker->pte_gpa[walker->level - 1] = pte_gpa;
  151. real_gfn = mmu->translate_gpa(vcpu, gfn_to_gpa(table_gfn),
  152. PFERR_USER_MASK|PFERR_WRITE_MASK);
  153. if (unlikely(real_gfn == UNMAPPED_GVA)) {
  154. present = false;
  155. break;
  156. }
  157. real_gfn = gpa_to_gfn(real_gfn);
  158. host_addr = gfn_to_hva(vcpu->kvm, real_gfn);
  159. if (unlikely(kvm_is_error_hva(host_addr))) {
  160. present = false;
  161. break;
  162. }
  163. ptep_user = (pt_element_t __user *)((void *)host_addr + offset);
  164. if (unlikely(copy_from_user(&pte, ptep_user, sizeof(pte)))) {
  165. present = false;
  166. break;
  167. }
  168. trace_kvm_mmu_paging_element(pte, walker->level);
  169. if (unlikely(!is_present_gpte(pte))) {
  170. present = false;
  171. break;
  172. }
  173. if (unlikely(is_rsvd_bits_set(&vcpu->arch.mmu, pte,
  174. walker->level))) {
  175. rsvd_fault = true;
  176. break;
  177. }
  178. if (unlikely(write_fault && !is_writable_pte(pte)
  179. && (user_fault || is_write_protection(vcpu))))
  180. eperm = true;
  181. if (unlikely(user_fault && !(pte & PT_USER_MASK)))
  182. eperm = true;
  183. #if PTTYPE == 64
  184. if (unlikely(fetch_fault && (pte & PT64_NX_MASK)))
  185. eperm = true;
  186. #endif
  187. if (!eperm && !rsvd_fault
  188. && unlikely(!(pte & PT_ACCESSED_MASK))) {
  189. int ret;
  190. trace_kvm_mmu_set_accessed_bit(table_gfn, index,
  191. sizeof(pte));
  192. ret = FNAME(cmpxchg_gpte)(vcpu, mmu, table_gfn,
  193. index, pte, pte|PT_ACCESSED_MASK);
  194. if (ret < 0) {
  195. present = false;
  196. break;
  197. } else if (ret)
  198. goto walk;
  199. mark_page_dirty(vcpu->kvm, table_gfn);
  200. pte |= PT_ACCESSED_MASK;
  201. }
  202. pte_access = pt_access & FNAME(gpte_access)(vcpu, pte);
  203. walker->ptes[walker->level - 1] = pte;
  204. if ((walker->level == PT_PAGE_TABLE_LEVEL) ||
  205. ((walker->level == PT_DIRECTORY_LEVEL) &&
  206. is_large_pte(pte) &&
  207. (PTTYPE == 64 || is_pse(vcpu))) ||
  208. ((walker->level == PT_PDPE_LEVEL) &&
  209. is_large_pte(pte) &&
  210. mmu->root_level == PT64_ROOT_LEVEL)) {
  211. int lvl = walker->level;
  212. gpa_t real_gpa;
  213. gfn_t gfn;
  214. u32 ac;
  215. gfn = gpte_to_gfn_lvl(pte, lvl);
  216. gfn += (addr & PT_LVL_OFFSET_MASK(lvl)) >> PAGE_SHIFT;
  217. if (PTTYPE == 32 &&
  218. walker->level == PT_DIRECTORY_LEVEL &&
  219. is_cpuid_PSE36())
  220. gfn += pse36_gfn_delta(pte);
  221. ac = write_fault | fetch_fault | user_fault;
  222. real_gpa = mmu->translate_gpa(vcpu, gfn_to_gpa(gfn),
  223. ac);
  224. if (real_gpa == UNMAPPED_GVA)
  225. return 0;
  226. walker->gfn = real_gpa >> PAGE_SHIFT;
  227. break;
  228. }
  229. pt_access = pte_access;
  230. --walker->level;
  231. }
  232. if (unlikely(!present || eperm || rsvd_fault))
  233. goto error;
  234. if (write_fault && unlikely(!is_dirty_gpte(pte))) {
  235. int ret;
  236. trace_kvm_mmu_set_dirty_bit(table_gfn, index, sizeof(pte));
  237. ret = FNAME(cmpxchg_gpte)(vcpu, mmu, table_gfn, index, pte,
  238. pte|PT_DIRTY_MASK);
  239. if (ret < 0) {
  240. present = false;
  241. goto error;
  242. } else if (ret)
  243. goto walk;
  244. mark_page_dirty(vcpu->kvm, table_gfn);
  245. pte |= PT_DIRTY_MASK;
  246. walker->ptes[walker->level - 1] = pte;
  247. }
  248. walker->pt_access = pt_access;
  249. walker->pte_access = pte_access;
  250. pgprintk("%s: pte %llx pte_access %x pt_access %x\n",
  251. __func__, (u64)pte, pte_access, pt_access);
  252. return 1;
  253. error:
  254. walker->fault.vector = PF_VECTOR;
  255. walker->fault.error_code_valid = true;
  256. walker->fault.error_code = 0;
  257. if (present)
  258. walker->fault.error_code |= PFERR_PRESENT_MASK;
  259. walker->fault.error_code |= write_fault | user_fault;
  260. if (fetch_fault && mmu->nx)
  261. walker->fault.error_code |= PFERR_FETCH_MASK;
  262. if (rsvd_fault)
  263. walker->fault.error_code |= PFERR_RSVD_MASK;
  264. walker->fault.address = addr;
  265. walker->fault.nested_page_fault = mmu != vcpu->arch.walk_mmu;
  266. trace_kvm_mmu_walker_error(walker->fault.error_code);
  267. return 0;
  268. }
  269. static int FNAME(walk_addr)(struct guest_walker *walker,
  270. struct kvm_vcpu *vcpu, gva_t addr, u32 access)
  271. {
  272. return FNAME(walk_addr_generic)(walker, vcpu, &vcpu->arch.mmu, addr,
  273. access);
  274. }
  275. static int FNAME(walk_addr_nested)(struct guest_walker *walker,
  276. struct kvm_vcpu *vcpu, gva_t addr,
  277. u32 access)
  278. {
  279. return FNAME(walk_addr_generic)(walker, vcpu, &vcpu->arch.nested_mmu,
  280. addr, access);
  281. }
  282. static bool FNAME(prefetch_invalid_gpte)(struct kvm_vcpu *vcpu,
  283. struct kvm_mmu_page *sp, u64 *spte,
  284. pt_element_t gpte)
  285. {
  286. u64 nonpresent = shadow_trap_nonpresent_pte;
  287. if (is_rsvd_bits_set(&vcpu->arch.mmu, gpte, PT_PAGE_TABLE_LEVEL))
  288. goto no_present;
  289. if (!is_present_gpte(gpte)) {
  290. if (!sp->unsync)
  291. nonpresent = shadow_notrap_nonpresent_pte;
  292. goto no_present;
  293. }
  294. if (!(gpte & PT_ACCESSED_MASK))
  295. goto no_present;
  296. return false;
  297. no_present:
  298. drop_spte(vcpu->kvm, spte, nonpresent);
  299. return true;
  300. }
  301. static void FNAME(update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
  302. u64 *spte, const void *pte)
  303. {
  304. pt_element_t gpte;
  305. unsigned pte_access;
  306. pfn_t pfn;
  307. gpte = *(const pt_element_t *)pte;
  308. if (FNAME(prefetch_invalid_gpte)(vcpu, sp, spte, gpte))
  309. return;
  310. pgprintk("%s: gpte %llx spte %p\n", __func__, (u64)gpte, spte);
  311. pte_access = sp->role.access & FNAME(gpte_access)(vcpu, gpte);
  312. pfn = gfn_to_pfn_atomic(vcpu->kvm, gpte_to_gfn(gpte));
  313. if (is_error_pfn(pfn)) {
  314. kvm_release_pfn_clean(pfn);
  315. return;
  316. }
  317. /*
  318. * we call mmu_set_spte() with host_writable = true because that
  319. * vcpu->arch.update_pte.pfn was fetched from get_user_pages(write = 1).
  320. */
  321. mmu_set_spte(vcpu, spte, sp->role.access, pte_access, 0, 0,
  322. is_dirty_gpte(gpte), NULL, PT_PAGE_TABLE_LEVEL,
  323. gpte_to_gfn(gpte), pfn, true, true);
  324. }
  325. static bool FNAME(gpte_changed)(struct kvm_vcpu *vcpu,
  326. struct guest_walker *gw, int level)
  327. {
  328. pt_element_t curr_pte;
  329. gpa_t base_gpa, pte_gpa = gw->pte_gpa[level - 1];
  330. u64 mask;
  331. int r, index;
  332. if (level == PT_PAGE_TABLE_LEVEL) {
  333. mask = PTE_PREFETCH_NUM * sizeof(pt_element_t) - 1;
  334. base_gpa = pte_gpa & ~mask;
  335. index = (pte_gpa - base_gpa) / sizeof(pt_element_t);
  336. r = kvm_read_guest_atomic(vcpu->kvm, base_gpa,
  337. gw->prefetch_ptes, sizeof(gw->prefetch_ptes));
  338. curr_pte = gw->prefetch_ptes[index];
  339. } else
  340. r = kvm_read_guest_atomic(vcpu->kvm, pte_gpa,
  341. &curr_pte, sizeof(curr_pte));
  342. return r || curr_pte != gw->ptes[level - 1];
  343. }
  344. static void FNAME(pte_prefetch)(struct kvm_vcpu *vcpu, struct guest_walker *gw,
  345. u64 *sptep)
  346. {
  347. struct kvm_mmu_page *sp;
  348. pt_element_t *gptep = gw->prefetch_ptes;
  349. u64 *spte;
  350. int i;
  351. sp = page_header(__pa(sptep));
  352. if (sp->role.level > PT_PAGE_TABLE_LEVEL)
  353. return;
  354. if (sp->role.direct)
  355. return __direct_pte_prefetch(vcpu, sp, sptep);
  356. i = (sptep - sp->spt) & ~(PTE_PREFETCH_NUM - 1);
  357. spte = sp->spt + i;
  358. for (i = 0; i < PTE_PREFETCH_NUM; i++, spte++) {
  359. pt_element_t gpte;
  360. unsigned pte_access;
  361. gfn_t gfn;
  362. pfn_t pfn;
  363. bool dirty;
  364. if (spte == sptep)
  365. continue;
  366. if (*spte != shadow_trap_nonpresent_pte)
  367. continue;
  368. gpte = gptep[i];
  369. if (FNAME(prefetch_invalid_gpte)(vcpu, sp, spte, gpte))
  370. continue;
  371. pte_access = sp->role.access & FNAME(gpte_access)(vcpu, gpte);
  372. gfn = gpte_to_gfn(gpte);
  373. dirty = is_dirty_gpte(gpte);
  374. pfn = pte_prefetch_gfn_to_pfn(vcpu, gfn,
  375. (pte_access & ACC_WRITE_MASK) && dirty);
  376. if (is_error_pfn(pfn)) {
  377. kvm_release_pfn_clean(pfn);
  378. break;
  379. }
  380. mmu_set_spte(vcpu, spte, sp->role.access, pte_access, 0, 0,
  381. dirty, NULL, PT_PAGE_TABLE_LEVEL, gfn,
  382. pfn, true, true);
  383. }
  384. }
  385. /*
  386. * Fetch a shadow pte for a specific level in the paging hierarchy.
  387. */
  388. static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
  389. struct guest_walker *gw,
  390. int user_fault, int write_fault, int hlevel,
  391. int *ptwrite, pfn_t pfn, bool map_writable,
  392. bool prefault)
  393. {
  394. unsigned access = gw->pt_access;
  395. struct kvm_mmu_page *sp = NULL;
  396. bool dirty = is_dirty_gpte(gw->ptes[gw->level - 1]);
  397. int top_level;
  398. unsigned direct_access;
  399. struct kvm_shadow_walk_iterator it;
  400. if (!is_present_gpte(gw->ptes[gw->level - 1]))
  401. return NULL;
  402. direct_access = gw->pt_access & gw->pte_access;
  403. if (!dirty)
  404. direct_access &= ~ACC_WRITE_MASK;
  405. top_level = vcpu->arch.mmu.root_level;
  406. if (top_level == PT32E_ROOT_LEVEL)
  407. top_level = PT32_ROOT_LEVEL;
  408. /*
  409. * Verify that the top-level gpte is still there. Since the page
  410. * is a root page, it is either write protected (and cannot be
  411. * changed from now on) or it is invalid (in which case, we don't
  412. * really care if it changes underneath us after this point).
  413. */
  414. if (FNAME(gpte_changed)(vcpu, gw, top_level))
  415. goto out_gpte_changed;
  416. for (shadow_walk_init(&it, vcpu, addr);
  417. shadow_walk_okay(&it) && it.level > gw->level;
  418. shadow_walk_next(&it)) {
  419. gfn_t table_gfn;
  420. drop_large_spte(vcpu, it.sptep);
  421. sp = NULL;
  422. if (!is_shadow_present_pte(*it.sptep)) {
  423. table_gfn = gw->table_gfn[it.level - 2];
  424. sp = kvm_mmu_get_page(vcpu, table_gfn, addr, it.level-1,
  425. false, access, it.sptep);
  426. }
  427. /*
  428. * Verify that the gpte in the page we've just write
  429. * protected is still there.
  430. */
  431. if (FNAME(gpte_changed)(vcpu, gw, it.level - 1))
  432. goto out_gpte_changed;
  433. if (sp)
  434. link_shadow_page(it.sptep, sp);
  435. }
  436. for (;
  437. shadow_walk_okay(&it) && it.level > hlevel;
  438. shadow_walk_next(&it)) {
  439. gfn_t direct_gfn;
  440. validate_direct_spte(vcpu, it.sptep, direct_access);
  441. drop_large_spte(vcpu, it.sptep);
  442. if (is_shadow_present_pte(*it.sptep))
  443. continue;
  444. direct_gfn = gw->gfn & ~(KVM_PAGES_PER_HPAGE(it.level) - 1);
  445. sp = kvm_mmu_get_page(vcpu, direct_gfn, addr, it.level-1,
  446. true, direct_access, it.sptep);
  447. link_shadow_page(it.sptep, sp);
  448. }
  449. mmu_set_spte(vcpu, it.sptep, access, gw->pte_access & access,
  450. user_fault, write_fault, dirty, ptwrite, it.level,
  451. gw->gfn, pfn, prefault, map_writable);
  452. FNAME(pte_prefetch)(vcpu, gw, it.sptep);
  453. return it.sptep;
  454. out_gpte_changed:
  455. if (sp)
  456. kvm_mmu_put_page(sp, it.sptep);
  457. kvm_release_pfn_clean(pfn);
  458. return NULL;
  459. }
  460. /*
  461. * Page fault handler. There are several causes for a page fault:
  462. * - there is no shadow pte for the guest pte
  463. * - write access through a shadow pte marked read only so that we can set
  464. * the dirty bit
  465. * - write access to a shadow pte marked read only so we can update the page
  466. * dirty bitmap, when userspace requests it
  467. * - mmio access; in this case we will never install a present shadow pte
  468. * - normal guest page fault due to the guest pte marked not present, not
  469. * writable, or not executable
  470. *
  471. * Returns: 1 if we need to emulate the instruction, 0 otherwise, or
  472. * a negative value on error.
  473. */
  474. static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr, u32 error_code,
  475. bool prefault)
  476. {
  477. int write_fault = error_code & PFERR_WRITE_MASK;
  478. int user_fault = error_code & PFERR_USER_MASK;
  479. struct guest_walker walker;
  480. u64 *sptep;
  481. int write_pt = 0;
  482. int r;
  483. pfn_t pfn;
  484. int level = PT_PAGE_TABLE_LEVEL;
  485. int force_pt_level;
  486. unsigned long mmu_seq;
  487. bool map_writable;
  488. pgprintk("%s: addr %lx err %x\n", __func__, addr, error_code);
  489. r = mmu_topup_memory_caches(vcpu);
  490. if (r)
  491. return r;
  492. /*
  493. * Look up the guest pte for the faulting address.
  494. */
  495. r = FNAME(walk_addr)(&walker, vcpu, addr, error_code);
  496. /*
  497. * The page is not mapped by the guest. Let the guest handle it.
  498. */
  499. if (!r) {
  500. pgprintk("%s: guest page fault\n", __func__);
  501. if (!prefault) {
  502. inject_page_fault(vcpu, &walker.fault);
  503. /* reset fork detector */
  504. vcpu->arch.last_pt_write_count = 0;
  505. }
  506. return 0;
  507. }
  508. if (walker.level >= PT_DIRECTORY_LEVEL)
  509. force_pt_level = mapping_level_dirty_bitmap(vcpu, walker.gfn);
  510. else
  511. force_pt_level = 1;
  512. if (!force_pt_level) {
  513. level = min(walker.level, mapping_level(vcpu, walker.gfn));
  514. walker.gfn = walker.gfn & ~(KVM_PAGES_PER_HPAGE(level) - 1);
  515. }
  516. mmu_seq = vcpu->kvm->mmu_notifier_seq;
  517. smp_rmb();
  518. if (try_async_pf(vcpu, prefault, walker.gfn, addr, &pfn, write_fault,
  519. &map_writable))
  520. return 0;
  521. /* mmio */
  522. if (is_error_pfn(pfn))
  523. return kvm_handle_bad_page(vcpu->kvm, walker.gfn, pfn);
  524. spin_lock(&vcpu->kvm->mmu_lock);
  525. if (mmu_notifier_retry(vcpu, mmu_seq))
  526. goto out_unlock;
  527. trace_kvm_mmu_audit(vcpu, AUDIT_PRE_PAGE_FAULT);
  528. kvm_mmu_free_some_pages(vcpu);
  529. if (!force_pt_level)
  530. transparent_hugepage_adjust(vcpu, &walker.gfn, &pfn, &level);
  531. sptep = FNAME(fetch)(vcpu, addr, &walker, user_fault, write_fault,
  532. level, &write_pt, pfn, map_writable, prefault);
  533. (void)sptep;
  534. pgprintk("%s: shadow pte %p %llx ptwrite %d\n", __func__,
  535. sptep, *sptep, write_pt);
  536. if (!write_pt)
  537. vcpu->arch.last_pt_write_count = 0; /* reset fork detector */
  538. ++vcpu->stat.pf_fixed;
  539. trace_kvm_mmu_audit(vcpu, AUDIT_POST_PAGE_FAULT);
  540. spin_unlock(&vcpu->kvm->mmu_lock);
  541. return write_pt;
  542. out_unlock:
  543. spin_unlock(&vcpu->kvm->mmu_lock);
  544. kvm_release_pfn_clean(pfn);
  545. return 0;
  546. }
  547. static void FNAME(invlpg)(struct kvm_vcpu *vcpu, gva_t gva)
  548. {
  549. struct kvm_shadow_walk_iterator iterator;
  550. struct kvm_mmu_page *sp;
  551. gpa_t pte_gpa = -1;
  552. int level;
  553. u64 *sptep;
  554. int need_flush = 0;
  555. spin_lock(&vcpu->kvm->mmu_lock);
  556. for_each_shadow_entry(vcpu, gva, iterator) {
  557. level = iterator.level;
  558. sptep = iterator.sptep;
  559. sp = page_header(__pa(sptep));
  560. if (is_last_spte(*sptep, level)) {
  561. int offset, shift;
  562. if (!sp->unsync)
  563. break;
  564. shift = PAGE_SHIFT -
  565. (PT_LEVEL_BITS - PT64_LEVEL_BITS) * level;
  566. offset = sp->role.quadrant << shift;
  567. pte_gpa = (sp->gfn << PAGE_SHIFT) + offset;
  568. pte_gpa += (sptep - sp->spt) * sizeof(pt_element_t);
  569. if (is_shadow_present_pte(*sptep)) {
  570. if (is_large_pte(*sptep))
  571. --vcpu->kvm->stat.lpages;
  572. drop_spte(vcpu->kvm, sptep,
  573. shadow_trap_nonpresent_pte);
  574. need_flush = 1;
  575. } else
  576. __set_spte(sptep, shadow_trap_nonpresent_pte);
  577. break;
  578. }
  579. if (!is_shadow_present_pte(*sptep) || !sp->unsync_children)
  580. break;
  581. }
  582. if (need_flush)
  583. kvm_flush_remote_tlbs(vcpu->kvm);
  584. atomic_inc(&vcpu->kvm->arch.invlpg_counter);
  585. spin_unlock(&vcpu->kvm->mmu_lock);
  586. if (pte_gpa == -1)
  587. return;
  588. if (mmu_topup_memory_caches(vcpu))
  589. return;
  590. kvm_mmu_pte_write(vcpu, pte_gpa, NULL, sizeof(pt_element_t), 0);
  591. }
  592. static gpa_t FNAME(gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t vaddr, u32 access,
  593. struct x86_exception *exception)
  594. {
  595. struct guest_walker walker;
  596. gpa_t gpa = UNMAPPED_GVA;
  597. int r;
  598. r = FNAME(walk_addr)(&walker, vcpu, vaddr, access);
  599. if (r) {
  600. gpa = gfn_to_gpa(walker.gfn);
  601. gpa |= vaddr & ~PAGE_MASK;
  602. } else if (exception)
  603. *exception = walker.fault;
  604. return gpa;
  605. }
  606. static gpa_t FNAME(gva_to_gpa_nested)(struct kvm_vcpu *vcpu, gva_t vaddr,
  607. u32 access,
  608. struct x86_exception *exception)
  609. {
  610. struct guest_walker walker;
  611. gpa_t gpa = UNMAPPED_GVA;
  612. int r;
  613. r = FNAME(walk_addr_nested)(&walker, vcpu, vaddr, access);
  614. if (r) {
  615. gpa = gfn_to_gpa(walker.gfn);
  616. gpa |= vaddr & ~PAGE_MASK;
  617. } else if (exception)
  618. *exception = walker.fault;
  619. return gpa;
  620. }
  621. static void FNAME(prefetch_page)(struct kvm_vcpu *vcpu,
  622. struct kvm_mmu_page *sp)
  623. {
  624. int i, j, offset, r;
  625. pt_element_t pt[256 / sizeof(pt_element_t)];
  626. gpa_t pte_gpa;
  627. if (sp->role.direct
  628. || (PTTYPE == 32 && sp->role.level > PT_PAGE_TABLE_LEVEL)) {
  629. nonpaging_prefetch_page(vcpu, sp);
  630. return;
  631. }
  632. pte_gpa = gfn_to_gpa(sp->gfn);
  633. if (PTTYPE == 32) {
  634. offset = sp->role.quadrant << PT64_LEVEL_BITS;
  635. pte_gpa += offset * sizeof(pt_element_t);
  636. }
  637. for (i = 0; i < PT64_ENT_PER_PAGE; i += ARRAY_SIZE(pt)) {
  638. r = kvm_read_guest_atomic(vcpu->kvm, pte_gpa, pt, sizeof pt);
  639. pte_gpa += ARRAY_SIZE(pt) * sizeof(pt_element_t);
  640. for (j = 0; j < ARRAY_SIZE(pt); ++j)
  641. if (r || is_present_gpte(pt[j]))
  642. sp->spt[i+j] = shadow_trap_nonpresent_pte;
  643. else
  644. sp->spt[i+j] = shadow_notrap_nonpresent_pte;
  645. }
  646. }
  647. /*
  648. * Using the cached information from sp->gfns is safe because:
  649. * - The spte has a reference to the struct page, so the pfn for a given gfn
  650. * can't change unless all sptes pointing to it are nuked first.
  651. *
  652. * Note:
  653. * We should flush all tlbs if spte is dropped even though guest is
  654. * responsible for it. Since if we don't, kvm_mmu_notifier_invalidate_page
  655. * and kvm_mmu_notifier_invalidate_range_start detect the mapping page isn't
  656. * used by guest then tlbs are not flushed, so guest is allowed to access the
  657. * freed pages.
  658. * And we increase kvm->tlbs_dirty to delay tlbs flush in this case.
  659. */
  660. static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
  661. {
  662. int i, offset, nr_present;
  663. bool host_writable;
  664. gpa_t first_pte_gpa;
  665. offset = nr_present = 0;
  666. /* direct kvm_mmu_page can not be unsync. */
  667. BUG_ON(sp->role.direct);
  668. if (PTTYPE == 32)
  669. offset = sp->role.quadrant << PT64_LEVEL_BITS;
  670. first_pte_gpa = gfn_to_gpa(sp->gfn) + offset * sizeof(pt_element_t);
  671. for (i = 0; i < PT64_ENT_PER_PAGE; i++) {
  672. unsigned pte_access;
  673. pt_element_t gpte;
  674. gpa_t pte_gpa;
  675. gfn_t gfn;
  676. if (!is_shadow_present_pte(sp->spt[i]))
  677. continue;
  678. pte_gpa = first_pte_gpa + i * sizeof(pt_element_t);
  679. if (kvm_read_guest_atomic(vcpu->kvm, pte_gpa, &gpte,
  680. sizeof(pt_element_t)))
  681. return -EINVAL;
  682. gfn = gpte_to_gfn(gpte);
  683. if (FNAME(prefetch_invalid_gpte)(vcpu, sp, &sp->spt[i], gpte)) {
  684. vcpu->kvm->tlbs_dirty++;
  685. continue;
  686. }
  687. if (gfn != sp->gfns[i]) {
  688. drop_spte(vcpu->kvm, &sp->spt[i],
  689. shadow_trap_nonpresent_pte);
  690. vcpu->kvm->tlbs_dirty++;
  691. continue;
  692. }
  693. nr_present++;
  694. pte_access = sp->role.access & FNAME(gpte_access)(vcpu, gpte);
  695. host_writable = sp->spt[i] & SPTE_HOST_WRITEABLE;
  696. set_spte(vcpu, &sp->spt[i], pte_access, 0, 0,
  697. is_dirty_gpte(gpte), PT_PAGE_TABLE_LEVEL, gfn,
  698. spte_to_pfn(sp->spt[i]), true, false,
  699. host_writable);
  700. }
  701. return !nr_present;
  702. }
  703. #undef pt_element_t
  704. #undef guest_walker
  705. #undef FNAME
  706. #undef PT_BASE_ADDR_MASK
  707. #undef PT_INDEX
  708. #undef PT_LVL_ADDR_MASK
  709. #undef PT_LVL_OFFSET_MASK
  710. #undef PT_LEVEL_BITS
  711. #undef PT_MAX_FULL_LEVELS
  712. #undef gpte_to_gfn
  713. #undef gpte_to_gfn_lvl
  714. #undef CMPXCHG