mmu.c 34 KB

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