mmu.c 19 KB

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