mmu.c 19 KB

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