mmu.c 28 KB

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