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