mmu.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  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 <linux/hugetlb.h>
  22. #include <trace/events/kvm.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 "trace.h"
  31. extern char __hyp_idmap_text_start[], __hyp_idmap_text_end[];
  32. static pgd_t *boot_hyp_pgd;
  33. static pgd_t *hyp_pgd;
  34. static DEFINE_MUTEX(kvm_hyp_pgd_mutex);
  35. static void *init_bounce_page;
  36. static unsigned long hyp_idmap_start;
  37. static unsigned long hyp_idmap_end;
  38. static phys_addr_t hyp_idmap_vector;
  39. #define kvm_pmd_huge(_x) (pmd_huge(_x) || pmd_trans_huge(_x))
  40. static void kvm_tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa)
  41. {
  42. /*
  43. * This function also gets called when dealing with HYP page
  44. * tables. As HYP doesn't have an associated struct kvm (and
  45. * the HYP page tables are fairly static), we don't do
  46. * anything there.
  47. */
  48. if (kvm)
  49. kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, kvm, ipa);
  50. }
  51. static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
  52. int min, int max)
  53. {
  54. void *page;
  55. BUG_ON(max > KVM_NR_MEM_OBJS);
  56. if (cache->nobjs >= min)
  57. return 0;
  58. while (cache->nobjs < max) {
  59. page = (void *)__get_free_page(PGALLOC_GFP);
  60. if (!page)
  61. return -ENOMEM;
  62. cache->objects[cache->nobjs++] = page;
  63. }
  64. return 0;
  65. }
  66. static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
  67. {
  68. while (mc->nobjs)
  69. free_page((unsigned long)mc->objects[--mc->nobjs]);
  70. }
  71. static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc)
  72. {
  73. void *p;
  74. BUG_ON(!mc || !mc->nobjs);
  75. p = mc->objects[--mc->nobjs];
  76. return p;
  77. }
  78. static bool page_empty(void *ptr)
  79. {
  80. struct page *ptr_page = virt_to_page(ptr);
  81. return page_count(ptr_page) == 1;
  82. }
  83. static void clear_pud_entry(struct kvm *kvm, pud_t *pud, phys_addr_t addr)
  84. {
  85. if (pud_huge(*pud)) {
  86. pud_clear(pud);
  87. kvm_tlb_flush_vmid_ipa(kvm, addr);
  88. } else {
  89. pmd_t *pmd_table = pmd_offset(pud, 0);
  90. pud_clear(pud);
  91. kvm_tlb_flush_vmid_ipa(kvm, addr);
  92. pmd_free(NULL, pmd_table);
  93. }
  94. put_page(virt_to_page(pud));
  95. }
  96. static void clear_pmd_entry(struct kvm *kvm, pmd_t *pmd, phys_addr_t addr)
  97. {
  98. if (kvm_pmd_huge(*pmd)) {
  99. pmd_clear(pmd);
  100. kvm_tlb_flush_vmid_ipa(kvm, addr);
  101. } else {
  102. pte_t *pte_table = pte_offset_kernel(pmd, 0);
  103. pmd_clear(pmd);
  104. kvm_tlb_flush_vmid_ipa(kvm, addr);
  105. pte_free_kernel(NULL, pte_table);
  106. }
  107. put_page(virt_to_page(pmd));
  108. }
  109. static void clear_pte_entry(struct kvm *kvm, pte_t *pte, phys_addr_t addr)
  110. {
  111. if (pte_present(*pte)) {
  112. kvm_set_pte(pte, __pte(0));
  113. put_page(virt_to_page(pte));
  114. kvm_tlb_flush_vmid_ipa(kvm, addr);
  115. }
  116. }
  117. static void unmap_range(struct kvm *kvm, pgd_t *pgdp,
  118. unsigned long long start, u64 size)
  119. {
  120. pgd_t *pgd;
  121. pud_t *pud;
  122. pmd_t *pmd;
  123. pte_t *pte;
  124. unsigned long long addr = start, end = start + size;
  125. u64 next;
  126. while (addr < end) {
  127. pgd = pgdp + pgd_index(addr);
  128. pud = pud_offset(pgd, addr);
  129. if (pud_none(*pud)) {
  130. addr = pud_addr_end(addr, end);
  131. continue;
  132. }
  133. if (pud_huge(*pud)) {
  134. /*
  135. * If we are dealing with a huge pud, just clear it and
  136. * move on.
  137. */
  138. clear_pud_entry(kvm, pud, addr);
  139. addr = pud_addr_end(addr, end);
  140. continue;
  141. }
  142. pmd = pmd_offset(pud, addr);
  143. if (pmd_none(*pmd)) {
  144. addr = pmd_addr_end(addr, end);
  145. continue;
  146. }
  147. if (!kvm_pmd_huge(*pmd)) {
  148. pte = pte_offset_kernel(pmd, addr);
  149. clear_pte_entry(kvm, pte, addr);
  150. next = addr + PAGE_SIZE;
  151. }
  152. /*
  153. * If the pmd entry is to be cleared, walk back up the ladder
  154. */
  155. if (kvm_pmd_huge(*pmd) || page_empty(pte)) {
  156. clear_pmd_entry(kvm, pmd, addr);
  157. next = pmd_addr_end(addr, end);
  158. if (page_empty(pmd) && !page_empty(pud)) {
  159. clear_pud_entry(kvm, pud, addr);
  160. next = pud_addr_end(addr, end);
  161. }
  162. }
  163. addr = next;
  164. }
  165. }
  166. /**
  167. * free_boot_hyp_pgd - free HYP boot page tables
  168. *
  169. * Free the HYP boot page tables. The bounce page is also freed.
  170. */
  171. void free_boot_hyp_pgd(void)
  172. {
  173. mutex_lock(&kvm_hyp_pgd_mutex);
  174. if (boot_hyp_pgd) {
  175. unmap_range(NULL, boot_hyp_pgd, hyp_idmap_start, PAGE_SIZE);
  176. unmap_range(NULL, boot_hyp_pgd, TRAMPOLINE_VA, PAGE_SIZE);
  177. kfree(boot_hyp_pgd);
  178. boot_hyp_pgd = NULL;
  179. }
  180. if (hyp_pgd)
  181. unmap_range(NULL, hyp_pgd, TRAMPOLINE_VA, PAGE_SIZE);
  182. kfree(init_bounce_page);
  183. init_bounce_page = NULL;
  184. mutex_unlock(&kvm_hyp_pgd_mutex);
  185. }
  186. /**
  187. * free_hyp_pgds - free Hyp-mode page tables
  188. *
  189. * Assumes hyp_pgd is a page table used strictly in Hyp-mode and
  190. * therefore contains either mappings in the kernel memory area (above
  191. * PAGE_OFFSET), or device mappings in the vmalloc range (from
  192. * VMALLOC_START to VMALLOC_END).
  193. *
  194. * boot_hyp_pgd should only map two pages for the init code.
  195. */
  196. void free_hyp_pgds(void)
  197. {
  198. unsigned long addr;
  199. free_boot_hyp_pgd();
  200. mutex_lock(&kvm_hyp_pgd_mutex);
  201. if (hyp_pgd) {
  202. for (addr = PAGE_OFFSET; virt_addr_valid(addr); addr += PGDIR_SIZE)
  203. unmap_range(NULL, hyp_pgd, KERN_TO_HYP(addr), PGDIR_SIZE);
  204. for (addr = VMALLOC_START; is_vmalloc_addr((void*)addr); addr += PGDIR_SIZE)
  205. unmap_range(NULL, hyp_pgd, KERN_TO_HYP(addr), PGDIR_SIZE);
  206. kfree(hyp_pgd);
  207. hyp_pgd = NULL;
  208. }
  209. mutex_unlock(&kvm_hyp_pgd_mutex);
  210. }
  211. static void create_hyp_pte_mappings(pmd_t *pmd, unsigned long start,
  212. unsigned long end, unsigned long pfn,
  213. pgprot_t prot)
  214. {
  215. pte_t *pte;
  216. unsigned long addr;
  217. addr = start;
  218. do {
  219. pte = pte_offset_kernel(pmd, addr);
  220. kvm_set_pte(pte, pfn_pte(pfn, prot));
  221. get_page(virt_to_page(pte));
  222. kvm_flush_dcache_to_poc(pte, sizeof(*pte));
  223. pfn++;
  224. } while (addr += PAGE_SIZE, addr != end);
  225. }
  226. static int create_hyp_pmd_mappings(pud_t *pud, unsigned long start,
  227. unsigned long end, unsigned long pfn,
  228. pgprot_t prot)
  229. {
  230. pmd_t *pmd;
  231. pte_t *pte;
  232. unsigned long addr, next;
  233. addr = start;
  234. do {
  235. pmd = pmd_offset(pud, addr);
  236. BUG_ON(pmd_sect(*pmd));
  237. if (pmd_none(*pmd)) {
  238. pte = pte_alloc_one_kernel(NULL, addr);
  239. if (!pte) {
  240. kvm_err("Cannot allocate Hyp pte\n");
  241. return -ENOMEM;
  242. }
  243. pmd_populate_kernel(NULL, pmd, pte);
  244. get_page(virt_to_page(pmd));
  245. kvm_flush_dcache_to_poc(pmd, sizeof(*pmd));
  246. }
  247. next = pmd_addr_end(addr, end);
  248. create_hyp_pte_mappings(pmd, addr, next, pfn, prot);
  249. pfn += (next - addr) >> PAGE_SHIFT;
  250. } while (addr = next, addr != end);
  251. return 0;
  252. }
  253. static int __create_hyp_mappings(pgd_t *pgdp,
  254. unsigned long start, unsigned long end,
  255. unsigned long pfn, pgprot_t prot)
  256. {
  257. pgd_t *pgd;
  258. pud_t *pud;
  259. pmd_t *pmd;
  260. unsigned long addr, next;
  261. int err = 0;
  262. mutex_lock(&kvm_hyp_pgd_mutex);
  263. addr = start & PAGE_MASK;
  264. end = PAGE_ALIGN(end);
  265. do {
  266. pgd = pgdp + pgd_index(addr);
  267. pud = pud_offset(pgd, addr);
  268. if (pud_none_or_clear_bad(pud)) {
  269. pmd = pmd_alloc_one(NULL, addr);
  270. if (!pmd) {
  271. kvm_err("Cannot allocate Hyp pmd\n");
  272. err = -ENOMEM;
  273. goto out;
  274. }
  275. pud_populate(NULL, pud, pmd);
  276. get_page(virt_to_page(pud));
  277. kvm_flush_dcache_to_poc(pud, sizeof(*pud));
  278. }
  279. next = pgd_addr_end(addr, end);
  280. err = create_hyp_pmd_mappings(pud, addr, next, pfn, prot);
  281. if (err)
  282. goto out;
  283. pfn += (next - addr) >> PAGE_SHIFT;
  284. } while (addr = next, addr != end);
  285. out:
  286. mutex_unlock(&kvm_hyp_pgd_mutex);
  287. return err;
  288. }
  289. /**
  290. * create_hyp_mappings - duplicate a kernel virtual address range in Hyp mode
  291. * @from: The virtual kernel start address of the range
  292. * @to: The virtual kernel end address of the range (exclusive)
  293. *
  294. * The same virtual address as the kernel virtual address is also used
  295. * in Hyp-mode mapping (modulo HYP_PAGE_OFFSET) to the same underlying
  296. * physical pages.
  297. */
  298. int create_hyp_mappings(void *from, void *to)
  299. {
  300. unsigned long phys_addr = virt_to_phys(from);
  301. unsigned long start = KERN_TO_HYP((unsigned long)from);
  302. unsigned long end = KERN_TO_HYP((unsigned long)to);
  303. /* Check for a valid kernel memory mapping */
  304. if (!virt_addr_valid(from) || !virt_addr_valid(to - 1))
  305. return -EINVAL;
  306. return __create_hyp_mappings(hyp_pgd, start, end,
  307. __phys_to_pfn(phys_addr), PAGE_HYP);
  308. }
  309. /**
  310. * create_hyp_io_mappings - duplicate a kernel IO mapping into Hyp mode
  311. * @from: The kernel start VA of the range
  312. * @to: The kernel end VA of the range (exclusive)
  313. * @phys_addr: The physical start address which gets mapped
  314. *
  315. * The resulting HYP VA is the same as the kernel VA, modulo
  316. * HYP_PAGE_OFFSET.
  317. */
  318. int create_hyp_io_mappings(void *from, void *to, phys_addr_t phys_addr)
  319. {
  320. unsigned long start = KERN_TO_HYP((unsigned long)from);
  321. unsigned long end = KERN_TO_HYP((unsigned long)to);
  322. /* Check for a valid kernel IO mapping */
  323. if (!is_vmalloc_addr(from) || !is_vmalloc_addr(to - 1))
  324. return -EINVAL;
  325. return __create_hyp_mappings(hyp_pgd, start, end,
  326. __phys_to_pfn(phys_addr), PAGE_HYP_DEVICE);
  327. }
  328. /**
  329. * kvm_alloc_stage2_pgd - allocate level-1 table for stage-2 translation.
  330. * @kvm: The KVM struct pointer for the VM.
  331. *
  332. * Allocates the 1st level table only of size defined by S2_PGD_ORDER (can
  333. * support either full 40-bit input addresses or limited to 32-bit input
  334. * addresses). Clears the allocated pages.
  335. *
  336. * Note we don't need locking here as this is only called when the VM is
  337. * created, which can only be done once.
  338. */
  339. int kvm_alloc_stage2_pgd(struct kvm *kvm)
  340. {
  341. pgd_t *pgd;
  342. if (kvm->arch.pgd != NULL) {
  343. kvm_err("kvm_arch already initialized?\n");
  344. return -EINVAL;
  345. }
  346. pgd = (pgd_t *)__get_free_pages(GFP_KERNEL, S2_PGD_ORDER);
  347. if (!pgd)
  348. return -ENOMEM;
  349. memset(pgd, 0, PTRS_PER_S2_PGD * sizeof(pgd_t));
  350. kvm_clean_pgd(pgd);
  351. kvm->arch.pgd = pgd;
  352. return 0;
  353. }
  354. /**
  355. * unmap_stage2_range -- Clear stage2 page table entries to unmap a range
  356. * @kvm: The VM pointer
  357. * @start: The intermediate physical base address of the range to unmap
  358. * @size: The size of the area to unmap
  359. *
  360. * Clear a range of stage-2 mappings, lowering the various ref-counts. Must
  361. * be called while holding mmu_lock (unless for freeing the stage2 pgd before
  362. * destroying the VM), otherwise another faulting VCPU may come in and mess
  363. * with things behind our backs.
  364. */
  365. static void unmap_stage2_range(struct kvm *kvm, phys_addr_t start, u64 size)
  366. {
  367. unmap_range(kvm, kvm->arch.pgd, start, size);
  368. }
  369. /**
  370. * kvm_free_stage2_pgd - free all stage-2 tables
  371. * @kvm: The KVM struct pointer for the VM.
  372. *
  373. * Walks the level-1 page table pointed to by kvm->arch.pgd and frees all
  374. * underlying level-2 and level-3 tables before freeing the actual level-1 table
  375. * and setting the struct pointer to NULL.
  376. *
  377. * Note we don't need locking here as this is only called when the VM is
  378. * destroyed, which can only be done once.
  379. */
  380. void kvm_free_stage2_pgd(struct kvm *kvm)
  381. {
  382. if (kvm->arch.pgd == NULL)
  383. return;
  384. unmap_stage2_range(kvm, 0, KVM_PHYS_SIZE);
  385. free_pages((unsigned long)kvm->arch.pgd, S2_PGD_ORDER);
  386. kvm->arch.pgd = NULL;
  387. }
  388. static pmd_t *stage2_get_pmd(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
  389. phys_addr_t addr)
  390. {
  391. pgd_t *pgd;
  392. pud_t *pud;
  393. pmd_t *pmd;
  394. pgd = kvm->arch.pgd + pgd_index(addr);
  395. pud = pud_offset(pgd, addr);
  396. if (pud_none(*pud)) {
  397. if (!cache)
  398. return NULL;
  399. pmd = mmu_memory_cache_alloc(cache);
  400. pud_populate(NULL, pud, pmd);
  401. get_page(virt_to_page(pud));
  402. }
  403. return pmd_offset(pud, addr);
  404. }
  405. static int stage2_set_pmd_huge(struct kvm *kvm, struct kvm_mmu_memory_cache
  406. *cache, phys_addr_t addr, const pmd_t *new_pmd)
  407. {
  408. pmd_t *pmd, old_pmd;
  409. pmd = stage2_get_pmd(kvm, cache, addr);
  410. VM_BUG_ON(!pmd);
  411. /*
  412. * Mapping in huge pages should only happen through a fault. If a
  413. * page is merged into a transparent huge page, the individual
  414. * subpages of that huge page should be unmapped through MMU
  415. * notifiers before we get here.
  416. *
  417. * Merging of CompoundPages is not supported; they should become
  418. * splitting first, unmapped, merged, and mapped back in on-demand.
  419. */
  420. VM_BUG_ON(pmd_present(*pmd) && pmd_pfn(*pmd) != pmd_pfn(*new_pmd));
  421. old_pmd = *pmd;
  422. kvm_set_pmd(pmd, *new_pmd);
  423. if (pmd_present(old_pmd))
  424. kvm_tlb_flush_vmid_ipa(kvm, addr);
  425. else
  426. get_page(virt_to_page(pmd));
  427. return 0;
  428. }
  429. static int stage2_set_pte(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
  430. phys_addr_t addr, const pte_t *new_pte, bool iomap)
  431. {
  432. pmd_t *pmd;
  433. pte_t *pte, old_pte;
  434. /* Create stage-2 page table mapping - Level 1 */
  435. pmd = stage2_get_pmd(kvm, cache, addr);
  436. if (!pmd) {
  437. /*
  438. * Ignore calls from kvm_set_spte_hva for unallocated
  439. * address ranges.
  440. */
  441. return 0;
  442. }
  443. /* Create stage-2 page mappings - Level 2 */
  444. if (pmd_none(*pmd)) {
  445. if (!cache)
  446. return 0; /* ignore calls from kvm_set_spte_hva */
  447. pte = mmu_memory_cache_alloc(cache);
  448. kvm_clean_pte(pte);
  449. pmd_populate_kernel(NULL, pmd, pte);
  450. get_page(virt_to_page(pmd));
  451. }
  452. pte = pte_offset_kernel(pmd, addr);
  453. if (iomap && pte_present(*pte))
  454. return -EFAULT;
  455. /* Create 2nd stage page table mapping - Level 3 */
  456. old_pte = *pte;
  457. kvm_set_pte(pte, *new_pte);
  458. if (pte_present(old_pte))
  459. kvm_tlb_flush_vmid_ipa(kvm, addr);
  460. else
  461. get_page(virt_to_page(pte));
  462. return 0;
  463. }
  464. /**
  465. * kvm_phys_addr_ioremap - map a device range to guest IPA
  466. *
  467. * @kvm: The KVM pointer
  468. * @guest_ipa: The IPA at which to insert the mapping
  469. * @pa: The physical address of the device
  470. * @size: The size of the mapping
  471. */
  472. int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
  473. phys_addr_t pa, unsigned long size)
  474. {
  475. phys_addr_t addr, end;
  476. int ret = 0;
  477. unsigned long pfn;
  478. struct kvm_mmu_memory_cache cache = { 0, };
  479. end = (guest_ipa + size + PAGE_SIZE - 1) & PAGE_MASK;
  480. pfn = __phys_to_pfn(pa);
  481. for (addr = guest_ipa; addr < end; addr += PAGE_SIZE) {
  482. pte_t pte = pfn_pte(pfn, PAGE_S2_DEVICE);
  483. ret = mmu_topup_memory_cache(&cache, 2, 2);
  484. if (ret)
  485. goto out;
  486. spin_lock(&kvm->mmu_lock);
  487. ret = stage2_set_pte(kvm, &cache, addr, &pte, true);
  488. spin_unlock(&kvm->mmu_lock);
  489. if (ret)
  490. goto out;
  491. pfn++;
  492. }
  493. out:
  494. mmu_free_memory_cache(&cache);
  495. return ret;
  496. }
  497. static bool transparent_hugepage_adjust(pfn_t *pfnp, phys_addr_t *ipap)
  498. {
  499. pfn_t pfn = *pfnp;
  500. gfn_t gfn = *ipap >> PAGE_SHIFT;
  501. if (PageTransCompound(pfn_to_page(pfn))) {
  502. unsigned long mask;
  503. /*
  504. * The address we faulted on is backed by a transparent huge
  505. * page. However, because we map the compound huge page and
  506. * not the individual tail page, we need to transfer the
  507. * refcount to the head page. We have to be careful that the
  508. * THP doesn't start to split while we are adjusting the
  509. * refcounts.
  510. *
  511. * We are sure this doesn't happen, because mmu_notifier_retry
  512. * was successful and we are holding the mmu_lock, so if this
  513. * THP is trying to split, it will be blocked in the mmu
  514. * notifier before touching any of the pages, specifically
  515. * before being able to call __split_huge_page_refcount().
  516. *
  517. * We can therefore safely transfer the refcount from PG_tail
  518. * to PG_head and switch the pfn from a tail page to the head
  519. * page accordingly.
  520. */
  521. mask = PTRS_PER_PMD - 1;
  522. VM_BUG_ON((gfn & mask) != (pfn & mask));
  523. if (pfn & mask) {
  524. *ipap &= PMD_MASK;
  525. kvm_release_pfn_clean(pfn);
  526. pfn &= ~mask;
  527. kvm_get_pfn(pfn);
  528. *pfnp = pfn;
  529. }
  530. return true;
  531. }
  532. return false;
  533. }
  534. static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
  535. struct kvm_memory_slot *memslot,
  536. unsigned long fault_status)
  537. {
  538. int ret;
  539. bool write_fault, writable, hugetlb = false, force_pte = false;
  540. unsigned long mmu_seq;
  541. gfn_t gfn = fault_ipa >> PAGE_SHIFT;
  542. unsigned long hva = gfn_to_hva(vcpu->kvm, gfn);
  543. struct kvm *kvm = vcpu->kvm;
  544. struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
  545. struct vm_area_struct *vma;
  546. pfn_t pfn;
  547. write_fault = kvm_is_write_fault(kvm_vcpu_get_hsr(vcpu));
  548. if (fault_status == FSC_PERM && !write_fault) {
  549. kvm_err("Unexpected L2 read permission error\n");
  550. return -EFAULT;
  551. }
  552. /* Let's check if we will get back a huge page backed by hugetlbfs */
  553. down_read(&current->mm->mmap_sem);
  554. vma = find_vma_intersection(current->mm, hva, hva + 1);
  555. if (is_vm_hugetlb_page(vma)) {
  556. hugetlb = true;
  557. gfn = (fault_ipa & PMD_MASK) >> PAGE_SHIFT;
  558. } else {
  559. /*
  560. * Pages belonging to VMAs not aligned to the PMD mapping
  561. * granularity cannot be mapped using block descriptors even
  562. * if the pages belong to a THP for the process, because the
  563. * stage-2 block descriptor will cover more than a single THP
  564. * and we loose atomicity for unmapping, updates, and splits
  565. * of the THP or other pages in the stage-2 block range.
  566. */
  567. if (vma->vm_start & ~PMD_MASK)
  568. force_pte = true;
  569. }
  570. up_read(&current->mm->mmap_sem);
  571. /* We need minimum second+third level pages */
  572. ret = mmu_topup_memory_cache(memcache, 2, KVM_NR_MEM_OBJS);
  573. if (ret)
  574. return ret;
  575. mmu_seq = vcpu->kvm->mmu_notifier_seq;
  576. /*
  577. * Ensure the read of mmu_notifier_seq happens before we call
  578. * gfn_to_pfn_prot (which calls get_user_pages), so that we don't risk
  579. * the page we just got a reference to gets unmapped before we have a
  580. * chance to grab the mmu_lock, which ensure that if the page gets
  581. * unmapped afterwards, the call to kvm_unmap_hva will take it away
  582. * from us again properly. This smp_rmb() interacts with the smp_wmb()
  583. * in kvm_mmu_notifier_invalidate_<page|range_end>.
  584. */
  585. smp_rmb();
  586. pfn = gfn_to_pfn_prot(kvm, gfn, write_fault, &writable);
  587. if (is_error_pfn(pfn))
  588. return -EFAULT;
  589. spin_lock(&kvm->mmu_lock);
  590. if (mmu_notifier_retry(kvm, mmu_seq))
  591. goto out_unlock;
  592. if (!hugetlb && !force_pte)
  593. hugetlb = transparent_hugepage_adjust(&pfn, &fault_ipa);
  594. if (hugetlb) {
  595. pmd_t new_pmd = pfn_pmd(pfn, PAGE_S2);
  596. new_pmd = pmd_mkhuge(new_pmd);
  597. if (writable) {
  598. kvm_set_s2pmd_writable(&new_pmd);
  599. kvm_set_pfn_dirty(pfn);
  600. }
  601. coherent_icache_guest_page(kvm, hva & PMD_MASK, PMD_SIZE);
  602. ret = stage2_set_pmd_huge(kvm, memcache, fault_ipa, &new_pmd);
  603. } else {
  604. pte_t new_pte = pfn_pte(pfn, PAGE_S2);
  605. if (writable) {
  606. kvm_set_s2pte_writable(&new_pte);
  607. kvm_set_pfn_dirty(pfn);
  608. }
  609. coherent_icache_guest_page(kvm, hva, PAGE_SIZE);
  610. ret = stage2_set_pte(kvm, memcache, fault_ipa, &new_pte, false);
  611. }
  612. out_unlock:
  613. spin_unlock(&kvm->mmu_lock);
  614. kvm_release_pfn_clean(pfn);
  615. return ret;
  616. }
  617. /**
  618. * kvm_handle_guest_abort - handles all 2nd stage aborts
  619. * @vcpu: the VCPU pointer
  620. * @run: the kvm_run structure
  621. *
  622. * Any abort that gets to the host is almost guaranteed to be caused by a
  623. * missing second stage translation table entry, which can mean that either the
  624. * guest simply needs more memory and we must allocate an appropriate page or it
  625. * can mean that the guest tried to access I/O memory, which is emulated by user
  626. * space. The distinction is based on the IPA causing the fault and whether this
  627. * memory region has been registered as standard RAM by user space.
  628. */
  629. int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run)
  630. {
  631. unsigned long fault_status;
  632. phys_addr_t fault_ipa;
  633. struct kvm_memory_slot *memslot;
  634. bool is_iabt;
  635. gfn_t gfn;
  636. int ret, idx;
  637. is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
  638. fault_ipa = kvm_vcpu_get_fault_ipa(vcpu);
  639. trace_kvm_guest_fault(*vcpu_pc(vcpu), kvm_vcpu_get_hsr(vcpu),
  640. kvm_vcpu_get_hfar(vcpu), fault_ipa);
  641. /* Check the stage-2 fault is trans. fault or write fault */
  642. fault_status = kvm_vcpu_trap_get_fault(vcpu);
  643. if (fault_status != FSC_FAULT && fault_status != FSC_PERM) {
  644. kvm_err("Unsupported fault status: EC=%#x DFCS=%#lx\n",
  645. kvm_vcpu_trap_get_class(vcpu), fault_status);
  646. return -EFAULT;
  647. }
  648. idx = srcu_read_lock(&vcpu->kvm->srcu);
  649. gfn = fault_ipa >> PAGE_SHIFT;
  650. if (!kvm_is_visible_gfn(vcpu->kvm, gfn)) {
  651. if (is_iabt) {
  652. /* Prefetch Abort on I/O address */
  653. kvm_inject_pabt(vcpu, kvm_vcpu_get_hfar(vcpu));
  654. ret = 1;
  655. goto out_unlock;
  656. }
  657. if (fault_status != FSC_FAULT) {
  658. kvm_err("Unsupported fault status on io memory: %#lx\n",
  659. fault_status);
  660. ret = -EFAULT;
  661. goto out_unlock;
  662. }
  663. /*
  664. * The IPA is reported as [MAX:12], so we need to
  665. * complement it with the bottom 12 bits from the
  666. * faulting VA. This is always 12 bits, irrespective
  667. * of the page size.
  668. */
  669. fault_ipa |= kvm_vcpu_get_hfar(vcpu) & ((1 << 12) - 1);
  670. ret = io_mem_abort(vcpu, run, fault_ipa);
  671. goto out_unlock;
  672. }
  673. memslot = gfn_to_memslot(vcpu->kvm, gfn);
  674. ret = user_mem_abort(vcpu, fault_ipa, memslot, fault_status);
  675. if (ret == 0)
  676. ret = 1;
  677. out_unlock:
  678. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  679. return ret;
  680. }
  681. static void handle_hva_to_gpa(struct kvm *kvm,
  682. unsigned long start,
  683. unsigned long end,
  684. void (*handler)(struct kvm *kvm,
  685. gpa_t gpa, void *data),
  686. void *data)
  687. {
  688. struct kvm_memslots *slots;
  689. struct kvm_memory_slot *memslot;
  690. slots = kvm_memslots(kvm);
  691. /* we only care about the pages that the guest sees */
  692. kvm_for_each_memslot(memslot, slots) {
  693. unsigned long hva_start, hva_end;
  694. gfn_t gfn, gfn_end;
  695. hva_start = max(start, memslot->userspace_addr);
  696. hva_end = min(end, memslot->userspace_addr +
  697. (memslot->npages << PAGE_SHIFT));
  698. if (hva_start >= hva_end)
  699. continue;
  700. /*
  701. * {gfn(page) | page intersects with [hva_start, hva_end)} =
  702. * {gfn_start, gfn_start+1, ..., gfn_end-1}.
  703. */
  704. gfn = hva_to_gfn_memslot(hva_start, memslot);
  705. gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot);
  706. for (; gfn < gfn_end; ++gfn) {
  707. gpa_t gpa = gfn << PAGE_SHIFT;
  708. handler(kvm, gpa, data);
  709. }
  710. }
  711. }
  712. static void kvm_unmap_hva_handler(struct kvm *kvm, gpa_t gpa, void *data)
  713. {
  714. unmap_stage2_range(kvm, gpa, PAGE_SIZE);
  715. }
  716. int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
  717. {
  718. unsigned long end = hva + PAGE_SIZE;
  719. if (!kvm->arch.pgd)
  720. return 0;
  721. trace_kvm_unmap_hva(hva);
  722. handle_hva_to_gpa(kvm, hva, end, &kvm_unmap_hva_handler, NULL);
  723. return 0;
  724. }
  725. int kvm_unmap_hva_range(struct kvm *kvm,
  726. unsigned long start, unsigned long end)
  727. {
  728. if (!kvm->arch.pgd)
  729. return 0;
  730. trace_kvm_unmap_hva_range(start, end);
  731. handle_hva_to_gpa(kvm, start, end, &kvm_unmap_hva_handler, NULL);
  732. return 0;
  733. }
  734. static void kvm_set_spte_handler(struct kvm *kvm, gpa_t gpa, void *data)
  735. {
  736. pte_t *pte = (pte_t *)data;
  737. stage2_set_pte(kvm, NULL, gpa, pte, false);
  738. }
  739. void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
  740. {
  741. unsigned long end = hva + PAGE_SIZE;
  742. pte_t stage2_pte;
  743. if (!kvm->arch.pgd)
  744. return;
  745. trace_kvm_set_spte_hva(hva);
  746. stage2_pte = pfn_pte(pte_pfn(pte), PAGE_S2);
  747. handle_hva_to_gpa(kvm, hva, end, &kvm_set_spte_handler, &stage2_pte);
  748. }
  749. void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu)
  750. {
  751. mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
  752. }
  753. phys_addr_t kvm_mmu_get_httbr(void)
  754. {
  755. return virt_to_phys(hyp_pgd);
  756. }
  757. phys_addr_t kvm_mmu_get_boot_httbr(void)
  758. {
  759. return virt_to_phys(boot_hyp_pgd);
  760. }
  761. phys_addr_t kvm_get_idmap_vector(void)
  762. {
  763. return hyp_idmap_vector;
  764. }
  765. int kvm_mmu_init(void)
  766. {
  767. int err;
  768. hyp_idmap_start = virt_to_phys(__hyp_idmap_text_start);
  769. hyp_idmap_end = virt_to_phys(__hyp_idmap_text_end);
  770. hyp_idmap_vector = virt_to_phys(__kvm_hyp_init);
  771. if ((hyp_idmap_start ^ hyp_idmap_end) & PAGE_MASK) {
  772. /*
  773. * Our init code is crossing a page boundary. Allocate
  774. * a bounce page, copy the code over and use that.
  775. */
  776. size_t len = __hyp_idmap_text_end - __hyp_idmap_text_start;
  777. phys_addr_t phys_base;
  778. init_bounce_page = kmalloc(PAGE_SIZE, GFP_KERNEL);
  779. if (!init_bounce_page) {
  780. kvm_err("Couldn't allocate HYP init bounce page\n");
  781. err = -ENOMEM;
  782. goto out;
  783. }
  784. memcpy(init_bounce_page, __hyp_idmap_text_start, len);
  785. /*
  786. * Warning: the code we just copied to the bounce page
  787. * must be flushed to the point of coherency.
  788. * Otherwise, the data may be sitting in L2, and HYP
  789. * mode won't be able to observe it as it runs with
  790. * caches off at that point.
  791. */
  792. kvm_flush_dcache_to_poc(init_bounce_page, len);
  793. phys_base = virt_to_phys(init_bounce_page);
  794. hyp_idmap_vector += phys_base - hyp_idmap_start;
  795. hyp_idmap_start = phys_base;
  796. hyp_idmap_end = phys_base + len;
  797. kvm_info("Using HYP init bounce page @%lx\n",
  798. (unsigned long)phys_base);
  799. }
  800. hyp_pgd = kzalloc(PTRS_PER_PGD * sizeof(pgd_t), GFP_KERNEL);
  801. boot_hyp_pgd = kzalloc(PTRS_PER_PGD * sizeof(pgd_t), GFP_KERNEL);
  802. if (!hyp_pgd || !boot_hyp_pgd) {
  803. kvm_err("Hyp mode PGD not allocated\n");
  804. err = -ENOMEM;
  805. goto out;
  806. }
  807. /* Create the idmap in the boot page tables */
  808. err = __create_hyp_mappings(boot_hyp_pgd,
  809. hyp_idmap_start, hyp_idmap_end,
  810. __phys_to_pfn(hyp_idmap_start),
  811. PAGE_HYP);
  812. if (err) {
  813. kvm_err("Failed to idmap %lx-%lx\n",
  814. hyp_idmap_start, hyp_idmap_end);
  815. goto out;
  816. }
  817. /* Map the very same page at the trampoline VA */
  818. err = __create_hyp_mappings(boot_hyp_pgd,
  819. TRAMPOLINE_VA, TRAMPOLINE_VA + PAGE_SIZE,
  820. __phys_to_pfn(hyp_idmap_start),
  821. PAGE_HYP);
  822. if (err) {
  823. kvm_err("Failed to map trampoline @%lx into boot HYP pgd\n",
  824. TRAMPOLINE_VA);
  825. goto out;
  826. }
  827. /* Map the same page again into the runtime page tables */
  828. err = __create_hyp_mappings(hyp_pgd,
  829. TRAMPOLINE_VA, TRAMPOLINE_VA + PAGE_SIZE,
  830. __phys_to_pfn(hyp_idmap_start),
  831. PAGE_HYP);
  832. if (err) {
  833. kvm_err("Failed to map trampoline @%lx into runtime HYP pgd\n",
  834. TRAMPOLINE_VA);
  835. goto out;
  836. }
  837. return 0;
  838. out:
  839. free_hyp_pgds();
  840. return err;
  841. }