kvm_main.c 89 KB

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