mmu.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821
  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. #define kvm_pud_huge(_x) pud_huge(_x)
  42. #define KVM_S2PTE_FLAG_IS_IOMAP (1UL << 0)
  43. #define KVM_S2_FLAG_LOGGING_ACTIVE (1UL << 1)
  44. static bool memslot_is_logging(struct kvm_memory_slot *memslot)
  45. {
  46. return memslot->dirty_bitmap && !(memslot->flags & KVM_MEM_READONLY);
  47. }
  48. /**
  49. * kvm_flush_remote_tlbs() - flush all VM TLB entries for v7/8
  50. * @kvm: pointer to kvm structure.
  51. *
  52. * Interface to HYP function to flush all VM TLB entries
  53. */
  54. void kvm_flush_remote_tlbs(struct kvm *kvm)
  55. {
  56. kvm_call_hyp(__kvm_tlb_flush_vmid, kvm);
  57. }
  58. static void kvm_tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa)
  59. {
  60. /*
  61. * This function also gets called when dealing with HYP page
  62. * tables. As HYP doesn't have an associated struct kvm (and
  63. * the HYP page tables are fairly static), we don't do
  64. * anything there.
  65. */
  66. if (kvm)
  67. kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, kvm, ipa);
  68. }
  69. /*
  70. * D-Cache management functions. They take the page table entries by
  71. * value, as they are flushing the cache using the kernel mapping (or
  72. * kmap on 32bit).
  73. */
  74. static void kvm_flush_dcache_pte(pte_t pte)
  75. {
  76. __kvm_flush_dcache_pte(pte);
  77. }
  78. static void kvm_flush_dcache_pmd(pmd_t pmd)
  79. {
  80. __kvm_flush_dcache_pmd(pmd);
  81. }
  82. static void kvm_flush_dcache_pud(pud_t pud)
  83. {
  84. __kvm_flush_dcache_pud(pud);
  85. }
  86. /**
  87. * stage2_dissolve_pmd() - clear and flush huge PMD entry
  88. * @kvm: pointer to kvm structure.
  89. * @addr: IPA
  90. * @pmd: pmd pointer for IPA
  91. *
  92. * Function clears a PMD entry, flushes addr 1st and 2nd stage TLBs. Marks all
  93. * pages in the range dirty.
  94. */
  95. static void stage2_dissolve_pmd(struct kvm *kvm, phys_addr_t addr, pmd_t *pmd)
  96. {
  97. if (!kvm_pmd_huge(*pmd))
  98. return;
  99. pmd_clear(pmd);
  100. kvm_tlb_flush_vmid_ipa(kvm, addr);
  101. put_page(virt_to_page(pmd));
  102. }
  103. static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
  104. int min, int max)
  105. {
  106. void *page;
  107. BUG_ON(max > KVM_NR_MEM_OBJS);
  108. if (cache->nobjs >= min)
  109. return 0;
  110. while (cache->nobjs < max) {
  111. page = (void *)__get_free_page(PGALLOC_GFP);
  112. if (!page)
  113. return -ENOMEM;
  114. cache->objects[cache->nobjs++] = page;
  115. }
  116. return 0;
  117. }
  118. static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
  119. {
  120. while (mc->nobjs)
  121. free_page((unsigned long)mc->objects[--mc->nobjs]);
  122. }
  123. static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc)
  124. {
  125. void *p;
  126. BUG_ON(!mc || !mc->nobjs);
  127. p = mc->objects[--mc->nobjs];
  128. return p;
  129. }
  130. static void clear_pgd_entry(struct kvm *kvm, pgd_t *pgd, phys_addr_t addr)
  131. {
  132. pud_t *pud_table __maybe_unused = pud_offset(pgd, 0);
  133. pgd_clear(pgd);
  134. kvm_tlb_flush_vmid_ipa(kvm, addr);
  135. pud_free(NULL, pud_table);
  136. put_page(virt_to_page(pgd));
  137. }
  138. static void clear_pud_entry(struct kvm *kvm, pud_t *pud, phys_addr_t addr)
  139. {
  140. pmd_t *pmd_table = pmd_offset(pud, 0);
  141. VM_BUG_ON(pud_huge(*pud));
  142. pud_clear(pud);
  143. kvm_tlb_flush_vmid_ipa(kvm, addr);
  144. pmd_free(NULL, pmd_table);
  145. put_page(virt_to_page(pud));
  146. }
  147. static void clear_pmd_entry(struct kvm *kvm, pmd_t *pmd, phys_addr_t addr)
  148. {
  149. pte_t *pte_table = pte_offset_kernel(pmd, 0);
  150. VM_BUG_ON(kvm_pmd_huge(*pmd));
  151. pmd_clear(pmd);
  152. kvm_tlb_flush_vmid_ipa(kvm, addr);
  153. pte_free_kernel(NULL, pte_table);
  154. put_page(virt_to_page(pmd));
  155. }
  156. /*
  157. * Unmapping vs dcache management:
  158. *
  159. * If a guest maps certain memory pages as uncached, all writes will
  160. * bypass the data cache and go directly to RAM. However, the CPUs
  161. * can still speculate reads (not writes) and fill cache lines with
  162. * data.
  163. *
  164. * Those cache lines will be *clean* cache lines though, so a
  165. * clean+invalidate operation is equivalent to an invalidate
  166. * operation, because no cache lines are marked dirty.
  167. *
  168. * Those clean cache lines could be filled prior to an uncached write
  169. * by the guest, and the cache coherent IO subsystem would therefore
  170. * end up writing old data to disk.
  171. *
  172. * This is why right after unmapping a page/section and invalidating
  173. * the corresponding TLBs, we call kvm_flush_dcache_p*() to make sure
  174. * the IO subsystem will never hit in the cache.
  175. */
  176. static void unmap_ptes(struct kvm *kvm, pmd_t *pmd,
  177. phys_addr_t addr, phys_addr_t end)
  178. {
  179. phys_addr_t start_addr = addr;
  180. pte_t *pte, *start_pte;
  181. start_pte = pte = pte_offset_kernel(pmd, addr);
  182. do {
  183. if (!pte_none(*pte)) {
  184. pte_t old_pte = *pte;
  185. kvm_set_pte(pte, __pte(0));
  186. kvm_tlb_flush_vmid_ipa(kvm, addr);
  187. /* No need to invalidate the cache for device mappings */
  188. if ((pte_val(old_pte) & PAGE_S2_DEVICE) != PAGE_S2_DEVICE)
  189. kvm_flush_dcache_pte(old_pte);
  190. put_page(virt_to_page(pte));
  191. }
  192. } while (pte++, addr += PAGE_SIZE, addr != end);
  193. if (kvm_pte_table_empty(kvm, start_pte))
  194. clear_pmd_entry(kvm, pmd, start_addr);
  195. }
  196. static void unmap_pmds(struct kvm *kvm, pud_t *pud,
  197. phys_addr_t addr, phys_addr_t end)
  198. {
  199. phys_addr_t next, start_addr = addr;
  200. pmd_t *pmd, *start_pmd;
  201. start_pmd = pmd = pmd_offset(pud, addr);
  202. do {
  203. next = kvm_pmd_addr_end(addr, end);
  204. if (!pmd_none(*pmd)) {
  205. if (kvm_pmd_huge(*pmd)) {
  206. pmd_t old_pmd = *pmd;
  207. pmd_clear(pmd);
  208. kvm_tlb_flush_vmid_ipa(kvm, addr);
  209. kvm_flush_dcache_pmd(old_pmd);
  210. put_page(virt_to_page(pmd));
  211. } else {
  212. unmap_ptes(kvm, pmd, addr, next);
  213. }
  214. }
  215. } while (pmd++, addr = next, addr != end);
  216. if (kvm_pmd_table_empty(kvm, start_pmd))
  217. clear_pud_entry(kvm, pud, start_addr);
  218. }
  219. static void unmap_puds(struct kvm *kvm, pgd_t *pgd,
  220. phys_addr_t addr, phys_addr_t end)
  221. {
  222. phys_addr_t next, start_addr = addr;
  223. pud_t *pud, *start_pud;
  224. start_pud = pud = pud_offset(pgd, addr);
  225. do {
  226. next = kvm_pud_addr_end(addr, end);
  227. if (!pud_none(*pud)) {
  228. if (pud_huge(*pud)) {
  229. pud_t old_pud = *pud;
  230. pud_clear(pud);
  231. kvm_tlb_flush_vmid_ipa(kvm, addr);
  232. kvm_flush_dcache_pud(old_pud);
  233. put_page(virt_to_page(pud));
  234. } else {
  235. unmap_pmds(kvm, pud, addr, next);
  236. }
  237. }
  238. } while (pud++, addr = next, addr != end);
  239. if (kvm_pud_table_empty(kvm, start_pud))
  240. clear_pgd_entry(kvm, pgd, start_addr);
  241. }
  242. static void unmap_range(struct kvm *kvm, pgd_t *pgdp,
  243. phys_addr_t start, u64 size)
  244. {
  245. pgd_t *pgd;
  246. phys_addr_t addr = start, end = start + size;
  247. phys_addr_t next;
  248. pgd = pgdp + kvm_pgd_index(addr);
  249. do {
  250. next = kvm_pgd_addr_end(addr, end);
  251. if (!pgd_none(*pgd))
  252. unmap_puds(kvm, pgd, addr, next);
  253. } while (pgd++, addr = next, addr != end);
  254. }
  255. static void stage2_flush_ptes(struct kvm *kvm, pmd_t *pmd,
  256. phys_addr_t addr, phys_addr_t end)
  257. {
  258. pte_t *pte;
  259. pte = pte_offset_kernel(pmd, addr);
  260. do {
  261. if (!pte_none(*pte) &&
  262. (pte_val(*pte) & PAGE_S2_DEVICE) != PAGE_S2_DEVICE)
  263. kvm_flush_dcache_pte(*pte);
  264. } while (pte++, addr += PAGE_SIZE, addr != end);
  265. }
  266. static void stage2_flush_pmds(struct kvm *kvm, pud_t *pud,
  267. phys_addr_t addr, phys_addr_t end)
  268. {
  269. pmd_t *pmd;
  270. phys_addr_t next;
  271. pmd = pmd_offset(pud, addr);
  272. do {
  273. next = kvm_pmd_addr_end(addr, end);
  274. if (!pmd_none(*pmd)) {
  275. if (kvm_pmd_huge(*pmd))
  276. kvm_flush_dcache_pmd(*pmd);
  277. else
  278. stage2_flush_ptes(kvm, pmd, addr, next);
  279. }
  280. } while (pmd++, addr = next, addr != end);
  281. }
  282. static void stage2_flush_puds(struct kvm *kvm, pgd_t *pgd,
  283. phys_addr_t addr, phys_addr_t end)
  284. {
  285. pud_t *pud;
  286. phys_addr_t next;
  287. pud = pud_offset(pgd, addr);
  288. do {
  289. next = kvm_pud_addr_end(addr, end);
  290. if (!pud_none(*pud)) {
  291. if (pud_huge(*pud))
  292. kvm_flush_dcache_pud(*pud);
  293. else
  294. stage2_flush_pmds(kvm, pud, addr, next);
  295. }
  296. } while (pud++, addr = next, addr != end);
  297. }
  298. static void stage2_flush_memslot(struct kvm *kvm,
  299. struct kvm_memory_slot *memslot)
  300. {
  301. phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
  302. phys_addr_t end = addr + PAGE_SIZE * memslot->npages;
  303. phys_addr_t next;
  304. pgd_t *pgd;
  305. pgd = kvm->arch.pgd + kvm_pgd_index(addr);
  306. do {
  307. next = kvm_pgd_addr_end(addr, end);
  308. stage2_flush_puds(kvm, pgd, addr, next);
  309. } while (pgd++, addr = next, addr != end);
  310. }
  311. /**
  312. * stage2_flush_vm - Invalidate cache for pages mapped in stage 2
  313. * @kvm: The struct kvm pointer
  314. *
  315. * Go through the stage 2 page tables and invalidate any cache lines
  316. * backing memory already mapped to the VM.
  317. */
  318. static void stage2_flush_vm(struct kvm *kvm)
  319. {
  320. struct kvm_memslots *slots;
  321. struct kvm_memory_slot *memslot;
  322. int idx;
  323. idx = srcu_read_lock(&kvm->srcu);
  324. spin_lock(&kvm->mmu_lock);
  325. slots = kvm_memslots(kvm);
  326. kvm_for_each_memslot(memslot, slots)
  327. stage2_flush_memslot(kvm, memslot);
  328. spin_unlock(&kvm->mmu_lock);
  329. srcu_read_unlock(&kvm->srcu, idx);
  330. }
  331. /**
  332. * free_boot_hyp_pgd - free HYP boot page tables
  333. *
  334. * Free the HYP boot page tables. The bounce page is also freed.
  335. */
  336. void free_boot_hyp_pgd(void)
  337. {
  338. mutex_lock(&kvm_hyp_pgd_mutex);
  339. if (boot_hyp_pgd) {
  340. unmap_range(NULL, boot_hyp_pgd, hyp_idmap_start, PAGE_SIZE);
  341. unmap_range(NULL, boot_hyp_pgd, TRAMPOLINE_VA, PAGE_SIZE);
  342. free_pages((unsigned long)boot_hyp_pgd, hyp_pgd_order);
  343. boot_hyp_pgd = NULL;
  344. }
  345. if (hyp_pgd)
  346. unmap_range(NULL, hyp_pgd, TRAMPOLINE_VA, PAGE_SIZE);
  347. free_page((unsigned long)init_bounce_page);
  348. init_bounce_page = NULL;
  349. mutex_unlock(&kvm_hyp_pgd_mutex);
  350. }
  351. /**
  352. * free_hyp_pgds - free Hyp-mode page tables
  353. *
  354. * Assumes hyp_pgd is a page table used strictly in Hyp-mode and
  355. * therefore contains either mappings in the kernel memory area (above
  356. * PAGE_OFFSET), or device mappings in the vmalloc range (from
  357. * VMALLOC_START to VMALLOC_END).
  358. *
  359. * boot_hyp_pgd should only map two pages for the init code.
  360. */
  361. void free_hyp_pgds(void)
  362. {
  363. unsigned long addr;
  364. free_boot_hyp_pgd();
  365. mutex_lock(&kvm_hyp_pgd_mutex);
  366. if (hyp_pgd) {
  367. for (addr = PAGE_OFFSET; virt_addr_valid(addr); addr += PGDIR_SIZE)
  368. unmap_range(NULL, hyp_pgd, KERN_TO_HYP(addr), PGDIR_SIZE);
  369. for (addr = VMALLOC_START; is_vmalloc_addr((void*)addr); addr += PGDIR_SIZE)
  370. unmap_range(NULL, hyp_pgd, KERN_TO_HYP(addr), PGDIR_SIZE);
  371. free_pages((unsigned long)hyp_pgd, hyp_pgd_order);
  372. hyp_pgd = NULL;
  373. }
  374. mutex_unlock(&kvm_hyp_pgd_mutex);
  375. }
  376. static void create_hyp_pte_mappings(pmd_t *pmd, unsigned long start,
  377. unsigned long end, unsigned long pfn,
  378. pgprot_t prot)
  379. {
  380. pte_t *pte;
  381. unsigned long addr;
  382. addr = start;
  383. do {
  384. pte = pte_offset_kernel(pmd, addr);
  385. kvm_set_pte(pte, pfn_pte(pfn, prot));
  386. get_page(virt_to_page(pte));
  387. kvm_flush_dcache_to_poc(pte, sizeof(*pte));
  388. pfn++;
  389. } while (addr += PAGE_SIZE, addr != end);
  390. }
  391. static int create_hyp_pmd_mappings(pud_t *pud, unsigned long start,
  392. unsigned long end, unsigned long pfn,
  393. pgprot_t prot)
  394. {
  395. pmd_t *pmd;
  396. pte_t *pte;
  397. unsigned long addr, next;
  398. addr = start;
  399. do {
  400. pmd = pmd_offset(pud, addr);
  401. BUG_ON(pmd_sect(*pmd));
  402. if (pmd_none(*pmd)) {
  403. pte = pte_alloc_one_kernel(NULL, addr);
  404. if (!pte) {
  405. kvm_err("Cannot allocate Hyp pte\n");
  406. return -ENOMEM;
  407. }
  408. pmd_populate_kernel(NULL, pmd, pte);
  409. get_page(virt_to_page(pmd));
  410. kvm_flush_dcache_to_poc(pmd, sizeof(*pmd));
  411. }
  412. next = pmd_addr_end(addr, end);
  413. create_hyp_pte_mappings(pmd, addr, next, pfn, prot);
  414. pfn += (next - addr) >> PAGE_SHIFT;
  415. } while (addr = next, addr != end);
  416. return 0;
  417. }
  418. static int create_hyp_pud_mappings(pgd_t *pgd, unsigned long start,
  419. unsigned long end, unsigned long pfn,
  420. pgprot_t prot)
  421. {
  422. pud_t *pud;
  423. pmd_t *pmd;
  424. unsigned long addr, next;
  425. int ret;
  426. addr = start;
  427. do {
  428. pud = pud_offset(pgd, addr);
  429. if (pud_none_or_clear_bad(pud)) {
  430. pmd = pmd_alloc_one(NULL, addr);
  431. if (!pmd) {
  432. kvm_err("Cannot allocate Hyp pmd\n");
  433. return -ENOMEM;
  434. }
  435. pud_populate(NULL, pud, pmd);
  436. get_page(virt_to_page(pud));
  437. kvm_flush_dcache_to_poc(pud, sizeof(*pud));
  438. }
  439. next = pud_addr_end(addr, end);
  440. ret = create_hyp_pmd_mappings(pud, addr, next, pfn, prot);
  441. if (ret)
  442. return ret;
  443. pfn += (next - addr) >> PAGE_SHIFT;
  444. } while (addr = next, addr != end);
  445. return 0;
  446. }
  447. static int __create_hyp_mappings(pgd_t *pgdp,
  448. unsigned long start, unsigned long end,
  449. unsigned long pfn, pgprot_t prot)
  450. {
  451. pgd_t *pgd;
  452. pud_t *pud;
  453. unsigned long addr, next;
  454. int err = 0;
  455. mutex_lock(&kvm_hyp_pgd_mutex);
  456. addr = start & PAGE_MASK;
  457. end = PAGE_ALIGN(end);
  458. do {
  459. pgd = pgdp + pgd_index(addr);
  460. if (pgd_none(*pgd)) {
  461. pud = pud_alloc_one(NULL, addr);
  462. if (!pud) {
  463. kvm_err("Cannot allocate Hyp pud\n");
  464. err = -ENOMEM;
  465. goto out;
  466. }
  467. pgd_populate(NULL, pgd, pud);
  468. get_page(virt_to_page(pgd));
  469. kvm_flush_dcache_to_poc(pgd, sizeof(*pgd));
  470. }
  471. next = pgd_addr_end(addr, end);
  472. err = create_hyp_pud_mappings(pgd, addr, next, pfn, prot);
  473. if (err)
  474. goto out;
  475. pfn += (next - addr) >> PAGE_SHIFT;
  476. } while (addr = next, addr != end);
  477. out:
  478. mutex_unlock(&kvm_hyp_pgd_mutex);
  479. return err;
  480. }
  481. static phys_addr_t kvm_kaddr_to_phys(void *kaddr)
  482. {
  483. if (!is_vmalloc_addr(kaddr)) {
  484. BUG_ON(!virt_addr_valid(kaddr));
  485. return __pa(kaddr);
  486. } else {
  487. return page_to_phys(vmalloc_to_page(kaddr)) +
  488. offset_in_page(kaddr);
  489. }
  490. }
  491. /**
  492. * create_hyp_mappings - duplicate a kernel virtual address range in Hyp mode
  493. * @from: The virtual kernel start address of the range
  494. * @to: The virtual kernel end address of the range (exclusive)
  495. *
  496. * The same virtual address as the kernel virtual address is also used
  497. * in Hyp-mode mapping (modulo HYP_PAGE_OFFSET) to the same underlying
  498. * physical pages.
  499. */
  500. int create_hyp_mappings(void *from, void *to)
  501. {
  502. phys_addr_t phys_addr;
  503. unsigned long virt_addr;
  504. unsigned long start = KERN_TO_HYP((unsigned long)from);
  505. unsigned long end = KERN_TO_HYP((unsigned long)to);
  506. start = start & PAGE_MASK;
  507. end = PAGE_ALIGN(end);
  508. for (virt_addr = start; virt_addr < end; virt_addr += PAGE_SIZE) {
  509. int err;
  510. phys_addr = kvm_kaddr_to_phys(from + virt_addr - start);
  511. err = __create_hyp_mappings(hyp_pgd, virt_addr,
  512. virt_addr + PAGE_SIZE,
  513. __phys_to_pfn(phys_addr),
  514. PAGE_HYP);
  515. if (err)
  516. return err;
  517. }
  518. return 0;
  519. }
  520. /**
  521. * create_hyp_io_mappings - duplicate a kernel IO mapping into Hyp mode
  522. * @from: The kernel start VA of the range
  523. * @to: The kernel end VA of the range (exclusive)
  524. * @phys_addr: The physical start address which gets mapped
  525. *
  526. * The resulting HYP VA is the same as the kernel VA, modulo
  527. * HYP_PAGE_OFFSET.
  528. */
  529. int create_hyp_io_mappings(void *from, void *to, phys_addr_t phys_addr)
  530. {
  531. unsigned long start = KERN_TO_HYP((unsigned long)from);
  532. unsigned long end = KERN_TO_HYP((unsigned long)to);
  533. /* Check for a valid kernel IO mapping */
  534. if (!is_vmalloc_addr(from) || !is_vmalloc_addr(to - 1))
  535. return -EINVAL;
  536. return __create_hyp_mappings(hyp_pgd, start, end,
  537. __phys_to_pfn(phys_addr), PAGE_HYP_DEVICE);
  538. }
  539. /* Free the HW pgd, one page at a time */
  540. static void kvm_free_hwpgd(void *hwpgd)
  541. {
  542. free_pages_exact(hwpgd, kvm_get_hwpgd_size());
  543. }
  544. /* Allocate the HW PGD, making sure that each page gets its own refcount */
  545. static void *kvm_alloc_hwpgd(void)
  546. {
  547. unsigned int size = kvm_get_hwpgd_size();
  548. return alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
  549. }
  550. /**
  551. * kvm_alloc_stage2_pgd - allocate level-1 table for stage-2 translation.
  552. * @kvm: The KVM struct pointer for the VM.
  553. *
  554. * Allocates the 1st level table only of size defined by S2_PGD_ORDER (can
  555. * support either full 40-bit input addresses or limited to 32-bit input
  556. * addresses). Clears the allocated pages.
  557. *
  558. * Note we don't need locking here as this is only called when the VM is
  559. * created, which can only be done once.
  560. */
  561. int kvm_alloc_stage2_pgd(struct kvm *kvm)
  562. {
  563. pgd_t *pgd;
  564. void *hwpgd;
  565. if (kvm->arch.pgd != NULL) {
  566. kvm_err("kvm_arch already initialized?\n");
  567. return -EINVAL;
  568. }
  569. hwpgd = kvm_alloc_hwpgd();
  570. if (!hwpgd)
  571. return -ENOMEM;
  572. /* When the kernel uses more levels of page tables than the
  573. * guest, we allocate a fake PGD and pre-populate it to point
  574. * to the next-level page table, which will be the real
  575. * initial page table pointed to by the VTTBR.
  576. *
  577. * When KVM_PREALLOC_LEVEL==2, we allocate a single page for
  578. * the PMD and the kernel will use folded pud.
  579. * When KVM_PREALLOC_LEVEL==1, we allocate 2 consecutive PUD
  580. * pages.
  581. */
  582. if (KVM_PREALLOC_LEVEL > 0) {
  583. int i;
  584. /*
  585. * Allocate fake pgd for the page table manipulation macros to
  586. * work. This is not used by the hardware and we have no
  587. * alignment requirement for this allocation.
  588. */
  589. pgd = (pgd_t *)kmalloc(PTRS_PER_S2_PGD * sizeof(pgd_t),
  590. GFP_KERNEL | __GFP_ZERO);
  591. if (!pgd) {
  592. kvm_free_hwpgd(hwpgd);
  593. return -ENOMEM;
  594. }
  595. /* Plug the HW PGD into the fake one. */
  596. for (i = 0; i < PTRS_PER_S2_PGD; i++) {
  597. if (KVM_PREALLOC_LEVEL == 1)
  598. pgd_populate(NULL, pgd + i,
  599. (pud_t *)hwpgd + i * PTRS_PER_PUD);
  600. else if (KVM_PREALLOC_LEVEL == 2)
  601. pud_populate(NULL, pud_offset(pgd, 0) + i,
  602. (pmd_t *)hwpgd + i * PTRS_PER_PMD);
  603. }
  604. } else {
  605. /*
  606. * Allocate actual first-level Stage-2 page table used by the
  607. * hardware for Stage-2 page table walks.
  608. */
  609. pgd = (pgd_t *)hwpgd;
  610. }
  611. kvm_clean_pgd(pgd);
  612. kvm->arch.pgd = pgd;
  613. return 0;
  614. }
  615. /**
  616. * unmap_stage2_range -- Clear stage2 page table entries to unmap a range
  617. * @kvm: The VM pointer
  618. * @start: The intermediate physical base address of the range to unmap
  619. * @size: The size of the area to unmap
  620. *
  621. * Clear a range of stage-2 mappings, lowering the various ref-counts. Must
  622. * be called while holding mmu_lock (unless for freeing the stage2 pgd before
  623. * destroying the VM), otherwise another faulting VCPU may come in and mess
  624. * with things behind our backs.
  625. */
  626. static void unmap_stage2_range(struct kvm *kvm, phys_addr_t start, u64 size)
  627. {
  628. unmap_range(kvm, kvm->arch.pgd, start, size);
  629. }
  630. static void stage2_unmap_memslot(struct kvm *kvm,
  631. struct kvm_memory_slot *memslot)
  632. {
  633. hva_t hva = memslot->userspace_addr;
  634. phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
  635. phys_addr_t size = PAGE_SIZE * memslot->npages;
  636. hva_t reg_end = hva + size;
  637. /*
  638. * A memory region could potentially cover multiple VMAs, and any holes
  639. * between them, so iterate over all of them to find out if we should
  640. * unmap any of them.
  641. *
  642. * +--------------------------------------------+
  643. * +---------------+----------------+ +----------------+
  644. * | : VMA 1 | VMA 2 | | VMA 3 : |
  645. * +---------------+----------------+ +----------------+
  646. * | memory region |
  647. * +--------------------------------------------+
  648. */
  649. do {
  650. struct vm_area_struct *vma = find_vma(current->mm, hva);
  651. hva_t vm_start, vm_end;
  652. if (!vma || vma->vm_start >= reg_end)
  653. break;
  654. /*
  655. * Take the intersection of this VMA with the memory region
  656. */
  657. vm_start = max(hva, vma->vm_start);
  658. vm_end = min(reg_end, vma->vm_end);
  659. if (!(vma->vm_flags & VM_PFNMAP)) {
  660. gpa_t gpa = addr + (vm_start - memslot->userspace_addr);
  661. unmap_stage2_range(kvm, gpa, vm_end - vm_start);
  662. }
  663. hva = vm_end;
  664. } while (hva < reg_end);
  665. }
  666. /**
  667. * stage2_unmap_vm - Unmap Stage-2 RAM mappings
  668. * @kvm: The struct kvm pointer
  669. *
  670. * Go through the memregions and unmap any reguler RAM
  671. * backing memory already mapped to the VM.
  672. */
  673. void stage2_unmap_vm(struct kvm *kvm)
  674. {
  675. struct kvm_memslots *slots;
  676. struct kvm_memory_slot *memslot;
  677. int idx;
  678. idx = srcu_read_lock(&kvm->srcu);
  679. spin_lock(&kvm->mmu_lock);
  680. slots = kvm_memslots(kvm);
  681. kvm_for_each_memslot(memslot, slots)
  682. stage2_unmap_memslot(kvm, memslot);
  683. spin_unlock(&kvm->mmu_lock);
  684. srcu_read_unlock(&kvm->srcu, idx);
  685. }
  686. /**
  687. * kvm_free_stage2_pgd - free all stage-2 tables
  688. * @kvm: The KVM struct pointer for the VM.
  689. *
  690. * Walks the level-1 page table pointed to by kvm->arch.pgd and frees all
  691. * underlying level-2 and level-3 tables before freeing the actual level-1 table
  692. * and setting the struct pointer to NULL.
  693. *
  694. * Note we don't need locking here as this is only called when the VM is
  695. * destroyed, which can only be done once.
  696. */
  697. void kvm_free_stage2_pgd(struct kvm *kvm)
  698. {
  699. if (kvm->arch.pgd == NULL)
  700. return;
  701. unmap_stage2_range(kvm, 0, KVM_PHYS_SIZE);
  702. kvm_free_hwpgd(kvm_get_hwpgd(kvm));
  703. if (KVM_PREALLOC_LEVEL > 0)
  704. kfree(kvm->arch.pgd);
  705. kvm->arch.pgd = NULL;
  706. }
  707. static pud_t *stage2_get_pud(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
  708. phys_addr_t addr)
  709. {
  710. pgd_t *pgd;
  711. pud_t *pud;
  712. pgd = kvm->arch.pgd + kvm_pgd_index(addr);
  713. if (WARN_ON(pgd_none(*pgd))) {
  714. if (!cache)
  715. return NULL;
  716. pud = mmu_memory_cache_alloc(cache);
  717. pgd_populate(NULL, pgd, pud);
  718. get_page(virt_to_page(pgd));
  719. }
  720. return pud_offset(pgd, addr);
  721. }
  722. static pmd_t *stage2_get_pmd(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
  723. phys_addr_t addr)
  724. {
  725. pud_t *pud;
  726. pmd_t *pmd;
  727. pud = stage2_get_pud(kvm, cache, addr);
  728. if (pud_none(*pud)) {
  729. if (!cache)
  730. return NULL;
  731. pmd = mmu_memory_cache_alloc(cache);
  732. pud_populate(NULL, pud, pmd);
  733. get_page(virt_to_page(pud));
  734. }
  735. return pmd_offset(pud, addr);
  736. }
  737. static int stage2_set_pmd_huge(struct kvm *kvm, struct kvm_mmu_memory_cache
  738. *cache, phys_addr_t addr, const pmd_t *new_pmd)
  739. {
  740. pmd_t *pmd, old_pmd;
  741. pmd = stage2_get_pmd(kvm, cache, addr);
  742. VM_BUG_ON(!pmd);
  743. /*
  744. * Mapping in huge pages should only happen through a fault. If a
  745. * page is merged into a transparent huge page, the individual
  746. * subpages of that huge page should be unmapped through MMU
  747. * notifiers before we get here.
  748. *
  749. * Merging of CompoundPages is not supported; they should become
  750. * splitting first, unmapped, merged, and mapped back in on-demand.
  751. */
  752. VM_BUG_ON(pmd_present(*pmd) && pmd_pfn(*pmd) != pmd_pfn(*new_pmd));
  753. old_pmd = *pmd;
  754. kvm_set_pmd(pmd, *new_pmd);
  755. if (pmd_present(old_pmd))
  756. kvm_tlb_flush_vmid_ipa(kvm, addr);
  757. else
  758. get_page(virt_to_page(pmd));
  759. return 0;
  760. }
  761. static int stage2_set_pte(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
  762. phys_addr_t addr, const pte_t *new_pte,
  763. unsigned long flags)
  764. {
  765. pmd_t *pmd;
  766. pte_t *pte, old_pte;
  767. bool iomap = flags & KVM_S2PTE_FLAG_IS_IOMAP;
  768. bool logging_active = flags & KVM_S2_FLAG_LOGGING_ACTIVE;
  769. VM_BUG_ON(logging_active && !cache);
  770. /* Create stage-2 page table mapping - Levels 0 and 1 */
  771. pmd = stage2_get_pmd(kvm, cache, addr);
  772. if (!pmd) {
  773. /*
  774. * Ignore calls from kvm_set_spte_hva for unallocated
  775. * address ranges.
  776. */
  777. return 0;
  778. }
  779. /*
  780. * While dirty page logging - dissolve huge PMD, then continue on to
  781. * allocate page.
  782. */
  783. if (logging_active)
  784. stage2_dissolve_pmd(kvm, addr, pmd);
  785. /* Create stage-2 page mappings - Level 2 */
  786. if (pmd_none(*pmd)) {
  787. if (!cache)
  788. return 0; /* ignore calls from kvm_set_spte_hva */
  789. pte = mmu_memory_cache_alloc(cache);
  790. kvm_clean_pte(pte);
  791. pmd_populate_kernel(NULL, pmd, pte);
  792. get_page(virt_to_page(pmd));
  793. }
  794. pte = pte_offset_kernel(pmd, addr);
  795. if (iomap && pte_present(*pte))
  796. return -EFAULT;
  797. /* Create 2nd stage page table mapping - Level 3 */
  798. old_pte = *pte;
  799. kvm_set_pte(pte, *new_pte);
  800. if (pte_present(old_pte))
  801. kvm_tlb_flush_vmid_ipa(kvm, addr);
  802. else
  803. get_page(virt_to_page(pte));
  804. return 0;
  805. }
  806. /**
  807. * kvm_phys_addr_ioremap - map a device range to guest IPA
  808. *
  809. * @kvm: The KVM pointer
  810. * @guest_ipa: The IPA at which to insert the mapping
  811. * @pa: The physical address of the device
  812. * @size: The size of the mapping
  813. */
  814. int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
  815. phys_addr_t pa, unsigned long size, bool writable)
  816. {
  817. phys_addr_t addr, end;
  818. int ret = 0;
  819. unsigned long pfn;
  820. struct kvm_mmu_memory_cache cache = { 0, };
  821. end = (guest_ipa + size + PAGE_SIZE - 1) & PAGE_MASK;
  822. pfn = __phys_to_pfn(pa);
  823. for (addr = guest_ipa; addr < end; addr += PAGE_SIZE) {
  824. pte_t pte = pfn_pte(pfn, PAGE_S2_DEVICE);
  825. if (writable)
  826. kvm_set_s2pte_writable(&pte);
  827. ret = mmu_topup_memory_cache(&cache, KVM_MMU_CACHE_MIN_PAGES,
  828. KVM_NR_MEM_OBJS);
  829. if (ret)
  830. goto out;
  831. spin_lock(&kvm->mmu_lock);
  832. ret = stage2_set_pte(kvm, &cache, addr, &pte,
  833. KVM_S2PTE_FLAG_IS_IOMAP);
  834. spin_unlock(&kvm->mmu_lock);
  835. if (ret)
  836. goto out;
  837. pfn++;
  838. }
  839. out:
  840. mmu_free_memory_cache(&cache);
  841. return ret;
  842. }
  843. static bool transparent_hugepage_adjust(pfn_t *pfnp, phys_addr_t *ipap)
  844. {
  845. pfn_t pfn = *pfnp;
  846. gfn_t gfn = *ipap >> PAGE_SHIFT;
  847. if (PageTransCompound(pfn_to_page(pfn))) {
  848. unsigned long mask;
  849. /*
  850. * The address we faulted on is backed by a transparent huge
  851. * page. However, because we map the compound huge page and
  852. * not the individual tail page, we need to transfer the
  853. * refcount to the head page. We have to be careful that the
  854. * THP doesn't start to split while we are adjusting the
  855. * refcounts.
  856. *
  857. * We are sure this doesn't happen, because mmu_notifier_retry
  858. * was successful and we are holding the mmu_lock, so if this
  859. * THP is trying to split, it will be blocked in the mmu
  860. * notifier before touching any of the pages, specifically
  861. * before being able to call __split_huge_page_refcount().
  862. *
  863. * We can therefore safely transfer the refcount from PG_tail
  864. * to PG_head and switch the pfn from a tail page to the head
  865. * page accordingly.
  866. */
  867. mask = PTRS_PER_PMD - 1;
  868. VM_BUG_ON((gfn & mask) != (pfn & mask));
  869. if (pfn & mask) {
  870. *ipap &= PMD_MASK;
  871. kvm_release_pfn_clean(pfn);
  872. pfn &= ~mask;
  873. kvm_get_pfn(pfn);
  874. *pfnp = pfn;
  875. }
  876. return true;
  877. }
  878. return false;
  879. }
  880. static bool kvm_is_write_fault(struct kvm_vcpu *vcpu)
  881. {
  882. if (kvm_vcpu_trap_is_iabt(vcpu))
  883. return false;
  884. return kvm_vcpu_dabt_iswrite(vcpu);
  885. }
  886. static bool kvm_is_device_pfn(unsigned long pfn)
  887. {
  888. return !pfn_valid(pfn);
  889. }
  890. /**
  891. * stage2_wp_ptes - write protect PMD range
  892. * @pmd: pointer to pmd entry
  893. * @addr: range start address
  894. * @end: range end address
  895. */
  896. static void stage2_wp_ptes(pmd_t *pmd, phys_addr_t addr, phys_addr_t end)
  897. {
  898. pte_t *pte;
  899. pte = pte_offset_kernel(pmd, addr);
  900. do {
  901. if (!pte_none(*pte)) {
  902. if (!kvm_s2pte_readonly(pte))
  903. kvm_set_s2pte_readonly(pte);
  904. }
  905. } while (pte++, addr += PAGE_SIZE, addr != end);
  906. }
  907. /**
  908. * stage2_wp_pmds - write protect PUD range
  909. * @pud: pointer to pud entry
  910. * @addr: range start address
  911. * @end: range end address
  912. */
  913. static void stage2_wp_pmds(pud_t *pud, phys_addr_t addr, phys_addr_t end)
  914. {
  915. pmd_t *pmd;
  916. phys_addr_t next;
  917. pmd = pmd_offset(pud, addr);
  918. do {
  919. next = kvm_pmd_addr_end(addr, end);
  920. if (!pmd_none(*pmd)) {
  921. if (kvm_pmd_huge(*pmd)) {
  922. if (!kvm_s2pmd_readonly(pmd))
  923. kvm_set_s2pmd_readonly(pmd);
  924. } else {
  925. stage2_wp_ptes(pmd, addr, next);
  926. }
  927. }
  928. } while (pmd++, addr = next, addr != end);
  929. }
  930. /**
  931. * stage2_wp_puds - write protect PGD range
  932. * @pgd: pointer to pgd entry
  933. * @addr: range start address
  934. * @end: range end address
  935. *
  936. * Process PUD entries, for a huge PUD we cause a panic.
  937. */
  938. static void stage2_wp_puds(pgd_t *pgd, phys_addr_t addr, phys_addr_t end)
  939. {
  940. pud_t *pud;
  941. phys_addr_t next;
  942. pud = pud_offset(pgd, addr);
  943. do {
  944. next = kvm_pud_addr_end(addr, end);
  945. if (!pud_none(*pud)) {
  946. /* TODO:PUD not supported, revisit later if supported */
  947. BUG_ON(kvm_pud_huge(*pud));
  948. stage2_wp_pmds(pud, addr, next);
  949. }
  950. } while (pud++, addr = next, addr != end);
  951. }
  952. /**
  953. * stage2_wp_range() - write protect stage2 memory region range
  954. * @kvm: The KVM pointer
  955. * @addr: Start address of range
  956. * @end: End address of range
  957. */
  958. static void stage2_wp_range(struct kvm *kvm, phys_addr_t addr, phys_addr_t end)
  959. {
  960. pgd_t *pgd;
  961. phys_addr_t next;
  962. pgd = kvm->arch.pgd + kvm_pgd_index(addr);
  963. do {
  964. /*
  965. * Release kvm_mmu_lock periodically if the memory region is
  966. * large. Otherwise, we may see kernel panics with
  967. * CONFIG_DETECT_HUNG_TASK, CONFIG_LOCKUP_DETECTOR,
  968. * CONFIG_LOCKDEP. Additionally, holding the lock too long
  969. * will also starve other vCPUs.
  970. */
  971. if (need_resched() || spin_needbreak(&kvm->mmu_lock))
  972. cond_resched_lock(&kvm->mmu_lock);
  973. next = kvm_pgd_addr_end(addr, end);
  974. if (pgd_present(*pgd))
  975. stage2_wp_puds(pgd, addr, next);
  976. } while (pgd++, addr = next, addr != end);
  977. }
  978. /**
  979. * kvm_mmu_wp_memory_region() - write protect stage 2 entries for memory slot
  980. * @kvm: The KVM pointer
  981. * @slot: The memory slot to write protect
  982. *
  983. * Called to start logging dirty pages after memory region
  984. * KVM_MEM_LOG_DIRTY_PAGES operation is called. After this function returns
  985. * all present PMD and PTEs are write protected in the memory region.
  986. * Afterwards read of dirty page log can be called.
  987. *
  988. * Acquires kvm_mmu_lock. Called with kvm->slots_lock mutex acquired,
  989. * serializing operations for VM memory regions.
  990. */
  991. void kvm_mmu_wp_memory_region(struct kvm *kvm, int slot)
  992. {
  993. struct kvm_memory_slot *memslot = id_to_memslot(kvm->memslots, slot);
  994. phys_addr_t start = memslot->base_gfn << PAGE_SHIFT;
  995. phys_addr_t end = (memslot->base_gfn + memslot->npages) << PAGE_SHIFT;
  996. spin_lock(&kvm->mmu_lock);
  997. stage2_wp_range(kvm, start, end);
  998. spin_unlock(&kvm->mmu_lock);
  999. kvm_flush_remote_tlbs(kvm);
  1000. }
  1001. /**
  1002. * kvm_mmu_write_protect_pt_masked() - write protect dirty pages
  1003. * @kvm: The KVM pointer
  1004. * @slot: The memory slot associated with mask
  1005. * @gfn_offset: The gfn offset in memory slot
  1006. * @mask: The mask of dirty pages at offset 'gfn_offset' in this memory
  1007. * slot to be write protected
  1008. *
  1009. * Walks bits set in mask write protects the associated pte's. Caller must
  1010. * acquire kvm_mmu_lock.
  1011. */
  1012. static void kvm_mmu_write_protect_pt_masked(struct kvm *kvm,
  1013. struct kvm_memory_slot *slot,
  1014. gfn_t gfn_offset, unsigned long mask)
  1015. {
  1016. phys_addr_t base_gfn = slot->base_gfn + gfn_offset;
  1017. phys_addr_t start = (base_gfn + __ffs(mask)) << PAGE_SHIFT;
  1018. phys_addr_t end = (base_gfn + __fls(mask) + 1) << PAGE_SHIFT;
  1019. stage2_wp_range(kvm, start, end);
  1020. }
  1021. /*
  1022. * kvm_arch_mmu_enable_log_dirty_pt_masked - enable dirty logging for selected
  1023. * dirty pages.
  1024. *
  1025. * It calls kvm_mmu_write_protect_pt_masked to write protect selected pages to
  1026. * enable dirty logging for them.
  1027. */
  1028. void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm,
  1029. struct kvm_memory_slot *slot,
  1030. gfn_t gfn_offset, unsigned long mask)
  1031. {
  1032. kvm_mmu_write_protect_pt_masked(kvm, slot, gfn_offset, mask);
  1033. }
  1034. static void coherent_cache_guest_page(struct kvm_vcpu *vcpu, pfn_t pfn,
  1035. unsigned long size, bool uncached)
  1036. {
  1037. __coherent_cache_guest_page(vcpu, pfn, size, uncached);
  1038. }
  1039. static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
  1040. struct kvm_memory_slot *memslot, unsigned long hva,
  1041. unsigned long fault_status)
  1042. {
  1043. int ret;
  1044. bool write_fault, writable, hugetlb = false, force_pte = false;
  1045. unsigned long mmu_seq;
  1046. gfn_t gfn = fault_ipa >> PAGE_SHIFT;
  1047. struct kvm *kvm = vcpu->kvm;
  1048. struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
  1049. struct vm_area_struct *vma;
  1050. pfn_t pfn;
  1051. pgprot_t mem_type = PAGE_S2;
  1052. bool fault_ipa_uncached;
  1053. bool logging_active = memslot_is_logging(memslot);
  1054. unsigned long flags = 0;
  1055. write_fault = kvm_is_write_fault(vcpu);
  1056. if (fault_status == FSC_PERM && !write_fault) {
  1057. kvm_err("Unexpected L2 read permission error\n");
  1058. return -EFAULT;
  1059. }
  1060. /* Let's check if we will get back a huge page backed by hugetlbfs */
  1061. down_read(&current->mm->mmap_sem);
  1062. vma = find_vma_intersection(current->mm, hva, hva + 1);
  1063. if (unlikely(!vma)) {
  1064. kvm_err("Failed to find VMA for hva 0x%lx\n", hva);
  1065. up_read(&current->mm->mmap_sem);
  1066. return -EFAULT;
  1067. }
  1068. if (is_vm_hugetlb_page(vma) && !logging_active) {
  1069. hugetlb = true;
  1070. gfn = (fault_ipa & PMD_MASK) >> PAGE_SHIFT;
  1071. } else {
  1072. /*
  1073. * Pages belonging to memslots that don't have the same
  1074. * alignment for userspace and IPA cannot be mapped using
  1075. * block descriptors even if the pages belong to a THP for
  1076. * the process, because the stage-2 block descriptor will
  1077. * cover more than a single THP and we loose atomicity for
  1078. * unmapping, updates, and splits of the THP or other pages
  1079. * in the stage-2 block range.
  1080. */
  1081. if ((memslot->userspace_addr & ~PMD_MASK) !=
  1082. ((memslot->base_gfn << PAGE_SHIFT) & ~PMD_MASK))
  1083. force_pte = true;
  1084. }
  1085. up_read(&current->mm->mmap_sem);
  1086. /* We need minimum second+third level pages */
  1087. ret = mmu_topup_memory_cache(memcache, KVM_MMU_CACHE_MIN_PAGES,
  1088. KVM_NR_MEM_OBJS);
  1089. if (ret)
  1090. return ret;
  1091. mmu_seq = vcpu->kvm->mmu_notifier_seq;
  1092. /*
  1093. * Ensure the read of mmu_notifier_seq happens before we call
  1094. * gfn_to_pfn_prot (which calls get_user_pages), so that we don't risk
  1095. * the page we just got a reference to gets unmapped before we have a
  1096. * chance to grab the mmu_lock, which ensure that if the page gets
  1097. * unmapped afterwards, the call to kvm_unmap_hva will take it away
  1098. * from us again properly. This smp_rmb() interacts with the smp_wmb()
  1099. * in kvm_mmu_notifier_invalidate_<page|range_end>.
  1100. */
  1101. smp_rmb();
  1102. pfn = gfn_to_pfn_prot(kvm, gfn, write_fault, &writable);
  1103. if (is_error_pfn(pfn))
  1104. return -EFAULT;
  1105. if (kvm_is_device_pfn(pfn)) {
  1106. mem_type = PAGE_S2_DEVICE;
  1107. flags |= KVM_S2PTE_FLAG_IS_IOMAP;
  1108. } else if (logging_active) {
  1109. /*
  1110. * Faults on pages in a memslot with logging enabled
  1111. * should not be mapped with huge pages (it introduces churn
  1112. * and performance degradation), so force a pte mapping.
  1113. */
  1114. force_pte = true;
  1115. flags |= KVM_S2_FLAG_LOGGING_ACTIVE;
  1116. /*
  1117. * Only actually map the page as writable if this was a write
  1118. * fault.
  1119. */
  1120. if (!write_fault)
  1121. writable = false;
  1122. }
  1123. spin_lock(&kvm->mmu_lock);
  1124. if (mmu_notifier_retry(kvm, mmu_seq))
  1125. goto out_unlock;
  1126. if (!hugetlb && !force_pte)
  1127. hugetlb = transparent_hugepage_adjust(&pfn, &fault_ipa);
  1128. fault_ipa_uncached = memslot->flags & KVM_MEMSLOT_INCOHERENT;
  1129. if (hugetlb) {
  1130. pmd_t new_pmd = pfn_pmd(pfn, mem_type);
  1131. new_pmd = pmd_mkhuge(new_pmd);
  1132. if (writable) {
  1133. kvm_set_s2pmd_writable(&new_pmd);
  1134. kvm_set_pfn_dirty(pfn);
  1135. }
  1136. coherent_cache_guest_page(vcpu, pfn, PMD_SIZE, fault_ipa_uncached);
  1137. ret = stage2_set_pmd_huge(kvm, memcache, fault_ipa, &new_pmd);
  1138. } else {
  1139. pte_t new_pte = pfn_pte(pfn, mem_type);
  1140. if (writable) {
  1141. kvm_set_s2pte_writable(&new_pte);
  1142. kvm_set_pfn_dirty(pfn);
  1143. mark_page_dirty(kvm, gfn);
  1144. }
  1145. coherent_cache_guest_page(vcpu, pfn, PAGE_SIZE, fault_ipa_uncached);
  1146. ret = stage2_set_pte(kvm, memcache, fault_ipa, &new_pte, flags);
  1147. }
  1148. out_unlock:
  1149. spin_unlock(&kvm->mmu_lock);
  1150. kvm_release_pfn_clean(pfn);
  1151. return ret;
  1152. }
  1153. /**
  1154. * kvm_handle_guest_abort - handles all 2nd stage aborts
  1155. * @vcpu: the VCPU pointer
  1156. * @run: the kvm_run structure
  1157. *
  1158. * Any abort that gets to the host is almost guaranteed to be caused by a
  1159. * missing second stage translation table entry, which can mean that either the
  1160. * guest simply needs more memory and we must allocate an appropriate page or it
  1161. * can mean that the guest tried to access I/O memory, which is emulated by user
  1162. * space. The distinction is based on the IPA causing the fault and whether this
  1163. * memory region has been registered as standard RAM by user space.
  1164. */
  1165. int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run)
  1166. {
  1167. unsigned long fault_status;
  1168. phys_addr_t fault_ipa;
  1169. struct kvm_memory_slot *memslot;
  1170. unsigned long hva;
  1171. bool is_iabt, write_fault, writable;
  1172. gfn_t gfn;
  1173. int ret, idx;
  1174. is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
  1175. fault_ipa = kvm_vcpu_get_fault_ipa(vcpu);
  1176. trace_kvm_guest_fault(*vcpu_pc(vcpu), kvm_vcpu_get_hsr(vcpu),
  1177. kvm_vcpu_get_hfar(vcpu), fault_ipa);
  1178. /* Check the stage-2 fault is trans. fault or write fault */
  1179. fault_status = kvm_vcpu_trap_get_fault_type(vcpu);
  1180. if (fault_status != FSC_FAULT && fault_status != FSC_PERM) {
  1181. kvm_err("Unsupported FSC: EC=%#x xFSC=%#lx ESR_EL2=%#lx\n",
  1182. kvm_vcpu_trap_get_class(vcpu),
  1183. (unsigned long)kvm_vcpu_trap_get_fault(vcpu),
  1184. (unsigned long)kvm_vcpu_get_hsr(vcpu));
  1185. return -EFAULT;
  1186. }
  1187. idx = srcu_read_lock(&vcpu->kvm->srcu);
  1188. gfn = fault_ipa >> PAGE_SHIFT;
  1189. memslot = gfn_to_memslot(vcpu->kvm, gfn);
  1190. hva = gfn_to_hva_memslot_prot(memslot, gfn, &writable);
  1191. write_fault = kvm_is_write_fault(vcpu);
  1192. if (kvm_is_error_hva(hva) || (write_fault && !writable)) {
  1193. if (is_iabt) {
  1194. /* Prefetch Abort on I/O address */
  1195. kvm_inject_pabt(vcpu, kvm_vcpu_get_hfar(vcpu));
  1196. ret = 1;
  1197. goto out_unlock;
  1198. }
  1199. /*
  1200. * The IPA is reported as [MAX:12], so we need to
  1201. * complement it with the bottom 12 bits from the
  1202. * faulting VA. This is always 12 bits, irrespective
  1203. * of the page size.
  1204. */
  1205. fault_ipa |= kvm_vcpu_get_hfar(vcpu) & ((1 << 12) - 1);
  1206. ret = io_mem_abort(vcpu, run, fault_ipa);
  1207. goto out_unlock;
  1208. }
  1209. /* Userspace should not be able to register out-of-bounds IPAs */
  1210. VM_BUG_ON(fault_ipa >= KVM_PHYS_SIZE);
  1211. ret = user_mem_abort(vcpu, fault_ipa, memslot, hva, fault_status);
  1212. if (ret == 0)
  1213. ret = 1;
  1214. out_unlock:
  1215. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  1216. return ret;
  1217. }
  1218. static void handle_hva_to_gpa(struct kvm *kvm,
  1219. unsigned long start,
  1220. unsigned long end,
  1221. void (*handler)(struct kvm *kvm,
  1222. gpa_t gpa, void *data),
  1223. void *data)
  1224. {
  1225. struct kvm_memslots *slots;
  1226. struct kvm_memory_slot *memslot;
  1227. slots = kvm_memslots(kvm);
  1228. /* we only care about the pages that the guest sees */
  1229. kvm_for_each_memslot(memslot, slots) {
  1230. unsigned long hva_start, hva_end;
  1231. gfn_t gfn, gfn_end;
  1232. hva_start = max(start, memslot->userspace_addr);
  1233. hva_end = min(end, memslot->userspace_addr +
  1234. (memslot->npages << PAGE_SHIFT));
  1235. if (hva_start >= hva_end)
  1236. continue;
  1237. /*
  1238. * {gfn(page) | page intersects with [hva_start, hva_end)} =
  1239. * {gfn_start, gfn_start+1, ..., gfn_end-1}.
  1240. */
  1241. gfn = hva_to_gfn_memslot(hva_start, memslot);
  1242. gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot);
  1243. for (; gfn < gfn_end; ++gfn) {
  1244. gpa_t gpa = gfn << PAGE_SHIFT;
  1245. handler(kvm, gpa, data);
  1246. }
  1247. }
  1248. }
  1249. static void kvm_unmap_hva_handler(struct kvm *kvm, gpa_t gpa, void *data)
  1250. {
  1251. unmap_stage2_range(kvm, gpa, PAGE_SIZE);
  1252. }
  1253. int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
  1254. {
  1255. unsigned long end = hva + PAGE_SIZE;
  1256. if (!kvm->arch.pgd)
  1257. return 0;
  1258. trace_kvm_unmap_hva(hva);
  1259. handle_hva_to_gpa(kvm, hva, end, &kvm_unmap_hva_handler, NULL);
  1260. return 0;
  1261. }
  1262. int kvm_unmap_hva_range(struct kvm *kvm,
  1263. unsigned long start, unsigned long end)
  1264. {
  1265. if (!kvm->arch.pgd)
  1266. return 0;
  1267. trace_kvm_unmap_hva_range(start, end);
  1268. handle_hva_to_gpa(kvm, start, end, &kvm_unmap_hva_handler, NULL);
  1269. return 0;
  1270. }
  1271. static void kvm_set_spte_handler(struct kvm *kvm, gpa_t gpa, void *data)
  1272. {
  1273. pte_t *pte = (pte_t *)data;
  1274. /*
  1275. * We can always call stage2_set_pte with KVM_S2PTE_FLAG_LOGGING_ACTIVE
  1276. * flag clear because MMU notifiers will have unmapped a huge PMD before
  1277. * calling ->change_pte() (which in turn calls kvm_set_spte_hva()) and
  1278. * therefore stage2_set_pte() never needs to clear out a huge PMD
  1279. * through this calling path.
  1280. */
  1281. stage2_set_pte(kvm, NULL, gpa, pte, 0);
  1282. }
  1283. void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
  1284. {
  1285. unsigned long end = hva + PAGE_SIZE;
  1286. pte_t stage2_pte;
  1287. if (!kvm->arch.pgd)
  1288. return;
  1289. trace_kvm_set_spte_hva(hva);
  1290. stage2_pte = pfn_pte(pte_pfn(pte), PAGE_S2);
  1291. handle_hva_to_gpa(kvm, hva, end, &kvm_set_spte_handler, &stage2_pte);
  1292. }
  1293. void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu)
  1294. {
  1295. mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
  1296. }
  1297. phys_addr_t kvm_mmu_get_httbr(void)
  1298. {
  1299. return virt_to_phys(hyp_pgd);
  1300. }
  1301. phys_addr_t kvm_mmu_get_boot_httbr(void)
  1302. {
  1303. return virt_to_phys(boot_hyp_pgd);
  1304. }
  1305. phys_addr_t kvm_get_idmap_vector(void)
  1306. {
  1307. return hyp_idmap_vector;
  1308. }
  1309. int kvm_mmu_init(void)
  1310. {
  1311. int err;
  1312. hyp_idmap_start = kvm_virt_to_phys(__hyp_idmap_text_start);
  1313. hyp_idmap_end = kvm_virt_to_phys(__hyp_idmap_text_end);
  1314. hyp_idmap_vector = kvm_virt_to_phys(__kvm_hyp_init);
  1315. if ((hyp_idmap_start ^ hyp_idmap_end) & PAGE_MASK) {
  1316. /*
  1317. * Our init code is crossing a page boundary. Allocate
  1318. * a bounce page, copy the code over and use that.
  1319. */
  1320. size_t len = __hyp_idmap_text_end - __hyp_idmap_text_start;
  1321. phys_addr_t phys_base;
  1322. init_bounce_page = (void *)__get_free_page(GFP_KERNEL);
  1323. if (!init_bounce_page) {
  1324. kvm_err("Couldn't allocate HYP init bounce page\n");
  1325. err = -ENOMEM;
  1326. goto out;
  1327. }
  1328. memcpy(init_bounce_page, __hyp_idmap_text_start, len);
  1329. /*
  1330. * Warning: the code we just copied to the bounce page
  1331. * must be flushed to the point of coherency.
  1332. * Otherwise, the data may be sitting in L2, and HYP
  1333. * mode won't be able to observe it as it runs with
  1334. * caches off at that point.
  1335. */
  1336. kvm_flush_dcache_to_poc(init_bounce_page, len);
  1337. phys_base = kvm_virt_to_phys(init_bounce_page);
  1338. hyp_idmap_vector += phys_base - hyp_idmap_start;
  1339. hyp_idmap_start = phys_base;
  1340. hyp_idmap_end = phys_base + len;
  1341. kvm_info("Using HYP init bounce page @%lx\n",
  1342. (unsigned long)phys_base);
  1343. }
  1344. hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, hyp_pgd_order);
  1345. boot_hyp_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, hyp_pgd_order);
  1346. if (!hyp_pgd || !boot_hyp_pgd) {
  1347. kvm_err("Hyp mode PGD not allocated\n");
  1348. err = -ENOMEM;
  1349. goto out;
  1350. }
  1351. /* Create the idmap in the boot page tables */
  1352. err = __create_hyp_mappings(boot_hyp_pgd,
  1353. hyp_idmap_start, hyp_idmap_end,
  1354. __phys_to_pfn(hyp_idmap_start),
  1355. PAGE_HYP);
  1356. if (err) {
  1357. kvm_err("Failed to idmap %lx-%lx\n",
  1358. hyp_idmap_start, hyp_idmap_end);
  1359. goto out;
  1360. }
  1361. /* Map the very same page at the trampoline VA */
  1362. err = __create_hyp_mappings(boot_hyp_pgd,
  1363. TRAMPOLINE_VA, TRAMPOLINE_VA + PAGE_SIZE,
  1364. __phys_to_pfn(hyp_idmap_start),
  1365. PAGE_HYP);
  1366. if (err) {
  1367. kvm_err("Failed to map trampoline @%lx into boot HYP pgd\n",
  1368. TRAMPOLINE_VA);
  1369. goto out;
  1370. }
  1371. /* Map the same page again into the runtime page tables */
  1372. err = __create_hyp_mappings(hyp_pgd,
  1373. TRAMPOLINE_VA, TRAMPOLINE_VA + PAGE_SIZE,
  1374. __phys_to_pfn(hyp_idmap_start),
  1375. PAGE_HYP);
  1376. if (err) {
  1377. kvm_err("Failed to map trampoline @%lx into runtime HYP pgd\n",
  1378. TRAMPOLINE_VA);
  1379. goto out;
  1380. }
  1381. return 0;
  1382. out:
  1383. free_hyp_pgds();
  1384. return err;
  1385. }
  1386. void kvm_arch_commit_memory_region(struct kvm *kvm,
  1387. struct kvm_userspace_memory_region *mem,
  1388. const struct kvm_memory_slot *old,
  1389. enum kvm_mr_change change)
  1390. {
  1391. /*
  1392. * At this point memslot has been committed and there is an
  1393. * allocated dirty_bitmap[], dirty pages will be be tracked while the
  1394. * memory slot is write protected.
  1395. */
  1396. if (change != KVM_MR_DELETE && mem->flags & KVM_MEM_LOG_DIRTY_PAGES)
  1397. kvm_mmu_wp_memory_region(kvm, mem->slot);
  1398. }
  1399. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  1400. struct kvm_memory_slot *memslot,
  1401. struct kvm_userspace_memory_region *mem,
  1402. enum kvm_mr_change change)
  1403. {
  1404. hva_t hva = mem->userspace_addr;
  1405. hva_t reg_end = hva + mem->memory_size;
  1406. bool writable = !(mem->flags & KVM_MEM_READONLY);
  1407. int ret = 0;
  1408. if (change != KVM_MR_CREATE && change != KVM_MR_MOVE &&
  1409. change != KVM_MR_FLAGS_ONLY)
  1410. return 0;
  1411. /*
  1412. * Prevent userspace from creating a memory region outside of the IPA
  1413. * space addressable by the KVM guest IPA space.
  1414. */
  1415. if (memslot->base_gfn + memslot->npages >=
  1416. (KVM_PHYS_SIZE >> PAGE_SHIFT))
  1417. return -EFAULT;
  1418. /*
  1419. * A memory region could potentially cover multiple VMAs, and any holes
  1420. * between them, so iterate over all of them to find out if we can map
  1421. * any of them right now.
  1422. *
  1423. * +--------------------------------------------+
  1424. * +---------------+----------------+ +----------------+
  1425. * | : VMA 1 | VMA 2 | | VMA 3 : |
  1426. * +---------------+----------------+ +----------------+
  1427. * | memory region |
  1428. * +--------------------------------------------+
  1429. */
  1430. do {
  1431. struct vm_area_struct *vma = find_vma(current->mm, hva);
  1432. hva_t vm_start, vm_end;
  1433. if (!vma || vma->vm_start >= reg_end)
  1434. break;
  1435. /*
  1436. * Mapping a read-only VMA is only allowed if the
  1437. * memory region is configured as read-only.
  1438. */
  1439. if (writable && !(vma->vm_flags & VM_WRITE)) {
  1440. ret = -EPERM;
  1441. break;
  1442. }
  1443. /*
  1444. * Take the intersection of this VMA with the memory region
  1445. */
  1446. vm_start = max(hva, vma->vm_start);
  1447. vm_end = min(reg_end, vma->vm_end);
  1448. if (vma->vm_flags & VM_PFNMAP) {
  1449. gpa_t gpa = mem->guest_phys_addr +
  1450. (vm_start - mem->userspace_addr);
  1451. phys_addr_t pa = (vma->vm_pgoff << PAGE_SHIFT) +
  1452. vm_start - vma->vm_start;
  1453. /* IO region dirty page logging not allowed */
  1454. if (memslot->flags & KVM_MEM_LOG_DIRTY_PAGES)
  1455. return -EINVAL;
  1456. ret = kvm_phys_addr_ioremap(kvm, gpa, pa,
  1457. vm_end - vm_start,
  1458. writable);
  1459. if (ret)
  1460. break;
  1461. }
  1462. hva = vm_end;
  1463. } while (hva < reg_end);
  1464. if (change == KVM_MR_FLAGS_ONLY)
  1465. return ret;
  1466. spin_lock(&kvm->mmu_lock);
  1467. if (ret)
  1468. unmap_stage2_range(kvm, mem->guest_phys_addr, mem->memory_size);
  1469. else
  1470. stage2_flush_memslot(kvm, memslot);
  1471. spin_unlock(&kvm->mmu_lock);
  1472. return ret;
  1473. }
  1474. void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
  1475. struct kvm_memory_slot *dont)
  1476. {
  1477. }
  1478. int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
  1479. unsigned long npages)
  1480. {
  1481. /*
  1482. * Readonly memslots are not incoherent with the caches by definition,
  1483. * but in practice, they are used mostly to emulate ROMs or NOR flashes
  1484. * that the guest may consider devices and hence map as uncached.
  1485. * To prevent incoherency issues in these cases, tag all readonly
  1486. * regions as incoherent.
  1487. */
  1488. if (slot->flags & KVM_MEM_READONLY)
  1489. slot->flags |= KVM_MEMSLOT_INCOHERENT;
  1490. return 0;
  1491. }
  1492. void kvm_arch_memslots_updated(struct kvm *kvm)
  1493. {
  1494. }
  1495. void kvm_arch_flush_shadow_all(struct kvm *kvm)
  1496. {
  1497. }
  1498. void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
  1499. struct kvm_memory_slot *slot)
  1500. {
  1501. gpa_t gpa = slot->base_gfn << PAGE_SHIFT;
  1502. phys_addr_t size = slot->npages << PAGE_SHIFT;
  1503. spin_lock(&kvm->mmu_lock);
  1504. unmap_stage2_range(kvm, gpa, size);
  1505. spin_unlock(&kvm->mmu_lock);
  1506. }
  1507. /*
  1508. * See note at ARMv7 ARM B1.14.4 (TL;DR: S/W ops are not easily virtualized).
  1509. *
  1510. * Main problems:
  1511. * - S/W ops are local to a CPU (not broadcast)
  1512. * - We have line migration behind our back (speculation)
  1513. * - System caches don't support S/W at all (damn!)
  1514. *
  1515. * In the face of the above, the best we can do is to try and convert
  1516. * S/W ops to VA ops. Because the guest is not allowed to infer the
  1517. * S/W to PA mapping, it can only use S/W to nuke the whole cache,
  1518. * which is a rather good thing for us.
  1519. *
  1520. * Also, it is only used when turning caches on/off ("The expected
  1521. * usage of the cache maintenance instructions that operate by set/way
  1522. * is associated with the cache maintenance instructions associated
  1523. * with the powerdown and powerup of caches, if this is required by
  1524. * the implementation.").
  1525. *
  1526. * We use the following policy:
  1527. *
  1528. * - If we trap a S/W operation, we enable VM trapping to detect
  1529. * caches being turned on/off, and do a full clean.
  1530. *
  1531. * - We flush the caches on both caches being turned on and off.
  1532. *
  1533. * - Once the caches are enabled, we stop trapping VM ops.
  1534. */
  1535. void kvm_set_way_flush(struct kvm_vcpu *vcpu)
  1536. {
  1537. unsigned long hcr = vcpu_get_hcr(vcpu);
  1538. /*
  1539. * If this is the first time we do a S/W operation
  1540. * (i.e. HCR_TVM not set) flush the whole memory, and set the
  1541. * VM trapping.
  1542. *
  1543. * Otherwise, rely on the VM trapping to wait for the MMU +
  1544. * Caches to be turned off. At that point, we'll be able to
  1545. * clean the caches again.
  1546. */
  1547. if (!(hcr & HCR_TVM)) {
  1548. trace_kvm_set_way_flush(*vcpu_pc(vcpu),
  1549. vcpu_has_cache_enabled(vcpu));
  1550. stage2_flush_vm(vcpu->kvm);
  1551. vcpu_set_hcr(vcpu, hcr | HCR_TVM);
  1552. }
  1553. }
  1554. void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled)
  1555. {
  1556. bool now_enabled = vcpu_has_cache_enabled(vcpu);
  1557. /*
  1558. * If switching the MMU+caches on, need to invalidate the caches.
  1559. * If switching it off, need to clean the caches.
  1560. * Clean + invalidate does the trick always.
  1561. */
  1562. if (now_enabled != was_enabled)
  1563. stage2_flush_vm(vcpu->kvm);
  1564. /* Caches are now on, stop trapping VM ops (until a S/W op) */
  1565. if (now_enabled)
  1566. vcpu_set_hcr(vcpu, vcpu_get_hcr(vcpu) & ~HCR_TVM);
  1567. trace_kvm_toggle_cache(*vcpu_pc(vcpu), was_enabled, now_enabled);
  1568. }