kvm_main.c 73 KB

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