mmu.c 50 KB

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