kvm_main.c 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289
  1. /*
  2. * Kernel-based Virtual Machine driver for Linux
  3. *
  4. * This module enables machines with Intel VT-x extensions to run virtual
  5. * machines without emulation or binary translation.
  6. *
  7. * Copyright (C) 2006 Qumranet, Inc.
  8. * Copyright 2010 Red Hat, Inc. and/or its affiliates.
  9. *
  10. * Authors:
  11. * Avi Kivity <avi@qumranet.com>
  12. * Yaniv Kamay <yaniv@qumranet.com>
  13. *
  14. * This work is licensed under the terms of the GNU GPL, version 2. See
  15. * the COPYING file in the top-level directory.
  16. *
  17. */
  18. #include "iodev.h"
  19. #include <linux/kvm_host.h>
  20. #include <linux/kvm.h>
  21. #include <linux/module.h>
  22. #include <linux/errno.h>
  23. #include <linux/percpu.h>
  24. #include <linux/mm.h>
  25. #include <linux/miscdevice.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/reboot.h>
  28. #include <linux/debugfs.h>
  29. #include <linux/highmem.h>
  30. #include <linux/file.h>
  31. #include <linux/syscore_ops.h>
  32. #include <linux/cpu.h>
  33. #include <linux/sched.h>
  34. #include <linux/cpumask.h>
  35. #include <linux/smp.h>
  36. #include <linux/anon_inodes.h>
  37. #include <linux/profile.h>
  38. #include <linux/kvm_para.h>
  39. #include <linux/pagemap.h>
  40. #include <linux/mman.h>
  41. #include <linux/swap.h>
  42. #include <linux/bitops.h>
  43. #include <linux/spinlock.h>
  44. #include <linux/compat.h>
  45. #include <linux/srcu.h>
  46. #include <linux/hugetlb.h>
  47. #include <linux/slab.h>
  48. #include <linux/sort.h>
  49. #include <linux/bsearch.h>
  50. #include <asm/processor.h>
  51. #include <asm/io.h>
  52. #include <asm/uaccess.h>
  53. #include <asm/pgtable.h>
  54. #include "coalesced_mmio.h"
  55. #include "async_pf.h"
  56. #define CREATE_TRACE_POINTS
  57. #include <trace/events/kvm.h>
  58. MODULE_AUTHOR("Qumranet");
  59. MODULE_LICENSE("GPL");
  60. /*
  61. * Ordering of locks:
  62. *
  63. * kvm->lock --> kvm->slots_lock --> kvm->irq_lock
  64. */
  65. DEFINE_SPINLOCK(kvm_lock);
  66. static DEFINE_RAW_SPINLOCK(kvm_count_lock);
  67. LIST_HEAD(vm_list);
  68. static cpumask_var_t cpus_hardware_enabled;
  69. static int kvm_usage_count = 0;
  70. static atomic_t hardware_enable_failed;
  71. struct kmem_cache *kvm_vcpu_cache;
  72. EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
  73. static __read_mostly struct preempt_ops kvm_preempt_ops;
  74. struct dentry *kvm_debugfs_dir;
  75. static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
  76. unsigned long arg);
  77. #ifdef CONFIG_COMPAT
  78. static long kvm_vcpu_compat_ioctl(struct file *file, unsigned int ioctl,
  79. unsigned long arg);
  80. #endif
  81. static int hardware_enable_all(void);
  82. static void hardware_disable_all(void);
  83. static void kvm_io_bus_destroy(struct kvm_io_bus *bus);
  84. bool kvm_rebooting;
  85. EXPORT_SYMBOL_GPL(kvm_rebooting);
  86. static bool largepages_enabled = true;
  87. bool kvm_is_mmio_pfn(pfn_t pfn)
  88. {
  89. if (pfn_valid(pfn))
  90. return PageReserved(pfn_to_page(pfn));
  91. return true;
  92. }
  93. /*
  94. * Switches to specified vcpu, until a matching vcpu_put()
  95. */
  96. int vcpu_load(struct kvm_vcpu *vcpu)
  97. {
  98. int cpu;
  99. if (mutex_lock_killable(&vcpu->mutex))
  100. return -EINTR;
  101. if (unlikely(vcpu->pid != current->pids[PIDTYPE_PID].pid)) {
  102. /* The thread running this VCPU changed. */
  103. struct pid *oldpid = vcpu->pid;
  104. struct pid *newpid = get_task_pid(current, PIDTYPE_PID);
  105. rcu_assign_pointer(vcpu->pid, newpid);
  106. synchronize_rcu();
  107. put_pid(oldpid);
  108. }
  109. cpu = get_cpu();
  110. preempt_notifier_register(&vcpu->preempt_notifier);
  111. kvm_arch_vcpu_load(vcpu, cpu);
  112. put_cpu();
  113. return 0;
  114. }
  115. void vcpu_put(struct kvm_vcpu *vcpu)
  116. {
  117. preempt_disable();
  118. kvm_arch_vcpu_put(vcpu);
  119. preempt_notifier_unregister(&vcpu->preempt_notifier);
  120. preempt_enable();
  121. mutex_unlock(&vcpu->mutex);
  122. }
  123. static void ack_flush(void *_completed)
  124. {
  125. }
  126. static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
  127. {
  128. int i, cpu, me;
  129. cpumask_var_t cpus;
  130. bool called = true;
  131. struct kvm_vcpu *vcpu;
  132. zalloc_cpumask_var(&cpus, GFP_ATOMIC);
  133. me = get_cpu();
  134. kvm_for_each_vcpu(i, vcpu, kvm) {
  135. kvm_make_request(req, vcpu);
  136. cpu = vcpu->cpu;
  137. /* Set ->requests bit before we read ->mode */
  138. smp_mb();
  139. if (cpus != NULL && cpu != -1 && cpu != me &&
  140. kvm_vcpu_exiting_guest_mode(vcpu) != OUTSIDE_GUEST_MODE)
  141. cpumask_set_cpu(cpu, cpus);
  142. }
  143. if (unlikely(cpus == NULL))
  144. smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
  145. else if (!cpumask_empty(cpus))
  146. smp_call_function_many(cpus, ack_flush, NULL, 1);
  147. else
  148. called = false;
  149. put_cpu();
  150. free_cpumask_var(cpus);
  151. return called;
  152. }
  153. void kvm_flush_remote_tlbs(struct kvm *kvm)
  154. {
  155. long dirty_count = kvm->tlbs_dirty;
  156. smp_mb();
  157. if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
  158. ++kvm->stat.remote_tlb_flush;
  159. cmpxchg(&kvm->tlbs_dirty, dirty_count, 0);
  160. }
  161. void kvm_reload_remote_mmus(struct kvm *kvm)
  162. {
  163. make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
  164. }
  165. void kvm_make_mclock_inprogress_request(struct kvm *kvm)
  166. {
  167. make_all_cpus_request(kvm, KVM_REQ_MCLOCK_INPROGRESS);
  168. }
  169. void kvm_make_scan_ioapic_request(struct kvm *kvm)
  170. {
  171. make_all_cpus_request(kvm, KVM_REQ_SCAN_IOAPIC);
  172. }
  173. int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
  174. {
  175. struct page *page;
  176. int r;
  177. mutex_init(&vcpu->mutex);
  178. vcpu->cpu = -1;
  179. vcpu->kvm = kvm;
  180. vcpu->vcpu_id = id;
  181. vcpu->pid = NULL;
  182. init_waitqueue_head(&vcpu->wq);
  183. kvm_async_pf_vcpu_init(vcpu);
  184. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  185. if (!page) {
  186. r = -ENOMEM;
  187. goto fail;
  188. }
  189. vcpu->run = page_address(page);
  190. kvm_vcpu_set_in_spin_loop(vcpu, false);
  191. kvm_vcpu_set_dy_eligible(vcpu, false);
  192. vcpu->preempted = false;
  193. r = kvm_arch_vcpu_init(vcpu);
  194. if (r < 0)
  195. goto fail_free_run;
  196. return 0;
  197. fail_free_run:
  198. free_page((unsigned long)vcpu->run);
  199. fail:
  200. return r;
  201. }
  202. EXPORT_SYMBOL_GPL(kvm_vcpu_init);
  203. void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
  204. {
  205. put_pid(vcpu->pid);
  206. kvm_arch_vcpu_uninit(vcpu);
  207. free_page((unsigned long)vcpu->run);
  208. }
  209. EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
  210. #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
  211. static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
  212. {
  213. return container_of(mn, struct kvm, mmu_notifier);
  214. }
  215. static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
  216. struct mm_struct *mm,
  217. unsigned long address)
  218. {
  219. struct kvm *kvm = mmu_notifier_to_kvm(mn);
  220. int need_tlb_flush, idx;
  221. /*
  222. * When ->invalidate_page runs, the linux pte has been zapped
  223. * already but the page is still allocated until
  224. * ->invalidate_page returns. So if we increase the sequence
  225. * here the kvm page fault will notice if the spte can't be
  226. * established because the page is going to be freed. If
  227. * instead the kvm page fault establishes the spte before
  228. * ->invalidate_page runs, kvm_unmap_hva will release it
  229. * before returning.
  230. *
  231. * The sequence increase only need to be seen at spin_unlock
  232. * time, and not at spin_lock time.
  233. *
  234. * Increasing the sequence after the spin_unlock would be
  235. * unsafe because the kvm page fault could then establish the
  236. * pte after kvm_unmap_hva returned, without noticing the page
  237. * is going to be freed.
  238. */
  239. idx = srcu_read_lock(&kvm->srcu);
  240. spin_lock(&kvm->mmu_lock);
  241. kvm->mmu_notifier_seq++;
  242. need_tlb_flush = kvm_unmap_hva(kvm, address) | kvm->tlbs_dirty;
  243. /* we've to flush the tlb before the pages can be freed */
  244. if (need_tlb_flush)
  245. kvm_flush_remote_tlbs(kvm);
  246. spin_unlock(&kvm->mmu_lock);
  247. srcu_read_unlock(&kvm->srcu, idx);
  248. }
  249. static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
  250. struct mm_struct *mm,
  251. unsigned long address,
  252. pte_t pte)
  253. {
  254. struct kvm *kvm = mmu_notifier_to_kvm(mn);
  255. int idx;
  256. idx = srcu_read_lock(&kvm->srcu);
  257. spin_lock(&kvm->mmu_lock);
  258. kvm->mmu_notifier_seq++;
  259. kvm_set_spte_hva(kvm, address, pte);
  260. spin_unlock(&kvm->mmu_lock);
  261. srcu_read_unlock(&kvm->srcu, idx);
  262. }
  263. static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
  264. struct mm_struct *mm,
  265. unsigned long start,
  266. unsigned long end)
  267. {
  268. struct kvm *kvm = mmu_notifier_to_kvm(mn);
  269. int need_tlb_flush = 0, idx;
  270. idx = srcu_read_lock(&kvm->srcu);
  271. spin_lock(&kvm->mmu_lock);
  272. /*
  273. * The count increase must become visible at unlock time as no
  274. * spte can be established without taking the mmu_lock and
  275. * count is also read inside the mmu_lock critical section.
  276. */
  277. kvm->mmu_notifier_count++;
  278. need_tlb_flush = kvm_unmap_hva_range(kvm, start, end);
  279. need_tlb_flush |= kvm->tlbs_dirty;
  280. /* we've to flush the tlb before the pages can be freed */
  281. if (need_tlb_flush)
  282. kvm_flush_remote_tlbs(kvm);
  283. spin_unlock(&kvm->mmu_lock);
  284. srcu_read_unlock(&kvm->srcu, idx);
  285. }
  286. static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
  287. struct mm_struct *mm,
  288. unsigned long start,
  289. unsigned long end)
  290. {
  291. struct kvm *kvm = mmu_notifier_to_kvm(mn);
  292. spin_lock(&kvm->mmu_lock);
  293. /*
  294. * This sequence increase will notify the kvm page fault that
  295. * the page that is going to be mapped in the spte could have
  296. * been freed.
  297. */
  298. kvm->mmu_notifier_seq++;
  299. smp_wmb();
  300. /*
  301. * The above sequence increase must be visible before the
  302. * below count decrease, which is ensured by the smp_wmb above
  303. * in conjunction with the smp_rmb in mmu_notifier_retry().
  304. */
  305. kvm->mmu_notifier_count--;
  306. spin_unlock(&kvm->mmu_lock);
  307. BUG_ON(kvm->mmu_notifier_count < 0);
  308. }
  309. static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
  310. struct mm_struct *mm,
  311. unsigned long address)
  312. {
  313. struct kvm *kvm = mmu_notifier_to_kvm(mn);
  314. int young, idx;
  315. idx = srcu_read_lock(&kvm->srcu);
  316. spin_lock(&kvm->mmu_lock);
  317. young = kvm_age_hva(kvm, address);
  318. if (young)
  319. kvm_flush_remote_tlbs(kvm);
  320. spin_unlock(&kvm->mmu_lock);
  321. srcu_read_unlock(&kvm->srcu, idx);
  322. return young;
  323. }
  324. static int kvm_mmu_notifier_test_young(struct mmu_notifier *mn,
  325. struct mm_struct *mm,
  326. unsigned long address)
  327. {
  328. struct kvm *kvm = mmu_notifier_to_kvm(mn);
  329. int young, idx;
  330. idx = srcu_read_lock(&kvm->srcu);
  331. spin_lock(&kvm->mmu_lock);
  332. young = kvm_test_age_hva(kvm, address);
  333. spin_unlock(&kvm->mmu_lock);
  334. srcu_read_unlock(&kvm->srcu, idx);
  335. return young;
  336. }
  337. static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
  338. struct mm_struct *mm)
  339. {
  340. struct kvm *kvm = mmu_notifier_to_kvm(mn);
  341. int idx;
  342. idx = srcu_read_lock(&kvm->srcu);
  343. kvm_arch_flush_shadow_all(kvm);
  344. srcu_read_unlock(&kvm->srcu, idx);
  345. }
  346. static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
  347. .invalidate_page = kvm_mmu_notifier_invalidate_page,
  348. .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
  349. .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
  350. .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
  351. .test_young = kvm_mmu_notifier_test_young,
  352. .change_pte = kvm_mmu_notifier_change_pte,
  353. .release = kvm_mmu_notifier_release,
  354. };
  355. static int kvm_init_mmu_notifier(struct kvm *kvm)
  356. {
  357. kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
  358. return mmu_notifier_register(&kvm->mmu_notifier, current->mm);
  359. }
  360. #else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
  361. static int kvm_init_mmu_notifier(struct kvm *kvm)
  362. {
  363. return 0;
  364. }
  365. #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
  366. static void kvm_init_memslots_id(struct kvm *kvm)
  367. {
  368. int i;
  369. struct kvm_memslots *slots = kvm->memslots;
  370. for (i = 0; i < KVM_MEM_SLOTS_NUM; i++)
  371. slots->id_to_index[i] = slots->memslots[i].id = i;
  372. }
  373. static struct kvm *kvm_create_vm(unsigned long type)
  374. {
  375. int r, i;
  376. struct kvm *kvm = kvm_arch_alloc_vm();
  377. if (!kvm)
  378. return ERR_PTR(-ENOMEM);
  379. r = kvm_arch_init_vm(kvm, type);
  380. if (r)
  381. goto out_err_nodisable;
  382. r = hardware_enable_all();
  383. if (r)
  384. goto out_err_nodisable;
  385. #ifdef CONFIG_HAVE_KVM_IRQCHIP
  386. INIT_HLIST_HEAD(&kvm->mask_notifier_list);
  387. INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
  388. #endif
  389. BUILD_BUG_ON(KVM_MEM_SLOTS_NUM > SHRT_MAX);
  390. r = -ENOMEM;
  391. kvm->memslots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
  392. if (!kvm->memslots)
  393. goto out_err_nosrcu;
  394. kvm_init_memslots_id(kvm);
  395. if (init_srcu_struct(&kvm->srcu))
  396. goto out_err_nosrcu;
  397. for (i = 0; i < KVM_NR_BUSES; i++) {
  398. kvm->buses[i] = kzalloc(sizeof(struct kvm_io_bus),
  399. GFP_KERNEL);
  400. if (!kvm->buses[i])
  401. goto out_err;
  402. }
  403. spin_lock_init(&kvm->mmu_lock);
  404. kvm->mm = current->mm;
  405. atomic_inc(&kvm->mm->mm_count);
  406. kvm_eventfd_init(kvm);
  407. mutex_init(&kvm->lock);
  408. mutex_init(&kvm->irq_lock);
  409. mutex_init(&kvm->slots_lock);
  410. atomic_set(&kvm->users_count, 1);
  411. INIT_LIST_HEAD(&kvm->devices);
  412. r = kvm_init_mmu_notifier(kvm);
  413. if (r)
  414. goto out_err;
  415. spin_lock(&kvm_lock);
  416. list_add(&kvm->vm_list, &vm_list);
  417. spin_unlock(&kvm_lock);
  418. return kvm;
  419. out_err:
  420. cleanup_srcu_struct(&kvm->srcu);
  421. out_err_nosrcu:
  422. hardware_disable_all();
  423. out_err_nodisable:
  424. for (i = 0; i < KVM_NR_BUSES; i++)
  425. kfree(kvm->buses[i]);
  426. kfree(kvm->memslots);
  427. kvm_arch_free_vm(kvm);
  428. return ERR_PTR(r);
  429. }
  430. /*
  431. * Avoid using vmalloc for a small buffer.
  432. * Should not be used when the size is statically known.
  433. */
  434. void *kvm_kvzalloc(unsigned long size)
  435. {
  436. if (size > PAGE_SIZE)
  437. return vzalloc(size);
  438. else
  439. return kzalloc(size, GFP_KERNEL);
  440. }
  441. void kvm_kvfree(const void *addr)
  442. {
  443. if (is_vmalloc_addr(addr))
  444. vfree(addr);
  445. else
  446. kfree(addr);
  447. }
  448. static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot)
  449. {
  450. if (!memslot->dirty_bitmap)
  451. return;
  452. kvm_kvfree(memslot->dirty_bitmap);
  453. memslot->dirty_bitmap = NULL;
  454. }
  455. /*
  456. * Free any memory in @free but not in @dont.
  457. */
  458. static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
  459. struct kvm_memory_slot *dont)
  460. {
  461. if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
  462. kvm_destroy_dirty_bitmap(free);
  463. kvm_arch_free_memslot(free, dont);
  464. free->npages = 0;
  465. }
  466. void kvm_free_physmem(struct kvm *kvm)
  467. {
  468. struct kvm_memslots *slots = kvm->memslots;
  469. struct kvm_memory_slot *memslot;
  470. kvm_for_each_memslot(memslot, slots)
  471. kvm_free_physmem_slot(memslot, NULL);
  472. kfree(kvm->memslots);
  473. }
  474. static void kvm_destroy_devices(struct kvm *kvm)
  475. {
  476. struct list_head *node, *tmp;
  477. list_for_each_safe(node, tmp, &kvm->devices) {
  478. struct kvm_device *dev =
  479. list_entry(node, struct kvm_device, vm_node);
  480. list_del(node);
  481. dev->ops->destroy(dev);
  482. }
  483. }
  484. static void kvm_destroy_vm(struct kvm *kvm)
  485. {
  486. int i;
  487. struct mm_struct *mm = kvm->mm;
  488. kvm_arch_sync_events(kvm);
  489. spin_lock(&kvm_lock);
  490. list_del(&kvm->vm_list);
  491. spin_unlock(&kvm_lock);
  492. kvm_free_irq_routing(kvm);
  493. for (i = 0; i < KVM_NR_BUSES; i++)
  494. kvm_io_bus_destroy(kvm->buses[i]);
  495. kvm_coalesced_mmio_free(kvm);
  496. #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
  497. mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
  498. #else
  499. kvm_arch_flush_shadow_all(kvm);
  500. #endif
  501. kvm_arch_destroy_vm(kvm);
  502. kvm_destroy_devices(kvm);
  503. kvm_free_physmem(kvm);
  504. cleanup_srcu_struct(&kvm->srcu);
  505. kvm_arch_free_vm(kvm);
  506. hardware_disable_all();
  507. mmdrop(mm);
  508. }
  509. void kvm_get_kvm(struct kvm *kvm)
  510. {
  511. atomic_inc(&kvm->users_count);
  512. }
  513. EXPORT_SYMBOL_GPL(kvm_get_kvm);
  514. void kvm_put_kvm(struct kvm *kvm)
  515. {
  516. if (atomic_dec_and_test(&kvm->users_count))
  517. kvm_destroy_vm(kvm);
  518. }
  519. EXPORT_SYMBOL_GPL(kvm_put_kvm);
  520. static int kvm_vm_release(struct inode *inode, struct file *filp)
  521. {
  522. struct kvm *kvm = filp->private_data;
  523. kvm_irqfd_release(kvm);
  524. kvm_put_kvm(kvm);
  525. return 0;
  526. }
  527. /*
  528. * Allocation size is twice as large as the actual dirty bitmap size.
  529. * See x86's kvm_vm_ioctl_get_dirty_log() why this is needed.
  530. */
  531. static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot)
  532. {
  533. #ifndef CONFIG_S390
  534. unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot);
  535. memslot->dirty_bitmap = kvm_kvzalloc(dirty_bytes);
  536. if (!memslot->dirty_bitmap)
  537. return -ENOMEM;
  538. #endif /* !CONFIG_S390 */
  539. return 0;
  540. }
  541. static int cmp_memslot(const void *slot1, const void *slot2)
  542. {
  543. struct kvm_memory_slot *s1, *s2;
  544. s1 = (struct kvm_memory_slot *)slot1;
  545. s2 = (struct kvm_memory_slot *)slot2;
  546. if (s1->npages < s2->npages)
  547. return 1;
  548. if (s1->npages > s2->npages)
  549. return -1;
  550. return 0;
  551. }
  552. /*
  553. * Sort the memslots base on its size, so the larger slots
  554. * will get better fit.
  555. */
  556. static void sort_memslots(struct kvm_memslots *slots)
  557. {
  558. int i;
  559. sort(slots->memslots, KVM_MEM_SLOTS_NUM,
  560. sizeof(struct kvm_memory_slot), cmp_memslot, NULL);
  561. for (i = 0; i < KVM_MEM_SLOTS_NUM; i++)
  562. slots->id_to_index[slots->memslots[i].id] = i;
  563. }
  564. void update_memslots(struct kvm_memslots *slots, struct kvm_memory_slot *new,
  565. u64 last_generation)
  566. {
  567. if (new) {
  568. int id = new->id;
  569. struct kvm_memory_slot *old = id_to_memslot(slots, id);
  570. unsigned long npages = old->npages;
  571. *old = *new;
  572. if (new->npages != npages)
  573. sort_memslots(slots);
  574. }
  575. slots->generation = last_generation + 1;
  576. }
  577. static int check_memory_region_flags(struct kvm_userspace_memory_region *mem)
  578. {
  579. u32 valid_flags = KVM_MEM_LOG_DIRTY_PAGES;
  580. #ifdef KVM_CAP_READONLY_MEM
  581. valid_flags |= KVM_MEM_READONLY;
  582. #endif
  583. if (mem->flags & ~valid_flags)
  584. return -EINVAL;
  585. return 0;
  586. }
  587. static struct kvm_memslots *install_new_memslots(struct kvm *kvm,
  588. struct kvm_memslots *slots, struct kvm_memory_slot *new)
  589. {
  590. struct kvm_memslots *old_memslots = kvm->memslots;
  591. update_memslots(slots, new, kvm->memslots->generation);
  592. rcu_assign_pointer(kvm->memslots, slots);
  593. synchronize_srcu_expedited(&kvm->srcu);
  594. kvm_arch_memslots_updated(kvm);
  595. return old_memslots;
  596. }
  597. /*
  598. * Allocate some memory and give it an address in the guest physical address
  599. * space.
  600. *
  601. * Discontiguous memory is allowed, mostly for framebuffers.
  602. *
  603. * Must be called holding mmap_sem for write.
  604. */
  605. int __kvm_set_memory_region(struct kvm *kvm,
  606. struct kvm_userspace_memory_region *mem)
  607. {
  608. int r;
  609. gfn_t base_gfn;
  610. unsigned long npages;
  611. struct kvm_memory_slot *slot;
  612. struct kvm_memory_slot old, new;
  613. struct kvm_memslots *slots = NULL, *old_memslots;
  614. enum kvm_mr_change change;
  615. r = check_memory_region_flags(mem);
  616. if (r)
  617. goto out;
  618. r = -EINVAL;
  619. /* General sanity checks */
  620. if (mem->memory_size & (PAGE_SIZE - 1))
  621. goto out;
  622. if (mem->guest_phys_addr & (PAGE_SIZE - 1))
  623. goto out;
  624. /* We can read the guest memory with __xxx_user() later on. */
  625. if ((mem->slot < KVM_USER_MEM_SLOTS) &&
  626. ((mem->userspace_addr & (PAGE_SIZE - 1)) ||
  627. !access_ok(VERIFY_WRITE,
  628. (void __user *)(unsigned long)mem->userspace_addr,
  629. mem->memory_size)))
  630. goto out;
  631. if (mem->slot >= KVM_MEM_SLOTS_NUM)
  632. goto out;
  633. if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
  634. goto out;
  635. slot = id_to_memslot(kvm->memslots, mem->slot);
  636. base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
  637. npages = mem->memory_size >> PAGE_SHIFT;
  638. r = -EINVAL;
  639. if (npages > KVM_MEM_MAX_NR_PAGES)
  640. goto out;
  641. if (!npages)
  642. mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
  643. new = old = *slot;
  644. new.id = mem->slot;
  645. new.base_gfn = base_gfn;
  646. new.npages = npages;
  647. new.flags = mem->flags;
  648. r = -EINVAL;
  649. if (npages) {
  650. if (!old.npages)
  651. change = KVM_MR_CREATE;
  652. else { /* Modify an existing slot. */
  653. if ((mem->userspace_addr != old.userspace_addr) ||
  654. (npages != old.npages) ||
  655. ((new.flags ^ old.flags) & KVM_MEM_READONLY))
  656. goto out;
  657. if (base_gfn != old.base_gfn)
  658. change = KVM_MR_MOVE;
  659. else if (new.flags != old.flags)
  660. change = KVM_MR_FLAGS_ONLY;
  661. else { /* Nothing to change. */
  662. r = 0;
  663. goto out;
  664. }
  665. }
  666. } else if (old.npages) {
  667. change = KVM_MR_DELETE;
  668. } else /* Modify a non-existent slot: disallowed. */
  669. goto out;
  670. if ((change == KVM_MR_CREATE) || (change == KVM_MR_MOVE)) {
  671. /* Check for overlaps */
  672. r = -EEXIST;
  673. kvm_for_each_memslot(slot, kvm->memslots) {
  674. if ((slot->id >= KVM_USER_MEM_SLOTS) ||
  675. (slot->id == mem->slot))
  676. continue;
  677. if (!((base_gfn + npages <= slot->base_gfn) ||
  678. (base_gfn >= slot->base_gfn + slot->npages)))
  679. goto out;
  680. }
  681. }
  682. /* Free page dirty bitmap if unneeded */
  683. if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
  684. new.dirty_bitmap = NULL;
  685. r = -ENOMEM;
  686. if (change == KVM_MR_CREATE) {
  687. new.userspace_addr = mem->userspace_addr;
  688. if (kvm_arch_create_memslot(&new, npages))
  689. goto out_free;
  690. }
  691. /* Allocate page dirty bitmap if needed */
  692. if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
  693. if (kvm_create_dirty_bitmap(&new) < 0)
  694. goto out_free;
  695. }
  696. if ((change == KVM_MR_DELETE) || (change == KVM_MR_MOVE)) {
  697. r = -ENOMEM;
  698. slots = kmemdup(kvm->memslots, sizeof(struct kvm_memslots),
  699. GFP_KERNEL);
  700. if (!slots)
  701. goto out_free;
  702. slot = id_to_memslot(slots, mem->slot);
  703. slot->flags |= KVM_MEMSLOT_INVALID;
  704. old_memslots = install_new_memslots(kvm, slots, NULL);
  705. /* slot was deleted or moved, clear iommu mapping */
  706. kvm_iommu_unmap_pages(kvm, &old);
  707. /* From this point no new shadow pages pointing to a deleted,
  708. * or moved, memslot will be created.
  709. *
  710. * validation of sp->gfn happens in:
  711. * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
  712. * - kvm_is_visible_gfn (mmu_check_roots)
  713. */
  714. kvm_arch_flush_shadow_memslot(kvm, slot);
  715. slots = old_memslots;
  716. }
  717. r = kvm_arch_prepare_memory_region(kvm, &new, mem, change);
  718. if (r)
  719. goto out_slots;
  720. r = -ENOMEM;
  721. /*
  722. * We can re-use the old_memslots from above, the only difference
  723. * from the currently installed memslots is the invalid flag. This
  724. * will get overwritten by update_memslots anyway.
  725. */
  726. if (!slots) {
  727. slots = kmemdup(kvm->memslots, sizeof(struct kvm_memslots),
  728. GFP_KERNEL);
  729. if (!slots)
  730. goto out_free;
  731. }
  732. /* actual memory is freed via old in kvm_free_physmem_slot below */
  733. if (change == KVM_MR_DELETE) {
  734. new.dirty_bitmap = NULL;
  735. memset(&new.arch, 0, sizeof(new.arch));
  736. }
  737. old_memslots = install_new_memslots(kvm, slots, &new);
  738. kvm_arch_commit_memory_region(kvm, mem, &old, change);
  739. kvm_free_physmem_slot(&old, &new);
  740. kfree(old_memslots);
  741. /*
  742. * IOMMU mapping: New slots need to be mapped. Old slots need to be
  743. * un-mapped and re-mapped if their base changes. Since base change
  744. * unmapping is handled above with slot deletion, mapping alone is
  745. * needed here. Anything else the iommu might care about for existing
  746. * slots (size changes, userspace addr changes and read-only flag
  747. * changes) is disallowed above, so any other attribute changes getting
  748. * here can be skipped.
  749. */
  750. if ((change == KVM_MR_CREATE) || (change == KVM_MR_MOVE)) {
  751. r = kvm_iommu_map_pages(kvm, &new);
  752. return r;
  753. }
  754. return 0;
  755. out_slots:
  756. kfree(slots);
  757. out_free:
  758. kvm_free_physmem_slot(&new, &old);
  759. out:
  760. return r;
  761. }
  762. EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
  763. int kvm_set_memory_region(struct kvm *kvm,
  764. struct kvm_userspace_memory_region *mem)
  765. {
  766. int r;
  767. mutex_lock(&kvm->slots_lock);
  768. r = __kvm_set_memory_region(kvm, mem);
  769. mutex_unlock(&kvm->slots_lock);
  770. return r;
  771. }
  772. EXPORT_SYMBOL_GPL(kvm_set_memory_region);
  773. int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
  774. struct kvm_userspace_memory_region *mem)
  775. {
  776. if (mem->slot >= KVM_USER_MEM_SLOTS)
  777. return -EINVAL;
  778. return kvm_set_memory_region(kvm, mem);
  779. }
  780. int kvm_get_dirty_log(struct kvm *kvm,
  781. struct kvm_dirty_log *log, int *is_dirty)
  782. {
  783. struct kvm_memory_slot *memslot;
  784. int r, i;
  785. unsigned long n;
  786. unsigned long any = 0;
  787. r = -EINVAL;
  788. if (log->slot >= KVM_USER_MEM_SLOTS)
  789. goto out;
  790. memslot = id_to_memslot(kvm->memslots, log->slot);
  791. r = -ENOENT;
  792. if (!memslot->dirty_bitmap)
  793. goto out;
  794. n = kvm_dirty_bitmap_bytes(memslot);
  795. for (i = 0; !any && i < n/sizeof(long); ++i)
  796. any = memslot->dirty_bitmap[i];
  797. r = -EFAULT;
  798. if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
  799. goto out;
  800. if (any)
  801. *is_dirty = 1;
  802. r = 0;
  803. out:
  804. return r;
  805. }
  806. bool kvm_largepages_enabled(void)
  807. {
  808. return largepages_enabled;
  809. }
  810. void kvm_disable_largepages(void)
  811. {
  812. largepages_enabled = false;
  813. }
  814. EXPORT_SYMBOL_GPL(kvm_disable_largepages);
  815. struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
  816. {
  817. return __gfn_to_memslot(kvm_memslots(kvm), gfn);
  818. }
  819. EXPORT_SYMBOL_GPL(gfn_to_memslot);
  820. int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
  821. {
  822. struct kvm_memory_slot *memslot = gfn_to_memslot(kvm, gfn);
  823. if (!memslot || memslot->id >= KVM_USER_MEM_SLOTS ||
  824. memslot->flags & KVM_MEMSLOT_INVALID)
  825. return 0;
  826. return 1;
  827. }
  828. EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
  829. unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn)
  830. {
  831. struct vm_area_struct *vma;
  832. unsigned long addr, size;
  833. size = PAGE_SIZE;
  834. addr = gfn_to_hva(kvm, gfn);
  835. if (kvm_is_error_hva(addr))
  836. return PAGE_SIZE;
  837. down_read(&current->mm->mmap_sem);
  838. vma = find_vma(current->mm, addr);
  839. if (!vma)
  840. goto out;
  841. size = vma_kernel_pagesize(vma);
  842. out:
  843. up_read(&current->mm->mmap_sem);
  844. return size;
  845. }
  846. static bool memslot_is_readonly(struct kvm_memory_slot *slot)
  847. {
  848. return slot->flags & KVM_MEM_READONLY;
  849. }
  850. static unsigned long __gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
  851. gfn_t *nr_pages, bool write)
  852. {
  853. if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
  854. return KVM_HVA_ERR_BAD;
  855. if (memslot_is_readonly(slot) && write)
  856. return KVM_HVA_ERR_RO_BAD;
  857. if (nr_pages)
  858. *nr_pages = slot->npages - (gfn - slot->base_gfn);
  859. return __gfn_to_hva_memslot(slot, gfn);
  860. }
  861. static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
  862. gfn_t *nr_pages)
  863. {
  864. return __gfn_to_hva_many(slot, gfn, nr_pages, true);
  865. }
  866. unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot,
  867. gfn_t gfn)
  868. {
  869. return gfn_to_hva_many(slot, gfn, NULL);
  870. }
  871. EXPORT_SYMBOL_GPL(gfn_to_hva_memslot);
  872. unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
  873. {
  874. return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL);
  875. }
  876. EXPORT_SYMBOL_GPL(gfn_to_hva);
  877. /*
  878. * If writable is set to false, the hva returned by this function is only
  879. * allowed to be read.
  880. */
  881. unsigned long gfn_to_hva_prot(struct kvm *kvm, gfn_t gfn, bool *writable)
  882. {
  883. struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
  884. unsigned long hva = __gfn_to_hva_many(slot, gfn, NULL, false);
  885. if (!kvm_is_error_hva(hva) && writable)
  886. *writable = !memslot_is_readonly(slot);
  887. return hva;
  888. }
  889. static int kvm_read_hva(void *data, void __user *hva, int len)
  890. {
  891. return __copy_from_user(data, hva, len);
  892. }
  893. static int kvm_read_hva_atomic(void *data, void __user *hva, int len)
  894. {
  895. return __copy_from_user_inatomic(data, hva, len);
  896. }
  897. static int get_user_page_nowait(struct task_struct *tsk, struct mm_struct *mm,
  898. unsigned long start, int write, struct page **page)
  899. {
  900. int flags = FOLL_TOUCH | FOLL_NOWAIT | FOLL_HWPOISON | FOLL_GET;
  901. if (write)
  902. flags |= FOLL_WRITE;
  903. return __get_user_pages(tsk, mm, start, 1, flags, page, NULL, NULL);
  904. }
  905. static inline int check_user_page_hwpoison(unsigned long addr)
  906. {
  907. int rc, flags = FOLL_TOUCH | FOLL_HWPOISON | FOLL_WRITE;
  908. rc = __get_user_pages(current, current->mm, addr, 1,
  909. flags, NULL, NULL, NULL);
  910. return rc == -EHWPOISON;
  911. }
  912. /*
  913. * The atomic path to get the writable pfn which will be stored in @pfn,
  914. * true indicates success, otherwise false is returned.
  915. */
  916. static bool hva_to_pfn_fast(unsigned long addr, bool atomic, bool *async,
  917. bool write_fault, bool *writable, pfn_t *pfn)
  918. {
  919. struct page *page[1];
  920. int npages;
  921. if (!(async || atomic))
  922. return false;
  923. /*
  924. * Fast pin a writable pfn only if it is a write fault request
  925. * or the caller allows to map a writable pfn for a read fault
  926. * request.
  927. */
  928. if (!(write_fault || writable))
  929. return false;
  930. npages = __get_user_pages_fast(addr, 1, 1, page);
  931. if (npages == 1) {
  932. *pfn = page_to_pfn(page[0]);
  933. if (writable)
  934. *writable = true;
  935. return true;
  936. }
  937. return false;
  938. }
  939. /*
  940. * The slow path to get the pfn of the specified host virtual address,
  941. * 1 indicates success, -errno is returned if error is detected.
  942. */
  943. static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault,
  944. bool *writable, pfn_t *pfn)
  945. {
  946. struct page *page[1];
  947. int npages = 0;
  948. might_sleep();
  949. if (writable)
  950. *writable = write_fault;
  951. if (async) {
  952. down_read(&current->mm->mmap_sem);
  953. npages = get_user_page_nowait(current, current->mm,
  954. addr, write_fault, page);
  955. up_read(&current->mm->mmap_sem);
  956. } else
  957. npages = get_user_pages_fast(addr, 1, write_fault,
  958. page);
  959. if (npages != 1)
  960. return npages;
  961. /* map read fault as writable if possible */
  962. if (unlikely(!write_fault) && writable) {
  963. struct page *wpage[1];
  964. npages = __get_user_pages_fast(addr, 1, 1, wpage);
  965. if (npages == 1) {
  966. *writable = true;
  967. put_page(page[0]);
  968. page[0] = wpage[0];
  969. }
  970. npages = 1;
  971. }
  972. *pfn = page_to_pfn(page[0]);
  973. return npages;
  974. }
  975. static bool vma_is_valid(struct vm_area_struct *vma, bool write_fault)
  976. {
  977. if (unlikely(!(vma->vm_flags & VM_READ)))
  978. return false;
  979. if (write_fault && (unlikely(!(vma->vm_flags & VM_WRITE))))
  980. return false;
  981. return true;
  982. }
  983. /*
  984. * Pin guest page in memory and return its pfn.
  985. * @addr: host virtual address which maps memory to the guest
  986. * @atomic: whether this function can sleep
  987. * @async: whether this function need to wait IO complete if the
  988. * host page is not in the memory
  989. * @write_fault: whether we should get a writable host page
  990. * @writable: whether it allows to map a writable host page for !@write_fault
  991. *
  992. * The function will map a writable host page for these two cases:
  993. * 1): @write_fault = true
  994. * 2): @write_fault = false && @writable, @writable will tell the caller
  995. * whether the mapping is writable.
  996. */
  997. static pfn_t hva_to_pfn(unsigned long addr, bool atomic, bool *async,
  998. bool write_fault, bool *writable)
  999. {
  1000. struct vm_area_struct *vma;
  1001. pfn_t pfn = 0;
  1002. int npages;
  1003. /* we can do it either atomically or asynchronously, not both */
  1004. BUG_ON(atomic && async);
  1005. if (hva_to_pfn_fast(addr, atomic, async, write_fault, writable, &pfn))
  1006. return pfn;
  1007. if (atomic)
  1008. return KVM_PFN_ERR_FAULT;
  1009. npages = hva_to_pfn_slow(addr, async, write_fault, writable, &pfn);
  1010. if (npages == 1)
  1011. return pfn;
  1012. down_read(&current->mm->mmap_sem);
  1013. if (npages == -EHWPOISON ||
  1014. (!async && check_user_page_hwpoison(addr))) {
  1015. pfn = KVM_PFN_ERR_HWPOISON;
  1016. goto exit;
  1017. }
  1018. vma = find_vma_intersection(current->mm, addr, addr + 1);
  1019. if (vma == NULL)
  1020. pfn = KVM_PFN_ERR_FAULT;
  1021. else if ((vma->vm_flags & VM_PFNMAP)) {
  1022. pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) +
  1023. vma->vm_pgoff;
  1024. BUG_ON(!kvm_is_mmio_pfn(pfn));
  1025. } else {
  1026. if (async && vma_is_valid(vma, write_fault))
  1027. *async = true;
  1028. pfn = KVM_PFN_ERR_FAULT;
  1029. }
  1030. exit:
  1031. up_read(&current->mm->mmap_sem);
  1032. return pfn;
  1033. }
  1034. static pfn_t
  1035. __gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn, bool atomic,
  1036. bool *async, bool write_fault, bool *writable)
  1037. {
  1038. unsigned long addr = __gfn_to_hva_many(slot, gfn, NULL, write_fault);
  1039. if (addr == KVM_HVA_ERR_RO_BAD)
  1040. return KVM_PFN_ERR_RO_FAULT;
  1041. if (kvm_is_error_hva(addr))
  1042. return KVM_PFN_NOSLOT;
  1043. /* Do not map writable pfn in the readonly memslot. */
  1044. if (writable && memslot_is_readonly(slot)) {
  1045. *writable = false;
  1046. writable = NULL;
  1047. }
  1048. return hva_to_pfn(addr, atomic, async, write_fault,
  1049. writable);
  1050. }
  1051. static pfn_t __gfn_to_pfn(struct kvm *kvm, gfn_t gfn, bool atomic, bool *async,
  1052. bool write_fault, bool *writable)
  1053. {
  1054. struct kvm_memory_slot *slot;
  1055. if (async)
  1056. *async = false;
  1057. slot = gfn_to_memslot(kvm, gfn);
  1058. return __gfn_to_pfn_memslot(slot, gfn, atomic, async, write_fault,
  1059. writable);
  1060. }
  1061. pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn)
  1062. {
  1063. return __gfn_to_pfn(kvm, gfn, true, NULL, true, NULL);
  1064. }
  1065. EXPORT_SYMBOL_GPL(gfn_to_pfn_atomic);
  1066. pfn_t gfn_to_pfn_async(struct kvm *kvm, gfn_t gfn, bool *async,
  1067. bool write_fault, bool *writable)
  1068. {
  1069. return __gfn_to_pfn(kvm, gfn, false, async, write_fault, writable);
  1070. }
  1071. EXPORT_SYMBOL_GPL(gfn_to_pfn_async);
  1072. pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
  1073. {
  1074. return __gfn_to_pfn(kvm, gfn, false, NULL, true, NULL);
  1075. }
  1076. EXPORT_SYMBOL_GPL(gfn_to_pfn);
  1077. pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
  1078. bool *writable)
  1079. {
  1080. return __gfn_to_pfn(kvm, gfn, false, NULL, write_fault, writable);
  1081. }
  1082. EXPORT_SYMBOL_GPL(gfn_to_pfn_prot);
  1083. pfn_t gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn)
  1084. {
  1085. return __gfn_to_pfn_memslot(slot, gfn, false, NULL, true, NULL);
  1086. }
  1087. pfn_t gfn_to_pfn_memslot_atomic(struct kvm_memory_slot *slot, gfn_t gfn)
  1088. {
  1089. return __gfn_to_pfn_memslot(slot, gfn, true, NULL, true, NULL);
  1090. }
  1091. EXPORT_SYMBOL_GPL(gfn_to_pfn_memslot_atomic);
  1092. int gfn_to_page_many_atomic(struct kvm *kvm, gfn_t gfn, struct page **pages,
  1093. int nr_pages)
  1094. {
  1095. unsigned long addr;
  1096. gfn_t entry;
  1097. addr = gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, &entry);
  1098. if (kvm_is_error_hva(addr))
  1099. return -1;
  1100. if (entry < nr_pages)
  1101. return 0;
  1102. return __get_user_pages_fast(addr, nr_pages, 1, pages);
  1103. }
  1104. EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic);
  1105. static struct page *kvm_pfn_to_page(pfn_t pfn)
  1106. {
  1107. if (is_error_noslot_pfn(pfn))
  1108. return KVM_ERR_PTR_BAD_PAGE;
  1109. if (kvm_is_mmio_pfn(pfn)) {
  1110. WARN_ON(1);
  1111. return KVM_ERR_PTR_BAD_PAGE;
  1112. }
  1113. return pfn_to_page(pfn);
  1114. }
  1115. struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
  1116. {
  1117. pfn_t pfn;
  1118. pfn = gfn_to_pfn(kvm, gfn);
  1119. return kvm_pfn_to_page(pfn);
  1120. }
  1121. EXPORT_SYMBOL_GPL(gfn_to_page);
  1122. void kvm_release_page_clean(struct page *page)
  1123. {
  1124. WARN_ON(is_error_page(page));
  1125. kvm_release_pfn_clean(page_to_pfn(page));
  1126. }
  1127. EXPORT_SYMBOL_GPL(kvm_release_page_clean);
  1128. void kvm_release_pfn_clean(pfn_t pfn)
  1129. {
  1130. if (!is_error_noslot_pfn(pfn) && !kvm_is_mmio_pfn(pfn))
  1131. put_page(pfn_to_page(pfn));
  1132. }
  1133. EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
  1134. void kvm_release_page_dirty(struct page *page)
  1135. {
  1136. WARN_ON(is_error_page(page));
  1137. kvm_release_pfn_dirty(page_to_pfn(page));
  1138. }
  1139. EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
  1140. void kvm_release_pfn_dirty(pfn_t pfn)
  1141. {
  1142. kvm_set_pfn_dirty(pfn);
  1143. kvm_release_pfn_clean(pfn);
  1144. }
  1145. EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
  1146. void kvm_set_page_dirty(struct page *page)
  1147. {
  1148. kvm_set_pfn_dirty(page_to_pfn(page));
  1149. }
  1150. EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
  1151. void kvm_set_pfn_dirty(pfn_t pfn)
  1152. {
  1153. if (!kvm_is_mmio_pfn(pfn)) {
  1154. struct page *page = pfn_to_page(pfn);
  1155. if (!PageReserved(page))
  1156. SetPageDirty(page);
  1157. }
  1158. }
  1159. EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
  1160. void kvm_set_pfn_accessed(pfn_t pfn)
  1161. {
  1162. if (!kvm_is_mmio_pfn(pfn))
  1163. mark_page_accessed(pfn_to_page(pfn));
  1164. }
  1165. EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
  1166. void kvm_get_pfn(pfn_t pfn)
  1167. {
  1168. if (!kvm_is_mmio_pfn(pfn))
  1169. get_page(pfn_to_page(pfn));
  1170. }
  1171. EXPORT_SYMBOL_GPL(kvm_get_pfn);
  1172. static int next_segment(unsigned long len, int offset)
  1173. {
  1174. if (len > PAGE_SIZE - offset)
  1175. return PAGE_SIZE - offset;
  1176. else
  1177. return len;
  1178. }
  1179. int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
  1180. int len)
  1181. {
  1182. int r;
  1183. unsigned long addr;
  1184. addr = gfn_to_hva_prot(kvm, gfn, NULL);
  1185. if (kvm_is_error_hva(addr))
  1186. return -EFAULT;
  1187. r = kvm_read_hva(data, (void __user *)addr + offset, len);
  1188. if (r)
  1189. return -EFAULT;
  1190. return 0;
  1191. }
  1192. EXPORT_SYMBOL_GPL(kvm_read_guest_page);
  1193. int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
  1194. {
  1195. gfn_t gfn = gpa >> PAGE_SHIFT;
  1196. int seg;
  1197. int offset = offset_in_page(gpa);
  1198. int ret;
  1199. while ((seg = next_segment(len, offset)) != 0) {
  1200. ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
  1201. if (ret < 0)
  1202. return ret;
  1203. offset = 0;
  1204. len -= seg;
  1205. data += seg;
  1206. ++gfn;
  1207. }
  1208. return 0;
  1209. }
  1210. EXPORT_SYMBOL_GPL(kvm_read_guest);
  1211. int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
  1212. unsigned long len)
  1213. {
  1214. int r;
  1215. unsigned long addr;
  1216. gfn_t gfn = gpa >> PAGE_SHIFT;
  1217. int offset = offset_in_page(gpa);
  1218. addr = gfn_to_hva_prot(kvm, gfn, NULL);
  1219. if (kvm_is_error_hva(addr))
  1220. return -EFAULT;
  1221. pagefault_disable();
  1222. r = kvm_read_hva_atomic(data, (void __user *)addr + offset, len);
  1223. pagefault_enable();
  1224. if (r)
  1225. return -EFAULT;
  1226. return 0;
  1227. }
  1228. EXPORT_SYMBOL(kvm_read_guest_atomic);
  1229. int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
  1230. int offset, int len)
  1231. {
  1232. int r;
  1233. unsigned long addr;
  1234. addr = gfn_to_hva(kvm, gfn);
  1235. if (kvm_is_error_hva(addr))
  1236. return -EFAULT;
  1237. r = __copy_to_user((void __user *)addr + offset, data, len);
  1238. if (r)
  1239. return -EFAULT;
  1240. mark_page_dirty(kvm, gfn);
  1241. return 0;
  1242. }
  1243. EXPORT_SYMBOL_GPL(kvm_write_guest_page);
  1244. int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
  1245. unsigned long len)
  1246. {
  1247. gfn_t gfn = gpa >> PAGE_SHIFT;
  1248. int seg;
  1249. int offset = offset_in_page(gpa);
  1250. int ret;
  1251. while ((seg = next_segment(len, offset)) != 0) {
  1252. ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
  1253. if (ret < 0)
  1254. return ret;
  1255. offset = 0;
  1256. len -= seg;
  1257. data += seg;
  1258. ++gfn;
  1259. }
  1260. return 0;
  1261. }
  1262. int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
  1263. gpa_t gpa, unsigned long len)
  1264. {
  1265. struct kvm_memslots *slots = kvm_memslots(kvm);
  1266. int offset = offset_in_page(gpa);
  1267. gfn_t start_gfn = gpa >> PAGE_SHIFT;
  1268. gfn_t end_gfn = (gpa + len - 1) >> PAGE_SHIFT;
  1269. gfn_t nr_pages_needed = end_gfn - start_gfn + 1;
  1270. gfn_t nr_pages_avail;
  1271. ghc->gpa = gpa;
  1272. ghc->generation = slots->generation;
  1273. ghc->len = len;
  1274. ghc->memslot = gfn_to_memslot(kvm, start_gfn);
  1275. ghc->hva = gfn_to_hva_many(ghc->memslot, start_gfn, &nr_pages_avail);
  1276. if (!kvm_is_error_hva(ghc->hva) && nr_pages_avail >= nr_pages_needed) {
  1277. ghc->hva += offset;
  1278. } else {
  1279. /*
  1280. * If the requested region crosses two memslots, we still
  1281. * verify that the entire region is valid here.
  1282. */
  1283. while (start_gfn <= end_gfn) {
  1284. ghc->memslot = gfn_to_memslot(kvm, start_gfn);
  1285. ghc->hva = gfn_to_hva_many(ghc->memslot, start_gfn,
  1286. &nr_pages_avail);
  1287. if (kvm_is_error_hva(ghc->hva))
  1288. return -EFAULT;
  1289. start_gfn += nr_pages_avail;
  1290. }
  1291. /* Use the slow path for cross page reads and writes. */
  1292. ghc->memslot = NULL;
  1293. }
  1294. return 0;
  1295. }
  1296. EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init);
  1297. int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
  1298. void *data, unsigned long len)
  1299. {
  1300. struct kvm_memslots *slots = kvm_memslots(kvm);
  1301. int r;
  1302. BUG_ON(len > ghc->len);
  1303. if (slots->generation != ghc->generation)
  1304. kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa, ghc->len);
  1305. if (unlikely(!ghc->memslot))
  1306. return kvm_write_guest(kvm, ghc->gpa, data, len);
  1307. if (kvm_is_error_hva(ghc->hva))
  1308. return -EFAULT;
  1309. r = __copy_to_user((void __user *)ghc->hva, data, len);
  1310. if (r)
  1311. return -EFAULT;
  1312. mark_page_dirty_in_slot(kvm, ghc->memslot, ghc->gpa >> PAGE_SHIFT);
  1313. return 0;
  1314. }
  1315. EXPORT_SYMBOL_GPL(kvm_write_guest_cached);
  1316. int kvm_read_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
  1317. void *data, unsigned long len)
  1318. {
  1319. struct kvm_memslots *slots = kvm_memslots(kvm);
  1320. int r;
  1321. BUG_ON(len > ghc->len);
  1322. if (slots->generation != ghc->generation)
  1323. kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa, ghc->len);
  1324. if (unlikely(!ghc->memslot))
  1325. return kvm_read_guest(kvm, ghc->gpa, data, len);
  1326. if (kvm_is_error_hva(ghc->hva))
  1327. return -EFAULT;
  1328. r = __copy_from_user(data, (void __user *)ghc->hva, len);
  1329. if (r)
  1330. return -EFAULT;
  1331. return 0;
  1332. }
  1333. EXPORT_SYMBOL_GPL(kvm_read_guest_cached);
  1334. int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
  1335. {
  1336. return kvm_write_guest_page(kvm, gfn, (const void *) empty_zero_page,
  1337. offset, len);
  1338. }
  1339. EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
  1340. int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
  1341. {
  1342. gfn_t gfn = gpa >> PAGE_SHIFT;
  1343. int seg;
  1344. int offset = offset_in_page(gpa);
  1345. int ret;
  1346. while ((seg = next_segment(len, offset)) != 0) {
  1347. ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
  1348. if (ret < 0)
  1349. return ret;
  1350. offset = 0;
  1351. len -= seg;
  1352. ++gfn;
  1353. }
  1354. return 0;
  1355. }
  1356. EXPORT_SYMBOL_GPL(kvm_clear_guest);
  1357. void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot,
  1358. gfn_t gfn)
  1359. {
  1360. if (memslot && memslot->dirty_bitmap) {
  1361. unsigned long rel_gfn = gfn - memslot->base_gfn;
  1362. set_bit_le(rel_gfn, memslot->dirty_bitmap);
  1363. }
  1364. }
  1365. void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
  1366. {
  1367. struct kvm_memory_slot *memslot;
  1368. memslot = gfn_to_memslot(kvm, gfn);
  1369. mark_page_dirty_in_slot(kvm, memslot, gfn);
  1370. }
  1371. /*
  1372. * The vCPU has executed a HLT instruction with in-kernel mode enabled.
  1373. */
  1374. void kvm_vcpu_block(struct kvm_vcpu *vcpu)
  1375. {
  1376. DEFINE_WAIT(wait);
  1377. for (;;) {
  1378. prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
  1379. if (kvm_arch_vcpu_runnable(vcpu)) {
  1380. kvm_make_request(KVM_REQ_UNHALT, vcpu);
  1381. break;
  1382. }
  1383. if (kvm_cpu_has_pending_timer(vcpu))
  1384. break;
  1385. if (signal_pending(current))
  1386. break;
  1387. schedule();
  1388. }
  1389. finish_wait(&vcpu->wq, &wait);
  1390. }
  1391. #ifndef CONFIG_S390
  1392. /*
  1393. * Kick a sleeping VCPU, or a guest VCPU in guest mode, into host kernel mode.
  1394. */
  1395. void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
  1396. {
  1397. int me;
  1398. int cpu = vcpu->cpu;
  1399. wait_queue_head_t *wqp;
  1400. wqp = kvm_arch_vcpu_wq(vcpu);
  1401. if (waitqueue_active(wqp)) {
  1402. wake_up_interruptible(wqp);
  1403. ++vcpu->stat.halt_wakeup;
  1404. }
  1405. me = get_cpu();
  1406. if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
  1407. if (kvm_arch_vcpu_should_kick(vcpu))
  1408. smp_send_reschedule(cpu);
  1409. put_cpu();
  1410. }
  1411. EXPORT_SYMBOL_GPL(kvm_vcpu_kick);
  1412. #endif /* !CONFIG_S390 */
  1413. void kvm_resched(struct kvm_vcpu *vcpu)
  1414. {
  1415. if (!need_resched())
  1416. return;
  1417. cond_resched();
  1418. }
  1419. EXPORT_SYMBOL_GPL(kvm_resched);
  1420. bool kvm_vcpu_yield_to(struct kvm_vcpu *target)
  1421. {
  1422. struct pid *pid;
  1423. struct task_struct *task = NULL;
  1424. bool ret = false;
  1425. rcu_read_lock();
  1426. pid = rcu_dereference(target->pid);
  1427. if (pid)
  1428. task = get_pid_task(target->pid, PIDTYPE_PID);
  1429. rcu_read_unlock();
  1430. if (!task)
  1431. return ret;
  1432. if (task->flags & PF_VCPU) {
  1433. put_task_struct(task);
  1434. return ret;
  1435. }
  1436. ret = yield_to(task, 1);
  1437. put_task_struct(task);
  1438. return ret;
  1439. }
  1440. EXPORT_SYMBOL_GPL(kvm_vcpu_yield_to);
  1441. #ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
  1442. /*
  1443. * Helper that checks whether a VCPU is eligible for directed yield.
  1444. * Most eligible candidate to yield is decided by following heuristics:
  1445. *
  1446. * (a) VCPU which has not done pl-exit or cpu relax intercepted recently
  1447. * (preempted lock holder), indicated by @in_spin_loop.
  1448. * Set at the beiginning and cleared at the end of interception/PLE handler.
  1449. *
  1450. * (b) VCPU which has done pl-exit/ cpu relax intercepted but did not get
  1451. * chance last time (mostly it has become eligible now since we have probably
  1452. * yielded to lockholder in last iteration. This is done by toggling
  1453. * @dy_eligible each time a VCPU checked for eligibility.)
  1454. *
  1455. * Yielding to a recently pl-exited/cpu relax intercepted VCPU before yielding
  1456. * to preempted lock-holder could result in wrong VCPU selection and CPU
  1457. * burning. Giving priority for a potential lock-holder increases lock
  1458. * progress.
  1459. *
  1460. * Since algorithm is based on heuristics, accessing another VCPU data without
  1461. * locking does not harm. It may result in trying to yield to same VCPU, fail
  1462. * and continue with next VCPU and so on.
  1463. */
  1464. bool kvm_vcpu_eligible_for_directed_yield(struct kvm_vcpu *vcpu)
  1465. {
  1466. bool eligible;
  1467. eligible = !vcpu->spin_loop.in_spin_loop ||
  1468. (vcpu->spin_loop.in_spin_loop &&
  1469. vcpu->spin_loop.dy_eligible);
  1470. if (vcpu->spin_loop.in_spin_loop)
  1471. kvm_vcpu_set_dy_eligible(vcpu, !vcpu->spin_loop.dy_eligible);
  1472. return eligible;
  1473. }
  1474. #endif
  1475. void kvm_vcpu_on_spin(struct kvm_vcpu *me)
  1476. {
  1477. struct kvm *kvm = me->kvm;
  1478. struct kvm_vcpu *vcpu;
  1479. int last_boosted_vcpu = me->kvm->last_boosted_vcpu;
  1480. int yielded = 0;
  1481. int try = 3;
  1482. int pass;
  1483. int i;
  1484. kvm_vcpu_set_in_spin_loop(me, true);
  1485. /*
  1486. * We boost the priority of a VCPU that is runnable but not
  1487. * currently running, because it got preempted by something
  1488. * else and called schedule in __vcpu_run. Hopefully that
  1489. * VCPU is holding the lock that we need and will release it.
  1490. * We approximate round-robin by starting at the last boosted VCPU.
  1491. */
  1492. for (pass = 0; pass < 2 && !yielded && try; pass++) {
  1493. kvm_for_each_vcpu(i, vcpu, kvm) {
  1494. if (!pass && i <= last_boosted_vcpu) {
  1495. i = last_boosted_vcpu;
  1496. continue;
  1497. } else if (pass && i > last_boosted_vcpu)
  1498. break;
  1499. if (!ACCESS_ONCE(vcpu->preempted))
  1500. continue;
  1501. if (vcpu == me)
  1502. continue;
  1503. if (waitqueue_active(&vcpu->wq))
  1504. continue;
  1505. if (!kvm_vcpu_eligible_for_directed_yield(vcpu))
  1506. continue;
  1507. yielded = kvm_vcpu_yield_to(vcpu);
  1508. if (yielded > 0) {
  1509. kvm->last_boosted_vcpu = i;
  1510. break;
  1511. } else if (yielded < 0) {
  1512. try--;
  1513. if (!try)
  1514. break;
  1515. }
  1516. }
  1517. }
  1518. kvm_vcpu_set_in_spin_loop(me, false);
  1519. /* Ensure vcpu is not eligible during next spinloop */
  1520. kvm_vcpu_set_dy_eligible(me, false);
  1521. }
  1522. EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
  1523. static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1524. {
  1525. struct kvm_vcpu *vcpu = vma->vm_file->private_data;
  1526. struct page *page;
  1527. if (vmf->pgoff == 0)
  1528. page = virt_to_page(vcpu->run);
  1529. #ifdef CONFIG_X86
  1530. else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
  1531. page = virt_to_page(vcpu->arch.pio_data);
  1532. #endif
  1533. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  1534. else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
  1535. page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
  1536. #endif
  1537. else
  1538. return kvm_arch_vcpu_fault(vcpu, vmf);
  1539. get_page(page);
  1540. vmf->page = page;
  1541. return 0;
  1542. }
  1543. static const struct vm_operations_struct kvm_vcpu_vm_ops = {
  1544. .fault = kvm_vcpu_fault,
  1545. };
  1546. static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
  1547. {
  1548. vma->vm_ops = &kvm_vcpu_vm_ops;
  1549. return 0;
  1550. }
  1551. static int kvm_vcpu_release(struct inode *inode, struct file *filp)
  1552. {
  1553. struct kvm_vcpu *vcpu = filp->private_data;
  1554. kvm_put_kvm(vcpu->kvm);
  1555. return 0;
  1556. }
  1557. static struct file_operations kvm_vcpu_fops = {
  1558. .release = kvm_vcpu_release,
  1559. .unlocked_ioctl = kvm_vcpu_ioctl,
  1560. #ifdef CONFIG_COMPAT
  1561. .compat_ioctl = kvm_vcpu_compat_ioctl,
  1562. #endif
  1563. .mmap = kvm_vcpu_mmap,
  1564. .llseek = noop_llseek,
  1565. };
  1566. /*
  1567. * Allocates an inode for the vcpu.
  1568. */
  1569. static int create_vcpu_fd(struct kvm_vcpu *vcpu)
  1570. {
  1571. return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR | O_CLOEXEC);
  1572. }
  1573. /*
  1574. * Creates some virtual cpus. Good luck creating more than one.
  1575. */
  1576. static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
  1577. {
  1578. int r;
  1579. struct kvm_vcpu *vcpu, *v;
  1580. vcpu = kvm_arch_vcpu_create(kvm, id);
  1581. if (IS_ERR(vcpu))
  1582. return PTR_ERR(vcpu);
  1583. preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
  1584. r = kvm_arch_vcpu_setup(vcpu);
  1585. if (r)
  1586. goto vcpu_destroy;
  1587. mutex_lock(&kvm->lock);
  1588. if (!kvm_vcpu_compatible(vcpu)) {
  1589. r = -EINVAL;
  1590. goto unlock_vcpu_destroy;
  1591. }
  1592. if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
  1593. r = -EINVAL;
  1594. goto unlock_vcpu_destroy;
  1595. }
  1596. kvm_for_each_vcpu(r, v, kvm)
  1597. if (v->vcpu_id == id) {
  1598. r = -EEXIST;
  1599. goto unlock_vcpu_destroy;
  1600. }
  1601. BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
  1602. /* Now it's all set up, let userspace reach it */
  1603. kvm_get_kvm(kvm);
  1604. r = create_vcpu_fd(vcpu);
  1605. if (r < 0) {
  1606. kvm_put_kvm(kvm);
  1607. goto unlock_vcpu_destroy;
  1608. }
  1609. kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
  1610. smp_wmb();
  1611. atomic_inc(&kvm->online_vcpus);
  1612. mutex_unlock(&kvm->lock);
  1613. kvm_arch_vcpu_postcreate(vcpu);
  1614. return r;
  1615. unlock_vcpu_destroy:
  1616. mutex_unlock(&kvm->lock);
  1617. vcpu_destroy:
  1618. kvm_arch_vcpu_destroy(vcpu);
  1619. return r;
  1620. }
  1621. static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
  1622. {
  1623. if (sigset) {
  1624. sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
  1625. vcpu->sigset_active = 1;
  1626. vcpu->sigset = *sigset;
  1627. } else
  1628. vcpu->sigset_active = 0;
  1629. return 0;
  1630. }
  1631. static long kvm_vcpu_ioctl(struct file *filp,
  1632. unsigned int ioctl, unsigned long arg)
  1633. {
  1634. struct kvm_vcpu *vcpu = filp->private_data;
  1635. void __user *argp = (void __user *)arg;
  1636. int r;
  1637. struct kvm_fpu *fpu = NULL;
  1638. struct kvm_sregs *kvm_sregs = NULL;
  1639. if (vcpu->kvm->mm != current->mm)
  1640. return -EIO;
  1641. #if defined(CONFIG_S390) || defined(CONFIG_PPC) || defined(CONFIG_MIPS)
  1642. /*
  1643. * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
  1644. * so vcpu_load() would break it.
  1645. */
  1646. if (ioctl == KVM_S390_INTERRUPT || ioctl == KVM_INTERRUPT)
  1647. return kvm_arch_vcpu_ioctl(filp, ioctl, arg);
  1648. #endif
  1649. r = vcpu_load(vcpu);
  1650. if (r)
  1651. return r;
  1652. switch (ioctl) {
  1653. case KVM_RUN:
  1654. r = -EINVAL;
  1655. if (arg)
  1656. goto out;
  1657. r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
  1658. trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
  1659. break;
  1660. case KVM_GET_REGS: {
  1661. struct kvm_regs *kvm_regs;
  1662. r = -ENOMEM;
  1663. kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
  1664. if (!kvm_regs)
  1665. goto out;
  1666. r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
  1667. if (r)
  1668. goto out_free1;
  1669. r = -EFAULT;
  1670. if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
  1671. goto out_free1;
  1672. r = 0;
  1673. out_free1:
  1674. kfree(kvm_regs);
  1675. break;
  1676. }
  1677. case KVM_SET_REGS: {
  1678. struct kvm_regs *kvm_regs;
  1679. r = -ENOMEM;
  1680. kvm_regs = memdup_user(argp, sizeof(*kvm_regs));
  1681. if (IS_ERR(kvm_regs)) {
  1682. r = PTR_ERR(kvm_regs);
  1683. goto out;
  1684. }
  1685. r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
  1686. kfree(kvm_regs);
  1687. break;
  1688. }
  1689. case KVM_GET_SREGS: {
  1690. kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
  1691. r = -ENOMEM;
  1692. if (!kvm_sregs)
  1693. goto out;
  1694. r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
  1695. if (r)
  1696. goto out;
  1697. r = -EFAULT;
  1698. if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
  1699. goto out;
  1700. r = 0;
  1701. break;
  1702. }
  1703. case KVM_SET_SREGS: {
  1704. kvm_sregs = memdup_user(argp, sizeof(*kvm_sregs));
  1705. if (IS_ERR(kvm_sregs)) {
  1706. r = PTR_ERR(kvm_sregs);
  1707. kvm_sregs = NULL;
  1708. goto out;
  1709. }
  1710. r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
  1711. break;
  1712. }
  1713. case KVM_GET_MP_STATE: {
  1714. struct kvm_mp_state mp_state;
  1715. r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
  1716. if (r)
  1717. goto out;
  1718. r = -EFAULT;
  1719. if (copy_to_user(argp, &mp_state, sizeof mp_state))
  1720. goto out;
  1721. r = 0;
  1722. break;
  1723. }
  1724. case KVM_SET_MP_STATE: {
  1725. struct kvm_mp_state mp_state;
  1726. r = -EFAULT;
  1727. if (copy_from_user(&mp_state, argp, sizeof mp_state))
  1728. goto out;
  1729. r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
  1730. break;
  1731. }
  1732. case KVM_TRANSLATE: {
  1733. struct kvm_translation tr;
  1734. r = -EFAULT;
  1735. if (copy_from_user(&tr, argp, sizeof tr))
  1736. goto out;
  1737. r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
  1738. if (r)
  1739. goto out;
  1740. r = -EFAULT;
  1741. if (copy_to_user(argp, &tr, sizeof tr))
  1742. goto out;
  1743. r = 0;
  1744. break;
  1745. }
  1746. case KVM_SET_GUEST_DEBUG: {
  1747. struct kvm_guest_debug dbg;
  1748. r = -EFAULT;
  1749. if (copy_from_user(&dbg, argp, sizeof dbg))
  1750. goto out;
  1751. r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
  1752. break;
  1753. }
  1754. case KVM_SET_SIGNAL_MASK: {
  1755. struct kvm_signal_mask __user *sigmask_arg = argp;
  1756. struct kvm_signal_mask kvm_sigmask;
  1757. sigset_t sigset, *p;
  1758. p = NULL;
  1759. if (argp) {
  1760. r = -EFAULT;
  1761. if (copy_from_user(&kvm_sigmask, argp,
  1762. sizeof kvm_sigmask))
  1763. goto out;
  1764. r = -EINVAL;
  1765. if (kvm_sigmask.len != sizeof sigset)
  1766. goto out;
  1767. r = -EFAULT;
  1768. if (copy_from_user(&sigset, sigmask_arg->sigset,
  1769. sizeof sigset))
  1770. goto out;
  1771. p = &sigset;
  1772. }
  1773. r = kvm_vcpu_ioctl_set_sigmask(vcpu, p);
  1774. break;
  1775. }
  1776. case KVM_GET_FPU: {
  1777. fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
  1778. r = -ENOMEM;
  1779. if (!fpu)
  1780. goto out;
  1781. r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
  1782. if (r)
  1783. goto out;
  1784. r = -EFAULT;
  1785. if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
  1786. goto out;
  1787. r = 0;
  1788. break;
  1789. }
  1790. case KVM_SET_FPU: {
  1791. fpu = memdup_user(argp, sizeof(*fpu));
  1792. if (IS_ERR(fpu)) {
  1793. r = PTR_ERR(fpu);
  1794. fpu = NULL;
  1795. goto out;
  1796. }
  1797. r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
  1798. break;
  1799. }
  1800. default:
  1801. r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
  1802. }
  1803. out:
  1804. vcpu_put(vcpu);
  1805. kfree(fpu);
  1806. kfree(kvm_sregs);
  1807. return r;
  1808. }
  1809. #ifdef CONFIG_COMPAT
  1810. static long kvm_vcpu_compat_ioctl(struct file *filp,
  1811. unsigned int ioctl, unsigned long arg)
  1812. {
  1813. struct kvm_vcpu *vcpu = filp->private_data;
  1814. void __user *argp = compat_ptr(arg);
  1815. int r;
  1816. if (vcpu->kvm->mm != current->mm)
  1817. return -EIO;
  1818. switch (ioctl) {
  1819. case KVM_SET_SIGNAL_MASK: {
  1820. struct kvm_signal_mask __user *sigmask_arg = argp;
  1821. struct kvm_signal_mask kvm_sigmask;
  1822. compat_sigset_t csigset;
  1823. sigset_t sigset;
  1824. if (argp) {
  1825. r = -EFAULT;
  1826. if (copy_from_user(&kvm_sigmask, argp,
  1827. sizeof kvm_sigmask))
  1828. goto out;
  1829. r = -EINVAL;
  1830. if (kvm_sigmask.len != sizeof csigset)
  1831. goto out;
  1832. r = -EFAULT;
  1833. if (copy_from_user(&csigset, sigmask_arg->sigset,
  1834. sizeof csigset))
  1835. goto out;
  1836. sigset_from_compat(&sigset, &csigset);
  1837. r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
  1838. } else
  1839. r = kvm_vcpu_ioctl_set_sigmask(vcpu, NULL);
  1840. break;
  1841. }
  1842. default:
  1843. r = kvm_vcpu_ioctl(filp, ioctl, arg);
  1844. }
  1845. out:
  1846. return r;
  1847. }
  1848. #endif
  1849. static int kvm_device_ioctl_attr(struct kvm_device *dev,
  1850. int (*accessor)(struct kvm_device *dev,
  1851. struct kvm_device_attr *attr),
  1852. unsigned long arg)
  1853. {
  1854. struct kvm_device_attr attr;
  1855. if (!accessor)
  1856. return -EPERM;
  1857. if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
  1858. return -EFAULT;
  1859. return accessor(dev, &attr);
  1860. }
  1861. static long kvm_device_ioctl(struct file *filp, unsigned int ioctl,
  1862. unsigned long arg)
  1863. {
  1864. struct kvm_device *dev = filp->private_data;
  1865. switch (ioctl) {
  1866. case KVM_SET_DEVICE_ATTR:
  1867. return kvm_device_ioctl_attr(dev, dev->ops->set_attr, arg);
  1868. case KVM_GET_DEVICE_ATTR:
  1869. return kvm_device_ioctl_attr(dev, dev->ops->get_attr, arg);
  1870. case KVM_HAS_DEVICE_ATTR:
  1871. return kvm_device_ioctl_attr(dev, dev->ops->has_attr, arg);
  1872. default:
  1873. if (dev->ops->ioctl)
  1874. return dev->ops->ioctl(dev, ioctl, arg);
  1875. return -ENOTTY;
  1876. }
  1877. }
  1878. static int kvm_device_release(struct inode *inode, struct file *filp)
  1879. {
  1880. struct kvm_device *dev = filp->private_data;
  1881. struct kvm *kvm = dev->kvm;
  1882. kvm_put_kvm(kvm);
  1883. return 0;
  1884. }
  1885. static const struct file_operations kvm_device_fops = {
  1886. .unlocked_ioctl = kvm_device_ioctl,
  1887. #ifdef CONFIG_COMPAT
  1888. .compat_ioctl = kvm_device_ioctl,
  1889. #endif
  1890. .release = kvm_device_release,
  1891. };
  1892. struct kvm_device *kvm_device_from_filp(struct file *filp)
  1893. {
  1894. if (filp->f_op != &kvm_device_fops)
  1895. return NULL;
  1896. return filp->private_data;
  1897. }
  1898. static int kvm_ioctl_create_device(struct kvm *kvm,
  1899. struct kvm_create_device *cd)
  1900. {
  1901. struct kvm_device_ops *ops = NULL;
  1902. struct kvm_device *dev;
  1903. bool test = cd->flags & KVM_CREATE_DEVICE_TEST;
  1904. int ret;
  1905. switch (cd->type) {
  1906. #ifdef CONFIG_KVM_MPIC
  1907. case KVM_DEV_TYPE_FSL_MPIC_20:
  1908. case KVM_DEV_TYPE_FSL_MPIC_42:
  1909. ops = &kvm_mpic_ops;
  1910. break;
  1911. #endif
  1912. #ifdef CONFIG_KVM_XICS
  1913. case KVM_DEV_TYPE_XICS:
  1914. ops = &kvm_xics_ops;
  1915. break;
  1916. #endif
  1917. default:
  1918. return -ENODEV;
  1919. }
  1920. if (test)
  1921. return 0;
  1922. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1923. if (!dev)
  1924. return -ENOMEM;
  1925. dev->ops = ops;
  1926. dev->kvm = kvm;
  1927. ret = ops->create(dev, cd->type);
  1928. if (ret < 0) {
  1929. kfree(dev);
  1930. return ret;
  1931. }
  1932. ret = anon_inode_getfd(ops->name, &kvm_device_fops, dev, O_RDWR | O_CLOEXEC);
  1933. if (ret < 0) {
  1934. ops->destroy(dev);
  1935. return ret;
  1936. }
  1937. list_add(&dev->vm_node, &kvm->devices);
  1938. kvm_get_kvm(kvm);
  1939. cd->fd = ret;
  1940. return 0;
  1941. }
  1942. static long kvm_vm_ioctl(struct file *filp,
  1943. unsigned int ioctl, unsigned long arg)
  1944. {
  1945. struct kvm *kvm = filp->private_data;
  1946. void __user *argp = (void __user *)arg;
  1947. int r;
  1948. if (kvm->mm != current->mm)
  1949. return -EIO;
  1950. switch (ioctl) {
  1951. case KVM_CREATE_VCPU:
  1952. r = kvm_vm_ioctl_create_vcpu(kvm, arg);
  1953. break;
  1954. case KVM_SET_USER_MEMORY_REGION: {
  1955. struct kvm_userspace_memory_region kvm_userspace_mem;
  1956. r = -EFAULT;
  1957. if (copy_from_user(&kvm_userspace_mem, argp,
  1958. sizeof kvm_userspace_mem))
  1959. goto out;
  1960. r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem);
  1961. break;
  1962. }
  1963. case KVM_GET_DIRTY_LOG: {
  1964. struct kvm_dirty_log log;
  1965. r = -EFAULT;
  1966. if (copy_from_user(&log, argp, sizeof log))
  1967. goto out;
  1968. r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
  1969. break;
  1970. }
  1971. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  1972. case KVM_REGISTER_COALESCED_MMIO: {
  1973. struct kvm_coalesced_mmio_zone zone;
  1974. r = -EFAULT;
  1975. if (copy_from_user(&zone, argp, sizeof zone))
  1976. goto out;
  1977. r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
  1978. break;
  1979. }
  1980. case KVM_UNREGISTER_COALESCED_MMIO: {
  1981. struct kvm_coalesced_mmio_zone zone;
  1982. r = -EFAULT;
  1983. if (copy_from_user(&zone, argp, sizeof zone))
  1984. goto out;
  1985. r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
  1986. break;
  1987. }
  1988. #endif
  1989. case KVM_IRQFD: {
  1990. struct kvm_irqfd data;
  1991. r = -EFAULT;
  1992. if (copy_from_user(&data, argp, sizeof data))
  1993. goto out;
  1994. r = kvm_irqfd(kvm, &data);
  1995. break;
  1996. }
  1997. case KVM_IOEVENTFD: {
  1998. struct kvm_ioeventfd data;
  1999. r = -EFAULT;
  2000. if (copy_from_user(&data, argp, sizeof data))
  2001. goto out;
  2002. r = kvm_ioeventfd(kvm, &data);
  2003. break;
  2004. }
  2005. #ifdef CONFIG_KVM_APIC_ARCHITECTURE
  2006. case KVM_SET_BOOT_CPU_ID:
  2007. r = 0;
  2008. mutex_lock(&kvm->lock);
  2009. if (atomic_read(&kvm->online_vcpus) != 0)
  2010. r = -EBUSY;
  2011. else
  2012. kvm->bsp_vcpu_id = arg;
  2013. mutex_unlock(&kvm->lock);
  2014. break;
  2015. #endif
  2016. #ifdef CONFIG_HAVE_KVM_MSI
  2017. case KVM_SIGNAL_MSI: {
  2018. struct kvm_msi msi;
  2019. r = -EFAULT;
  2020. if (copy_from_user(&msi, argp, sizeof msi))
  2021. goto out;
  2022. r = kvm_send_userspace_msi(kvm, &msi);
  2023. break;
  2024. }
  2025. #endif
  2026. #ifdef __KVM_HAVE_IRQ_LINE
  2027. case KVM_IRQ_LINE_STATUS:
  2028. case KVM_IRQ_LINE: {
  2029. struct kvm_irq_level irq_event;
  2030. r = -EFAULT;
  2031. if (copy_from_user(&irq_event, argp, sizeof irq_event))
  2032. goto out;
  2033. r = kvm_vm_ioctl_irq_line(kvm, &irq_event,
  2034. ioctl == KVM_IRQ_LINE_STATUS);
  2035. if (r)
  2036. goto out;
  2037. r = -EFAULT;
  2038. if (ioctl == KVM_IRQ_LINE_STATUS) {
  2039. if (copy_to_user(argp, &irq_event, sizeof irq_event))
  2040. goto out;
  2041. }
  2042. r = 0;
  2043. break;
  2044. }
  2045. #endif
  2046. #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING
  2047. case KVM_SET_GSI_ROUTING: {
  2048. struct kvm_irq_routing routing;
  2049. struct kvm_irq_routing __user *urouting;
  2050. struct kvm_irq_routing_entry *entries;
  2051. r = -EFAULT;
  2052. if (copy_from_user(&routing, argp, sizeof(routing)))
  2053. goto out;
  2054. r = -EINVAL;
  2055. if (routing.nr >= KVM_MAX_IRQ_ROUTES)
  2056. goto out;
  2057. if (routing.flags)
  2058. goto out;
  2059. r = -ENOMEM;
  2060. entries = vmalloc(routing.nr * sizeof(*entries));
  2061. if (!entries)
  2062. goto out;
  2063. r = -EFAULT;
  2064. urouting = argp;
  2065. if (copy_from_user(entries, urouting->entries,
  2066. routing.nr * sizeof(*entries)))
  2067. goto out_free_irq_routing;
  2068. r = kvm_set_irq_routing(kvm, entries, routing.nr,
  2069. routing.flags);
  2070. out_free_irq_routing:
  2071. vfree(entries);
  2072. break;
  2073. }
  2074. #endif /* CONFIG_HAVE_KVM_IRQ_ROUTING */
  2075. case KVM_CREATE_DEVICE: {
  2076. struct kvm_create_device cd;
  2077. r = -EFAULT;
  2078. if (copy_from_user(&cd, argp, sizeof(cd)))
  2079. goto out;
  2080. r = kvm_ioctl_create_device(kvm, &cd);
  2081. if (r)
  2082. goto out;
  2083. r = -EFAULT;
  2084. if (copy_to_user(argp, &cd, sizeof(cd)))
  2085. goto out;
  2086. r = 0;
  2087. break;
  2088. }
  2089. default:
  2090. r = kvm_arch_vm_ioctl(filp, ioctl, arg);
  2091. if (r == -ENOTTY)
  2092. r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
  2093. }
  2094. out:
  2095. return r;
  2096. }
  2097. #ifdef CONFIG_COMPAT
  2098. struct compat_kvm_dirty_log {
  2099. __u32 slot;
  2100. __u32 padding1;
  2101. union {
  2102. compat_uptr_t dirty_bitmap; /* one bit per page */
  2103. __u64 padding2;
  2104. };
  2105. };
  2106. static long kvm_vm_compat_ioctl(struct file *filp,
  2107. unsigned int ioctl, unsigned long arg)
  2108. {
  2109. struct kvm *kvm = filp->private_data;
  2110. int r;
  2111. if (kvm->mm != current->mm)
  2112. return -EIO;
  2113. switch (ioctl) {
  2114. case KVM_GET_DIRTY_LOG: {
  2115. struct compat_kvm_dirty_log compat_log;
  2116. struct kvm_dirty_log log;
  2117. r = -EFAULT;
  2118. if (copy_from_user(&compat_log, (void __user *)arg,
  2119. sizeof(compat_log)))
  2120. goto out;
  2121. log.slot = compat_log.slot;
  2122. log.padding1 = compat_log.padding1;
  2123. log.padding2 = compat_log.padding2;
  2124. log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
  2125. r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
  2126. break;
  2127. }
  2128. default:
  2129. r = kvm_vm_ioctl(filp, ioctl, arg);
  2130. }
  2131. out:
  2132. return r;
  2133. }
  2134. #endif
  2135. static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  2136. {
  2137. struct page *page[1];
  2138. unsigned long addr;
  2139. int npages;
  2140. gfn_t gfn = vmf->pgoff;
  2141. struct kvm *kvm = vma->vm_file->private_data;
  2142. addr = gfn_to_hva(kvm, gfn);
  2143. if (kvm_is_error_hva(addr))
  2144. return VM_FAULT_SIGBUS;
  2145. npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
  2146. NULL);
  2147. if (unlikely(npages != 1))
  2148. return VM_FAULT_SIGBUS;
  2149. vmf->page = page[0];
  2150. return 0;
  2151. }
  2152. static const struct vm_operations_struct kvm_vm_vm_ops = {
  2153. .fault = kvm_vm_fault,
  2154. };
  2155. static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
  2156. {
  2157. vma->vm_ops = &kvm_vm_vm_ops;
  2158. return 0;
  2159. }
  2160. static struct file_operations kvm_vm_fops = {
  2161. .release = kvm_vm_release,
  2162. .unlocked_ioctl = kvm_vm_ioctl,
  2163. #ifdef CONFIG_COMPAT
  2164. .compat_ioctl = kvm_vm_compat_ioctl,
  2165. #endif
  2166. .mmap = kvm_vm_mmap,
  2167. .llseek = noop_llseek,
  2168. };
  2169. static int kvm_dev_ioctl_create_vm(unsigned long type)
  2170. {
  2171. int r;
  2172. struct kvm *kvm;
  2173. kvm = kvm_create_vm(type);
  2174. if (IS_ERR(kvm))
  2175. return PTR_ERR(kvm);
  2176. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  2177. r = kvm_coalesced_mmio_init(kvm);
  2178. if (r < 0) {
  2179. kvm_put_kvm(kvm);
  2180. return r;
  2181. }
  2182. #endif
  2183. r = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR | O_CLOEXEC);
  2184. if (r < 0)
  2185. kvm_put_kvm(kvm);
  2186. return r;
  2187. }
  2188. static long kvm_dev_ioctl_check_extension_generic(long arg)
  2189. {
  2190. switch (arg) {
  2191. case KVM_CAP_USER_MEMORY:
  2192. case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
  2193. case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
  2194. #ifdef CONFIG_KVM_APIC_ARCHITECTURE
  2195. case KVM_CAP_SET_BOOT_CPU_ID:
  2196. #endif
  2197. case KVM_CAP_INTERNAL_ERROR_DATA:
  2198. #ifdef CONFIG_HAVE_KVM_MSI
  2199. case KVM_CAP_SIGNAL_MSI:
  2200. #endif
  2201. #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING
  2202. case KVM_CAP_IRQFD_RESAMPLE:
  2203. #endif
  2204. return 1;
  2205. #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING
  2206. case KVM_CAP_IRQ_ROUTING:
  2207. return KVM_MAX_IRQ_ROUTES;
  2208. #endif
  2209. default:
  2210. break;
  2211. }
  2212. return kvm_dev_ioctl_check_extension(arg);
  2213. }
  2214. static long kvm_dev_ioctl(struct file *filp,
  2215. unsigned int ioctl, unsigned long arg)
  2216. {
  2217. long r = -EINVAL;
  2218. switch (ioctl) {
  2219. case KVM_GET_API_VERSION:
  2220. r = -EINVAL;
  2221. if (arg)
  2222. goto out;
  2223. r = KVM_API_VERSION;
  2224. break;
  2225. case KVM_CREATE_VM:
  2226. r = kvm_dev_ioctl_create_vm(arg);
  2227. break;
  2228. case KVM_CHECK_EXTENSION:
  2229. r = kvm_dev_ioctl_check_extension_generic(arg);
  2230. break;
  2231. case KVM_GET_VCPU_MMAP_SIZE:
  2232. r = -EINVAL;
  2233. if (arg)
  2234. goto out;
  2235. r = PAGE_SIZE; /* struct kvm_run */
  2236. #ifdef CONFIG_X86
  2237. r += PAGE_SIZE; /* pio data page */
  2238. #endif
  2239. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  2240. r += PAGE_SIZE; /* coalesced mmio ring page */
  2241. #endif
  2242. break;
  2243. case KVM_TRACE_ENABLE:
  2244. case KVM_TRACE_PAUSE:
  2245. case KVM_TRACE_DISABLE:
  2246. r = -EOPNOTSUPP;
  2247. break;
  2248. default:
  2249. return kvm_arch_dev_ioctl(filp, ioctl, arg);
  2250. }
  2251. out:
  2252. return r;
  2253. }
  2254. static struct file_operations kvm_chardev_ops = {
  2255. .unlocked_ioctl = kvm_dev_ioctl,
  2256. .compat_ioctl = kvm_dev_ioctl,
  2257. .llseek = noop_llseek,
  2258. };
  2259. static struct miscdevice kvm_dev = {
  2260. KVM_MINOR,
  2261. "kvm",
  2262. &kvm_chardev_ops,
  2263. };
  2264. static void hardware_enable_nolock(void *junk)
  2265. {
  2266. int cpu = raw_smp_processor_id();
  2267. int r;
  2268. if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
  2269. return;
  2270. cpumask_set_cpu(cpu, cpus_hardware_enabled);
  2271. r = kvm_arch_hardware_enable(NULL);
  2272. if (r) {
  2273. cpumask_clear_cpu(cpu, cpus_hardware_enabled);
  2274. atomic_inc(&hardware_enable_failed);
  2275. printk(KERN_INFO "kvm: enabling virtualization on "
  2276. "CPU%d failed\n", cpu);
  2277. }
  2278. }
  2279. static void hardware_enable(void)
  2280. {
  2281. raw_spin_lock(&kvm_count_lock);
  2282. if (kvm_usage_count)
  2283. hardware_enable_nolock(NULL);
  2284. raw_spin_unlock(&kvm_count_lock);
  2285. }
  2286. static void hardware_disable_nolock(void *junk)
  2287. {
  2288. int cpu = raw_smp_processor_id();
  2289. if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
  2290. return;
  2291. cpumask_clear_cpu(cpu, cpus_hardware_enabled);
  2292. kvm_arch_hardware_disable(NULL);
  2293. }
  2294. static void hardware_disable(void)
  2295. {
  2296. raw_spin_lock(&kvm_count_lock);
  2297. if (kvm_usage_count)
  2298. hardware_disable_nolock(NULL);
  2299. raw_spin_unlock(&kvm_count_lock);
  2300. }
  2301. static void hardware_disable_all_nolock(void)
  2302. {
  2303. BUG_ON(!kvm_usage_count);
  2304. kvm_usage_count--;
  2305. if (!kvm_usage_count)
  2306. on_each_cpu(hardware_disable_nolock, NULL, 1);
  2307. }
  2308. static void hardware_disable_all(void)
  2309. {
  2310. raw_spin_lock(&kvm_count_lock);
  2311. hardware_disable_all_nolock();
  2312. raw_spin_unlock(&kvm_count_lock);
  2313. }
  2314. static int hardware_enable_all(void)
  2315. {
  2316. int r = 0;
  2317. raw_spin_lock(&kvm_count_lock);
  2318. kvm_usage_count++;
  2319. if (kvm_usage_count == 1) {
  2320. atomic_set(&hardware_enable_failed, 0);
  2321. on_each_cpu(hardware_enable_nolock, NULL, 1);
  2322. if (atomic_read(&hardware_enable_failed)) {
  2323. hardware_disable_all_nolock();
  2324. r = -EBUSY;
  2325. }
  2326. }
  2327. raw_spin_unlock(&kvm_count_lock);
  2328. return r;
  2329. }
  2330. static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
  2331. void *v)
  2332. {
  2333. int cpu = (long)v;
  2334. val &= ~CPU_TASKS_FROZEN;
  2335. switch (val) {
  2336. case CPU_DYING:
  2337. printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
  2338. cpu);
  2339. hardware_disable();
  2340. break;
  2341. case CPU_STARTING:
  2342. printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
  2343. cpu);
  2344. hardware_enable();
  2345. break;
  2346. }
  2347. return NOTIFY_OK;
  2348. }
  2349. static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
  2350. void *v)
  2351. {
  2352. /*
  2353. * Some (well, at least mine) BIOSes hang on reboot if
  2354. * in vmx root mode.
  2355. *
  2356. * And Intel TXT required VMX off for all cpu when system shutdown.
  2357. */
  2358. printk(KERN_INFO "kvm: exiting hardware virtualization\n");
  2359. kvm_rebooting = true;
  2360. on_each_cpu(hardware_disable_nolock, NULL, 1);
  2361. return NOTIFY_OK;
  2362. }
  2363. static struct notifier_block kvm_reboot_notifier = {
  2364. .notifier_call = kvm_reboot,
  2365. .priority = 0,
  2366. };
  2367. static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
  2368. {
  2369. int i;
  2370. for (i = 0; i < bus->dev_count; i++) {
  2371. struct kvm_io_device *pos = bus->range[i].dev;
  2372. kvm_iodevice_destructor(pos);
  2373. }
  2374. kfree(bus);
  2375. }
  2376. static inline int kvm_io_bus_cmp(const struct kvm_io_range *r1,
  2377. const struct kvm_io_range *r2)
  2378. {
  2379. if (r1->addr < r2->addr)
  2380. return -1;
  2381. if (r1->addr + r1->len > r2->addr + r2->len)
  2382. return 1;
  2383. return 0;
  2384. }
  2385. static int kvm_io_bus_sort_cmp(const void *p1, const void *p2)
  2386. {
  2387. return kvm_io_bus_cmp(p1, p2);
  2388. }
  2389. static int kvm_io_bus_insert_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev,
  2390. gpa_t addr, int len)
  2391. {
  2392. bus->range[bus->dev_count++] = (struct kvm_io_range) {
  2393. .addr = addr,
  2394. .len = len,
  2395. .dev = dev,
  2396. };
  2397. sort(bus->range, bus->dev_count, sizeof(struct kvm_io_range),
  2398. kvm_io_bus_sort_cmp, NULL);
  2399. return 0;
  2400. }
  2401. static int kvm_io_bus_get_first_dev(struct kvm_io_bus *bus,
  2402. gpa_t addr, int len)
  2403. {
  2404. struct kvm_io_range *range, key;
  2405. int off;
  2406. key = (struct kvm_io_range) {
  2407. .addr = addr,
  2408. .len = len,
  2409. };
  2410. range = bsearch(&key, bus->range, bus->dev_count,
  2411. sizeof(struct kvm_io_range), kvm_io_bus_sort_cmp);
  2412. if (range == NULL)
  2413. return -ENOENT;
  2414. off = range - bus->range;
  2415. while (off > 0 && kvm_io_bus_cmp(&key, &bus->range[off-1]) == 0)
  2416. off--;
  2417. return off;
  2418. }
  2419. static int __kvm_io_bus_write(struct kvm_io_bus *bus,
  2420. struct kvm_io_range *range, const void *val)
  2421. {
  2422. int idx;
  2423. idx = kvm_io_bus_get_first_dev(bus, range->addr, range->len);
  2424. if (idx < 0)
  2425. return -EOPNOTSUPP;
  2426. while (idx < bus->dev_count &&
  2427. kvm_io_bus_cmp(range, &bus->range[idx]) == 0) {
  2428. if (!kvm_iodevice_write(bus->range[idx].dev, range->addr,
  2429. range->len, val))
  2430. return idx;
  2431. idx++;
  2432. }
  2433. return -EOPNOTSUPP;
  2434. }
  2435. /* kvm_io_bus_write - called under kvm->slots_lock */
  2436. int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
  2437. int len, const void *val)
  2438. {
  2439. struct kvm_io_bus *bus;
  2440. struct kvm_io_range range;
  2441. int r;
  2442. range = (struct kvm_io_range) {
  2443. .addr = addr,
  2444. .len = len,
  2445. };
  2446. bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
  2447. r = __kvm_io_bus_write(bus, &range, val);
  2448. return r < 0 ? r : 0;
  2449. }
  2450. /* kvm_io_bus_write_cookie - called under kvm->slots_lock */
  2451. int kvm_io_bus_write_cookie(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
  2452. int len, const void *val, long cookie)
  2453. {
  2454. struct kvm_io_bus *bus;
  2455. struct kvm_io_range range;
  2456. range = (struct kvm_io_range) {
  2457. .addr = addr,
  2458. .len = len,
  2459. };
  2460. bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
  2461. /* First try the device referenced by cookie. */
  2462. if ((cookie >= 0) && (cookie < bus->dev_count) &&
  2463. (kvm_io_bus_cmp(&range, &bus->range[cookie]) == 0))
  2464. if (!kvm_iodevice_write(bus->range[cookie].dev, addr, len,
  2465. val))
  2466. return cookie;
  2467. /*
  2468. * cookie contained garbage; fall back to search and return the
  2469. * correct cookie value.
  2470. */
  2471. return __kvm_io_bus_write(bus, &range, val);
  2472. }
  2473. static int __kvm_io_bus_read(struct kvm_io_bus *bus, struct kvm_io_range *range,
  2474. void *val)
  2475. {
  2476. int idx;
  2477. idx = kvm_io_bus_get_first_dev(bus, range->addr, range->len);
  2478. if (idx < 0)
  2479. return -EOPNOTSUPP;
  2480. while (idx < bus->dev_count &&
  2481. kvm_io_bus_cmp(range, &bus->range[idx]) == 0) {
  2482. if (!kvm_iodevice_read(bus->range[idx].dev, range->addr,
  2483. range->len, val))
  2484. return idx;
  2485. idx++;
  2486. }
  2487. return -EOPNOTSUPP;
  2488. }
  2489. /* kvm_io_bus_read - called under kvm->slots_lock */
  2490. int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
  2491. int len, void *val)
  2492. {
  2493. struct kvm_io_bus *bus;
  2494. struct kvm_io_range range;
  2495. int r;
  2496. range = (struct kvm_io_range) {
  2497. .addr = addr,
  2498. .len = len,
  2499. };
  2500. bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
  2501. r = __kvm_io_bus_read(bus, &range, val);
  2502. return r < 0 ? r : 0;
  2503. }
  2504. /* kvm_io_bus_read_cookie - called under kvm->slots_lock */
  2505. int kvm_io_bus_read_cookie(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
  2506. int len, void *val, long cookie)
  2507. {
  2508. struct kvm_io_bus *bus;
  2509. struct kvm_io_range range;
  2510. range = (struct kvm_io_range) {
  2511. .addr = addr,
  2512. .len = len,
  2513. };
  2514. bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
  2515. /* First try the device referenced by cookie. */
  2516. if ((cookie >= 0) && (cookie < bus->dev_count) &&
  2517. (kvm_io_bus_cmp(&range, &bus->range[cookie]) == 0))
  2518. if (!kvm_iodevice_read(bus->range[cookie].dev, addr, len,
  2519. val))
  2520. return cookie;
  2521. /*
  2522. * cookie contained garbage; fall back to search and return the
  2523. * correct cookie value.
  2524. */
  2525. return __kvm_io_bus_read(bus, &range, val);
  2526. }
  2527. /* Caller must hold slots_lock. */
  2528. int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
  2529. int len, struct kvm_io_device *dev)
  2530. {
  2531. struct kvm_io_bus *new_bus, *bus;
  2532. bus = kvm->buses[bus_idx];
  2533. /* exclude ioeventfd which is limited by maximum fd */
  2534. if (bus->dev_count - bus->ioeventfd_count > NR_IOBUS_DEVS - 1)
  2535. return -ENOSPC;
  2536. new_bus = kzalloc(sizeof(*bus) + ((bus->dev_count + 1) *
  2537. sizeof(struct kvm_io_range)), GFP_KERNEL);
  2538. if (!new_bus)
  2539. return -ENOMEM;
  2540. memcpy(new_bus, bus, sizeof(*bus) + (bus->dev_count *
  2541. sizeof(struct kvm_io_range)));
  2542. kvm_io_bus_insert_dev(new_bus, dev, addr, len);
  2543. rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
  2544. synchronize_srcu_expedited(&kvm->srcu);
  2545. kfree(bus);
  2546. return 0;
  2547. }
  2548. /* Caller must hold slots_lock. */
  2549. int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
  2550. struct kvm_io_device *dev)
  2551. {
  2552. int i, r;
  2553. struct kvm_io_bus *new_bus, *bus;
  2554. bus = kvm->buses[bus_idx];
  2555. r = -ENOENT;
  2556. for (i = 0; i < bus->dev_count; i++)
  2557. if (bus->range[i].dev == dev) {
  2558. r = 0;
  2559. break;
  2560. }
  2561. if (r)
  2562. return r;
  2563. new_bus = kzalloc(sizeof(*bus) + ((bus->dev_count - 1) *
  2564. sizeof(struct kvm_io_range)), GFP_KERNEL);
  2565. if (!new_bus)
  2566. return -ENOMEM;
  2567. memcpy(new_bus, bus, sizeof(*bus) + i * sizeof(struct kvm_io_range));
  2568. new_bus->dev_count--;
  2569. memcpy(new_bus->range + i, bus->range + i + 1,
  2570. (new_bus->dev_count - i) * sizeof(struct kvm_io_range));
  2571. rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
  2572. synchronize_srcu_expedited(&kvm->srcu);
  2573. kfree(bus);
  2574. return r;
  2575. }
  2576. static struct notifier_block kvm_cpu_notifier = {
  2577. .notifier_call = kvm_cpu_hotplug,
  2578. };
  2579. static int vm_stat_get(void *_offset, u64 *val)
  2580. {
  2581. unsigned offset = (long)_offset;
  2582. struct kvm *kvm;
  2583. *val = 0;
  2584. spin_lock(&kvm_lock);
  2585. list_for_each_entry(kvm, &vm_list, vm_list)
  2586. *val += *(u32 *)((void *)kvm + offset);
  2587. spin_unlock(&kvm_lock);
  2588. return 0;
  2589. }
  2590. DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
  2591. static int vcpu_stat_get(void *_offset, u64 *val)
  2592. {
  2593. unsigned offset = (long)_offset;
  2594. struct kvm *kvm;
  2595. struct kvm_vcpu *vcpu;
  2596. int i;
  2597. *val = 0;
  2598. spin_lock(&kvm_lock);
  2599. list_for_each_entry(kvm, &vm_list, vm_list)
  2600. kvm_for_each_vcpu(i, vcpu, kvm)
  2601. *val += *(u32 *)((void *)vcpu + offset);
  2602. spin_unlock(&kvm_lock);
  2603. return 0;
  2604. }
  2605. DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
  2606. static const struct file_operations *stat_fops[] = {
  2607. [KVM_STAT_VCPU] = &vcpu_stat_fops,
  2608. [KVM_STAT_VM] = &vm_stat_fops,
  2609. };
  2610. static int kvm_init_debug(void)
  2611. {
  2612. int r = -EFAULT;
  2613. struct kvm_stats_debugfs_item *p;
  2614. kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
  2615. if (kvm_debugfs_dir == NULL)
  2616. goto out;
  2617. for (p = debugfs_entries; p->name; ++p) {
  2618. p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
  2619. (void *)(long)p->offset,
  2620. stat_fops[p->kind]);
  2621. if (p->dentry == NULL)
  2622. goto out_dir;
  2623. }
  2624. return 0;
  2625. out_dir:
  2626. debugfs_remove_recursive(kvm_debugfs_dir);
  2627. out:
  2628. return r;
  2629. }
  2630. static void kvm_exit_debug(void)
  2631. {
  2632. struct kvm_stats_debugfs_item *p;
  2633. for (p = debugfs_entries; p->name; ++p)
  2634. debugfs_remove(p->dentry);
  2635. debugfs_remove(kvm_debugfs_dir);
  2636. }
  2637. static int kvm_suspend(void)
  2638. {
  2639. if (kvm_usage_count)
  2640. hardware_disable_nolock(NULL);
  2641. return 0;
  2642. }
  2643. static void kvm_resume(void)
  2644. {
  2645. if (kvm_usage_count) {
  2646. WARN_ON(raw_spin_is_locked(&kvm_count_lock));
  2647. hardware_enable_nolock(NULL);
  2648. }
  2649. }
  2650. static struct syscore_ops kvm_syscore_ops = {
  2651. .suspend = kvm_suspend,
  2652. .resume = kvm_resume,
  2653. };
  2654. static inline
  2655. struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
  2656. {
  2657. return container_of(pn, struct kvm_vcpu, preempt_notifier);
  2658. }
  2659. static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
  2660. {
  2661. struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
  2662. if (vcpu->preempted)
  2663. vcpu->preempted = false;
  2664. kvm_arch_vcpu_load(vcpu, cpu);
  2665. }
  2666. static void kvm_sched_out(struct preempt_notifier *pn,
  2667. struct task_struct *next)
  2668. {
  2669. struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
  2670. if (current->state == TASK_RUNNING)
  2671. vcpu->preempted = true;
  2672. kvm_arch_vcpu_put(vcpu);
  2673. }
  2674. int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
  2675. struct module *module)
  2676. {
  2677. int r;
  2678. int cpu;
  2679. r = kvm_arch_init(opaque);
  2680. if (r)
  2681. goto out_fail;
  2682. /*
  2683. * kvm_arch_init makes sure there's at most one caller
  2684. * for architectures that support multiple implementations,
  2685. * like intel and amd on x86.
  2686. * kvm_arch_init must be called before kvm_irqfd_init to avoid creating
  2687. * conflicts in case kvm is already setup for another implementation.
  2688. */
  2689. r = kvm_irqfd_init();
  2690. if (r)
  2691. goto out_irqfd;
  2692. if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
  2693. r = -ENOMEM;
  2694. goto out_free_0;
  2695. }
  2696. r = kvm_arch_hardware_setup();
  2697. if (r < 0)
  2698. goto out_free_0a;
  2699. for_each_online_cpu(cpu) {
  2700. smp_call_function_single(cpu,
  2701. kvm_arch_check_processor_compat,
  2702. &r, 1);
  2703. if (r < 0)
  2704. goto out_free_1;
  2705. }
  2706. r = register_cpu_notifier(&kvm_cpu_notifier);
  2707. if (r)
  2708. goto out_free_2;
  2709. register_reboot_notifier(&kvm_reboot_notifier);
  2710. /* A kmem cache lets us meet the alignment requirements of fx_save. */
  2711. if (!vcpu_align)
  2712. vcpu_align = __alignof__(struct kvm_vcpu);
  2713. kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
  2714. 0, NULL);
  2715. if (!kvm_vcpu_cache) {
  2716. r = -ENOMEM;
  2717. goto out_free_3;
  2718. }
  2719. r = kvm_async_pf_init();
  2720. if (r)
  2721. goto out_free;
  2722. kvm_chardev_ops.owner = module;
  2723. kvm_vm_fops.owner = module;
  2724. kvm_vcpu_fops.owner = module;
  2725. r = misc_register(&kvm_dev);
  2726. if (r) {
  2727. printk(KERN_ERR "kvm: misc device register failed\n");
  2728. goto out_unreg;
  2729. }
  2730. register_syscore_ops(&kvm_syscore_ops);
  2731. kvm_preempt_ops.sched_in = kvm_sched_in;
  2732. kvm_preempt_ops.sched_out = kvm_sched_out;
  2733. r = kvm_init_debug();
  2734. if (r) {
  2735. printk(KERN_ERR "kvm: create debugfs files failed\n");
  2736. goto out_undebugfs;
  2737. }
  2738. return 0;
  2739. out_undebugfs:
  2740. unregister_syscore_ops(&kvm_syscore_ops);
  2741. misc_deregister(&kvm_dev);
  2742. out_unreg:
  2743. kvm_async_pf_deinit();
  2744. out_free:
  2745. kmem_cache_destroy(kvm_vcpu_cache);
  2746. out_free_3:
  2747. unregister_reboot_notifier(&kvm_reboot_notifier);
  2748. unregister_cpu_notifier(&kvm_cpu_notifier);
  2749. out_free_2:
  2750. out_free_1:
  2751. kvm_arch_hardware_unsetup();
  2752. out_free_0a:
  2753. free_cpumask_var(cpus_hardware_enabled);
  2754. out_free_0:
  2755. kvm_irqfd_exit();
  2756. out_irqfd:
  2757. kvm_arch_exit();
  2758. out_fail:
  2759. return r;
  2760. }
  2761. EXPORT_SYMBOL_GPL(kvm_init);
  2762. void kvm_exit(void)
  2763. {
  2764. kvm_exit_debug();
  2765. misc_deregister(&kvm_dev);
  2766. kmem_cache_destroy(kvm_vcpu_cache);
  2767. kvm_async_pf_deinit();
  2768. unregister_syscore_ops(&kvm_syscore_ops);
  2769. unregister_reboot_notifier(&kvm_reboot_notifier);
  2770. unregister_cpu_notifier(&kvm_cpu_notifier);
  2771. on_each_cpu(hardware_disable_nolock, NULL, 1);
  2772. kvm_arch_hardware_unsetup();
  2773. kvm_arch_exit();
  2774. kvm_irqfd_exit();
  2775. free_cpumask_var(cpus_hardware_enabled);
  2776. }
  2777. EXPORT_SYMBOL_GPL(kvm_exit);