mmu.c 52 KB

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