kvm_main.c 92 KB

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