mmu.c 52 KB

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