kvm_main.c 84 KB

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