mmu.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256
  1. /*
  2. * Copyright (C) 2012 - Virtual Open Systems and Columbia University
  3. * Author: Christoffer Dall <c.dall@virtualopensystems.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License, version 2, as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. */
  18. #include <linux/mman.h>
  19. #include <linux/kvm_host.h>
  20. #include <linux/io.h>
  21. #include <linux/hugetlb.h>
  22. #include <linux/sched/signal.h>
  23. #include <trace/events/kvm.h>
  24. #include <asm/pgalloc.h>
  25. #include <asm/cacheflush.h>
  26. #include <asm/kvm_arm.h>
  27. #include <asm/kvm_mmu.h>
  28. #include <asm/kvm_mmio.h>
  29. #include <asm/kvm_asm.h>
  30. #include <asm/kvm_emulate.h>
  31. #include <asm/virt.h>
  32. #include <asm/system_misc.h>
  33. #include "trace.h"
  34. static pgd_t *boot_hyp_pgd;
  35. static pgd_t *hyp_pgd;
  36. static pgd_t *merged_hyp_pgd;
  37. static DEFINE_MUTEX(kvm_hyp_pgd_mutex);
  38. static unsigned long hyp_idmap_start;
  39. static unsigned long hyp_idmap_end;
  40. static phys_addr_t hyp_idmap_vector;
  41. static unsigned long io_map_base;
  42. #define S2_PGD_SIZE (PTRS_PER_S2_PGD * sizeof(pgd_t))
  43. #define hyp_pgd_order get_order(PTRS_PER_PGD * sizeof(pgd_t))
  44. #define KVM_S2PTE_FLAG_IS_IOMAP (1UL << 0)
  45. #define KVM_S2_FLAG_LOGGING_ACTIVE (1UL << 1)
  46. static bool memslot_is_logging(struct kvm_memory_slot *memslot)
  47. {
  48. return memslot->dirty_bitmap && !(memslot->flags & KVM_MEM_READONLY);
  49. }
  50. /**
  51. * kvm_flush_remote_tlbs() - flush all VM TLB entries for v7/8
  52. * @kvm: pointer to kvm structure.
  53. *
  54. * Interface to HYP function to flush all VM TLB entries
  55. */
  56. void kvm_flush_remote_tlbs(struct kvm *kvm)
  57. {
  58. kvm_call_hyp(__kvm_tlb_flush_vmid, kvm);
  59. }
  60. static void kvm_tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa)
  61. {
  62. kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, kvm, ipa);
  63. }
  64. /*
  65. * D-Cache management functions. They take the page table entries by
  66. * value, as they are flushing the cache using the kernel mapping (or
  67. * kmap on 32bit).
  68. */
  69. static void kvm_flush_dcache_pte(pte_t pte)
  70. {
  71. __kvm_flush_dcache_pte(pte);
  72. }
  73. static void kvm_flush_dcache_pmd(pmd_t pmd)
  74. {
  75. __kvm_flush_dcache_pmd(pmd);
  76. }
  77. static void kvm_flush_dcache_pud(pud_t pud)
  78. {
  79. __kvm_flush_dcache_pud(pud);
  80. }
  81. static bool kvm_is_device_pfn(unsigned long pfn)
  82. {
  83. return !pfn_valid(pfn);
  84. }
  85. /**
  86. * stage2_dissolve_pmd() - clear and flush huge PMD entry
  87. * @kvm: pointer to kvm structure.
  88. * @addr: IPA
  89. * @pmd: pmd pointer for IPA
  90. *
  91. * Function clears a PMD entry, flushes addr 1st and 2nd stage TLBs. Marks all
  92. * pages in the range dirty.
  93. */
  94. static void stage2_dissolve_pmd(struct kvm *kvm, phys_addr_t addr, pmd_t *pmd)
  95. {
  96. if (!pmd_thp_or_huge(*pmd))
  97. return;
  98. pmd_clear(pmd);
  99. kvm_tlb_flush_vmid_ipa(kvm, addr);
  100. put_page(virt_to_page(pmd));
  101. }
  102. static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
  103. int min, int max)
  104. {
  105. void *page;
  106. BUG_ON(max > KVM_NR_MEM_OBJS);
  107. if (cache->nobjs >= min)
  108. return 0;
  109. while (cache->nobjs < max) {
  110. page = (void *)__get_free_page(PGALLOC_GFP);
  111. if (!page)
  112. return -ENOMEM;
  113. cache->objects[cache->nobjs++] = page;
  114. }
  115. return 0;
  116. }
  117. static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
  118. {
  119. while (mc->nobjs)
  120. free_page((unsigned long)mc->objects[--mc->nobjs]);
  121. }
  122. static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc)
  123. {
  124. void *p;
  125. BUG_ON(!mc || !mc->nobjs);
  126. p = mc->objects[--mc->nobjs];
  127. return p;
  128. }
  129. static void clear_stage2_pgd_entry(struct kvm *kvm, pgd_t *pgd, phys_addr_t addr)
  130. {
  131. pud_t *pud_table __maybe_unused = stage2_pud_offset(pgd, 0UL);
  132. stage2_pgd_clear(pgd);
  133. kvm_tlb_flush_vmid_ipa(kvm, addr);
  134. stage2_pud_free(pud_table);
  135. put_page(virt_to_page(pgd));
  136. }
  137. static void clear_stage2_pud_entry(struct kvm *kvm, pud_t *pud, phys_addr_t addr)
  138. {
  139. pmd_t *pmd_table __maybe_unused = stage2_pmd_offset(pud, 0);
  140. VM_BUG_ON(stage2_pud_huge(*pud));
  141. stage2_pud_clear(pud);
  142. kvm_tlb_flush_vmid_ipa(kvm, addr);
  143. stage2_pmd_free(pmd_table);
  144. put_page(virt_to_page(pud));
  145. }
  146. static void clear_stage2_pmd_entry(struct kvm *kvm, pmd_t *pmd, phys_addr_t addr)
  147. {
  148. pte_t *pte_table = pte_offset_kernel(pmd, 0);
  149. VM_BUG_ON(pmd_thp_or_huge(*pmd));
  150. pmd_clear(pmd);
  151. kvm_tlb_flush_vmid_ipa(kvm, addr);
  152. pte_free_kernel(NULL, pte_table);
  153. put_page(virt_to_page(pmd));
  154. }
  155. static inline void kvm_set_pte(pte_t *ptep, pte_t new_pte)
  156. {
  157. WRITE_ONCE(*ptep, new_pte);
  158. dsb(ishst);
  159. }
  160. static inline void kvm_set_pmd(pmd_t *pmdp, pmd_t new_pmd)
  161. {
  162. WRITE_ONCE(*pmdp, new_pmd);
  163. dsb(ishst);
  164. }
  165. static inline void kvm_pmd_populate(pmd_t *pmdp, pte_t *ptep)
  166. {
  167. kvm_set_pmd(pmdp, kvm_mk_pmd(ptep));
  168. }
  169. static inline void kvm_pud_populate(pud_t *pudp, pmd_t *pmdp)
  170. {
  171. WRITE_ONCE(*pudp, kvm_mk_pud(pmdp));
  172. dsb(ishst);
  173. }
  174. static inline void kvm_pgd_populate(pgd_t *pgdp, pud_t *pudp)
  175. {
  176. WRITE_ONCE(*pgdp, kvm_mk_pgd(pudp));
  177. dsb(ishst);
  178. }
  179. /*
  180. * Unmapping vs dcache management:
  181. *
  182. * If a guest maps certain memory pages as uncached, all writes will
  183. * bypass the data cache and go directly to RAM. However, the CPUs
  184. * can still speculate reads (not writes) and fill cache lines with
  185. * data.
  186. *
  187. * Those cache lines will be *clean* cache lines though, so a
  188. * clean+invalidate operation is equivalent to an invalidate
  189. * operation, because no cache lines are marked dirty.
  190. *
  191. * Those clean cache lines could be filled prior to an uncached write
  192. * by the guest, and the cache coherent IO subsystem would therefore
  193. * end up writing old data to disk.
  194. *
  195. * This is why right after unmapping a page/section and invalidating
  196. * the corresponding TLBs, we call kvm_flush_dcache_p*() to make sure
  197. * the IO subsystem will never hit in the cache.
  198. *
  199. * This is all avoided on systems that have ARM64_HAS_STAGE2_FWB, as
  200. * we then fully enforce cacheability of RAM, no matter what the guest
  201. * does.
  202. */
  203. static void unmap_stage2_ptes(struct kvm *kvm, pmd_t *pmd,
  204. phys_addr_t addr, phys_addr_t end)
  205. {
  206. phys_addr_t start_addr = addr;
  207. pte_t *pte, *start_pte;
  208. start_pte = pte = pte_offset_kernel(pmd, addr);
  209. do {
  210. if (!pte_none(*pte)) {
  211. pte_t old_pte = *pte;
  212. kvm_set_pte(pte, __pte(0));
  213. kvm_tlb_flush_vmid_ipa(kvm, addr);
  214. /* No need to invalidate the cache for device mappings */
  215. if (!kvm_is_device_pfn(pte_pfn(old_pte)))
  216. kvm_flush_dcache_pte(old_pte);
  217. put_page(virt_to_page(pte));
  218. }
  219. } while (pte++, addr += PAGE_SIZE, addr != end);
  220. if (stage2_pte_table_empty(start_pte))
  221. clear_stage2_pmd_entry(kvm, pmd, start_addr);
  222. }
  223. static void unmap_stage2_pmds(struct kvm *kvm, pud_t *pud,
  224. phys_addr_t addr, phys_addr_t end)
  225. {
  226. phys_addr_t next, start_addr = addr;
  227. pmd_t *pmd, *start_pmd;
  228. start_pmd = pmd = stage2_pmd_offset(pud, addr);
  229. do {
  230. next = stage2_pmd_addr_end(addr, end);
  231. if (!pmd_none(*pmd)) {
  232. if (pmd_thp_or_huge(*pmd)) {
  233. pmd_t old_pmd = *pmd;
  234. pmd_clear(pmd);
  235. kvm_tlb_flush_vmid_ipa(kvm, addr);
  236. kvm_flush_dcache_pmd(old_pmd);
  237. put_page(virt_to_page(pmd));
  238. } else {
  239. unmap_stage2_ptes(kvm, pmd, addr, next);
  240. }
  241. }
  242. } while (pmd++, addr = next, addr != end);
  243. if (stage2_pmd_table_empty(start_pmd))
  244. clear_stage2_pud_entry(kvm, pud, start_addr);
  245. }
  246. static void unmap_stage2_puds(struct kvm *kvm, pgd_t *pgd,
  247. phys_addr_t addr, phys_addr_t end)
  248. {
  249. phys_addr_t next, start_addr = addr;
  250. pud_t *pud, *start_pud;
  251. start_pud = pud = stage2_pud_offset(pgd, addr);
  252. do {
  253. next = stage2_pud_addr_end(addr, end);
  254. if (!stage2_pud_none(*pud)) {
  255. if (stage2_pud_huge(*pud)) {
  256. pud_t old_pud = *pud;
  257. stage2_pud_clear(pud);
  258. kvm_tlb_flush_vmid_ipa(kvm, addr);
  259. kvm_flush_dcache_pud(old_pud);
  260. put_page(virt_to_page(pud));
  261. } else {
  262. unmap_stage2_pmds(kvm, pud, addr, next);
  263. }
  264. }
  265. } while (pud++, addr = next, addr != end);
  266. if (stage2_pud_table_empty(start_pud))
  267. clear_stage2_pgd_entry(kvm, pgd, start_addr);
  268. }
  269. /**
  270. * unmap_stage2_range -- Clear stage2 page table entries to unmap a range
  271. * @kvm: The VM pointer
  272. * @start: The intermediate physical base address of the range to unmap
  273. * @size: The size of the area to unmap
  274. *
  275. * Clear a range of stage-2 mappings, lowering the various ref-counts. Must
  276. * be called while holding mmu_lock (unless for freeing the stage2 pgd before
  277. * destroying the VM), otherwise another faulting VCPU may come in and mess
  278. * with things behind our backs.
  279. */
  280. static void __unmap_stage2_range(struct kvm *kvm, phys_addr_t start, u64 size,
  281. bool may_block)
  282. {
  283. pgd_t *pgd;
  284. phys_addr_t addr = start, end = start + size;
  285. phys_addr_t next;
  286. assert_spin_locked(&kvm->mmu_lock);
  287. WARN_ON(size & ~PAGE_MASK);
  288. pgd = kvm->arch.pgd + stage2_pgd_index(addr);
  289. do {
  290. /*
  291. * Make sure the page table is still active, as another thread
  292. * could have possibly freed the page table, while we released
  293. * the lock.
  294. */
  295. if (!READ_ONCE(kvm->arch.pgd))
  296. break;
  297. next = stage2_pgd_addr_end(addr, end);
  298. if (!stage2_pgd_none(*pgd))
  299. unmap_stage2_puds(kvm, pgd, addr, next);
  300. /*
  301. * If the range is too large, release the kvm->mmu_lock
  302. * to prevent starvation and lockup detector warnings.
  303. */
  304. if (may_block && next != end)
  305. cond_resched_lock(&kvm->mmu_lock);
  306. } while (pgd++, addr = next, addr != end);
  307. }
  308. static void unmap_stage2_range(struct kvm *kvm, phys_addr_t start, u64 size)
  309. {
  310. __unmap_stage2_range(kvm, start, size, true);
  311. }
  312. static void stage2_flush_ptes(struct kvm *kvm, pmd_t *pmd,
  313. phys_addr_t addr, phys_addr_t end)
  314. {
  315. pte_t *pte;
  316. pte = pte_offset_kernel(pmd, addr);
  317. do {
  318. if (!pte_none(*pte) && !kvm_is_device_pfn(pte_pfn(*pte)))
  319. kvm_flush_dcache_pte(*pte);
  320. } while (pte++, addr += PAGE_SIZE, addr != end);
  321. }
  322. static void stage2_flush_pmds(struct kvm *kvm, pud_t *pud,
  323. phys_addr_t addr, phys_addr_t end)
  324. {
  325. pmd_t *pmd;
  326. phys_addr_t next;
  327. pmd = stage2_pmd_offset(pud, addr);
  328. do {
  329. next = stage2_pmd_addr_end(addr, end);
  330. if (!pmd_none(*pmd)) {
  331. if (pmd_thp_or_huge(*pmd))
  332. kvm_flush_dcache_pmd(*pmd);
  333. else
  334. stage2_flush_ptes(kvm, pmd, addr, next);
  335. }
  336. } while (pmd++, addr = next, addr != end);
  337. }
  338. static void stage2_flush_puds(struct kvm *kvm, pgd_t *pgd,
  339. phys_addr_t addr, phys_addr_t end)
  340. {
  341. pud_t *pud;
  342. phys_addr_t next;
  343. pud = stage2_pud_offset(pgd, addr);
  344. do {
  345. next = stage2_pud_addr_end(addr, end);
  346. if (!stage2_pud_none(*pud)) {
  347. if (stage2_pud_huge(*pud))
  348. kvm_flush_dcache_pud(*pud);
  349. else
  350. stage2_flush_pmds(kvm, pud, addr, next);
  351. }
  352. } while (pud++, addr = next, addr != end);
  353. }
  354. static void stage2_flush_memslot(struct kvm *kvm,
  355. struct kvm_memory_slot *memslot)
  356. {
  357. phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
  358. phys_addr_t end = addr + PAGE_SIZE * memslot->npages;
  359. phys_addr_t next;
  360. pgd_t *pgd;
  361. pgd = kvm->arch.pgd + stage2_pgd_index(addr);
  362. do {
  363. next = stage2_pgd_addr_end(addr, end);
  364. if (!stage2_pgd_none(*pgd))
  365. stage2_flush_puds(kvm, pgd, addr, next);
  366. } while (pgd++, addr = next, addr != end);
  367. }
  368. /**
  369. * stage2_flush_vm - Invalidate cache for pages mapped in stage 2
  370. * @kvm: The struct kvm pointer
  371. *
  372. * Go through the stage 2 page tables and invalidate any cache lines
  373. * backing memory already mapped to the VM.
  374. */
  375. static void stage2_flush_vm(struct kvm *kvm)
  376. {
  377. struct kvm_memslots *slots;
  378. struct kvm_memory_slot *memslot;
  379. int idx;
  380. idx = srcu_read_lock(&kvm->srcu);
  381. spin_lock(&kvm->mmu_lock);
  382. slots = kvm_memslots(kvm);
  383. kvm_for_each_memslot(memslot, slots)
  384. stage2_flush_memslot(kvm, memslot);
  385. spin_unlock(&kvm->mmu_lock);
  386. srcu_read_unlock(&kvm->srcu, idx);
  387. }
  388. static void clear_hyp_pgd_entry(pgd_t *pgd)
  389. {
  390. pud_t *pud_table __maybe_unused = pud_offset(pgd, 0UL);
  391. pgd_clear(pgd);
  392. pud_free(NULL, pud_table);
  393. put_page(virt_to_page(pgd));
  394. }
  395. static void clear_hyp_pud_entry(pud_t *pud)
  396. {
  397. pmd_t *pmd_table __maybe_unused = pmd_offset(pud, 0);
  398. VM_BUG_ON(pud_huge(*pud));
  399. pud_clear(pud);
  400. pmd_free(NULL, pmd_table);
  401. put_page(virt_to_page(pud));
  402. }
  403. static void clear_hyp_pmd_entry(pmd_t *pmd)
  404. {
  405. pte_t *pte_table = pte_offset_kernel(pmd, 0);
  406. VM_BUG_ON(pmd_thp_or_huge(*pmd));
  407. pmd_clear(pmd);
  408. pte_free_kernel(NULL, pte_table);
  409. put_page(virt_to_page(pmd));
  410. }
  411. static void unmap_hyp_ptes(pmd_t *pmd, phys_addr_t addr, phys_addr_t end)
  412. {
  413. pte_t *pte, *start_pte;
  414. start_pte = pte = pte_offset_kernel(pmd, addr);
  415. do {
  416. if (!pte_none(*pte)) {
  417. kvm_set_pte(pte, __pte(0));
  418. put_page(virt_to_page(pte));
  419. }
  420. } while (pte++, addr += PAGE_SIZE, addr != end);
  421. if (hyp_pte_table_empty(start_pte))
  422. clear_hyp_pmd_entry(pmd);
  423. }
  424. static void unmap_hyp_pmds(pud_t *pud, phys_addr_t addr, phys_addr_t end)
  425. {
  426. phys_addr_t next;
  427. pmd_t *pmd, *start_pmd;
  428. start_pmd = pmd = pmd_offset(pud, addr);
  429. do {
  430. next = pmd_addr_end(addr, end);
  431. /* Hyp doesn't use huge pmds */
  432. if (!pmd_none(*pmd))
  433. unmap_hyp_ptes(pmd, addr, next);
  434. } while (pmd++, addr = next, addr != end);
  435. if (hyp_pmd_table_empty(start_pmd))
  436. clear_hyp_pud_entry(pud);
  437. }
  438. static void unmap_hyp_puds(pgd_t *pgd, phys_addr_t addr, phys_addr_t end)
  439. {
  440. phys_addr_t next;
  441. pud_t *pud, *start_pud;
  442. start_pud = pud = pud_offset(pgd, addr);
  443. do {
  444. next = pud_addr_end(addr, end);
  445. /* Hyp doesn't use huge puds */
  446. if (!pud_none(*pud))
  447. unmap_hyp_pmds(pud, addr, next);
  448. } while (pud++, addr = next, addr != end);
  449. if (hyp_pud_table_empty(start_pud))
  450. clear_hyp_pgd_entry(pgd);
  451. }
  452. static unsigned int kvm_pgd_index(unsigned long addr, unsigned int ptrs_per_pgd)
  453. {
  454. return (addr >> PGDIR_SHIFT) & (ptrs_per_pgd - 1);
  455. }
  456. static void __unmap_hyp_range(pgd_t *pgdp, unsigned long ptrs_per_pgd,
  457. phys_addr_t start, u64 size)
  458. {
  459. pgd_t *pgd;
  460. phys_addr_t addr = start, end = start + size;
  461. phys_addr_t next;
  462. /*
  463. * We don't unmap anything from HYP, except at the hyp tear down.
  464. * Hence, we don't have to invalidate the TLBs here.
  465. */
  466. pgd = pgdp + kvm_pgd_index(addr, ptrs_per_pgd);
  467. do {
  468. next = pgd_addr_end(addr, end);
  469. if (!pgd_none(*pgd))
  470. unmap_hyp_puds(pgd, addr, next);
  471. } while (pgd++, addr = next, addr != end);
  472. }
  473. static void unmap_hyp_range(pgd_t *pgdp, phys_addr_t start, u64 size)
  474. {
  475. __unmap_hyp_range(pgdp, PTRS_PER_PGD, start, size);
  476. }
  477. static void unmap_hyp_idmap_range(pgd_t *pgdp, phys_addr_t start, u64 size)
  478. {
  479. __unmap_hyp_range(pgdp, __kvm_idmap_ptrs_per_pgd(), start, size);
  480. }
  481. /**
  482. * free_hyp_pgds - free Hyp-mode page tables
  483. *
  484. * Assumes hyp_pgd is a page table used strictly in Hyp-mode and
  485. * therefore contains either mappings in the kernel memory area (above
  486. * PAGE_OFFSET), or device mappings in the idmap range.
  487. *
  488. * boot_hyp_pgd should only map the idmap range, and is only used in
  489. * the extended idmap case.
  490. */
  491. void free_hyp_pgds(void)
  492. {
  493. pgd_t *id_pgd;
  494. mutex_lock(&kvm_hyp_pgd_mutex);
  495. id_pgd = boot_hyp_pgd ? boot_hyp_pgd : hyp_pgd;
  496. if (id_pgd) {
  497. /* In case we never called hyp_mmu_init() */
  498. if (!io_map_base)
  499. io_map_base = hyp_idmap_start;
  500. unmap_hyp_idmap_range(id_pgd, io_map_base,
  501. hyp_idmap_start + PAGE_SIZE - io_map_base);
  502. }
  503. if (boot_hyp_pgd) {
  504. free_pages((unsigned long)boot_hyp_pgd, hyp_pgd_order);
  505. boot_hyp_pgd = NULL;
  506. }
  507. if (hyp_pgd) {
  508. unmap_hyp_range(hyp_pgd, kern_hyp_va(PAGE_OFFSET),
  509. (uintptr_t)high_memory - PAGE_OFFSET);
  510. free_pages((unsigned long)hyp_pgd, hyp_pgd_order);
  511. hyp_pgd = NULL;
  512. }
  513. if (merged_hyp_pgd) {
  514. clear_page(merged_hyp_pgd);
  515. free_page((unsigned long)merged_hyp_pgd);
  516. merged_hyp_pgd = NULL;
  517. }
  518. mutex_unlock(&kvm_hyp_pgd_mutex);
  519. }
  520. static void create_hyp_pte_mappings(pmd_t *pmd, unsigned long start,
  521. unsigned long end, unsigned long pfn,
  522. pgprot_t prot)
  523. {
  524. pte_t *pte;
  525. unsigned long addr;
  526. addr = start;
  527. do {
  528. pte = pte_offset_kernel(pmd, addr);
  529. kvm_set_pte(pte, pfn_pte(pfn, prot));
  530. get_page(virt_to_page(pte));
  531. pfn++;
  532. } while (addr += PAGE_SIZE, addr != end);
  533. }
  534. static int create_hyp_pmd_mappings(pud_t *pud, unsigned long start,
  535. unsigned long end, unsigned long pfn,
  536. pgprot_t prot)
  537. {
  538. pmd_t *pmd;
  539. pte_t *pte;
  540. unsigned long addr, next;
  541. addr = start;
  542. do {
  543. pmd = pmd_offset(pud, addr);
  544. BUG_ON(pmd_sect(*pmd));
  545. if (pmd_none(*pmd)) {
  546. pte = pte_alloc_one_kernel(NULL, addr);
  547. if (!pte) {
  548. kvm_err("Cannot allocate Hyp pte\n");
  549. return -ENOMEM;
  550. }
  551. kvm_pmd_populate(pmd, pte);
  552. get_page(virt_to_page(pmd));
  553. }
  554. next = pmd_addr_end(addr, end);
  555. create_hyp_pte_mappings(pmd, addr, next, pfn, prot);
  556. pfn += (next - addr) >> PAGE_SHIFT;
  557. } while (addr = next, addr != end);
  558. return 0;
  559. }
  560. static int create_hyp_pud_mappings(pgd_t *pgd, unsigned long start,
  561. unsigned long end, unsigned long pfn,
  562. pgprot_t prot)
  563. {
  564. pud_t *pud;
  565. pmd_t *pmd;
  566. unsigned long addr, next;
  567. int ret;
  568. addr = start;
  569. do {
  570. pud = pud_offset(pgd, addr);
  571. if (pud_none_or_clear_bad(pud)) {
  572. pmd = pmd_alloc_one(NULL, addr);
  573. if (!pmd) {
  574. kvm_err("Cannot allocate Hyp pmd\n");
  575. return -ENOMEM;
  576. }
  577. kvm_pud_populate(pud, pmd);
  578. get_page(virt_to_page(pud));
  579. }
  580. next = pud_addr_end(addr, end);
  581. ret = create_hyp_pmd_mappings(pud, addr, next, pfn, prot);
  582. if (ret)
  583. return ret;
  584. pfn += (next - addr) >> PAGE_SHIFT;
  585. } while (addr = next, addr != end);
  586. return 0;
  587. }
  588. static int __create_hyp_mappings(pgd_t *pgdp, unsigned long ptrs_per_pgd,
  589. unsigned long start, unsigned long end,
  590. unsigned long pfn, pgprot_t prot)
  591. {
  592. pgd_t *pgd;
  593. pud_t *pud;
  594. unsigned long addr, next;
  595. int err = 0;
  596. mutex_lock(&kvm_hyp_pgd_mutex);
  597. addr = start & PAGE_MASK;
  598. end = PAGE_ALIGN(end);
  599. do {
  600. pgd = pgdp + kvm_pgd_index(addr, ptrs_per_pgd);
  601. if (pgd_none(*pgd)) {
  602. pud = pud_alloc_one(NULL, addr);
  603. if (!pud) {
  604. kvm_err("Cannot allocate Hyp pud\n");
  605. err = -ENOMEM;
  606. goto out;
  607. }
  608. kvm_pgd_populate(pgd, pud);
  609. get_page(virt_to_page(pgd));
  610. }
  611. next = pgd_addr_end(addr, end);
  612. err = create_hyp_pud_mappings(pgd, addr, next, pfn, prot);
  613. if (err)
  614. goto out;
  615. pfn += (next - addr) >> PAGE_SHIFT;
  616. } while (addr = next, addr != end);
  617. out:
  618. mutex_unlock(&kvm_hyp_pgd_mutex);
  619. return err;
  620. }
  621. static phys_addr_t kvm_kaddr_to_phys(void *kaddr)
  622. {
  623. if (!is_vmalloc_addr(kaddr)) {
  624. BUG_ON(!virt_addr_valid(kaddr));
  625. return __pa(kaddr);
  626. } else {
  627. return page_to_phys(vmalloc_to_page(kaddr)) +
  628. offset_in_page(kaddr);
  629. }
  630. }
  631. /**
  632. * create_hyp_mappings - duplicate a kernel virtual address range in Hyp mode
  633. * @from: The virtual kernel start address of the range
  634. * @to: The virtual kernel end address of the range (exclusive)
  635. * @prot: The protection to be applied to this range
  636. *
  637. * The same virtual address as the kernel virtual address is also used
  638. * in Hyp-mode mapping (modulo HYP_PAGE_OFFSET) to the same underlying
  639. * physical pages.
  640. */
  641. int create_hyp_mappings(void *from, void *to, pgprot_t prot)
  642. {
  643. phys_addr_t phys_addr;
  644. unsigned long virt_addr;
  645. unsigned long start = kern_hyp_va((unsigned long)from);
  646. unsigned long end = kern_hyp_va((unsigned long)to);
  647. if (is_kernel_in_hyp_mode())
  648. return 0;
  649. start = start & PAGE_MASK;
  650. end = PAGE_ALIGN(end);
  651. for (virt_addr = start; virt_addr < end; virt_addr += PAGE_SIZE) {
  652. int err;
  653. phys_addr = kvm_kaddr_to_phys(from + virt_addr - start);
  654. err = __create_hyp_mappings(hyp_pgd, PTRS_PER_PGD,
  655. virt_addr, virt_addr + PAGE_SIZE,
  656. __phys_to_pfn(phys_addr),
  657. prot);
  658. if (err)
  659. return err;
  660. }
  661. return 0;
  662. }
  663. static int __create_hyp_private_mapping(phys_addr_t phys_addr, size_t size,
  664. unsigned long *haddr, pgprot_t prot)
  665. {
  666. pgd_t *pgd = hyp_pgd;
  667. unsigned long base;
  668. int ret = 0;
  669. mutex_lock(&kvm_hyp_pgd_mutex);
  670. /*
  671. * This assumes that we we have enough space below the idmap
  672. * page to allocate our VAs. If not, the check below will
  673. * kick. A potential alternative would be to detect that
  674. * overflow and switch to an allocation above the idmap.
  675. *
  676. * The allocated size is always a multiple of PAGE_SIZE.
  677. */
  678. size = PAGE_ALIGN(size + offset_in_page(phys_addr));
  679. base = io_map_base - size;
  680. /*
  681. * Verify that BIT(VA_BITS - 1) hasn't been flipped by
  682. * allocating the new area, as it would indicate we've
  683. * overflowed the idmap/IO address range.
  684. */
  685. if ((base ^ io_map_base) & BIT(VA_BITS - 1))
  686. ret = -ENOMEM;
  687. else
  688. io_map_base = base;
  689. mutex_unlock(&kvm_hyp_pgd_mutex);
  690. if (ret)
  691. goto out;
  692. if (__kvm_cpu_uses_extended_idmap())
  693. pgd = boot_hyp_pgd;
  694. ret = __create_hyp_mappings(pgd, __kvm_idmap_ptrs_per_pgd(),
  695. base, base + size,
  696. __phys_to_pfn(phys_addr), prot);
  697. if (ret)
  698. goto out;
  699. *haddr = base + offset_in_page(phys_addr);
  700. out:
  701. return ret;
  702. }
  703. /**
  704. * create_hyp_io_mappings - Map IO into both kernel and HYP
  705. * @phys_addr: The physical start address which gets mapped
  706. * @size: Size of the region being mapped
  707. * @kaddr: Kernel VA for this mapping
  708. * @haddr: HYP VA for this mapping
  709. */
  710. int create_hyp_io_mappings(phys_addr_t phys_addr, size_t size,
  711. void __iomem **kaddr,
  712. void __iomem **haddr)
  713. {
  714. unsigned long addr;
  715. int ret;
  716. *kaddr = ioremap(phys_addr, size);
  717. if (!*kaddr)
  718. return -ENOMEM;
  719. if (is_kernel_in_hyp_mode()) {
  720. *haddr = *kaddr;
  721. return 0;
  722. }
  723. ret = __create_hyp_private_mapping(phys_addr, size,
  724. &addr, PAGE_HYP_DEVICE);
  725. if (ret) {
  726. iounmap(*kaddr);
  727. *kaddr = NULL;
  728. *haddr = NULL;
  729. return ret;
  730. }
  731. *haddr = (void __iomem *)addr;
  732. return 0;
  733. }
  734. /**
  735. * create_hyp_exec_mappings - Map an executable range into HYP
  736. * @phys_addr: The physical start address which gets mapped
  737. * @size: Size of the region being mapped
  738. * @haddr: HYP VA for this mapping
  739. */
  740. int create_hyp_exec_mappings(phys_addr_t phys_addr, size_t size,
  741. void **haddr)
  742. {
  743. unsigned long addr;
  744. int ret;
  745. BUG_ON(is_kernel_in_hyp_mode());
  746. ret = __create_hyp_private_mapping(phys_addr, size,
  747. &addr, PAGE_HYP_EXEC);
  748. if (ret) {
  749. *haddr = NULL;
  750. return ret;
  751. }
  752. *haddr = (void *)addr;
  753. return 0;
  754. }
  755. /**
  756. * kvm_alloc_stage2_pgd - allocate level-1 table for stage-2 translation.
  757. * @kvm: The KVM struct pointer for the VM.
  758. *
  759. * Allocates only the stage-2 HW PGD level table(s) (can support either full
  760. * 40-bit input addresses or limited to 32-bit input addresses). Clears the
  761. * allocated pages.
  762. *
  763. * Note we don't need locking here as this is only called when the VM is
  764. * created, which can only be done once.
  765. */
  766. int kvm_alloc_stage2_pgd(struct kvm *kvm)
  767. {
  768. pgd_t *pgd;
  769. if (kvm->arch.pgd != NULL) {
  770. kvm_err("kvm_arch already initialized?\n");
  771. return -EINVAL;
  772. }
  773. /* Allocate the HW PGD, making sure that each page gets its own refcount */
  774. pgd = alloc_pages_exact(S2_PGD_SIZE, GFP_KERNEL | __GFP_ZERO);
  775. if (!pgd)
  776. return -ENOMEM;
  777. kvm->arch.pgd = pgd;
  778. return 0;
  779. }
  780. static void stage2_unmap_memslot(struct kvm *kvm,
  781. struct kvm_memory_slot *memslot)
  782. {
  783. hva_t hva = memslot->userspace_addr;
  784. phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
  785. phys_addr_t size = PAGE_SIZE * memslot->npages;
  786. hva_t reg_end = hva + size;
  787. /*
  788. * A memory region could potentially cover multiple VMAs, and any holes
  789. * between them, so iterate over all of them to find out if we should
  790. * unmap any of them.
  791. *
  792. * +--------------------------------------------+
  793. * +---------------+----------------+ +----------------+
  794. * | : VMA 1 | VMA 2 | | VMA 3 : |
  795. * +---------------+----------------+ +----------------+
  796. * | memory region |
  797. * +--------------------------------------------+
  798. */
  799. do {
  800. struct vm_area_struct *vma = find_vma(current->mm, hva);
  801. hva_t vm_start, vm_end;
  802. if (!vma || vma->vm_start >= reg_end)
  803. break;
  804. /*
  805. * Take the intersection of this VMA with the memory region
  806. */
  807. vm_start = max(hva, vma->vm_start);
  808. vm_end = min(reg_end, vma->vm_end);
  809. if (!(vma->vm_flags & VM_PFNMAP)) {
  810. gpa_t gpa = addr + (vm_start - memslot->userspace_addr);
  811. unmap_stage2_range(kvm, gpa, vm_end - vm_start);
  812. }
  813. hva = vm_end;
  814. } while (hva < reg_end);
  815. }
  816. /**
  817. * stage2_unmap_vm - Unmap Stage-2 RAM mappings
  818. * @kvm: The struct kvm pointer
  819. *
  820. * Go through the memregions and unmap any reguler RAM
  821. * backing memory already mapped to the VM.
  822. */
  823. void stage2_unmap_vm(struct kvm *kvm)
  824. {
  825. struct kvm_memslots *slots;
  826. struct kvm_memory_slot *memslot;
  827. int idx;
  828. idx = srcu_read_lock(&kvm->srcu);
  829. down_read(&current->mm->mmap_sem);
  830. spin_lock(&kvm->mmu_lock);
  831. slots = kvm_memslots(kvm);
  832. kvm_for_each_memslot(memslot, slots)
  833. stage2_unmap_memslot(kvm, memslot);
  834. spin_unlock(&kvm->mmu_lock);
  835. up_read(&current->mm->mmap_sem);
  836. srcu_read_unlock(&kvm->srcu, idx);
  837. }
  838. /**
  839. * kvm_free_stage2_pgd - free all stage-2 tables
  840. * @kvm: The KVM struct pointer for the VM.
  841. *
  842. * Walks the level-1 page table pointed to by kvm->arch.pgd and frees all
  843. * underlying level-2 and level-3 tables before freeing the actual level-1 table
  844. * and setting the struct pointer to NULL.
  845. */
  846. void kvm_free_stage2_pgd(struct kvm *kvm)
  847. {
  848. void *pgd = NULL;
  849. spin_lock(&kvm->mmu_lock);
  850. if (kvm->arch.pgd) {
  851. unmap_stage2_range(kvm, 0, KVM_PHYS_SIZE);
  852. pgd = READ_ONCE(kvm->arch.pgd);
  853. kvm->arch.pgd = NULL;
  854. }
  855. spin_unlock(&kvm->mmu_lock);
  856. /* Free the HW pgd, one page at a time */
  857. if (pgd)
  858. free_pages_exact(pgd, S2_PGD_SIZE);
  859. }
  860. static pud_t *stage2_get_pud(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
  861. phys_addr_t addr)
  862. {
  863. pgd_t *pgd;
  864. pud_t *pud;
  865. pgd = kvm->arch.pgd + stage2_pgd_index(addr);
  866. if (WARN_ON(stage2_pgd_none(*pgd))) {
  867. if (!cache)
  868. return NULL;
  869. pud = mmu_memory_cache_alloc(cache);
  870. stage2_pgd_populate(pgd, pud);
  871. get_page(virt_to_page(pgd));
  872. }
  873. return stage2_pud_offset(pgd, addr);
  874. }
  875. static pmd_t *stage2_get_pmd(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
  876. phys_addr_t addr)
  877. {
  878. pud_t *pud;
  879. pmd_t *pmd;
  880. pud = stage2_get_pud(kvm, cache, addr);
  881. if (!pud)
  882. return NULL;
  883. if (stage2_pud_none(*pud)) {
  884. if (!cache)
  885. return NULL;
  886. pmd = mmu_memory_cache_alloc(cache);
  887. stage2_pud_populate(pud, pmd);
  888. get_page(virt_to_page(pud));
  889. }
  890. return stage2_pmd_offset(pud, addr);
  891. }
  892. static int stage2_set_pmd_huge(struct kvm *kvm, struct kvm_mmu_memory_cache
  893. *cache, phys_addr_t addr, const pmd_t *new_pmd)
  894. {
  895. pmd_t *pmd, old_pmd;
  896. pmd = stage2_get_pmd(kvm, cache, addr);
  897. VM_BUG_ON(!pmd);
  898. old_pmd = *pmd;
  899. if (pmd_present(old_pmd)) {
  900. /*
  901. * Multiple vcpus faulting on the same PMD entry, can
  902. * lead to them sequentially updating the PMD with the
  903. * same value. Following the break-before-make
  904. * (pmd_clear() followed by tlb_flush()) process can
  905. * hinder forward progress due to refaults generated
  906. * on missing translations.
  907. *
  908. * Skip updating the page table if the entry is
  909. * unchanged.
  910. */
  911. if (pmd_val(old_pmd) == pmd_val(*new_pmd))
  912. return 0;
  913. /*
  914. * Mapping in huge pages should only happen through a
  915. * fault. If a page is merged into a transparent huge
  916. * page, the individual subpages of that huge page
  917. * should be unmapped through MMU notifiers before we
  918. * get here.
  919. *
  920. * Merging of CompoundPages is not supported; they
  921. * should become splitting first, unmapped, merged,
  922. * and mapped back in on-demand.
  923. */
  924. VM_BUG_ON(pmd_pfn(old_pmd) != pmd_pfn(*new_pmd));
  925. pmd_clear(pmd);
  926. kvm_tlb_flush_vmid_ipa(kvm, addr);
  927. } else {
  928. get_page(virt_to_page(pmd));
  929. }
  930. kvm_set_pmd(pmd, *new_pmd);
  931. return 0;
  932. }
  933. static bool stage2_is_exec(struct kvm *kvm, phys_addr_t addr)
  934. {
  935. pmd_t *pmdp;
  936. pte_t *ptep;
  937. pmdp = stage2_get_pmd(kvm, NULL, addr);
  938. if (!pmdp || pmd_none(*pmdp) || !pmd_present(*pmdp))
  939. return false;
  940. if (pmd_thp_or_huge(*pmdp))
  941. return kvm_s2pmd_exec(pmdp);
  942. ptep = pte_offset_kernel(pmdp, addr);
  943. if (!ptep || pte_none(*ptep) || !pte_present(*ptep))
  944. return false;
  945. return kvm_s2pte_exec(ptep);
  946. }
  947. static int stage2_set_pte(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
  948. phys_addr_t addr, const pte_t *new_pte,
  949. unsigned long flags)
  950. {
  951. pmd_t *pmd;
  952. pte_t *pte, old_pte;
  953. bool iomap = flags & KVM_S2PTE_FLAG_IS_IOMAP;
  954. bool logging_active = flags & KVM_S2_FLAG_LOGGING_ACTIVE;
  955. VM_BUG_ON(logging_active && !cache);
  956. /* Create stage-2 page table mapping - Levels 0 and 1 */
  957. pmd = stage2_get_pmd(kvm, cache, addr);
  958. if (!pmd) {
  959. /*
  960. * Ignore calls from kvm_set_spte_hva for unallocated
  961. * address ranges.
  962. */
  963. return 0;
  964. }
  965. /*
  966. * While dirty page logging - dissolve huge PMD, then continue on to
  967. * allocate page.
  968. */
  969. if (logging_active)
  970. stage2_dissolve_pmd(kvm, addr, pmd);
  971. /* Create stage-2 page mappings - Level 2 */
  972. if (pmd_none(*pmd)) {
  973. if (!cache)
  974. return 0; /* ignore calls from kvm_set_spte_hva */
  975. pte = mmu_memory_cache_alloc(cache);
  976. kvm_pmd_populate(pmd, pte);
  977. get_page(virt_to_page(pmd));
  978. }
  979. pte = pte_offset_kernel(pmd, addr);
  980. if (iomap && pte_present(*pte))
  981. return -EFAULT;
  982. /* Create 2nd stage page table mapping - Level 3 */
  983. old_pte = *pte;
  984. if (pte_present(old_pte)) {
  985. /* Skip page table update if there is no change */
  986. if (pte_val(old_pte) == pte_val(*new_pte))
  987. return 0;
  988. kvm_set_pte(pte, __pte(0));
  989. kvm_tlb_flush_vmid_ipa(kvm, addr);
  990. } else {
  991. get_page(virt_to_page(pte));
  992. }
  993. kvm_set_pte(pte, *new_pte);
  994. return 0;
  995. }
  996. #ifndef __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
  997. static int stage2_ptep_test_and_clear_young(pte_t *pte)
  998. {
  999. if (pte_young(*pte)) {
  1000. *pte = pte_mkold(*pte);
  1001. return 1;
  1002. }
  1003. return 0;
  1004. }
  1005. #else
  1006. static int stage2_ptep_test_and_clear_young(pte_t *pte)
  1007. {
  1008. return __ptep_test_and_clear_young(pte);
  1009. }
  1010. #endif
  1011. static int stage2_pmdp_test_and_clear_young(pmd_t *pmd)
  1012. {
  1013. return stage2_ptep_test_and_clear_young((pte_t *)pmd);
  1014. }
  1015. /**
  1016. * kvm_phys_addr_ioremap - map a device range to guest IPA
  1017. *
  1018. * @kvm: The KVM pointer
  1019. * @guest_ipa: The IPA at which to insert the mapping
  1020. * @pa: The physical address of the device
  1021. * @size: The size of the mapping
  1022. */
  1023. int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
  1024. phys_addr_t pa, unsigned long size, bool writable)
  1025. {
  1026. phys_addr_t addr, end;
  1027. int ret = 0;
  1028. unsigned long pfn;
  1029. struct kvm_mmu_memory_cache cache = { 0, };
  1030. end = (guest_ipa + size + PAGE_SIZE - 1) & PAGE_MASK;
  1031. pfn = __phys_to_pfn(pa);
  1032. for (addr = guest_ipa; addr < end; addr += PAGE_SIZE) {
  1033. pte_t pte = pfn_pte(pfn, PAGE_S2_DEVICE);
  1034. if (writable)
  1035. pte = kvm_s2pte_mkwrite(pte);
  1036. ret = mmu_topup_memory_cache(&cache, KVM_MMU_CACHE_MIN_PAGES,
  1037. KVM_NR_MEM_OBJS);
  1038. if (ret)
  1039. goto out;
  1040. spin_lock(&kvm->mmu_lock);
  1041. ret = stage2_set_pte(kvm, &cache, addr, &pte,
  1042. KVM_S2PTE_FLAG_IS_IOMAP);
  1043. spin_unlock(&kvm->mmu_lock);
  1044. if (ret)
  1045. goto out;
  1046. pfn++;
  1047. }
  1048. out:
  1049. mmu_free_memory_cache(&cache);
  1050. return ret;
  1051. }
  1052. static bool transparent_hugepage_adjust(kvm_pfn_t *pfnp, phys_addr_t *ipap)
  1053. {
  1054. kvm_pfn_t pfn = *pfnp;
  1055. gfn_t gfn = *ipap >> PAGE_SHIFT;
  1056. struct page *page = pfn_to_page(pfn);
  1057. /*
  1058. * PageTransCompoungMap() returns true for THP and
  1059. * hugetlbfs. Make sure the adjustment is done only for THP
  1060. * pages.
  1061. */
  1062. if (!PageHuge(page) && PageTransCompoundMap(page)) {
  1063. unsigned long mask;
  1064. /*
  1065. * The address we faulted on is backed by a transparent huge
  1066. * page. However, because we map the compound huge page and
  1067. * not the individual tail page, we need to transfer the
  1068. * refcount to the head page. We have to be careful that the
  1069. * THP doesn't start to split while we are adjusting the
  1070. * refcounts.
  1071. *
  1072. * We are sure this doesn't happen, because mmu_notifier_retry
  1073. * was successful and we are holding the mmu_lock, so if this
  1074. * THP is trying to split, it will be blocked in the mmu
  1075. * notifier before touching any of the pages, specifically
  1076. * before being able to call __split_huge_page_refcount().
  1077. *
  1078. * We can therefore safely transfer the refcount from PG_tail
  1079. * to PG_head and switch the pfn from a tail page to the head
  1080. * page accordingly.
  1081. */
  1082. mask = PTRS_PER_PMD - 1;
  1083. VM_BUG_ON((gfn & mask) != (pfn & mask));
  1084. if (pfn & mask) {
  1085. *ipap &= PMD_MASK;
  1086. kvm_release_pfn_clean(pfn);
  1087. pfn &= ~mask;
  1088. kvm_get_pfn(pfn);
  1089. *pfnp = pfn;
  1090. }
  1091. return true;
  1092. }
  1093. return false;
  1094. }
  1095. static bool kvm_is_write_fault(struct kvm_vcpu *vcpu)
  1096. {
  1097. if (kvm_vcpu_abt_iss1tw(vcpu))
  1098. return true;
  1099. if (kvm_vcpu_trap_is_iabt(vcpu))
  1100. return false;
  1101. return kvm_vcpu_dabt_iswrite(vcpu);
  1102. }
  1103. /**
  1104. * stage2_wp_ptes - write protect PMD range
  1105. * @pmd: pointer to pmd entry
  1106. * @addr: range start address
  1107. * @end: range end address
  1108. */
  1109. static void stage2_wp_ptes(pmd_t *pmd, phys_addr_t addr, phys_addr_t end)
  1110. {
  1111. pte_t *pte;
  1112. pte = pte_offset_kernel(pmd, addr);
  1113. do {
  1114. if (!pte_none(*pte)) {
  1115. if (!kvm_s2pte_readonly(pte))
  1116. kvm_set_s2pte_readonly(pte);
  1117. }
  1118. } while (pte++, addr += PAGE_SIZE, addr != end);
  1119. }
  1120. /**
  1121. * stage2_wp_pmds - write protect PUD range
  1122. * @pud: pointer to pud entry
  1123. * @addr: range start address
  1124. * @end: range end address
  1125. */
  1126. static void stage2_wp_pmds(pud_t *pud, phys_addr_t addr, phys_addr_t end)
  1127. {
  1128. pmd_t *pmd;
  1129. phys_addr_t next;
  1130. pmd = stage2_pmd_offset(pud, addr);
  1131. do {
  1132. next = stage2_pmd_addr_end(addr, end);
  1133. if (!pmd_none(*pmd)) {
  1134. if (pmd_thp_or_huge(*pmd)) {
  1135. if (!kvm_s2pmd_readonly(pmd))
  1136. kvm_set_s2pmd_readonly(pmd);
  1137. } else {
  1138. stage2_wp_ptes(pmd, addr, next);
  1139. }
  1140. }
  1141. } while (pmd++, addr = next, addr != end);
  1142. }
  1143. /**
  1144. * stage2_wp_puds - write protect PGD range
  1145. * @pgd: pointer to pgd entry
  1146. * @addr: range start address
  1147. * @end: range end address
  1148. *
  1149. * Process PUD entries, for a huge PUD we cause a panic.
  1150. */
  1151. static void stage2_wp_puds(pgd_t *pgd, phys_addr_t addr, phys_addr_t end)
  1152. {
  1153. pud_t *pud;
  1154. phys_addr_t next;
  1155. pud = stage2_pud_offset(pgd, addr);
  1156. do {
  1157. next = stage2_pud_addr_end(addr, end);
  1158. if (!stage2_pud_none(*pud)) {
  1159. /* TODO:PUD not supported, revisit later if supported */
  1160. BUG_ON(stage2_pud_huge(*pud));
  1161. stage2_wp_pmds(pud, addr, next);
  1162. }
  1163. } while (pud++, addr = next, addr != end);
  1164. }
  1165. /**
  1166. * stage2_wp_range() - write protect stage2 memory region range
  1167. * @kvm: The KVM pointer
  1168. * @addr: Start address of range
  1169. * @end: End address of range
  1170. */
  1171. static void stage2_wp_range(struct kvm *kvm, phys_addr_t addr, phys_addr_t end)
  1172. {
  1173. pgd_t *pgd;
  1174. phys_addr_t next;
  1175. pgd = kvm->arch.pgd + stage2_pgd_index(addr);
  1176. do {
  1177. /*
  1178. * Release kvm_mmu_lock periodically if the memory region is
  1179. * large. Otherwise, we may see kernel panics with
  1180. * CONFIG_DETECT_HUNG_TASK, CONFIG_LOCKUP_DETECTOR,
  1181. * CONFIG_LOCKDEP. Additionally, holding the lock too long
  1182. * will also starve other vCPUs. We have to also make sure
  1183. * that the page tables are not freed while we released
  1184. * the lock.
  1185. */
  1186. cond_resched_lock(&kvm->mmu_lock);
  1187. if (!READ_ONCE(kvm->arch.pgd))
  1188. break;
  1189. next = stage2_pgd_addr_end(addr, end);
  1190. if (stage2_pgd_present(*pgd))
  1191. stage2_wp_puds(pgd, addr, next);
  1192. } while (pgd++, addr = next, addr != end);
  1193. }
  1194. /**
  1195. * kvm_mmu_wp_memory_region() - write protect stage 2 entries for memory slot
  1196. * @kvm: The KVM pointer
  1197. * @slot: The memory slot to write protect
  1198. *
  1199. * Called to start logging dirty pages after memory region
  1200. * KVM_MEM_LOG_DIRTY_PAGES operation is called. After this function returns
  1201. * all present PMD and PTEs are write protected in the memory region.
  1202. * Afterwards read of dirty page log can be called.
  1203. *
  1204. * Acquires kvm_mmu_lock. Called with kvm->slots_lock mutex acquired,
  1205. * serializing operations for VM memory regions.
  1206. */
  1207. void kvm_mmu_wp_memory_region(struct kvm *kvm, int slot)
  1208. {
  1209. struct kvm_memslots *slots = kvm_memslots(kvm);
  1210. struct kvm_memory_slot *memslot = id_to_memslot(slots, slot);
  1211. phys_addr_t start = memslot->base_gfn << PAGE_SHIFT;
  1212. phys_addr_t end = (memslot->base_gfn + memslot->npages) << PAGE_SHIFT;
  1213. spin_lock(&kvm->mmu_lock);
  1214. stage2_wp_range(kvm, start, end);
  1215. spin_unlock(&kvm->mmu_lock);
  1216. kvm_flush_remote_tlbs(kvm);
  1217. }
  1218. /**
  1219. * kvm_mmu_write_protect_pt_masked() - write protect dirty pages
  1220. * @kvm: The KVM pointer
  1221. * @slot: The memory slot associated with mask
  1222. * @gfn_offset: The gfn offset in memory slot
  1223. * @mask: The mask of dirty pages at offset 'gfn_offset' in this memory
  1224. * slot to be write protected
  1225. *
  1226. * Walks bits set in mask write protects the associated pte's. Caller must
  1227. * acquire kvm_mmu_lock.
  1228. */
  1229. static void kvm_mmu_write_protect_pt_masked(struct kvm *kvm,
  1230. struct kvm_memory_slot *slot,
  1231. gfn_t gfn_offset, unsigned long mask)
  1232. {
  1233. phys_addr_t base_gfn = slot->base_gfn + gfn_offset;
  1234. phys_addr_t start = (base_gfn + __ffs(mask)) << PAGE_SHIFT;
  1235. phys_addr_t end = (base_gfn + __fls(mask) + 1) << PAGE_SHIFT;
  1236. stage2_wp_range(kvm, start, end);
  1237. }
  1238. /*
  1239. * kvm_arch_mmu_enable_log_dirty_pt_masked - enable dirty logging for selected
  1240. * dirty pages.
  1241. *
  1242. * It calls kvm_mmu_write_protect_pt_masked to write protect selected pages to
  1243. * enable dirty logging for them.
  1244. */
  1245. void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm,
  1246. struct kvm_memory_slot *slot,
  1247. gfn_t gfn_offset, unsigned long mask)
  1248. {
  1249. kvm_mmu_write_protect_pt_masked(kvm, slot, gfn_offset, mask);
  1250. }
  1251. static void clean_dcache_guest_page(kvm_pfn_t pfn, unsigned long size)
  1252. {
  1253. __clean_dcache_guest_page(pfn, size);
  1254. }
  1255. static void invalidate_icache_guest_page(kvm_pfn_t pfn, unsigned long size)
  1256. {
  1257. __invalidate_icache_guest_page(pfn, size);
  1258. }
  1259. static void kvm_send_hwpoison_signal(unsigned long address,
  1260. struct vm_area_struct *vma)
  1261. {
  1262. siginfo_t info;
  1263. clear_siginfo(&info);
  1264. info.si_signo = SIGBUS;
  1265. info.si_errno = 0;
  1266. info.si_code = BUS_MCEERR_AR;
  1267. info.si_addr = (void __user *)address;
  1268. if (is_vm_hugetlb_page(vma))
  1269. info.si_addr_lsb = huge_page_shift(hstate_vma(vma));
  1270. else
  1271. info.si_addr_lsb = PAGE_SHIFT;
  1272. send_sig_info(SIGBUS, &info, current);
  1273. }
  1274. static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
  1275. struct kvm_memory_slot *memslot, unsigned long hva,
  1276. unsigned long fault_status)
  1277. {
  1278. int ret;
  1279. bool write_fault, exec_fault, writable, hugetlb = false, force_pte = false;
  1280. unsigned long mmu_seq;
  1281. gfn_t gfn = fault_ipa >> PAGE_SHIFT;
  1282. struct kvm *kvm = vcpu->kvm;
  1283. struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
  1284. struct vm_area_struct *vma;
  1285. kvm_pfn_t pfn;
  1286. pgprot_t mem_type = PAGE_S2;
  1287. bool logging_active = memslot_is_logging(memslot);
  1288. unsigned long flags = 0;
  1289. write_fault = kvm_is_write_fault(vcpu);
  1290. exec_fault = kvm_vcpu_trap_is_exec_fault(vcpu);
  1291. VM_BUG_ON(write_fault && exec_fault);
  1292. if (fault_status == FSC_PERM && !write_fault && !exec_fault) {
  1293. kvm_err("Unexpected L2 read permission error\n");
  1294. return -EFAULT;
  1295. }
  1296. /* Let's check if we will get back a huge page backed by hugetlbfs */
  1297. down_read(&current->mm->mmap_sem);
  1298. vma = find_vma_intersection(current->mm, hva, hva + 1);
  1299. if (unlikely(!vma)) {
  1300. kvm_err("Failed to find VMA for hva 0x%lx\n", hva);
  1301. up_read(&current->mm->mmap_sem);
  1302. return -EFAULT;
  1303. }
  1304. if (vma_kernel_pagesize(vma) == PMD_SIZE && !logging_active) {
  1305. hugetlb = true;
  1306. gfn = (fault_ipa & PMD_MASK) >> PAGE_SHIFT;
  1307. } else {
  1308. /*
  1309. * Pages belonging to memslots that don't have the same
  1310. * alignment for userspace and IPA cannot be mapped using
  1311. * block descriptors even if the pages belong to a THP for
  1312. * the process, because the stage-2 block descriptor will
  1313. * cover more than a single THP and we loose atomicity for
  1314. * unmapping, updates, and splits of the THP or other pages
  1315. * in the stage-2 block range.
  1316. */
  1317. if ((memslot->userspace_addr & ~PMD_MASK) !=
  1318. ((memslot->base_gfn << PAGE_SHIFT) & ~PMD_MASK))
  1319. force_pte = true;
  1320. }
  1321. up_read(&current->mm->mmap_sem);
  1322. /* We need minimum second+third level pages */
  1323. ret = mmu_topup_memory_cache(memcache, KVM_MMU_CACHE_MIN_PAGES,
  1324. KVM_NR_MEM_OBJS);
  1325. if (ret)
  1326. return ret;
  1327. mmu_seq = vcpu->kvm->mmu_notifier_seq;
  1328. /*
  1329. * Ensure the read of mmu_notifier_seq happens before we call
  1330. * gfn_to_pfn_prot (which calls get_user_pages), so that we don't risk
  1331. * the page we just got a reference to gets unmapped before we have a
  1332. * chance to grab the mmu_lock, which ensure that if the page gets
  1333. * unmapped afterwards, the call to kvm_unmap_hva will take it away
  1334. * from us again properly. This smp_rmb() interacts with the smp_wmb()
  1335. * in kvm_mmu_notifier_invalidate_<page|range_end>.
  1336. */
  1337. smp_rmb();
  1338. pfn = gfn_to_pfn_prot(kvm, gfn, write_fault, &writable);
  1339. if (pfn == KVM_PFN_ERR_HWPOISON) {
  1340. kvm_send_hwpoison_signal(hva, vma);
  1341. return 0;
  1342. }
  1343. if (is_error_noslot_pfn(pfn))
  1344. return -EFAULT;
  1345. if (kvm_is_device_pfn(pfn)) {
  1346. mem_type = PAGE_S2_DEVICE;
  1347. flags |= KVM_S2PTE_FLAG_IS_IOMAP;
  1348. } else if (logging_active) {
  1349. /*
  1350. * Faults on pages in a memslot with logging enabled
  1351. * should not be mapped with huge pages (it introduces churn
  1352. * and performance degradation), so force a pte mapping.
  1353. */
  1354. force_pte = true;
  1355. flags |= KVM_S2_FLAG_LOGGING_ACTIVE;
  1356. /*
  1357. * Only actually map the page as writable if this was a write
  1358. * fault.
  1359. */
  1360. if (!write_fault)
  1361. writable = false;
  1362. }
  1363. spin_lock(&kvm->mmu_lock);
  1364. if (mmu_notifier_retry(kvm, mmu_seq))
  1365. goto out_unlock;
  1366. if (!hugetlb && !force_pte)
  1367. hugetlb = transparent_hugepage_adjust(&pfn, &fault_ipa);
  1368. if (hugetlb) {
  1369. pmd_t new_pmd = pfn_pmd(pfn, mem_type);
  1370. new_pmd = pmd_mkhuge(new_pmd);
  1371. if (writable) {
  1372. new_pmd = kvm_s2pmd_mkwrite(new_pmd);
  1373. kvm_set_pfn_dirty(pfn);
  1374. }
  1375. if (fault_status != FSC_PERM)
  1376. clean_dcache_guest_page(pfn, PMD_SIZE);
  1377. if (exec_fault) {
  1378. new_pmd = kvm_s2pmd_mkexec(new_pmd);
  1379. invalidate_icache_guest_page(pfn, PMD_SIZE);
  1380. } else if (fault_status == FSC_PERM) {
  1381. /* Preserve execute if XN was already cleared */
  1382. if (stage2_is_exec(kvm, fault_ipa))
  1383. new_pmd = kvm_s2pmd_mkexec(new_pmd);
  1384. }
  1385. ret = stage2_set_pmd_huge(kvm, memcache, fault_ipa, &new_pmd);
  1386. } else {
  1387. pte_t new_pte = pfn_pte(pfn, mem_type);
  1388. if (writable) {
  1389. new_pte = kvm_s2pte_mkwrite(new_pte);
  1390. kvm_set_pfn_dirty(pfn);
  1391. mark_page_dirty(kvm, gfn);
  1392. }
  1393. if (fault_status != FSC_PERM)
  1394. clean_dcache_guest_page(pfn, PAGE_SIZE);
  1395. if (exec_fault) {
  1396. new_pte = kvm_s2pte_mkexec(new_pte);
  1397. invalidate_icache_guest_page(pfn, PAGE_SIZE);
  1398. } else if (fault_status == FSC_PERM) {
  1399. /* Preserve execute if XN was already cleared */
  1400. if (stage2_is_exec(kvm, fault_ipa))
  1401. new_pte = kvm_s2pte_mkexec(new_pte);
  1402. }
  1403. ret = stage2_set_pte(kvm, memcache, fault_ipa, &new_pte, flags);
  1404. }
  1405. out_unlock:
  1406. spin_unlock(&kvm->mmu_lock);
  1407. kvm_set_pfn_accessed(pfn);
  1408. kvm_release_pfn_clean(pfn);
  1409. return ret;
  1410. }
  1411. /*
  1412. * Resolve the access fault by making the page young again.
  1413. * Note that because the faulting entry is guaranteed not to be
  1414. * cached in the TLB, we don't need to invalidate anything.
  1415. * Only the HW Access Flag updates are supported for Stage 2 (no DBM),
  1416. * so there is no need for atomic (pte|pmd)_mkyoung operations.
  1417. */
  1418. static void handle_access_fault(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa)
  1419. {
  1420. pmd_t *pmd;
  1421. pte_t *pte;
  1422. kvm_pfn_t pfn;
  1423. bool pfn_valid = false;
  1424. trace_kvm_access_fault(fault_ipa);
  1425. spin_lock(&vcpu->kvm->mmu_lock);
  1426. pmd = stage2_get_pmd(vcpu->kvm, NULL, fault_ipa);
  1427. if (!pmd || pmd_none(*pmd)) /* Nothing there */
  1428. goto out;
  1429. if (pmd_thp_or_huge(*pmd)) { /* THP, HugeTLB */
  1430. *pmd = pmd_mkyoung(*pmd);
  1431. pfn = pmd_pfn(*pmd);
  1432. pfn_valid = true;
  1433. goto out;
  1434. }
  1435. pte = pte_offset_kernel(pmd, fault_ipa);
  1436. if (pte_none(*pte)) /* Nothing there either */
  1437. goto out;
  1438. *pte = pte_mkyoung(*pte); /* Just a page... */
  1439. pfn = pte_pfn(*pte);
  1440. pfn_valid = true;
  1441. out:
  1442. spin_unlock(&vcpu->kvm->mmu_lock);
  1443. if (pfn_valid)
  1444. kvm_set_pfn_accessed(pfn);
  1445. }
  1446. /**
  1447. * kvm_handle_guest_abort - handles all 2nd stage aborts
  1448. * @vcpu: the VCPU pointer
  1449. * @run: the kvm_run structure
  1450. *
  1451. * Any abort that gets to the host is almost guaranteed to be caused by a
  1452. * missing second stage translation table entry, which can mean that either the
  1453. * guest simply needs more memory and we must allocate an appropriate page or it
  1454. * can mean that the guest tried to access I/O memory, which is emulated by user
  1455. * space. The distinction is based on the IPA causing the fault and whether this
  1456. * memory region has been registered as standard RAM by user space.
  1457. */
  1458. int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run)
  1459. {
  1460. unsigned long fault_status;
  1461. phys_addr_t fault_ipa;
  1462. struct kvm_memory_slot *memslot;
  1463. unsigned long hva;
  1464. bool is_iabt, write_fault, writable;
  1465. gfn_t gfn;
  1466. int ret, idx;
  1467. fault_status = kvm_vcpu_trap_get_fault_type(vcpu);
  1468. fault_ipa = kvm_vcpu_get_fault_ipa(vcpu);
  1469. is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
  1470. /* Synchronous External Abort? */
  1471. if (kvm_vcpu_dabt_isextabt(vcpu)) {
  1472. /*
  1473. * For RAS the host kernel may handle this abort.
  1474. * There is no need to pass the error into the guest.
  1475. */
  1476. if (!handle_guest_sea(fault_ipa, kvm_vcpu_get_hsr(vcpu)))
  1477. return 1;
  1478. if (unlikely(!is_iabt)) {
  1479. kvm_inject_vabt(vcpu);
  1480. return 1;
  1481. }
  1482. }
  1483. trace_kvm_guest_fault(*vcpu_pc(vcpu), kvm_vcpu_get_hsr(vcpu),
  1484. kvm_vcpu_get_hfar(vcpu), fault_ipa);
  1485. /* Check the stage-2 fault is trans. fault or write fault */
  1486. if (fault_status != FSC_FAULT && fault_status != FSC_PERM &&
  1487. fault_status != FSC_ACCESS) {
  1488. kvm_err("Unsupported FSC: EC=%#x xFSC=%#lx ESR_EL2=%#lx\n",
  1489. kvm_vcpu_trap_get_class(vcpu),
  1490. (unsigned long)kvm_vcpu_trap_get_fault(vcpu),
  1491. (unsigned long)kvm_vcpu_get_hsr(vcpu));
  1492. return -EFAULT;
  1493. }
  1494. idx = srcu_read_lock(&vcpu->kvm->srcu);
  1495. gfn = fault_ipa >> PAGE_SHIFT;
  1496. memslot = gfn_to_memslot(vcpu->kvm, gfn);
  1497. hva = gfn_to_hva_memslot_prot(memslot, gfn, &writable);
  1498. write_fault = kvm_is_write_fault(vcpu);
  1499. if (kvm_is_error_hva(hva) || (write_fault && !writable)) {
  1500. if (is_iabt) {
  1501. /* Prefetch Abort on I/O address */
  1502. kvm_inject_pabt(vcpu, kvm_vcpu_get_hfar(vcpu));
  1503. ret = 1;
  1504. goto out_unlock;
  1505. }
  1506. /*
  1507. * Check for a cache maintenance operation. Since we
  1508. * ended-up here, we know it is outside of any memory
  1509. * slot. But we can't find out if that is for a device,
  1510. * or if the guest is just being stupid. The only thing
  1511. * we know for sure is that this range cannot be cached.
  1512. *
  1513. * So let's assume that the guest is just being
  1514. * cautious, and skip the instruction.
  1515. */
  1516. if (kvm_vcpu_dabt_is_cm(vcpu)) {
  1517. kvm_skip_instr(vcpu, kvm_vcpu_trap_il_is32bit(vcpu));
  1518. ret = 1;
  1519. goto out_unlock;
  1520. }
  1521. /*
  1522. * The IPA is reported as [MAX:12], so we need to
  1523. * complement it with the bottom 12 bits from the
  1524. * faulting VA. This is always 12 bits, irrespective
  1525. * of the page size.
  1526. */
  1527. fault_ipa |= kvm_vcpu_get_hfar(vcpu) & ((1 << 12) - 1);
  1528. ret = io_mem_abort(vcpu, run, fault_ipa);
  1529. goto out_unlock;
  1530. }
  1531. /* Userspace should not be able to register out-of-bounds IPAs */
  1532. VM_BUG_ON(fault_ipa >= KVM_PHYS_SIZE);
  1533. if (fault_status == FSC_ACCESS) {
  1534. handle_access_fault(vcpu, fault_ipa);
  1535. ret = 1;
  1536. goto out_unlock;
  1537. }
  1538. ret = user_mem_abort(vcpu, fault_ipa, memslot, hva, fault_status);
  1539. if (ret == 0)
  1540. ret = 1;
  1541. out_unlock:
  1542. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  1543. return ret;
  1544. }
  1545. static int handle_hva_to_gpa(struct kvm *kvm,
  1546. unsigned long start,
  1547. unsigned long end,
  1548. int (*handler)(struct kvm *kvm,
  1549. gpa_t gpa, u64 size,
  1550. void *data),
  1551. void *data)
  1552. {
  1553. struct kvm_memslots *slots;
  1554. struct kvm_memory_slot *memslot;
  1555. int ret = 0;
  1556. slots = kvm_memslots(kvm);
  1557. /* we only care about the pages that the guest sees */
  1558. kvm_for_each_memslot(memslot, slots) {
  1559. unsigned long hva_start, hva_end;
  1560. gfn_t gpa;
  1561. hva_start = max(start, memslot->userspace_addr);
  1562. hva_end = min(end, memslot->userspace_addr +
  1563. (memslot->npages << PAGE_SHIFT));
  1564. if (hva_start >= hva_end)
  1565. continue;
  1566. gpa = hva_to_gfn_memslot(hva_start, memslot) << PAGE_SHIFT;
  1567. ret |= handler(kvm, gpa, (u64)(hva_end - hva_start), data);
  1568. }
  1569. return ret;
  1570. }
  1571. static int kvm_unmap_hva_handler(struct kvm *kvm, gpa_t gpa, u64 size, void *data)
  1572. {
  1573. bool may_block = *(bool *)data;
  1574. __unmap_stage2_range(kvm, gpa, size, may_block);
  1575. return 0;
  1576. }
  1577. int kvm_unmap_hva_range(struct kvm *kvm,
  1578. unsigned long start, unsigned long end, bool blockable)
  1579. {
  1580. if (!kvm->arch.pgd)
  1581. return 0;
  1582. trace_kvm_unmap_hva_range(start, end);
  1583. handle_hva_to_gpa(kvm, start, end, &kvm_unmap_hva_handler, &blockable);
  1584. return 0;
  1585. }
  1586. static int kvm_set_spte_handler(struct kvm *kvm, gpa_t gpa, u64 size, void *data)
  1587. {
  1588. pte_t *pte = (pte_t *)data;
  1589. WARN_ON(size != PAGE_SIZE);
  1590. /*
  1591. * We can always call stage2_set_pte with KVM_S2PTE_FLAG_LOGGING_ACTIVE
  1592. * flag clear because MMU notifiers will have unmapped a huge PMD before
  1593. * calling ->change_pte() (which in turn calls kvm_set_spte_hva()) and
  1594. * therefore stage2_set_pte() never needs to clear out a huge PMD
  1595. * through this calling path.
  1596. */
  1597. stage2_set_pte(kvm, NULL, gpa, pte, 0);
  1598. return 0;
  1599. }
  1600. void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
  1601. {
  1602. unsigned long end = hva + PAGE_SIZE;
  1603. kvm_pfn_t pfn = pte_pfn(pte);
  1604. pte_t stage2_pte;
  1605. if (!kvm->arch.pgd)
  1606. return;
  1607. trace_kvm_set_spte_hva(hva);
  1608. /*
  1609. * We've moved a page around, probably through CoW, so let's treat it
  1610. * just like a translation fault and clean the cache to the PoC.
  1611. */
  1612. clean_dcache_guest_page(pfn, PAGE_SIZE);
  1613. stage2_pte = pfn_pte(pfn, PAGE_S2);
  1614. handle_hva_to_gpa(kvm, hva, end, &kvm_set_spte_handler, &stage2_pte);
  1615. }
  1616. static int kvm_age_hva_handler(struct kvm *kvm, gpa_t gpa, u64 size, void *data)
  1617. {
  1618. pmd_t *pmd;
  1619. pte_t *pte;
  1620. WARN_ON(size != PAGE_SIZE && size != PMD_SIZE);
  1621. pmd = stage2_get_pmd(kvm, NULL, gpa);
  1622. if (!pmd || pmd_none(*pmd)) /* Nothing there */
  1623. return 0;
  1624. if (pmd_thp_or_huge(*pmd)) /* THP, HugeTLB */
  1625. return stage2_pmdp_test_and_clear_young(pmd);
  1626. pte = pte_offset_kernel(pmd, gpa);
  1627. if (pte_none(*pte))
  1628. return 0;
  1629. return stage2_ptep_test_and_clear_young(pte);
  1630. }
  1631. static int kvm_test_age_hva_handler(struct kvm *kvm, gpa_t gpa, u64 size, void *data)
  1632. {
  1633. pmd_t *pmd;
  1634. pte_t *pte;
  1635. WARN_ON(size != PAGE_SIZE && size != PMD_SIZE);
  1636. pmd = stage2_get_pmd(kvm, NULL, gpa);
  1637. if (!pmd || pmd_none(*pmd)) /* Nothing there */
  1638. return 0;
  1639. if (pmd_thp_or_huge(*pmd)) /* THP, HugeTLB */
  1640. return pmd_young(*pmd);
  1641. pte = pte_offset_kernel(pmd, gpa);
  1642. if (!pte_none(*pte)) /* Just a page... */
  1643. return pte_young(*pte);
  1644. return 0;
  1645. }
  1646. int kvm_age_hva(struct kvm *kvm, unsigned long start, unsigned long end)
  1647. {
  1648. if (!kvm->arch.pgd)
  1649. return 0;
  1650. trace_kvm_age_hva(start, end);
  1651. return handle_hva_to_gpa(kvm, start, end, kvm_age_hva_handler, NULL);
  1652. }
  1653. int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
  1654. {
  1655. if (!kvm->arch.pgd)
  1656. return 0;
  1657. trace_kvm_test_age_hva(hva);
  1658. return handle_hva_to_gpa(kvm, hva, hva + PAGE_SIZE,
  1659. kvm_test_age_hva_handler, NULL);
  1660. }
  1661. void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu)
  1662. {
  1663. mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
  1664. }
  1665. phys_addr_t kvm_mmu_get_httbr(void)
  1666. {
  1667. if (__kvm_cpu_uses_extended_idmap())
  1668. return virt_to_phys(merged_hyp_pgd);
  1669. else
  1670. return virt_to_phys(hyp_pgd);
  1671. }
  1672. phys_addr_t kvm_get_idmap_vector(void)
  1673. {
  1674. return hyp_idmap_vector;
  1675. }
  1676. static int kvm_map_idmap_text(pgd_t *pgd)
  1677. {
  1678. int err;
  1679. /* Create the idmap in the boot page tables */
  1680. err = __create_hyp_mappings(pgd, __kvm_idmap_ptrs_per_pgd(),
  1681. hyp_idmap_start, hyp_idmap_end,
  1682. __phys_to_pfn(hyp_idmap_start),
  1683. PAGE_HYP_EXEC);
  1684. if (err)
  1685. kvm_err("Failed to idmap %lx-%lx\n",
  1686. hyp_idmap_start, hyp_idmap_end);
  1687. return err;
  1688. }
  1689. int kvm_mmu_init(void)
  1690. {
  1691. int err;
  1692. hyp_idmap_start = kvm_virt_to_phys(__hyp_idmap_text_start);
  1693. hyp_idmap_start = ALIGN_DOWN(hyp_idmap_start, PAGE_SIZE);
  1694. hyp_idmap_end = kvm_virt_to_phys(__hyp_idmap_text_end);
  1695. hyp_idmap_end = ALIGN(hyp_idmap_end, PAGE_SIZE);
  1696. hyp_idmap_vector = kvm_virt_to_phys(__kvm_hyp_init);
  1697. /*
  1698. * We rely on the linker script to ensure at build time that the HYP
  1699. * init code does not cross a page boundary.
  1700. */
  1701. BUG_ON((hyp_idmap_start ^ (hyp_idmap_end - 1)) & PAGE_MASK);
  1702. kvm_debug("IDMAP page: %lx\n", hyp_idmap_start);
  1703. kvm_debug("HYP VA range: %lx:%lx\n",
  1704. kern_hyp_va(PAGE_OFFSET),
  1705. kern_hyp_va((unsigned long)high_memory - 1));
  1706. if (hyp_idmap_start >= kern_hyp_va(PAGE_OFFSET) &&
  1707. hyp_idmap_start < kern_hyp_va((unsigned long)high_memory - 1) &&
  1708. hyp_idmap_start != (unsigned long)__hyp_idmap_text_start) {
  1709. /*
  1710. * The idmap page is intersecting with the VA space,
  1711. * it is not safe to continue further.
  1712. */
  1713. kvm_err("IDMAP intersecting with HYP VA, unable to continue\n");
  1714. err = -EINVAL;
  1715. goto out;
  1716. }
  1717. hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, hyp_pgd_order);
  1718. if (!hyp_pgd) {
  1719. kvm_err("Hyp mode PGD not allocated\n");
  1720. err = -ENOMEM;
  1721. goto out;
  1722. }
  1723. if (__kvm_cpu_uses_extended_idmap()) {
  1724. boot_hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  1725. hyp_pgd_order);
  1726. if (!boot_hyp_pgd) {
  1727. kvm_err("Hyp boot PGD not allocated\n");
  1728. err = -ENOMEM;
  1729. goto out;
  1730. }
  1731. err = kvm_map_idmap_text(boot_hyp_pgd);
  1732. if (err)
  1733. goto out;
  1734. merged_hyp_pgd = (pgd_t *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
  1735. if (!merged_hyp_pgd) {
  1736. kvm_err("Failed to allocate extra HYP pgd\n");
  1737. goto out;
  1738. }
  1739. __kvm_extend_hypmap(boot_hyp_pgd, hyp_pgd, merged_hyp_pgd,
  1740. hyp_idmap_start);
  1741. } else {
  1742. err = kvm_map_idmap_text(hyp_pgd);
  1743. if (err)
  1744. goto out;
  1745. }
  1746. io_map_base = hyp_idmap_start;
  1747. return 0;
  1748. out:
  1749. free_hyp_pgds();
  1750. return err;
  1751. }
  1752. void kvm_arch_commit_memory_region(struct kvm *kvm,
  1753. const struct kvm_userspace_memory_region *mem,
  1754. const struct kvm_memory_slot *old,
  1755. const struct kvm_memory_slot *new,
  1756. enum kvm_mr_change change)
  1757. {
  1758. /*
  1759. * At this point memslot has been committed and there is an
  1760. * allocated dirty_bitmap[], dirty pages will be be tracked while the
  1761. * memory slot is write protected.
  1762. */
  1763. if (change != KVM_MR_DELETE && mem->flags & KVM_MEM_LOG_DIRTY_PAGES)
  1764. kvm_mmu_wp_memory_region(kvm, mem->slot);
  1765. }
  1766. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  1767. struct kvm_memory_slot *memslot,
  1768. const struct kvm_userspace_memory_region *mem,
  1769. enum kvm_mr_change change)
  1770. {
  1771. hva_t hva = mem->userspace_addr;
  1772. hva_t reg_end = hva + mem->memory_size;
  1773. bool writable = !(mem->flags & KVM_MEM_READONLY);
  1774. int ret = 0;
  1775. if (change != KVM_MR_CREATE && change != KVM_MR_MOVE &&
  1776. change != KVM_MR_FLAGS_ONLY)
  1777. return 0;
  1778. /*
  1779. * Prevent userspace from creating a memory region outside of the IPA
  1780. * space addressable by the KVM guest IPA space.
  1781. */
  1782. if (memslot->base_gfn + memslot->npages >
  1783. (KVM_PHYS_SIZE >> PAGE_SHIFT))
  1784. return -EFAULT;
  1785. down_read(&current->mm->mmap_sem);
  1786. /*
  1787. * A memory region could potentially cover multiple VMAs, and any holes
  1788. * between them, so iterate over all of them to find out if we can map
  1789. * any of them right now.
  1790. *
  1791. * +--------------------------------------------+
  1792. * +---------------+----------------+ +----------------+
  1793. * | : VMA 1 | VMA 2 | | VMA 3 : |
  1794. * +---------------+----------------+ +----------------+
  1795. * | memory region |
  1796. * +--------------------------------------------+
  1797. */
  1798. do {
  1799. struct vm_area_struct *vma = find_vma(current->mm, hva);
  1800. hva_t vm_start, vm_end;
  1801. if (!vma || vma->vm_start >= reg_end)
  1802. break;
  1803. /*
  1804. * Mapping a read-only VMA is only allowed if the
  1805. * memory region is configured as read-only.
  1806. */
  1807. if (writable && !(vma->vm_flags & VM_WRITE)) {
  1808. ret = -EPERM;
  1809. break;
  1810. }
  1811. /*
  1812. * Take the intersection of this VMA with the memory region
  1813. */
  1814. vm_start = max(hva, vma->vm_start);
  1815. vm_end = min(reg_end, vma->vm_end);
  1816. if (vma->vm_flags & VM_PFNMAP) {
  1817. gpa_t gpa = mem->guest_phys_addr +
  1818. (vm_start - mem->userspace_addr);
  1819. phys_addr_t pa;
  1820. pa = (phys_addr_t)vma->vm_pgoff << PAGE_SHIFT;
  1821. pa += vm_start - vma->vm_start;
  1822. /* IO region dirty page logging not allowed */
  1823. if (memslot->flags & KVM_MEM_LOG_DIRTY_PAGES) {
  1824. ret = -EINVAL;
  1825. goto out;
  1826. }
  1827. ret = kvm_phys_addr_ioremap(kvm, gpa, pa,
  1828. vm_end - vm_start,
  1829. writable);
  1830. if (ret)
  1831. break;
  1832. }
  1833. hva = vm_end;
  1834. } while (hva < reg_end);
  1835. if (change == KVM_MR_FLAGS_ONLY)
  1836. goto out;
  1837. spin_lock(&kvm->mmu_lock);
  1838. if (ret)
  1839. unmap_stage2_range(kvm, mem->guest_phys_addr, mem->memory_size);
  1840. else
  1841. stage2_flush_memslot(kvm, memslot);
  1842. spin_unlock(&kvm->mmu_lock);
  1843. out:
  1844. up_read(&current->mm->mmap_sem);
  1845. return ret;
  1846. }
  1847. void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
  1848. struct kvm_memory_slot *dont)
  1849. {
  1850. }
  1851. int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
  1852. unsigned long npages)
  1853. {
  1854. return 0;
  1855. }
  1856. void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen)
  1857. {
  1858. }
  1859. void kvm_arch_flush_shadow_all(struct kvm *kvm)
  1860. {
  1861. kvm_free_stage2_pgd(kvm);
  1862. }
  1863. void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
  1864. struct kvm_memory_slot *slot)
  1865. {
  1866. gpa_t gpa = slot->base_gfn << PAGE_SHIFT;
  1867. phys_addr_t size = slot->npages << PAGE_SHIFT;
  1868. spin_lock(&kvm->mmu_lock);
  1869. unmap_stage2_range(kvm, gpa, size);
  1870. spin_unlock(&kvm->mmu_lock);
  1871. }
  1872. /*
  1873. * See note at ARMv7 ARM B1.14.4 (TL;DR: S/W ops are not easily virtualized).
  1874. *
  1875. * Main problems:
  1876. * - S/W ops are local to a CPU (not broadcast)
  1877. * - We have line migration behind our back (speculation)
  1878. * - System caches don't support S/W at all (damn!)
  1879. *
  1880. * In the face of the above, the best we can do is to try and convert
  1881. * S/W ops to VA ops. Because the guest is not allowed to infer the
  1882. * S/W to PA mapping, it can only use S/W to nuke the whole cache,
  1883. * which is a rather good thing for us.
  1884. *
  1885. * Also, it is only used when turning caches on/off ("The expected
  1886. * usage of the cache maintenance instructions that operate by set/way
  1887. * is associated with the cache maintenance instructions associated
  1888. * with the powerdown and powerup of caches, if this is required by
  1889. * the implementation.").
  1890. *
  1891. * We use the following policy:
  1892. *
  1893. * - If we trap a S/W operation, we enable VM trapping to detect
  1894. * caches being turned on/off, and do a full clean.
  1895. *
  1896. * - We flush the caches on both caches being turned on and off.
  1897. *
  1898. * - Once the caches are enabled, we stop trapping VM ops.
  1899. */
  1900. void kvm_set_way_flush(struct kvm_vcpu *vcpu)
  1901. {
  1902. unsigned long hcr = *vcpu_hcr(vcpu);
  1903. /*
  1904. * If this is the first time we do a S/W operation
  1905. * (i.e. HCR_TVM not set) flush the whole memory, and set the
  1906. * VM trapping.
  1907. *
  1908. * Otherwise, rely on the VM trapping to wait for the MMU +
  1909. * Caches to be turned off. At that point, we'll be able to
  1910. * clean the caches again.
  1911. */
  1912. if (!(hcr & HCR_TVM)) {
  1913. trace_kvm_set_way_flush(*vcpu_pc(vcpu),
  1914. vcpu_has_cache_enabled(vcpu));
  1915. stage2_flush_vm(vcpu->kvm);
  1916. *vcpu_hcr(vcpu) = hcr | HCR_TVM;
  1917. }
  1918. }
  1919. void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled)
  1920. {
  1921. bool now_enabled = vcpu_has_cache_enabled(vcpu);
  1922. /*
  1923. * If switching the MMU+caches on, need to invalidate the caches.
  1924. * If switching it off, need to clean the caches.
  1925. * Clean + invalidate does the trick always.
  1926. */
  1927. if (now_enabled != was_enabled)
  1928. stage2_flush_vm(vcpu->kvm);
  1929. /* Caches are now on, stop trapping VM ops (until a S/W op) */
  1930. if (now_enabled)
  1931. *vcpu_hcr(vcpu) &= ~HCR_TVM;
  1932. trace_kvm_toggle_cache(*vcpu_pc(vcpu), was_enabled, now_enabled);
  1933. }