mmu.c 51 KB

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