mmu.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929
  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. static bool kvm_is_device_pfn(unsigned long pfn)
  87. {
  88. return !pfn_valid(pfn);
  89. }
  90. /**
  91. * stage2_dissolve_pmd() - clear and flush huge PMD entry
  92. * @kvm: pointer to kvm structure.
  93. * @addr: IPA
  94. * @pmd: pmd pointer for IPA
  95. *
  96. * Function clears a PMD entry, flushes addr 1st and 2nd stage TLBs. Marks all
  97. * pages in the range dirty.
  98. */
  99. static void stage2_dissolve_pmd(struct kvm *kvm, phys_addr_t addr, pmd_t *pmd)
  100. {
  101. if (!kvm_pmd_huge(*pmd))
  102. return;
  103. pmd_clear(pmd);
  104. kvm_tlb_flush_vmid_ipa(kvm, addr);
  105. put_page(virt_to_page(pmd));
  106. }
  107. static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
  108. int min, int max)
  109. {
  110. void *page;
  111. BUG_ON(max > KVM_NR_MEM_OBJS);
  112. if (cache->nobjs >= min)
  113. return 0;
  114. while (cache->nobjs < max) {
  115. page = (void *)__get_free_page(PGALLOC_GFP);
  116. if (!page)
  117. return -ENOMEM;
  118. cache->objects[cache->nobjs++] = page;
  119. }
  120. return 0;
  121. }
  122. static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc)
  123. {
  124. while (mc->nobjs)
  125. free_page((unsigned long)mc->objects[--mc->nobjs]);
  126. }
  127. static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc)
  128. {
  129. void *p;
  130. BUG_ON(!mc || !mc->nobjs);
  131. p = mc->objects[--mc->nobjs];
  132. return p;
  133. }
  134. static void clear_pgd_entry(struct kvm *kvm, pgd_t *pgd, phys_addr_t addr)
  135. {
  136. pud_t *pud_table __maybe_unused = pud_offset(pgd, 0);
  137. pgd_clear(pgd);
  138. kvm_tlb_flush_vmid_ipa(kvm, addr);
  139. pud_free(NULL, pud_table);
  140. put_page(virt_to_page(pgd));
  141. }
  142. static void clear_pud_entry(struct kvm *kvm, pud_t *pud, phys_addr_t addr)
  143. {
  144. pmd_t *pmd_table = pmd_offset(pud, 0);
  145. VM_BUG_ON(pud_huge(*pud));
  146. pud_clear(pud);
  147. kvm_tlb_flush_vmid_ipa(kvm, addr);
  148. pmd_free(NULL, pmd_table);
  149. put_page(virt_to_page(pud));
  150. }
  151. static void clear_pmd_entry(struct kvm *kvm, pmd_t *pmd, phys_addr_t addr)
  152. {
  153. pte_t *pte_table = pte_offset_kernel(pmd, 0);
  154. VM_BUG_ON(kvm_pmd_huge(*pmd));
  155. pmd_clear(pmd);
  156. kvm_tlb_flush_vmid_ipa(kvm, addr);
  157. pte_free_kernel(NULL, pte_table);
  158. put_page(virt_to_page(pmd));
  159. }
  160. /*
  161. * Unmapping vs dcache management:
  162. *
  163. * If a guest maps certain memory pages as uncached, all writes will
  164. * bypass the data cache and go directly to RAM. However, the CPUs
  165. * can still speculate reads (not writes) and fill cache lines with
  166. * data.
  167. *
  168. * Those cache lines will be *clean* cache lines though, so a
  169. * clean+invalidate operation is equivalent to an invalidate
  170. * operation, because no cache lines are marked dirty.
  171. *
  172. * Those clean cache lines could be filled prior to an uncached write
  173. * by the guest, and the cache coherent IO subsystem would therefore
  174. * end up writing old data to disk.
  175. *
  176. * This is why right after unmapping a page/section and invalidating
  177. * the corresponding TLBs, we call kvm_flush_dcache_p*() to make sure
  178. * the IO subsystem will never hit in the cache.
  179. */
  180. static void unmap_ptes(struct kvm *kvm, pmd_t *pmd,
  181. phys_addr_t addr, phys_addr_t end)
  182. {
  183. phys_addr_t start_addr = addr;
  184. pte_t *pte, *start_pte;
  185. start_pte = pte = pte_offset_kernel(pmd, addr);
  186. do {
  187. if (!pte_none(*pte)) {
  188. pte_t old_pte = *pte;
  189. kvm_set_pte(pte, __pte(0));
  190. kvm_tlb_flush_vmid_ipa(kvm, addr);
  191. /* No need to invalidate the cache for device mappings */
  192. if (!kvm_is_device_pfn(pte_pfn(old_pte)))
  193. kvm_flush_dcache_pte(old_pte);
  194. put_page(virt_to_page(pte));
  195. }
  196. } while (pte++, addr += PAGE_SIZE, addr != end);
  197. if (kvm_pte_table_empty(kvm, start_pte))
  198. clear_pmd_entry(kvm, pmd, start_addr);
  199. }
  200. static void unmap_pmds(struct kvm *kvm, pud_t *pud,
  201. phys_addr_t addr, phys_addr_t end)
  202. {
  203. phys_addr_t next, start_addr = addr;
  204. pmd_t *pmd, *start_pmd;
  205. start_pmd = pmd = pmd_offset(pud, addr);
  206. do {
  207. next = kvm_pmd_addr_end(addr, end);
  208. if (!pmd_none(*pmd)) {
  209. if (kvm_pmd_huge(*pmd)) {
  210. pmd_t old_pmd = *pmd;
  211. pmd_clear(pmd);
  212. kvm_tlb_flush_vmid_ipa(kvm, addr);
  213. kvm_flush_dcache_pmd(old_pmd);
  214. put_page(virt_to_page(pmd));
  215. } else {
  216. unmap_ptes(kvm, pmd, addr, next);
  217. }
  218. }
  219. } while (pmd++, addr = next, addr != end);
  220. if (kvm_pmd_table_empty(kvm, start_pmd))
  221. clear_pud_entry(kvm, pud, start_addr);
  222. }
  223. static void unmap_puds(struct kvm *kvm, pgd_t *pgd,
  224. phys_addr_t addr, phys_addr_t end)
  225. {
  226. phys_addr_t next, start_addr = addr;
  227. pud_t *pud, *start_pud;
  228. start_pud = pud = pud_offset(pgd, addr);
  229. do {
  230. next = kvm_pud_addr_end(addr, end);
  231. if (!pud_none(*pud)) {
  232. if (pud_huge(*pud)) {
  233. pud_t old_pud = *pud;
  234. pud_clear(pud);
  235. kvm_tlb_flush_vmid_ipa(kvm, addr);
  236. kvm_flush_dcache_pud(old_pud);
  237. put_page(virt_to_page(pud));
  238. } else {
  239. unmap_pmds(kvm, pud, addr, next);
  240. }
  241. }
  242. } while (pud++, addr = next, addr != end);
  243. if (kvm_pud_table_empty(kvm, start_pud))
  244. clear_pgd_entry(kvm, pgd, start_addr);
  245. }
  246. static void unmap_range(struct kvm *kvm, pgd_t *pgdp,
  247. phys_addr_t start, u64 size)
  248. {
  249. pgd_t *pgd;
  250. phys_addr_t addr = start, end = start + size;
  251. phys_addr_t next;
  252. pgd = pgdp + kvm_pgd_index(addr);
  253. do {
  254. next = kvm_pgd_addr_end(addr, end);
  255. if (!pgd_none(*pgd))
  256. unmap_puds(kvm, pgd, addr, next);
  257. } while (pgd++, addr = next, addr != end);
  258. }
  259. static void stage2_flush_ptes(struct kvm *kvm, pmd_t *pmd,
  260. phys_addr_t addr, phys_addr_t end)
  261. {
  262. pte_t *pte;
  263. pte = pte_offset_kernel(pmd, addr);
  264. do {
  265. if (!pte_none(*pte) && !kvm_is_device_pfn(pte_pfn(*pte)))
  266. kvm_flush_dcache_pte(*pte);
  267. } while (pte++, addr += PAGE_SIZE, addr != end);
  268. }
  269. static void stage2_flush_pmds(struct kvm *kvm, pud_t *pud,
  270. phys_addr_t addr, phys_addr_t end)
  271. {
  272. pmd_t *pmd;
  273. phys_addr_t next;
  274. pmd = pmd_offset(pud, addr);
  275. do {
  276. next = kvm_pmd_addr_end(addr, end);
  277. if (!pmd_none(*pmd)) {
  278. if (kvm_pmd_huge(*pmd))
  279. kvm_flush_dcache_pmd(*pmd);
  280. else
  281. stage2_flush_ptes(kvm, pmd, addr, next);
  282. }
  283. } while (pmd++, addr = next, addr != end);
  284. }
  285. static void stage2_flush_puds(struct kvm *kvm, pgd_t *pgd,
  286. phys_addr_t addr, phys_addr_t end)
  287. {
  288. pud_t *pud;
  289. phys_addr_t next;
  290. pud = pud_offset(pgd, addr);
  291. do {
  292. next = kvm_pud_addr_end(addr, end);
  293. if (!pud_none(*pud)) {
  294. if (pud_huge(*pud))
  295. kvm_flush_dcache_pud(*pud);
  296. else
  297. stage2_flush_pmds(kvm, pud, addr, next);
  298. }
  299. } while (pud++, addr = next, addr != end);
  300. }
  301. static void stage2_flush_memslot(struct kvm *kvm,
  302. struct kvm_memory_slot *memslot)
  303. {
  304. phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
  305. phys_addr_t end = addr + PAGE_SIZE * memslot->npages;
  306. phys_addr_t next;
  307. pgd_t *pgd;
  308. pgd = kvm->arch.pgd + kvm_pgd_index(addr);
  309. do {
  310. next = kvm_pgd_addr_end(addr, end);
  311. stage2_flush_puds(kvm, pgd, addr, next);
  312. } while (pgd++, addr = next, addr != end);
  313. }
  314. /**
  315. * stage2_flush_vm - Invalidate cache for pages mapped in stage 2
  316. * @kvm: The struct kvm pointer
  317. *
  318. * Go through the stage 2 page tables and invalidate any cache lines
  319. * backing memory already mapped to the VM.
  320. */
  321. static void stage2_flush_vm(struct kvm *kvm)
  322. {
  323. struct kvm_memslots *slots;
  324. struct kvm_memory_slot *memslot;
  325. int idx;
  326. idx = srcu_read_lock(&kvm->srcu);
  327. spin_lock(&kvm->mmu_lock);
  328. slots = kvm_memslots(kvm);
  329. kvm_for_each_memslot(memslot, slots)
  330. stage2_flush_memslot(kvm, memslot);
  331. spin_unlock(&kvm->mmu_lock);
  332. srcu_read_unlock(&kvm->srcu, idx);
  333. }
  334. /**
  335. * free_boot_hyp_pgd - free HYP boot page tables
  336. *
  337. * Free the HYP boot page tables. The bounce page is also freed.
  338. */
  339. void free_boot_hyp_pgd(void)
  340. {
  341. mutex_lock(&kvm_hyp_pgd_mutex);
  342. if (boot_hyp_pgd) {
  343. unmap_range(NULL, boot_hyp_pgd, hyp_idmap_start, PAGE_SIZE);
  344. unmap_range(NULL, boot_hyp_pgd, TRAMPOLINE_VA, PAGE_SIZE);
  345. free_pages((unsigned long)boot_hyp_pgd, hyp_pgd_order);
  346. boot_hyp_pgd = NULL;
  347. }
  348. if (hyp_pgd)
  349. unmap_range(NULL, hyp_pgd, TRAMPOLINE_VA, PAGE_SIZE);
  350. mutex_unlock(&kvm_hyp_pgd_mutex);
  351. }
  352. /**
  353. * free_hyp_pgds - free Hyp-mode page tables
  354. *
  355. * Assumes hyp_pgd is a page table used strictly in Hyp-mode and
  356. * therefore contains either mappings in the kernel memory area (above
  357. * PAGE_OFFSET), or device mappings in the vmalloc range (from
  358. * VMALLOC_START to VMALLOC_END).
  359. *
  360. * boot_hyp_pgd should only map two pages for the init code.
  361. */
  362. void free_hyp_pgds(void)
  363. {
  364. unsigned long addr;
  365. free_boot_hyp_pgd();
  366. mutex_lock(&kvm_hyp_pgd_mutex);
  367. if (hyp_pgd) {
  368. for (addr = PAGE_OFFSET; virt_addr_valid(addr); addr += PGDIR_SIZE)
  369. unmap_range(NULL, hyp_pgd, KERN_TO_HYP(addr), PGDIR_SIZE);
  370. for (addr = VMALLOC_START; is_vmalloc_addr((void*)addr); addr += PGDIR_SIZE)
  371. unmap_range(NULL, hyp_pgd, KERN_TO_HYP(addr), PGDIR_SIZE);
  372. free_pages((unsigned long)hyp_pgd, hyp_pgd_order);
  373. hyp_pgd = NULL;
  374. }
  375. if (merged_hyp_pgd) {
  376. clear_page(merged_hyp_pgd);
  377. free_page((unsigned long)merged_hyp_pgd);
  378. merged_hyp_pgd = NULL;
  379. }
  380. mutex_unlock(&kvm_hyp_pgd_mutex);
  381. }
  382. static void create_hyp_pte_mappings(pmd_t *pmd, unsigned long start,
  383. unsigned long end, unsigned long pfn,
  384. pgprot_t prot)
  385. {
  386. pte_t *pte;
  387. unsigned long addr;
  388. addr = start;
  389. do {
  390. pte = pte_offset_kernel(pmd, addr);
  391. kvm_set_pte(pte, pfn_pte(pfn, prot));
  392. get_page(virt_to_page(pte));
  393. kvm_flush_dcache_to_poc(pte, sizeof(*pte));
  394. pfn++;
  395. } while (addr += PAGE_SIZE, addr != end);
  396. }
  397. static int create_hyp_pmd_mappings(pud_t *pud, unsigned long start,
  398. unsigned long end, unsigned long pfn,
  399. pgprot_t prot)
  400. {
  401. pmd_t *pmd;
  402. pte_t *pte;
  403. unsigned long addr, next;
  404. addr = start;
  405. do {
  406. pmd = pmd_offset(pud, addr);
  407. BUG_ON(pmd_sect(*pmd));
  408. if (pmd_none(*pmd)) {
  409. pte = pte_alloc_one_kernel(NULL, addr);
  410. if (!pte) {
  411. kvm_err("Cannot allocate Hyp pte\n");
  412. return -ENOMEM;
  413. }
  414. pmd_populate_kernel(NULL, pmd, pte);
  415. get_page(virt_to_page(pmd));
  416. kvm_flush_dcache_to_poc(pmd, sizeof(*pmd));
  417. }
  418. next = pmd_addr_end(addr, end);
  419. create_hyp_pte_mappings(pmd, addr, next, pfn, prot);
  420. pfn += (next - addr) >> PAGE_SHIFT;
  421. } while (addr = next, addr != end);
  422. return 0;
  423. }
  424. static int create_hyp_pud_mappings(pgd_t *pgd, unsigned long start,
  425. unsigned long end, unsigned long pfn,
  426. pgprot_t prot)
  427. {
  428. pud_t *pud;
  429. pmd_t *pmd;
  430. unsigned long addr, next;
  431. int ret;
  432. addr = start;
  433. do {
  434. pud = pud_offset(pgd, addr);
  435. if (pud_none_or_clear_bad(pud)) {
  436. pmd = pmd_alloc_one(NULL, addr);
  437. if (!pmd) {
  438. kvm_err("Cannot allocate Hyp pmd\n");
  439. return -ENOMEM;
  440. }
  441. pud_populate(NULL, pud, pmd);
  442. get_page(virt_to_page(pud));
  443. kvm_flush_dcache_to_poc(pud, sizeof(*pud));
  444. }
  445. next = pud_addr_end(addr, end);
  446. ret = create_hyp_pmd_mappings(pud, addr, next, pfn, prot);
  447. if (ret)
  448. return ret;
  449. pfn += (next - addr) >> PAGE_SHIFT;
  450. } while (addr = next, addr != end);
  451. return 0;
  452. }
  453. static int __create_hyp_mappings(pgd_t *pgdp,
  454. unsigned long start, unsigned long end,
  455. unsigned long pfn, pgprot_t prot)
  456. {
  457. pgd_t *pgd;
  458. pud_t *pud;
  459. unsigned long addr, next;
  460. int err = 0;
  461. mutex_lock(&kvm_hyp_pgd_mutex);
  462. addr = start & PAGE_MASK;
  463. end = PAGE_ALIGN(end);
  464. do {
  465. pgd = pgdp + pgd_index(addr);
  466. if (pgd_none(*pgd)) {
  467. pud = pud_alloc_one(NULL, addr);
  468. if (!pud) {
  469. kvm_err("Cannot allocate Hyp pud\n");
  470. err = -ENOMEM;
  471. goto out;
  472. }
  473. pgd_populate(NULL, pgd, pud);
  474. get_page(virt_to_page(pgd));
  475. kvm_flush_dcache_to_poc(pgd, sizeof(*pgd));
  476. }
  477. next = pgd_addr_end(addr, end);
  478. err = create_hyp_pud_mappings(pgd, addr, next, pfn, prot);
  479. if (err)
  480. goto out;
  481. pfn += (next - addr) >> PAGE_SHIFT;
  482. } while (addr = next, addr != end);
  483. out:
  484. mutex_unlock(&kvm_hyp_pgd_mutex);
  485. return err;
  486. }
  487. static phys_addr_t kvm_kaddr_to_phys(void *kaddr)
  488. {
  489. if (!is_vmalloc_addr(kaddr)) {
  490. BUG_ON(!virt_addr_valid(kaddr));
  491. return __pa(kaddr);
  492. } else {
  493. return page_to_phys(vmalloc_to_page(kaddr)) +
  494. offset_in_page(kaddr);
  495. }
  496. }
  497. /**
  498. * create_hyp_mappings - duplicate a kernel virtual address range in Hyp mode
  499. * @from: The virtual kernel start address of the range
  500. * @to: The virtual kernel end address of the range (exclusive)
  501. *
  502. * The same virtual address as the kernel virtual address is also used
  503. * in Hyp-mode mapping (modulo HYP_PAGE_OFFSET) to the same underlying
  504. * physical pages.
  505. */
  506. int create_hyp_mappings(void *from, void *to)
  507. {
  508. phys_addr_t phys_addr;
  509. unsigned long virt_addr;
  510. unsigned long start = KERN_TO_HYP((unsigned long)from);
  511. unsigned long end = KERN_TO_HYP((unsigned long)to);
  512. start = start & PAGE_MASK;
  513. end = PAGE_ALIGN(end);
  514. for (virt_addr = start; virt_addr < end; virt_addr += PAGE_SIZE) {
  515. int err;
  516. phys_addr = kvm_kaddr_to_phys(from + virt_addr - start);
  517. err = __create_hyp_mappings(hyp_pgd, virt_addr,
  518. virt_addr + PAGE_SIZE,
  519. __phys_to_pfn(phys_addr),
  520. PAGE_HYP);
  521. if (err)
  522. return err;
  523. }
  524. return 0;
  525. }
  526. /**
  527. * create_hyp_io_mappings - duplicate a kernel IO mapping into Hyp mode
  528. * @from: The kernel start VA of the range
  529. * @to: The kernel end VA of the range (exclusive)
  530. * @phys_addr: The physical start address which gets mapped
  531. *
  532. * The resulting HYP VA is the same as the kernel VA, modulo
  533. * HYP_PAGE_OFFSET.
  534. */
  535. int create_hyp_io_mappings(void *from, void *to, phys_addr_t phys_addr)
  536. {
  537. unsigned long start = KERN_TO_HYP((unsigned long)from);
  538. unsigned long end = KERN_TO_HYP((unsigned long)to);
  539. /* Check for a valid kernel IO mapping */
  540. if (!is_vmalloc_addr(from) || !is_vmalloc_addr(to - 1))
  541. return -EINVAL;
  542. return __create_hyp_mappings(hyp_pgd, start, end,
  543. __phys_to_pfn(phys_addr), PAGE_HYP_DEVICE);
  544. }
  545. /* Free the HW pgd, one page at a time */
  546. static void kvm_free_hwpgd(void *hwpgd)
  547. {
  548. free_pages_exact(hwpgd, kvm_get_hwpgd_size());
  549. }
  550. /* Allocate the HW PGD, making sure that each page gets its own refcount */
  551. static void *kvm_alloc_hwpgd(void)
  552. {
  553. unsigned int size = kvm_get_hwpgd_size();
  554. return alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
  555. }
  556. /**
  557. * kvm_alloc_stage2_pgd - allocate level-1 table for stage-2 translation.
  558. * @kvm: The KVM struct pointer for the VM.
  559. *
  560. * Allocates the 1st level table only of size defined by S2_PGD_ORDER (can
  561. * support either full 40-bit input addresses or limited to 32-bit input
  562. * addresses). Clears the allocated pages.
  563. *
  564. * Note we don't need locking here as this is only called when the VM is
  565. * created, which can only be done once.
  566. */
  567. int kvm_alloc_stage2_pgd(struct kvm *kvm)
  568. {
  569. pgd_t *pgd;
  570. void *hwpgd;
  571. if (kvm->arch.pgd != NULL) {
  572. kvm_err("kvm_arch already initialized?\n");
  573. return -EINVAL;
  574. }
  575. hwpgd = kvm_alloc_hwpgd();
  576. if (!hwpgd)
  577. return -ENOMEM;
  578. /* When the kernel uses more levels of page tables than the
  579. * guest, we allocate a fake PGD and pre-populate it to point
  580. * to the next-level page table, which will be the real
  581. * initial page table pointed to by the VTTBR.
  582. *
  583. * When KVM_PREALLOC_LEVEL==2, we allocate a single page for
  584. * the PMD and the kernel will use folded pud.
  585. * When KVM_PREALLOC_LEVEL==1, we allocate 2 consecutive PUD
  586. * pages.
  587. */
  588. if (KVM_PREALLOC_LEVEL > 0) {
  589. int i;
  590. /*
  591. * Allocate fake pgd for the page table manipulation macros to
  592. * work. This is not used by the hardware and we have no
  593. * alignment requirement for this allocation.
  594. */
  595. pgd = kmalloc(PTRS_PER_S2_PGD * sizeof(pgd_t),
  596. GFP_KERNEL | __GFP_ZERO);
  597. if (!pgd) {
  598. kvm_free_hwpgd(hwpgd);
  599. return -ENOMEM;
  600. }
  601. /* Plug the HW PGD into the fake one. */
  602. for (i = 0; i < PTRS_PER_S2_PGD; i++) {
  603. if (KVM_PREALLOC_LEVEL == 1)
  604. pgd_populate(NULL, pgd + i,
  605. (pud_t *)hwpgd + i * PTRS_PER_PUD);
  606. else if (KVM_PREALLOC_LEVEL == 2)
  607. pud_populate(NULL, pud_offset(pgd, 0) + i,
  608. (pmd_t *)hwpgd + i * PTRS_PER_PMD);
  609. }
  610. } else {
  611. /*
  612. * Allocate actual first-level Stage-2 page table used by the
  613. * hardware for Stage-2 page table walks.
  614. */
  615. pgd = (pgd_t *)hwpgd;
  616. }
  617. kvm_clean_pgd(pgd);
  618. kvm->arch.pgd = pgd;
  619. return 0;
  620. }
  621. /**
  622. * unmap_stage2_range -- Clear stage2 page table entries to unmap a range
  623. * @kvm: The VM pointer
  624. * @start: The intermediate physical base address of the range to unmap
  625. * @size: The size of the area to unmap
  626. *
  627. * Clear a range of stage-2 mappings, lowering the various ref-counts. Must
  628. * be called while holding mmu_lock (unless for freeing the stage2 pgd before
  629. * destroying the VM), otherwise another faulting VCPU may come in and mess
  630. * with things behind our backs.
  631. */
  632. static void unmap_stage2_range(struct kvm *kvm, phys_addr_t start, u64 size)
  633. {
  634. unmap_range(kvm, kvm->arch.pgd, start, size);
  635. }
  636. static void stage2_unmap_memslot(struct kvm *kvm,
  637. struct kvm_memory_slot *memslot)
  638. {
  639. hva_t hva = memslot->userspace_addr;
  640. phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
  641. phys_addr_t size = PAGE_SIZE * memslot->npages;
  642. hva_t reg_end = hva + size;
  643. /*
  644. * A memory region could potentially cover multiple VMAs, and any holes
  645. * between them, so iterate over all of them to find out if we should
  646. * unmap any of them.
  647. *
  648. * +--------------------------------------------+
  649. * +---------------+----------------+ +----------------+
  650. * | : VMA 1 | VMA 2 | | VMA 3 : |
  651. * +---------------+----------------+ +----------------+
  652. * | memory region |
  653. * +--------------------------------------------+
  654. */
  655. do {
  656. struct vm_area_struct *vma = find_vma(current->mm, hva);
  657. hva_t vm_start, vm_end;
  658. if (!vma || vma->vm_start >= reg_end)
  659. break;
  660. /*
  661. * Take the intersection of this VMA with the memory region
  662. */
  663. vm_start = max(hva, vma->vm_start);
  664. vm_end = min(reg_end, vma->vm_end);
  665. if (!(vma->vm_flags & VM_PFNMAP)) {
  666. gpa_t gpa = addr + (vm_start - memslot->userspace_addr);
  667. unmap_stage2_range(kvm, gpa, vm_end - vm_start);
  668. }
  669. hva = vm_end;
  670. } while (hva < reg_end);
  671. }
  672. /**
  673. * stage2_unmap_vm - Unmap Stage-2 RAM mappings
  674. * @kvm: The struct kvm pointer
  675. *
  676. * Go through the memregions and unmap any reguler RAM
  677. * backing memory already mapped to the VM.
  678. */
  679. void stage2_unmap_vm(struct kvm *kvm)
  680. {
  681. struct kvm_memslots *slots;
  682. struct kvm_memory_slot *memslot;
  683. int idx;
  684. idx = srcu_read_lock(&kvm->srcu);
  685. spin_lock(&kvm->mmu_lock);
  686. slots = kvm_memslots(kvm);
  687. kvm_for_each_memslot(memslot, slots)
  688. stage2_unmap_memslot(kvm, memslot);
  689. spin_unlock(&kvm->mmu_lock);
  690. srcu_read_unlock(&kvm->srcu, idx);
  691. }
  692. /**
  693. * kvm_free_stage2_pgd - free all stage-2 tables
  694. * @kvm: The KVM struct pointer for the VM.
  695. *
  696. * Walks the level-1 page table pointed to by kvm->arch.pgd and frees all
  697. * underlying level-2 and level-3 tables before freeing the actual level-1 table
  698. * and setting the struct pointer to NULL.
  699. *
  700. * Note we don't need locking here as this is only called when the VM is
  701. * destroyed, which can only be done once.
  702. */
  703. void kvm_free_stage2_pgd(struct kvm *kvm)
  704. {
  705. if (kvm->arch.pgd == NULL)
  706. return;
  707. unmap_stage2_range(kvm, 0, KVM_PHYS_SIZE);
  708. kvm_free_hwpgd(kvm_get_hwpgd(kvm));
  709. if (KVM_PREALLOC_LEVEL > 0)
  710. kfree(kvm->arch.pgd);
  711. kvm->arch.pgd = NULL;
  712. }
  713. static pud_t *stage2_get_pud(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
  714. phys_addr_t addr)
  715. {
  716. pgd_t *pgd;
  717. pud_t *pud;
  718. pgd = kvm->arch.pgd + kvm_pgd_index(addr);
  719. if (WARN_ON(pgd_none(*pgd))) {
  720. if (!cache)
  721. return NULL;
  722. pud = mmu_memory_cache_alloc(cache);
  723. pgd_populate(NULL, pgd, pud);
  724. get_page(virt_to_page(pgd));
  725. }
  726. return pud_offset(pgd, addr);
  727. }
  728. static pmd_t *stage2_get_pmd(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
  729. phys_addr_t addr)
  730. {
  731. pud_t *pud;
  732. pmd_t *pmd;
  733. pud = stage2_get_pud(kvm, cache, addr);
  734. if (pud_none(*pud)) {
  735. if (!cache)
  736. return NULL;
  737. pmd = mmu_memory_cache_alloc(cache);
  738. pud_populate(NULL, pud, pmd);
  739. get_page(virt_to_page(pud));
  740. }
  741. return pmd_offset(pud, addr);
  742. }
  743. static int stage2_set_pmd_huge(struct kvm *kvm, struct kvm_mmu_memory_cache
  744. *cache, phys_addr_t addr, const pmd_t *new_pmd)
  745. {
  746. pmd_t *pmd, old_pmd;
  747. pmd = stage2_get_pmd(kvm, cache, addr);
  748. VM_BUG_ON(!pmd);
  749. /*
  750. * Mapping in huge pages should only happen through a fault. If a
  751. * page is merged into a transparent huge page, the individual
  752. * subpages of that huge page should be unmapped through MMU
  753. * notifiers before we get here.
  754. *
  755. * Merging of CompoundPages is not supported; they should become
  756. * splitting first, unmapped, merged, and mapped back in on-demand.
  757. */
  758. VM_BUG_ON(pmd_present(*pmd) && pmd_pfn(*pmd) != pmd_pfn(*new_pmd));
  759. old_pmd = *pmd;
  760. kvm_set_pmd(pmd, *new_pmd);
  761. if (pmd_present(old_pmd))
  762. kvm_tlb_flush_vmid_ipa(kvm, addr);
  763. else
  764. get_page(virt_to_page(pmd));
  765. return 0;
  766. }
  767. static int stage2_set_pte(struct kvm *kvm, struct kvm_mmu_memory_cache *cache,
  768. phys_addr_t addr, const pte_t *new_pte,
  769. unsigned long flags)
  770. {
  771. pmd_t *pmd;
  772. pte_t *pte, old_pte;
  773. bool iomap = flags & KVM_S2PTE_FLAG_IS_IOMAP;
  774. bool logging_active = flags & KVM_S2_FLAG_LOGGING_ACTIVE;
  775. VM_BUG_ON(logging_active && !cache);
  776. /* Create stage-2 page table mapping - Levels 0 and 1 */
  777. pmd = stage2_get_pmd(kvm, cache, addr);
  778. if (!pmd) {
  779. /*
  780. * Ignore calls from kvm_set_spte_hva for unallocated
  781. * address ranges.
  782. */
  783. return 0;
  784. }
  785. /*
  786. * While dirty page logging - dissolve huge PMD, then continue on to
  787. * allocate page.
  788. */
  789. if (logging_active)
  790. stage2_dissolve_pmd(kvm, addr, pmd);
  791. /* Create stage-2 page mappings - Level 2 */
  792. if (pmd_none(*pmd)) {
  793. if (!cache)
  794. return 0; /* ignore calls from kvm_set_spte_hva */
  795. pte = mmu_memory_cache_alloc(cache);
  796. kvm_clean_pte(pte);
  797. pmd_populate_kernel(NULL, pmd, pte);
  798. get_page(virt_to_page(pmd));
  799. }
  800. pte = pte_offset_kernel(pmd, addr);
  801. if (iomap && pte_present(*pte))
  802. return -EFAULT;
  803. /* Create 2nd stage page table mapping - Level 3 */
  804. old_pte = *pte;
  805. kvm_set_pte(pte, *new_pte);
  806. if (pte_present(old_pte))
  807. kvm_tlb_flush_vmid_ipa(kvm, addr);
  808. else
  809. get_page(virt_to_page(pte));
  810. return 0;
  811. }
  812. /**
  813. * kvm_phys_addr_ioremap - map a device range to guest IPA
  814. *
  815. * @kvm: The KVM pointer
  816. * @guest_ipa: The IPA at which to insert the mapping
  817. * @pa: The physical address of the device
  818. * @size: The size of the mapping
  819. */
  820. int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
  821. phys_addr_t pa, unsigned long size, bool writable)
  822. {
  823. phys_addr_t addr, end;
  824. int ret = 0;
  825. unsigned long pfn;
  826. struct kvm_mmu_memory_cache cache = { 0, };
  827. end = (guest_ipa + size + PAGE_SIZE - 1) & PAGE_MASK;
  828. pfn = __phys_to_pfn(pa);
  829. for (addr = guest_ipa; addr < end; addr += PAGE_SIZE) {
  830. pte_t pte = pfn_pte(pfn, PAGE_S2_DEVICE);
  831. if (writable)
  832. kvm_set_s2pte_writable(&pte);
  833. ret = mmu_topup_memory_cache(&cache, KVM_MMU_CACHE_MIN_PAGES,
  834. KVM_NR_MEM_OBJS);
  835. if (ret)
  836. goto out;
  837. spin_lock(&kvm->mmu_lock);
  838. ret = stage2_set_pte(kvm, &cache, addr, &pte,
  839. KVM_S2PTE_FLAG_IS_IOMAP);
  840. spin_unlock(&kvm->mmu_lock);
  841. if (ret)
  842. goto out;
  843. pfn++;
  844. }
  845. out:
  846. mmu_free_memory_cache(&cache);
  847. return ret;
  848. }
  849. static bool transparent_hugepage_adjust(pfn_t *pfnp, phys_addr_t *ipap)
  850. {
  851. pfn_t pfn = *pfnp;
  852. gfn_t gfn = *ipap >> PAGE_SHIFT;
  853. if (PageTransCompound(pfn_to_page(pfn))) {
  854. unsigned long mask;
  855. /*
  856. * The address we faulted on is backed by a transparent huge
  857. * page. However, because we map the compound huge page and
  858. * not the individual tail page, we need to transfer the
  859. * refcount to the head page. We have to be careful that the
  860. * THP doesn't start to split while we are adjusting the
  861. * refcounts.
  862. *
  863. * We are sure this doesn't happen, because mmu_notifier_retry
  864. * was successful and we are holding the mmu_lock, so if this
  865. * THP is trying to split, it will be blocked in the mmu
  866. * notifier before touching any of the pages, specifically
  867. * before being able to call __split_huge_page_refcount().
  868. *
  869. * We can therefore safely transfer the refcount from PG_tail
  870. * to PG_head and switch the pfn from a tail page to the head
  871. * page accordingly.
  872. */
  873. mask = PTRS_PER_PMD - 1;
  874. VM_BUG_ON((gfn & mask) != (pfn & mask));
  875. if (pfn & mask) {
  876. *ipap &= PMD_MASK;
  877. kvm_release_pfn_clean(pfn);
  878. pfn &= ~mask;
  879. kvm_get_pfn(pfn);
  880. *pfnp = pfn;
  881. }
  882. return true;
  883. }
  884. return false;
  885. }
  886. static bool kvm_is_write_fault(struct kvm_vcpu *vcpu)
  887. {
  888. if (kvm_vcpu_trap_is_iabt(vcpu))
  889. return false;
  890. return kvm_vcpu_dabt_iswrite(vcpu);
  891. }
  892. /**
  893. * stage2_wp_ptes - write protect PMD range
  894. * @pmd: pointer to pmd entry
  895. * @addr: range start address
  896. * @end: range end address
  897. */
  898. static void stage2_wp_ptes(pmd_t *pmd, phys_addr_t addr, phys_addr_t end)
  899. {
  900. pte_t *pte;
  901. pte = pte_offset_kernel(pmd, addr);
  902. do {
  903. if (!pte_none(*pte)) {
  904. if (!kvm_s2pte_readonly(pte))
  905. kvm_set_s2pte_readonly(pte);
  906. }
  907. } while (pte++, addr += PAGE_SIZE, addr != end);
  908. }
  909. /**
  910. * stage2_wp_pmds - write protect PUD range
  911. * @pud: pointer to pud entry
  912. * @addr: range start address
  913. * @end: range end address
  914. */
  915. static void stage2_wp_pmds(pud_t *pud, phys_addr_t addr, phys_addr_t end)
  916. {
  917. pmd_t *pmd;
  918. phys_addr_t next;
  919. pmd = pmd_offset(pud, addr);
  920. do {
  921. next = kvm_pmd_addr_end(addr, end);
  922. if (!pmd_none(*pmd)) {
  923. if (kvm_pmd_huge(*pmd)) {
  924. if (!kvm_s2pmd_readonly(pmd))
  925. kvm_set_s2pmd_readonly(pmd);
  926. } else {
  927. stage2_wp_ptes(pmd, addr, next);
  928. }
  929. }
  930. } while (pmd++, addr = next, addr != end);
  931. }
  932. /**
  933. * stage2_wp_puds - write protect PGD range
  934. * @pgd: pointer to pgd entry
  935. * @addr: range start address
  936. * @end: range end address
  937. *
  938. * Process PUD entries, for a huge PUD we cause a panic.
  939. */
  940. static void stage2_wp_puds(pgd_t *pgd, phys_addr_t addr, phys_addr_t end)
  941. {
  942. pud_t *pud;
  943. phys_addr_t next;
  944. pud = pud_offset(pgd, addr);
  945. do {
  946. next = kvm_pud_addr_end(addr, end);
  947. if (!pud_none(*pud)) {
  948. /* TODO:PUD not supported, revisit later if supported */
  949. BUG_ON(kvm_pud_huge(*pud));
  950. stage2_wp_pmds(pud, addr, next);
  951. }
  952. } while (pud++, addr = next, addr != end);
  953. }
  954. /**
  955. * stage2_wp_range() - write protect stage2 memory region range
  956. * @kvm: The KVM pointer
  957. * @addr: Start address of range
  958. * @end: End address of range
  959. */
  960. static void stage2_wp_range(struct kvm *kvm, phys_addr_t addr, phys_addr_t end)
  961. {
  962. pgd_t *pgd;
  963. phys_addr_t next;
  964. pgd = kvm->arch.pgd + kvm_pgd_index(addr);
  965. do {
  966. /*
  967. * Release kvm_mmu_lock periodically if the memory region is
  968. * large. Otherwise, we may see kernel panics with
  969. * CONFIG_DETECT_HUNG_TASK, CONFIG_LOCKUP_DETECTOR,
  970. * CONFIG_LOCKDEP. Additionally, holding the lock too long
  971. * will also starve other vCPUs.
  972. */
  973. if (need_resched() || spin_needbreak(&kvm->mmu_lock))
  974. cond_resched_lock(&kvm->mmu_lock);
  975. next = kvm_pgd_addr_end(addr, end);
  976. if (pgd_present(*pgd))
  977. stage2_wp_puds(pgd, addr, next);
  978. } while (pgd++, addr = next, addr != end);
  979. }
  980. /**
  981. * kvm_mmu_wp_memory_region() - write protect stage 2 entries for memory slot
  982. * @kvm: The KVM pointer
  983. * @slot: The memory slot to write protect
  984. *
  985. * Called to start logging dirty pages after memory region
  986. * KVM_MEM_LOG_DIRTY_PAGES operation is called. After this function returns
  987. * all present PMD and PTEs are write protected in the memory region.
  988. * Afterwards read of dirty page log can be called.
  989. *
  990. * Acquires kvm_mmu_lock. Called with kvm->slots_lock mutex acquired,
  991. * serializing operations for VM memory regions.
  992. */
  993. void kvm_mmu_wp_memory_region(struct kvm *kvm, int slot)
  994. {
  995. struct kvm_memslots *slots = kvm_memslots(kvm);
  996. struct kvm_memory_slot *memslot = id_to_memslot(slots, 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. const struct kvm_userspace_memory_region *mem,
  1474. const struct kvm_memory_slot *old,
  1475. const struct kvm_memory_slot *new,
  1476. enum kvm_mr_change change)
  1477. {
  1478. /*
  1479. * At this point memslot has been committed and there is an
  1480. * allocated dirty_bitmap[], dirty pages will be be tracked while the
  1481. * memory slot is write protected.
  1482. */
  1483. if (change != KVM_MR_DELETE && mem->flags & KVM_MEM_LOG_DIRTY_PAGES)
  1484. kvm_mmu_wp_memory_region(kvm, mem->slot);
  1485. }
  1486. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  1487. struct kvm_memory_slot *memslot,
  1488. const struct kvm_userspace_memory_region *mem,
  1489. enum kvm_mr_change change)
  1490. {
  1491. hva_t hva = mem->userspace_addr;
  1492. hva_t reg_end = hva + mem->memory_size;
  1493. bool writable = !(mem->flags & KVM_MEM_READONLY);
  1494. int ret = 0;
  1495. if (change != KVM_MR_CREATE && change != KVM_MR_MOVE &&
  1496. change != KVM_MR_FLAGS_ONLY)
  1497. return 0;
  1498. /*
  1499. * Prevent userspace from creating a memory region outside of the IPA
  1500. * space addressable by the KVM guest IPA space.
  1501. */
  1502. if (memslot->base_gfn + memslot->npages >=
  1503. (KVM_PHYS_SIZE >> PAGE_SHIFT))
  1504. return -EFAULT;
  1505. /*
  1506. * A memory region could potentially cover multiple VMAs, and any holes
  1507. * between them, so iterate over all of them to find out if we can map
  1508. * any of them right now.
  1509. *
  1510. * +--------------------------------------------+
  1511. * +---------------+----------------+ +----------------+
  1512. * | : VMA 1 | VMA 2 | | VMA 3 : |
  1513. * +---------------+----------------+ +----------------+
  1514. * | memory region |
  1515. * +--------------------------------------------+
  1516. */
  1517. do {
  1518. struct vm_area_struct *vma = find_vma(current->mm, hva);
  1519. hva_t vm_start, vm_end;
  1520. if (!vma || vma->vm_start >= reg_end)
  1521. break;
  1522. /*
  1523. * Mapping a read-only VMA is only allowed if the
  1524. * memory region is configured as read-only.
  1525. */
  1526. if (writable && !(vma->vm_flags & VM_WRITE)) {
  1527. ret = -EPERM;
  1528. break;
  1529. }
  1530. /*
  1531. * Take the intersection of this VMA with the memory region
  1532. */
  1533. vm_start = max(hva, vma->vm_start);
  1534. vm_end = min(reg_end, vma->vm_end);
  1535. if (vma->vm_flags & VM_PFNMAP) {
  1536. gpa_t gpa = mem->guest_phys_addr +
  1537. (vm_start - mem->userspace_addr);
  1538. phys_addr_t pa;
  1539. pa = (phys_addr_t)vma->vm_pgoff << PAGE_SHIFT;
  1540. pa += vm_start - vma->vm_start;
  1541. /* IO region dirty page logging not allowed */
  1542. if (memslot->flags & KVM_MEM_LOG_DIRTY_PAGES)
  1543. return -EINVAL;
  1544. ret = kvm_phys_addr_ioremap(kvm, gpa, pa,
  1545. vm_end - vm_start,
  1546. writable);
  1547. if (ret)
  1548. break;
  1549. }
  1550. hva = vm_end;
  1551. } while (hva < reg_end);
  1552. if (change == KVM_MR_FLAGS_ONLY)
  1553. return ret;
  1554. spin_lock(&kvm->mmu_lock);
  1555. if (ret)
  1556. unmap_stage2_range(kvm, mem->guest_phys_addr, mem->memory_size);
  1557. else
  1558. stage2_flush_memslot(kvm, memslot);
  1559. spin_unlock(&kvm->mmu_lock);
  1560. return ret;
  1561. }
  1562. void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
  1563. struct kvm_memory_slot *dont)
  1564. {
  1565. }
  1566. int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
  1567. unsigned long npages)
  1568. {
  1569. /*
  1570. * Readonly memslots are not incoherent with the caches by definition,
  1571. * but in practice, they are used mostly to emulate ROMs or NOR flashes
  1572. * that the guest may consider devices and hence map as uncached.
  1573. * To prevent incoherency issues in these cases, tag all readonly
  1574. * regions as incoherent.
  1575. */
  1576. if (slot->flags & KVM_MEM_READONLY)
  1577. slot->flags |= KVM_MEMSLOT_INCOHERENT;
  1578. return 0;
  1579. }
  1580. void kvm_arch_memslots_updated(struct kvm *kvm, struct kvm_memslots *slots)
  1581. {
  1582. }
  1583. void kvm_arch_flush_shadow_all(struct kvm *kvm)
  1584. {
  1585. }
  1586. void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
  1587. struct kvm_memory_slot *slot)
  1588. {
  1589. gpa_t gpa = slot->base_gfn << PAGE_SHIFT;
  1590. phys_addr_t size = slot->npages << PAGE_SHIFT;
  1591. spin_lock(&kvm->mmu_lock);
  1592. unmap_stage2_range(kvm, gpa, size);
  1593. spin_unlock(&kvm->mmu_lock);
  1594. }
  1595. /*
  1596. * See note at ARMv7 ARM B1.14.4 (TL;DR: S/W ops are not easily virtualized).
  1597. *
  1598. * Main problems:
  1599. * - S/W ops are local to a CPU (not broadcast)
  1600. * - We have line migration behind our back (speculation)
  1601. * - System caches don't support S/W at all (damn!)
  1602. *
  1603. * In the face of the above, the best we can do is to try and convert
  1604. * S/W ops to VA ops. Because the guest is not allowed to infer the
  1605. * S/W to PA mapping, it can only use S/W to nuke the whole cache,
  1606. * which is a rather good thing for us.
  1607. *
  1608. * Also, it is only used when turning caches on/off ("The expected
  1609. * usage of the cache maintenance instructions that operate by set/way
  1610. * is associated with the cache maintenance instructions associated
  1611. * with the powerdown and powerup of caches, if this is required by
  1612. * the implementation.").
  1613. *
  1614. * We use the following policy:
  1615. *
  1616. * - If we trap a S/W operation, we enable VM trapping to detect
  1617. * caches being turned on/off, and do a full clean.
  1618. *
  1619. * - We flush the caches on both caches being turned on and off.
  1620. *
  1621. * - Once the caches are enabled, we stop trapping VM ops.
  1622. */
  1623. void kvm_set_way_flush(struct kvm_vcpu *vcpu)
  1624. {
  1625. unsigned long hcr = vcpu_get_hcr(vcpu);
  1626. /*
  1627. * If this is the first time we do a S/W operation
  1628. * (i.e. HCR_TVM not set) flush the whole memory, and set the
  1629. * VM trapping.
  1630. *
  1631. * Otherwise, rely on the VM trapping to wait for the MMU +
  1632. * Caches to be turned off. At that point, we'll be able to
  1633. * clean the caches again.
  1634. */
  1635. if (!(hcr & HCR_TVM)) {
  1636. trace_kvm_set_way_flush(*vcpu_pc(vcpu),
  1637. vcpu_has_cache_enabled(vcpu));
  1638. stage2_flush_vm(vcpu->kvm);
  1639. vcpu_set_hcr(vcpu, hcr | HCR_TVM);
  1640. }
  1641. }
  1642. void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled)
  1643. {
  1644. bool now_enabled = vcpu_has_cache_enabled(vcpu);
  1645. /*
  1646. * If switching the MMU+caches on, need to invalidate the caches.
  1647. * If switching it off, need to clean the caches.
  1648. * Clean + invalidate does the trick always.
  1649. */
  1650. if (now_enabled != was_enabled)
  1651. stage2_flush_vm(vcpu->kvm);
  1652. /* Caches are now on, stop trapping VM ops (until a S/W op) */
  1653. if (now_enabled)
  1654. vcpu_set_hcr(vcpu, vcpu_get_hcr(vcpu) & ~HCR_TVM);
  1655. trace_kvm_toggle_cache(*vcpu_pc(vcpu), was_enabled, now_enabled);
  1656. }