kvm_main.c 74 KB

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