mmu.c 20 KB

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