mmu.c 40 KB

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