kvm_main.c 74 KB

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