paging_tmpl.h 29 KB

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