kvm_main.c 92 KB

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