mmu.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  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 <trace/events/kvm.h>
  22. #include <asm/pgalloc.h>
  23. #include <asm/cacheflush.h>
  24. #include <asm/kvm_arm.h>
  25. #include <asm/kvm_mmu.h>
  26. #include <asm/kvm_mmio.h>
  27. #include <asm/kvm_asm.h>
  28. #include <asm/kvm_emulate.h>
  29. #include "trace.h"
  30. extern char __hyp_idmap_text_start[], __hyp_idmap_text_end[];
  31. static pgd_t *boot_hyp_pgd;
  32. static pgd_t *hyp_pgd;
  33. static DEFINE_MUTEX(kvm_hyp_pgd_mutex);
  34. static void *init_bounce_page;
  35. static unsigned long hyp_idmap_start;
  36. static unsigned long hyp_idmap_end;
  37. static phys_addr_t hyp_idmap_vector;
  38. static void kvm_tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa)
  39. {
  40. kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, kvm, ipa);
  41. }
  42. static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
  43. int min, int max)
  44. {
  45. void *page;
  46. BUG_ON(max > KVM_NR_MEM_OBJS);
  47. if (cache->nobjs >= min)
  48. return 0;
  49. while (cache->nobjs < max) {
  50. page = (void *)__get_free_page(PGALLOC_GFP);
  51. if (!page)
  52. return -ENOMEM;
  53. cache->objects[cache->nobjs++] = page;
  54. }
  55. return 0;
  56. }
  57. static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
  58. {
  59. while (mc->nobjs)
  60. free_page((unsigned long)mc->objects[--mc->nobjs]);
  61. }
  62. static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc)
  63. {
  64. void *p;
  65. BUG_ON(!mc || !mc->nobjs);
  66. p = mc->objects[--mc->nobjs];
  67. return p;
  68. }
  69. static void clear_pud_entry(pud_t *pud)
  70. {
  71. pmd_t *pmd_table = pmd_offset(pud, 0);
  72. pud_clear(pud);
  73. pmd_free(NULL, pmd_table);
  74. put_page(virt_to_page(pud));
  75. }
  76. static void clear_pmd_entry(pmd_t *pmd)
  77. {
  78. pte_t *pte_table = pte_offset_kernel(pmd, 0);
  79. pmd_clear(pmd);
  80. pte_free_kernel(NULL, pte_table);
  81. put_page(virt_to_page(pmd));
  82. }
  83. static bool pmd_empty(pmd_t *pmd)
  84. {
  85. struct page *pmd_page = virt_to_page(pmd);
  86. return page_count(pmd_page) == 1;
  87. }
  88. static void clear_pte_entry(pte_t *pte)
  89. {
  90. if (pte_present(*pte)) {
  91. kvm_set_pte(pte, __pte(0));
  92. put_page(virt_to_page(pte));
  93. }
  94. }
  95. static bool pte_empty(pte_t *pte)
  96. {
  97. struct page *pte_page = virt_to_page(pte);
  98. return page_count(pte_page) == 1;
  99. }
  100. static void unmap_range(pgd_t *pgdp, unsigned long long start, u64 size)
  101. {
  102. pgd_t *pgd;
  103. pud_t *pud;
  104. pmd_t *pmd;
  105. pte_t *pte;
  106. unsigned long long addr = start, end = start + size;
  107. u64 range;
  108. while (addr < end) {
  109. pgd = pgdp + pgd_index(addr);
  110. pud = pud_offset(pgd, addr);
  111. if (pud_none(*pud)) {
  112. addr += PUD_SIZE;
  113. continue;
  114. }
  115. pmd = pmd_offset(pud, addr);
  116. if (pmd_none(*pmd)) {
  117. addr += PMD_SIZE;
  118. continue;
  119. }
  120. pte = pte_offset_kernel(pmd, addr);
  121. clear_pte_entry(pte);
  122. range = PAGE_SIZE;
  123. /* If we emptied the pte, walk back up the ladder */
  124. if (pte_empty(pte)) {
  125. clear_pmd_entry(pmd);
  126. range = PMD_SIZE;
  127. if (pmd_empty(pmd)) {
  128. clear_pud_entry(pud);
  129. range = PUD_SIZE;
  130. }
  131. }
  132. addr += range;
  133. }
  134. }
  135. /**
  136. * free_boot_hyp_pgd - free HYP boot page tables
  137. *
  138. * Free the HYP boot page tables. The bounce page is also freed.
  139. */
  140. void free_boot_hyp_pgd(void)
  141. {
  142. mutex_lock(&kvm_hyp_pgd_mutex);
  143. if (boot_hyp_pgd) {
  144. unmap_range(boot_hyp_pgd, hyp_idmap_start, PAGE_SIZE);
  145. unmap_range(boot_hyp_pgd, TRAMPOLINE_VA, PAGE_SIZE);
  146. kfree(boot_hyp_pgd);
  147. boot_hyp_pgd = NULL;
  148. }
  149. if (hyp_pgd)
  150. unmap_range(hyp_pgd, TRAMPOLINE_VA, PAGE_SIZE);
  151. kfree(init_bounce_page);
  152. init_bounce_page = NULL;
  153. mutex_unlock(&kvm_hyp_pgd_mutex);
  154. }
  155. /**
  156. * free_hyp_pgds - free Hyp-mode page tables
  157. *
  158. * Assumes hyp_pgd is a page table used strictly in Hyp-mode and
  159. * therefore contains either mappings in the kernel memory area (above
  160. * PAGE_OFFSET), or device mappings in the vmalloc range (from
  161. * VMALLOC_START to VMALLOC_END).
  162. *
  163. * boot_hyp_pgd should only map two pages for the init code.
  164. */
  165. void free_hyp_pgds(void)
  166. {
  167. unsigned long addr;
  168. free_boot_hyp_pgd();
  169. mutex_lock(&kvm_hyp_pgd_mutex);
  170. if (hyp_pgd) {
  171. for (addr = PAGE_OFFSET; virt_addr_valid(addr); addr += PGDIR_SIZE)
  172. unmap_range(hyp_pgd, KERN_TO_HYP(addr), PGDIR_SIZE);
  173. for (addr = VMALLOC_START; is_vmalloc_addr((void*)addr); addr += PGDIR_SIZE)
  174. unmap_range(hyp_pgd, KERN_TO_HYP(addr), PGDIR_SIZE);
  175. kfree(hyp_pgd);
  176. hyp_pgd = NULL;
  177. }
  178. mutex_unlock(&kvm_hyp_pgd_mutex);
  179. }
  180. static void create_hyp_pte_mappings(pmd_t *pmd, unsigned long start,
  181. unsigned long end, unsigned long pfn,
  182. pgprot_t prot)
  183. {
  184. pte_t *pte;
  185. unsigned long addr;
  186. addr = start;
  187. do {
  188. pte = pte_offset_kernel(pmd, addr);
  189. kvm_set_pte(pte, pfn_pte(pfn, prot));
  190. get_page(virt_to_page(pte));
  191. kvm_flush_dcache_to_poc(pte, sizeof(*pte));
  192. pfn++;
  193. } while (addr += PAGE_SIZE, addr != end);
  194. }
  195. static int create_hyp_pmd_mappings(pud_t *pud, unsigned long start,
  196. unsigned long end, unsigned long pfn,
  197. pgprot_t prot)
  198. {
  199. pmd_t *pmd;
  200. pte_t *pte;
  201. unsigned long addr, next;
  202. addr = start;
  203. do {
  204. pmd = pmd_offset(pud, addr);
  205. BUG_ON(pmd_sect(*pmd));
  206. if (pmd_none(*pmd)) {
  207. pte = pte_alloc_one_kernel(NULL, addr);
  208. if (!pte) {
  209. kvm_err("Cannot allocate Hyp pte\n");
  210. return -ENOMEM;
  211. }
  212. pmd_populate_kernel(NULL, pmd, pte);
  213. get_page(virt_to_page(pmd));
  214. kvm_flush_dcache_to_poc(pmd, sizeof(*pmd));
  215. }
  216. next = pmd_addr_end(addr, end);
  217. create_hyp_pte_mappings(pmd, addr, next, pfn, prot);
  218. pfn += (next - addr) >> PAGE_SHIFT;
  219. } while (addr = next, addr != end);
  220. return 0;
  221. }
  222. static int __create_hyp_mappings(pgd_t *pgdp,
  223. unsigned long start, unsigned long end,
  224. unsigned long pfn, pgprot_t prot)
  225. {
  226. pgd_t *pgd;
  227. pud_t *pud;
  228. pmd_t *pmd;
  229. unsigned long addr, next;
  230. int err = 0;
  231. mutex_lock(&kvm_hyp_pgd_mutex);
  232. addr = start & PAGE_MASK;
  233. end = PAGE_ALIGN(end);
  234. do {
  235. pgd = pgdp + pgd_index(addr);
  236. pud = pud_offset(pgd, addr);
  237. if (pud_none_or_clear_bad(pud)) {
  238. pmd = pmd_alloc_one(NULL, addr);
  239. if (!pmd) {
  240. kvm_err("Cannot allocate Hyp pmd\n");
  241. err = -ENOMEM;
  242. goto out;
  243. }
  244. pud_populate(NULL, pud, pmd);
  245. get_page(virt_to_page(pud));
  246. kvm_flush_dcache_to_poc(pud, sizeof(*pud));
  247. }
  248. next = pgd_addr_end(addr, end);
  249. err = create_hyp_pmd_mappings(pud, addr, next, pfn, prot);
  250. if (err)
  251. goto out;
  252. pfn += (next - addr) >> PAGE_SHIFT;
  253. } while (addr = next, addr != end);
  254. out:
  255. mutex_unlock(&kvm_hyp_pgd_mutex);
  256. return err;
  257. }
  258. /**
  259. * create_hyp_mappings - duplicate a kernel virtual address range in Hyp mode
  260. * @from: The virtual kernel start address of the range
  261. * @to: The virtual kernel end address of the range (exclusive)
  262. *
  263. * The same virtual address as the kernel virtual address is also used
  264. * in Hyp-mode mapping (modulo HYP_PAGE_OFFSET) to the same underlying
  265. * physical pages.
  266. */
  267. int create_hyp_mappings(void *from, void *to)
  268. {
  269. unsigned long phys_addr = virt_to_phys(from);
  270. unsigned long start = KERN_TO_HYP((unsigned long)from);
  271. unsigned long end = KERN_TO_HYP((unsigned long)to);
  272. /* Check for a valid kernel memory mapping */
  273. if (!virt_addr_valid(from) || !virt_addr_valid(to - 1))
  274. return -EINVAL;
  275. return __create_hyp_mappings(hyp_pgd, start, end,
  276. __phys_to_pfn(phys_addr), PAGE_HYP);
  277. }
  278. /**
  279. * create_hyp_io_mappings - duplicate a kernel IO mapping into Hyp mode
  280. * @from: The kernel start VA of the range
  281. * @to: The kernel end VA of the range (exclusive)
  282. * @phys_addr: The physical start address which gets mapped
  283. *
  284. * The resulting HYP VA is the same as the kernel VA, modulo
  285. * HYP_PAGE_OFFSET.
  286. */
  287. int create_hyp_io_mappings(void *from, void *to, phys_addr_t phys_addr)
  288. {
  289. unsigned long start = KERN_TO_HYP((unsigned long)from);
  290. unsigned long end = KERN_TO_HYP((unsigned long)to);
  291. /* Check for a valid kernel IO mapping */
  292. if (!is_vmalloc_addr(from) || !is_vmalloc_addr(to - 1))
  293. return -EINVAL;
  294. return __create_hyp_mappings(hyp_pgd, start, end,
  295. __phys_to_pfn(phys_addr), PAGE_HYP_DEVICE);
  296. }
  297. /**
  298. * kvm_alloc_stage2_pgd - allocate level-1 table for stage-2 translation.
  299. * @kvm: The KVM struct pointer for the VM.
  300. *
  301. * Allocates the 1st level table only of size defined by S2_PGD_ORDER (can
  302. * support either full 40-bit input addresses or limited to 32-bit input
  303. * addresses). Clears the allocated pages.
  304. *
  305. * Note we don't need locking here as this is only called when the VM is
  306. * created, which can only be done once.
  307. */
  308. int kvm_alloc_stage2_pgd(struct kvm *kvm)
  309. {
  310. pgd_t *pgd;
  311. if (kvm->arch.pgd != NULL) {
  312. kvm_err("kvm_arch already initialized?\n");
  313. return -EINVAL;
  314. }
  315. pgd = (pgd_t *)__get_free_pages(GFP_KERNEL, S2_PGD_ORDER);
  316. if (!pgd)
  317. return -ENOMEM;
  318. memset(pgd, 0, PTRS_PER_S2_PGD * sizeof(pgd_t));
  319. kvm_clean_pgd(pgd);
  320. kvm->arch.pgd = pgd;
  321. return 0;
  322. }
  323. /**
  324. * unmap_stage2_range -- Clear stage2 page table entries to unmap a range
  325. * @kvm: The VM pointer
  326. * @start: The intermediate physical base address of the range to unmap
  327. * @size: The size of the area to unmap
  328. *
  329. * Clear a range of stage-2 mappings, lowering the various ref-counts. Must
  330. * be called while holding mmu_lock (unless for freeing the stage2 pgd before
  331. * destroying the VM), otherwise another faulting VCPU may come in and mess
  332. * with things behind our backs.
  333. */
  334. static void unmap_stage2_range(struct kvm *kvm, phys_addr_t start, u64 size)
  335. {
  336. unmap_range(kvm->arch.pgd, start, size);
  337. }
  338. /**
  339. * kvm_free_stage2_pgd - free all stage-2 tables
  340. * @kvm: The KVM struct pointer for the VM.
  341. *
  342. * Walks the level-1 page table pointed to by kvm->arch.pgd and frees all
  343. * underlying level-2 and level-3 tables before freeing the actual level-1 table
  344. * and setting the struct pointer to NULL.
  345. *
  346. * Note we don't need locking here as this is only called when the VM is
  347. * destroyed, which can only be done once.
  348. */
  349. void kvm_free_stage2_pgd(struct kvm *kvm)
  350. {
  351. if (kvm->arch.pgd == NULL)
  352. return;
  353. unmap_stage2_range(kvm, 0, KVM_PHYS_SIZE);
  354. free_pages((unsigned long)kvm->arch.pgd, S2_PGD_ORDER);
  355. kvm->arch.pgd = NULL;
  356. }
  357. static int stage2_set_pte(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
  358. phys_addr_t addr, const pte_t *new_pte, bool iomap)
  359. {
  360. pgd_t *pgd;
  361. pud_t *pud;
  362. pmd_t *pmd;
  363. pte_t *pte, old_pte;
  364. /* Create 2nd stage page table mapping - Level 1 */
  365. pgd = kvm->arch.pgd + pgd_index(addr);
  366. pud = pud_offset(pgd, addr);
  367. if (pud_none(*pud)) {
  368. if (!cache)
  369. return 0; /* ignore calls from kvm_set_spte_hva */
  370. pmd = mmu_memory_cache_alloc(cache);
  371. pud_populate(NULL, pud, pmd);
  372. get_page(virt_to_page(pud));
  373. }
  374. pmd = pmd_offset(pud, addr);
  375. /* Create 2nd stage page table mapping - Level 2 */
  376. if (pmd_none(*pmd)) {
  377. if (!cache)
  378. return 0; /* ignore calls from kvm_set_spte_hva */
  379. pte = mmu_memory_cache_alloc(cache);
  380. kvm_clean_pte(pte);
  381. pmd_populate_kernel(NULL, pmd, pte);
  382. get_page(virt_to_page(pmd));
  383. }
  384. pte = pte_offset_kernel(pmd, addr);
  385. if (iomap && pte_present(*pte))
  386. return -EFAULT;
  387. /* Create 2nd stage page table mapping - Level 3 */
  388. old_pte = *pte;
  389. kvm_set_pte(pte, *new_pte);
  390. if (pte_present(old_pte))
  391. kvm_tlb_flush_vmid_ipa(kvm, addr);
  392. else
  393. get_page(virt_to_page(pte));
  394. return 0;
  395. }
  396. /**
  397. * kvm_phys_addr_ioremap - map a device range to guest IPA
  398. *
  399. * @kvm: The KVM pointer
  400. * @guest_ipa: The IPA at which to insert the mapping
  401. * @pa: The physical address of the device
  402. * @size: The size of the mapping
  403. */
  404. int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
  405. phys_addr_t pa, unsigned long size)
  406. {
  407. phys_addr_t addr, end;
  408. int ret = 0;
  409. unsigned long pfn;
  410. struct kvm_mmu_memory_cache cache = { 0, };
  411. end = (guest_ipa + size + PAGE_SIZE - 1) & PAGE_MASK;
  412. pfn = __phys_to_pfn(pa);
  413. for (addr = guest_ipa; addr < end; addr += PAGE_SIZE) {
  414. pte_t pte = pfn_pte(pfn, PAGE_S2_DEVICE);
  415. kvm_set_s2pte_writable(&pte);
  416. ret = mmu_topup_memory_cache(&cache, 2, 2);
  417. if (ret)
  418. goto out;
  419. spin_lock(&kvm->mmu_lock);
  420. ret = stage2_set_pte(kvm, &cache, addr, &pte, true);
  421. spin_unlock(&kvm->mmu_lock);
  422. if (ret)
  423. goto out;
  424. pfn++;
  425. }
  426. out:
  427. mmu_free_memory_cache(&cache);
  428. return ret;
  429. }
  430. static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
  431. gfn_t gfn, struct kvm_memory_slot *memslot,
  432. unsigned long fault_status)
  433. {
  434. pte_t new_pte;
  435. pfn_t pfn;
  436. int ret;
  437. bool write_fault, writable;
  438. unsigned long mmu_seq;
  439. struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
  440. write_fault = kvm_is_write_fault(kvm_vcpu_get_hsr(vcpu));
  441. if (fault_status == FSC_PERM && !write_fault) {
  442. kvm_err("Unexpected L2 read permission error\n");
  443. return -EFAULT;
  444. }
  445. /* We need minimum second+third level pages */
  446. ret = mmu_topup_memory_cache(memcache, 2, KVM_NR_MEM_OBJS);
  447. if (ret)
  448. return ret;
  449. mmu_seq = vcpu->kvm->mmu_notifier_seq;
  450. /*
  451. * Ensure the read of mmu_notifier_seq happens before we call
  452. * gfn_to_pfn_prot (which calls get_user_pages), so that we don't risk
  453. * the page we just got a reference to gets unmapped before we have a
  454. * chance to grab the mmu_lock, which ensure that if the page gets
  455. * unmapped afterwards, the call to kvm_unmap_hva will take it away
  456. * from us again properly. This smp_rmb() interacts with the smp_wmb()
  457. * in kvm_mmu_notifier_invalidate_<page|range_end>.
  458. */
  459. smp_rmb();
  460. pfn = gfn_to_pfn_prot(vcpu->kvm, gfn, write_fault, &writable);
  461. if (is_error_pfn(pfn))
  462. return -EFAULT;
  463. new_pte = pfn_pte(pfn, PAGE_S2);
  464. coherent_icache_guest_page(vcpu->kvm, gfn);
  465. spin_lock(&vcpu->kvm->mmu_lock);
  466. if (mmu_notifier_retry(vcpu->kvm, mmu_seq))
  467. goto out_unlock;
  468. if (writable) {
  469. kvm_set_s2pte_writable(&new_pte);
  470. kvm_set_pfn_dirty(pfn);
  471. }
  472. stage2_set_pte(vcpu->kvm, memcache, fault_ipa, &new_pte, false);
  473. out_unlock:
  474. spin_unlock(&vcpu->kvm->mmu_lock);
  475. kvm_release_pfn_clean(pfn);
  476. return 0;
  477. }
  478. /**
  479. * kvm_handle_guest_abort - handles all 2nd stage aborts
  480. * @vcpu: the VCPU pointer
  481. * @run: the kvm_run structure
  482. *
  483. * Any abort that gets to the host is almost guaranteed to be caused by a
  484. * missing second stage translation table entry, which can mean that either the
  485. * guest simply needs more memory and we must allocate an appropriate page or it
  486. * can mean that the guest tried to access I/O memory, which is emulated by user
  487. * space. The distinction is based on the IPA causing the fault and whether this
  488. * memory region has been registered as standard RAM by user space.
  489. */
  490. int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run)
  491. {
  492. unsigned long fault_status;
  493. phys_addr_t fault_ipa;
  494. struct kvm_memory_slot *memslot;
  495. bool is_iabt;
  496. gfn_t gfn;
  497. int ret, idx;
  498. is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
  499. fault_ipa = kvm_vcpu_get_fault_ipa(vcpu);
  500. trace_kvm_guest_fault(*vcpu_pc(vcpu), kvm_vcpu_get_hsr(vcpu),
  501. kvm_vcpu_get_hfar(vcpu), fault_ipa);
  502. /* Check the stage-2 fault is trans. fault or write fault */
  503. fault_status = kvm_vcpu_trap_get_fault(vcpu);
  504. if (fault_status != FSC_FAULT && fault_status != FSC_PERM) {
  505. kvm_err("Unsupported fault status: EC=%#x DFCS=%#lx\n",
  506. kvm_vcpu_trap_get_class(vcpu), fault_status);
  507. return -EFAULT;
  508. }
  509. idx = srcu_read_lock(&vcpu->kvm->srcu);
  510. gfn = fault_ipa >> PAGE_SHIFT;
  511. if (!kvm_is_visible_gfn(vcpu->kvm, gfn)) {
  512. if (is_iabt) {
  513. /* Prefetch Abort on I/O address */
  514. kvm_inject_pabt(vcpu, kvm_vcpu_get_hfar(vcpu));
  515. ret = 1;
  516. goto out_unlock;
  517. }
  518. if (fault_status != FSC_FAULT) {
  519. kvm_err("Unsupported fault status on io memory: %#lx\n",
  520. fault_status);
  521. ret = -EFAULT;
  522. goto out_unlock;
  523. }
  524. /*
  525. * The IPA is reported as [MAX:12], so we need to
  526. * complement it with the bottom 12 bits from the
  527. * faulting VA. This is always 12 bits, irrespective
  528. * of the page size.
  529. */
  530. fault_ipa |= kvm_vcpu_get_hfar(vcpu) & ((1 << 12) - 1);
  531. ret = io_mem_abort(vcpu, run, fault_ipa);
  532. goto out_unlock;
  533. }
  534. memslot = gfn_to_memslot(vcpu->kvm, gfn);
  535. ret = user_mem_abort(vcpu, fault_ipa, gfn, memslot, fault_status);
  536. if (ret == 0)
  537. ret = 1;
  538. out_unlock:
  539. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  540. return ret;
  541. }
  542. static void handle_hva_to_gpa(struct kvm *kvm,
  543. unsigned long start,
  544. unsigned long end,
  545. void (*handler)(struct kvm *kvm,
  546. gpa_t gpa, void *data),
  547. void *data)
  548. {
  549. struct kvm_memslots *slots;
  550. struct kvm_memory_slot *memslot;
  551. slots = kvm_memslots(kvm);
  552. /* we only care about the pages that the guest sees */
  553. kvm_for_each_memslot(memslot, slots) {
  554. unsigned long hva_start, hva_end;
  555. gfn_t gfn, gfn_end;
  556. hva_start = max(start, memslot->userspace_addr);
  557. hva_end = min(end, memslot->userspace_addr +
  558. (memslot->npages << PAGE_SHIFT));
  559. if (hva_start >= hva_end)
  560. continue;
  561. /*
  562. * {gfn(page) | page intersects with [hva_start, hva_end)} =
  563. * {gfn_start, gfn_start+1, ..., gfn_end-1}.
  564. */
  565. gfn = hva_to_gfn_memslot(hva_start, memslot);
  566. gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot);
  567. for (; gfn < gfn_end; ++gfn) {
  568. gpa_t gpa = gfn << PAGE_SHIFT;
  569. handler(kvm, gpa, data);
  570. }
  571. }
  572. }
  573. static void kvm_unmap_hva_handler(struct kvm *kvm, gpa_t gpa, void *data)
  574. {
  575. unmap_stage2_range(kvm, gpa, PAGE_SIZE);
  576. kvm_tlb_flush_vmid_ipa(kvm, gpa);
  577. }
  578. int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
  579. {
  580. unsigned long end = hva + PAGE_SIZE;
  581. if (!kvm->arch.pgd)
  582. return 0;
  583. trace_kvm_unmap_hva(hva);
  584. handle_hva_to_gpa(kvm, hva, end, &kvm_unmap_hva_handler, NULL);
  585. return 0;
  586. }
  587. int kvm_unmap_hva_range(struct kvm *kvm,
  588. unsigned long start, unsigned long end)
  589. {
  590. if (!kvm->arch.pgd)
  591. return 0;
  592. trace_kvm_unmap_hva_range(start, end);
  593. handle_hva_to_gpa(kvm, start, end, &kvm_unmap_hva_handler, NULL);
  594. return 0;
  595. }
  596. static void kvm_set_spte_handler(struct kvm *kvm, gpa_t gpa, void *data)
  597. {
  598. pte_t *pte = (pte_t *)data;
  599. stage2_set_pte(kvm, NULL, gpa, pte, false);
  600. }
  601. void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
  602. {
  603. unsigned long end = hva + PAGE_SIZE;
  604. pte_t stage2_pte;
  605. if (!kvm->arch.pgd)
  606. return;
  607. trace_kvm_set_spte_hva(hva);
  608. stage2_pte = pfn_pte(pte_pfn(pte), PAGE_S2);
  609. handle_hva_to_gpa(kvm, hva, end, &kvm_set_spte_handler, &stage2_pte);
  610. }
  611. void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu)
  612. {
  613. mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
  614. }
  615. phys_addr_t kvm_mmu_get_httbr(void)
  616. {
  617. return virt_to_phys(hyp_pgd);
  618. }
  619. phys_addr_t kvm_mmu_get_boot_httbr(void)
  620. {
  621. return virt_to_phys(boot_hyp_pgd);
  622. }
  623. phys_addr_t kvm_get_idmap_vector(void)
  624. {
  625. return hyp_idmap_vector;
  626. }
  627. int kvm_mmu_init(void)
  628. {
  629. int err;
  630. hyp_idmap_start = virt_to_phys(__hyp_idmap_text_start);
  631. hyp_idmap_end = virt_to_phys(__hyp_idmap_text_end);
  632. hyp_idmap_vector = virt_to_phys(__kvm_hyp_init);
  633. if ((hyp_idmap_start ^ hyp_idmap_end) & PAGE_MASK) {
  634. /*
  635. * Our init code is crossing a page boundary. Allocate
  636. * a bounce page, copy the code over and use that.
  637. */
  638. size_t len = __hyp_idmap_text_end - __hyp_idmap_text_start;
  639. phys_addr_t phys_base;
  640. init_bounce_page = kmalloc(PAGE_SIZE, GFP_KERNEL);
  641. if (!init_bounce_page) {
  642. kvm_err("Couldn't allocate HYP init bounce page\n");
  643. err = -ENOMEM;
  644. goto out;
  645. }
  646. memcpy(init_bounce_page, __hyp_idmap_text_start, len);
  647. /*
  648. * Warning: the code we just copied to the bounce page
  649. * must be flushed to the point of coherency.
  650. * Otherwise, the data may be sitting in L2, and HYP
  651. * mode won't be able to observe it as it runs with
  652. * caches off at that point.
  653. */
  654. kvm_flush_dcache_to_poc(init_bounce_page, len);
  655. phys_base = virt_to_phys(init_bounce_page);
  656. hyp_idmap_vector += phys_base - hyp_idmap_start;
  657. hyp_idmap_start = phys_base;
  658. hyp_idmap_end = phys_base + len;
  659. kvm_info("Using HYP init bounce page @%lx\n",
  660. (unsigned long)phys_base);
  661. }
  662. hyp_pgd = kzalloc(PTRS_PER_PGD * sizeof(pgd_t), GFP_KERNEL);
  663. boot_hyp_pgd = kzalloc(PTRS_PER_PGD * sizeof(pgd_t), GFP_KERNEL);
  664. if (!hyp_pgd || !boot_hyp_pgd) {
  665. kvm_err("Hyp mode PGD not allocated\n");
  666. err = -ENOMEM;
  667. goto out;
  668. }
  669. /* Create the idmap in the boot page tables */
  670. err = __create_hyp_mappings(boot_hyp_pgd,
  671. hyp_idmap_start, hyp_idmap_end,
  672. __phys_to_pfn(hyp_idmap_start),
  673. PAGE_HYP);
  674. if (err) {
  675. kvm_err("Failed to idmap %lx-%lx\n",
  676. hyp_idmap_start, hyp_idmap_end);
  677. goto out;
  678. }
  679. /* Map the very same page at the trampoline VA */
  680. err = __create_hyp_mappings(boot_hyp_pgd,
  681. TRAMPOLINE_VA, TRAMPOLINE_VA + PAGE_SIZE,
  682. __phys_to_pfn(hyp_idmap_start),
  683. PAGE_HYP);
  684. if (err) {
  685. kvm_err("Failed to map trampoline @%lx into boot HYP pgd\n",
  686. TRAMPOLINE_VA);
  687. goto out;
  688. }
  689. /* Map the same page again into the runtime page tables */
  690. err = __create_hyp_mappings(hyp_pgd,
  691. TRAMPOLINE_VA, TRAMPOLINE_VA + PAGE_SIZE,
  692. __phys_to_pfn(hyp_idmap_start),
  693. PAGE_HYP);
  694. if (err) {
  695. kvm_err("Failed to map trampoline @%lx into runtime HYP pgd\n",
  696. TRAMPOLINE_VA);
  697. goto out;
  698. }
  699. return 0;
  700. out:
  701. free_hyp_pgds();
  702. return err;
  703. }