mmu.c 26 KB

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