mmu.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  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. static phys_addr_t kvm_kaddr_to_phys(void *kaddr)
  290. {
  291. if (!is_vmalloc_addr(kaddr)) {
  292. BUG_ON(!virt_addr_valid(kaddr));
  293. return __pa(kaddr);
  294. } else {
  295. return page_to_phys(vmalloc_to_page(kaddr)) +
  296. offset_in_page(kaddr);
  297. }
  298. }
  299. /**
  300. * create_hyp_mappings - duplicate a kernel virtual address range in Hyp mode
  301. * @from: The virtual kernel start address of the range
  302. * @to: The virtual kernel end address of the range (exclusive)
  303. *
  304. * The same virtual address as the kernel virtual address is also used
  305. * in Hyp-mode mapping (modulo HYP_PAGE_OFFSET) to the same underlying
  306. * physical pages.
  307. */
  308. int create_hyp_mappings(void *from, void *to)
  309. {
  310. phys_addr_t phys_addr;
  311. unsigned long virt_addr;
  312. unsigned long start = KERN_TO_HYP((unsigned long)from);
  313. unsigned long end = KERN_TO_HYP((unsigned long)to);
  314. start = start & PAGE_MASK;
  315. end = PAGE_ALIGN(end);
  316. for (virt_addr = start; virt_addr < end; virt_addr += PAGE_SIZE) {
  317. int err;
  318. phys_addr = kvm_kaddr_to_phys(from + virt_addr - start);
  319. err = __create_hyp_mappings(hyp_pgd, virt_addr,
  320. virt_addr + PAGE_SIZE,
  321. __phys_to_pfn(phys_addr),
  322. PAGE_HYP);
  323. if (err)
  324. return err;
  325. }
  326. return 0;
  327. }
  328. /**
  329. * create_hyp_io_mappings - duplicate a kernel IO mapping into Hyp mode
  330. * @from: The kernel start VA of the range
  331. * @to: The kernel end VA of the range (exclusive)
  332. * @phys_addr: The physical start address which gets mapped
  333. *
  334. * The resulting HYP VA is the same as the kernel VA, modulo
  335. * HYP_PAGE_OFFSET.
  336. */
  337. int create_hyp_io_mappings(void *from, void *to, phys_addr_t phys_addr)
  338. {
  339. unsigned long start = KERN_TO_HYP((unsigned long)from);
  340. unsigned long end = KERN_TO_HYP((unsigned long)to);
  341. /* Check for a valid kernel IO mapping */
  342. if (!is_vmalloc_addr(from) || !is_vmalloc_addr(to - 1))
  343. return -EINVAL;
  344. return __create_hyp_mappings(hyp_pgd, start, end,
  345. __phys_to_pfn(phys_addr), PAGE_HYP_DEVICE);
  346. }
  347. /**
  348. * kvm_alloc_stage2_pgd - allocate level-1 table for stage-2 translation.
  349. * @kvm: The KVM struct pointer for the VM.
  350. *
  351. * Allocates the 1st level table only of size defined by S2_PGD_ORDER (can
  352. * support either full 40-bit input addresses or limited to 32-bit input
  353. * addresses). Clears the allocated pages.
  354. *
  355. * Note we don't need locking here as this is only called when the VM is
  356. * created, which can only be done once.
  357. */
  358. int kvm_alloc_stage2_pgd(struct kvm *kvm)
  359. {
  360. pgd_t *pgd;
  361. if (kvm->arch.pgd != NULL) {
  362. kvm_err("kvm_arch already initialized?\n");
  363. return -EINVAL;
  364. }
  365. pgd = (pgd_t *)__get_free_pages(GFP_KERNEL, S2_PGD_ORDER);
  366. if (!pgd)
  367. return -ENOMEM;
  368. memset(pgd, 0, PTRS_PER_S2_PGD * sizeof(pgd_t));
  369. kvm_clean_pgd(pgd);
  370. kvm->arch.pgd = pgd;
  371. return 0;
  372. }
  373. /**
  374. * unmap_stage2_range -- Clear stage2 page table entries to unmap a range
  375. * @kvm: The VM pointer
  376. * @start: The intermediate physical base address of the range to unmap
  377. * @size: The size of the area to unmap
  378. *
  379. * Clear a range of stage-2 mappings, lowering the various ref-counts. Must
  380. * be called while holding mmu_lock (unless for freeing the stage2 pgd before
  381. * destroying the VM), otherwise another faulting VCPU may come in and mess
  382. * with things behind our backs.
  383. */
  384. static void unmap_stage2_range(struct kvm *kvm, phys_addr_t start, u64 size)
  385. {
  386. unmap_range(kvm, kvm->arch.pgd, start, size);
  387. }
  388. /**
  389. * kvm_free_stage2_pgd - free all stage-2 tables
  390. * @kvm: The KVM struct pointer for the VM.
  391. *
  392. * Walks the level-1 page table pointed to by kvm->arch.pgd and frees all
  393. * underlying level-2 and level-3 tables before freeing the actual level-1 table
  394. * and setting the struct pointer to NULL.
  395. *
  396. * Note we don't need locking here as this is only called when the VM is
  397. * destroyed, which can only be done once.
  398. */
  399. void kvm_free_stage2_pgd(struct kvm *kvm)
  400. {
  401. if (kvm->arch.pgd == NULL)
  402. return;
  403. unmap_stage2_range(kvm, 0, KVM_PHYS_SIZE);
  404. free_pages((unsigned long)kvm->arch.pgd, S2_PGD_ORDER);
  405. kvm->arch.pgd = NULL;
  406. }
  407. static pmd_t *stage2_get_pmd(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
  408. phys_addr_t addr)
  409. {
  410. pgd_t *pgd;
  411. pud_t *pud;
  412. pmd_t *pmd;
  413. pgd = kvm->arch.pgd + pgd_index(addr);
  414. pud = pud_offset(pgd, addr);
  415. if (pud_none(*pud)) {
  416. if (!cache)
  417. return NULL;
  418. pmd = mmu_memory_cache_alloc(cache);
  419. pud_populate(NULL, pud, pmd);
  420. get_page(virt_to_page(pud));
  421. }
  422. return pmd_offset(pud, addr);
  423. }
  424. static int stage2_set_pmd_huge(struct kvm *kvm, struct kvm_mmu_memory_cache
  425. *cache, phys_addr_t addr, const pmd_t *new_pmd)
  426. {
  427. pmd_t *pmd, old_pmd;
  428. pmd = stage2_get_pmd(kvm, cache, addr);
  429. VM_BUG_ON(!pmd);
  430. /*
  431. * Mapping in huge pages should only happen through a fault. If a
  432. * page is merged into a transparent huge page, the individual
  433. * subpages of that huge page should be unmapped through MMU
  434. * notifiers before we get here.
  435. *
  436. * Merging of CompoundPages is not supported; they should become
  437. * splitting first, unmapped, merged, and mapped back in on-demand.
  438. */
  439. VM_BUG_ON(pmd_present(*pmd) && pmd_pfn(*pmd) != pmd_pfn(*new_pmd));
  440. old_pmd = *pmd;
  441. kvm_set_pmd(pmd, *new_pmd);
  442. if (pmd_present(old_pmd))
  443. kvm_tlb_flush_vmid_ipa(kvm, addr);
  444. else
  445. get_page(virt_to_page(pmd));
  446. return 0;
  447. }
  448. static int stage2_set_pte(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
  449. phys_addr_t addr, const pte_t *new_pte, bool iomap)
  450. {
  451. pmd_t *pmd;
  452. pte_t *pte, old_pte;
  453. /* Create stage-2 page table mapping - Level 1 */
  454. pmd = stage2_get_pmd(kvm, cache, addr);
  455. if (!pmd) {
  456. /*
  457. * Ignore calls from kvm_set_spte_hva for unallocated
  458. * address ranges.
  459. */
  460. return 0;
  461. }
  462. /* Create stage-2 page mappings - Level 2 */
  463. if (pmd_none(*pmd)) {
  464. if (!cache)
  465. return 0; /* ignore calls from kvm_set_spte_hva */
  466. pte = mmu_memory_cache_alloc(cache);
  467. kvm_clean_pte(pte);
  468. pmd_populate_kernel(NULL, pmd, pte);
  469. get_page(virt_to_page(pmd));
  470. }
  471. pte = pte_offset_kernel(pmd, addr);
  472. if (iomap && pte_present(*pte))
  473. return -EFAULT;
  474. /* Create 2nd stage page table mapping - Level 3 */
  475. old_pte = *pte;
  476. kvm_set_pte(pte, *new_pte);
  477. if (pte_present(old_pte))
  478. kvm_tlb_flush_vmid_ipa(kvm, addr);
  479. else
  480. get_page(virt_to_page(pte));
  481. return 0;
  482. }
  483. /**
  484. * kvm_phys_addr_ioremap - map a device range to guest IPA
  485. *
  486. * @kvm: The KVM pointer
  487. * @guest_ipa: The IPA at which to insert the mapping
  488. * @pa: The physical address of the device
  489. * @size: The size of the mapping
  490. */
  491. int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
  492. phys_addr_t pa, unsigned long size)
  493. {
  494. phys_addr_t addr, end;
  495. int ret = 0;
  496. unsigned long pfn;
  497. struct kvm_mmu_memory_cache cache = { 0, };
  498. end = (guest_ipa + size + PAGE_SIZE - 1) & PAGE_MASK;
  499. pfn = __phys_to_pfn(pa);
  500. for (addr = guest_ipa; addr < end; addr += PAGE_SIZE) {
  501. pte_t pte = pfn_pte(pfn, PAGE_S2_DEVICE);
  502. ret = mmu_topup_memory_cache(&cache, 2, 2);
  503. if (ret)
  504. goto out;
  505. spin_lock(&kvm->mmu_lock);
  506. ret = stage2_set_pte(kvm, &cache, addr, &pte, true);
  507. spin_unlock(&kvm->mmu_lock);
  508. if (ret)
  509. goto out;
  510. pfn++;
  511. }
  512. out:
  513. mmu_free_memory_cache(&cache);
  514. return ret;
  515. }
  516. static bool transparent_hugepage_adjust(pfn_t *pfnp, phys_addr_t *ipap)
  517. {
  518. pfn_t pfn = *pfnp;
  519. gfn_t gfn = *ipap >> PAGE_SHIFT;
  520. if (PageTransCompound(pfn_to_page(pfn))) {
  521. unsigned long mask;
  522. /*
  523. * The address we faulted on is backed by a transparent huge
  524. * page. However, because we map the compound huge page and
  525. * not the individual tail page, we need to transfer the
  526. * refcount to the head page. We have to be careful that the
  527. * THP doesn't start to split while we are adjusting the
  528. * refcounts.
  529. *
  530. * We are sure this doesn't happen, because mmu_notifier_retry
  531. * was successful and we are holding the mmu_lock, so if this
  532. * THP is trying to split, it will be blocked in the mmu
  533. * notifier before touching any of the pages, specifically
  534. * before being able to call __split_huge_page_refcount().
  535. *
  536. * We can therefore safely transfer the refcount from PG_tail
  537. * to PG_head and switch the pfn from a tail page to the head
  538. * page accordingly.
  539. */
  540. mask = PTRS_PER_PMD - 1;
  541. VM_BUG_ON((gfn & mask) != (pfn & mask));
  542. if (pfn & mask) {
  543. *ipap &= PMD_MASK;
  544. kvm_release_pfn_clean(pfn);
  545. pfn &= ~mask;
  546. kvm_get_pfn(pfn);
  547. *pfnp = pfn;
  548. }
  549. return true;
  550. }
  551. return false;
  552. }
  553. static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
  554. struct kvm_memory_slot *memslot,
  555. unsigned long fault_status)
  556. {
  557. int ret;
  558. bool write_fault, writable, hugetlb = false, force_pte = false;
  559. unsigned long mmu_seq;
  560. gfn_t gfn = fault_ipa >> PAGE_SHIFT;
  561. unsigned long hva = gfn_to_hva(vcpu->kvm, gfn);
  562. struct kvm *kvm = vcpu->kvm;
  563. struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
  564. struct vm_area_struct *vma;
  565. pfn_t pfn;
  566. write_fault = kvm_is_write_fault(kvm_vcpu_get_hsr(vcpu));
  567. if (fault_status == FSC_PERM && !write_fault) {
  568. kvm_err("Unexpected L2 read permission error\n");
  569. return -EFAULT;
  570. }
  571. /* Let's check if we will get back a huge page backed by hugetlbfs */
  572. down_read(&current->mm->mmap_sem);
  573. vma = find_vma_intersection(current->mm, hva, hva + 1);
  574. if (is_vm_hugetlb_page(vma)) {
  575. hugetlb = true;
  576. gfn = (fault_ipa & PMD_MASK) >> PAGE_SHIFT;
  577. } else {
  578. /*
  579. * Pages belonging to memslots that don't have the same
  580. * alignment for userspace and IPA cannot be mapped using
  581. * block descriptors even if the pages belong to a THP for
  582. * the process, because the stage-2 block descriptor will
  583. * cover more than a single THP and we loose atomicity for
  584. * unmapping, updates, and splits of the THP or other pages
  585. * in the stage-2 block range.
  586. */
  587. if ((memslot->userspace_addr & ~PMD_MASK) !=
  588. ((memslot->base_gfn << PAGE_SHIFT) & ~PMD_MASK))
  589. force_pte = true;
  590. }
  591. up_read(&current->mm->mmap_sem);
  592. /* We need minimum second+third level pages */
  593. ret = mmu_topup_memory_cache(memcache, 2, KVM_NR_MEM_OBJS);
  594. if (ret)
  595. return ret;
  596. mmu_seq = vcpu->kvm->mmu_notifier_seq;
  597. /*
  598. * Ensure the read of mmu_notifier_seq happens before we call
  599. * gfn_to_pfn_prot (which calls get_user_pages), so that we don't risk
  600. * the page we just got a reference to gets unmapped before we have a
  601. * chance to grab the mmu_lock, which ensure that if the page gets
  602. * unmapped afterwards, the call to kvm_unmap_hva will take it away
  603. * from us again properly. This smp_rmb() interacts with the smp_wmb()
  604. * in kvm_mmu_notifier_invalidate_<page|range_end>.
  605. */
  606. smp_rmb();
  607. pfn = gfn_to_pfn_prot(kvm, gfn, write_fault, &writable);
  608. if (is_error_pfn(pfn))
  609. return -EFAULT;
  610. spin_lock(&kvm->mmu_lock);
  611. if (mmu_notifier_retry(kvm, mmu_seq))
  612. goto out_unlock;
  613. if (!hugetlb && !force_pte)
  614. hugetlb = transparent_hugepage_adjust(&pfn, &fault_ipa);
  615. if (hugetlb) {
  616. pmd_t new_pmd = pfn_pmd(pfn, PAGE_S2);
  617. new_pmd = pmd_mkhuge(new_pmd);
  618. if (writable) {
  619. kvm_set_s2pmd_writable(&new_pmd);
  620. kvm_set_pfn_dirty(pfn);
  621. }
  622. coherent_icache_guest_page(kvm, hva & PMD_MASK, PMD_SIZE);
  623. ret = stage2_set_pmd_huge(kvm, memcache, fault_ipa, &new_pmd);
  624. } else {
  625. pte_t new_pte = pfn_pte(pfn, PAGE_S2);
  626. if (writable) {
  627. kvm_set_s2pte_writable(&new_pte);
  628. kvm_set_pfn_dirty(pfn);
  629. }
  630. coherent_icache_guest_page(kvm, hva, PAGE_SIZE);
  631. ret = stage2_set_pte(kvm, memcache, fault_ipa, &new_pte, false);
  632. }
  633. out_unlock:
  634. spin_unlock(&kvm->mmu_lock);
  635. kvm_release_pfn_clean(pfn);
  636. return ret;
  637. }
  638. /**
  639. * kvm_handle_guest_abort - handles all 2nd stage aborts
  640. * @vcpu: the VCPU pointer
  641. * @run: the kvm_run structure
  642. *
  643. * Any abort that gets to the host is almost guaranteed to be caused by a
  644. * missing second stage translation table entry, which can mean that either the
  645. * guest simply needs more memory and we must allocate an appropriate page or it
  646. * can mean that the guest tried to access I/O memory, which is emulated by user
  647. * space. The distinction is based on the IPA causing the fault and whether this
  648. * memory region has been registered as standard RAM by user space.
  649. */
  650. int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run)
  651. {
  652. unsigned long fault_status;
  653. phys_addr_t fault_ipa;
  654. struct kvm_memory_slot *memslot;
  655. bool is_iabt;
  656. gfn_t gfn;
  657. int ret, idx;
  658. is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
  659. fault_ipa = kvm_vcpu_get_fault_ipa(vcpu);
  660. trace_kvm_guest_fault(*vcpu_pc(vcpu), kvm_vcpu_get_hsr(vcpu),
  661. kvm_vcpu_get_hfar(vcpu), fault_ipa);
  662. /* Check the stage-2 fault is trans. fault or write fault */
  663. fault_status = kvm_vcpu_trap_get_fault(vcpu);
  664. if (fault_status != FSC_FAULT && fault_status != FSC_PERM) {
  665. kvm_err("Unsupported fault status: EC=%#x DFCS=%#lx\n",
  666. kvm_vcpu_trap_get_class(vcpu), fault_status);
  667. return -EFAULT;
  668. }
  669. idx = srcu_read_lock(&vcpu->kvm->srcu);
  670. gfn = fault_ipa >> PAGE_SHIFT;
  671. if (!kvm_is_visible_gfn(vcpu->kvm, gfn)) {
  672. if (is_iabt) {
  673. /* Prefetch Abort on I/O address */
  674. kvm_inject_pabt(vcpu, kvm_vcpu_get_hfar(vcpu));
  675. ret = 1;
  676. goto out_unlock;
  677. }
  678. if (fault_status != FSC_FAULT) {
  679. kvm_err("Unsupported fault status on io memory: %#lx\n",
  680. fault_status);
  681. ret = -EFAULT;
  682. goto out_unlock;
  683. }
  684. /*
  685. * The IPA is reported as [MAX:12], so we need to
  686. * complement it with the bottom 12 bits from the
  687. * faulting VA. This is always 12 bits, irrespective
  688. * of the page size.
  689. */
  690. fault_ipa |= kvm_vcpu_get_hfar(vcpu) & ((1 << 12) - 1);
  691. ret = io_mem_abort(vcpu, run, fault_ipa);
  692. goto out_unlock;
  693. }
  694. memslot = gfn_to_memslot(vcpu->kvm, gfn);
  695. ret = user_mem_abort(vcpu, fault_ipa, memslot, fault_status);
  696. if (ret == 0)
  697. ret = 1;
  698. out_unlock:
  699. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  700. return ret;
  701. }
  702. static void handle_hva_to_gpa(struct kvm *kvm,
  703. unsigned long start,
  704. unsigned long end,
  705. void (*handler)(struct kvm *kvm,
  706. gpa_t gpa, void *data),
  707. void *data)
  708. {
  709. struct kvm_memslots *slots;
  710. struct kvm_memory_slot *memslot;
  711. slots = kvm_memslots(kvm);
  712. /* we only care about the pages that the guest sees */
  713. kvm_for_each_memslot(memslot, slots) {
  714. unsigned long hva_start, hva_end;
  715. gfn_t gfn, gfn_end;
  716. hva_start = max(start, memslot->userspace_addr);
  717. hva_end = min(end, memslot->userspace_addr +
  718. (memslot->npages << PAGE_SHIFT));
  719. if (hva_start >= hva_end)
  720. continue;
  721. /*
  722. * {gfn(page) | page intersects with [hva_start, hva_end)} =
  723. * {gfn_start, gfn_start+1, ..., gfn_end-1}.
  724. */
  725. gfn = hva_to_gfn_memslot(hva_start, memslot);
  726. gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot);
  727. for (; gfn < gfn_end; ++gfn) {
  728. gpa_t gpa = gfn << PAGE_SHIFT;
  729. handler(kvm, gpa, data);
  730. }
  731. }
  732. }
  733. static void kvm_unmap_hva_handler(struct kvm *kvm, gpa_t gpa, void *data)
  734. {
  735. unmap_stage2_range(kvm, gpa, PAGE_SIZE);
  736. }
  737. int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
  738. {
  739. unsigned long end = hva + PAGE_SIZE;
  740. if (!kvm->arch.pgd)
  741. return 0;
  742. trace_kvm_unmap_hva(hva);
  743. handle_hva_to_gpa(kvm, hva, end, &kvm_unmap_hva_handler, NULL);
  744. return 0;
  745. }
  746. int kvm_unmap_hva_range(struct kvm *kvm,
  747. unsigned long start, unsigned long end)
  748. {
  749. if (!kvm->arch.pgd)
  750. return 0;
  751. trace_kvm_unmap_hva_range(start, end);
  752. handle_hva_to_gpa(kvm, start, end, &kvm_unmap_hva_handler, NULL);
  753. return 0;
  754. }
  755. static void kvm_set_spte_handler(struct kvm *kvm, gpa_t gpa, void *data)
  756. {
  757. pte_t *pte = (pte_t *)data;
  758. stage2_set_pte(kvm, NULL, gpa, pte, false);
  759. }
  760. void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
  761. {
  762. unsigned long end = hva + PAGE_SIZE;
  763. pte_t stage2_pte;
  764. if (!kvm->arch.pgd)
  765. return;
  766. trace_kvm_set_spte_hva(hva);
  767. stage2_pte = pfn_pte(pte_pfn(pte), PAGE_S2);
  768. handle_hva_to_gpa(kvm, hva, end, &kvm_set_spte_handler, &stage2_pte);
  769. }
  770. void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu)
  771. {
  772. mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
  773. }
  774. phys_addr_t kvm_mmu_get_httbr(void)
  775. {
  776. return virt_to_phys(hyp_pgd);
  777. }
  778. phys_addr_t kvm_mmu_get_boot_httbr(void)
  779. {
  780. return virt_to_phys(boot_hyp_pgd);
  781. }
  782. phys_addr_t kvm_get_idmap_vector(void)
  783. {
  784. return hyp_idmap_vector;
  785. }
  786. int kvm_mmu_init(void)
  787. {
  788. int err;
  789. hyp_idmap_start = kvm_virt_to_phys(__hyp_idmap_text_start);
  790. hyp_idmap_end = kvm_virt_to_phys(__hyp_idmap_text_end);
  791. hyp_idmap_vector = kvm_virt_to_phys(__kvm_hyp_init);
  792. if ((hyp_idmap_start ^ hyp_idmap_end) & PAGE_MASK) {
  793. /*
  794. * Our init code is crossing a page boundary. Allocate
  795. * a bounce page, copy the code over and use that.
  796. */
  797. size_t len = __hyp_idmap_text_end - __hyp_idmap_text_start;
  798. phys_addr_t phys_base;
  799. init_bounce_page = kmalloc(PAGE_SIZE, GFP_KERNEL);
  800. if (!init_bounce_page) {
  801. kvm_err("Couldn't allocate HYP init bounce page\n");
  802. err = -ENOMEM;
  803. goto out;
  804. }
  805. memcpy(init_bounce_page, __hyp_idmap_text_start, len);
  806. /*
  807. * Warning: the code we just copied to the bounce page
  808. * must be flushed to the point of coherency.
  809. * Otherwise, the data may be sitting in L2, and HYP
  810. * mode won't be able to observe it as it runs with
  811. * caches off at that point.
  812. */
  813. kvm_flush_dcache_to_poc(init_bounce_page, len);
  814. phys_base = kvm_virt_to_phys(init_bounce_page);
  815. hyp_idmap_vector += phys_base - hyp_idmap_start;
  816. hyp_idmap_start = phys_base;
  817. hyp_idmap_end = phys_base + len;
  818. kvm_info("Using HYP init bounce page @%lx\n",
  819. (unsigned long)phys_base);
  820. }
  821. hyp_pgd = kzalloc(PTRS_PER_PGD * sizeof(pgd_t), GFP_KERNEL);
  822. boot_hyp_pgd = kzalloc(PTRS_PER_PGD * sizeof(pgd_t), GFP_KERNEL);
  823. if (!hyp_pgd || !boot_hyp_pgd) {
  824. kvm_err("Hyp mode PGD not allocated\n");
  825. err = -ENOMEM;
  826. goto out;
  827. }
  828. /* Create the idmap in the boot page tables */
  829. err = __create_hyp_mappings(boot_hyp_pgd,
  830. hyp_idmap_start, hyp_idmap_end,
  831. __phys_to_pfn(hyp_idmap_start),
  832. PAGE_HYP);
  833. if (err) {
  834. kvm_err("Failed to idmap %lx-%lx\n",
  835. hyp_idmap_start, hyp_idmap_end);
  836. goto out;
  837. }
  838. /* Map the very same page at the trampoline VA */
  839. err = __create_hyp_mappings(boot_hyp_pgd,
  840. TRAMPOLINE_VA, TRAMPOLINE_VA + PAGE_SIZE,
  841. __phys_to_pfn(hyp_idmap_start),
  842. PAGE_HYP);
  843. if (err) {
  844. kvm_err("Failed to map trampoline @%lx into boot HYP pgd\n",
  845. TRAMPOLINE_VA);
  846. goto out;
  847. }
  848. /* Map the same page again into the runtime page tables */
  849. err = __create_hyp_mappings(hyp_pgd,
  850. TRAMPOLINE_VA, TRAMPOLINE_VA + PAGE_SIZE,
  851. __phys_to_pfn(hyp_idmap_start),
  852. PAGE_HYP);
  853. if (err) {
  854. kvm_err("Failed to map trampoline @%lx into runtime HYP pgd\n",
  855. TRAMPOLINE_VA);
  856. goto out;
  857. }
  858. return 0;
  859. out:
  860. free_hyp_pgds();
  861. return err;
  862. }