kvm_main.c 74 KB

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