mmu.c 57 KB

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