mmu.c 50 KB

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