mmu.c 19 KB

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