kvm_main.c 78 KB

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