kvm_main.c 66 KB

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