kvm_main.c 74 KB

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