mmu.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413
  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 hyp_pgd_order get_order(PTRS_PER_PGD * sizeof(pgd_t))
  40. #define kvm_pmd_huge(_x) (pmd_huge(_x) || pmd_trans_huge(_x))
  41. static void kvm_tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa)
  42. {
  43. /*
  44. * This function also gets called when dealing with HYP page
  45. * tables. As HYP doesn't have an associated struct kvm (and
  46. * the HYP page tables are fairly static), we don't do
  47. * anything there.
  48. */
  49. if (kvm)
  50. kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, kvm, ipa);
  51. }
  52. static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
  53. int min, int max)
  54. {
  55. void *page;
  56. BUG_ON(max > KVM_NR_MEM_OBJS);
  57. if (cache->nobjs >= min)
  58. return 0;
  59. while (cache->nobjs < max) {
  60. page = (void *)__get_free_page(PGALLOC_GFP);
  61. if (!page)
  62. return -ENOMEM;
  63. cache->objects[cache->nobjs++] = page;
  64. }
  65. return 0;
  66. }
  67. static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
  68. {
  69. while (mc->nobjs)
  70. free_page((unsigned long)mc->objects[--mc->nobjs]);
  71. }
  72. static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc)
  73. {
  74. void *p;
  75. BUG_ON(!mc || !mc->nobjs);
  76. p = mc->objects[--mc->nobjs];
  77. return p;
  78. }
  79. static void clear_pgd_entry(struct kvm *kvm, pgd_t *pgd, phys_addr_t addr)
  80. {
  81. pud_t *pud_table __maybe_unused = pud_offset(pgd, 0);
  82. pgd_clear(pgd);
  83. kvm_tlb_flush_vmid_ipa(kvm, addr);
  84. pud_free(NULL, pud_table);
  85. put_page(virt_to_page(pgd));
  86. }
  87. static void clear_pud_entry(struct kvm *kvm, pud_t *pud, phys_addr_t addr)
  88. {
  89. pmd_t *pmd_table = pmd_offset(pud, 0);
  90. VM_BUG_ON(pud_huge(*pud));
  91. pud_clear(pud);
  92. kvm_tlb_flush_vmid_ipa(kvm, addr);
  93. pmd_free(NULL, pmd_table);
  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. pte_t *pte_table = pte_offset_kernel(pmd, 0);
  99. VM_BUG_ON(kvm_pmd_huge(*pmd));
  100. pmd_clear(pmd);
  101. kvm_tlb_flush_vmid_ipa(kvm, addr);
  102. pte_free_kernel(NULL, pte_table);
  103. put_page(virt_to_page(pmd));
  104. }
  105. static void unmap_ptes(struct kvm *kvm, pmd_t *pmd,
  106. phys_addr_t addr, phys_addr_t end)
  107. {
  108. phys_addr_t start_addr = addr;
  109. pte_t *pte, *start_pte;
  110. start_pte = pte = pte_offset_kernel(pmd, addr);
  111. do {
  112. if (!pte_none(*pte)) {
  113. kvm_set_pte(pte, __pte(0));
  114. put_page(virt_to_page(pte));
  115. kvm_tlb_flush_vmid_ipa(kvm, addr);
  116. }
  117. } while (pte++, addr += PAGE_SIZE, addr != end);
  118. if (kvm_pte_table_empty(kvm, start_pte))
  119. clear_pmd_entry(kvm, pmd, start_addr);
  120. }
  121. static void unmap_pmds(struct kvm *kvm, pud_t *pud,
  122. phys_addr_t addr, phys_addr_t end)
  123. {
  124. phys_addr_t next, start_addr = addr;
  125. pmd_t *pmd, *start_pmd;
  126. start_pmd = pmd = pmd_offset(pud, addr);
  127. do {
  128. next = kvm_pmd_addr_end(addr, end);
  129. if (!pmd_none(*pmd)) {
  130. if (kvm_pmd_huge(*pmd)) {
  131. pmd_clear(pmd);
  132. kvm_tlb_flush_vmid_ipa(kvm, addr);
  133. put_page(virt_to_page(pmd));
  134. } else {
  135. unmap_ptes(kvm, pmd, addr, next);
  136. }
  137. }
  138. } while (pmd++, addr = next, addr != end);
  139. if (kvm_pmd_table_empty(kvm, start_pmd))
  140. clear_pud_entry(kvm, pud, start_addr);
  141. }
  142. static void unmap_puds(struct kvm *kvm, pgd_t *pgd,
  143. phys_addr_t addr, phys_addr_t end)
  144. {
  145. phys_addr_t next, start_addr = addr;
  146. pud_t *pud, *start_pud;
  147. start_pud = pud = pud_offset(pgd, addr);
  148. do {
  149. next = kvm_pud_addr_end(addr, end);
  150. if (!pud_none(*pud)) {
  151. if (pud_huge(*pud)) {
  152. pud_clear(pud);
  153. kvm_tlb_flush_vmid_ipa(kvm, addr);
  154. put_page(virt_to_page(pud));
  155. } else {
  156. unmap_pmds(kvm, pud, addr, next);
  157. }
  158. }
  159. } while (pud++, addr = next, addr != end);
  160. if (kvm_pud_table_empty(kvm, start_pud))
  161. clear_pgd_entry(kvm, pgd, start_addr);
  162. }
  163. static void unmap_range(struct kvm *kvm, pgd_t *pgdp,
  164. phys_addr_t start, u64 size)
  165. {
  166. pgd_t *pgd;
  167. phys_addr_t addr = start, end = start + size;
  168. phys_addr_t next;
  169. pgd = pgdp + pgd_index(addr);
  170. do {
  171. next = kvm_pgd_addr_end(addr, end);
  172. if (!pgd_none(*pgd))
  173. unmap_puds(kvm, pgd, addr, next);
  174. } while (pgd++, addr = next, addr != end);
  175. }
  176. static void stage2_flush_ptes(struct kvm *kvm, pmd_t *pmd,
  177. phys_addr_t addr, phys_addr_t end)
  178. {
  179. pte_t *pte;
  180. pte = pte_offset_kernel(pmd, addr);
  181. do {
  182. if (!pte_none(*pte)) {
  183. hva_t hva = gfn_to_hva(kvm, addr >> PAGE_SHIFT);
  184. kvm_flush_dcache_to_poc((void*)hva, PAGE_SIZE);
  185. }
  186. } while (pte++, addr += PAGE_SIZE, addr != end);
  187. }
  188. static void stage2_flush_pmds(struct kvm *kvm, pud_t *pud,
  189. phys_addr_t addr, phys_addr_t end)
  190. {
  191. pmd_t *pmd;
  192. phys_addr_t next;
  193. pmd = pmd_offset(pud, addr);
  194. do {
  195. next = kvm_pmd_addr_end(addr, end);
  196. if (!pmd_none(*pmd)) {
  197. if (kvm_pmd_huge(*pmd)) {
  198. hva_t hva = gfn_to_hva(kvm, addr >> PAGE_SHIFT);
  199. kvm_flush_dcache_to_poc((void*)hva, PMD_SIZE);
  200. } else {
  201. stage2_flush_ptes(kvm, pmd, addr, next);
  202. }
  203. }
  204. } while (pmd++, addr = next, addr != end);
  205. }
  206. static void stage2_flush_puds(struct kvm *kvm, pgd_t *pgd,
  207. phys_addr_t addr, phys_addr_t end)
  208. {
  209. pud_t *pud;
  210. phys_addr_t next;
  211. pud = pud_offset(pgd, addr);
  212. do {
  213. next = kvm_pud_addr_end(addr, end);
  214. if (!pud_none(*pud)) {
  215. if (pud_huge(*pud)) {
  216. hva_t hva = gfn_to_hva(kvm, addr >> PAGE_SHIFT);
  217. kvm_flush_dcache_to_poc((void*)hva, PUD_SIZE);
  218. } else {
  219. stage2_flush_pmds(kvm, pud, addr, next);
  220. }
  221. }
  222. } while (pud++, addr = next, addr != end);
  223. }
  224. static void stage2_flush_memslot(struct kvm *kvm,
  225. struct kvm_memory_slot *memslot)
  226. {
  227. phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
  228. phys_addr_t end = addr + PAGE_SIZE * memslot->npages;
  229. phys_addr_t next;
  230. pgd_t *pgd;
  231. pgd = kvm->arch.pgd + pgd_index(addr);
  232. do {
  233. next = kvm_pgd_addr_end(addr, end);
  234. stage2_flush_puds(kvm, pgd, addr, next);
  235. } while (pgd++, addr = next, addr != end);
  236. }
  237. /**
  238. * stage2_flush_vm - Invalidate cache for pages mapped in stage 2
  239. * @kvm: The struct kvm pointer
  240. *
  241. * Go through the stage 2 page tables and invalidate any cache lines
  242. * backing memory already mapped to the VM.
  243. */
  244. void stage2_flush_vm(struct kvm *kvm)
  245. {
  246. struct kvm_memslots *slots;
  247. struct kvm_memory_slot *memslot;
  248. int idx;
  249. idx = srcu_read_lock(&kvm->srcu);
  250. spin_lock(&kvm->mmu_lock);
  251. slots = kvm_memslots(kvm);
  252. kvm_for_each_memslot(memslot, slots)
  253. stage2_flush_memslot(kvm, memslot);
  254. spin_unlock(&kvm->mmu_lock);
  255. srcu_read_unlock(&kvm->srcu, idx);
  256. }
  257. /**
  258. * free_boot_hyp_pgd - free HYP boot page tables
  259. *
  260. * Free the HYP boot page tables. The bounce page is also freed.
  261. */
  262. void free_boot_hyp_pgd(void)
  263. {
  264. mutex_lock(&kvm_hyp_pgd_mutex);
  265. if (boot_hyp_pgd) {
  266. unmap_range(NULL, boot_hyp_pgd, hyp_idmap_start, PAGE_SIZE);
  267. unmap_range(NULL, boot_hyp_pgd, TRAMPOLINE_VA, PAGE_SIZE);
  268. free_pages((unsigned long)boot_hyp_pgd, hyp_pgd_order);
  269. boot_hyp_pgd = NULL;
  270. }
  271. if (hyp_pgd)
  272. unmap_range(NULL, hyp_pgd, TRAMPOLINE_VA, PAGE_SIZE);
  273. free_page((unsigned long)init_bounce_page);
  274. init_bounce_page = NULL;
  275. mutex_unlock(&kvm_hyp_pgd_mutex);
  276. }
  277. /**
  278. * free_hyp_pgds - free Hyp-mode page tables
  279. *
  280. * Assumes hyp_pgd is a page table used strictly in Hyp-mode and
  281. * therefore contains either mappings in the kernel memory area (above
  282. * PAGE_OFFSET), or device mappings in the vmalloc range (from
  283. * VMALLOC_START to VMALLOC_END).
  284. *
  285. * boot_hyp_pgd should only map two pages for the init code.
  286. */
  287. void free_hyp_pgds(void)
  288. {
  289. unsigned long addr;
  290. free_boot_hyp_pgd();
  291. mutex_lock(&kvm_hyp_pgd_mutex);
  292. if (hyp_pgd) {
  293. for (addr = PAGE_OFFSET; virt_addr_valid(addr); addr += PGDIR_SIZE)
  294. unmap_range(NULL, hyp_pgd, KERN_TO_HYP(addr), PGDIR_SIZE);
  295. for (addr = VMALLOC_START; is_vmalloc_addr((void*)addr); addr += PGDIR_SIZE)
  296. unmap_range(NULL, hyp_pgd, KERN_TO_HYP(addr), PGDIR_SIZE);
  297. free_pages((unsigned long)hyp_pgd, hyp_pgd_order);
  298. hyp_pgd = NULL;
  299. }
  300. mutex_unlock(&kvm_hyp_pgd_mutex);
  301. }
  302. static void create_hyp_pte_mappings(pmd_t *pmd, unsigned long start,
  303. unsigned long end, unsigned long pfn,
  304. pgprot_t prot)
  305. {
  306. pte_t *pte;
  307. unsigned long addr;
  308. addr = start;
  309. do {
  310. pte = pte_offset_kernel(pmd, addr);
  311. kvm_set_pte(pte, pfn_pte(pfn, prot));
  312. get_page(virt_to_page(pte));
  313. kvm_flush_dcache_to_poc(pte, sizeof(*pte));
  314. pfn++;
  315. } while (addr += PAGE_SIZE, addr != end);
  316. }
  317. static int create_hyp_pmd_mappings(pud_t *pud, unsigned long start,
  318. unsigned long end, unsigned long pfn,
  319. pgprot_t prot)
  320. {
  321. pmd_t *pmd;
  322. pte_t *pte;
  323. unsigned long addr, next;
  324. addr = start;
  325. do {
  326. pmd = pmd_offset(pud, addr);
  327. BUG_ON(pmd_sect(*pmd));
  328. if (pmd_none(*pmd)) {
  329. pte = pte_alloc_one_kernel(NULL, addr);
  330. if (!pte) {
  331. kvm_err("Cannot allocate Hyp pte\n");
  332. return -ENOMEM;
  333. }
  334. pmd_populate_kernel(NULL, pmd, pte);
  335. get_page(virt_to_page(pmd));
  336. kvm_flush_dcache_to_poc(pmd, sizeof(*pmd));
  337. }
  338. next = pmd_addr_end(addr, end);
  339. create_hyp_pte_mappings(pmd, addr, next, pfn, prot);
  340. pfn += (next - addr) >> PAGE_SHIFT;
  341. } while (addr = next, addr != end);
  342. return 0;
  343. }
  344. static int create_hyp_pud_mappings(pgd_t *pgd, unsigned long start,
  345. unsigned long end, unsigned long pfn,
  346. pgprot_t prot)
  347. {
  348. pud_t *pud;
  349. pmd_t *pmd;
  350. unsigned long addr, next;
  351. int ret;
  352. addr = start;
  353. do {
  354. pud = pud_offset(pgd, addr);
  355. if (pud_none_or_clear_bad(pud)) {
  356. pmd = pmd_alloc_one(NULL, addr);
  357. if (!pmd) {
  358. kvm_err("Cannot allocate Hyp pmd\n");
  359. return -ENOMEM;
  360. }
  361. pud_populate(NULL, pud, pmd);
  362. get_page(virt_to_page(pud));
  363. kvm_flush_dcache_to_poc(pud, sizeof(*pud));
  364. }
  365. next = pud_addr_end(addr, end);
  366. ret = create_hyp_pmd_mappings(pud, addr, next, pfn, prot);
  367. if (ret)
  368. return ret;
  369. pfn += (next - addr) >> PAGE_SHIFT;
  370. } while (addr = next, addr != end);
  371. return 0;
  372. }
  373. static int __create_hyp_mappings(pgd_t *pgdp,
  374. unsigned long start, unsigned long end,
  375. unsigned long pfn, pgprot_t prot)
  376. {
  377. pgd_t *pgd;
  378. pud_t *pud;
  379. unsigned long addr, next;
  380. int err = 0;
  381. mutex_lock(&kvm_hyp_pgd_mutex);
  382. addr = start & PAGE_MASK;
  383. end = PAGE_ALIGN(end);
  384. do {
  385. pgd = pgdp + pgd_index(addr);
  386. if (pgd_none(*pgd)) {
  387. pud = pud_alloc_one(NULL, addr);
  388. if (!pud) {
  389. kvm_err("Cannot allocate Hyp pud\n");
  390. err = -ENOMEM;
  391. goto out;
  392. }
  393. pgd_populate(NULL, pgd, pud);
  394. get_page(virt_to_page(pgd));
  395. kvm_flush_dcache_to_poc(pgd, sizeof(*pgd));
  396. }
  397. next = pgd_addr_end(addr, end);
  398. err = create_hyp_pud_mappings(pgd, addr, next, pfn, prot);
  399. if (err)
  400. goto out;
  401. pfn += (next - addr) >> PAGE_SHIFT;
  402. } while (addr = next, addr != end);
  403. out:
  404. mutex_unlock(&kvm_hyp_pgd_mutex);
  405. return err;
  406. }
  407. static phys_addr_t kvm_kaddr_to_phys(void *kaddr)
  408. {
  409. if (!is_vmalloc_addr(kaddr)) {
  410. BUG_ON(!virt_addr_valid(kaddr));
  411. return __pa(kaddr);
  412. } else {
  413. return page_to_phys(vmalloc_to_page(kaddr)) +
  414. offset_in_page(kaddr);
  415. }
  416. }
  417. /**
  418. * create_hyp_mappings - duplicate a kernel virtual address range in Hyp mode
  419. * @from: The virtual kernel start address of the range
  420. * @to: The virtual kernel end address of the range (exclusive)
  421. *
  422. * The same virtual address as the kernel virtual address is also used
  423. * in Hyp-mode mapping (modulo HYP_PAGE_OFFSET) to the same underlying
  424. * physical pages.
  425. */
  426. int create_hyp_mappings(void *from, void *to)
  427. {
  428. phys_addr_t phys_addr;
  429. unsigned long virt_addr;
  430. unsigned long start = KERN_TO_HYP((unsigned long)from);
  431. unsigned long end = KERN_TO_HYP((unsigned long)to);
  432. start = start & PAGE_MASK;
  433. end = PAGE_ALIGN(end);
  434. for (virt_addr = start; virt_addr < end; virt_addr += PAGE_SIZE) {
  435. int err;
  436. phys_addr = kvm_kaddr_to_phys(from + virt_addr - start);
  437. err = __create_hyp_mappings(hyp_pgd, virt_addr,
  438. virt_addr + PAGE_SIZE,
  439. __phys_to_pfn(phys_addr),
  440. PAGE_HYP);
  441. if (err)
  442. return err;
  443. }
  444. return 0;
  445. }
  446. /**
  447. * create_hyp_io_mappings - duplicate a kernel IO mapping into Hyp mode
  448. * @from: The kernel start VA of the range
  449. * @to: The kernel end VA of the range (exclusive)
  450. * @phys_addr: The physical start address which gets mapped
  451. *
  452. * The resulting HYP VA is the same as the kernel VA, modulo
  453. * HYP_PAGE_OFFSET.
  454. */
  455. int create_hyp_io_mappings(void *from, void *to, phys_addr_t phys_addr)
  456. {
  457. unsigned long start = KERN_TO_HYP((unsigned long)from);
  458. unsigned long end = KERN_TO_HYP((unsigned long)to);
  459. /* Check for a valid kernel IO mapping */
  460. if (!is_vmalloc_addr(from) || !is_vmalloc_addr(to - 1))
  461. return -EINVAL;
  462. return __create_hyp_mappings(hyp_pgd, start, end,
  463. __phys_to_pfn(phys_addr), PAGE_HYP_DEVICE);
  464. }
  465. /**
  466. * kvm_alloc_stage2_pgd - allocate level-1 table for stage-2 translation.
  467. * @kvm: The KVM struct pointer for the VM.
  468. *
  469. * Allocates the 1st level table only of size defined by S2_PGD_ORDER (can
  470. * support either full 40-bit input addresses or limited to 32-bit input
  471. * addresses). Clears the allocated pages.
  472. *
  473. * Note we don't need locking here as this is only called when the VM is
  474. * created, which can only be done once.
  475. */
  476. int kvm_alloc_stage2_pgd(struct kvm *kvm)
  477. {
  478. int ret;
  479. pgd_t *pgd;
  480. if (kvm->arch.pgd != NULL) {
  481. kvm_err("kvm_arch already initialized?\n");
  482. return -EINVAL;
  483. }
  484. if (KVM_PREALLOC_LEVEL > 0) {
  485. /*
  486. * Allocate fake pgd for the page table manipulation macros to
  487. * work. This is not used by the hardware and we have no
  488. * alignment requirement for this allocation.
  489. */
  490. pgd = (pgd_t *)kmalloc(PTRS_PER_S2_PGD * sizeof(pgd_t),
  491. GFP_KERNEL | __GFP_ZERO);
  492. } else {
  493. /*
  494. * Allocate actual first-level Stage-2 page table used by the
  495. * hardware for Stage-2 page table walks.
  496. */
  497. pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, S2_PGD_ORDER);
  498. }
  499. if (!pgd)
  500. return -ENOMEM;
  501. ret = kvm_prealloc_hwpgd(kvm, pgd);
  502. if (ret)
  503. goto out_err;
  504. kvm_clean_pgd(pgd);
  505. kvm->arch.pgd = pgd;
  506. return 0;
  507. out_err:
  508. if (KVM_PREALLOC_LEVEL > 0)
  509. kfree(pgd);
  510. else
  511. free_pages((unsigned long)pgd, S2_PGD_ORDER);
  512. return ret;
  513. }
  514. /**
  515. * unmap_stage2_range -- Clear stage2 page table entries to unmap a range
  516. * @kvm: The VM pointer
  517. * @start: The intermediate physical base address of the range to unmap
  518. * @size: The size of the area to unmap
  519. *
  520. * Clear a range of stage-2 mappings, lowering the various ref-counts. Must
  521. * be called while holding mmu_lock (unless for freeing the stage2 pgd before
  522. * destroying the VM), otherwise another faulting VCPU may come in and mess
  523. * with things behind our backs.
  524. */
  525. static void unmap_stage2_range(struct kvm *kvm, phys_addr_t start, u64 size)
  526. {
  527. unmap_range(kvm, kvm->arch.pgd, start, size);
  528. }
  529. static void stage2_unmap_memslot(struct kvm *kvm,
  530. struct kvm_memory_slot *memslot)
  531. {
  532. hva_t hva = memslot->userspace_addr;
  533. phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
  534. phys_addr_t size = PAGE_SIZE * memslot->npages;
  535. hva_t reg_end = hva + size;
  536. /*
  537. * A memory region could potentially cover multiple VMAs, and any holes
  538. * between them, so iterate over all of them to find out if we should
  539. * unmap any of them.
  540. *
  541. * +--------------------------------------------+
  542. * +---------------+----------------+ +----------------+
  543. * | : VMA 1 | VMA 2 | | VMA 3 : |
  544. * +---------------+----------------+ +----------------+
  545. * | memory region |
  546. * +--------------------------------------------+
  547. */
  548. do {
  549. struct vm_area_struct *vma = find_vma(current->mm, hva);
  550. hva_t vm_start, vm_end;
  551. if (!vma || vma->vm_start >= reg_end)
  552. break;
  553. /*
  554. * Take the intersection of this VMA with the memory region
  555. */
  556. vm_start = max(hva, vma->vm_start);
  557. vm_end = min(reg_end, vma->vm_end);
  558. if (!(vma->vm_flags & VM_PFNMAP)) {
  559. gpa_t gpa = addr + (vm_start - memslot->userspace_addr);
  560. unmap_stage2_range(kvm, gpa, vm_end - vm_start);
  561. }
  562. hva = vm_end;
  563. } while (hva < reg_end);
  564. }
  565. /**
  566. * stage2_unmap_vm - Unmap Stage-2 RAM mappings
  567. * @kvm: The struct kvm pointer
  568. *
  569. * Go through the memregions and unmap any reguler RAM
  570. * backing memory already mapped to the VM.
  571. */
  572. void stage2_unmap_vm(struct kvm *kvm)
  573. {
  574. struct kvm_memslots *slots;
  575. struct kvm_memory_slot *memslot;
  576. int idx;
  577. idx = srcu_read_lock(&kvm->srcu);
  578. spin_lock(&kvm->mmu_lock);
  579. slots = kvm_memslots(kvm);
  580. kvm_for_each_memslot(memslot, slots)
  581. stage2_unmap_memslot(kvm, memslot);
  582. spin_unlock(&kvm->mmu_lock);
  583. srcu_read_unlock(&kvm->srcu, idx);
  584. }
  585. /**
  586. * kvm_free_stage2_pgd - free all stage-2 tables
  587. * @kvm: The KVM struct pointer for the VM.
  588. *
  589. * Walks the level-1 page table pointed to by kvm->arch.pgd and frees all
  590. * underlying level-2 and level-3 tables before freeing the actual level-1 table
  591. * and setting the struct pointer to NULL.
  592. *
  593. * Note we don't need locking here as this is only called when the VM is
  594. * destroyed, which can only be done once.
  595. */
  596. void kvm_free_stage2_pgd(struct kvm *kvm)
  597. {
  598. if (kvm->arch.pgd == NULL)
  599. return;
  600. unmap_stage2_range(kvm, 0, KVM_PHYS_SIZE);
  601. kvm_free_hwpgd(kvm);
  602. if (KVM_PREALLOC_LEVEL > 0)
  603. kfree(kvm->arch.pgd);
  604. else
  605. free_pages((unsigned long)kvm->arch.pgd, S2_PGD_ORDER);
  606. kvm->arch.pgd = NULL;
  607. }
  608. static pud_t *stage2_get_pud(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
  609. phys_addr_t addr)
  610. {
  611. pgd_t *pgd;
  612. pud_t *pud;
  613. pgd = kvm->arch.pgd + pgd_index(addr);
  614. if (WARN_ON(pgd_none(*pgd))) {
  615. if (!cache)
  616. return NULL;
  617. pud = mmu_memory_cache_alloc(cache);
  618. pgd_populate(NULL, pgd, pud);
  619. get_page(virt_to_page(pgd));
  620. }
  621. return pud_offset(pgd, addr);
  622. }
  623. static pmd_t *stage2_get_pmd(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
  624. phys_addr_t addr)
  625. {
  626. pud_t *pud;
  627. pmd_t *pmd;
  628. pud = stage2_get_pud(kvm, cache, addr);
  629. if (pud_none(*pud)) {
  630. if (!cache)
  631. return NULL;
  632. pmd = mmu_memory_cache_alloc(cache);
  633. pud_populate(NULL, pud, pmd);
  634. get_page(virt_to_page(pud));
  635. }
  636. return pmd_offset(pud, addr);
  637. }
  638. static int stage2_set_pmd_huge(struct kvm *kvm, struct kvm_mmu_memory_cache
  639. *cache, phys_addr_t addr, const pmd_t *new_pmd)
  640. {
  641. pmd_t *pmd, old_pmd;
  642. pmd = stage2_get_pmd(kvm, cache, addr);
  643. VM_BUG_ON(!pmd);
  644. /*
  645. * Mapping in huge pages should only happen through a fault. If a
  646. * page is merged into a transparent huge page, the individual
  647. * subpages of that huge page should be unmapped through MMU
  648. * notifiers before we get here.
  649. *
  650. * Merging of CompoundPages is not supported; they should become
  651. * splitting first, unmapped, merged, and mapped back in on-demand.
  652. */
  653. VM_BUG_ON(pmd_present(*pmd) && pmd_pfn(*pmd) != pmd_pfn(*new_pmd));
  654. old_pmd = *pmd;
  655. kvm_set_pmd(pmd, *new_pmd);
  656. if (pmd_present(old_pmd))
  657. kvm_tlb_flush_vmid_ipa(kvm, addr);
  658. else
  659. get_page(virt_to_page(pmd));
  660. return 0;
  661. }
  662. static int stage2_set_pte(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
  663. phys_addr_t addr, const pte_t *new_pte, bool iomap)
  664. {
  665. pmd_t *pmd;
  666. pte_t *pte, old_pte;
  667. /* Create stage-2 page table mapping - Levels 0 and 1 */
  668. pmd = stage2_get_pmd(kvm, cache, addr);
  669. if (!pmd) {
  670. /*
  671. * Ignore calls from kvm_set_spte_hva for unallocated
  672. * address ranges.
  673. */
  674. return 0;
  675. }
  676. /* Create stage-2 page mappings - Level 2 */
  677. if (pmd_none(*pmd)) {
  678. if (!cache)
  679. return 0; /* ignore calls from kvm_set_spte_hva */
  680. pte = mmu_memory_cache_alloc(cache);
  681. kvm_clean_pte(pte);
  682. pmd_populate_kernel(NULL, pmd, pte);
  683. get_page(virt_to_page(pmd));
  684. }
  685. pte = pte_offset_kernel(pmd, addr);
  686. if (iomap && pte_present(*pte))
  687. return -EFAULT;
  688. /* Create 2nd stage page table mapping - Level 3 */
  689. old_pte = *pte;
  690. kvm_set_pte(pte, *new_pte);
  691. if (pte_present(old_pte))
  692. kvm_tlb_flush_vmid_ipa(kvm, addr);
  693. else
  694. get_page(virt_to_page(pte));
  695. return 0;
  696. }
  697. /**
  698. * kvm_phys_addr_ioremap - map a device range to guest IPA
  699. *
  700. * @kvm: The KVM pointer
  701. * @guest_ipa: The IPA at which to insert the mapping
  702. * @pa: The physical address of the device
  703. * @size: The size of the mapping
  704. */
  705. int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
  706. phys_addr_t pa, unsigned long size, bool writable)
  707. {
  708. phys_addr_t addr, end;
  709. int ret = 0;
  710. unsigned long pfn;
  711. struct kvm_mmu_memory_cache cache = { 0, };
  712. end = (guest_ipa + size + PAGE_SIZE - 1) & PAGE_MASK;
  713. pfn = __phys_to_pfn(pa);
  714. for (addr = guest_ipa; addr < end; addr += PAGE_SIZE) {
  715. pte_t pte = pfn_pte(pfn, PAGE_S2_DEVICE);
  716. if (writable)
  717. kvm_set_s2pte_writable(&pte);
  718. ret = mmu_topup_memory_cache(&cache, KVM_MMU_CACHE_MIN_PAGES,
  719. KVM_NR_MEM_OBJS);
  720. if (ret)
  721. goto out;
  722. spin_lock(&kvm->mmu_lock);
  723. ret = stage2_set_pte(kvm, &cache, addr, &pte, true);
  724. spin_unlock(&kvm->mmu_lock);
  725. if (ret)
  726. goto out;
  727. pfn++;
  728. }
  729. out:
  730. mmu_free_memory_cache(&cache);
  731. return ret;
  732. }
  733. static bool transparent_hugepage_adjust(pfn_t *pfnp, phys_addr_t *ipap)
  734. {
  735. pfn_t pfn = *pfnp;
  736. gfn_t gfn = *ipap >> PAGE_SHIFT;
  737. if (PageTransCompound(pfn_to_page(pfn))) {
  738. unsigned long mask;
  739. /*
  740. * The address we faulted on is backed by a transparent huge
  741. * page. However, because we map the compound huge page and
  742. * not the individual tail page, we need to transfer the
  743. * refcount to the head page. We have to be careful that the
  744. * THP doesn't start to split while we are adjusting the
  745. * refcounts.
  746. *
  747. * We are sure this doesn't happen, because mmu_notifier_retry
  748. * was successful and we are holding the mmu_lock, so if this
  749. * THP is trying to split, it will be blocked in the mmu
  750. * notifier before touching any of the pages, specifically
  751. * before being able to call __split_huge_page_refcount().
  752. *
  753. * We can therefore safely transfer the refcount from PG_tail
  754. * to PG_head and switch the pfn from a tail page to the head
  755. * page accordingly.
  756. */
  757. mask = PTRS_PER_PMD - 1;
  758. VM_BUG_ON((gfn & mask) != (pfn & mask));
  759. if (pfn & mask) {
  760. *ipap &= PMD_MASK;
  761. kvm_release_pfn_clean(pfn);
  762. pfn &= ~mask;
  763. kvm_get_pfn(pfn);
  764. *pfnp = pfn;
  765. }
  766. return true;
  767. }
  768. return false;
  769. }
  770. static bool kvm_is_write_fault(struct kvm_vcpu *vcpu)
  771. {
  772. if (kvm_vcpu_trap_is_iabt(vcpu))
  773. return false;
  774. return kvm_vcpu_dabt_iswrite(vcpu);
  775. }
  776. static bool kvm_is_device_pfn(unsigned long pfn)
  777. {
  778. return !pfn_valid(pfn);
  779. }
  780. static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
  781. struct kvm_memory_slot *memslot, unsigned long hva,
  782. unsigned long fault_status)
  783. {
  784. int ret;
  785. bool write_fault, writable, hugetlb = false, force_pte = false;
  786. unsigned long mmu_seq;
  787. gfn_t gfn = fault_ipa >> PAGE_SHIFT;
  788. struct kvm *kvm = vcpu->kvm;
  789. struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
  790. struct vm_area_struct *vma;
  791. pfn_t pfn;
  792. pgprot_t mem_type = PAGE_S2;
  793. bool fault_ipa_uncached;
  794. write_fault = kvm_is_write_fault(vcpu);
  795. if (fault_status == FSC_PERM && !write_fault) {
  796. kvm_err("Unexpected L2 read permission error\n");
  797. return -EFAULT;
  798. }
  799. /* Let's check if we will get back a huge page backed by hugetlbfs */
  800. down_read(&current->mm->mmap_sem);
  801. vma = find_vma_intersection(current->mm, hva, hva + 1);
  802. if (unlikely(!vma)) {
  803. kvm_err("Failed to find VMA for hva 0x%lx\n", hva);
  804. up_read(&current->mm->mmap_sem);
  805. return -EFAULT;
  806. }
  807. if (is_vm_hugetlb_page(vma)) {
  808. hugetlb = true;
  809. gfn = (fault_ipa & PMD_MASK) >> PAGE_SHIFT;
  810. } else {
  811. /*
  812. * Pages belonging to memslots that don't have the same
  813. * alignment for userspace and IPA cannot be mapped using
  814. * block descriptors even if the pages belong to a THP for
  815. * the process, because the stage-2 block descriptor will
  816. * cover more than a single THP and we loose atomicity for
  817. * unmapping, updates, and splits of the THP or other pages
  818. * in the stage-2 block range.
  819. */
  820. if ((memslot->userspace_addr & ~PMD_MASK) !=
  821. ((memslot->base_gfn << PAGE_SHIFT) & ~PMD_MASK))
  822. force_pte = true;
  823. }
  824. up_read(&current->mm->mmap_sem);
  825. /* We need minimum second+third level pages */
  826. ret = mmu_topup_memory_cache(memcache, KVM_MMU_CACHE_MIN_PAGES,
  827. KVM_NR_MEM_OBJS);
  828. if (ret)
  829. return ret;
  830. mmu_seq = vcpu->kvm->mmu_notifier_seq;
  831. /*
  832. * Ensure the read of mmu_notifier_seq happens before we call
  833. * gfn_to_pfn_prot (which calls get_user_pages), so that we don't risk
  834. * the page we just got a reference to gets unmapped before we have a
  835. * chance to grab the mmu_lock, which ensure that if the page gets
  836. * unmapped afterwards, the call to kvm_unmap_hva will take it away
  837. * from us again properly. This smp_rmb() interacts with the smp_wmb()
  838. * in kvm_mmu_notifier_invalidate_<page|range_end>.
  839. */
  840. smp_rmb();
  841. pfn = gfn_to_pfn_prot(kvm, gfn, write_fault, &writable);
  842. if (is_error_pfn(pfn))
  843. return -EFAULT;
  844. if (kvm_is_device_pfn(pfn))
  845. mem_type = PAGE_S2_DEVICE;
  846. spin_lock(&kvm->mmu_lock);
  847. if (mmu_notifier_retry(kvm, mmu_seq))
  848. goto out_unlock;
  849. if (!hugetlb && !force_pte)
  850. hugetlb = transparent_hugepage_adjust(&pfn, &fault_ipa);
  851. fault_ipa_uncached = memslot->flags & KVM_MEMSLOT_INCOHERENT;
  852. if (hugetlb) {
  853. pmd_t new_pmd = pfn_pmd(pfn, mem_type);
  854. new_pmd = pmd_mkhuge(new_pmd);
  855. if (writable) {
  856. kvm_set_s2pmd_writable(&new_pmd);
  857. kvm_set_pfn_dirty(pfn);
  858. }
  859. coherent_cache_guest_page(vcpu, hva & PMD_MASK, PMD_SIZE,
  860. fault_ipa_uncached);
  861. ret = stage2_set_pmd_huge(kvm, memcache, fault_ipa, &new_pmd);
  862. } else {
  863. pte_t new_pte = pfn_pte(pfn, mem_type);
  864. if (writable) {
  865. kvm_set_s2pte_writable(&new_pte);
  866. kvm_set_pfn_dirty(pfn);
  867. }
  868. coherent_cache_guest_page(vcpu, hva, PAGE_SIZE,
  869. fault_ipa_uncached);
  870. ret = stage2_set_pte(kvm, memcache, fault_ipa, &new_pte,
  871. pgprot_val(mem_type) == pgprot_val(PAGE_S2_DEVICE));
  872. }
  873. out_unlock:
  874. spin_unlock(&kvm->mmu_lock);
  875. kvm_release_pfn_clean(pfn);
  876. return ret;
  877. }
  878. /**
  879. * kvm_handle_guest_abort - handles all 2nd stage aborts
  880. * @vcpu: the VCPU pointer
  881. * @run: the kvm_run structure
  882. *
  883. * Any abort that gets to the host is almost guaranteed to be caused by a
  884. * missing second stage translation table entry, which can mean that either the
  885. * guest simply needs more memory and we must allocate an appropriate page or it
  886. * can mean that the guest tried to access I/O memory, which is emulated by user
  887. * space. The distinction is based on the IPA causing the fault and whether this
  888. * memory region has been registered as standard RAM by user space.
  889. */
  890. int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run)
  891. {
  892. unsigned long fault_status;
  893. phys_addr_t fault_ipa;
  894. struct kvm_memory_slot *memslot;
  895. unsigned long hva;
  896. bool is_iabt, write_fault, writable;
  897. gfn_t gfn;
  898. int ret, idx;
  899. is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
  900. fault_ipa = kvm_vcpu_get_fault_ipa(vcpu);
  901. trace_kvm_guest_fault(*vcpu_pc(vcpu), kvm_vcpu_get_hsr(vcpu),
  902. kvm_vcpu_get_hfar(vcpu), fault_ipa);
  903. /* Check the stage-2 fault is trans. fault or write fault */
  904. fault_status = kvm_vcpu_trap_get_fault_type(vcpu);
  905. if (fault_status != FSC_FAULT && fault_status != FSC_PERM) {
  906. kvm_err("Unsupported FSC: EC=%#x xFSC=%#lx ESR_EL2=%#lx\n",
  907. kvm_vcpu_trap_get_class(vcpu),
  908. (unsigned long)kvm_vcpu_trap_get_fault(vcpu),
  909. (unsigned long)kvm_vcpu_get_hsr(vcpu));
  910. return -EFAULT;
  911. }
  912. idx = srcu_read_lock(&vcpu->kvm->srcu);
  913. gfn = fault_ipa >> PAGE_SHIFT;
  914. memslot = gfn_to_memslot(vcpu->kvm, gfn);
  915. hva = gfn_to_hva_memslot_prot(memslot, gfn, &writable);
  916. write_fault = kvm_is_write_fault(vcpu);
  917. if (kvm_is_error_hva(hva) || (write_fault && !writable)) {
  918. if (is_iabt) {
  919. /* Prefetch Abort on I/O address */
  920. kvm_inject_pabt(vcpu, kvm_vcpu_get_hfar(vcpu));
  921. ret = 1;
  922. goto out_unlock;
  923. }
  924. /*
  925. * The IPA is reported as [MAX:12], so we need to
  926. * complement it with the bottom 12 bits from the
  927. * faulting VA. This is always 12 bits, irrespective
  928. * of the page size.
  929. */
  930. fault_ipa |= kvm_vcpu_get_hfar(vcpu) & ((1 << 12) - 1);
  931. ret = io_mem_abort(vcpu, run, fault_ipa);
  932. goto out_unlock;
  933. }
  934. /* Userspace should not be able to register out-of-bounds IPAs */
  935. VM_BUG_ON(fault_ipa >= KVM_PHYS_SIZE);
  936. ret = user_mem_abort(vcpu, fault_ipa, memslot, hva, fault_status);
  937. if (ret == 0)
  938. ret = 1;
  939. out_unlock:
  940. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  941. return ret;
  942. }
  943. static void handle_hva_to_gpa(struct kvm *kvm,
  944. unsigned long start,
  945. unsigned long end,
  946. void (*handler)(struct kvm *kvm,
  947. gpa_t gpa, void *data),
  948. void *data)
  949. {
  950. struct kvm_memslots *slots;
  951. struct kvm_memory_slot *memslot;
  952. slots = kvm_memslots(kvm);
  953. /* we only care about the pages that the guest sees */
  954. kvm_for_each_memslot(memslot, slots) {
  955. unsigned long hva_start, hva_end;
  956. gfn_t gfn, gfn_end;
  957. hva_start = max(start, memslot->userspace_addr);
  958. hva_end = min(end, memslot->userspace_addr +
  959. (memslot->npages << PAGE_SHIFT));
  960. if (hva_start >= hva_end)
  961. continue;
  962. /*
  963. * {gfn(page) | page intersects with [hva_start, hva_end)} =
  964. * {gfn_start, gfn_start+1, ..., gfn_end-1}.
  965. */
  966. gfn = hva_to_gfn_memslot(hva_start, memslot);
  967. gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot);
  968. for (; gfn < gfn_end; ++gfn) {
  969. gpa_t gpa = gfn << PAGE_SHIFT;
  970. handler(kvm, gpa, data);
  971. }
  972. }
  973. }
  974. static void kvm_unmap_hva_handler(struct kvm *kvm, gpa_t gpa, void *data)
  975. {
  976. unmap_stage2_range(kvm, gpa, PAGE_SIZE);
  977. }
  978. int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
  979. {
  980. unsigned long end = hva + PAGE_SIZE;
  981. if (!kvm->arch.pgd)
  982. return 0;
  983. trace_kvm_unmap_hva(hva);
  984. handle_hva_to_gpa(kvm, hva, end, &kvm_unmap_hva_handler, NULL);
  985. return 0;
  986. }
  987. int kvm_unmap_hva_range(struct kvm *kvm,
  988. unsigned long start, unsigned long end)
  989. {
  990. if (!kvm->arch.pgd)
  991. return 0;
  992. trace_kvm_unmap_hva_range(start, end);
  993. handle_hva_to_gpa(kvm, start, end, &kvm_unmap_hva_handler, NULL);
  994. return 0;
  995. }
  996. static void kvm_set_spte_handler(struct kvm *kvm, gpa_t gpa, void *data)
  997. {
  998. pte_t *pte = (pte_t *)data;
  999. stage2_set_pte(kvm, NULL, gpa, pte, false);
  1000. }
  1001. void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
  1002. {
  1003. unsigned long end = hva + PAGE_SIZE;
  1004. pte_t stage2_pte;
  1005. if (!kvm->arch.pgd)
  1006. return;
  1007. trace_kvm_set_spte_hva(hva);
  1008. stage2_pte = pfn_pte(pte_pfn(pte), PAGE_S2);
  1009. handle_hva_to_gpa(kvm, hva, end, &kvm_set_spte_handler, &stage2_pte);
  1010. }
  1011. void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu)
  1012. {
  1013. mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
  1014. }
  1015. phys_addr_t kvm_mmu_get_httbr(void)
  1016. {
  1017. return virt_to_phys(hyp_pgd);
  1018. }
  1019. phys_addr_t kvm_mmu_get_boot_httbr(void)
  1020. {
  1021. return virt_to_phys(boot_hyp_pgd);
  1022. }
  1023. phys_addr_t kvm_get_idmap_vector(void)
  1024. {
  1025. return hyp_idmap_vector;
  1026. }
  1027. int kvm_mmu_init(void)
  1028. {
  1029. int err;
  1030. hyp_idmap_start = kvm_virt_to_phys(__hyp_idmap_text_start);
  1031. hyp_idmap_end = kvm_virt_to_phys(__hyp_idmap_text_end);
  1032. hyp_idmap_vector = kvm_virt_to_phys(__kvm_hyp_init);
  1033. if ((hyp_idmap_start ^ hyp_idmap_end) & PAGE_MASK) {
  1034. /*
  1035. * Our init code is crossing a page boundary. Allocate
  1036. * a bounce page, copy the code over and use that.
  1037. */
  1038. size_t len = __hyp_idmap_text_end - __hyp_idmap_text_start;
  1039. phys_addr_t phys_base;
  1040. init_bounce_page = (void *)__get_free_page(GFP_KERNEL);
  1041. if (!init_bounce_page) {
  1042. kvm_err("Couldn't allocate HYP init bounce page\n");
  1043. err = -ENOMEM;
  1044. goto out;
  1045. }
  1046. memcpy(init_bounce_page, __hyp_idmap_text_start, len);
  1047. /*
  1048. * Warning: the code we just copied to the bounce page
  1049. * must be flushed to the point of coherency.
  1050. * Otherwise, the data may be sitting in L2, and HYP
  1051. * mode won't be able to observe it as it runs with
  1052. * caches off at that point.
  1053. */
  1054. kvm_flush_dcache_to_poc(init_bounce_page, len);
  1055. phys_base = kvm_virt_to_phys(init_bounce_page);
  1056. hyp_idmap_vector += phys_base - hyp_idmap_start;
  1057. hyp_idmap_start = phys_base;
  1058. hyp_idmap_end = phys_base + len;
  1059. kvm_info("Using HYP init bounce page @%lx\n",
  1060. (unsigned long)phys_base);
  1061. }
  1062. hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, hyp_pgd_order);
  1063. boot_hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, hyp_pgd_order);
  1064. if (!hyp_pgd || !boot_hyp_pgd) {
  1065. kvm_err("Hyp mode PGD not allocated\n");
  1066. err = -ENOMEM;
  1067. goto out;
  1068. }
  1069. /* Create the idmap in the boot page tables */
  1070. err = __create_hyp_mappings(boot_hyp_pgd,
  1071. hyp_idmap_start, hyp_idmap_end,
  1072. __phys_to_pfn(hyp_idmap_start),
  1073. PAGE_HYP);
  1074. if (err) {
  1075. kvm_err("Failed to idmap %lx-%lx\n",
  1076. hyp_idmap_start, hyp_idmap_end);
  1077. goto out;
  1078. }
  1079. /* Map the very same page at the trampoline VA */
  1080. err = __create_hyp_mappings(boot_hyp_pgd,
  1081. TRAMPOLINE_VA, TRAMPOLINE_VA + PAGE_SIZE,
  1082. __phys_to_pfn(hyp_idmap_start),
  1083. PAGE_HYP);
  1084. if (err) {
  1085. kvm_err("Failed to map trampoline @%lx into boot HYP pgd\n",
  1086. TRAMPOLINE_VA);
  1087. goto out;
  1088. }
  1089. /* Map the same page again into the runtime page tables */
  1090. err = __create_hyp_mappings(hyp_pgd,
  1091. TRAMPOLINE_VA, TRAMPOLINE_VA + PAGE_SIZE,
  1092. __phys_to_pfn(hyp_idmap_start),
  1093. PAGE_HYP);
  1094. if (err) {
  1095. kvm_err("Failed to map trampoline @%lx into runtime HYP pgd\n",
  1096. TRAMPOLINE_VA);
  1097. goto out;
  1098. }
  1099. return 0;
  1100. out:
  1101. free_hyp_pgds();
  1102. return err;
  1103. }
  1104. void kvm_arch_commit_memory_region(struct kvm *kvm,
  1105. struct kvm_userspace_memory_region *mem,
  1106. const struct kvm_memory_slot *old,
  1107. enum kvm_mr_change change)
  1108. {
  1109. }
  1110. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  1111. struct kvm_memory_slot *memslot,
  1112. struct kvm_userspace_memory_region *mem,
  1113. enum kvm_mr_change change)
  1114. {
  1115. hva_t hva = mem->userspace_addr;
  1116. hva_t reg_end = hva + mem->memory_size;
  1117. bool writable = !(mem->flags & KVM_MEM_READONLY);
  1118. int ret = 0;
  1119. if (change != KVM_MR_CREATE && change != KVM_MR_MOVE)
  1120. return 0;
  1121. /*
  1122. * Prevent userspace from creating a memory region outside of the IPA
  1123. * space addressable by the KVM guest IPA space.
  1124. */
  1125. if (memslot->base_gfn + memslot->npages >=
  1126. (KVM_PHYS_SIZE >> PAGE_SHIFT))
  1127. return -EFAULT;
  1128. /*
  1129. * A memory region could potentially cover multiple VMAs, and any holes
  1130. * between them, so iterate over all of them to find out if we can map
  1131. * any of them right now.
  1132. *
  1133. * +--------------------------------------------+
  1134. * +---------------+----------------+ +----------------+
  1135. * | : VMA 1 | VMA 2 | | VMA 3 : |
  1136. * +---------------+----------------+ +----------------+
  1137. * | memory region |
  1138. * +--------------------------------------------+
  1139. */
  1140. do {
  1141. struct vm_area_struct *vma = find_vma(current->mm, hva);
  1142. hva_t vm_start, vm_end;
  1143. if (!vma || vma->vm_start >= reg_end)
  1144. break;
  1145. /*
  1146. * Mapping a read-only VMA is only allowed if the
  1147. * memory region is configured as read-only.
  1148. */
  1149. if (writable && !(vma->vm_flags & VM_WRITE)) {
  1150. ret = -EPERM;
  1151. break;
  1152. }
  1153. /*
  1154. * Take the intersection of this VMA with the memory region
  1155. */
  1156. vm_start = max(hva, vma->vm_start);
  1157. vm_end = min(reg_end, vma->vm_end);
  1158. if (vma->vm_flags & VM_PFNMAP) {
  1159. gpa_t gpa = mem->guest_phys_addr +
  1160. (vm_start - mem->userspace_addr);
  1161. phys_addr_t pa = (vma->vm_pgoff << PAGE_SHIFT) +
  1162. vm_start - vma->vm_start;
  1163. ret = kvm_phys_addr_ioremap(kvm, gpa, pa,
  1164. vm_end - vm_start,
  1165. writable);
  1166. if (ret)
  1167. break;
  1168. }
  1169. hva = vm_end;
  1170. } while (hva < reg_end);
  1171. spin_lock(&kvm->mmu_lock);
  1172. if (ret)
  1173. unmap_stage2_range(kvm, mem->guest_phys_addr, mem->memory_size);
  1174. else
  1175. stage2_flush_memslot(kvm, memslot);
  1176. spin_unlock(&kvm->mmu_lock);
  1177. return ret;
  1178. }
  1179. void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
  1180. struct kvm_memory_slot *dont)
  1181. {
  1182. }
  1183. int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
  1184. unsigned long npages)
  1185. {
  1186. /*
  1187. * Readonly memslots are not incoherent with the caches by definition,
  1188. * but in practice, they are used mostly to emulate ROMs or NOR flashes
  1189. * that the guest may consider devices and hence map as uncached.
  1190. * To prevent incoherency issues in these cases, tag all readonly
  1191. * regions as incoherent.
  1192. */
  1193. if (slot->flags & KVM_MEM_READONLY)
  1194. slot->flags |= KVM_MEMSLOT_INCOHERENT;
  1195. return 0;
  1196. }
  1197. void kvm_arch_memslots_updated(struct kvm *kvm)
  1198. {
  1199. }
  1200. void kvm_arch_flush_shadow_all(struct kvm *kvm)
  1201. {
  1202. }
  1203. void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
  1204. struct kvm_memory_slot *slot)
  1205. {
  1206. gpa_t gpa = slot->base_gfn << PAGE_SHIFT;
  1207. phys_addr_t size = slot->npages << PAGE_SHIFT;
  1208. spin_lock(&kvm->mmu_lock);
  1209. unmap_stage2_range(kvm, gpa, size);
  1210. spin_unlock(&kvm->mmu_lock);
  1211. }