mmu.c 52 KB

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