mmu.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242
  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. if (PageTransCompoundMap(pfn_to_page(pfn))) {
  1051. unsigned long mask;
  1052. /*
  1053. * The address we faulted on is backed by a transparent huge
  1054. * page. However, because we map the compound huge page and
  1055. * not the individual tail page, we need to transfer the
  1056. * refcount to the head page. We have to be careful that the
  1057. * THP doesn't start to split while we are adjusting the
  1058. * refcounts.
  1059. *
  1060. * We are sure this doesn't happen, because mmu_notifier_retry
  1061. * was successful and we are holding the mmu_lock, so if this
  1062. * THP is trying to split, it will be blocked in the mmu
  1063. * notifier before touching any of the pages, specifically
  1064. * before being able to call __split_huge_page_refcount().
  1065. *
  1066. * We can therefore safely transfer the refcount from PG_tail
  1067. * to PG_head and switch the pfn from a tail page to the head
  1068. * page accordingly.
  1069. */
  1070. mask = PTRS_PER_PMD - 1;
  1071. VM_BUG_ON((gfn & mask) != (pfn & mask));
  1072. if (pfn & mask) {
  1073. *ipap &= PMD_MASK;
  1074. kvm_release_pfn_clean(pfn);
  1075. pfn &= ~mask;
  1076. kvm_get_pfn(pfn);
  1077. *pfnp = pfn;
  1078. }
  1079. return true;
  1080. }
  1081. return false;
  1082. }
  1083. static bool kvm_is_write_fault(struct kvm_vcpu *vcpu)
  1084. {
  1085. if (kvm_vcpu_trap_is_iabt(vcpu))
  1086. return false;
  1087. return kvm_vcpu_dabt_iswrite(vcpu);
  1088. }
  1089. /**
  1090. * stage2_wp_ptes - write protect PMD range
  1091. * @pmd: pointer to pmd entry
  1092. * @addr: range start address
  1093. * @end: range end address
  1094. */
  1095. static void stage2_wp_ptes(pmd_t *pmd, phys_addr_t addr, phys_addr_t end)
  1096. {
  1097. pte_t *pte;
  1098. pte = pte_offset_kernel(pmd, addr);
  1099. do {
  1100. if (!pte_none(*pte)) {
  1101. if (!kvm_s2pte_readonly(pte))
  1102. kvm_set_s2pte_readonly(pte);
  1103. }
  1104. } while (pte++, addr += PAGE_SIZE, addr != end);
  1105. }
  1106. /**
  1107. * stage2_wp_pmds - write protect PUD range
  1108. * @pud: pointer to pud entry
  1109. * @addr: range start address
  1110. * @end: range end address
  1111. */
  1112. static void stage2_wp_pmds(pud_t *pud, phys_addr_t addr, phys_addr_t end)
  1113. {
  1114. pmd_t *pmd;
  1115. phys_addr_t next;
  1116. pmd = stage2_pmd_offset(pud, addr);
  1117. do {
  1118. next = stage2_pmd_addr_end(addr, end);
  1119. if (!pmd_none(*pmd)) {
  1120. if (pmd_thp_or_huge(*pmd)) {
  1121. if (!kvm_s2pmd_readonly(pmd))
  1122. kvm_set_s2pmd_readonly(pmd);
  1123. } else {
  1124. stage2_wp_ptes(pmd, addr, next);
  1125. }
  1126. }
  1127. } while (pmd++, addr = next, addr != end);
  1128. }
  1129. /**
  1130. * stage2_wp_puds - write protect PGD range
  1131. * @pgd: pointer to pgd entry
  1132. * @addr: range start address
  1133. * @end: range end address
  1134. *
  1135. * Process PUD entries, for a huge PUD we cause a panic.
  1136. */
  1137. static void stage2_wp_puds(pgd_t *pgd, phys_addr_t addr, phys_addr_t end)
  1138. {
  1139. pud_t *pud;
  1140. phys_addr_t next;
  1141. pud = stage2_pud_offset(pgd, addr);
  1142. do {
  1143. next = stage2_pud_addr_end(addr, end);
  1144. if (!stage2_pud_none(*pud)) {
  1145. /* TODO:PUD not supported, revisit later if supported */
  1146. BUG_ON(stage2_pud_huge(*pud));
  1147. stage2_wp_pmds(pud, addr, next);
  1148. }
  1149. } while (pud++, addr = next, addr != end);
  1150. }
  1151. /**
  1152. * stage2_wp_range() - write protect stage2 memory region range
  1153. * @kvm: The KVM pointer
  1154. * @addr: Start address of range
  1155. * @end: End address of range
  1156. */
  1157. static void stage2_wp_range(struct kvm *kvm, phys_addr_t addr, phys_addr_t end)
  1158. {
  1159. pgd_t *pgd;
  1160. phys_addr_t next;
  1161. pgd = kvm->arch.pgd + stage2_pgd_index(addr);
  1162. do {
  1163. /*
  1164. * Release kvm_mmu_lock periodically if the memory region is
  1165. * large. Otherwise, we may see kernel panics with
  1166. * CONFIG_DETECT_HUNG_TASK, CONFIG_LOCKUP_DETECTOR,
  1167. * CONFIG_LOCKDEP. Additionally, holding the lock too long
  1168. * will also starve other vCPUs. We have to also make sure
  1169. * that the page tables are not freed while we released
  1170. * the lock.
  1171. */
  1172. cond_resched_lock(&kvm->mmu_lock);
  1173. if (!READ_ONCE(kvm->arch.pgd))
  1174. break;
  1175. next = stage2_pgd_addr_end(addr, end);
  1176. if (stage2_pgd_present(*pgd))
  1177. stage2_wp_puds(pgd, addr, next);
  1178. } while (pgd++, addr = next, addr != end);
  1179. }
  1180. /**
  1181. * kvm_mmu_wp_memory_region() - write protect stage 2 entries for memory slot
  1182. * @kvm: The KVM pointer
  1183. * @slot: The memory slot to write protect
  1184. *
  1185. * Called to start logging dirty pages after memory region
  1186. * KVM_MEM_LOG_DIRTY_PAGES operation is called. After this function returns
  1187. * all present PMD and PTEs are write protected in the memory region.
  1188. * Afterwards read of dirty page log can be called.
  1189. *
  1190. * Acquires kvm_mmu_lock. Called with kvm->slots_lock mutex acquired,
  1191. * serializing operations for VM memory regions.
  1192. */
  1193. void kvm_mmu_wp_memory_region(struct kvm *kvm, int slot)
  1194. {
  1195. struct kvm_memslots *slots = kvm_memslots(kvm);
  1196. struct kvm_memory_slot *memslot = id_to_memslot(slots, slot);
  1197. phys_addr_t start = memslot->base_gfn << PAGE_SHIFT;
  1198. phys_addr_t end = (memslot->base_gfn + memslot->npages) << PAGE_SHIFT;
  1199. spin_lock(&kvm->mmu_lock);
  1200. stage2_wp_range(kvm, start, end);
  1201. spin_unlock(&kvm->mmu_lock);
  1202. kvm_flush_remote_tlbs(kvm);
  1203. }
  1204. /**
  1205. * kvm_mmu_write_protect_pt_masked() - write protect dirty pages
  1206. * @kvm: The KVM pointer
  1207. * @slot: The memory slot associated with mask
  1208. * @gfn_offset: The gfn offset in memory slot
  1209. * @mask: The mask of dirty pages at offset 'gfn_offset' in this memory
  1210. * slot to be write protected
  1211. *
  1212. * Walks bits set in mask write protects the associated pte's. Caller must
  1213. * acquire kvm_mmu_lock.
  1214. */
  1215. static void kvm_mmu_write_protect_pt_masked(struct kvm *kvm,
  1216. struct kvm_memory_slot *slot,
  1217. gfn_t gfn_offset, unsigned long mask)
  1218. {
  1219. phys_addr_t base_gfn = slot->base_gfn + gfn_offset;
  1220. phys_addr_t start = (base_gfn + __ffs(mask)) << PAGE_SHIFT;
  1221. phys_addr_t end = (base_gfn + __fls(mask) + 1) << PAGE_SHIFT;
  1222. stage2_wp_range(kvm, start, end);
  1223. }
  1224. /*
  1225. * kvm_arch_mmu_enable_log_dirty_pt_masked - enable dirty logging for selected
  1226. * dirty pages.
  1227. *
  1228. * It calls kvm_mmu_write_protect_pt_masked to write protect selected pages to
  1229. * enable dirty logging for them.
  1230. */
  1231. void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm,
  1232. struct kvm_memory_slot *slot,
  1233. gfn_t gfn_offset, unsigned long mask)
  1234. {
  1235. kvm_mmu_write_protect_pt_masked(kvm, slot, gfn_offset, mask);
  1236. }
  1237. static void clean_dcache_guest_page(kvm_pfn_t pfn, unsigned long size)
  1238. {
  1239. __clean_dcache_guest_page(pfn, size);
  1240. }
  1241. static void invalidate_icache_guest_page(kvm_pfn_t pfn, unsigned long size)
  1242. {
  1243. __invalidate_icache_guest_page(pfn, size);
  1244. }
  1245. static void kvm_send_hwpoison_signal(unsigned long address,
  1246. struct vm_area_struct *vma)
  1247. {
  1248. siginfo_t info;
  1249. clear_siginfo(&info);
  1250. info.si_signo = SIGBUS;
  1251. info.si_errno = 0;
  1252. info.si_code = BUS_MCEERR_AR;
  1253. info.si_addr = (void __user *)address;
  1254. if (is_vm_hugetlb_page(vma))
  1255. info.si_addr_lsb = huge_page_shift(hstate_vma(vma));
  1256. else
  1257. info.si_addr_lsb = PAGE_SHIFT;
  1258. send_sig_info(SIGBUS, &info, current);
  1259. }
  1260. static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
  1261. struct kvm_memory_slot *memslot, unsigned long hva,
  1262. unsigned long fault_status)
  1263. {
  1264. int ret;
  1265. bool write_fault, exec_fault, writable, hugetlb = false, force_pte = false;
  1266. unsigned long mmu_seq;
  1267. gfn_t gfn = fault_ipa >> PAGE_SHIFT;
  1268. struct kvm *kvm = vcpu->kvm;
  1269. struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
  1270. struct vm_area_struct *vma;
  1271. kvm_pfn_t pfn;
  1272. pgprot_t mem_type = PAGE_S2;
  1273. bool logging_active = memslot_is_logging(memslot);
  1274. unsigned long flags = 0;
  1275. write_fault = kvm_is_write_fault(vcpu);
  1276. exec_fault = kvm_vcpu_trap_is_iabt(vcpu);
  1277. VM_BUG_ON(write_fault && exec_fault);
  1278. if (fault_status == FSC_PERM && !write_fault && !exec_fault) {
  1279. kvm_err("Unexpected L2 read permission error\n");
  1280. return -EFAULT;
  1281. }
  1282. /* Let's check if we will get back a huge page backed by hugetlbfs */
  1283. down_read(&current->mm->mmap_sem);
  1284. vma = find_vma_intersection(current->mm, hva, hva + 1);
  1285. if (unlikely(!vma)) {
  1286. kvm_err("Failed to find VMA for hva 0x%lx\n", hva);
  1287. up_read(&current->mm->mmap_sem);
  1288. return -EFAULT;
  1289. }
  1290. if (vma_kernel_pagesize(vma) == PMD_SIZE && !logging_active) {
  1291. hugetlb = true;
  1292. gfn = (fault_ipa & PMD_MASK) >> PAGE_SHIFT;
  1293. } else {
  1294. /*
  1295. * Pages belonging to memslots that don't have the same
  1296. * alignment for userspace and IPA cannot be mapped using
  1297. * block descriptors even if the pages belong to a THP for
  1298. * the process, because the stage-2 block descriptor will
  1299. * cover more than a single THP and we loose atomicity for
  1300. * unmapping, updates, and splits of the THP or other pages
  1301. * in the stage-2 block range.
  1302. */
  1303. if ((memslot->userspace_addr & ~PMD_MASK) !=
  1304. ((memslot->base_gfn << PAGE_SHIFT) & ~PMD_MASK))
  1305. force_pte = true;
  1306. }
  1307. up_read(&current->mm->mmap_sem);
  1308. /* We need minimum second+third level pages */
  1309. ret = mmu_topup_memory_cache(memcache, KVM_MMU_CACHE_MIN_PAGES,
  1310. KVM_NR_MEM_OBJS);
  1311. if (ret)
  1312. return ret;
  1313. mmu_seq = vcpu->kvm->mmu_notifier_seq;
  1314. /*
  1315. * Ensure the read of mmu_notifier_seq happens before we call
  1316. * gfn_to_pfn_prot (which calls get_user_pages), so that we don't risk
  1317. * the page we just got a reference to gets unmapped before we have a
  1318. * chance to grab the mmu_lock, which ensure that if the page gets
  1319. * unmapped afterwards, the call to kvm_unmap_hva will take it away
  1320. * from us again properly. This smp_rmb() interacts with the smp_wmb()
  1321. * in kvm_mmu_notifier_invalidate_<page|range_end>.
  1322. */
  1323. smp_rmb();
  1324. pfn = gfn_to_pfn_prot(kvm, gfn, write_fault, &writable);
  1325. if (pfn == KVM_PFN_ERR_HWPOISON) {
  1326. kvm_send_hwpoison_signal(hva, vma);
  1327. return 0;
  1328. }
  1329. if (is_error_noslot_pfn(pfn))
  1330. return -EFAULT;
  1331. if (kvm_is_device_pfn(pfn)) {
  1332. mem_type = PAGE_S2_DEVICE;
  1333. flags |= KVM_S2PTE_FLAG_IS_IOMAP;
  1334. } else if (logging_active) {
  1335. /*
  1336. * Faults on pages in a memslot with logging enabled
  1337. * should not be mapped with huge pages (it introduces churn
  1338. * and performance degradation), so force a pte mapping.
  1339. */
  1340. force_pte = true;
  1341. flags |= KVM_S2_FLAG_LOGGING_ACTIVE;
  1342. /*
  1343. * Only actually map the page as writable if this was a write
  1344. * fault.
  1345. */
  1346. if (!write_fault)
  1347. writable = false;
  1348. }
  1349. spin_lock(&kvm->mmu_lock);
  1350. if (mmu_notifier_retry(kvm, mmu_seq))
  1351. goto out_unlock;
  1352. if (!hugetlb && !force_pte)
  1353. hugetlb = transparent_hugepage_adjust(&pfn, &fault_ipa);
  1354. if (hugetlb) {
  1355. pmd_t new_pmd = pfn_pmd(pfn, mem_type);
  1356. new_pmd = pmd_mkhuge(new_pmd);
  1357. if (writable) {
  1358. new_pmd = kvm_s2pmd_mkwrite(new_pmd);
  1359. kvm_set_pfn_dirty(pfn);
  1360. }
  1361. if (fault_status != FSC_PERM)
  1362. clean_dcache_guest_page(pfn, PMD_SIZE);
  1363. if (exec_fault) {
  1364. new_pmd = kvm_s2pmd_mkexec(new_pmd);
  1365. invalidate_icache_guest_page(pfn, PMD_SIZE);
  1366. } else if (fault_status == FSC_PERM) {
  1367. /* Preserve execute if XN was already cleared */
  1368. if (stage2_is_exec(kvm, fault_ipa))
  1369. new_pmd = kvm_s2pmd_mkexec(new_pmd);
  1370. }
  1371. ret = stage2_set_pmd_huge(kvm, memcache, fault_ipa, &new_pmd);
  1372. } else {
  1373. pte_t new_pte = pfn_pte(pfn, mem_type);
  1374. if (writable) {
  1375. new_pte = kvm_s2pte_mkwrite(new_pte);
  1376. kvm_set_pfn_dirty(pfn);
  1377. mark_page_dirty(kvm, gfn);
  1378. }
  1379. if (fault_status != FSC_PERM)
  1380. clean_dcache_guest_page(pfn, PAGE_SIZE);
  1381. if (exec_fault) {
  1382. new_pte = kvm_s2pte_mkexec(new_pte);
  1383. invalidate_icache_guest_page(pfn, PAGE_SIZE);
  1384. } else if (fault_status == FSC_PERM) {
  1385. /* Preserve execute if XN was already cleared */
  1386. if (stage2_is_exec(kvm, fault_ipa))
  1387. new_pte = kvm_s2pte_mkexec(new_pte);
  1388. }
  1389. ret = stage2_set_pte(kvm, memcache, fault_ipa, &new_pte, flags);
  1390. }
  1391. out_unlock:
  1392. spin_unlock(&kvm->mmu_lock);
  1393. kvm_set_pfn_accessed(pfn);
  1394. kvm_release_pfn_clean(pfn);
  1395. return ret;
  1396. }
  1397. /*
  1398. * Resolve the access fault by making the page young again.
  1399. * Note that because the faulting entry is guaranteed not to be
  1400. * cached in the TLB, we don't need to invalidate anything.
  1401. * Only the HW Access Flag updates are supported for Stage 2 (no DBM),
  1402. * so there is no need for atomic (pte|pmd)_mkyoung operations.
  1403. */
  1404. static void handle_access_fault(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa)
  1405. {
  1406. pmd_t *pmd;
  1407. pte_t *pte;
  1408. kvm_pfn_t pfn;
  1409. bool pfn_valid = false;
  1410. trace_kvm_access_fault(fault_ipa);
  1411. spin_lock(&vcpu->kvm->mmu_lock);
  1412. pmd = stage2_get_pmd(vcpu->kvm, NULL, fault_ipa);
  1413. if (!pmd || pmd_none(*pmd)) /* Nothing there */
  1414. goto out;
  1415. if (pmd_thp_or_huge(*pmd)) { /* THP, HugeTLB */
  1416. *pmd = pmd_mkyoung(*pmd);
  1417. pfn = pmd_pfn(*pmd);
  1418. pfn_valid = true;
  1419. goto out;
  1420. }
  1421. pte = pte_offset_kernel(pmd, fault_ipa);
  1422. if (pte_none(*pte)) /* Nothing there either */
  1423. goto out;
  1424. *pte = pte_mkyoung(*pte); /* Just a page... */
  1425. pfn = pte_pfn(*pte);
  1426. pfn_valid = true;
  1427. out:
  1428. spin_unlock(&vcpu->kvm->mmu_lock);
  1429. if (pfn_valid)
  1430. kvm_set_pfn_accessed(pfn);
  1431. }
  1432. /**
  1433. * kvm_handle_guest_abort - handles all 2nd stage aborts
  1434. * @vcpu: the VCPU pointer
  1435. * @run: the kvm_run structure
  1436. *
  1437. * Any abort that gets to the host is almost guaranteed to be caused by a
  1438. * missing second stage translation table entry, which can mean that either the
  1439. * guest simply needs more memory and we must allocate an appropriate page or it
  1440. * can mean that the guest tried to access I/O memory, which is emulated by user
  1441. * space. The distinction is based on the IPA causing the fault and whether this
  1442. * memory region has been registered as standard RAM by user space.
  1443. */
  1444. int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run)
  1445. {
  1446. unsigned long fault_status;
  1447. phys_addr_t fault_ipa;
  1448. struct kvm_memory_slot *memslot;
  1449. unsigned long hva;
  1450. bool is_iabt, write_fault, writable;
  1451. gfn_t gfn;
  1452. int ret, idx;
  1453. fault_status = kvm_vcpu_trap_get_fault_type(vcpu);
  1454. fault_ipa = kvm_vcpu_get_fault_ipa(vcpu);
  1455. is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
  1456. /* Synchronous External Abort? */
  1457. if (kvm_vcpu_dabt_isextabt(vcpu)) {
  1458. /*
  1459. * For RAS the host kernel may handle this abort.
  1460. * There is no need to pass the error into the guest.
  1461. */
  1462. if (!handle_guest_sea(fault_ipa, kvm_vcpu_get_hsr(vcpu)))
  1463. return 1;
  1464. if (unlikely(!is_iabt)) {
  1465. kvm_inject_vabt(vcpu);
  1466. return 1;
  1467. }
  1468. }
  1469. trace_kvm_guest_fault(*vcpu_pc(vcpu), kvm_vcpu_get_hsr(vcpu),
  1470. kvm_vcpu_get_hfar(vcpu), fault_ipa);
  1471. /* Check the stage-2 fault is trans. fault or write fault */
  1472. if (fault_status != FSC_FAULT && fault_status != FSC_PERM &&
  1473. fault_status != FSC_ACCESS) {
  1474. kvm_err("Unsupported FSC: EC=%#x xFSC=%#lx ESR_EL2=%#lx\n",
  1475. kvm_vcpu_trap_get_class(vcpu),
  1476. (unsigned long)kvm_vcpu_trap_get_fault(vcpu),
  1477. (unsigned long)kvm_vcpu_get_hsr(vcpu));
  1478. return -EFAULT;
  1479. }
  1480. idx = srcu_read_lock(&vcpu->kvm->srcu);
  1481. gfn = fault_ipa >> PAGE_SHIFT;
  1482. memslot = gfn_to_memslot(vcpu->kvm, gfn);
  1483. hva = gfn_to_hva_memslot_prot(memslot, gfn, &writable);
  1484. write_fault = kvm_is_write_fault(vcpu);
  1485. if (kvm_is_error_hva(hva) || (write_fault && !writable)) {
  1486. if (is_iabt) {
  1487. /* Prefetch Abort on I/O address */
  1488. kvm_inject_pabt(vcpu, kvm_vcpu_get_hfar(vcpu));
  1489. ret = 1;
  1490. goto out_unlock;
  1491. }
  1492. /*
  1493. * Check for a cache maintenance operation. Since we
  1494. * ended-up here, we know it is outside of any memory
  1495. * slot. But we can't find out if that is for a device,
  1496. * or if the guest is just being stupid. The only thing
  1497. * we know for sure is that this range cannot be cached.
  1498. *
  1499. * So let's assume that the guest is just being
  1500. * cautious, and skip the instruction.
  1501. */
  1502. if (kvm_vcpu_dabt_is_cm(vcpu)) {
  1503. kvm_skip_instr(vcpu, kvm_vcpu_trap_il_is32bit(vcpu));
  1504. ret = 1;
  1505. goto out_unlock;
  1506. }
  1507. /*
  1508. * The IPA is reported as [MAX:12], so we need to
  1509. * complement it with the bottom 12 bits from the
  1510. * faulting VA. This is always 12 bits, irrespective
  1511. * of the page size.
  1512. */
  1513. fault_ipa |= kvm_vcpu_get_hfar(vcpu) & ((1 << 12) - 1);
  1514. ret = io_mem_abort(vcpu, run, fault_ipa);
  1515. goto out_unlock;
  1516. }
  1517. /* Userspace should not be able to register out-of-bounds IPAs */
  1518. VM_BUG_ON(fault_ipa >= KVM_PHYS_SIZE);
  1519. if (fault_status == FSC_ACCESS) {
  1520. handle_access_fault(vcpu, fault_ipa);
  1521. ret = 1;
  1522. goto out_unlock;
  1523. }
  1524. ret = user_mem_abort(vcpu, fault_ipa, memslot, hva, fault_status);
  1525. if (ret == 0)
  1526. ret = 1;
  1527. out_unlock:
  1528. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  1529. return ret;
  1530. }
  1531. static int handle_hva_to_gpa(struct kvm *kvm,
  1532. unsigned long start,
  1533. unsigned long end,
  1534. int (*handler)(struct kvm *kvm,
  1535. gpa_t gpa, u64 size,
  1536. void *data),
  1537. void *data)
  1538. {
  1539. struct kvm_memslots *slots;
  1540. struct kvm_memory_slot *memslot;
  1541. int ret = 0;
  1542. slots = kvm_memslots(kvm);
  1543. /* we only care about the pages that the guest sees */
  1544. kvm_for_each_memslot(memslot, slots) {
  1545. unsigned long hva_start, hva_end;
  1546. gfn_t gpa;
  1547. hva_start = max(start, memslot->userspace_addr);
  1548. hva_end = min(end, memslot->userspace_addr +
  1549. (memslot->npages << PAGE_SHIFT));
  1550. if (hva_start >= hva_end)
  1551. continue;
  1552. gpa = hva_to_gfn_memslot(hva_start, memslot) << PAGE_SHIFT;
  1553. ret |= handler(kvm, gpa, (u64)(hva_end - hva_start), data);
  1554. }
  1555. return ret;
  1556. }
  1557. static int kvm_unmap_hva_handler(struct kvm *kvm, gpa_t gpa, u64 size, void *data)
  1558. {
  1559. unmap_stage2_range(kvm, gpa, size);
  1560. return 0;
  1561. }
  1562. int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
  1563. {
  1564. unsigned long end = hva + PAGE_SIZE;
  1565. if (!kvm->arch.pgd)
  1566. return 0;
  1567. trace_kvm_unmap_hva(hva);
  1568. handle_hva_to_gpa(kvm, hva, end, &kvm_unmap_hva_handler, NULL);
  1569. return 0;
  1570. }
  1571. int kvm_unmap_hva_range(struct kvm *kvm,
  1572. unsigned long start, unsigned long end)
  1573. {
  1574. if (!kvm->arch.pgd)
  1575. return 0;
  1576. trace_kvm_unmap_hva_range(start, end);
  1577. handle_hva_to_gpa(kvm, start, end, &kvm_unmap_hva_handler, NULL);
  1578. return 0;
  1579. }
  1580. static int kvm_set_spte_handler(struct kvm *kvm, gpa_t gpa, u64 size, void *data)
  1581. {
  1582. pte_t *pte = (pte_t *)data;
  1583. WARN_ON(size != PAGE_SIZE);
  1584. /*
  1585. * We can always call stage2_set_pte with KVM_S2PTE_FLAG_LOGGING_ACTIVE
  1586. * flag clear because MMU notifiers will have unmapped a huge PMD before
  1587. * calling ->change_pte() (which in turn calls kvm_set_spte_hva()) and
  1588. * therefore stage2_set_pte() never needs to clear out a huge PMD
  1589. * through this calling path.
  1590. */
  1591. stage2_set_pte(kvm, NULL, gpa, pte, 0);
  1592. return 0;
  1593. }
  1594. void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
  1595. {
  1596. unsigned long end = hva + PAGE_SIZE;
  1597. pte_t stage2_pte;
  1598. if (!kvm->arch.pgd)
  1599. return;
  1600. trace_kvm_set_spte_hva(hva);
  1601. stage2_pte = pfn_pte(pte_pfn(pte), PAGE_S2);
  1602. handle_hva_to_gpa(kvm, hva, end, &kvm_set_spte_handler, &stage2_pte);
  1603. }
  1604. static int kvm_age_hva_handler(struct kvm *kvm, gpa_t gpa, u64 size, void *data)
  1605. {
  1606. pmd_t *pmd;
  1607. pte_t *pte;
  1608. WARN_ON(size != PAGE_SIZE && size != PMD_SIZE);
  1609. pmd = stage2_get_pmd(kvm, NULL, gpa);
  1610. if (!pmd || pmd_none(*pmd)) /* Nothing there */
  1611. return 0;
  1612. if (pmd_thp_or_huge(*pmd)) /* THP, HugeTLB */
  1613. return stage2_pmdp_test_and_clear_young(pmd);
  1614. pte = pte_offset_kernel(pmd, gpa);
  1615. if (pte_none(*pte))
  1616. return 0;
  1617. return stage2_ptep_test_and_clear_young(pte);
  1618. }
  1619. static int kvm_test_age_hva_handler(struct kvm *kvm, gpa_t gpa, u64 size, void *data)
  1620. {
  1621. pmd_t *pmd;
  1622. pte_t *pte;
  1623. WARN_ON(size != PAGE_SIZE && size != PMD_SIZE);
  1624. pmd = stage2_get_pmd(kvm, NULL, gpa);
  1625. if (!pmd || pmd_none(*pmd)) /* Nothing there */
  1626. return 0;
  1627. if (pmd_thp_or_huge(*pmd)) /* THP, HugeTLB */
  1628. return pmd_young(*pmd);
  1629. pte = pte_offset_kernel(pmd, gpa);
  1630. if (!pte_none(*pte)) /* Just a page... */
  1631. return pte_young(*pte);
  1632. return 0;
  1633. }
  1634. int kvm_age_hva(struct kvm *kvm, unsigned long start, unsigned long end)
  1635. {
  1636. if (!kvm->arch.pgd)
  1637. return 0;
  1638. trace_kvm_age_hva(start, end);
  1639. return handle_hva_to_gpa(kvm, start, end, kvm_age_hva_handler, NULL);
  1640. }
  1641. int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
  1642. {
  1643. if (!kvm->arch.pgd)
  1644. return 0;
  1645. trace_kvm_test_age_hva(hva);
  1646. return handle_hva_to_gpa(kvm, hva, hva, kvm_test_age_hva_handler, NULL);
  1647. }
  1648. void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu)
  1649. {
  1650. mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
  1651. }
  1652. phys_addr_t kvm_mmu_get_httbr(void)
  1653. {
  1654. if (__kvm_cpu_uses_extended_idmap())
  1655. return virt_to_phys(merged_hyp_pgd);
  1656. else
  1657. return virt_to_phys(hyp_pgd);
  1658. }
  1659. phys_addr_t kvm_get_idmap_vector(void)
  1660. {
  1661. return hyp_idmap_vector;
  1662. }
  1663. static int kvm_map_idmap_text(pgd_t *pgd)
  1664. {
  1665. int err;
  1666. /* Create the idmap in the boot page tables */
  1667. err = __create_hyp_mappings(pgd, __kvm_idmap_ptrs_per_pgd(),
  1668. hyp_idmap_start, hyp_idmap_end,
  1669. __phys_to_pfn(hyp_idmap_start),
  1670. PAGE_HYP_EXEC);
  1671. if (err)
  1672. kvm_err("Failed to idmap %lx-%lx\n",
  1673. hyp_idmap_start, hyp_idmap_end);
  1674. return err;
  1675. }
  1676. int kvm_mmu_init(void)
  1677. {
  1678. int err;
  1679. hyp_idmap_start = kvm_virt_to_phys(__hyp_idmap_text_start);
  1680. hyp_idmap_start = ALIGN_DOWN(hyp_idmap_start, PAGE_SIZE);
  1681. hyp_idmap_end = kvm_virt_to_phys(__hyp_idmap_text_end);
  1682. hyp_idmap_end = ALIGN(hyp_idmap_end, PAGE_SIZE);
  1683. hyp_idmap_vector = kvm_virt_to_phys(__kvm_hyp_init);
  1684. /*
  1685. * We rely on the linker script to ensure at build time that the HYP
  1686. * init code does not cross a page boundary.
  1687. */
  1688. BUG_ON((hyp_idmap_start ^ (hyp_idmap_end - 1)) & PAGE_MASK);
  1689. kvm_debug("IDMAP page: %lx\n", hyp_idmap_start);
  1690. kvm_debug("HYP VA range: %lx:%lx\n",
  1691. kern_hyp_va(PAGE_OFFSET),
  1692. kern_hyp_va((unsigned long)high_memory - 1));
  1693. if (hyp_idmap_start >= kern_hyp_va(PAGE_OFFSET) &&
  1694. hyp_idmap_start < kern_hyp_va((unsigned long)high_memory - 1) &&
  1695. hyp_idmap_start != (unsigned long)__hyp_idmap_text_start) {
  1696. /*
  1697. * The idmap page is intersecting with the VA space,
  1698. * it is not safe to continue further.
  1699. */
  1700. kvm_err("IDMAP intersecting with HYP VA, unable to continue\n");
  1701. err = -EINVAL;
  1702. goto out;
  1703. }
  1704. hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, hyp_pgd_order);
  1705. if (!hyp_pgd) {
  1706. kvm_err("Hyp mode PGD not allocated\n");
  1707. err = -ENOMEM;
  1708. goto out;
  1709. }
  1710. if (__kvm_cpu_uses_extended_idmap()) {
  1711. boot_hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  1712. hyp_pgd_order);
  1713. if (!boot_hyp_pgd) {
  1714. kvm_err("Hyp boot PGD not allocated\n");
  1715. err = -ENOMEM;
  1716. goto out;
  1717. }
  1718. err = kvm_map_idmap_text(boot_hyp_pgd);
  1719. if (err)
  1720. goto out;
  1721. merged_hyp_pgd = (pgd_t *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
  1722. if (!merged_hyp_pgd) {
  1723. kvm_err("Failed to allocate extra HYP pgd\n");
  1724. goto out;
  1725. }
  1726. __kvm_extend_hypmap(boot_hyp_pgd, hyp_pgd, merged_hyp_pgd,
  1727. hyp_idmap_start);
  1728. } else {
  1729. err = kvm_map_idmap_text(hyp_pgd);
  1730. if (err)
  1731. goto out;
  1732. }
  1733. io_map_base = hyp_idmap_start;
  1734. return 0;
  1735. out:
  1736. free_hyp_pgds();
  1737. return err;
  1738. }
  1739. void kvm_arch_commit_memory_region(struct kvm *kvm,
  1740. const struct kvm_userspace_memory_region *mem,
  1741. const struct kvm_memory_slot *old,
  1742. const struct kvm_memory_slot *new,
  1743. enum kvm_mr_change change)
  1744. {
  1745. /*
  1746. * At this point memslot has been committed and there is an
  1747. * allocated dirty_bitmap[], dirty pages will be be tracked while the
  1748. * memory slot is write protected.
  1749. */
  1750. if (change != KVM_MR_DELETE && mem->flags & KVM_MEM_LOG_DIRTY_PAGES)
  1751. kvm_mmu_wp_memory_region(kvm, mem->slot);
  1752. }
  1753. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  1754. struct kvm_memory_slot *memslot,
  1755. const struct kvm_userspace_memory_region *mem,
  1756. enum kvm_mr_change change)
  1757. {
  1758. hva_t hva = mem->userspace_addr;
  1759. hva_t reg_end = hva + mem->memory_size;
  1760. bool writable = !(mem->flags & KVM_MEM_READONLY);
  1761. int ret = 0;
  1762. if (change != KVM_MR_CREATE && change != KVM_MR_MOVE &&
  1763. change != KVM_MR_FLAGS_ONLY)
  1764. return 0;
  1765. /*
  1766. * Prevent userspace from creating a memory region outside of the IPA
  1767. * space addressable by the KVM guest IPA space.
  1768. */
  1769. if (memslot->base_gfn + memslot->npages >=
  1770. (KVM_PHYS_SIZE >> PAGE_SHIFT))
  1771. return -EFAULT;
  1772. down_read(&current->mm->mmap_sem);
  1773. /*
  1774. * A memory region could potentially cover multiple VMAs, and any holes
  1775. * between them, so iterate over all of them to find out if we can map
  1776. * any of them right now.
  1777. *
  1778. * +--------------------------------------------+
  1779. * +---------------+----------------+ +----------------+
  1780. * | : VMA 1 | VMA 2 | | VMA 3 : |
  1781. * +---------------+----------------+ +----------------+
  1782. * | memory region |
  1783. * +--------------------------------------------+
  1784. */
  1785. do {
  1786. struct vm_area_struct *vma = find_vma(current->mm, hva);
  1787. hva_t vm_start, vm_end;
  1788. if (!vma || vma->vm_start >= reg_end)
  1789. break;
  1790. /*
  1791. * Mapping a read-only VMA is only allowed if the
  1792. * memory region is configured as read-only.
  1793. */
  1794. if (writable && !(vma->vm_flags & VM_WRITE)) {
  1795. ret = -EPERM;
  1796. break;
  1797. }
  1798. /*
  1799. * Take the intersection of this VMA with the memory region
  1800. */
  1801. vm_start = max(hva, vma->vm_start);
  1802. vm_end = min(reg_end, vma->vm_end);
  1803. if (vma->vm_flags & VM_PFNMAP) {
  1804. gpa_t gpa = mem->guest_phys_addr +
  1805. (vm_start - mem->userspace_addr);
  1806. phys_addr_t pa;
  1807. pa = (phys_addr_t)vma->vm_pgoff << PAGE_SHIFT;
  1808. pa += vm_start - vma->vm_start;
  1809. /* IO region dirty page logging not allowed */
  1810. if (memslot->flags & KVM_MEM_LOG_DIRTY_PAGES) {
  1811. ret = -EINVAL;
  1812. goto out;
  1813. }
  1814. ret = kvm_phys_addr_ioremap(kvm, gpa, pa,
  1815. vm_end - vm_start,
  1816. writable);
  1817. if (ret)
  1818. break;
  1819. }
  1820. hva = vm_end;
  1821. } while (hva < reg_end);
  1822. if (change == KVM_MR_FLAGS_ONLY)
  1823. goto out;
  1824. spin_lock(&kvm->mmu_lock);
  1825. if (ret)
  1826. unmap_stage2_range(kvm, mem->guest_phys_addr, mem->memory_size);
  1827. else
  1828. stage2_flush_memslot(kvm, memslot);
  1829. spin_unlock(&kvm->mmu_lock);
  1830. out:
  1831. up_read(&current->mm->mmap_sem);
  1832. return ret;
  1833. }
  1834. void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
  1835. struct kvm_memory_slot *dont)
  1836. {
  1837. }
  1838. int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
  1839. unsigned long npages)
  1840. {
  1841. return 0;
  1842. }
  1843. void kvm_arch_memslots_updated(struct kvm *kvm, struct kvm_memslots *slots)
  1844. {
  1845. }
  1846. void kvm_arch_flush_shadow_all(struct kvm *kvm)
  1847. {
  1848. kvm_free_stage2_pgd(kvm);
  1849. }
  1850. void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
  1851. struct kvm_memory_slot *slot)
  1852. {
  1853. gpa_t gpa = slot->base_gfn << PAGE_SHIFT;
  1854. phys_addr_t size = slot->npages << PAGE_SHIFT;
  1855. spin_lock(&kvm->mmu_lock);
  1856. unmap_stage2_range(kvm, gpa, size);
  1857. spin_unlock(&kvm->mmu_lock);
  1858. }
  1859. /*
  1860. * See note at ARMv7 ARM B1.14.4 (TL;DR: S/W ops are not easily virtualized).
  1861. *
  1862. * Main problems:
  1863. * - S/W ops are local to a CPU (not broadcast)
  1864. * - We have line migration behind our back (speculation)
  1865. * - System caches don't support S/W at all (damn!)
  1866. *
  1867. * In the face of the above, the best we can do is to try and convert
  1868. * S/W ops to VA ops. Because the guest is not allowed to infer the
  1869. * S/W to PA mapping, it can only use S/W to nuke the whole cache,
  1870. * which is a rather good thing for us.
  1871. *
  1872. * Also, it is only used when turning caches on/off ("The expected
  1873. * usage of the cache maintenance instructions that operate by set/way
  1874. * is associated with the cache maintenance instructions associated
  1875. * with the powerdown and powerup of caches, if this is required by
  1876. * the implementation.").
  1877. *
  1878. * We use the following policy:
  1879. *
  1880. * - If we trap a S/W operation, we enable VM trapping to detect
  1881. * caches being turned on/off, and do a full clean.
  1882. *
  1883. * - We flush the caches on both caches being turned on and off.
  1884. *
  1885. * - Once the caches are enabled, we stop trapping VM ops.
  1886. */
  1887. void kvm_set_way_flush(struct kvm_vcpu *vcpu)
  1888. {
  1889. unsigned long hcr = *vcpu_hcr(vcpu);
  1890. /*
  1891. * If this is the first time we do a S/W operation
  1892. * (i.e. HCR_TVM not set) flush the whole memory, and set the
  1893. * VM trapping.
  1894. *
  1895. * Otherwise, rely on the VM trapping to wait for the MMU +
  1896. * Caches to be turned off. At that point, we'll be able to
  1897. * clean the caches again.
  1898. */
  1899. if (!(hcr & HCR_TVM)) {
  1900. trace_kvm_set_way_flush(*vcpu_pc(vcpu),
  1901. vcpu_has_cache_enabled(vcpu));
  1902. stage2_flush_vm(vcpu->kvm);
  1903. *vcpu_hcr(vcpu) = hcr | HCR_TVM;
  1904. }
  1905. }
  1906. void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled)
  1907. {
  1908. bool now_enabled = vcpu_has_cache_enabled(vcpu);
  1909. /*
  1910. * If switching the MMU+caches on, need to invalidate the caches.
  1911. * If switching it off, need to clean the caches.
  1912. * Clean + invalidate does the trick always.
  1913. */
  1914. if (now_enabled != was_enabled)
  1915. stage2_flush_vm(vcpu->kvm);
  1916. /* Caches are now on, stop trapping VM ops (until a S/W op) */
  1917. if (now_enabled)
  1918. *vcpu_hcr(vcpu) &= ~HCR_TVM;
  1919. trace_kvm_toggle_cache(*vcpu_pc(vcpu), was_enabled, now_enabled);
  1920. }