mmu.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2012 - Virtual Open Systems and Columbia University
  4. * Author: Christoffer Dall <[email protected]>
  5. */
  6. #include <linux/mman.h>
  7. #include <linux/kvm_host.h>
  8. #include <linux/io.h>
  9. #include <linux/hugetlb.h>
  10. #include <linux/sched/signal.h>
  11. #include <trace/events/kvm.h>
  12. #include <asm/pgalloc.h>
  13. #include <asm/cacheflush.h>
  14. #include <asm/kvm_arm.h>
  15. #include <asm/kvm_mmu.h>
  16. #include <asm/kvm_pgtable.h>
  17. #include <asm/kvm_ras.h>
  18. #include <asm/kvm_asm.h>
  19. #include <asm/kvm_emulate.h>
  20. #include <asm/virt.h>
  21. #include "trace.h"
  22. static struct kvm_pgtable *hyp_pgtable;
  23. static DEFINE_MUTEX(kvm_hyp_pgd_mutex);
  24. static unsigned long hyp_idmap_start;
  25. static unsigned long hyp_idmap_end;
  26. static phys_addr_t hyp_idmap_vector;
  27. static unsigned long io_map_base;
  28. static phys_addr_t stage2_range_addr_end(phys_addr_t addr, phys_addr_t end)
  29. {
  30. phys_addr_t size = kvm_granule_size(KVM_PGTABLE_MIN_BLOCK_LEVEL);
  31. phys_addr_t boundary = ALIGN_DOWN(addr + size, size);
  32. return (boundary - 1 < end - 1) ? boundary : end;
  33. }
  34. /*
  35. * Release kvm_mmu_lock periodically if the memory region is large. Otherwise,
  36. * we may see kernel panics with CONFIG_DETECT_HUNG_TASK,
  37. * CONFIG_LOCKUP_DETECTOR, CONFIG_LOCKDEP. Additionally, holding the lock too
  38. * long will also starve other vCPUs. We have to also make sure that the page
  39. * tables are not freed while we released the lock.
  40. */
  41. static int stage2_apply_range(struct kvm *kvm, phys_addr_t addr,
  42. phys_addr_t end,
  43. int (*fn)(struct kvm_pgtable *, u64, u64),
  44. bool resched)
  45. {
  46. int ret;
  47. u64 next;
  48. do {
  49. struct kvm_pgtable *pgt = kvm->arch.mmu.pgt;
  50. if (!pgt)
  51. return -EINVAL;
  52. next = stage2_range_addr_end(addr, end);
  53. ret = fn(pgt, addr, next - addr);
  54. if (ret)
  55. break;
  56. if (resched && next != end)
  57. cond_resched_rwlock_write(&kvm->mmu_lock);
  58. } while (addr = next, addr != end);
  59. return ret;
  60. }
  61. #define stage2_apply_range_resched(kvm, addr, end, fn) \
  62. stage2_apply_range(kvm, addr, end, fn, true)
  63. static bool memslot_is_logging(struct kvm_memory_slot *memslot)
  64. {
  65. return memslot->dirty_bitmap && !(memslot->flags & KVM_MEM_READONLY);
  66. }
  67. /**
  68. * kvm_flush_remote_tlbs() - flush all VM TLB entries for v7/8
  69. * @kvm: pointer to kvm structure.
  70. *
  71. * Interface to HYP function to flush all VM TLB entries
  72. */
  73. void kvm_flush_remote_tlbs(struct kvm *kvm)
  74. {
  75. ++kvm->stat.generic.remote_tlb_flush_requests;
  76. kvm_call_hyp(__kvm_tlb_flush_vmid, &kvm->arch.mmu);
  77. }
  78. static bool kvm_is_device_pfn(unsigned long pfn)
  79. {
  80. return !pfn_is_map_memory(pfn);
  81. }
  82. static void *stage2_memcache_zalloc_page(void *arg)
  83. {
  84. struct kvm_mmu_memory_cache *mc = arg;
  85. void *virt;
  86. /* Allocated with __GFP_ZERO, so no need to zero */
  87. virt = kvm_mmu_memory_cache_alloc(mc);
  88. if (virt)
  89. kvm_account_pgtable_pages(virt, 1);
  90. return virt;
  91. }
  92. static void *kvm_host_zalloc_pages_exact(size_t size)
  93. {
  94. return alloc_pages_exact(size, GFP_KERNEL_ACCOUNT | __GFP_ZERO);
  95. }
  96. static void *kvm_s2_zalloc_pages_exact(size_t size)
  97. {
  98. void *virt = kvm_host_zalloc_pages_exact(size);
  99. if (virt)
  100. kvm_account_pgtable_pages(virt, (size >> PAGE_SHIFT));
  101. return virt;
  102. }
  103. static void kvm_s2_free_pages_exact(void *virt, size_t size)
  104. {
  105. kvm_account_pgtable_pages(virt, -(size >> PAGE_SHIFT));
  106. free_pages_exact(virt, size);
  107. }
  108. static void kvm_host_get_page(void *addr)
  109. {
  110. get_page(virt_to_page(addr));
  111. }
  112. static void kvm_host_put_page(void *addr)
  113. {
  114. put_page(virt_to_page(addr));
  115. }
  116. static void kvm_s2_put_page(void *addr)
  117. {
  118. struct page *p = virt_to_page(addr);
  119. /* Dropping last refcount, the page will be freed */
  120. if (page_count(p) == 1)
  121. kvm_account_pgtable_pages(addr, -1);
  122. put_page(p);
  123. }
  124. static int kvm_host_page_count(void *addr)
  125. {
  126. return page_count(virt_to_page(addr));
  127. }
  128. static phys_addr_t kvm_host_pa(void *addr)
  129. {
  130. return __pa(addr);
  131. }
  132. static void *kvm_host_va(phys_addr_t phys)
  133. {
  134. return __va(phys);
  135. }
  136. static void clean_dcache_guest_page(void *va, size_t size)
  137. {
  138. __clean_dcache_guest_page(va, size);
  139. }
  140. static void invalidate_icache_guest_page(void *va, size_t size)
  141. {
  142. __invalidate_icache_guest_page(va, size);
  143. }
  144. /*
  145. * Unmapping vs dcache management:
  146. *
  147. * If a guest maps certain memory pages as uncached, all writes will
  148. * bypass the data cache and go directly to RAM. However, the CPUs
  149. * can still speculate reads (not writes) and fill cache lines with
  150. * data.
  151. *
  152. * Those cache lines will be *clean* cache lines though, so a
  153. * clean+invalidate operation is equivalent to an invalidate
  154. * operation, because no cache lines are marked dirty.
  155. *
  156. * Those clean cache lines could be filled prior to an uncached write
  157. * by the guest, and the cache coherent IO subsystem would therefore
  158. * end up writing old data to disk.
  159. *
  160. * This is why right after unmapping a page/section and invalidating
  161. * the corresponding TLBs, we flush to make sure the IO subsystem will
  162. * never hit in the cache.
  163. *
  164. * This is all avoided on systems that have ARM64_HAS_STAGE2_FWB, as
  165. * we then fully enforce cacheability of RAM, no matter what the guest
  166. * does.
  167. */
  168. /**
  169. * unmap_stage2_range -- Clear stage2 page table entries to unmap a range
  170. * @mmu: The KVM stage-2 MMU pointer
  171. * @start: The intermediate physical base address of the range to unmap
  172. * @size: The size of the area to unmap
  173. * @may_block: Whether or not we are permitted to block
  174. *
  175. * Clear a range of stage-2 mappings, lowering the various ref-counts. Must
  176. * be called while holding mmu_lock (unless for freeing the stage2 pgd before
  177. * destroying the VM), otherwise another faulting VCPU may come in and mess
  178. * with things behind our backs.
  179. */
  180. static void __unmap_stage2_range(struct kvm_s2_mmu *mmu, phys_addr_t start, u64 size,
  181. bool may_block)
  182. {
  183. struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
  184. phys_addr_t end = start + size;
  185. lockdep_assert_held_write(&kvm->mmu_lock);
  186. WARN_ON(size & ~PAGE_MASK);
  187. WARN_ON(stage2_apply_range(kvm, start, end, kvm_pgtable_stage2_unmap,
  188. may_block));
  189. }
  190. static void unmap_stage2_range(struct kvm_s2_mmu *mmu, phys_addr_t start, u64 size)
  191. {
  192. __unmap_stage2_range(mmu, start, size, true);
  193. }
  194. static void pkvm_stage2_flush(struct kvm *kvm)
  195. {
  196. struct kvm_pinned_page *ppage;
  197. struct rb_node *node;
  198. /*
  199. * Contrary to stage2_apply_range(), we don't need to check
  200. * whether the VM is being torn down, as this is always called
  201. * from a vcpu thread, and the list is only ever freed on VM
  202. * destroy (which only occurs when all vcpu are gone).
  203. */
  204. for (node = rb_first(&kvm->arch.pkvm.pinned_pages); node; node = rb_next(node)) {
  205. ppage = rb_entry(node, struct kvm_pinned_page, node);
  206. __clean_dcache_guest_page(page_address(ppage->page), PAGE_SIZE);
  207. cond_resched_rwlock_write(&kvm->mmu_lock);
  208. }
  209. }
  210. static void stage2_flush_memslot(struct kvm *kvm,
  211. struct kvm_memory_slot *memslot)
  212. {
  213. phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
  214. phys_addr_t end = addr + PAGE_SIZE * memslot->npages;
  215. stage2_apply_range_resched(kvm, addr, end, kvm_pgtable_stage2_flush);
  216. }
  217. /**
  218. * stage2_flush_vm - Invalidate cache for pages mapped in stage 2
  219. * @kvm: The struct kvm pointer
  220. *
  221. * Go through the stage 2 page tables and invalidate any cache lines
  222. * backing memory already mapped to the VM.
  223. */
  224. static void stage2_flush_vm(struct kvm *kvm)
  225. {
  226. struct kvm_memslots *slots;
  227. struct kvm_memory_slot *memslot;
  228. int idx, bkt;
  229. idx = srcu_read_lock(&kvm->srcu);
  230. write_lock(&kvm->mmu_lock);
  231. if (!is_protected_kvm_enabled()) {
  232. slots = kvm_memslots(kvm);
  233. kvm_for_each_memslot(memslot, bkt, slots)
  234. stage2_flush_memslot(kvm, memslot);
  235. } else if (!kvm_vm_is_protected(kvm)) {
  236. pkvm_stage2_flush(kvm);
  237. }
  238. write_unlock(&kvm->mmu_lock);
  239. srcu_read_unlock(&kvm->srcu, idx);
  240. }
  241. /**
  242. * free_hyp_pgds - free Hyp-mode page tables
  243. */
  244. void free_hyp_pgds(void)
  245. {
  246. mutex_lock(&kvm_hyp_pgd_mutex);
  247. if (hyp_pgtable) {
  248. kvm_pgtable_hyp_destroy(hyp_pgtable);
  249. kfree(hyp_pgtable);
  250. hyp_pgtable = NULL;
  251. }
  252. mutex_unlock(&kvm_hyp_pgd_mutex);
  253. }
  254. static bool kvm_host_owns_hyp_mappings(void)
  255. {
  256. if (is_kernel_in_hyp_mode())
  257. return false;
  258. if (static_branch_likely(&kvm_protected_mode_initialized))
  259. return false;
  260. /*
  261. * This can happen at boot time when __create_hyp_mappings() is called
  262. * after the hyp protection has been enabled, but the static key has
  263. * not been flipped yet.
  264. */
  265. if (!hyp_pgtable && is_protected_kvm_enabled())
  266. return false;
  267. WARN_ON(!hyp_pgtable);
  268. return true;
  269. }
  270. int __create_hyp_mappings(unsigned long start, unsigned long size,
  271. unsigned long phys, enum kvm_pgtable_prot prot)
  272. {
  273. int err;
  274. if (WARN_ON(!kvm_host_owns_hyp_mappings()))
  275. return -EINVAL;
  276. mutex_lock(&kvm_hyp_pgd_mutex);
  277. err = kvm_pgtable_hyp_map(hyp_pgtable, start, size, phys, prot);
  278. mutex_unlock(&kvm_hyp_pgd_mutex);
  279. return err;
  280. }
  281. static phys_addr_t kvm_kaddr_to_phys(void *kaddr)
  282. {
  283. if (!is_vmalloc_addr(kaddr)) {
  284. BUG_ON(!virt_addr_valid(kaddr));
  285. return __pa(kaddr);
  286. } else {
  287. return page_to_phys(vmalloc_to_page(kaddr)) +
  288. offset_in_page(kaddr);
  289. }
  290. }
  291. struct hyp_shared_pfn {
  292. u64 pfn;
  293. int count;
  294. struct rb_node node;
  295. };
  296. static DEFINE_MUTEX(hyp_shared_pfns_lock);
  297. static struct rb_root hyp_shared_pfns = RB_ROOT;
  298. static struct hyp_shared_pfn *find_shared_pfn(u64 pfn, struct rb_node ***node,
  299. struct rb_node **parent)
  300. {
  301. struct hyp_shared_pfn *this;
  302. *node = &hyp_shared_pfns.rb_node;
  303. *parent = NULL;
  304. while (**node) {
  305. this = container_of(**node, struct hyp_shared_pfn, node);
  306. *parent = **node;
  307. if (this->pfn < pfn)
  308. *node = &((**node)->rb_left);
  309. else if (this->pfn > pfn)
  310. *node = &((**node)->rb_right);
  311. else
  312. return this;
  313. }
  314. return NULL;
  315. }
  316. static int share_pfn_hyp(u64 pfn)
  317. {
  318. struct rb_node **node, *parent;
  319. struct hyp_shared_pfn *this;
  320. int ret = 0;
  321. mutex_lock(&hyp_shared_pfns_lock);
  322. this = find_shared_pfn(pfn, &node, &parent);
  323. if (this) {
  324. this->count++;
  325. goto unlock;
  326. }
  327. this = kzalloc(sizeof(*this), GFP_KERNEL);
  328. if (!this) {
  329. ret = -ENOMEM;
  330. goto unlock;
  331. }
  332. this->pfn = pfn;
  333. this->count = 1;
  334. rb_link_node(&this->node, parent, node);
  335. rb_insert_color(&this->node, &hyp_shared_pfns);
  336. ret = kvm_call_hyp_nvhe(__pkvm_host_share_hyp, pfn, 1);
  337. unlock:
  338. mutex_unlock(&hyp_shared_pfns_lock);
  339. return ret;
  340. }
  341. static int unshare_pfn_hyp(u64 pfn)
  342. {
  343. struct rb_node **node, *parent;
  344. struct hyp_shared_pfn *this;
  345. int ret = 0;
  346. mutex_lock(&hyp_shared_pfns_lock);
  347. this = find_shared_pfn(pfn, &node, &parent);
  348. if (WARN_ON(!this)) {
  349. ret = -ENOENT;
  350. goto unlock;
  351. }
  352. this->count--;
  353. if (this->count)
  354. goto unlock;
  355. rb_erase(&this->node, &hyp_shared_pfns);
  356. kfree(this);
  357. ret = kvm_call_hyp_nvhe(__pkvm_host_unshare_hyp, pfn, 1);
  358. unlock:
  359. mutex_unlock(&hyp_shared_pfns_lock);
  360. return ret;
  361. }
  362. int kvm_share_hyp(void *from, void *to)
  363. {
  364. phys_addr_t start, end, cur;
  365. u64 pfn;
  366. int ret;
  367. if (is_kernel_in_hyp_mode())
  368. return 0;
  369. /*
  370. * The share hcall maps things in the 'fixed-offset' region of the hyp
  371. * VA space, so we can only share physically contiguous data-structures
  372. * for now.
  373. */
  374. if (is_vmalloc_or_module_addr(from) || is_vmalloc_or_module_addr(to))
  375. return -EINVAL;
  376. if (kvm_host_owns_hyp_mappings())
  377. return create_hyp_mappings(from, to, PAGE_HYP);
  378. start = ALIGN_DOWN(__pa(from), PAGE_SIZE);
  379. end = PAGE_ALIGN(__pa(to));
  380. for (cur = start; cur < end; cur += PAGE_SIZE) {
  381. pfn = __phys_to_pfn(cur);
  382. ret = share_pfn_hyp(pfn);
  383. if (ret)
  384. return ret;
  385. }
  386. return 0;
  387. }
  388. void kvm_unshare_hyp(void *from, void *to)
  389. {
  390. phys_addr_t start, end, cur;
  391. u64 pfn;
  392. if (is_kernel_in_hyp_mode() || kvm_host_owns_hyp_mappings() || !from)
  393. return;
  394. start = ALIGN_DOWN(__pa(from), PAGE_SIZE);
  395. end = PAGE_ALIGN(__pa(to));
  396. for (cur = start; cur < end; cur += PAGE_SIZE) {
  397. pfn = __phys_to_pfn(cur);
  398. WARN_ON(unshare_pfn_hyp(pfn));
  399. }
  400. }
  401. /**
  402. * create_hyp_mappings - duplicate a kernel virtual address range in Hyp mode
  403. * @from: The virtual kernel start address of the range
  404. * @to: The virtual kernel end address of the range (exclusive)
  405. * @prot: The protection to be applied to this range
  406. *
  407. * The same virtual address as the kernel virtual address is also used
  408. * in Hyp-mode mapping (modulo HYP_PAGE_OFFSET) to the same underlying
  409. * physical pages.
  410. */
  411. int create_hyp_mappings(void *from, void *to, enum kvm_pgtable_prot prot)
  412. {
  413. phys_addr_t phys_addr;
  414. unsigned long virt_addr;
  415. unsigned long start = kern_hyp_va((unsigned long)from);
  416. unsigned long end = kern_hyp_va((unsigned long)to);
  417. if (is_kernel_in_hyp_mode())
  418. return 0;
  419. if (!kvm_host_owns_hyp_mappings())
  420. return -EPERM;
  421. start = start & PAGE_MASK;
  422. end = PAGE_ALIGN(end);
  423. for (virt_addr = start; virt_addr < end; virt_addr += PAGE_SIZE) {
  424. int err;
  425. phys_addr = kvm_kaddr_to_phys(from + virt_addr - start);
  426. err = __create_hyp_mappings(virt_addr, PAGE_SIZE, phys_addr,
  427. prot);
  428. if (err)
  429. return err;
  430. }
  431. return 0;
  432. }
  433. /**
  434. * hyp_alloc_private_va_range - Allocates a private VA range.
  435. * @size: The size of the VA range to reserve.
  436. * @haddr: The hypervisor virtual start address of the allocation.
  437. *
  438. * The private virtual address (VA) range is allocated below io_map_base
  439. * and aligned based on the order of @size.
  440. *
  441. * Return: 0 on success or negative error code on failure.
  442. */
  443. int hyp_alloc_private_va_range(size_t size, unsigned long *haddr)
  444. {
  445. unsigned long base;
  446. int ret = 0;
  447. mutex_lock(&kvm_hyp_pgd_mutex);
  448. /*
  449. * This assumes that we have enough space below the idmap
  450. * page to allocate our VAs. If not, the check below will
  451. * kick. A potential alternative would be to detect that
  452. * overflow and switch to an allocation above the idmap.
  453. *
  454. * The allocated size is always a multiple of PAGE_SIZE.
  455. */
  456. base = io_map_base - PAGE_ALIGN(size);
  457. /* Align the allocation based on the order of its size */
  458. base = ALIGN_DOWN(base, PAGE_SIZE << get_order(size));
  459. /*
  460. * Verify that BIT(VA_BITS - 1) hasn't been flipped by
  461. * allocating the new area, as it would indicate we've
  462. * overflowed the idmap/IO address range.
  463. */
  464. if ((base ^ io_map_base) & BIT(VA_BITS - 1))
  465. ret = -ENOMEM;
  466. else
  467. *haddr = io_map_base = base;
  468. mutex_unlock(&kvm_hyp_pgd_mutex);
  469. return ret;
  470. }
  471. static int __create_hyp_private_mapping(phys_addr_t phys_addr, size_t size,
  472. unsigned long *haddr,
  473. enum kvm_pgtable_prot prot)
  474. {
  475. unsigned long addr;
  476. int ret = 0;
  477. if (!kvm_host_owns_hyp_mappings()) {
  478. addr = kvm_call_hyp_nvhe(__pkvm_create_private_mapping,
  479. phys_addr, size, prot);
  480. if (IS_ERR_VALUE(addr))
  481. return addr;
  482. *haddr = addr;
  483. return 0;
  484. }
  485. size = PAGE_ALIGN(size + offset_in_page(phys_addr));
  486. ret = hyp_alloc_private_va_range(size, &addr);
  487. if (ret)
  488. return ret;
  489. ret = __create_hyp_mappings(addr, size, phys_addr, prot);
  490. if (ret)
  491. return ret;
  492. *haddr = addr + offset_in_page(phys_addr);
  493. return ret;
  494. }
  495. /**
  496. * create_hyp_io_mappings - Map IO into both kernel and HYP
  497. * @phys_addr: The physical start address which gets mapped
  498. * @size: Size of the region being mapped
  499. * @kaddr: Kernel VA for this mapping
  500. * @haddr: HYP VA for this mapping
  501. */
  502. int create_hyp_io_mappings(phys_addr_t phys_addr, size_t size,
  503. void __iomem **kaddr,
  504. void __iomem **haddr)
  505. {
  506. unsigned long addr;
  507. int ret;
  508. if (is_protected_kvm_enabled())
  509. return -EPERM;
  510. *kaddr = ioremap(phys_addr, size);
  511. if (!*kaddr)
  512. return -ENOMEM;
  513. if (is_kernel_in_hyp_mode()) {
  514. *haddr = *kaddr;
  515. return 0;
  516. }
  517. ret = __create_hyp_private_mapping(phys_addr, size,
  518. &addr, PAGE_HYP_DEVICE);
  519. if (ret) {
  520. iounmap(*kaddr);
  521. *kaddr = NULL;
  522. *haddr = NULL;
  523. return ret;
  524. }
  525. *haddr = (void __iomem *)addr;
  526. return 0;
  527. }
  528. /**
  529. * create_hyp_exec_mappings - Map an executable range into HYP
  530. * @phys_addr: The physical start address which gets mapped
  531. * @size: Size of the region being mapped
  532. * @haddr: HYP VA for this mapping
  533. */
  534. int create_hyp_exec_mappings(phys_addr_t phys_addr, size_t size,
  535. void **haddr)
  536. {
  537. unsigned long addr;
  538. int ret;
  539. BUG_ON(is_kernel_in_hyp_mode());
  540. ret = __create_hyp_private_mapping(phys_addr, size,
  541. &addr, PAGE_HYP_EXEC);
  542. if (ret) {
  543. *haddr = NULL;
  544. return ret;
  545. }
  546. *haddr = (void *)addr;
  547. return 0;
  548. }
  549. static struct kvm_pgtable_mm_ops kvm_user_mm_ops = {
  550. /* We shouldn't need any other callback to walk the PT */
  551. .phys_to_virt = kvm_host_va,
  552. };
  553. static int get_user_mapping_size(struct kvm *kvm, u64 addr)
  554. {
  555. struct kvm_pgtable pgt = {
  556. .pgd = (kvm_pte_t *)kvm->mm->pgd,
  557. .ia_bits = VA_BITS,
  558. .start_level = (KVM_PGTABLE_MAX_LEVELS -
  559. CONFIG_PGTABLE_LEVELS),
  560. .mm_ops = &kvm_user_mm_ops,
  561. };
  562. unsigned long flags;
  563. kvm_pte_t pte = 0; /* Keep GCC quiet... */
  564. u32 level = ~0;
  565. int ret;
  566. /*
  567. * Disable IRQs so that we hazard against a concurrent
  568. * teardown of the userspace page tables (which relies on
  569. * IPI-ing threads).
  570. */
  571. local_irq_save(flags);
  572. ret = kvm_pgtable_get_leaf(&pgt, addr, &pte, &level);
  573. local_irq_restore(flags);
  574. if (ret)
  575. return ret;
  576. /*
  577. * Not seeing an error, but not updating level? Something went
  578. * deeply wrong...
  579. */
  580. if (WARN_ON(level >= KVM_PGTABLE_MAX_LEVELS))
  581. return -EFAULT;
  582. /* Oops, the userspace PTs are gone... Replay the fault */
  583. if (!kvm_pte_valid(pte))
  584. return -EAGAIN;
  585. return BIT(ARM64_HW_PGTABLE_LEVEL_SHIFT(level));
  586. }
  587. static bool stage2_force_pte_cb(u64 addr, u64 end, enum kvm_pgtable_prot prot)
  588. {
  589. return false;
  590. }
  591. static bool stage2_pte_is_counted(kvm_pte_t pte, u32 level)
  592. {
  593. return !!pte;
  594. }
  595. static struct kvm_pgtable_mm_ops kvm_s2_mm_ops = {
  596. .zalloc_page = stage2_memcache_zalloc_page,
  597. .zalloc_pages_exact = kvm_s2_zalloc_pages_exact,
  598. .free_pages_exact = kvm_s2_free_pages_exact,
  599. .get_page = kvm_host_get_page,
  600. .put_page = kvm_s2_put_page,
  601. .page_count = kvm_host_page_count,
  602. .phys_to_virt = kvm_host_va,
  603. .virt_to_phys = kvm_host_pa,
  604. .dcache_clean_inval_poc = clean_dcache_guest_page,
  605. .icache_inval_pou = invalidate_icache_guest_page,
  606. };
  607. static struct kvm_pgtable_pte_ops kvm_s2_pte_ops = {
  608. .force_pte_cb = stage2_force_pte_cb,
  609. .pte_is_counted_cb = stage2_pte_is_counted
  610. };
  611. /**
  612. * kvm_init_stage2_mmu - Initialise a S2 MMU structure
  613. * @kvm: The pointer to the KVM structure
  614. * @mmu: The pointer to the s2 MMU structure
  615. * @type: The machine type of the virtual machine
  616. *
  617. * Allocates only the stage-2 HW PGD level table(s).
  618. * Note we don't need locking here as this is only called when the VM is
  619. * created, which can only be done once.
  620. */
  621. int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long type)
  622. {
  623. u32 kvm_ipa_limit = get_kvm_ipa_limit();
  624. int cpu, err;
  625. struct kvm_pgtable *pgt;
  626. u64 mmfr0, mmfr1;
  627. u32 phys_shift;
  628. phys_shift = KVM_VM_TYPE_ARM_IPA_SIZE(type);
  629. if (is_protected_kvm_enabled()) {
  630. phys_shift = kvm_ipa_limit;
  631. } else if (phys_shift) {
  632. if (phys_shift > kvm_ipa_limit ||
  633. phys_shift < ARM64_MIN_PARANGE_BITS)
  634. return -EINVAL;
  635. } else {
  636. phys_shift = KVM_PHYS_SHIFT;
  637. if (phys_shift > kvm_ipa_limit) {
  638. pr_warn_once("%s using unsupported default IPA limit, upgrade your VMM\n",
  639. current->comm);
  640. return -EINVAL;
  641. }
  642. }
  643. mmfr0 = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
  644. mmfr1 = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1);
  645. kvm->arch.vtcr = kvm_get_vtcr(mmfr0, mmfr1, phys_shift);
  646. kvm->arch.pkvm.pinned_pages = RB_ROOT;
  647. mmu->arch = &kvm->arch;
  648. if (is_protected_kvm_enabled())
  649. return 0;
  650. if (mmu->pgt != NULL) {
  651. kvm_err("kvm_arch already initialized?\n");
  652. return -EINVAL;
  653. }
  654. pgt = kzalloc(sizeof(*pgt), GFP_KERNEL_ACCOUNT);
  655. if (!pgt)
  656. return -ENOMEM;
  657. mmu->arch = &kvm->arch;
  658. err = kvm_pgtable_stage2_init(pgt, mmu, &kvm_s2_mm_ops,
  659. &kvm_s2_pte_ops);
  660. if (err)
  661. goto out_free_pgtable;
  662. mmu->last_vcpu_ran = alloc_percpu(typeof(*mmu->last_vcpu_ran));
  663. if (!mmu->last_vcpu_ran) {
  664. err = -ENOMEM;
  665. goto out_destroy_pgtable;
  666. }
  667. for_each_possible_cpu(cpu)
  668. *per_cpu_ptr(mmu->last_vcpu_ran, cpu) = -1;
  669. mmu->pgt = pgt;
  670. mmu->pgd_phys = __pa(pgt->pgd);
  671. return 0;
  672. out_destroy_pgtable:
  673. kvm_pgtable_stage2_destroy(pgt);
  674. out_free_pgtable:
  675. kfree(pgt);
  676. return err;
  677. }
  678. static void stage2_unmap_memslot(struct kvm *kvm,
  679. struct kvm_memory_slot *memslot)
  680. {
  681. hva_t hva = memslot->userspace_addr;
  682. phys_addr_t addr = memslot->base_gfn << PAGE_SHIFT;
  683. phys_addr_t size = PAGE_SIZE * memslot->npages;
  684. hva_t reg_end = hva + size;
  685. /*
  686. * A memory region could potentially cover multiple VMAs, and any holes
  687. * between them, so iterate over all of them to find out if we should
  688. * unmap any of them.
  689. *
  690. * +--------------------------------------------+
  691. * +---------------+----------------+ +----------------+
  692. * | : VMA 1 | VMA 2 | | VMA 3 : |
  693. * +---------------+----------------+ +----------------+
  694. * | memory region |
  695. * +--------------------------------------------+
  696. */
  697. do {
  698. struct vm_area_struct *vma;
  699. hva_t vm_start, vm_end;
  700. vma = find_vma_intersection(current->mm, hva, reg_end);
  701. if (!vma)
  702. break;
  703. /*
  704. * Take the intersection of this VMA with the memory region
  705. */
  706. vm_start = max(hva, vma->vm_start);
  707. vm_end = min(reg_end, vma->vm_end);
  708. if (!(vma->vm_flags & VM_PFNMAP)) {
  709. gpa_t gpa = addr + (vm_start - memslot->userspace_addr);
  710. unmap_stage2_range(&kvm->arch.mmu, gpa, vm_end - vm_start);
  711. }
  712. hva = vm_end;
  713. } while (hva < reg_end);
  714. }
  715. /**
  716. * stage2_unmap_vm - Unmap Stage-2 RAM mappings
  717. * @kvm: The struct kvm pointer
  718. *
  719. * Go through the memregions and unmap any regular RAM
  720. * backing memory already mapped to the VM.
  721. */
  722. void stage2_unmap_vm(struct kvm *kvm)
  723. {
  724. struct kvm_memslots *slots;
  725. struct kvm_memory_slot *memslot;
  726. int idx, bkt;
  727. idx = srcu_read_lock(&kvm->srcu);
  728. mmap_read_lock(current->mm);
  729. write_lock(&kvm->mmu_lock);
  730. slots = kvm_memslots(kvm);
  731. kvm_for_each_memslot(memslot, bkt, slots)
  732. stage2_unmap_memslot(kvm, memslot);
  733. write_unlock(&kvm->mmu_lock);
  734. mmap_read_unlock(current->mm);
  735. srcu_read_unlock(&kvm->srcu, idx);
  736. }
  737. void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu)
  738. {
  739. struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
  740. struct kvm_pgtable *pgt = NULL;
  741. if (is_protected_kvm_enabled())
  742. return;
  743. write_lock(&kvm->mmu_lock);
  744. pgt = mmu->pgt;
  745. if (pgt) {
  746. mmu->pgd_phys = 0;
  747. mmu->pgt = NULL;
  748. free_percpu(mmu->last_vcpu_ran);
  749. }
  750. write_unlock(&kvm->mmu_lock);
  751. if (pgt) {
  752. kvm_pgtable_stage2_destroy(pgt);
  753. kfree(pgt);
  754. }
  755. }
  756. static void hyp_mc_free_fn(void *addr, void *args)
  757. {
  758. bool account_stage2 = (bool)args;
  759. if (account_stage2)
  760. kvm_account_pgtable_pages(addr, -1);
  761. free_page((unsigned long)addr);
  762. }
  763. static void *hyp_mc_alloc_fn(void *unused)
  764. {
  765. void *addr = (void *)__get_free_page(GFP_KERNEL_ACCOUNT);
  766. if (addr)
  767. kvm_account_pgtable_pages(addr, 1);
  768. return addr;
  769. }
  770. static void account_hyp_memcache(struct kvm_hyp_memcache *mc,
  771. unsigned long prev_nr_pages,
  772. struct kvm *kvm)
  773. {
  774. unsigned long nr_pages = mc->nr_pages;
  775. if (prev_nr_pages == nr_pages)
  776. return;
  777. if (nr_pages > prev_nr_pages) {
  778. atomic64_add((nr_pages - prev_nr_pages) << PAGE_SHIFT,
  779. &kvm->stat.protected_hyp_mem);
  780. } else {
  781. atomic64_sub((prev_nr_pages - nr_pages) << PAGE_SHIFT,
  782. &kvm->stat.protected_hyp_mem);
  783. }
  784. }
  785. static void __free_account_hyp_memcache(struct kvm_hyp_memcache *mc,
  786. struct kvm *kvm,
  787. bool account_stage2)
  788. {
  789. unsigned long prev_nr_pages;
  790. if (!is_protected_kvm_enabled())
  791. return;
  792. prev_nr_pages = mc->nr_pages;
  793. __free_hyp_memcache(mc, hyp_mc_free_fn, kvm_host_va,
  794. (void *)account_stage2);
  795. account_hyp_memcache(mc, prev_nr_pages, kvm);
  796. }
  797. void free_hyp_memcache(struct kvm_hyp_memcache *mc, struct kvm *kvm)
  798. {
  799. __free_account_hyp_memcache(mc, kvm, false);
  800. }
  801. /*
  802. * All pages donated to the hypervisor through kvm_hyp_memcache are for the
  803. * stage-2 page table. However, kvm_hyp_memcache is also a vehicule to retrieve
  804. * meta-data from the hypervisor, hence the need for a stage2 specific free
  805. * function.
  806. */
  807. void free_hyp_stage2_memcache(struct kvm_hyp_memcache *mc, struct kvm *kvm)
  808. {
  809. __free_account_hyp_memcache(mc, kvm, true);
  810. }
  811. int topup_hyp_memcache(struct kvm_vcpu *vcpu)
  812. {
  813. struct kvm_hyp_memcache *mc = &vcpu->arch.pkvm_memcache;
  814. unsigned long prev_nr_pages;
  815. int err;
  816. if (!is_protected_kvm_enabled())
  817. return 0;
  818. prev_nr_pages = mc->nr_pages;
  819. err = __topup_hyp_memcache(mc, kvm_mmu_cache_min_pages(vcpu->kvm),
  820. hyp_mc_alloc_fn,
  821. kvm_host_pa, NULL);
  822. if (!err)
  823. account_hyp_memcache(mc, prev_nr_pages, vcpu->kvm);
  824. return err;
  825. }
  826. /**
  827. * kvm_phys_addr_ioremap - map a device range to guest IPA
  828. *
  829. * @kvm: The KVM pointer
  830. * @guest_ipa: The IPA at which to insert the mapping
  831. * @pa: The physical address of the device
  832. * @size: The size of the mapping
  833. * @writable: Whether or not to create a writable mapping
  834. */
  835. int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
  836. phys_addr_t pa, unsigned long size, bool writable)
  837. {
  838. phys_addr_t addr;
  839. int ret = 0;
  840. struct kvm_mmu_memory_cache cache = { .gfp_zero = __GFP_ZERO };
  841. struct kvm_pgtable *pgt = kvm->arch.mmu.pgt;
  842. enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_DEVICE |
  843. KVM_PGTABLE_PROT_R |
  844. (writable ? KVM_PGTABLE_PROT_W : 0);
  845. if (is_protected_kvm_enabled())
  846. return -EPERM;
  847. size += offset_in_page(guest_ipa);
  848. guest_ipa &= PAGE_MASK;
  849. for (addr = guest_ipa; addr < guest_ipa + size; addr += PAGE_SIZE) {
  850. ret = kvm_mmu_topup_memory_cache(&cache,
  851. kvm_mmu_cache_min_pages(kvm));
  852. if (ret)
  853. break;
  854. write_lock(&kvm->mmu_lock);
  855. ret = kvm_pgtable_stage2_map(pgt, addr, PAGE_SIZE, pa, prot,
  856. &cache);
  857. write_unlock(&kvm->mmu_lock);
  858. if (ret)
  859. break;
  860. pa += PAGE_SIZE;
  861. }
  862. kvm_mmu_free_memory_cache(&cache);
  863. return ret;
  864. }
  865. /**
  866. * stage2_wp_range() - write protect stage2 memory region range
  867. * @mmu: The KVM stage-2 MMU pointer
  868. * @addr: Start address of range
  869. * @end: End address of range
  870. */
  871. static void stage2_wp_range(struct kvm_s2_mmu *mmu, phys_addr_t addr, phys_addr_t end)
  872. {
  873. struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
  874. stage2_apply_range_resched(kvm, addr, end, kvm_pgtable_stage2_wrprotect);
  875. }
  876. /**
  877. * kvm_mmu_wp_memory_region() - write protect stage 2 entries for memory slot
  878. * @kvm: The KVM pointer
  879. * @slot: The memory slot to write protect
  880. *
  881. * Called to start logging dirty pages after memory region
  882. * KVM_MEM_LOG_DIRTY_PAGES operation is called. After this function returns
  883. * all present PUD, PMD and PTEs are write protected in the memory region.
  884. * Afterwards read of dirty page log can be called.
  885. *
  886. * Acquires kvm_mmu_lock. Called with kvm->slots_lock mutex acquired,
  887. * serializing operations for VM memory regions.
  888. */
  889. static void kvm_mmu_wp_memory_region(struct kvm *kvm, int slot)
  890. {
  891. struct kvm_memslots *slots = kvm_memslots(kvm);
  892. struct kvm_memory_slot *memslot = id_to_memslot(slots, slot);
  893. phys_addr_t start, end;
  894. if (WARN_ON_ONCE(!memslot))
  895. return;
  896. start = memslot->base_gfn << PAGE_SHIFT;
  897. end = (memslot->base_gfn + memslot->npages) << PAGE_SHIFT;
  898. write_lock(&kvm->mmu_lock);
  899. stage2_wp_range(&kvm->arch.mmu, start, end);
  900. write_unlock(&kvm->mmu_lock);
  901. kvm_flush_remote_tlbs(kvm);
  902. }
  903. /**
  904. * kvm_mmu_write_protect_pt_masked() - write protect dirty pages
  905. * @kvm: The KVM pointer
  906. * @slot: The memory slot associated with mask
  907. * @gfn_offset: The gfn offset in memory slot
  908. * @mask: The mask of dirty pages at offset 'gfn_offset' in this memory
  909. * slot to be write protected
  910. *
  911. * Walks bits set in mask write protects the associated pte's. Caller must
  912. * acquire kvm_mmu_lock.
  913. */
  914. static void kvm_mmu_write_protect_pt_masked(struct kvm *kvm,
  915. struct kvm_memory_slot *slot,
  916. gfn_t gfn_offset, unsigned long mask)
  917. {
  918. phys_addr_t base_gfn = slot->base_gfn + gfn_offset;
  919. phys_addr_t start = (base_gfn + __ffs(mask)) << PAGE_SHIFT;
  920. phys_addr_t end = (base_gfn + __fls(mask) + 1) << PAGE_SHIFT;
  921. stage2_wp_range(&kvm->arch.mmu, start, end);
  922. }
  923. /*
  924. * kvm_arch_mmu_enable_log_dirty_pt_masked - enable dirty logging for selected
  925. * dirty pages.
  926. *
  927. * It calls kvm_mmu_write_protect_pt_masked to write protect selected pages to
  928. * enable dirty logging for them.
  929. */
  930. void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm,
  931. struct kvm_memory_slot *slot,
  932. gfn_t gfn_offset, unsigned long mask)
  933. {
  934. kvm_mmu_write_protect_pt_masked(kvm, slot, gfn_offset, mask);
  935. }
  936. static void kvm_send_hwpoison_signal(unsigned long address, short lsb)
  937. {
  938. send_sig_mceerr(BUS_MCEERR_AR, (void __user *)address, lsb, current);
  939. }
  940. static bool fault_supports_stage2_huge_mapping(struct kvm_memory_slot *memslot,
  941. unsigned long hva,
  942. unsigned long map_size)
  943. {
  944. gpa_t gpa_start;
  945. hva_t uaddr_start, uaddr_end;
  946. size_t size;
  947. /* The memslot and the VMA are guaranteed to be aligned to PAGE_SIZE */
  948. if (map_size == PAGE_SIZE)
  949. return true;
  950. size = memslot->npages * PAGE_SIZE;
  951. gpa_start = memslot->base_gfn << PAGE_SHIFT;
  952. uaddr_start = memslot->userspace_addr;
  953. uaddr_end = uaddr_start + size;
  954. /*
  955. * Pages belonging to memslots that don't have the same alignment
  956. * within a PMD/PUD for userspace and IPA cannot be mapped with stage-2
  957. * PMD/PUD entries, because we'll end up mapping the wrong pages.
  958. *
  959. * Consider a layout like the following:
  960. *
  961. * memslot->userspace_addr:
  962. * +-----+--------------------+--------------------+---+
  963. * |abcde|fgh Stage-1 block | Stage-1 block tv|xyz|
  964. * +-----+--------------------+--------------------+---+
  965. *
  966. * memslot->base_gfn << PAGE_SHIFT:
  967. * +---+--------------------+--------------------+-----+
  968. * |abc|def Stage-2 block | Stage-2 block |tvxyz|
  969. * +---+--------------------+--------------------+-----+
  970. *
  971. * If we create those stage-2 blocks, we'll end up with this incorrect
  972. * mapping:
  973. * d -> f
  974. * e -> g
  975. * f -> h
  976. */
  977. if ((gpa_start & (map_size - 1)) != (uaddr_start & (map_size - 1)))
  978. return false;
  979. /*
  980. * Next, let's make sure we're not trying to map anything not covered
  981. * by the memslot. This means we have to prohibit block size mappings
  982. * for the beginning and end of a non-block aligned and non-block sized
  983. * memory slot (illustrated by the head and tail parts of the
  984. * userspace view above containing pages 'abcde' and 'xyz',
  985. * respectively).
  986. *
  987. * Note that it doesn't matter if we do the check using the
  988. * userspace_addr or the base_gfn, as both are equally aligned (per
  989. * the check above) and equally sized.
  990. */
  991. return (hva & ~(map_size - 1)) >= uaddr_start &&
  992. (hva & ~(map_size - 1)) + map_size <= uaddr_end;
  993. }
  994. /*
  995. * Check if the given hva is backed by a transparent huge page (THP) and
  996. * whether it can be mapped using block mapping in stage2. If so, adjust
  997. * the stage2 PFN and IPA accordingly. Only PMD_SIZE THPs are currently
  998. * supported. This will need to be updated to support other THP sizes.
  999. *
  1000. * Returns the size of the mapping.
  1001. */
  1002. static long
  1003. transparent_hugepage_adjust(struct kvm *kvm, struct kvm_memory_slot *memslot,
  1004. unsigned long hva, kvm_pfn_t *pfnp,
  1005. phys_addr_t *ipap)
  1006. {
  1007. kvm_pfn_t pfn = *pfnp;
  1008. /*
  1009. * Make sure the adjustment is done only for THP pages. Also make
  1010. * sure that the HVA and IPA are sufficiently aligned and that the
  1011. * block map is contained within the memslot.
  1012. */
  1013. if (fault_supports_stage2_huge_mapping(memslot, hva, PMD_SIZE)) {
  1014. int sz = get_user_mapping_size(kvm, hva);
  1015. if (sz < 0)
  1016. return sz;
  1017. if (sz < PMD_SIZE)
  1018. return PAGE_SIZE;
  1019. /*
  1020. * The address we faulted on is backed by a transparent huge
  1021. * page. However, because we map the compound huge page and
  1022. * not the individual tail page, we need to transfer the
  1023. * refcount to the head page. We have to be careful that the
  1024. * THP doesn't start to split while we are adjusting the
  1025. * refcounts.
  1026. *
  1027. * We are sure this doesn't happen, because mmu_invalidate_retry
  1028. * was successful and we are holding the mmu_lock, so if this
  1029. * THP is trying to split, it will be blocked in the mmu
  1030. * notifier before touching any of the pages, specifically
  1031. * before being able to call __split_huge_page_refcount().
  1032. *
  1033. * We can therefore safely transfer the refcount from PG_tail
  1034. * to PG_head and switch the pfn from a tail page to the head
  1035. * page accordingly.
  1036. */
  1037. *ipap &= PMD_MASK;
  1038. kvm_release_pfn_clean(pfn);
  1039. pfn &= ~(PTRS_PER_PMD - 1);
  1040. get_page(pfn_to_page(pfn));
  1041. *pfnp = pfn;
  1042. return PMD_SIZE;
  1043. }
  1044. /* Use page mapping if we cannot use block mapping. */
  1045. return PAGE_SIZE;
  1046. }
  1047. static int get_vma_page_shift(struct vm_area_struct *vma, unsigned long hva)
  1048. {
  1049. unsigned long pa;
  1050. if (is_vm_hugetlb_page(vma) && !(vma->vm_flags & VM_PFNMAP))
  1051. return huge_page_shift(hstate_vma(vma));
  1052. if (!(vma->vm_flags & VM_PFNMAP))
  1053. return PAGE_SHIFT;
  1054. VM_BUG_ON(is_vm_hugetlb_page(vma));
  1055. pa = (vma->vm_pgoff << PAGE_SHIFT) + (hva - vma->vm_start);
  1056. #ifndef __PAGETABLE_PMD_FOLDED
  1057. if ((hva & (PUD_SIZE - 1)) == (pa & (PUD_SIZE - 1)) &&
  1058. ALIGN_DOWN(hva, PUD_SIZE) >= vma->vm_start &&
  1059. ALIGN(hva, PUD_SIZE) <= vma->vm_end)
  1060. return PUD_SHIFT;
  1061. #endif
  1062. if ((hva & (PMD_SIZE - 1)) == (pa & (PMD_SIZE - 1)) &&
  1063. ALIGN_DOWN(hva, PMD_SIZE) >= vma->vm_start &&
  1064. ALIGN(hva, PMD_SIZE) <= vma->vm_end)
  1065. return PMD_SHIFT;
  1066. return PAGE_SHIFT;
  1067. }
  1068. /*
  1069. * The page will be mapped in stage 2 as Normal Cacheable, so the VM will be
  1070. * able to see the page's tags and therefore they must be initialised first. If
  1071. * PG_mte_tagged is set, tags have already been initialised.
  1072. *
  1073. * The race in the test/set of the PG_mte_tagged flag is handled by:
  1074. * - preventing VM_SHARED mappings in a memslot with MTE preventing two VMs
  1075. * racing to santise the same page
  1076. * - mmap_lock protects between a VM faulting a page in and the VMM performing
  1077. * an mprotect() to add VM_MTE
  1078. */
  1079. static int sanitise_mte_tags(struct kvm *kvm, kvm_pfn_t pfn,
  1080. unsigned long size)
  1081. {
  1082. unsigned long i, nr_pages = size >> PAGE_SHIFT;
  1083. struct page *page;
  1084. if (!kvm_has_mte(kvm))
  1085. return 0;
  1086. /*
  1087. * pfn_to_online_page() is used to reject ZONE_DEVICE pages
  1088. * that may not support tags.
  1089. */
  1090. page = pfn_to_online_page(pfn);
  1091. if (!page)
  1092. return -EFAULT;
  1093. for (i = 0; i < nr_pages; i++, page++) {
  1094. if (!page_mte_tagged(page)) {
  1095. mte_clear_page_tags(page_address(page));
  1096. set_page_mte_tagged(page);
  1097. }
  1098. }
  1099. return 0;
  1100. }
  1101. static int pkvm_host_map_guest(u64 pfn, u64 gfn)
  1102. {
  1103. int ret = kvm_call_hyp_nvhe(__pkvm_host_map_guest, pfn, gfn);
  1104. /*
  1105. * Getting -EPERM at this point implies that the pfn has already been
  1106. * mapped. This should only ever happen when two vCPUs faulted on the
  1107. * same page, and the current one lost the race to do the mapping.
  1108. */
  1109. return (ret == -EPERM) ? -EAGAIN : ret;
  1110. }
  1111. static int cmp_ppages(struct rb_node *node, const struct rb_node *parent)
  1112. {
  1113. struct kvm_pinned_page *a = container_of(node, struct kvm_pinned_page, node);
  1114. struct kvm_pinned_page *b = container_of(parent, struct kvm_pinned_page, node);
  1115. if (a->ipa < b->ipa)
  1116. return -1;
  1117. if (a->ipa > b->ipa)
  1118. return 1;
  1119. return 0;
  1120. }
  1121. static int insert_ppage(struct kvm *kvm, struct kvm_pinned_page *ppage)
  1122. {
  1123. if (rb_find_add(&ppage->node, &kvm->arch.pkvm.pinned_pages, cmp_ppages))
  1124. return -EEXIST;
  1125. return 0;
  1126. }
  1127. static int pkvm_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
  1128. unsigned long hva)
  1129. {
  1130. struct mm_struct *mm = current->mm;
  1131. unsigned int flags = FOLL_HWPOISON | FOLL_LONGTERM | FOLL_WRITE;
  1132. struct kvm_pinned_page *ppage;
  1133. struct kvm *kvm = vcpu->kvm;
  1134. struct page *page;
  1135. u64 pfn;
  1136. int ret;
  1137. ret = topup_hyp_memcache(vcpu);
  1138. if (ret)
  1139. return -ENOMEM;
  1140. ppage = kmalloc(sizeof(*ppage), GFP_KERNEL_ACCOUNT);
  1141. if (!ppage)
  1142. return -ENOMEM;
  1143. ret = account_locked_vm(mm, 1, true);
  1144. if (ret)
  1145. goto free_ppage;
  1146. mmap_read_lock(mm);
  1147. ret = pin_user_pages(hva, 1, flags, &page, NULL);
  1148. mmap_read_unlock(mm);
  1149. if (ret == -EHWPOISON) {
  1150. kvm_send_hwpoison_signal(hva, PAGE_SHIFT);
  1151. ret = 0;
  1152. goto dec_account;
  1153. } else if (ret != 1) {
  1154. ret = -EFAULT;
  1155. goto dec_account;
  1156. } else if (!PageSwapBacked(page)) {
  1157. /*
  1158. * We really can't deal with page-cache pages returned by GUP
  1159. * because (a) we may trigger writeback of a page for which we
  1160. * no longer have access and (b) page_mkclean() won't find the
  1161. * stage-2 mapping in the rmap so we can get out-of-whack with
  1162. * the filesystem when marking the page dirty during unpinning
  1163. * (see cc5095747edf ("ext4: don't BUG if someone dirty pages
  1164. * without asking ext4 first")).
  1165. *
  1166. * Ideally we'd just restrict ourselves to anonymous pages, but
  1167. * we also want to allow memfd (i.e. shmem) pages, so check for
  1168. * pages backed by swap in the knowledge that the GUP pin will
  1169. * prevent try_to_unmap() from succeeding.
  1170. */
  1171. ret = -EIO;
  1172. goto unpin;
  1173. }
  1174. write_lock(&kvm->mmu_lock);
  1175. pfn = page_to_pfn(page);
  1176. ret = pkvm_host_map_guest(pfn, fault_ipa >> PAGE_SHIFT);
  1177. if (ret) {
  1178. if (ret == -EAGAIN)
  1179. ret = 0;
  1180. goto unlock;
  1181. }
  1182. ppage->page = page;
  1183. ppage->ipa = fault_ipa;
  1184. WARN_ON(insert_ppage(kvm, ppage));
  1185. write_unlock(&kvm->mmu_lock);
  1186. return 0;
  1187. unlock:
  1188. write_unlock(&kvm->mmu_lock);
  1189. unpin:
  1190. unpin_user_pages(&page, 1);
  1191. dec_account:
  1192. account_locked_vm(mm, 1, false);
  1193. free_ppage:
  1194. kfree(ppage);
  1195. return ret;
  1196. }
  1197. static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
  1198. struct kvm_memory_slot *memslot, unsigned long hva,
  1199. unsigned long fault_status)
  1200. {
  1201. int ret = 0;
  1202. bool write_fault, writable, force_pte = false;
  1203. bool exec_fault;
  1204. bool device = false;
  1205. bool shared;
  1206. unsigned long mmu_seq;
  1207. struct kvm *kvm = vcpu->kvm;
  1208. struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache;
  1209. struct vm_area_struct *vma;
  1210. short vma_shift;
  1211. gfn_t gfn;
  1212. kvm_pfn_t pfn;
  1213. bool logging_active = memslot_is_logging(memslot);
  1214. bool use_read_lock = false;
  1215. unsigned long fault_level = kvm_vcpu_trap_get_fault_level(vcpu);
  1216. long vma_pagesize, fault_granule;
  1217. enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_R;
  1218. struct kvm_pgtable *pgt;
  1219. fault_granule = 1UL << ARM64_HW_PGTABLE_LEVEL_SHIFT(fault_level);
  1220. write_fault = kvm_is_write_fault(vcpu);
  1221. exec_fault = kvm_vcpu_trap_is_exec_fault(vcpu);
  1222. VM_BUG_ON(write_fault && exec_fault);
  1223. if (fault_status == FSC_PERM && !write_fault && !exec_fault) {
  1224. kvm_err("Unexpected L2 read permission error\n");
  1225. return -EFAULT;
  1226. }
  1227. /*
  1228. * Permission faults just need to update the existing leaf entry,
  1229. * and so normally don't require allocations from the memcache. The
  1230. * only exception to this is when dirty logging is enabled at runtime
  1231. * and a write fault needs to collapse a block entry into a table.
  1232. */
  1233. if (fault_status != FSC_PERM ||
  1234. (logging_active && write_fault)) {
  1235. ret = kvm_mmu_topup_memory_cache(memcache,
  1236. kvm_mmu_cache_min_pages(kvm));
  1237. if (ret)
  1238. return ret;
  1239. }
  1240. /*
  1241. * Let's check if we will get back a huge page backed by hugetlbfs, or
  1242. * get block mapping for device MMIO region.
  1243. */
  1244. mmap_read_lock(current->mm);
  1245. vma = vma_lookup(current->mm, hva);
  1246. if (unlikely(!vma)) {
  1247. kvm_err("Failed to find VMA for hva 0x%lx\n", hva);
  1248. mmap_read_unlock(current->mm);
  1249. return -EFAULT;
  1250. }
  1251. /*
  1252. * logging_active is guaranteed to never be true for VM_PFNMAP
  1253. * memslots.
  1254. */
  1255. if (logging_active) {
  1256. force_pte = true;
  1257. vma_shift = PAGE_SHIFT;
  1258. use_read_lock = (fault_status == FSC_PERM && write_fault &&
  1259. fault_granule == PAGE_SIZE);
  1260. } else {
  1261. vma_shift = get_vma_page_shift(vma, hva);
  1262. }
  1263. shared = (vma->vm_flags & VM_SHARED);
  1264. switch (vma_shift) {
  1265. #ifndef __PAGETABLE_PMD_FOLDED
  1266. case PUD_SHIFT:
  1267. if (fault_supports_stage2_huge_mapping(memslot, hva, PUD_SIZE))
  1268. break;
  1269. fallthrough;
  1270. #endif
  1271. case CONT_PMD_SHIFT:
  1272. vma_shift = PMD_SHIFT;
  1273. fallthrough;
  1274. case PMD_SHIFT:
  1275. if (fault_supports_stage2_huge_mapping(memslot, hva, PMD_SIZE))
  1276. break;
  1277. fallthrough;
  1278. case CONT_PTE_SHIFT:
  1279. vma_shift = PAGE_SHIFT;
  1280. force_pte = true;
  1281. fallthrough;
  1282. case PAGE_SHIFT:
  1283. break;
  1284. default:
  1285. WARN_ONCE(1, "Unknown vma_shift %d", vma_shift);
  1286. }
  1287. vma_pagesize = 1UL << vma_shift;
  1288. if (vma_pagesize == PMD_SIZE || vma_pagesize == PUD_SIZE)
  1289. fault_ipa &= ~(vma_pagesize - 1);
  1290. gfn = fault_ipa >> PAGE_SHIFT;
  1291. /*
  1292. * Read mmu_invalidate_seq so that KVM can detect if the results of
  1293. * vma_lookup() or __gfn_to_pfn_memslot() become stale prior to
  1294. * acquiring kvm->mmu_lock.
  1295. *
  1296. * Rely on mmap_read_unlock() for an implicit smp_rmb(), which pairs
  1297. * with the smp_wmb() in kvm_mmu_invalidate_end().
  1298. */
  1299. mmu_seq = vcpu->kvm->mmu_invalidate_seq;
  1300. mmap_read_unlock(current->mm);
  1301. pfn = __gfn_to_pfn_memslot(memslot, gfn, false, NULL,
  1302. write_fault, &writable, NULL);
  1303. if (pfn == KVM_PFN_ERR_HWPOISON) {
  1304. kvm_send_hwpoison_signal(hva, vma_shift);
  1305. return 0;
  1306. }
  1307. if (is_error_noslot_pfn(pfn))
  1308. return -EFAULT;
  1309. if (kvm_is_device_pfn(pfn)) {
  1310. /*
  1311. * If the page was identified as device early by looking at
  1312. * the VMA flags, vma_pagesize is already representing the
  1313. * largest quantity we can map. If instead it was mapped
  1314. * via gfn_to_pfn_prot(), vma_pagesize is set to PAGE_SIZE
  1315. * and must not be upgraded.
  1316. *
  1317. * In both cases, we don't let transparent_hugepage_adjust()
  1318. * change things at the last minute.
  1319. */
  1320. device = true;
  1321. } else if (logging_active && !write_fault) {
  1322. /*
  1323. * Only actually map the page as writable if this was a write
  1324. * fault.
  1325. */
  1326. writable = false;
  1327. }
  1328. if (exec_fault && device)
  1329. return -ENOEXEC;
  1330. /*
  1331. * To reduce MMU contentions and enhance concurrency during dirty
  1332. * logging dirty logging, only acquire read lock for permission
  1333. * relaxation.
  1334. */
  1335. if (use_read_lock)
  1336. read_lock(&kvm->mmu_lock);
  1337. else
  1338. write_lock(&kvm->mmu_lock);
  1339. pgt = vcpu->arch.hw_mmu->pgt;
  1340. if (mmu_invalidate_retry(kvm, mmu_seq))
  1341. goto out_unlock;
  1342. /*
  1343. * If we are not forced to use page mapping, check if we are
  1344. * backed by a THP and thus use block mapping if possible.
  1345. */
  1346. if (vma_pagesize == PAGE_SIZE && !(force_pte || device)) {
  1347. if (fault_status == FSC_PERM && fault_granule > PAGE_SIZE)
  1348. vma_pagesize = fault_granule;
  1349. else
  1350. vma_pagesize = transparent_hugepage_adjust(kvm, memslot,
  1351. hva, &pfn,
  1352. &fault_ipa);
  1353. if (vma_pagesize < 0) {
  1354. ret = vma_pagesize;
  1355. goto out_unlock;
  1356. }
  1357. }
  1358. if (fault_status != FSC_PERM && !device && kvm_has_mte(kvm)) {
  1359. /* Check the VMM hasn't introduced a new VM_SHARED VMA */
  1360. if (!shared)
  1361. ret = sanitise_mte_tags(kvm, pfn, vma_pagesize);
  1362. else
  1363. ret = -EFAULT;
  1364. if (ret)
  1365. goto out_unlock;
  1366. }
  1367. if (writable)
  1368. prot |= KVM_PGTABLE_PROT_W;
  1369. if (exec_fault)
  1370. prot |= KVM_PGTABLE_PROT_X;
  1371. if (device)
  1372. prot |= KVM_PGTABLE_PROT_DEVICE;
  1373. else if (cpus_have_const_cap(ARM64_HAS_CACHE_DIC))
  1374. prot |= KVM_PGTABLE_PROT_X;
  1375. /*
  1376. * Under the premise of getting a FSC_PERM fault, we just need to relax
  1377. * permissions only if vma_pagesize equals fault_granule. Otherwise,
  1378. * kvm_pgtable_stage2_map() should be called to change block size.
  1379. */
  1380. if (fault_status == FSC_PERM && vma_pagesize == fault_granule) {
  1381. ret = kvm_pgtable_stage2_relax_perms(pgt, fault_ipa, prot);
  1382. } else {
  1383. WARN_ONCE(use_read_lock, "Attempted stage-2 map outside of write lock\n");
  1384. ret = kvm_pgtable_stage2_map(pgt, fault_ipa, vma_pagesize,
  1385. __pfn_to_phys(pfn), prot,
  1386. memcache);
  1387. }
  1388. /* Mark the page dirty only if the fault is handled successfully */
  1389. if (writable && !ret) {
  1390. kvm_set_pfn_dirty(pfn);
  1391. mark_page_dirty_in_slot(kvm, memslot, gfn);
  1392. }
  1393. out_unlock:
  1394. if (use_read_lock)
  1395. read_unlock(&kvm->mmu_lock);
  1396. else
  1397. write_unlock(&kvm->mmu_lock);
  1398. kvm_set_pfn_accessed(pfn);
  1399. kvm_release_pfn_clean(pfn);
  1400. return ret != -EAGAIN ? ret : 0;
  1401. }
  1402. /* Resolve the access fault by making the page young again. */
  1403. static void handle_access_fault(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa)
  1404. {
  1405. pte_t pte;
  1406. kvm_pte_t kpte;
  1407. struct kvm_s2_mmu *mmu;
  1408. trace_kvm_access_fault(fault_ipa);
  1409. write_lock(&vcpu->kvm->mmu_lock);
  1410. mmu = vcpu->arch.hw_mmu;
  1411. kpte = kvm_pgtable_stage2_mkyoung(mmu->pgt, fault_ipa);
  1412. write_unlock(&vcpu->kvm->mmu_lock);
  1413. pte = __pte(kpte);
  1414. if (pte_valid(pte))
  1415. kvm_set_pfn_accessed(pte_pfn(pte));
  1416. }
  1417. /**
  1418. * kvm_handle_guest_abort - handles all 2nd stage aborts
  1419. * @vcpu: the VCPU pointer
  1420. *
  1421. * Any abort that gets to the host is almost guaranteed to be caused by a
  1422. * missing second stage translation table entry, which can mean that either the
  1423. * guest simply needs more memory and we must allocate an appropriate page or it
  1424. * can mean that the guest tried to access I/O memory, which is emulated by user
  1425. * space. The distinction is based on the IPA causing the fault and whether this
  1426. * memory region has been registered as standard RAM by user space.
  1427. */
  1428. int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)
  1429. {
  1430. unsigned long fault_status;
  1431. phys_addr_t fault_ipa;
  1432. struct kvm_memory_slot *memslot;
  1433. unsigned long hva;
  1434. bool is_iabt, write_fault, writable;
  1435. gfn_t gfn;
  1436. int ret, idx;
  1437. fault_status = kvm_vcpu_trap_get_fault_type(vcpu);
  1438. fault_ipa = kvm_vcpu_get_fault_ipa(vcpu);
  1439. is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
  1440. if (fault_status == FSC_FAULT) {
  1441. /* Beyond sanitised PARange (which is the IPA limit) */
  1442. if (fault_ipa >= BIT_ULL(get_kvm_ipa_limit())) {
  1443. kvm_inject_size_fault(vcpu);
  1444. return 1;
  1445. }
  1446. /* Falls between the IPA range and the PARange? */
  1447. if (!is_protected_kvm_enabled() &&
  1448. fault_ipa >= BIT_ULL(vcpu->arch.hw_mmu->pgt->ia_bits)) {
  1449. fault_ipa |= kvm_vcpu_get_hfar(vcpu) & GENMASK(11, 0);
  1450. if (is_iabt)
  1451. kvm_inject_pabt(vcpu, fault_ipa);
  1452. else
  1453. kvm_inject_dabt(vcpu, fault_ipa);
  1454. return 1;
  1455. }
  1456. }
  1457. /* Synchronous External Abort? */
  1458. if (kvm_vcpu_abt_issea(vcpu)) {
  1459. /*
  1460. * For RAS the host kernel may handle this abort.
  1461. * There is no need to pass the error into the guest.
  1462. */
  1463. if (kvm_handle_guest_sea(fault_ipa, kvm_vcpu_get_esr(vcpu)))
  1464. kvm_inject_vabt(vcpu);
  1465. return 1;
  1466. }
  1467. trace_kvm_guest_fault(*vcpu_pc(vcpu), kvm_vcpu_get_esr(vcpu),
  1468. kvm_vcpu_get_hfar(vcpu), fault_ipa);
  1469. /* Check the stage-2 fault is trans. fault or write fault */
  1470. if (fault_status != FSC_FAULT && fault_status != FSC_PERM &&
  1471. fault_status != FSC_ACCESS) {
  1472. kvm_err("Unsupported FSC: EC=%#x xFSC=%#lx ESR_EL2=%#lx\n",
  1473. kvm_vcpu_trap_get_class(vcpu),
  1474. (unsigned long)kvm_vcpu_trap_get_fault(vcpu),
  1475. (unsigned long)kvm_vcpu_get_esr(vcpu));
  1476. return -EFAULT;
  1477. }
  1478. idx = srcu_read_lock(&vcpu->kvm->srcu);
  1479. gfn = fault_ipa >> PAGE_SHIFT;
  1480. memslot = gfn_to_memslot(vcpu->kvm, gfn);
  1481. hva = gfn_to_hva_memslot_prot(memslot, gfn, &writable);
  1482. write_fault = kvm_is_write_fault(vcpu);
  1483. if (kvm_is_error_hva(hva) || (write_fault && !writable)) {
  1484. /*
  1485. * The guest has put either its instructions or its page-tables
  1486. * somewhere it shouldn't have. Userspace won't be able to do
  1487. * anything about this (there's no syndrome for a start), so
  1488. * re-inject the abort back into the guest.
  1489. */
  1490. if (is_iabt) {
  1491. ret = -ENOEXEC;
  1492. goto out;
  1493. }
  1494. if (kvm_vcpu_abt_iss1tw(vcpu)) {
  1495. kvm_inject_dabt(vcpu, kvm_vcpu_get_hfar(vcpu));
  1496. ret = 1;
  1497. goto out_unlock;
  1498. }
  1499. /*
  1500. * Check for a cache maintenance operation. Since we
  1501. * ended-up here, we know it is outside of any memory
  1502. * slot. But we can't find out if that is for a device,
  1503. * or if the guest is just being stupid. The only thing
  1504. * we know for sure is that this range cannot be cached.
  1505. *
  1506. * So let's assume that the guest is just being
  1507. * cautious, and skip the instruction.
  1508. */
  1509. if (kvm_is_error_hva(hva) && kvm_vcpu_dabt_is_cm(vcpu)) {
  1510. kvm_incr_pc(vcpu);
  1511. ret = 1;
  1512. goto out_unlock;
  1513. }
  1514. /*
  1515. * The IPA is reported as [MAX:12], so we need to
  1516. * complement it with the bottom 12 bits from the
  1517. * faulting VA. This is always 12 bits, irrespective
  1518. * of the page size.
  1519. */
  1520. fault_ipa |= kvm_vcpu_get_hfar(vcpu) & FAR_MASK;
  1521. ret = io_mem_abort(vcpu, fault_ipa);
  1522. goto out_unlock;
  1523. }
  1524. /* Userspace should not be able to register out-of-bounds IPAs */
  1525. VM_BUG_ON(fault_ipa >= kvm_phys_size(vcpu->kvm));
  1526. if (fault_status == FSC_ACCESS) {
  1527. handle_access_fault(vcpu, fault_ipa);
  1528. ret = 1;
  1529. goto out_unlock;
  1530. }
  1531. if (is_protected_kvm_enabled())
  1532. ret = pkvm_mem_abort(vcpu, fault_ipa, hva);
  1533. else
  1534. ret = user_mem_abort(vcpu, fault_ipa, memslot, hva, fault_status);
  1535. if (ret == 0)
  1536. ret = 1;
  1537. out:
  1538. if (ret == -ENOEXEC) {
  1539. kvm_inject_pabt(vcpu, kvm_vcpu_get_hfar(vcpu));
  1540. ret = 1;
  1541. }
  1542. out_unlock:
  1543. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  1544. return ret;
  1545. }
  1546. bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
  1547. {
  1548. if (!kvm->arch.mmu.pgt)
  1549. return false;
  1550. __unmap_stage2_range(&kvm->arch.mmu, range->start << PAGE_SHIFT,
  1551. (range->end - range->start) << PAGE_SHIFT,
  1552. range->may_block);
  1553. return false;
  1554. }
  1555. bool kvm_set_spte_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
  1556. {
  1557. kvm_pfn_t pfn = pte_pfn(range->pte);
  1558. int ret;
  1559. if (!kvm->arch.mmu.pgt)
  1560. return false;
  1561. WARN_ON(range->end - range->start != 1);
  1562. ret = sanitise_mte_tags(kvm, pfn, PAGE_SIZE);
  1563. if (ret)
  1564. return false;
  1565. /*
  1566. * We've moved a page around, probably through CoW, so let's treat
  1567. * it just like a translation fault and the map handler will clean
  1568. * the cache to the PoC.
  1569. *
  1570. * The MMU notifiers will have unmapped a huge PMD before calling
  1571. * ->change_pte() (which in turn calls kvm_set_spte_gfn()) and
  1572. * therefore we never need to clear out a huge PMD through this
  1573. * calling path and a memcache is not required.
  1574. */
  1575. kvm_pgtable_stage2_map(kvm->arch.mmu.pgt, range->start << PAGE_SHIFT,
  1576. PAGE_SIZE, __pfn_to_phys(pfn),
  1577. KVM_PGTABLE_PROT_R, NULL);
  1578. return false;
  1579. }
  1580. bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
  1581. {
  1582. u64 size = (range->end - range->start) << PAGE_SHIFT;
  1583. kvm_pte_t kpte;
  1584. pte_t pte;
  1585. if (!kvm->arch.mmu.pgt)
  1586. return false;
  1587. WARN_ON(size != PAGE_SIZE && size != PMD_SIZE && size != PUD_SIZE);
  1588. kpte = kvm_pgtable_stage2_mkold(kvm->arch.mmu.pgt,
  1589. range->start << PAGE_SHIFT);
  1590. pte = __pte(kpte);
  1591. return pte_valid(pte) && pte_young(pte);
  1592. }
  1593. bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
  1594. {
  1595. if (!kvm->arch.mmu.pgt)
  1596. return false;
  1597. return kvm_pgtable_stage2_is_young(kvm->arch.mmu.pgt,
  1598. range->start << PAGE_SHIFT);
  1599. }
  1600. phys_addr_t kvm_mmu_get_httbr(void)
  1601. {
  1602. return __pa(hyp_pgtable->pgd);
  1603. }
  1604. phys_addr_t kvm_get_idmap_vector(void)
  1605. {
  1606. return hyp_idmap_vector;
  1607. }
  1608. static int kvm_map_idmap_text(void)
  1609. {
  1610. unsigned long size = hyp_idmap_end - hyp_idmap_start;
  1611. int err = __create_hyp_mappings(hyp_idmap_start, size, hyp_idmap_start,
  1612. PAGE_HYP_EXEC);
  1613. if (err)
  1614. kvm_err("Failed to idmap %lx-%lx\n",
  1615. hyp_idmap_start, hyp_idmap_end);
  1616. return err;
  1617. }
  1618. static void *kvm_hyp_zalloc_page(void *arg)
  1619. {
  1620. return (void *)get_zeroed_page(GFP_KERNEL);
  1621. }
  1622. static struct kvm_pgtable_mm_ops kvm_hyp_mm_ops = {
  1623. .zalloc_page = kvm_hyp_zalloc_page,
  1624. .get_page = kvm_host_get_page,
  1625. .put_page = kvm_host_put_page,
  1626. .phys_to_virt = kvm_host_va,
  1627. .virt_to_phys = kvm_host_pa,
  1628. };
  1629. int kvm_mmu_init(u32 *hyp_va_bits)
  1630. {
  1631. int err;
  1632. hyp_idmap_start = __pa_symbol(__hyp_idmap_text_start);
  1633. hyp_idmap_start = ALIGN_DOWN(hyp_idmap_start, PAGE_SIZE);
  1634. hyp_idmap_end = __pa_symbol(__hyp_idmap_text_end);
  1635. hyp_idmap_end = ALIGN(hyp_idmap_end, PAGE_SIZE);
  1636. hyp_idmap_vector = __pa_symbol(__kvm_hyp_init);
  1637. /*
  1638. * We rely on the linker script to ensure at build time that the HYP
  1639. * init code does not cross a page boundary.
  1640. */
  1641. BUG_ON((hyp_idmap_start ^ (hyp_idmap_end - 1)) & PAGE_MASK);
  1642. *hyp_va_bits = 64 - ((idmap_t0sz & TCR_T0SZ_MASK) >> TCR_T0SZ_OFFSET);
  1643. kvm_debug("Using %u-bit virtual addresses at EL2\n", *hyp_va_bits);
  1644. kvm_debug("IDMAP page: %lx\n", hyp_idmap_start);
  1645. kvm_debug("HYP VA range: %lx:%lx\n",
  1646. kern_hyp_va(PAGE_OFFSET),
  1647. kern_hyp_va((unsigned long)high_memory - 1));
  1648. if (hyp_idmap_start >= kern_hyp_va(PAGE_OFFSET) &&
  1649. hyp_idmap_start < kern_hyp_va((unsigned long)high_memory - 1) &&
  1650. hyp_idmap_start != (unsigned long)__hyp_idmap_text_start) {
  1651. /*
  1652. * The idmap page is intersecting with the VA space,
  1653. * it is not safe to continue further.
  1654. */
  1655. kvm_err("IDMAP intersecting with HYP VA, unable to continue\n");
  1656. err = -EINVAL;
  1657. goto out;
  1658. }
  1659. hyp_pgtable = kzalloc(sizeof(*hyp_pgtable), GFP_KERNEL);
  1660. if (!hyp_pgtable) {
  1661. kvm_err("Hyp mode page-table not allocated\n");
  1662. err = -ENOMEM;
  1663. goto out;
  1664. }
  1665. err = kvm_pgtable_hyp_init(hyp_pgtable, *hyp_va_bits, &kvm_hyp_mm_ops);
  1666. if (err)
  1667. goto out_free_pgtable;
  1668. err = kvm_map_idmap_text();
  1669. if (err)
  1670. goto out_destroy_pgtable;
  1671. io_map_base = hyp_idmap_start;
  1672. return 0;
  1673. out_destroy_pgtable:
  1674. kvm_pgtable_hyp_destroy(hyp_pgtable);
  1675. out_free_pgtable:
  1676. kfree(hyp_pgtable);
  1677. hyp_pgtable = NULL;
  1678. out:
  1679. return err;
  1680. }
  1681. void kvm_arch_commit_memory_region(struct kvm *kvm,
  1682. struct kvm_memory_slot *old,
  1683. const struct kvm_memory_slot *new,
  1684. enum kvm_mr_change change)
  1685. {
  1686. /*
  1687. * At this point memslot has been committed and there is an
  1688. * allocated dirty_bitmap[], dirty pages will be tracked while the
  1689. * memory slot is write protected.
  1690. */
  1691. if (change != KVM_MR_DELETE && new->flags & KVM_MEM_LOG_DIRTY_PAGES) {
  1692. /*
  1693. * If we're with initial-all-set, we don't need to write
  1694. * protect any pages because they're all reported as dirty.
  1695. * Huge pages and normal pages will be write protect gradually.
  1696. */
  1697. if (!kvm_dirty_log_manual_protect_and_init_set(kvm)) {
  1698. kvm_mmu_wp_memory_region(kvm, new->id);
  1699. }
  1700. }
  1701. }
  1702. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  1703. const struct kvm_memory_slot *old,
  1704. struct kvm_memory_slot *new,
  1705. enum kvm_mr_change change)
  1706. {
  1707. hva_t hva, reg_end;
  1708. int ret = 0;
  1709. if (is_protected_kvm_enabled()) {
  1710. /* In protected mode, cannot modify memslots once a VM has run. */
  1711. if ((change == KVM_MR_DELETE || change == KVM_MR_MOVE) &&
  1712. kvm->arch.pkvm.handle) {
  1713. return -EPERM;
  1714. }
  1715. if (new &&
  1716. new->flags & (KVM_MEM_LOG_DIRTY_PAGES | KVM_MEM_READONLY)) {
  1717. return -EPERM;
  1718. }
  1719. }
  1720. if (change != KVM_MR_CREATE && change != KVM_MR_MOVE &&
  1721. change != KVM_MR_FLAGS_ONLY)
  1722. return 0;
  1723. /*
  1724. * Prevent userspace from creating a memory region outside of the IPA
  1725. * space addressable by the KVM guest IPA space.
  1726. */
  1727. if ((new->base_gfn + new->npages) > (kvm_phys_size(kvm) >> PAGE_SHIFT))
  1728. return -EFAULT;
  1729. hva = new->userspace_addr;
  1730. reg_end = hva + (new->npages << PAGE_SHIFT);
  1731. mmap_read_lock(current->mm);
  1732. /*
  1733. * A memory region could potentially cover multiple VMAs, and any holes
  1734. * between them, so iterate over all of them.
  1735. *
  1736. * +--------------------------------------------+
  1737. * +---------------+----------------+ +----------------+
  1738. * | : VMA 1 | VMA 2 | | VMA 3 : |
  1739. * +---------------+----------------+ +----------------+
  1740. * | memory region |
  1741. * +--------------------------------------------+
  1742. */
  1743. do {
  1744. struct vm_area_struct *vma;
  1745. vma = find_vma_intersection(current->mm, hva, reg_end);
  1746. if (!vma)
  1747. break;
  1748. /*
  1749. * VM_SHARED mappings are not allowed with MTE to avoid races
  1750. * when updating the PG_mte_tagged page flag, see
  1751. * sanitise_mte_tags for more details.
  1752. */
  1753. if (kvm_has_mte(kvm) && vma->vm_flags & VM_SHARED) {
  1754. ret = -EINVAL;
  1755. break;
  1756. }
  1757. if (vma->vm_flags & VM_PFNMAP) {
  1758. /* IO region dirty page logging not allowed */
  1759. if (new->flags & KVM_MEM_LOG_DIRTY_PAGES) {
  1760. ret = -EINVAL;
  1761. break;
  1762. }
  1763. }
  1764. hva = min(reg_end, vma->vm_end);
  1765. } while (hva < reg_end);
  1766. mmap_read_unlock(current->mm);
  1767. return ret;
  1768. }
  1769. void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot)
  1770. {
  1771. }
  1772. void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen)
  1773. {
  1774. }
  1775. void kvm_arch_flush_shadow_all(struct kvm *kvm)
  1776. {
  1777. kvm_free_stage2_pgd(&kvm->arch.mmu);
  1778. }
  1779. void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
  1780. struct kvm_memory_slot *slot)
  1781. {
  1782. gpa_t gpa = slot->base_gfn << PAGE_SHIFT;
  1783. phys_addr_t size = slot->npages << PAGE_SHIFT;
  1784. /* Stage-2 is managed by hyp in protected mode. */
  1785. if (is_protected_kvm_enabled())
  1786. return;
  1787. write_lock(&kvm->mmu_lock);
  1788. unmap_stage2_range(&kvm->arch.mmu, gpa, size);
  1789. write_unlock(&kvm->mmu_lock);
  1790. }
  1791. /*
  1792. * See note at ARMv7 ARM B1.14.4 (TL;DR: S/W ops are not easily virtualized).
  1793. *
  1794. * Main problems:
  1795. * - S/W ops are local to a CPU (not broadcast)
  1796. * - We have line migration behind our back (speculation)
  1797. * - System caches don't support S/W at all (damn!)
  1798. *
  1799. * In the face of the above, the best we can do is to try and convert
  1800. * S/W ops to VA ops. Because the guest is not allowed to infer the
  1801. * S/W to PA mapping, it can only use S/W to nuke the whole cache,
  1802. * which is a rather good thing for us.
  1803. *
  1804. * Also, it is only used when turning caches on/off ("The expected
  1805. * usage of the cache maintenance instructions that operate by set/way
  1806. * is associated with the cache maintenance instructions associated
  1807. * with the powerdown and powerup of caches, if this is required by
  1808. * the implementation.").
  1809. *
  1810. * We use the following policy:
  1811. *
  1812. * - If we trap a S/W operation, we enable VM trapping to detect
  1813. * caches being turned on/off, and do a full clean.
  1814. *
  1815. * - We flush the caches on both caches being turned on and off.
  1816. *
  1817. * - Once the caches are enabled, we stop trapping VM ops.
  1818. */
  1819. void kvm_set_way_flush(struct kvm_vcpu *vcpu)
  1820. {
  1821. unsigned long hcr = *vcpu_hcr(vcpu);
  1822. /*
  1823. * If this is the first time we do a S/W operation
  1824. * (i.e. HCR_TVM not set) flush the whole memory, and set the
  1825. * VM trapping.
  1826. *
  1827. * Otherwise, rely on the VM trapping to wait for the MMU +
  1828. * Caches to be turned off. At that point, we'll be able to
  1829. * clean the caches again.
  1830. */
  1831. if (!(hcr & HCR_TVM)) {
  1832. trace_kvm_set_way_flush(*vcpu_pc(vcpu),
  1833. vcpu_has_cache_enabled(vcpu));
  1834. stage2_flush_vm(vcpu->kvm);
  1835. *vcpu_hcr(vcpu) = hcr | HCR_TVM;
  1836. }
  1837. }
  1838. void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled)
  1839. {
  1840. bool now_enabled = vcpu_has_cache_enabled(vcpu);
  1841. /*
  1842. * If switching the MMU+caches on, need to invalidate the caches.
  1843. * If switching it off, need to clean the caches.
  1844. * Clean + invalidate does the trick always.
  1845. */
  1846. if (now_enabled != was_enabled)
  1847. stage2_flush_vm(vcpu->kvm);
  1848. /* Caches are now on, stop trapping VM ops (until a S/W op) */
  1849. if (now_enabled)
  1850. *vcpu_hcr(vcpu) &= ~HCR_TVM;
  1851. trace_kvm_toggle_cache(*vcpu_pc(vcpu), was_enabled, now_enabled);
  1852. }