mmu.c 20 KB

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