paging_tmpl.h 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  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. #define PT_GUEST_DIRTY_SHIFT PT_DIRTY_SHIFT
  34. #define PT_GUEST_ACCESSED_SHIFT PT_ACCESSED_SHIFT
  35. #define PT_HAVE_ACCESSED_DIRTY(mmu) true
  36. #ifdef CONFIG_X86_64
  37. #define PT_MAX_FULL_LEVELS 4
  38. #define CMPXCHG cmpxchg
  39. #else
  40. #define CMPXCHG cmpxchg64
  41. #define PT_MAX_FULL_LEVELS 2
  42. #endif
  43. #elif PTTYPE == 32
  44. #define pt_element_t u32
  45. #define guest_walker guest_walker32
  46. #define FNAME(name) paging##32_##name
  47. #define PT_BASE_ADDR_MASK PT32_BASE_ADDR_MASK
  48. #define PT_LVL_ADDR_MASK(lvl) PT32_LVL_ADDR_MASK(lvl)
  49. #define PT_LVL_OFFSET_MASK(lvl) PT32_LVL_OFFSET_MASK(lvl)
  50. #define PT_INDEX(addr, level) PT32_INDEX(addr, level)
  51. #define PT_LEVEL_BITS PT32_LEVEL_BITS
  52. #define PT_MAX_FULL_LEVELS 2
  53. #define PT_GUEST_DIRTY_SHIFT PT_DIRTY_SHIFT
  54. #define PT_GUEST_ACCESSED_SHIFT PT_ACCESSED_SHIFT
  55. #define PT_HAVE_ACCESSED_DIRTY(mmu) true
  56. #define CMPXCHG cmpxchg
  57. #elif PTTYPE == PTTYPE_EPT
  58. #define pt_element_t u64
  59. #define guest_walker guest_walkerEPT
  60. #define FNAME(name) ept_##name
  61. #define PT_BASE_ADDR_MASK PT64_BASE_ADDR_MASK
  62. #define PT_LVL_ADDR_MASK(lvl) PT64_LVL_ADDR_MASK(lvl)
  63. #define PT_LVL_OFFSET_MASK(lvl) PT64_LVL_OFFSET_MASK(lvl)
  64. #define PT_INDEX(addr, level) PT64_INDEX(addr, level)
  65. #define PT_LEVEL_BITS PT64_LEVEL_BITS
  66. #define PT_GUEST_DIRTY_SHIFT 9
  67. #define PT_GUEST_ACCESSED_SHIFT 8
  68. #define PT_HAVE_ACCESSED_DIRTY(mmu) ((mmu)->ept_ad)
  69. #define CMPXCHG cmpxchg64
  70. #define PT_MAX_FULL_LEVELS 4
  71. #else
  72. #error Invalid PTTYPE value
  73. #endif
  74. #define PT_GUEST_DIRTY_MASK (1 << PT_GUEST_DIRTY_SHIFT)
  75. #define PT_GUEST_ACCESSED_MASK (1 << PT_GUEST_ACCESSED_SHIFT)
  76. #define gpte_to_gfn_lvl FNAME(gpte_to_gfn_lvl)
  77. #define gpte_to_gfn(pte) gpte_to_gfn_lvl((pte), PT_PAGE_TABLE_LEVEL)
  78. /*
  79. * The guest_walker structure emulates the behavior of the hardware page
  80. * table walker.
  81. */
  82. struct guest_walker {
  83. int level;
  84. unsigned max_level;
  85. gfn_t table_gfn[PT_MAX_FULL_LEVELS];
  86. pt_element_t ptes[PT_MAX_FULL_LEVELS];
  87. pt_element_t prefetch_ptes[PTE_PREFETCH_NUM];
  88. gpa_t pte_gpa[PT_MAX_FULL_LEVELS];
  89. pt_element_t __user *ptep_user[PT_MAX_FULL_LEVELS];
  90. bool pte_writable[PT_MAX_FULL_LEVELS];
  91. unsigned pt_access;
  92. unsigned pte_access;
  93. gfn_t gfn;
  94. struct x86_exception fault;
  95. };
  96. static gfn_t gpte_to_gfn_lvl(pt_element_t gpte, int lvl)
  97. {
  98. return (gpte & PT_LVL_ADDR_MASK(lvl)) >> PAGE_SHIFT;
  99. }
  100. static inline void FNAME(protect_clean_gpte)(struct kvm_mmu *mmu, unsigned *access,
  101. unsigned gpte)
  102. {
  103. unsigned mask;
  104. /* dirty bit is not supported, so no need to track it */
  105. if (!PT_HAVE_ACCESSED_DIRTY(mmu))
  106. return;
  107. BUILD_BUG_ON(PT_WRITABLE_MASK != ACC_WRITE_MASK);
  108. mask = (unsigned)~ACC_WRITE_MASK;
  109. /* Allow write access to dirty gptes */
  110. mask |= (gpte >> (PT_GUEST_DIRTY_SHIFT - PT_WRITABLE_SHIFT)) &
  111. PT_WRITABLE_MASK;
  112. *access &= mask;
  113. }
  114. static inline int FNAME(is_present_gpte)(unsigned long pte)
  115. {
  116. #if PTTYPE != PTTYPE_EPT
  117. return pte & PT_PRESENT_MASK;
  118. #else
  119. return pte & 7;
  120. #endif
  121. }
  122. static int FNAME(cmpxchg_gpte)(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
  123. pt_element_t __user *ptep_user, unsigned index,
  124. pt_element_t orig_pte, pt_element_t new_pte)
  125. {
  126. int npages;
  127. pt_element_t ret;
  128. pt_element_t *table;
  129. struct page *page;
  130. npages = get_user_pages_fast((unsigned long)ptep_user, 1, 1, &page);
  131. /* Check if the user is doing something meaningless. */
  132. if (unlikely(npages != 1))
  133. return -EFAULT;
  134. table = kmap_atomic(page);
  135. ret = CMPXCHG(&table[index], orig_pte, new_pte);
  136. kunmap_atomic(table);
  137. kvm_release_page_dirty(page);
  138. return (ret != orig_pte);
  139. }
  140. static bool FNAME(prefetch_invalid_gpte)(struct kvm_vcpu *vcpu,
  141. struct kvm_mmu_page *sp, u64 *spte,
  142. u64 gpte)
  143. {
  144. if (is_rsvd_bits_set(vcpu->arch.mmu, gpte, PT_PAGE_TABLE_LEVEL))
  145. goto no_present;
  146. if (!FNAME(is_present_gpte)(gpte))
  147. goto no_present;
  148. /* if accessed bit is not supported prefetch non accessed gpte */
  149. if (PT_HAVE_ACCESSED_DIRTY(vcpu->arch.mmu) &&
  150. !(gpte & PT_GUEST_ACCESSED_MASK))
  151. goto no_present;
  152. return false;
  153. no_present:
  154. drop_spte(vcpu->kvm, spte);
  155. return true;
  156. }
  157. /*
  158. * For PTTYPE_EPT, a page table can be executable but not readable
  159. * on supported processors. Therefore, set_spte does not automatically
  160. * set bit 0 if execute only is supported. Here, we repurpose ACC_USER_MASK
  161. * to signify readability since it isn't used in the EPT case
  162. */
  163. static inline unsigned FNAME(gpte_access)(u64 gpte)
  164. {
  165. unsigned access;
  166. #if PTTYPE == PTTYPE_EPT
  167. access = ((gpte & VMX_EPT_WRITABLE_MASK) ? ACC_WRITE_MASK : 0) |
  168. ((gpte & VMX_EPT_EXECUTABLE_MASK) ? ACC_EXEC_MASK : 0) |
  169. ((gpte & VMX_EPT_READABLE_MASK) ? ACC_USER_MASK : 0);
  170. #else
  171. BUILD_BUG_ON(ACC_EXEC_MASK != PT_PRESENT_MASK);
  172. BUILD_BUG_ON(ACC_EXEC_MASK != 1);
  173. access = gpte & (PT_WRITABLE_MASK | PT_USER_MASK | PT_PRESENT_MASK);
  174. /* Combine NX with P (which is set here) to get ACC_EXEC_MASK. */
  175. access ^= (gpte >> PT64_NX_SHIFT);
  176. #endif
  177. return access;
  178. }
  179. static int FNAME(update_accessed_dirty_bits)(struct kvm_vcpu *vcpu,
  180. struct kvm_mmu *mmu,
  181. struct guest_walker *walker,
  182. int write_fault)
  183. {
  184. unsigned level, index;
  185. pt_element_t pte, orig_pte;
  186. pt_element_t __user *ptep_user;
  187. gfn_t table_gfn;
  188. int ret;
  189. /* dirty/accessed bits are not supported, so no need to update them */
  190. if (!PT_HAVE_ACCESSED_DIRTY(mmu))
  191. return 0;
  192. for (level = walker->max_level; level >= walker->level; --level) {
  193. pte = orig_pte = walker->ptes[level - 1];
  194. table_gfn = walker->table_gfn[level - 1];
  195. ptep_user = walker->ptep_user[level - 1];
  196. index = offset_in_page(ptep_user) / sizeof(pt_element_t);
  197. if (!(pte & PT_GUEST_ACCESSED_MASK)) {
  198. trace_kvm_mmu_set_accessed_bit(table_gfn, index, sizeof(pte));
  199. pte |= PT_GUEST_ACCESSED_MASK;
  200. }
  201. if (level == walker->level && write_fault &&
  202. !(pte & PT_GUEST_DIRTY_MASK)) {
  203. trace_kvm_mmu_set_dirty_bit(table_gfn, index, sizeof(pte));
  204. #if PTTYPE == PTTYPE_EPT
  205. if (kvm_arch_write_log_dirty(vcpu))
  206. return -EINVAL;
  207. #endif
  208. pte |= PT_GUEST_DIRTY_MASK;
  209. }
  210. if (pte == orig_pte)
  211. continue;
  212. /*
  213. * If the slot is read-only, simply do not process the accessed
  214. * and dirty bits. This is the correct thing to do if the slot
  215. * is ROM, and page tables in read-as-ROM/write-as-MMIO slots
  216. * are only supported if the accessed and dirty bits are already
  217. * set in the ROM (so that MMIO writes are never needed).
  218. *
  219. * Note that NPT does not allow this at all and faults, since
  220. * it always wants nested page table entries for the guest
  221. * page tables to be writable. And EPT works but will simply
  222. * overwrite the read-only memory to set the accessed and dirty
  223. * bits.
  224. */
  225. if (unlikely(!walker->pte_writable[level - 1]))
  226. continue;
  227. ret = FNAME(cmpxchg_gpte)(vcpu, mmu, ptep_user, index, orig_pte, pte);
  228. if (ret)
  229. return ret;
  230. kvm_vcpu_mark_page_dirty(vcpu, table_gfn);
  231. walker->ptes[level - 1] = pte;
  232. }
  233. return 0;
  234. }
  235. static inline unsigned FNAME(gpte_pkeys)(struct kvm_vcpu *vcpu, u64 gpte)
  236. {
  237. unsigned pkeys = 0;
  238. #if PTTYPE == 64
  239. pte_t pte = {.pte = gpte};
  240. pkeys = pte_flags_pkey(pte_flags(pte));
  241. #endif
  242. return pkeys;
  243. }
  244. /*
  245. * Fetch a guest pte for a guest virtual address
  246. */
  247. static int FNAME(walk_addr_generic)(struct guest_walker *walker,
  248. struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
  249. gva_t addr, u32 access)
  250. {
  251. int ret;
  252. pt_element_t pte;
  253. pt_element_t __user *uninitialized_var(ptep_user);
  254. gfn_t table_gfn;
  255. u64 pt_access, pte_access;
  256. unsigned index, accessed_dirty, pte_pkey;
  257. unsigned nested_access;
  258. gpa_t pte_gpa;
  259. bool have_ad;
  260. int offset;
  261. u64 walk_nx_mask = 0;
  262. const int write_fault = access & PFERR_WRITE_MASK;
  263. const int user_fault = access & PFERR_USER_MASK;
  264. const int fetch_fault = access & PFERR_FETCH_MASK;
  265. u16 errcode = 0;
  266. gpa_t real_gpa;
  267. gfn_t gfn;
  268. trace_kvm_mmu_pagetable_walk(addr, access);
  269. retry_walk:
  270. walker->level = mmu->root_level;
  271. pte = mmu->get_cr3(vcpu);
  272. have_ad = PT_HAVE_ACCESSED_DIRTY(mmu);
  273. #if PTTYPE == 64
  274. walk_nx_mask = 1ULL << PT64_NX_SHIFT;
  275. if (walker->level == PT32E_ROOT_LEVEL) {
  276. pte = mmu->get_pdptr(vcpu, (addr >> 30) & 3);
  277. trace_kvm_mmu_paging_element(pte, walker->level);
  278. if (!FNAME(is_present_gpte)(pte))
  279. goto error;
  280. --walker->level;
  281. }
  282. #endif
  283. walker->max_level = walker->level;
  284. ASSERT(!(is_long_mode(vcpu) && !is_pae(vcpu)));
  285. /*
  286. * FIXME: on Intel processors, loads of the PDPTE registers for PAE paging
  287. * by the MOV to CR instruction are treated as reads and do not cause the
  288. * processor to set the dirty flag in any EPT paging-structure entry.
  289. */
  290. nested_access = (have_ad ? PFERR_WRITE_MASK : 0) | PFERR_USER_MASK;
  291. pte_access = ~0;
  292. ++walker->level;
  293. do {
  294. gfn_t real_gfn;
  295. unsigned long host_addr;
  296. pt_access = pte_access;
  297. --walker->level;
  298. index = PT_INDEX(addr, walker->level);
  299. table_gfn = gpte_to_gfn(pte);
  300. offset = index * sizeof(pt_element_t);
  301. pte_gpa = gfn_to_gpa(table_gfn) + offset;
  302. BUG_ON(walker->level < 1);
  303. walker->table_gfn[walker->level - 1] = table_gfn;
  304. walker->pte_gpa[walker->level - 1] = pte_gpa;
  305. real_gfn = mmu->translate_gpa(vcpu, gfn_to_gpa(table_gfn),
  306. nested_access,
  307. &walker->fault);
  308. /*
  309. * FIXME: This can happen if emulation (for of an INS/OUTS
  310. * instruction) triggers a nested page fault. The exit
  311. * qualification / exit info field will incorrectly have
  312. * "guest page access" as the nested page fault's cause,
  313. * instead of "guest page structure access". To fix this,
  314. * the x86_exception struct should be augmented with enough
  315. * information to fix the exit_qualification or exit_info_1
  316. * fields.
  317. */
  318. if (unlikely(real_gfn == UNMAPPED_GVA))
  319. return 0;
  320. real_gfn = gpa_to_gfn(real_gfn);
  321. host_addr = kvm_vcpu_gfn_to_hva_prot(vcpu, real_gfn,
  322. &walker->pte_writable[walker->level - 1]);
  323. if (unlikely(kvm_is_error_hva(host_addr)))
  324. goto error;
  325. ptep_user = (pt_element_t __user *)((void *)host_addr + offset);
  326. if (unlikely(__copy_from_user(&pte, ptep_user, sizeof(pte))))
  327. goto error;
  328. walker->ptep_user[walker->level - 1] = ptep_user;
  329. trace_kvm_mmu_paging_element(pte, walker->level);
  330. /*
  331. * Inverting the NX it lets us AND it like other
  332. * permission bits.
  333. */
  334. pte_access = pt_access & (pte ^ walk_nx_mask);
  335. if (unlikely(!FNAME(is_present_gpte)(pte)))
  336. goto error;
  337. if (unlikely(is_rsvd_bits_set(mmu, pte, walker->level))) {
  338. errcode = PFERR_RSVD_MASK | PFERR_PRESENT_MASK;
  339. goto error;
  340. }
  341. walker->ptes[walker->level - 1] = pte;
  342. } while (!is_last_gpte(mmu, walker->level, pte));
  343. pte_pkey = FNAME(gpte_pkeys)(vcpu, pte);
  344. accessed_dirty = have_ad ? pte_access & PT_GUEST_ACCESSED_MASK : 0;
  345. /* Convert to ACC_*_MASK flags for struct guest_walker. */
  346. walker->pt_access = FNAME(gpte_access)(pt_access ^ walk_nx_mask);
  347. walker->pte_access = FNAME(gpte_access)(pte_access ^ walk_nx_mask);
  348. errcode = permission_fault(vcpu, mmu, walker->pte_access, pte_pkey, access);
  349. if (unlikely(errcode))
  350. goto error;
  351. gfn = gpte_to_gfn_lvl(pte, walker->level);
  352. gfn += (addr & PT_LVL_OFFSET_MASK(walker->level)) >> PAGE_SHIFT;
  353. if (PTTYPE == 32 && walker->level == PT_DIRECTORY_LEVEL && is_cpuid_PSE36())
  354. gfn += pse36_gfn_delta(pte);
  355. real_gpa = mmu->translate_gpa(vcpu, gfn_to_gpa(gfn), access, &walker->fault);
  356. if (real_gpa == UNMAPPED_GVA)
  357. return 0;
  358. walker->gfn = real_gpa >> PAGE_SHIFT;
  359. if (!write_fault)
  360. FNAME(protect_clean_gpte)(mmu, &walker->pte_access, pte);
  361. else
  362. /*
  363. * On a write fault, fold the dirty bit into accessed_dirty.
  364. * For modes without A/D bits support accessed_dirty will be
  365. * always clear.
  366. */
  367. accessed_dirty &= pte >>
  368. (PT_GUEST_DIRTY_SHIFT - PT_GUEST_ACCESSED_SHIFT);
  369. if (unlikely(!accessed_dirty)) {
  370. ret = FNAME(update_accessed_dirty_bits)(vcpu, mmu, walker, write_fault);
  371. if (unlikely(ret < 0))
  372. goto error;
  373. else if (ret)
  374. goto retry_walk;
  375. }
  376. pgprintk("%s: pte %llx pte_access %x pt_access %x\n",
  377. __func__, (u64)pte, walker->pte_access, walker->pt_access);
  378. return 1;
  379. error:
  380. errcode |= write_fault | user_fault;
  381. if (fetch_fault && (mmu->nx ||
  382. kvm_read_cr4_bits(vcpu, X86_CR4_SMEP)))
  383. errcode |= PFERR_FETCH_MASK;
  384. walker->fault.vector = PF_VECTOR;
  385. walker->fault.error_code_valid = true;
  386. walker->fault.error_code = errcode;
  387. #if PTTYPE == PTTYPE_EPT
  388. /*
  389. * Use PFERR_RSVD_MASK in error_code to to tell if EPT
  390. * misconfiguration requires to be injected. The detection is
  391. * done by is_rsvd_bits_set() above.
  392. *
  393. * We set up the value of exit_qualification to inject:
  394. * [2:0] - Derive from the access bits. The exit_qualification might be
  395. * out of date if it is serving an EPT misconfiguration.
  396. * [5:3] - Calculated by the page walk of the guest EPT page tables
  397. * [7:8] - Derived from [7:8] of real exit_qualification
  398. *
  399. * The other bits are set to 0.
  400. */
  401. if (!(errcode & PFERR_RSVD_MASK)) {
  402. vcpu->arch.exit_qualification &= 0x180;
  403. if (write_fault)
  404. vcpu->arch.exit_qualification |= EPT_VIOLATION_ACC_WRITE;
  405. if (user_fault)
  406. vcpu->arch.exit_qualification |= EPT_VIOLATION_ACC_READ;
  407. if (fetch_fault)
  408. vcpu->arch.exit_qualification |= EPT_VIOLATION_ACC_INSTR;
  409. vcpu->arch.exit_qualification |= (pte_access & 0x7) << 3;
  410. }
  411. #endif
  412. walker->fault.address = addr;
  413. walker->fault.nested_page_fault = mmu != vcpu->arch.walk_mmu;
  414. trace_kvm_mmu_walker_error(walker->fault.error_code);
  415. return 0;
  416. }
  417. static int FNAME(walk_addr)(struct guest_walker *walker,
  418. struct kvm_vcpu *vcpu, gva_t addr, u32 access)
  419. {
  420. return FNAME(walk_addr_generic)(walker, vcpu, vcpu->arch.mmu, addr,
  421. access);
  422. }
  423. #if PTTYPE != PTTYPE_EPT
  424. static int FNAME(walk_addr_nested)(struct guest_walker *walker,
  425. struct kvm_vcpu *vcpu, gva_t addr,
  426. u32 access)
  427. {
  428. return FNAME(walk_addr_generic)(walker, vcpu, &vcpu->arch.nested_mmu,
  429. addr, access);
  430. }
  431. #endif
  432. static bool
  433. FNAME(prefetch_gpte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
  434. u64 *spte, pt_element_t gpte, bool no_dirty_log)
  435. {
  436. unsigned pte_access;
  437. gfn_t gfn;
  438. kvm_pfn_t pfn;
  439. if (FNAME(prefetch_invalid_gpte)(vcpu, sp, spte, gpte))
  440. return false;
  441. pgprintk("%s: gpte %llx spte %p\n", __func__, (u64)gpte, spte);
  442. gfn = gpte_to_gfn(gpte);
  443. pte_access = sp->role.access & FNAME(gpte_access)(gpte);
  444. FNAME(protect_clean_gpte)(vcpu->arch.mmu, &pte_access, gpte);
  445. pfn = pte_prefetch_gfn_to_pfn(vcpu, gfn,
  446. no_dirty_log && (pte_access & ACC_WRITE_MASK));
  447. if (is_error_pfn(pfn))
  448. return false;
  449. /*
  450. * we call mmu_set_spte() with host_writable = true because
  451. * pte_prefetch_gfn_to_pfn always gets a writable pfn.
  452. */
  453. mmu_set_spte(vcpu, spte, pte_access, 0, PT_PAGE_TABLE_LEVEL, gfn, pfn,
  454. true, true);
  455. return true;
  456. }
  457. static void FNAME(update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
  458. u64 *spte, const void *pte)
  459. {
  460. pt_element_t gpte = *(const pt_element_t *)pte;
  461. FNAME(prefetch_gpte)(vcpu, sp, spte, gpte, false);
  462. }
  463. static bool FNAME(gpte_changed)(struct kvm_vcpu *vcpu,
  464. struct guest_walker *gw, int level)
  465. {
  466. pt_element_t curr_pte;
  467. gpa_t base_gpa, pte_gpa = gw->pte_gpa[level - 1];
  468. u64 mask;
  469. int r, index;
  470. if (level == PT_PAGE_TABLE_LEVEL) {
  471. mask = PTE_PREFETCH_NUM * sizeof(pt_element_t) - 1;
  472. base_gpa = pte_gpa & ~mask;
  473. index = (pte_gpa - base_gpa) / sizeof(pt_element_t);
  474. r = kvm_vcpu_read_guest_atomic(vcpu, base_gpa,
  475. gw->prefetch_ptes, sizeof(gw->prefetch_ptes));
  476. curr_pte = gw->prefetch_ptes[index];
  477. } else
  478. r = kvm_vcpu_read_guest_atomic(vcpu, pte_gpa,
  479. &curr_pte, sizeof(curr_pte));
  480. return r || curr_pte != gw->ptes[level - 1];
  481. }
  482. static void FNAME(pte_prefetch)(struct kvm_vcpu *vcpu, struct guest_walker *gw,
  483. u64 *sptep)
  484. {
  485. struct kvm_mmu_page *sp;
  486. pt_element_t *gptep = gw->prefetch_ptes;
  487. u64 *spte;
  488. int i;
  489. sp = page_header(__pa(sptep));
  490. if (sp->role.level > PT_PAGE_TABLE_LEVEL)
  491. return;
  492. if (sp->role.direct)
  493. return __direct_pte_prefetch(vcpu, sp, sptep);
  494. i = (sptep - sp->spt) & ~(PTE_PREFETCH_NUM - 1);
  495. spte = sp->spt + i;
  496. for (i = 0; i < PTE_PREFETCH_NUM; i++, spte++) {
  497. if (spte == sptep)
  498. continue;
  499. if (is_shadow_present_pte(*spte))
  500. continue;
  501. if (!FNAME(prefetch_gpte)(vcpu, sp, spte, gptep[i], true))
  502. break;
  503. }
  504. }
  505. /*
  506. * Fetch a shadow pte for a specific level in the paging hierarchy.
  507. * If the guest tries to write a write-protected page, we need to
  508. * emulate this operation, return 1 to indicate this case.
  509. */
  510. static int FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
  511. struct guest_walker *gw,
  512. int write_fault, int hlevel,
  513. kvm_pfn_t pfn, bool map_writable, bool prefault)
  514. {
  515. struct kvm_mmu_page *sp = NULL;
  516. struct kvm_shadow_walk_iterator it;
  517. unsigned direct_access, access = gw->pt_access;
  518. int top_level, ret;
  519. direct_access = gw->pte_access;
  520. top_level = vcpu->arch.mmu->root_level;
  521. if (top_level == PT32E_ROOT_LEVEL)
  522. top_level = PT32_ROOT_LEVEL;
  523. /*
  524. * Verify that the top-level gpte is still there. Since the page
  525. * is a root page, it is either write protected (and cannot be
  526. * changed from now on) or it is invalid (in which case, we don't
  527. * really care if it changes underneath us after this point).
  528. */
  529. if (FNAME(gpte_changed)(vcpu, gw, top_level))
  530. goto out_gpte_changed;
  531. if (!VALID_PAGE(vcpu->arch.mmu->root_hpa))
  532. goto out_gpte_changed;
  533. for (shadow_walk_init(&it, vcpu, addr);
  534. shadow_walk_okay(&it) && it.level > gw->level;
  535. shadow_walk_next(&it)) {
  536. gfn_t table_gfn;
  537. clear_sp_write_flooding_count(it.sptep);
  538. drop_large_spte(vcpu, it.sptep);
  539. sp = NULL;
  540. if (!is_shadow_present_pte(*it.sptep)) {
  541. table_gfn = gw->table_gfn[it.level - 2];
  542. sp = kvm_mmu_get_page(vcpu, table_gfn, addr, it.level-1,
  543. false, access);
  544. }
  545. /*
  546. * Verify that the gpte in the page we've just write
  547. * protected is still there.
  548. */
  549. if (FNAME(gpte_changed)(vcpu, gw, it.level - 1))
  550. goto out_gpte_changed;
  551. if (sp)
  552. link_shadow_page(vcpu, it.sptep, sp);
  553. }
  554. for (;
  555. shadow_walk_okay(&it) && it.level > hlevel;
  556. shadow_walk_next(&it)) {
  557. gfn_t direct_gfn;
  558. clear_sp_write_flooding_count(it.sptep);
  559. validate_direct_spte(vcpu, it.sptep, direct_access);
  560. drop_large_spte(vcpu, it.sptep);
  561. if (is_shadow_present_pte(*it.sptep))
  562. continue;
  563. direct_gfn = gw->gfn & ~(KVM_PAGES_PER_HPAGE(it.level) - 1);
  564. sp = kvm_mmu_get_page(vcpu, direct_gfn, addr, it.level-1,
  565. true, direct_access);
  566. link_shadow_page(vcpu, it.sptep, sp);
  567. }
  568. clear_sp_write_flooding_count(it.sptep);
  569. ret = mmu_set_spte(vcpu, it.sptep, gw->pte_access, write_fault,
  570. it.level, gw->gfn, pfn, prefault, map_writable);
  571. FNAME(pte_prefetch)(vcpu, gw, it.sptep);
  572. return ret;
  573. out_gpte_changed:
  574. kvm_release_pfn_clean(pfn);
  575. return RET_PF_RETRY;
  576. }
  577. /*
  578. * To see whether the mapped gfn can write its page table in the current
  579. * mapping.
  580. *
  581. * It is the helper function of FNAME(page_fault). When guest uses large page
  582. * size to map the writable gfn which is used as current page table, we should
  583. * force kvm to use small page size to map it because new shadow page will be
  584. * created when kvm establishes shadow page table that stop kvm using large
  585. * page size. Do it early can avoid unnecessary #PF and emulation.
  586. *
  587. * @write_fault_to_shadow_pgtable will return true if the fault gfn is
  588. * currently used as its page table.
  589. *
  590. * Note: the PDPT page table is not checked for PAE-32 bit guest. It is ok
  591. * since the PDPT is always shadowed, that means, we can not use large page
  592. * size to map the gfn which is used as PDPT.
  593. */
  594. static bool
  595. FNAME(is_self_change_mapping)(struct kvm_vcpu *vcpu,
  596. struct guest_walker *walker, int user_fault,
  597. bool *write_fault_to_shadow_pgtable)
  598. {
  599. int level;
  600. gfn_t mask = ~(KVM_PAGES_PER_HPAGE(walker->level) - 1);
  601. bool self_changed = false;
  602. if (!(walker->pte_access & ACC_WRITE_MASK ||
  603. (!is_write_protection(vcpu) && !user_fault)))
  604. return false;
  605. for (level = walker->level; level <= walker->max_level; level++) {
  606. gfn_t gfn = walker->gfn ^ walker->table_gfn[level - 1];
  607. self_changed |= !(gfn & mask);
  608. *write_fault_to_shadow_pgtable |= !gfn;
  609. }
  610. return self_changed;
  611. }
  612. /*
  613. * Page fault handler. There are several causes for a page fault:
  614. * - there is no shadow pte for the guest pte
  615. * - write access through a shadow pte marked read only so that we can set
  616. * the dirty bit
  617. * - write access to a shadow pte marked read only so we can update the page
  618. * dirty bitmap, when userspace requests it
  619. * - mmio access; in this case we will never install a present shadow pte
  620. * - normal guest page fault due to the guest pte marked not present, not
  621. * writable, or not executable
  622. *
  623. * Returns: 1 if we need to emulate the instruction, 0 otherwise, or
  624. * a negative value on error.
  625. */
  626. static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr, u32 error_code,
  627. bool prefault)
  628. {
  629. int write_fault = error_code & PFERR_WRITE_MASK;
  630. int user_fault = error_code & PFERR_USER_MASK;
  631. struct guest_walker walker;
  632. int r;
  633. kvm_pfn_t pfn;
  634. int level = PT_PAGE_TABLE_LEVEL;
  635. bool force_pt_level = false;
  636. unsigned long mmu_seq;
  637. bool map_writable, is_self_change_mapping;
  638. pgprintk("%s: addr %lx err %x\n", __func__, addr, error_code);
  639. r = mmu_topup_memory_caches(vcpu);
  640. if (r)
  641. return r;
  642. /*
  643. * If PFEC.RSVD is set, this is a shadow page fault.
  644. * The bit needs to be cleared before walking guest page tables.
  645. */
  646. error_code &= ~PFERR_RSVD_MASK;
  647. /*
  648. * Look up the guest pte for the faulting address.
  649. */
  650. r = FNAME(walk_addr)(&walker, vcpu, addr, error_code);
  651. /*
  652. * The page is not mapped by the guest. Let the guest handle it.
  653. */
  654. if (!r) {
  655. pgprintk("%s: guest page fault\n", __func__);
  656. if (!prefault)
  657. inject_page_fault(vcpu, &walker.fault);
  658. return RET_PF_RETRY;
  659. }
  660. if (page_fault_handle_page_track(vcpu, error_code, walker.gfn)) {
  661. shadow_page_table_clear_flood(vcpu, addr);
  662. return RET_PF_EMULATE;
  663. }
  664. vcpu->arch.write_fault_to_shadow_pgtable = false;
  665. is_self_change_mapping = FNAME(is_self_change_mapping)(vcpu,
  666. &walker, user_fault, &vcpu->arch.write_fault_to_shadow_pgtable);
  667. if (walker.level >= PT_DIRECTORY_LEVEL && !is_self_change_mapping) {
  668. level = mapping_level(vcpu, walker.gfn, &force_pt_level);
  669. if (likely(!force_pt_level)) {
  670. level = min(walker.level, level);
  671. walker.gfn = walker.gfn & ~(KVM_PAGES_PER_HPAGE(level) - 1);
  672. }
  673. } else
  674. force_pt_level = true;
  675. mmu_seq = vcpu->kvm->mmu_notifier_seq;
  676. smp_rmb();
  677. if (try_async_pf(vcpu, prefault, walker.gfn, addr, &pfn, write_fault,
  678. &map_writable))
  679. return RET_PF_RETRY;
  680. if (handle_abnormal_pfn(vcpu, addr, walker.gfn, pfn, walker.pte_access, &r))
  681. return r;
  682. /*
  683. * Do not change pte_access if the pfn is a mmio page, otherwise
  684. * we will cache the incorrect access into mmio spte.
  685. */
  686. if (write_fault && !(walker.pte_access & ACC_WRITE_MASK) &&
  687. !is_write_protection(vcpu) && !user_fault &&
  688. !is_noslot_pfn(pfn)) {
  689. walker.pte_access |= ACC_WRITE_MASK;
  690. walker.pte_access &= ~ACC_USER_MASK;
  691. /*
  692. * If we converted a user page to a kernel page,
  693. * so that the kernel can write to it when cr0.wp=0,
  694. * then we should prevent the kernel from executing it
  695. * if SMEP is enabled.
  696. */
  697. if (kvm_read_cr4_bits(vcpu, X86_CR4_SMEP))
  698. walker.pte_access &= ~ACC_EXEC_MASK;
  699. }
  700. spin_lock(&vcpu->kvm->mmu_lock);
  701. if (mmu_notifier_retry(vcpu->kvm, mmu_seq))
  702. goto out_unlock;
  703. kvm_mmu_audit(vcpu, AUDIT_PRE_PAGE_FAULT);
  704. if (make_mmu_pages_available(vcpu) < 0)
  705. goto out_unlock;
  706. if (!force_pt_level)
  707. transparent_hugepage_adjust(vcpu, &walker.gfn, &pfn, &level);
  708. r = FNAME(fetch)(vcpu, addr, &walker, write_fault,
  709. level, pfn, map_writable, prefault);
  710. ++vcpu->stat.pf_fixed;
  711. kvm_mmu_audit(vcpu, AUDIT_POST_PAGE_FAULT);
  712. spin_unlock(&vcpu->kvm->mmu_lock);
  713. return r;
  714. out_unlock:
  715. spin_unlock(&vcpu->kvm->mmu_lock);
  716. kvm_release_pfn_clean(pfn);
  717. return RET_PF_RETRY;
  718. }
  719. static gpa_t FNAME(get_level1_sp_gpa)(struct kvm_mmu_page *sp)
  720. {
  721. int offset = 0;
  722. WARN_ON(sp->role.level != PT_PAGE_TABLE_LEVEL);
  723. if (PTTYPE == 32)
  724. offset = sp->role.quadrant << PT64_LEVEL_BITS;
  725. return gfn_to_gpa(sp->gfn) + offset * sizeof(pt_element_t);
  726. }
  727. static void FNAME(invlpg)(struct kvm_vcpu *vcpu, gva_t gva, hpa_t root_hpa)
  728. {
  729. struct kvm_shadow_walk_iterator iterator;
  730. struct kvm_mmu_page *sp;
  731. int level;
  732. u64 *sptep;
  733. vcpu_clear_mmio_info(vcpu, gva);
  734. /*
  735. * No need to check return value here, rmap_can_add() can
  736. * help us to skip pte prefetch later.
  737. */
  738. mmu_topup_memory_caches(vcpu);
  739. if (!VALID_PAGE(root_hpa)) {
  740. WARN_ON(1);
  741. return;
  742. }
  743. spin_lock(&vcpu->kvm->mmu_lock);
  744. for_each_shadow_entry_using_root(vcpu, root_hpa, gva, iterator) {
  745. level = iterator.level;
  746. sptep = iterator.sptep;
  747. sp = page_header(__pa(sptep));
  748. if (is_last_spte(*sptep, level)) {
  749. pt_element_t gpte;
  750. gpa_t pte_gpa;
  751. if (!sp->unsync)
  752. break;
  753. pte_gpa = FNAME(get_level1_sp_gpa)(sp);
  754. pte_gpa += (sptep - sp->spt) * sizeof(pt_element_t);
  755. if (mmu_page_zap_pte(vcpu->kvm, sp, sptep))
  756. kvm_flush_remote_tlbs(vcpu->kvm);
  757. if (!rmap_can_add(vcpu))
  758. break;
  759. if (kvm_vcpu_read_guest_atomic(vcpu, pte_gpa, &gpte,
  760. sizeof(pt_element_t)))
  761. break;
  762. FNAME(update_pte)(vcpu, sp, sptep, &gpte);
  763. }
  764. if (!is_shadow_present_pte(*sptep) || !sp->unsync_children)
  765. break;
  766. }
  767. spin_unlock(&vcpu->kvm->mmu_lock);
  768. }
  769. static gpa_t FNAME(gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t vaddr, u32 access,
  770. struct x86_exception *exception)
  771. {
  772. struct guest_walker walker;
  773. gpa_t gpa = UNMAPPED_GVA;
  774. int r;
  775. r = FNAME(walk_addr)(&walker, vcpu, vaddr, access);
  776. if (r) {
  777. gpa = gfn_to_gpa(walker.gfn);
  778. gpa |= vaddr & ~PAGE_MASK;
  779. } else if (exception)
  780. *exception = walker.fault;
  781. return gpa;
  782. }
  783. #if PTTYPE != PTTYPE_EPT
  784. static gpa_t FNAME(gva_to_gpa_nested)(struct kvm_vcpu *vcpu, gva_t vaddr,
  785. u32 access,
  786. struct x86_exception *exception)
  787. {
  788. struct guest_walker walker;
  789. gpa_t gpa = UNMAPPED_GVA;
  790. int r;
  791. r = FNAME(walk_addr_nested)(&walker, vcpu, vaddr, access);
  792. if (r) {
  793. gpa = gfn_to_gpa(walker.gfn);
  794. gpa |= vaddr & ~PAGE_MASK;
  795. } else if (exception)
  796. *exception = walker.fault;
  797. return gpa;
  798. }
  799. #endif
  800. /*
  801. * Using the cached information from sp->gfns is safe because:
  802. * - The spte has a reference to the struct page, so the pfn for a given gfn
  803. * can't change unless all sptes pointing to it are nuked first.
  804. *
  805. * Note:
  806. * We should flush all tlbs if spte is dropped even though guest is
  807. * responsible for it. Since if we don't, kvm_mmu_notifier_invalidate_page
  808. * and kvm_mmu_notifier_invalidate_range_start detect the mapping page isn't
  809. * used by guest then tlbs are not flushed, so guest is allowed to access the
  810. * freed pages.
  811. * And we increase kvm->tlbs_dirty to delay tlbs flush in this case.
  812. */
  813. static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
  814. {
  815. int i, nr_present = 0;
  816. bool host_writable;
  817. gpa_t first_pte_gpa;
  818. int set_spte_ret = 0;
  819. /* direct kvm_mmu_page can not be unsync. */
  820. BUG_ON(sp->role.direct);
  821. first_pte_gpa = FNAME(get_level1_sp_gpa)(sp);
  822. for (i = 0; i < PT64_ENT_PER_PAGE; i++) {
  823. unsigned pte_access;
  824. pt_element_t gpte;
  825. gpa_t pte_gpa;
  826. gfn_t gfn;
  827. if (!sp->spt[i])
  828. continue;
  829. pte_gpa = first_pte_gpa + i * sizeof(pt_element_t);
  830. if (kvm_vcpu_read_guest_atomic(vcpu, pte_gpa, &gpte,
  831. sizeof(pt_element_t)))
  832. return 0;
  833. if (FNAME(prefetch_invalid_gpte)(vcpu, sp, &sp->spt[i], gpte)) {
  834. /*
  835. * Update spte before increasing tlbs_dirty to make
  836. * sure no tlb flush is lost after spte is zapped; see
  837. * the comments in kvm_flush_remote_tlbs().
  838. */
  839. smp_wmb();
  840. vcpu->kvm->tlbs_dirty++;
  841. continue;
  842. }
  843. gfn = gpte_to_gfn(gpte);
  844. pte_access = sp->role.access;
  845. pte_access &= FNAME(gpte_access)(gpte);
  846. FNAME(protect_clean_gpte)(vcpu->arch.mmu, &pte_access, gpte);
  847. if (sync_mmio_spte(vcpu, &sp->spt[i], gfn, pte_access,
  848. &nr_present))
  849. continue;
  850. if (gfn != sp->gfns[i]) {
  851. drop_spte(vcpu->kvm, &sp->spt[i]);
  852. /*
  853. * The same as above where we are doing
  854. * prefetch_invalid_gpte().
  855. */
  856. smp_wmb();
  857. vcpu->kvm->tlbs_dirty++;
  858. continue;
  859. }
  860. nr_present++;
  861. host_writable = sp->spt[i] & SPTE_HOST_WRITEABLE;
  862. set_spte_ret |= set_spte(vcpu, &sp->spt[i],
  863. pte_access, PT_PAGE_TABLE_LEVEL,
  864. gfn, spte_to_pfn(sp->spt[i]),
  865. true, false, host_writable);
  866. }
  867. if (set_spte_ret & SET_SPTE_NEED_REMOTE_TLB_FLUSH)
  868. kvm_flush_remote_tlbs(vcpu->kvm);
  869. return nr_present;
  870. }
  871. #undef pt_element_t
  872. #undef guest_walker
  873. #undef FNAME
  874. #undef PT_BASE_ADDR_MASK
  875. #undef PT_INDEX
  876. #undef PT_LVL_ADDR_MASK
  877. #undef PT_LVL_OFFSET_MASK
  878. #undef PT_LEVEL_BITS
  879. #undef PT_MAX_FULL_LEVELS
  880. #undef gpte_to_gfn
  881. #undef gpte_to_gfn_lvl
  882. #undef CMPXCHG
  883. #undef PT_GUEST_ACCESSED_MASK
  884. #undef PT_GUEST_DIRTY_MASK
  885. #undef PT_GUEST_DIRTY_SHIFT
  886. #undef PT_GUEST_ACCESSED_SHIFT
  887. #undef PT_HAVE_ACCESSED_DIRTY