mmu.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980
  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->arch.pgd = pgd;
  642. return 0;
  643. }
  644. static void stage2_unmap_memslot(struct kvm *kvm,
  645. struct kvm_memory_slot *memslot)
  646. {
  647. hva_t hva = memslot->userspace_addr;
  648. phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
  649. phys_addr_t size = PAGE_SIZE * memslot->npages;
  650. hva_t reg_end = hva + size;
  651. /*
  652. * A memory region could potentially cover multiple VMAs, and any holes
  653. * between them, so iterate over all of them to find out if we should
  654. * unmap any of them.
  655. *
  656. * +--------------------------------------------+
  657. * +---------------+----------------+ +----------------+
  658. * | : VMA 1 | VMA 2 | | VMA 3 : |
  659. * +---------------+----------------+ +----------------+
  660. * | memory region |
  661. * +--------------------------------------------+
  662. */
  663. do {
  664. struct vm_area_struct *vma = find_vma(current->mm, hva);
  665. hva_t vm_start, vm_end;
  666. if (!vma || vma->vm_start >= reg_end)
  667. break;
  668. /*
  669. * Take the intersection of this VMA with the memory region
  670. */
  671. vm_start = max(hva, vma->vm_start);
  672. vm_end = min(reg_end, vma->vm_end);
  673. if (!(vma->vm_flags & VM_PFNMAP)) {
  674. gpa_t gpa = addr + (vm_start - memslot->userspace_addr);
  675. unmap_stage2_range(kvm, gpa, vm_end - vm_start);
  676. }
  677. hva = vm_end;
  678. } while (hva < reg_end);
  679. }
  680. /**
  681. * stage2_unmap_vm - Unmap Stage-2 RAM mappings
  682. * @kvm: The struct kvm pointer
  683. *
  684. * Go through the memregions and unmap any reguler RAM
  685. * backing memory already mapped to the VM.
  686. */
  687. void stage2_unmap_vm(struct kvm *kvm)
  688. {
  689. struct kvm_memslots *slots;
  690. struct kvm_memory_slot *memslot;
  691. int idx;
  692. idx = srcu_read_lock(&kvm->srcu);
  693. spin_lock(&kvm->mmu_lock);
  694. slots = kvm_memslots(kvm);
  695. kvm_for_each_memslot(memslot, slots)
  696. stage2_unmap_memslot(kvm, memslot);
  697. spin_unlock(&kvm->mmu_lock);
  698. srcu_read_unlock(&kvm->srcu, idx);
  699. }
  700. /**
  701. * kvm_free_stage2_pgd - free all stage-2 tables
  702. * @kvm: The KVM struct pointer for the VM.
  703. *
  704. * Walks the level-1 page table pointed to by kvm->arch.pgd and frees all
  705. * underlying level-2 and level-3 tables before freeing the actual level-1 table
  706. * and setting the struct pointer to NULL.
  707. *
  708. * Note we don't need locking here as this is only called when the VM is
  709. * destroyed, which can only be done once.
  710. */
  711. void kvm_free_stage2_pgd(struct kvm *kvm)
  712. {
  713. if (kvm->arch.pgd == NULL)
  714. return;
  715. unmap_stage2_range(kvm, 0, KVM_PHYS_SIZE);
  716. /* Free the HW pgd, one page at a time */
  717. free_pages_exact(kvm->arch.pgd, S2_PGD_SIZE);
  718. kvm->arch.pgd = NULL;
  719. }
  720. static pud_t *stage2_get_pud(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
  721. phys_addr_t addr)
  722. {
  723. pgd_t *pgd;
  724. pud_t *pud;
  725. pgd = kvm->arch.pgd + stage2_pgd_index(addr);
  726. if (WARN_ON(stage2_pgd_none(*pgd))) {
  727. if (!cache)
  728. return NULL;
  729. pud = mmu_memory_cache_alloc(cache);
  730. stage2_pgd_populate(pgd, pud);
  731. get_page(virt_to_page(pgd));
  732. }
  733. return stage2_pud_offset(pgd, addr);
  734. }
  735. static pmd_t *stage2_get_pmd(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
  736. phys_addr_t addr)
  737. {
  738. pud_t *pud;
  739. pmd_t *pmd;
  740. pud = stage2_get_pud(kvm, cache, addr);
  741. if (stage2_pud_none(*pud)) {
  742. if (!cache)
  743. return NULL;
  744. pmd = mmu_memory_cache_alloc(cache);
  745. stage2_pud_populate(pud, pmd);
  746. get_page(virt_to_page(pud));
  747. }
  748. return stage2_pmd_offset(pud, addr);
  749. }
  750. static int stage2_set_pmd_huge(struct kvm *kvm, struct kvm_mmu_memory_cache
  751. *cache, phys_addr_t addr, const pmd_t *new_pmd)
  752. {
  753. pmd_t *pmd, old_pmd;
  754. pmd = stage2_get_pmd(kvm, cache, addr);
  755. VM_BUG_ON(!pmd);
  756. /*
  757. * Mapping in huge pages should only happen through a fault. If a
  758. * page is merged into a transparent huge page, the individual
  759. * subpages of that huge page should be unmapped through MMU
  760. * notifiers before we get here.
  761. *
  762. * Merging of CompoundPages is not supported; they should become
  763. * splitting first, unmapped, merged, and mapped back in on-demand.
  764. */
  765. VM_BUG_ON(pmd_present(*pmd) && pmd_pfn(*pmd) != pmd_pfn(*new_pmd));
  766. old_pmd = *pmd;
  767. if (pmd_present(old_pmd)) {
  768. pmd_clear(pmd);
  769. kvm_tlb_flush_vmid_ipa(kvm, addr);
  770. } else {
  771. get_page(virt_to_page(pmd));
  772. }
  773. kvm_set_pmd(pmd, *new_pmd);
  774. return 0;
  775. }
  776. static int stage2_set_pte(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
  777. phys_addr_t addr, const pte_t *new_pte,
  778. unsigned long flags)
  779. {
  780. pmd_t *pmd;
  781. pte_t *pte, old_pte;
  782. bool iomap = flags & KVM_S2PTE_FLAG_IS_IOMAP;
  783. bool logging_active = flags & KVM_S2_FLAG_LOGGING_ACTIVE;
  784. VM_BUG_ON(logging_active && !cache);
  785. /* Create stage-2 page table mapping - Levels 0 and 1 */
  786. pmd = stage2_get_pmd(kvm, cache, addr);
  787. if (!pmd) {
  788. /*
  789. * Ignore calls from kvm_set_spte_hva for unallocated
  790. * address ranges.
  791. */
  792. return 0;
  793. }
  794. /*
  795. * While dirty page logging - dissolve huge PMD, then continue on to
  796. * allocate page.
  797. */
  798. if (logging_active)
  799. stage2_dissolve_pmd(kvm, addr, pmd);
  800. /* Create stage-2 page mappings - Level 2 */
  801. if (pmd_none(*pmd)) {
  802. if (!cache)
  803. return 0; /* ignore calls from kvm_set_spte_hva */
  804. pte = mmu_memory_cache_alloc(cache);
  805. pmd_populate_kernel(NULL, pmd, pte);
  806. get_page(virt_to_page(pmd));
  807. }
  808. pte = pte_offset_kernel(pmd, addr);
  809. if (iomap && pte_present(*pte))
  810. return -EFAULT;
  811. /* Create 2nd stage page table mapping - Level 3 */
  812. old_pte = *pte;
  813. if (pte_present(old_pte)) {
  814. kvm_set_pte(pte, __pte(0));
  815. kvm_tlb_flush_vmid_ipa(kvm, addr);
  816. } else {
  817. get_page(virt_to_page(pte));
  818. }
  819. kvm_set_pte(pte, *new_pte);
  820. return 0;
  821. }
  822. #ifndef __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
  823. static int stage2_ptep_test_and_clear_young(pte_t *pte)
  824. {
  825. if (pte_young(*pte)) {
  826. *pte = pte_mkold(*pte);
  827. return 1;
  828. }
  829. return 0;
  830. }
  831. #else
  832. static int stage2_ptep_test_and_clear_young(pte_t *pte)
  833. {
  834. return __ptep_test_and_clear_young(pte);
  835. }
  836. #endif
  837. static int stage2_pmdp_test_and_clear_young(pmd_t *pmd)
  838. {
  839. return stage2_ptep_test_and_clear_young((pte_t *)pmd);
  840. }
  841. /**
  842. * kvm_phys_addr_ioremap - map a device range to guest IPA
  843. *
  844. * @kvm: The KVM pointer
  845. * @guest_ipa: The IPA at which to insert the mapping
  846. * @pa: The physical address of the device
  847. * @size: The size of the mapping
  848. */
  849. int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
  850. phys_addr_t pa, unsigned long size, bool writable)
  851. {
  852. phys_addr_t addr, end;
  853. int ret = 0;
  854. unsigned long pfn;
  855. struct kvm_mmu_memory_cache cache = { 0, };
  856. end = (guest_ipa + size + PAGE_SIZE - 1) & PAGE_MASK;
  857. pfn = __phys_to_pfn(pa);
  858. for (addr = guest_ipa; addr < end; addr += PAGE_SIZE) {
  859. pte_t pte = pfn_pte(pfn, PAGE_S2_DEVICE);
  860. if (writable)
  861. pte = kvm_s2pte_mkwrite(pte);
  862. ret = mmu_topup_memory_cache(&cache, KVM_MMU_CACHE_MIN_PAGES,
  863. KVM_NR_MEM_OBJS);
  864. if (ret)
  865. goto out;
  866. spin_lock(&kvm->mmu_lock);
  867. ret = stage2_set_pte(kvm, &cache, addr, &pte,
  868. KVM_S2PTE_FLAG_IS_IOMAP);
  869. spin_unlock(&kvm->mmu_lock);
  870. if (ret)
  871. goto out;
  872. pfn++;
  873. }
  874. out:
  875. mmu_free_memory_cache(&cache);
  876. return ret;
  877. }
  878. static bool transparent_hugepage_adjust(kvm_pfn_t *pfnp, phys_addr_t *ipap)
  879. {
  880. kvm_pfn_t pfn = *pfnp;
  881. gfn_t gfn = *ipap >> PAGE_SHIFT;
  882. if (PageTransCompoundMap(pfn_to_page(pfn))) {
  883. unsigned long mask;
  884. /*
  885. * The address we faulted on is backed by a transparent huge
  886. * page. However, because we map the compound huge page and
  887. * not the individual tail page, we need to transfer the
  888. * refcount to the head page. We have to be careful that the
  889. * THP doesn't start to split while we are adjusting the
  890. * refcounts.
  891. *
  892. * We are sure this doesn't happen, because mmu_notifier_retry
  893. * was successful and we are holding the mmu_lock, so if this
  894. * THP is trying to split, it will be blocked in the mmu
  895. * notifier before touching any of the pages, specifically
  896. * before being able to call __split_huge_page_refcount().
  897. *
  898. * We can therefore safely transfer the refcount from PG_tail
  899. * to PG_head and switch the pfn from a tail page to the head
  900. * page accordingly.
  901. */
  902. mask = PTRS_PER_PMD - 1;
  903. VM_BUG_ON((gfn & mask) != (pfn & mask));
  904. if (pfn & mask) {
  905. *ipap &= PMD_MASK;
  906. kvm_release_pfn_clean(pfn);
  907. pfn &= ~mask;
  908. kvm_get_pfn(pfn);
  909. *pfnp = pfn;
  910. }
  911. return true;
  912. }
  913. return false;
  914. }
  915. static bool kvm_is_write_fault(struct kvm_vcpu *vcpu)
  916. {
  917. if (kvm_vcpu_trap_is_iabt(vcpu))
  918. return false;
  919. return kvm_vcpu_dabt_iswrite(vcpu);
  920. }
  921. /**
  922. * stage2_wp_ptes - write protect PMD range
  923. * @pmd: pointer to pmd entry
  924. * @addr: range start address
  925. * @end: range end address
  926. */
  927. static void stage2_wp_ptes(pmd_t *pmd, phys_addr_t addr, phys_addr_t end)
  928. {
  929. pte_t *pte;
  930. pte = pte_offset_kernel(pmd, addr);
  931. do {
  932. if (!pte_none(*pte)) {
  933. if (!kvm_s2pte_readonly(pte))
  934. kvm_set_s2pte_readonly(pte);
  935. }
  936. } while (pte++, addr += PAGE_SIZE, addr != end);
  937. }
  938. /**
  939. * stage2_wp_pmds - write protect PUD range
  940. * @pud: pointer to pud entry
  941. * @addr: range start address
  942. * @end: range end address
  943. */
  944. static void stage2_wp_pmds(pud_t *pud, phys_addr_t addr, phys_addr_t end)
  945. {
  946. pmd_t *pmd;
  947. phys_addr_t next;
  948. pmd = stage2_pmd_offset(pud, addr);
  949. do {
  950. next = stage2_pmd_addr_end(addr, end);
  951. if (!pmd_none(*pmd)) {
  952. if (pmd_thp_or_huge(*pmd)) {
  953. if (!kvm_s2pmd_readonly(pmd))
  954. kvm_set_s2pmd_readonly(pmd);
  955. } else {
  956. stage2_wp_ptes(pmd, addr, next);
  957. }
  958. }
  959. } while (pmd++, addr = next, addr != end);
  960. }
  961. /**
  962. * stage2_wp_puds - write protect PGD range
  963. * @pgd: pointer to pgd entry
  964. * @addr: range start address
  965. * @end: range end address
  966. *
  967. * Process PUD entries, for a huge PUD we cause a panic.
  968. */
  969. static void stage2_wp_puds(pgd_t *pgd, phys_addr_t addr, phys_addr_t end)
  970. {
  971. pud_t *pud;
  972. phys_addr_t next;
  973. pud = stage2_pud_offset(pgd, addr);
  974. do {
  975. next = stage2_pud_addr_end(addr, end);
  976. if (!stage2_pud_none(*pud)) {
  977. /* TODO:PUD not supported, revisit later if supported */
  978. BUG_ON(stage2_pud_huge(*pud));
  979. stage2_wp_pmds(pud, addr, next);
  980. }
  981. } while (pud++, addr = next, addr != end);
  982. }
  983. /**
  984. * stage2_wp_range() - write protect stage2 memory region range
  985. * @kvm: The KVM pointer
  986. * @addr: Start address of range
  987. * @end: End address of range
  988. */
  989. static void stage2_wp_range(struct kvm *kvm, phys_addr_t addr, phys_addr_t end)
  990. {
  991. pgd_t *pgd;
  992. phys_addr_t next;
  993. pgd = kvm->arch.pgd + stage2_pgd_index(addr);
  994. do {
  995. /*
  996. * Release kvm_mmu_lock periodically if the memory region is
  997. * large. Otherwise, we may see kernel panics with
  998. * CONFIG_DETECT_HUNG_TASK, CONFIG_LOCKUP_DETECTOR,
  999. * CONFIG_LOCKDEP. Additionally, holding the lock too long
  1000. * will also starve other vCPUs.
  1001. */
  1002. if (need_resched() || spin_needbreak(&kvm->mmu_lock))
  1003. cond_resched_lock(&kvm->mmu_lock);
  1004. next = stage2_pgd_addr_end(addr, end);
  1005. if (stage2_pgd_present(*pgd))
  1006. stage2_wp_puds(pgd, addr, next);
  1007. } while (pgd++, addr = next, addr != end);
  1008. }
  1009. /**
  1010. * kvm_mmu_wp_memory_region() - write protect stage 2 entries for memory slot
  1011. * @kvm: The KVM pointer
  1012. * @slot: The memory slot to write protect
  1013. *
  1014. * Called to start logging dirty pages after memory region
  1015. * KVM_MEM_LOG_DIRTY_PAGES operation is called. After this function returns
  1016. * all present PMD and PTEs are write protected in the memory region.
  1017. * Afterwards read of dirty page log can be called.
  1018. *
  1019. * Acquires kvm_mmu_lock. Called with kvm->slots_lock mutex acquired,
  1020. * serializing operations for VM memory regions.
  1021. */
  1022. void kvm_mmu_wp_memory_region(struct kvm *kvm, int slot)
  1023. {
  1024. struct kvm_memslots *slots = kvm_memslots(kvm);
  1025. struct kvm_memory_slot *memslot = id_to_memslot(slots, slot);
  1026. phys_addr_t start = memslot->base_gfn << PAGE_SHIFT;
  1027. phys_addr_t end = (memslot->base_gfn + memslot->npages) << PAGE_SHIFT;
  1028. spin_lock(&kvm->mmu_lock);
  1029. stage2_wp_range(kvm, start, end);
  1030. spin_unlock(&kvm->mmu_lock);
  1031. kvm_flush_remote_tlbs(kvm);
  1032. }
  1033. /**
  1034. * kvm_mmu_write_protect_pt_masked() - write protect dirty pages
  1035. * @kvm: The KVM pointer
  1036. * @slot: The memory slot associated with mask
  1037. * @gfn_offset: The gfn offset in memory slot
  1038. * @mask: The mask of dirty pages at offset 'gfn_offset' in this memory
  1039. * slot to be write protected
  1040. *
  1041. * Walks bits set in mask write protects the associated pte's. Caller must
  1042. * acquire kvm_mmu_lock.
  1043. */
  1044. static void kvm_mmu_write_protect_pt_masked(struct kvm *kvm,
  1045. struct kvm_memory_slot *slot,
  1046. gfn_t gfn_offset, unsigned long mask)
  1047. {
  1048. phys_addr_t base_gfn = slot->base_gfn + gfn_offset;
  1049. phys_addr_t start = (base_gfn + __ffs(mask)) << PAGE_SHIFT;
  1050. phys_addr_t end = (base_gfn + __fls(mask) + 1) << PAGE_SHIFT;
  1051. stage2_wp_range(kvm, start, end);
  1052. }
  1053. /*
  1054. * kvm_arch_mmu_enable_log_dirty_pt_masked - enable dirty logging for selected
  1055. * dirty pages.
  1056. *
  1057. * It calls kvm_mmu_write_protect_pt_masked to write protect selected pages to
  1058. * enable dirty logging for them.
  1059. */
  1060. void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm,
  1061. struct kvm_memory_slot *slot,
  1062. gfn_t gfn_offset, unsigned long mask)
  1063. {
  1064. kvm_mmu_write_protect_pt_masked(kvm, slot, gfn_offset, mask);
  1065. }
  1066. static void coherent_cache_guest_page(struct kvm_vcpu *vcpu, kvm_pfn_t pfn,
  1067. unsigned long size, bool uncached)
  1068. {
  1069. __coherent_cache_guest_page(vcpu, pfn, size, uncached);
  1070. }
  1071. static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
  1072. struct kvm_memory_slot *memslot, unsigned long hva,
  1073. unsigned long fault_status)
  1074. {
  1075. int ret;
  1076. bool write_fault, writable, hugetlb = false, force_pte = false;
  1077. unsigned long mmu_seq;
  1078. gfn_t gfn = fault_ipa >> PAGE_SHIFT;
  1079. struct kvm *kvm = vcpu->kvm;
  1080. struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
  1081. struct vm_area_struct *vma;
  1082. kvm_pfn_t pfn;
  1083. pgprot_t mem_type = PAGE_S2;
  1084. bool fault_ipa_uncached;
  1085. bool logging_active = memslot_is_logging(memslot);
  1086. unsigned long flags = 0;
  1087. write_fault = kvm_is_write_fault(vcpu);
  1088. if (fault_status == FSC_PERM && !write_fault) {
  1089. kvm_err("Unexpected L2 read permission error\n");
  1090. return -EFAULT;
  1091. }
  1092. /* Let's check if we will get back a huge page backed by hugetlbfs */
  1093. down_read(&current->mm->mmap_sem);
  1094. vma = find_vma_intersection(current->mm, hva, hva + 1);
  1095. if (unlikely(!vma)) {
  1096. kvm_err("Failed to find VMA for hva 0x%lx\n", hva);
  1097. up_read(&current->mm->mmap_sem);
  1098. return -EFAULT;
  1099. }
  1100. if (is_vm_hugetlb_page(vma) && !logging_active) {
  1101. hugetlb = true;
  1102. gfn = (fault_ipa & PMD_MASK) >> PAGE_SHIFT;
  1103. } else {
  1104. /*
  1105. * Pages belonging to memslots that don't have the same
  1106. * alignment for userspace and IPA cannot be mapped using
  1107. * block descriptors even if the pages belong to a THP for
  1108. * the process, because the stage-2 block descriptor will
  1109. * cover more than a single THP and we loose atomicity for
  1110. * unmapping, updates, and splits of the THP or other pages
  1111. * in the stage-2 block range.
  1112. */
  1113. if ((memslot->userspace_addr & ~PMD_MASK) !=
  1114. ((memslot->base_gfn << PAGE_SHIFT) & ~PMD_MASK))
  1115. force_pte = true;
  1116. }
  1117. up_read(&current->mm->mmap_sem);
  1118. /* We need minimum second+third level pages */
  1119. ret = mmu_topup_memory_cache(memcache, KVM_MMU_CACHE_MIN_PAGES,
  1120. KVM_NR_MEM_OBJS);
  1121. if (ret)
  1122. return ret;
  1123. mmu_seq = vcpu->kvm->mmu_notifier_seq;
  1124. /*
  1125. * Ensure the read of mmu_notifier_seq happens before we call
  1126. * gfn_to_pfn_prot (which calls get_user_pages), so that we don't risk
  1127. * the page we just got a reference to gets unmapped before we have a
  1128. * chance to grab the mmu_lock, which ensure that if the page gets
  1129. * unmapped afterwards, the call to kvm_unmap_hva will take it away
  1130. * from us again properly. This smp_rmb() interacts with the smp_wmb()
  1131. * in kvm_mmu_notifier_invalidate_<page|range_end>.
  1132. */
  1133. smp_rmb();
  1134. pfn = gfn_to_pfn_prot(kvm, gfn, write_fault, &writable);
  1135. if (is_error_noslot_pfn(pfn))
  1136. return -EFAULT;
  1137. if (kvm_is_device_pfn(pfn)) {
  1138. mem_type = PAGE_S2_DEVICE;
  1139. flags |= KVM_S2PTE_FLAG_IS_IOMAP;
  1140. } else if (logging_active) {
  1141. /*
  1142. * Faults on pages in a memslot with logging enabled
  1143. * should not be mapped with huge pages (it introduces churn
  1144. * and performance degradation), so force a pte mapping.
  1145. */
  1146. force_pte = true;
  1147. flags |= KVM_S2_FLAG_LOGGING_ACTIVE;
  1148. /*
  1149. * Only actually map the page as writable if this was a write
  1150. * fault.
  1151. */
  1152. if (!write_fault)
  1153. writable = false;
  1154. }
  1155. spin_lock(&kvm->mmu_lock);
  1156. if (mmu_notifier_retry(kvm, mmu_seq))
  1157. goto out_unlock;
  1158. if (!hugetlb && !force_pte)
  1159. hugetlb = transparent_hugepage_adjust(&pfn, &fault_ipa);
  1160. fault_ipa_uncached = memslot->flags & KVM_MEMSLOT_INCOHERENT;
  1161. if (hugetlb) {
  1162. pmd_t new_pmd = pfn_pmd(pfn, mem_type);
  1163. new_pmd = pmd_mkhuge(new_pmd);
  1164. if (writable) {
  1165. new_pmd = kvm_s2pmd_mkwrite(new_pmd);
  1166. kvm_set_pfn_dirty(pfn);
  1167. }
  1168. coherent_cache_guest_page(vcpu, pfn, PMD_SIZE, fault_ipa_uncached);
  1169. ret = stage2_set_pmd_huge(kvm, memcache, fault_ipa, &new_pmd);
  1170. } else {
  1171. pte_t new_pte = pfn_pte(pfn, mem_type);
  1172. if (writable) {
  1173. new_pte = kvm_s2pte_mkwrite(new_pte);
  1174. kvm_set_pfn_dirty(pfn);
  1175. mark_page_dirty(kvm, gfn);
  1176. }
  1177. coherent_cache_guest_page(vcpu, pfn, PAGE_SIZE, fault_ipa_uncached);
  1178. ret = stage2_set_pte(kvm, memcache, fault_ipa, &new_pte, flags);
  1179. }
  1180. out_unlock:
  1181. spin_unlock(&kvm->mmu_lock);
  1182. kvm_set_pfn_accessed(pfn);
  1183. kvm_release_pfn_clean(pfn);
  1184. return ret;
  1185. }
  1186. /*
  1187. * Resolve the access fault by making the page young again.
  1188. * Note that because the faulting entry is guaranteed not to be
  1189. * cached in the TLB, we don't need to invalidate anything.
  1190. * Only the HW Access Flag updates are supported for Stage 2 (no DBM),
  1191. * so there is no need for atomic (pte|pmd)_mkyoung operations.
  1192. */
  1193. static void handle_access_fault(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa)
  1194. {
  1195. pmd_t *pmd;
  1196. pte_t *pte;
  1197. kvm_pfn_t pfn;
  1198. bool pfn_valid = false;
  1199. trace_kvm_access_fault(fault_ipa);
  1200. spin_lock(&vcpu->kvm->mmu_lock);
  1201. pmd = stage2_get_pmd(vcpu->kvm, NULL, fault_ipa);
  1202. if (!pmd || pmd_none(*pmd)) /* Nothing there */
  1203. goto out;
  1204. if (pmd_thp_or_huge(*pmd)) { /* THP, HugeTLB */
  1205. *pmd = pmd_mkyoung(*pmd);
  1206. pfn = pmd_pfn(*pmd);
  1207. pfn_valid = true;
  1208. goto out;
  1209. }
  1210. pte = pte_offset_kernel(pmd, fault_ipa);
  1211. if (pte_none(*pte)) /* Nothing there either */
  1212. goto out;
  1213. *pte = pte_mkyoung(*pte); /* Just a page... */
  1214. pfn = pte_pfn(*pte);
  1215. pfn_valid = true;
  1216. out:
  1217. spin_unlock(&vcpu->kvm->mmu_lock);
  1218. if (pfn_valid)
  1219. kvm_set_pfn_accessed(pfn);
  1220. }
  1221. /**
  1222. * kvm_handle_guest_abort - handles all 2nd stage aborts
  1223. * @vcpu: the VCPU pointer
  1224. * @run: the kvm_run structure
  1225. *
  1226. * Any abort that gets to the host is almost guaranteed to be caused by a
  1227. * missing second stage translation table entry, which can mean that either the
  1228. * guest simply needs more memory and we must allocate an appropriate page or it
  1229. * can mean that the guest tried to access I/O memory, which is emulated by user
  1230. * space. The distinction is based on the IPA causing the fault and whether this
  1231. * memory region has been registered as standard RAM by user space.
  1232. */
  1233. int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run)
  1234. {
  1235. unsigned long fault_status;
  1236. phys_addr_t fault_ipa;
  1237. struct kvm_memory_slot *memslot;
  1238. unsigned long hva;
  1239. bool is_iabt, write_fault, writable;
  1240. gfn_t gfn;
  1241. int ret, idx;
  1242. is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
  1243. if (unlikely(!is_iabt && kvm_vcpu_dabt_isextabt(vcpu))) {
  1244. kvm_inject_vabt(vcpu);
  1245. return 1;
  1246. }
  1247. fault_ipa = kvm_vcpu_get_fault_ipa(vcpu);
  1248. trace_kvm_guest_fault(*vcpu_pc(vcpu), kvm_vcpu_get_hsr(vcpu),
  1249. kvm_vcpu_get_hfar(vcpu), fault_ipa);
  1250. /* Check the stage-2 fault is trans. fault or write fault */
  1251. fault_status = kvm_vcpu_trap_get_fault_type(vcpu);
  1252. if (fault_status != FSC_FAULT && fault_status != FSC_PERM &&
  1253. fault_status != FSC_ACCESS) {
  1254. kvm_err("Unsupported FSC: EC=%#x xFSC=%#lx ESR_EL2=%#lx\n",
  1255. kvm_vcpu_trap_get_class(vcpu),
  1256. (unsigned long)kvm_vcpu_trap_get_fault(vcpu),
  1257. (unsigned long)kvm_vcpu_get_hsr(vcpu));
  1258. return -EFAULT;
  1259. }
  1260. idx = srcu_read_lock(&vcpu->kvm->srcu);
  1261. gfn = fault_ipa >> PAGE_SHIFT;
  1262. memslot = gfn_to_memslot(vcpu->kvm, gfn);
  1263. hva = gfn_to_hva_memslot_prot(memslot, gfn, &writable);
  1264. write_fault = kvm_is_write_fault(vcpu);
  1265. if (kvm_is_error_hva(hva) || (write_fault && !writable)) {
  1266. if (is_iabt) {
  1267. /* Prefetch Abort on I/O address */
  1268. kvm_inject_pabt(vcpu, kvm_vcpu_get_hfar(vcpu));
  1269. ret = 1;
  1270. goto out_unlock;
  1271. }
  1272. /*
  1273. * Check for a cache maintenance operation. Since we
  1274. * ended-up here, we know it is outside of any memory
  1275. * slot. But we can't find out if that is for a device,
  1276. * or if the guest is just being stupid. The only thing
  1277. * we know for sure is that this range cannot be cached.
  1278. *
  1279. * So let's assume that the guest is just being
  1280. * cautious, and skip the instruction.
  1281. */
  1282. if (kvm_vcpu_dabt_is_cm(vcpu)) {
  1283. kvm_skip_instr(vcpu, kvm_vcpu_trap_il_is32bit(vcpu));
  1284. ret = 1;
  1285. goto out_unlock;
  1286. }
  1287. /*
  1288. * The IPA is reported as [MAX:12], so we need to
  1289. * complement it with the bottom 12 bits from the
  1290. * faulting VA. This is always 12 bits, irrespective
  1291. * of the page size.
  1292. */
  1293. fault_ipa |= kvm_vcpu_get_hfar(vcpu) & ((1 << 12) - 1);
  1294. ret = io_mem_abort(vcpu, run, fault_ipa);
  1295. goto out_unlock;
  1296. }
  1297. /* Userspace should not be able to register out-of-bounds IPAs */
  1298. VM_BUG_ON(fault_ipa >= KVM_PHYS_SIZE);
  1299. if (fault_status == FSC_ACCESS) {
  1300. handle_access_fault(vcpu, fault_ipa);
  1301. ret = 1;
  1302. goto out_unlock;
  1303. }
  1304. ret = user_mem_abort(vcpu, fault_ipa, memslot, hva, fault_status);
  1305. if (ret == 0)
  1306. ret = 1;
  1307. out_unlock:
  1308. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  1309. return ret;
  1310. }
  1311. static int handle_hva_to_gpa(struct kvm *kvm,
  1312. unsigned long start,
  1313. unsigned long end,
  1314. int (*handler)(struct kvm *kvm,
  1315. gpa_t gpa, void *data),
  1316. void *data)
  1317. {
  1318. struct kvm_memslots *slots;
  1319. struct kvm_memory_slot *memslot;
  1320. int ret = 0;
  1321. slots = kvm_memslots(kvm);
  1322. /* we only care about the pages that the guest sees */
  1323. kvm_for_each_memslot(memslot, slots) {
  1324. unsigned long hva_start, hva_end;
  1325. gfn_t gfn, gfn_end;
  1326. hva_start = max(start, memslot->userspace_addr);
  1327. hva_end = min(end, memslot->userspace_addr +
  1328. (memslot->npages << PAGE_SHIFT));
  1329. if (hva_start >= hva_end)
  1330. continue;
  1331. /*
  1332. * {gfn(page) | page intersects with [hva_start, hva_end)} =
  1333. * {gfn_start, gfn_start+1, ..., gfn_end-1}.
  1334. */
  1335. gfn = hva_to_gfn_memslot(hva_start, memslot);
  1336. gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot);
  1337. for (; gfn < gfn_end; ++gfn) {
  1338. gpa_t gpa = gfn << PAGE_SHIFT;
  1339. ret |= handler(kvm, gpa, data);
  1340. }
  1341. }
  1342. return ret;
  1343. }
  1344. static int kvm_unmap_hva_handler(struct kvm *kvm, gpa_t gpa, void *data)
  1345. {
  1346. unmap_stage2_range(kvm, gpa, PAGE_SIZE);
  1347. return 0;
  1348. }
  1349. int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
  1350. {
  1351. unsigned long end = hva + PAGE_SIZE;
  1352. if (!kvm->arch.pgd)
  1353. return 0;
  1354. trace_kvm_unmap_hva(hva);
  1355. handle_hva_to_gpa(kvm, hva, end, &kvm_unmap_hva_handler, NULL);
  1356. return 0;
  1357. }
  1358. int kvm_unmap_hva_range(struct kvm *kvm,
  1359. unsigned long start, unsigned long end)
  1360. {
  1361. if (!kvm->arch.pgd)
  1362. return 0;
  1363. trace_kvm_unmap_hva_range(start, end);
  1364. handle_hva_to_gpa(kvm, start, end, &kvm_unmap_hva_handler, NULL);
  1365. return 0;
  1366. }
  1367. static int kvm_set_spte_handler(struct kvm *kvm, gpa_t gpa, void *data)
  1368. {
  1369. pte_t *pte = (pte_t *)data;
  1370. /*
  1371. * We can always call stage2_set_pte with KVM_S2PTE_FLAG_LOGGING_ACTIVE
  1372. * flag clear because MMU notifiers will have unmapped a huge PMD before
  1373. * calling ->change_pte() (which in turn calls kvm_set_spte_hva()) and
  1374. * therefore stage2_set_pte() never needs to clear out a huge PMD
  1375. * through this calling path.
  1376. */
  1377. stage2_set_pte(kvm, NULL, gpa, pte, 0);
  1378. return 0;
  1379. }
  1380. void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
  1381. {
  1382. unsigned long end = hva + PAGE_SIZE;
  1383. pte_t stage2_pte;
  1384. if (!kvm->arch.pgd)
  1385. return;
  1386. trace_kvm_set_spte_hva(hva);
  1387. stage2_pte = pfn_pte(pte_pfn(pte), PAGE_S2);
  1388. handle_hva_to_gpa(kvm, hva, end, &kvm_set_spte_handler, &stage2_pte);
  1389. }
  1390. static int kvm_age_hva_handler(struct kvm *kvm, gpa_t gpa, void *data)
  1391. {
  1392. pmd_t *pmd;
  1393. pte_t *pte;
  1394. pmd = stage2_get_pmd(kvm, NULL, gpa);
  1395. if (!pmd || pmd_none(*pmd)) /* Nothing there */
  1396. return 0;
  1397. if (pmd_thp_or_huge(*pmd)) /* THP, HugeTLB */
  1398. return stage2_pmdp_test_and_clear_young(pmd);
  1399. pte = pte_offset_kernel(pmd, gpa);
  1400. if (pte_none(*pte))
  1401. return 0;
  1402. return stage2_ptep_test_and_clear_young(pte);
  1403. }
  1404. static int kvm_test_age_hva_handler(struct kvm *kvm, gpa_t gpa, void *data)
  1405. {
  1406. pmd_t *pmd;
  1407. pte_t *pte;
  1408. pmd = stage2_get_pmd(kvm, NULL, gpa);
  1409. if (!pmd || pmd_none(*pmd)) /* Nothing there */
  1410. return 0;
  1411. if (pmd_thp_or_huge(*pmd)) /* THP, HugeTLB */
  1412. return pmd_young(*pmd);
  1413. pte = pte_offset_kernel(pmd, gpa);
  1414. if (!pte_none(*pte)) /* Just a page... */
  1415. return pte_young(*pte);
  1416. return 0;
  1417. }
  1418. int kvm_age_hva(struct kvm *kvm, unsigned long start, unsigned long end)
  1419. {
  1420. trace_kvm_age_hva(start, end);
  1421. return handle_hva_to_gpa(kvm, start, end, kvm_age_hva_handler, NULL);
  1422. }
  1423. int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
  1424. {
  1425. trace_kvm_test_age_hva(hva);
  1426. return handle_hva_to_gpa(kvm, hva, hva, kvm_test_age_hva_handler, NULL);
  1427. }
  1428. void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu)
  1429. {
  1430. mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
  1431. }
  1432. phys_addr_t kvm_mmu_get_httbr(void)
  1433. {
  1434. if (__kvm_cpu_uses_extended_idmap())
  1435. return virt_to_phys(merged_hyp_pgd);
  1436. else
  1437. return virt_to_phys(hyp_pgd);
  1438. }
  1439. phys_addr_t kvm_get_idmap_vector(void)
  1440. {
  1441. return hyp_idmap_vector;
  1442. }
  1443. phys_addr_t kvm_get_idmap_start(void)
  1444. {
  1445. return hyp_idmap_start;
  1446. }
  1447. static int kvm_map_idmap_text(pgd_t *pgd)
  1448. {
  1449. int err;
  1450. /* Create the idmap in the boot page tables */
  1451. err = __create_hyp_mappings(pgd,
  1452. hyp_idmap_start, hyp_idmap_end,
  1453. __phys_to_pfn(hyp_idmap_start),
  1454. PAGE_HYP_EXEC);
  1455. if (err)
  1456. kvm_err("Failed to idmap %lx-%lx\n",
  1457. hyp_idmap_start, hyp_idmap_end);
  1458. return err;
  1459. }
  1460. int kvm_mmu_init(void)
  1461. {
  1462. int err;
  1463. hyp_idmap_start = kvm_virt_to_phys(__hyp_idmap_text_start);
  1464. hyp_idmap_end = kvm_virt_to_phys(__hyp_idmap_text_end);
  1465. hyp_idmap_vector = kvm_virt_to_phys(__kvm_hyp_init);
  1466. /*
  1467. * We rely on the linker script to ensure at build time that the HYP
  1468. * init code does not cross a page boundary.
  1469. */
  1470. BUG_ON((hyp_idmap_start ^ (hyp_idmap_end - 1)) & PAGE_MASK);
  1471. kvm_info("IDMAP page: %lx\n", hyp_idmap_start);
  1472. kvm_info("HYP VA range: %lx:%lx\n",
  1473. kern_hyp_va(PAGE_OFFSET), kern_hyp_va(~0UL));
  1474. if (hyp_idmap_start >= kern_hyp_va(PAGE_OFFSET) &&
  1475. hyp_idmap_start < kern_hyp_va(~0UL) &&
  1476. hyp_idmap_start != (unsigned long)__hyp_idmap_text_start) {
  1477. /*
  1478. * The idmap page is intersecting with the VA space,
  1479. * it is not safe to continue further.
  1480. */
  1481. kvm_err("IDMAP intersecting with HYP VA, unable to continue\n");
  1482. err = -EINVAL;
  1483. goto out;
  1484. }
  1485. hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, hyp_pgd_order);
  1486. if (!hyp_pgd) {
  1487. kvm_err("Hyp mode PGD not allocated\n");
  1488. err = -ENOMEM;
  1489. goto out;
  1490. }
  1491. if (__kvm_cpu_uses_extended_idmap()) {
  1492. boot_hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  1493. hyp_pgd_order);
  1494. if (!boot_hyp_pgd) {
  1495. kvm_err("Hyp boot PGD not allocated\n");
  1496. err = -ENOMEM;
  1497. goto out;
  1498. }
  1499. err = kvm_map_idmap_text(boot_hyp_pgd);
  1500. if (err)
  1501. goto out;
  1502. merged_hyp_pgd = (pgd_t *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
  1503. if (!merged_hyp_pgd) {
  1504. kvm_err("Failed to allocate extra HYP pgd\n");
  1505. goto out;
  1506. }
  1507. __kvm_extend_hypmap(boot_hyp_pgd, hyp_pgd, merged_hyp_pgd,
  1508. hyp_idmap_start);
  1509. } else {
  1510. err = kvm_map_idmap_text(hyp_pgd);
  1511. if (err)
  1512. goto out;
  1513. }
  1514. return 0;
  1515. out:
  1516. free_hyp_pgds();
  1517. return err;
  1518. }
  1519. void kvm_arch_commit_memory_region(struct kvm *kvm,
  1520. const struct kvm_userspace_memory_region *mem,
  1521. const struct kvm_memory_slot *old,
  1522. const struct kvm_memory_slot *new,
  1523. enum kvm_mr_change change)
  1524. {
  1525. /*
  1526. * At this point memslot has been committed and there is an
  1527. * allocated dirty_bitmap[], dirty pages will be be tracked while the
  1528. * memory slot is write protected.
  1529. */
  1530. if (change != KVM_MR_DELETE && mem->flags & KVM_MEM_LOG_DIRTY_PAGES)
  1531. kvm_mmu_wp_memory_region(kvm, mem->slot);
  1532. }
  1533. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  1534. struct kvm_memory_slot *memslot,
  1535. const struct kvm_userspace_memory_region *mem,
  1536. enum kvm_mr_change change)
  1537. {
  1538. hva_t hva = mem->userspace_addr;
  1539. hva_t reg_end = hva + mem->memory_size;
  1540. bool writable = !(mem->flags & KVM_MEM_READONLY);
  1541. int ret = 0;
  1542. if (change != KVM_MR_CREATE && change != KVM_MR_MOVE &&
  1543. change != KVM_MR_FLAGS_ONLY)
  1544. return 0;
  1545. /*
  1546. * Prevent userspace from creating a memory region outside of the IPA
  1547. * space addressable by the KVM guest IPA space.
  1548. */
  1549. if (memslot->base_gfn + memslot->npages >=
  1550. (KVM_PHYS_SIZE >> PAGE_SHIFT))
  1551. return -EFAULT;
  1552. /*
  1553. * A memory region could potentially cover multiple VMAs, and any holes
  1554. * between them, so iterate over all of them to find out if we can map
  1555. * any of them right now.
  1556. *
  1557. * +--------------------------------------------+
  1558. * +---------------+----------------+ +----------------+
  1559. * | : VMA 1 | VMA 2 | | VMA 3 : |
  1560. * +---------------+----------------+ +----------------+
  1561. * | memory region |
  1562. * +--------------------------------------------+
  1563. */
  1564. do {
  1565. struct vm_area_struct *vma = find_vma(current->mm, hva);
  1566. hva_t vm_start, vm_end;
  1567. if (!vma || vma->vm_start >= reg_end)
  1568. break;
  1569. /*
  1570. * Mapping a read-only VMA is only allowed if the
  1571. * memory region is configured as read-only.
  1572. */
  1573. if (writable && !(vma->vm_flags & VM_WRITE)) {
  1574. ret = -EPERM;
  1575. break;
  1576. }
  1577. /*
  1578. * Take the intersection of this VMA with the memory region
  1579. */
  1580. vm_start = max(hva, vma->vm_start);
  1581. vm_end = min(reg_end, vma->vm_end);
  1582. if (vma->vm_flags & VM_PFNMAP) {
  1583. gpa_t gpa = mem->guest_phys_addr +
  1584. (vm_start - mem->userspace_addr);
  1585. phys_addr_t pa;
  1586. pa = (phys_addr_t)vma->vm_pgoff << PAGE_SHIFT;
  1587. pa += vm_start - vma->vm_start;
  1588. /* IO region dirty page logging not allowed */
  1589. if (memslot->flags & KVM_MEM_LOG_DIRTY_PAGES)
  1590. return -EINVAL;
  1591. ret = kvm_phys_addr_ioremap(kvm, gpa, pa,
  1592. vm_end - vm_start,
  1593. writable);
  1594. if (ret)
  1595. break;
  1596. }
  1597. hva = vm_end;
  1598. } while (hva < reg_end);
  1599. if (change == KVM_MR_FLAGS_ONLY)
  1600. return ret;
  1601. spin_lock(&kvm->mmu_lock);
  1602. if (ret)
  1603. unmap_stage2_range(kvm, mem->guest_phys_addr, mem->memory_size);
  1604. else
  1605. stage2_flush_memslot(kvm, memslot);
  1606. spin_unlock(&kvm->mmu_lock);
  1607. return ret;
  1608. }
  1609. void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
  1610. struct kvm_memory_slot *dont)
  1611. {
  1612. }
  1613. int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
  1614. unsigned long npages)
  1615. {
  1616. /*
  1617. * Readonly memslots are not incoherent with the caches by definition,
  1618. * but in practice, they are used mostly to emulate ROMs or NOR flashes
  1619. * that the guest may consider devices and hence map as uncached.
  1620. * To prevent incoherency issues in these cases, tag all readonly
  1621. * regions as incoherent.
  1622. */
  1623. if (slot->flags & KVM_MEM_READONLY)
  1624. slot->flags |= KVM_MEMSLOT_INCOHERENT;
  1625. return 0;
  1626. }
  1627. void kvm_arch_memslots_updated(struct kvm *kvm, struct kvm_memslots *slots)
  1628. {
  1629. }
  1630. void kvm_arch_flush_shadow_all(struct kvm *kvm)
  1631. {
  1632. kvm_free_stage2_pgd(kvm);
  1633. }
  1634. void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
  1635. struct kvm_memory_slot *slot)
  1636. {
  1637. gpa_t gpa = slot->base_gfn << PAGE_SHIFT;
  1638. phys_addr_t size = slot->npages << PAGE_SHIFT;
  1639. spin_lock(&kvm->mmu_lock);
  1640. unmap_stage2_range(kvm, gpa, size);
  1641. spin_unlock(&kvm->mmu_lock);
  1642. }
  1643. /*
  1644. * See note at ARMv7 ARM B1.14.4 (TL;DR: S/W ops are not easily virtualized).
  1645. *
  1646. * Main problems:
  1647. * - S/W ops are local to a CPU (not broadcast)
  1648. * - We have line migration behind our back (speculation)
  1649. * - System caches don't support S/W at all (damn!)
  1650. *
  1651. * In the face of the above, the best we can do is to try and convert
  1652. * S/W ops to VA ops. Because the guest is not allowed to infer the
  1653. * S/W to PA mapping, it can only use S/W to nuke the whole cache,
  1654. * which is a rather good thing for us.
  1655. *
  1656. * Also, it is only used when turning caches on/off ("The expected
  1657. * usage of the cache maintenance instructions that operate by set/way
  1658. * is associated with the cache maintenance instructions associated
  1659. * with the powerdown and powerup of caches, if this is required by
  1660. * the implementation.").
  1661. *
  1662. * We use the following policy:
  1663. *
  1664. * - If we trap a S/W operation, we enable VM trapping to detect
  1665. * caches being turned on/off, and do a full clean.
  1666. *
  1667. * - We flush the caches on both caches being turned on and off.
  1668. *
  1669. * - Once the caches are enabled, we stop trapping VM ops.
  1670. */
  1671. void kvm_set_way_flush(struct kvm_vcpu *vcpu)
  1672. {
  1673. unsigned long hcr = vcpu_get_hcr(vcpu);
  1674. /*
  1675. * If this is the first time we do a S/W operation
  1676. * (i.e. HCR_TVM not set) flush the whole memory, and set the
  1677. * VM trapping.
  1678. *
  1679. * Otherwise, rely on the VM trapping to wait for the MMU +
  1680. * Caches to be turned off. At that point, we'll be able to
  1681. * clean the caches again.
  1682. */
  1683. if (!(hcr & HCR_TVM)) {
  1684. trace_kvm_set_way_flush(*vcpu_pc(vcpu),
  1685. vcpu_has_cache_enabled(vcpu));
  1686. stage2_flush_vm(vcpu->kvm);
  1687. vcpu_set_hcr(vcpu, hcr | HCR_TVM);
  1688. }
  1689. }
  1690. void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled)
  1691. {
  1692. bool now_enabled = vcpu_has_cache_enabled(vcpu);
  1693. /*
  1694. * If switching the MMU+caches on, need to invalidate the caches.
  1695. * If switching it off, need to clean the caches.
  1696. * Clean + invalidate does the trick always.
  1697. */
  1698. if (now_enabled != was_enabled)
  1699. stage2_flush_vm(vcpu->kvm);
  1700. /* Caches are now on, stop trapping VM ops (until a S/W op) */
  1701. if (now_enabled)
  1702. vcpu_set_hcr(vcpu, vcpu_get_hcr(vcpu) & ~HCR_TVM);
  1703. trace_kvm_toggle_cache(*vcpu_pc(vcpu), was_enabled, now_enabled);
  1704. }