kvm_main.c 94 KB

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