mmu.c 21 KB

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