kvm_main.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964
  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. struct kvm_memory_slot *memslot, *slot;
  603. struct kvm_memory_slot old, new;
  604. struct kvm_memslots *slots, *old_memslots;
  605. r = check_memory_region_flags(mem);
  606. if (r)
  607. goto out;
  608. r = -EINVAL;
  609. /* General sanity checks */
  610. if (mem->memory_size & (PAGE_SIZE - 1))
  611. goto out;
  612. if (mem->guest_phys_addr & (PAGE_SIZE - 1))
  613. goto out;
  614. /* We can read the guest memory with __xxx_user() later on. */
  615. if (user_alloc &&
  616. ((mem->userspace_addr & (PAGE_SIZE - 1)) ||
  617. !access_ok(VERIFY_WRITE,
  618. (void __user *)(unsigned long)mem->userspace_addr,
  619. mem->memory_size)))
  620. goto out;
  621. if (mem->slot >= KVM_MEM_SLOTS_NUM)
  622. goto out;
  623. if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
  624. goto out;
  625. memslot = id_to_memslot(kvm->memslots, mem->slot);
  626. base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
  627. npages = mem->memory_size >> PAGE_SHIFT;
  628. r = -EINVAL;
  629. if (npages > KVM_MEM_MAX_NR_PAGES)
  630. goto out;
  631. if (!npages)
  632. mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
  633. new = old = *memslot;
  634. new.id = mem->slot;
  635. new.base_gfn = base_gfn;
  636. new.npages = npages;
  637. new.flags = mem->flags;
  638. /*
  639. * Disallow changing a memory slot's size or changing anything about
  640. * zero sized slots that doesn't involve making them non-zero.
  641. */
  642. r = -EINVAL;
  643. if (npages && old.npages && npages != old.npages)
  644. goto out_free;
  645. if (!npages && !old.npages)
  646. goto out_free;
  647. /* Check for overlaps */
  648. r = -EEXIST;
  649. kvm_for_each_memslot(slot, kvm->memslots) {
  650. if (slot->id >= KVM_MEMORY_SLOTS || slot == memslot)
  651. continue;
  652. if (!((base_gfn + npages <= slot->base_gfn) ||
  653. (base_gfn >= slot->base_gfn + slot->npages)))
  654. goto out_free;
  655. }
  656. /* Free page dirty bitmap if unneeded */
  657. if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
  658. new.dirty_bitmap = NULL;
  659. r = -ENOMEM;
  660. /*
  661. * Allocate if a slot is being created. If modifying a slot,
  662. * the userspace_addr cannot change.
  663. */
  664. if (!old.npages) {
  665. new.user_alloc = user_alloc;
  666. new.userspace_addr = mem->userspace_addr;
  667. if (kvm_arch_create_memslot(&new, npages))
  668. goto out_free;
  669. } else if (npages && mem->userspace_addr != old.userspace_addr) {
  670. r = -EINVAL;
  671. goto out_free;
  672. }
  673. /* Allocate page dirty bitmap if needed */
  674. if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
  675. if (kvm_create_dirty_bitmap(&new) < 0)
  676. goto out_free;
  677. /* destroy any largepage mappings for dirty tracking */
  678. }
  679. if (!npages || base_gfn != old.base_gfn) {
  680. struct kvm_memory_slot *slot;
  681. r = -ENOMEM;
  682. slots = kmemdup(kvm->memslots, sizeof(struct kvm_memslots),
  683. GFP_KERNEL);
  684. if (!slots)
  685. goto out_free;
  686. slot = id_to_memslot(slots, mem->slot);
  687. slot->flags |= KVM_MEMSLOT_INVALID;
  688. update_memslots(slots, NULL);
  689. old_memslots = kvm->memslots;
  690. rcu_assign_pointer(kvm->memslots, slots);
  691. synchronize_srcu_expedited(&kvm->srcu);
  692. /* slot was deleted or moved, clear iommu mapping */
  693. kvm_iommu_unmap_pages(kvm, &old);
  694. /* From this point no new shadow pages pointing to a deleted,
  695. * or moved, memslot will be created.
  696. *
  697. * validation of sp->gfn happens in:
  698. * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
  699. * - kvm_is_visible_gfn (mmu_check_roots)
  700. */
  701. kvm_arch_flush_shadow_memslot(kvm, slot);
  702. kfree(old_memslots);
  703. }
  704. r = kvm_arch_prepare_memory_region(kvm, &new, old, mem, user_alloc);
  705. if (r)
  706. goto out_free;
  707. r = -ENOMEM;
  708. slots = kmemdup(kvm->memslots, sizeof(struct kvm_memslots),
  709. GFP_KERNEL);
  710. if (!slots)
  711. goto out_free;
  712. /* map new memory slot into the iommu */
  713. if (npages) {
  714. r = kvm_iommu_map_pages(kvm, &new);
  715. if (r)
  716. goto out_slots;
  717. }
  718. /* actual memory is freed via old in kvm_free_physmem_slot below */
  719. if (!npages) {
  720. new.dirty_bitmap = NULL;
  721. memset(&new.arch, 0, sizeof(new.arch));
  722. }
  723. update_memslots(slots, &new);
  724. old_memslots = kvm->memslots;
  725. rcu_assign_pointer(kvm->memslots, slots);
  726. synchronize_srcu_expedited(&kvm->srcu);
  727. kvm_arch_commit_memory_region(kvm, mem, old, user_alloc);
  728. kvm_free_physmem_slot(&old, &new);
  729. kfree(old_memslots);
  730. return 0;
  731. out_slots:
  732. kfree(slots);
  733. out_free:
  734. kvm_free_physmem_slot(&new, &old);
  735. out:
  736. return r;
  737. }
  738. EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
  739. int kvm_set_memory_region(struct kvm *kvm,
  740. struct kvm_userspace_memory_region *mem,
  741. int user_alloc)
  742. {
  743. int r;
  744. mutex_lock(&kvm->slots_lock);
  745. r = __kvm_set_memory_region(kvm, mem, user_alloc);
  746. mutex_unlock(&kvm->slots_lock);
  747. return r;
  748. }
  749. EXPORT_SYMBOL_GPL(kvm_set_memory_region);
  750. int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
  751. struct
  752. kvm_userspace_memory_region *mem,
  753. int user_alloc)
  754. {
  755. if (mem->slot >= KVM_MEMORY_SLOTS)
  756. return -EINVAL;
  757. return kvm_set_memory_region(kvm, mem, user_alloc);
  758. }
  759. int kvm_get_dirty_log(struct kvm *kvm,
  760. struct kvm_dirty_log *log, int *is_dirty)
  761. {
  762. struct kvm_memory_slot *memslot;
  763. int r, i;
  764. unsigned long n;
  765. unsigned long any = 0;
  766. r = -EINVAL;
  767. if (log->slot >= KVM_MEMORY_SLOTS)
  768. goto out;
  769. memslot = id_to_memslot(kvm->memslots, log->slot);
  770. r = -ENOENT;
  771. if (!memslot->dirty_bitmap)
  772. goto out;
  773. n = kvm_dirty_bitmap_bytes(memslot);
  774. for (i = 0; !any && i < n/sizeof(long); ++i)
  775. any = memslot->dirty_bitmap[i];
  776. r = -EFAULT;
  777. if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
  778. goto out;
  779. if (any)
  780. *is_dirty = 1;
  781. r = 0;
  782. out:
  783. return r;
  784. }
  785. bool kvm_largepages_enabled(void)
  786. {
  787. return largepages_enabled;
  788. }
  789. void kvm_disable_largepages(void)
  790. {
  791. largepages_enabled = false;
  792. }
  793. EXPORT_SYMBOL_GPL(kvm_disable_largepages);
  794. struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
  795. {
  796. return __gfn_to_memslot(kvm_memslots(kvm), gfn);
  797. }
  798. EXPORT_SYMBOL_GPL(gfn_to_memslot);
  799. int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
  800. {
  801. struct kvm_memory_slot *memslot = gfn_to_memslot(kvm, gfn);
  802. if (!memslot || memslot->id >= KVM_MEMORY_SLOTS ||
  803. memslot->flags & KVM_MEMSLOT_INVALID)
  804. return 0;
  805. return 1;
  806. }
  807. EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
  808. unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn)
  809. {
  810. struct vm_area_struct *vma;
  811. unsigned long addr, size;
  812. size = PAGE_SIZE;
  813. addr = gfn_to_hva(kvm, gfn);
  814. if (kvm_is_error_hva(addr))
  815. return PAGE_SIZE;
  816. down_read(&current->mm->mmap_sem);
  817. vma = find_vma(current->mm, addr);
  818. if (!vma)
  819. goto out;
  820. size = vma_kernel_pagesize(vma);
  821. out:
  822. up_read(&current->mm->mmap_sem);
  823. return size;
  824. }
  825. static bool memslot_is_readonly(struct kvm_memory_slot *slot)
  826. {
  827. return slot->flags & KVM_MEM_READONLY;
  828. }
  829. static unsigned long __gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
  830. gfn_t *nr_pages, bool write)
  831. {
  832. if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
  833. return KVM_HVA_ERR_BAD;
  834. if (memslot_is_readonly(slot) && write)
  835. return KVM_HVA_ERR_RO_BAD;
  836. if (nr_pages)
  837. *nr_pages = slot->npages - (gfn - slot->base_gfn);
  838. return __gfn_to_hva_memslot(slot, gfn);
  839. }
  840. static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
  841. gfn_t *nr_pages)
  842. {
  843. return __gfn_to_hva_many(slot, gfn, nr_pages, true);
  844. }
  845. unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot,
  846. gfn_t gfn)
  847. {
  848. return gfn_to_hva_many(slot, gfn, NULL);
  849. }
  850. EXPORT_SYMBOL_GPL(gfn_to_hva_memslot);
  851. unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
  852. {
  853. return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL);
  854. }
  855. EXPORT_SYMBOL_GPL(gfn_to_hva);
  856. /*
  857. * The hva returned by this function is only allowed to be read.
  858. * It should pair with kvm_read_hva() or kvm_read_hva_atomic().
  859. */
  860. static unsigned long gfn_to_hva_read(struct kvm *kvm, gfn_t gfn)
  861. {
  862. return __gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL, false);
  863. }
  864. static int kvm_read_hva(void *data, void __user *hva, int len)
  865. {
  866. return __copy_from_user(data, hva, len);
  867. }
  868. static int kvm_read_hva_atomic(void *data, void __user *hva, int len)
  869. {
  870. return __copy_from_user_inatomic(data, hva, len);
  871. }
  872. int get_user_page_nowait(struct task_struct *tsk, struct mm_struct *mm,
  873. unsigned long start, int write, struct page **page)
  874. {
  875. int flags = FOLL_TOUCH | FOLL_NOWAIT | FOLL_HWPOISON | FOLL_GET;
  876. if (write)
  877. flags |= FOLL_WRITE;
  878. return __get_user_pages(tsk, mm, start, 1, flags, page, NULL, NULL);
  879. }
  880. static inline int check_user_page_hwpoison(unsigned long addr)
  881. {
  882. int rc, flags = FOLL_TOUCH | FOLL_HWPOISON | FOLL_WRITE;
  883. rc = __get_user_pages(current, current->mm, addr, 1,
  884. flags, NULL, NULL, NULL);
  885. return rc == -EHWPOISON;
  886. }
  887. /*
  888. * The atomic path to get the writable pfn which will be stored in @pfn,
  889. * true indicates success, otherwise false is returned.
  890. */
  891. static bool hva_to_pfn_fast(unsigned long addr, bool atomic, bool *async,
  892. bool write_fault, bool *writable, pfn_t *pfn)
  893. {
  894. struct page *page[1];
  895. int npages;
  896. if (!(async || atomic))
  897. return false;
  898. /*
  899. * Fast pin a writable pfn only if it is a write fault request
  900. * or the caller allows to map a writable pfn for a read fault
  901. * request.
  902. */
  903. if (!(write_fault || writable))
  904. return false;
  905. npages = __get_user_pages_fast(addr, 1, 1, page);
  906. if (npages == 1) {
  907. *pfn = page_to_pfn(page[0]);
  908. if (writable)
  909. *writable = true;
  910. return true;
  911. }
  912. return false;
  913. }
  914. /*
  915. * The slow path to get the pfn of the specified host virtual address,
  916. * 1 indicates success, -errno is returned if error is detected.
  917. */
  918. static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault,
  919. bool *writable, pfn_t *pfn)
  920. {
  921. struct page *page[1];
  922. int npages = 0;
  923. might_sleep();
  924. if (writable)
  925. *writable = write_fault;
  926. if (async) {
  927. down_read(&current->mm->mmap_sem);
  928. npages = get_user_page_nowait(current, current->mm,
  929. addr, write_fault, page);
  930. up_read(&current->mm->mmap_sem);
  931. } else
  932. npages = get_user_pages_fast(addr, 1, write_fault,
  933. page);
  934. if (npages != 1)
  935. return npages;
  936. /* map read fault as writable if possible */
  937. if (unlikely(!write_fault) && writable) {
  938. struct page *wpage[1];
  939. npages = __get_user_pages_fast(addr, 1, 1, wpage);
  940. if (npages == 1) {
  941. *writable = true;
  942. put_page(page[0]);
  943. page[0] = wpage[0];
  944. }
  945. npages = 1;
  946. }
  947. *pfn = page_to_pfn(page[0]);
  948. return npages;
  949. }
  950. static bool vma_is_valid(struct vm_area_struct *vma, bool write_fault)
  951. {
  952. if (unlikely(!(vma->vm_flags & VM_READ)))
  953. return false;
  954. if (write_fault && (unlikely(!(vma->vm_flags & VM_WRITE))))
  955. return false;
  956. return true;
  957. }
  958. /*
  959. * Pin guest page in memory and return its pfn.
  960. * @addr: host virtual address which maps memory to the guest
  961. * @atomic: whether this function can sleep
  962. * @async: whether this function need to wait IO complete if the
  963. * host page is not in the memory
  964. * @write_fault: whether we should get a writable host page
  965. * @writable: whether it allows to map a writable host page for !@write_fault
  966. *
  967. * The function will map a writable host page for these two cases:
  968. * 1): @write_fault = true
  969. * 2): @write_fault = false && @writable, @writable will tell the caller
  970. * whether the mapping is writable.
  971. */
  972. static pfn_t hva_to_pfn(unsigned long addr, bool atomic, bool *async,
  973. bool write_fault, bool *writable)
  974. {
  975. struct vm_area_struct *vma;
  976. pfn_t pfn = 0;
  977. int npages;
  978. /* we can do it either atomically or asynchronously, not both */
  979. BUG_ON(atomic && async);
  980. if (hva_to_pfn_fast(addr, atomic, async, write_fault, writable, &pfn))
  981. return pfn;
  982. if (atomic)
  983. return KVM_PFN_ERR_FAULT;
  984. npages = hva_to_pfn_slow(addr, async, write_fault, writable, &pfn);
  985. if (npages == 1)
  986. return pfn;
  987. down_read(&current->mm->mmap_sem);
  988. if (npages == -EHWPOISON ||
  989. (!async && check_user_page_hwpoison(addr))) {
  990. pfn = KVM_PFN_ERR_HWPOISON;
  991. goto exit;
  992. }
  993. vma = find_vma_intersection(current->mm, addr, addr + 1);
  994. if (vma == NULL)
  995. pfn = KVM_PFN_ERR_FAULT;
  996. else if ((vma->vm_flags & VM_PFNMAP)) {
  997. pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) +
  998. vma->vm_pgoff;
  999. BUG_ON(!kvm_is_mmio_pfn(pfn));
  1000. } else {
  1001. if (async && vma_is_valid(vma, write_fault))
  1002. *async = true;
  1003. pfn = KVM_PFN_ERR_FAULT;
  1004. }
  1005. exit:
  1006. up_read(&current->mm->mmap_sem);
  1007. return pfn;
  1008. }
  1009. static pfn_t
  1010. __gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn, bool atomic,
  1011. bool *async, bool write_fault, bool *writable)
  1012. {
  1013. unsigned long addr = __gfn_to_hva_many(slot, gfn, NULL, write_fault);
  1014. if (addr == KVM_HVA_ERR_RO_BAD)
  1015. return KVM_PFN_ERR_RO_FAULT;
  1016. if (kvm_is_error_hva(addr))
  1017. return KVM_PFN_NOSLOT;
  1018. /* Do not map writable pfn in the readonly memslot. */
  1019. if (writable && memslot_is_readonly(slot)) {
  1020. *writable = false;
  1021. writable = NULL;
  1022. }
  1023. return hva_to_pfn(addr, atomic, async, write_fault,
  1024. writable);
  1025. }
  1026. static pfn_t __gfn_to_pfn(struct kvm *kvm, gfn_t gfn, bool atomic, bool *async,
  1027. bool write_fault, bool *writable)
  1028. {
  1029. struct kvm_memory_slot *slot;
  1030. if (async)
  1031. *async = false;
  1032. slot = gfn_to_memslot(kvm, gfn);
  1033. return __gfn_to_pfn_memslot(slot, gfn, atomic, async, write_fault,
  1034. writable);
  1035. }
  1036. pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn)
  1037. {
  1038. return __gfn_to_pfn(kvm, gfn, true, NULL, true, NULL);
  1039. }
  1040. EXPORT_SYMBOL_GPL(gfn_to_pfn_atomic);
  1041. pfn_t gfn_to_pfn_async(struct kvm *kvm, gfn_t gfn, bool *async,
  1042. bool write_fault, bool *writable)
  1043. {
  1044. return __gfn_to_pfn(kvm, gfn, false, async, write_fault, writable);
  1045. }
  1046. EXPORT_SYMBOL_GPL(gfn_to_pfn_async);
  1047. pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
  1048. {
  1049. return __gfn_to_pfn(kvm, gfn, false, NULL, true, NULL);
  1050. }
  1051. EXPORT_SYMBOL_GPL(gfn_to_pfn);
  1052. pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
  1053. bool *writable)
  1054. {
  1055. return __gfn_to_pfn(kvm, gfn, false, NULL, write_fault, writable);
  1056. }
  1057. EXPORT_SYMBOL_GPL(gfn_to_pfn_prot);
  1058. pfn_t gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn)
  1059. {
  1060. return __gfn_to_pfn_memslot(slot, gfn, false, NULL, true, NULL);
  1061. }
  1062. pfn_t gfn_to_pfn_memslot_atomic(struct kvm_memory_slot *slot, gfn_t gfn)
  1063. {
  1064. return __gfn_to_pfn_memslot(slot, gfn, true, NULL, true, NULL);
  1065. }
  1066. EXPORT_SYMBOL_GPL(gfn_to_pfn_memslot_atomic);
  1067. int gfn_to_page_many_atomic(struct kvm *kvm, gfn_t gfn, struct page **pages,
  1068. int nr_pages)
  1069. {
  1070. unsigned long addr;
  1071. gfn_t entry;
  1072. addr = gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, &entry);
  1073. if (kvm_is_error_hva(addr))
  1074. return -1;
  1075. if (entry < nr_pages)
  1076. return 0;
  1077. return __get_user_pages_fast(addr, nr_pages, 1, pages);
  1078. }
  1079. EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic);
  1080. static struct page *kvm_pfn_to_page(pfn_t pfn)
  1081. {
  1082. if (is_error_noslot_pfn(pfn))
  1083. return KVM_ERR_PTR_BAD_PAGE;
  1084. if (kvm_is_mmio_pfn(pfn)) {
  1085. WARN_ON(1);
  1086. return KVM_ERR_PTR_BAD_PAGE;
  1087. }
  1088. return pfn_to_page(pfn);
  1089. }
  1090. struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
  1091. {
  1092. pfn_t pfn;
  1093. pfn = gfn_to_pfn(kvm, gfn);
  1094. return kvm_pfn_to_page(pfn);
  1095. }
  1096. EXPORT_SYMBOL_GPL(gfn_to_page);
  1097. void kvm_release_page_clean(struct page *page)
  1098. {
  1099. WARN_ON(is_error_page(page));
  1100. kvm_release_pfn_clean(page_to_pfn(page));
  1101. }
  1102. EXPORT_SYMBOL_GPL(kvm_release_page_clean);
  1103. void kvm_release_pfn_clean(pfn_t pfn)
  1104. {
  1105. if (!is_error_noslot_pfn(pfn) && !kvm_is_mmio_pfn(pfn))
  1106. put_page(pfn_to_page(pfn));
  1107. }
  1108. EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
  1109. void kvm_release_page_dirty(struct page *page)
  1110. {
  1111. WARN_ON(is_error_page(page));
  1112. kvm_release_pfn_dirty(page_to_pfn(page));
  1113. }
  1114. EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
  1115. void kvm_release_pfn_dirty(pfn_t pfn)
  1116. {
  1117. kvm_set_pfn_dirty(pfn);
  1118. kvm_release_pfn_clean(pfn);
  1119. }
  1120. EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
  1121. void kvm_set_page_dirty(struct page *page)
  1122. {
  1123. kvm_set_pfn_dirty(page_to_pfn(page));
  1124. }
  1125. EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
  1126. void kvm_set_pfn_dirty(pfn_t pfn)
  1127. {
  1128. if (!kvm_is_mmio_pfn(pfn)) {
  1129. struct page *page = pfn_to_page(pfn);
  1130. if (!PageReserved(page))
  1131. SetPageDirty(page);
  1132. }
  1133. }
  1134. EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
  1135. void kvm_set_pfn_accessed(pfn_t pfn)
  1136. {
  1137. if (!kvm_is_mmio_pfn(pfn))
  1138. mark_page_accessed(pfn_to_page(pfn));
  1139. }
  1140. EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
  1141. void kvm_get_pfn(pfn_t pfn)
  1142. {
  1143. if (!kvm_is_mmio_pfn(pfn))
  1144. get_page(pfn_to_page(pfn));
  1145. }
  1146. EXPORT_SYMBOL_GPL(kvm_get_pfn);
  1147. static int next_segment(unsigned long len, int offset)
  1148. {
  1149. if (len > PAGE_SIZE - offset)
  1150. return PAGE_SIZE - offset;
  1151. else
  1152. return len;
  1153. }
  1154. int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
  1155. int len)
  1156. {
  1157. int r;
  1158. unsigned long addr;
  1159. addr = gfn_to_hva_read(kvm, gfn);
  1160. if (kvm_is_error_hva(addr))
  1161. return -EFAULT;
  1162. r = kvm_read_hva(data, (void __user *)addr + offset, len);
  1163. if (r)
  1164. return -EFAULT;
  1165. return 0;
  1166. }
  1167. EXPORT_SYMBOL_GPL(kvm_read_guest_page);
  1168. int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
  1169. {
  1170. gfn_t gfn = gpa >> PAGE_SHIFT;
  1171. int seg;
  1172. int offset = offset_in_page(gpa);
  1173. int ret;
  1174. while ((seg = next_segment(len, offset)) != 0) {
  1175. ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
  1176. if (ret < 0)
  1177. return ret;
  1178. offset = 0;
  1179. len -= seg;
  1180. data += seg;
  1181. ++gfn;
  1182. }
  1183. return 0;
  1184. }
  1185. EXPORT_SYMBOL_GPL(kvm_read_guest);
  1186. int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
  1187. unsigned long len)
  1188. {
  1189. int r;
  1190. unsigned long addr;
  1191. gfn_t gfn = gpa >> PAGE_SHIFT;
  1192. int offset = offset_in_page(gpa);
  1193. addr = gfn_to_hva_read(kvm, gfn);
  1194. if (kvm_is_error_hva(addr))
  1195. return -EFAULT;
  1196. pagefault_disable();
  1197. r = kvm_read_hva_atomic(data, (void __user *)addr + offset, len);
  1198. pagefault_enable();
  1199. if (r)
  1200. return -EFAULT;
  1201. return 0;
  1202. }
  1203. EXPORT_SYMBOL(kvm_read_guest_atomic);
  1204. int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
  1205. int offset, int len)
  1206. {
  1207. int r;
  1208. unsigned long addr;
  1209. addr = gfn_to_hva(kvm, gfn);
  1210. if (kvm_is_error_hva(addr))
  1211. return -EFAULT;
  1212. r = __copy_to_user((void __user *)addr + offset, data, len);
  1213. if (r)
  1214. return -EFAULT;
  1215. mark_page_dirty(kvm, gfn);
  1216. return 0;
  1217. }
  1218. EXPORT_SYMBOL_GPL(kvm_write_guest_page);
  1219. int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
  1220. unsigned long len)
  1221. {
  1222. gfn_t gfn = gpa >> PAGE_SHIFT;
  1223. int seg;
  1224. int offset = offset_in_page(gpa);
  1225. int ret;
  1226. while ((seg = next_segment(len, offset)) != 0) {
  1227. ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
  1228. if (ret < 0)
  1229. return ret;
  1230. offset = 0;
  1231. len -= seg;
  1232. data += seg;
  1233. ++gfn;
  1234. }
  1235. return 0;
  1236. }
  1237. int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
  1238. gpa_t gpa)
  1239. {
  1240. struct kvm_memslots *slots = kvm_memslots(kvm);
  1241. int offset = offset_in_page(gpa);
  1242. gfn_t gfn = gpa >> PAGE_SHIFT;
  1243. ghc->gpa = gpa;
  1244. ghc->generation = slots->generation;
  1245. ghc->memslot = gfn_to_memslot(kvm, gfn);
  1246. ghc->hva = gfn_to_hva_many(ghc->memslot, gfn, NULL);
  1247. if (!kvm_is_error_hva(ghc->hva))
  1248. ghc->hva += offset;
  1249. else
  1250. return -EFAULT;
  1251. return 0;
  1252. }
  1253. EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init);
  1254. int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
  1255. void *data, unsigned long len)
  1256. {
  1257. struct kvm_memslots *slots = kvm_memslots(kvm);
  1258. int r;
  1259. if (slots->generation != ghc->generation)
  1260. kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa);
  1261. if (kvm_is_error_hva(ghc->hva))
  1262. return -EFAULT;
  1263. r = __copy_to_user((void __user *)ghc->hva, data, len);
  1264. if (r)
  1265. return -EFAULT;
  1266. mark_page_dirty_in_slot(kvm, ghc->memslot, ghc->gpa >> PAGE_SHIFT);
  1267. return 0;
  1268. }
  1269. EXPORT_SYMBOL_GPL(kvm_write_guest_cached);
  1270. int kvm_read_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
  1271. void *data, unsigned long len)
  1272. {
  1273. struct kvm_memslots *slots = kvm_memslots(kvm);
  1274. int r;
  1275. if (slots->generation != ghc->generation)
  1276. kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa);
  1277. if (kvm_is_error_hva(ghc->hva))
  1278. return -EFAULT;
  1279. r = __copy_from_user(data, (void __user *)ghc->hva, len);
  1280. if (r)
  1281. return -EFAULT;
  1282. return 0;
  1283. }
  1284. EXPORT_SYMBOL_GPL(kvm_read_guest_cached);
  1285. int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
  1286. {
  1287. return kvm_write_guest_page(kvm, gfn, (const void *) empty_zero_page,
  1288. offset, len);
  1289. }
  1290. EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
  1291. int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
  1292. {
  1293. gfn_t gfn = gpa >> PAGE_SHIFT;
  1294. int seg;
  1295. int offset = offset_in_page(gpa);
  1296. int ret;
  1297. while ((seg = next_segment(len, offset)) != 0) {
  1298. ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
  1299. if (ret < 0)
  1300. return ret;
  1301. offset = 0;
  1302. len -= seg;
  1303. ++gfn;
  1304. }
  1305. return 0;
  1306. }
  1307. EXPORT_SYMBOL_GPL(kvm_clear_guest);
  1308. void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot,
  1309. gfn_t gfn)
  1310. {
  1311. if (memslot && memslot->dirty_bitmap) {
  1312. unsigned long rel_gfn = gfn - memslot->base_gfn;
  1313. set_bit_le(rel_gfn, memslot->dirty_bitmap);
  1314. }
  1315. }
  1316. void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
  1317. {
  1318. struct kvm_memory_slot *memslot;
  1319. memslot = gfn_to_memslot(kvm, gfn);
  1320. mark_page_dirty_in_slot(kvm, memslot, gfn);
  1321. }
  1322. /*
  1323. * The vCPU has executed a HLT instruction with in-kernel mode enabled.
  1324. */
  1325. void kvm_vcpu_block(struct kvm_vcpu *vcpu)
  1326. {
  1327. DEFINE_WAIT(wait);
  1328. for (;;) {
  1329. prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
  1330. if (kvm_arch_vcpu_runnable(vcpu)) {
  1331. kvm_make_request(KVM_REQ_UNHALT, vcpu);
  1332. break;
  1333. }
  1334. if (kvm_cpu_has_pending_timer(vcpu))
  1335. break;
  1336. if (signal_pending(current))
  1337. break;
  1338. schedule();
  1339. }
  1340. finish_wait(&vcpu->wq, &wait);
  1341. }
  1342. #ifndef CONFIG_S390
  1343. /*
  1344. * Kick a sleeping VCPU, or a guest VCPU in guest mode, into host kernel mode.
  1345. */
  1346. void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
  1347. {
  1348. int me;
  1349. int cpu = vcpu->cpu;
  1350. wait_queue_head_t *wqp;
  1351. wqp = kvm_arch_vcpu_wq(vcpu);
  1352. if (waitqueue_active(wqp)) {
  1353. wake_up_interruptible(wqp);
  1354. ++vcpu->stat.halt_wakeup;
  1355. }
  1356. me = get_cpu();
  1357. if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
  1358. if (kvm_arch_vcpu_should_kick(vcpu))
  1359. smp_send_reschedule(cpu);
  1360. put_cpu();
  1361. }
  1362. #endif /* !CONFIG_S390 */
  1363. void kvm_resched(struct kvm_vcpu *vcpu)
  1364. {
  1365. if (!need_resched())
  1366. return;
  1367. cond_resched();
  1368. }
  1369. EXPORT_SYMBOL_GPL(kvm_resched);
  1370. bool kvm_vcpu_yield_to(struct kvm_vcpu *target)
  1371. {
  1372. struct pid *pid;
  1373. struct task_struct *task = NULL;
  1374. rcu_read_lock();
  1375. pid = rcu_dereference(target->pid);
  1376. if (pid)
  1377. task = get_pid_task(target->pid, PIDTYPE_PID);
  1378. rcu_read_unlock();
  1379. if (!task)
  1380. return false;
  1381. if (task->flags & PF_VCPU) {
  1382. put_task_struct(task);
  1383. return false;
  1384. }
  1385. if (yield_to(task, 1)) {
  1386. put_task_struct(task);
  1387. return true;
  1388. }
  1389. put_task_struct(task);
  1390. return false;
  1391. }
  1392. EXPORT_SYMBOL_GPL(kvm_vcpu_yield_to);
  1393. #ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
  1394. /*
  1395. * Helper that checks whether a VCPU is eligible for directed yield.
  1396. * Most eligible candidate to yield is decided by following heuristics:
  1397. *
  1398. * (a) VCPU which has not done pl-exit or cpu relax intercepted recently
  1399. * (preempted lock holder), indicated by @in_spin_loop.
  1400. * Set at the beiginning and cleared at the end of interception/PLE handler.
  1401. *
  1402. * (b) VCPU which has done pl-exit/ cpu relax intercepted but did not get
  1403. * chance last time (mostly it has become eligible now since we have probably
  1404. * yielded to lockholder in last iteration. This is done by toggling
  1405. * @dy_eligible each time a VCPU checked for eligibility.)
  1406. *
  1407. * Yielding to a recently pl-exited/cpu relax intercepted VCPU before yielding
  1408. * to preempted lock-holder could result in wrong VCPU selection and CPU
  1409. * burning. Giving priority for a potential lock-holder increases lock
  1410. * progress.
  1411. *
  1412. * Since algorithm is based on heuristics, accessing another VCPU data without
  1413. * locking does not harm. It may result in trying to yield to same VCPU, fail
  1414. * and continue with next VCPU and so on.
  1415. */
  1416. bool kvm_vcpu_eligible_for_directed_yield(struct kvm_vcpu *vcpu)
  1417. {
  1418. bool eligible;
  1419. eligible = !vcpu->spin_loop.in_spin_loop ||
  1420. (vcpu->spin_loop.in_spin_loop &&
  1421. vcpu->spin_loop.dy_eligible);
  1422. if (vcpu->spin_loop.in_spin_loop)
  1423. kvm_vcpu_set_dy_eligible(vcpu, !vcpu->spin_loop.dy_eligible);
  1424. return eligible;
  1425. }
  1426. #endif
  1427. void kvm_vcpu_on_spin(struct kvm_vcpu *me)
  1428. {
  1429. struct kvm *kvm = me->kvm;
  1430. struct kvm_vcpu *vcpu;
  1431. int last_boosted_vcpu = me->kvm->last_boosted_vcpu;
  1432. int yielded = 0;
  1433. int pass;
  1434. int i;
  1435. kvm_vcpu_set_in_spin_loop(me, true);
  1436. /*
  1437. * We boost the priority of a VCPU that is runnable but not
  1438. * currently running, because it got preempted by something
  1439. * else and called schedule in __vcpu_run. Hopefully that
  1440. * VCPU is holding the lock that we need and will release it.
  1441. * We approximate round-robin by starting at the last boosted VCPU.
  1442. */
  1443. for (pass = 0; pass < 2 && !yielded; pass++) {
  1444. kvm_for_each_vcpu(i, vcpu, kvm) {
  1445. if (!pass && i <= last_boosted_vcpu) {
  1446. i = last_boosted_vcpu;
  1447. continue;
  1448. } else if (pass && i > last_boosted_vcpu)
  1449. break;
  1450. if (vcpu == me)
  1451. continue;
  1452. if (waitqueue_active(&vcpu->wq))
  1453. continue;
  1454. if (!kvm_vcpu_eligible_for_directed_yield(vcpu))
  1455. continue;
  1456. if (kvm_vcpu_yield_to(vcpu)) {
  1457. kvm->last_boosted_vcpu = i;
  1458. yielded = 1;
  1459. break;
  1460. }
  1461. }
  1462. }
  1463. kvm_vcpu_set_in_spin_loop(me, false);
  1464. /* Ensure vcpu is not eligible during next spinloop */
  1465. kvm_vcpu_set_dy_eligible(me, false);
  1466. }
  1467. EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
  1468. static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1469. {
  1470. struct kvm_vcpu *vcpu = vma->vm_file->private_data;
  1471. struct page *page;
  1472. if (vmf->pgoff == 0)
  1473. page = virt_to_page(vcpu->run);
  1474. #ifdef CONFIG_X86
  1475. else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
  1476. page = virt_to_page(vcpu->arch.pio_data);
  1477. #endif
  1478. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  1479. else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
  1480. page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
  1481. #endif
  1482. else
  1483. return kvm_arch_vcpu_fault(vcpu, vmf);
  1484. get_page(page);
  1485. vmf->page = page;
  1486. return 0;
  1487. }
  1488. static const struct vm_operations_struct kvm_vcpu_vm_ops = {
  1489. .fault = kvm_vcpu_fault,
  1490. };
  1491. static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
  1492. {
  1493. vma->vm_ops = &kvm_vcpu_vm_ops;
  1494. return 0;
  1495. }
  1496. static int kvm_vcpu_release(struct inode *inode, struct file *filp)
  1497. {
  1498. struct kvm_vcpu *vcpu = filp->private_data;
  1499. kvm_put_kvm(vcpu->kvm);
  1500. return 0;
  1501. }
  1502. static struct file_operations kvm_vcpu_fops = {
  1503. .release = kvm_vcpu_release,
  1504. .unlocked_ioctl = kvm_vcpu_ioctl,
  1505. #ifdef CONFIG_COMPAT
  1506. .compat_ioctl = kvm_vcpu_compat_ioctl,
  1507. #endif
  1508. .mmap = kvm_vcpu_mmap,
  1509. .llseek = noop_llseek,
  1510. };
  1511. /*
  1512. * Allocates an inode for the vcpu.
  1513. */
  1514. static int create_vcpu_fd(struct kvm_vcpu *vcpu)
  1515. {
  1516. return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR);
  1517. }
  1518. /*
  1519. * Creates some virtual cpus. Good luck creating more than one.
  1520. */
  1521. static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
  1522. {
  1523. int r;
  1524. struct kvm_vcpu *vcpu, *v;
  1525. vcpu = kvm_arch_vcpu_create(kvm, id);
  1526. if (IS_ERR(vcpu))
  1527. return PTR_ERR(vcpu);
  1528. preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
  1529. r = kvm_arch_vcpu_setup(vcpu);
  1530. if (r)
  1531. goto vcpu_destroy;
  1532. mutex_lock(&kvm->lock);
  1533. if (!kvm_vcpu_compatible(vcpu)) {
  1534. r = -EINVAL;
  1535. goto unlock_vcpu_destroy;
  1536. }
  1537. if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
  1538. r = -EINVAL;
  1539. goto unlock_vcpu_destroy;
  1540. }
  1541. kvm_for_each_vcpu(r, v, kvm)
  1542. if (v->vcpu_id == id) {
  1543. r = -EEXIST;
  1544. goto unlock_vcpu_destroy;
  1545. }
  1546. BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
  1547. /* Now it's all set up, let userspace reach it */
  1548. kvm_get_kvm(kvm);
  1549. r = create_vcpu_fd(vcpu);
  1550. if (r < 0) {
  1551. kvm_put_kvm(kvm);
  1552. goto unlock_vcpu_destroy;
  1553. }
  1554. kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
  1555. smp_wmb();
  1556. atomic_inc(&kvm->online_vcpus);
  1557. mutex_unlock(&kvm->lock);
  1558. kvm_arch_vcpu_postcreate(vcpu);
  1559. return r;
  1560. unlock_vcpu_destroy:
  1561. mutex_unlock(&kvm->lock);
  1562. vcpu_destroy:
  1563. kvm_arch_vcpu_destroy(vcpu);
  1564. return r;
  1565. }
  1566. static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
  1567. {
  1568. if (sigset) {
  1569. sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
  1570. vcpu->sigset_active = 1;
  1571. vcpu->sigset = *sigset;
  1572. } else
  1573. vcpu->sigset_active = 0;
  1574. return 0;
  1575. }
  1576. static long kvm_vcpu_ioctl(struct file *filp,
  1577. unsigned int ioctl, unsigned long arg)
  1578. {
  1579. struct kvm_vcpu *vcpu = filp->private_data;
  1580. void __user *argp = (void __user *)arg;
  1581. int r;
  1582. struct kvm_fpu *fpu = NULL;
  1583. struct kvm_sregs *kvm_sregs = NULL;
  1584. if (vcpu->kvm->mm != current->mm)
  1585. return -EIO;
  1586. #if defined(CONFIG_S390) || defined(CONFIG_PPC)
  1587. /*
  1588. * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
  1589. * so vcpu_load() would break it.
  1590. */
  1591. if (ioctl == KVM_S390_INTERRUPT || ioctl == KVM_INTERRUPT)
  1592. return kvm_arch_vcpu_ioctl(filp, ioctl, arg);
  1593. #endif
  1594. r = vcpu_load(vcpu);
  1595. if (r)
  1596. return r;
  1597. switch (ioctl) {
  1598. case KVM_RUN:
  1599. r = -EINVAL;
  1600. if (arg)
  1601. goto out;
  1602. r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
  1603. trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
  1604. break;
  1605. case KVM_GET_REGS: {
  1606. struct kvm_regs *kvm_regs;
  1607. r = -ENOMEM;
  1608. kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
  1609. if (!kvm_regs)
  1610. goto out;
  1611. r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
  1612. if (r)
  1613. goto out_free1;
  1614. r = -EFAULT;
  1615. if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
  1616. goto out_free1;
  1617. r = 0;
  1618. out_free1:
  1619. kfree(kvm_regs);
  1620. break;
  1621. }
  1622. case KVM_SET_REGS: {
  1623. struct kvm_regs *kvm_regs;
  1624. r = -ENOMEM;
  1625. kvm_regs = memdup_user(argp, sizeof(*kvm_regs));
  1626. if (IS_ERR(kvm_regs)) {
  1627. r = PTR_ERR(kvm_regs);
  1628. goto out;
  1629. }
  1630. r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
  1631. kfree(kvm_regs);
  1632. break;
  1633. }
  1634. case KVM_GET_SREGS: {
  1635. kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
  1636. r = -ENOMEM;
  1637. if (!kvm_sregs)
  1638. goto out;
  1639. r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
  1640. if (r)
  1641. goto out;
  1642. r = -EFAULT;
  1643. if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
  1644. goto out;
  1645. r = 0;
  1646. break;
  1647. }
  1648. case KVM_SET_SREGS: {
  1649. kvm_sregs = memdup_user(argp, sizeof(*kvm_sregs));
  1650. if (IS_ERR(kvm_sregs)) {
  1651. r = PTR_ERR(kvm_sregs);
  1652. kvm_sregs = NULL;
  1653. goto out;
  1654. }
  1655. r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
  1656. break;
  1657. }
  1658. case KVM_GET_MP_STATE: {
  1659. struct kvm_mp_state mp_state;
  1660. r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
  1661. if (r)
  1662. goto out;
  1663. r = -EFAULT;
  1664. if (copy_to_user(argp, &mp_state, sizeof mp_state))
  1665. goto out;
  1666. r = 0;
  1667. break;
  1668. }
  1669. case KVM_SET_MP_STATE: {
  1670. struct kvm_mp_state mp_state;
  1671. r = -EFAULT;
  1672. if (copy_from_user(&mp_state, argp, sizeof mp_state))
  1673. goto out;
  1674. r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
  1675. break;
  1676. }
  1677. case KVM_TRANSLATE: {
  1678. struct kvm_translation tr;
  1679. r = -EFAULT;
  1680. if (copy_from_user(&tr, argp, sizeof tr))
  1681. goto out;
  1682. r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
  1683. if (r)
  1684. goto out;
  1685. r = -EFAULT;
  1686. if (copy_to_user(argp, &tr, sizeof tr))
  1687. goto out;
  1688. r = 0;
  1689. break;
  1690. }
  1691. case KVM_SET_GUEST_DEBUG: {
  1692. struct kvm_guest_debug dbg;
  1693. r = -EFAULT;
  1694. if (copy_from_user(&dbg, argp, sizeof dbg))
  1695. goto out;
  1696. r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
  1697. break;
  1698. }
  1699. case KVM_SET_SIGNAL_MASK: {
  1700. struct kvm_signal_mask __user *sigmask_arg = argp;
  1701. struct kvm_signal_mask kvm_sigmask;
  1702. sigset_t sigset, *p;
  1703. p = NULL;
  1704. if (argp) {
  1705. r = -EFAULT;
  1706. if (copy_from_user(&kvm_sigmask, argp,
  1707. sizeof kvm_sigmask))
  1708. goto out;
  1709. r = -EINVAL;
  1710. if (kvm_sigmask.len != sizeof sigset)
  1711. goto out;
  1712. r = -EFAULT;
  1713. if (copy_from_user(&sigset, sigmask_arg->sigset,
  1714. sizeof sigset))
  1715. goto out;
  1716. p = &sigset;
  1717. }
  1718. r = kvm_vcpu_ioctl_set_sigmask(vcpu, p);
  1719. break;
  1720. }
  1721. case KVM_GET_FPU: {
  1722. fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
  1723. r = -ENOMEM;
  1724. if (!fpu)
  1725. goto out;
  1726. r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
  1727. if (r)
  1728. goto out;
  1729. r = -EFAULT;
  1730. if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
  1731. goto out;
  1732. r = 0;
  1733. break;
  1734. }
  1735. case KVM_SET_FPU: {
  1736. fpu = memdup_user(argp, sizeof(*fpu));
  1737. if (IS_ERR(fpu)) {
  1738. r = PTR_ERR(fpu);
  1739. fpu = NULL;
  1740. goto out;
  1741. }
  1742. r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
  1743. break;
  1744. }
  1745. default:
  1746. r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
  1747. }
  1748. out:
  1749. vcpu_put(vcpu);
  1750. kfree(fpu);
  1751. kfree(kvm_sregs);
  1752. return r;
  1753. }
  1754. #ifdef CONFIG_COMPAT
  1755. static long kvm_vcpu_compat_ioctl(struct file *filp,
  1756. unsigned int ioctl, unsigned long arg)
  1757. {
  1758. struct kvm_vcpu *vcpu = filp->private_data;
  1759. void __user *argp = compat_ptr(arg);
  1760. int r;
  1761. if (vcpu->kvm->mm != current->mm)
  1762. return -EIO;
  1763. switch (ioctl) {
  1764. case KVM_SET_SIGNAL_MASK: {
  1765. struct kvm_signal_mask __user *sigmask_arg = argp;
  1766. struct kvm_signal_mask kvm_sigmask;
  1767. compat_sigset_t csigset;
  1768. sigset_t sigset;
  1769. if (argp) {
  1770. r = -EFAULT;
  1771. if (copy_from_user(&kvm_sigmask, argp,
  1772. sizeof kvm_sigmask))
  1773. goto out;
  1774. r = -EINVAL;
  1775. if (kvm_sigmask.len != sizeof csigset)
  1776. goto out;
  1777. r = -EFAULT;
  1778. if (copy_from_user(&csigset, sigmask_arg->sigset,
  1779. sizeof csigset))
  1780. goto out;
  1781. sigset_from_compat(&sigset, &csigset);
  1782. r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
  1783. } else
  1784. r = kvm_vcpu_ioctl_set_sigmask(vcpu, NULL);
  1785. break;
  1786. }
  1787. default:
  1788. r = kvm_vcpu_ioctl(filp, ioctl, arg);
  1789. }
  1790. out:
  1791. return r;
  1792. }
  1793. #endif
  1794. static long kvm_vm_ioctl(struct file *filp,
  1795. unsigned int ioctl, unsigned long arg)
  1796. {
  1797. struct kvm *kvm = filp->private_data;
  1798. void __user *argp = (void __user *)arg;
  1799. int r;
  1800. if (kvm->mm != current->mm)
  1801. return -EIO;
  1802. switch (ioctl) {
  1803. case KVM_CREATE_VCPU:
  1804. r = kvm_vm_ioctl_create_vcpu(kvm, arg);
  1805. break;
  1806. case KVM_SET_USER_MEMORY_REGION: {
  1807. struct kvm_userspace_memory_region kvm_userspace_mem;
  1808. r = -EFAULT;
  1809. if (copy_from_user(&kvm_userspace_mem, argp,
  1810. sizeof kvm_userspace_mem))
  1811. goto out;
  1812. r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
  1813. break;
  1814. }
  1815. case KVM_GET_DIRTY_LOG: {
  1816. struct kvm_dirty_log log;
  1817. r = -EFAULT;
  1818. if (copy_from_user(&log, argp, sizeof log))
  1819. goto out;
  1820. r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
  1821. break;
  1822. }
  1823. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  1824. case KVM_REGISTER_COALESCED_MMIO: {
  1825. struct kvm_coalesced_mmio_zone zone;
  1826. r = -EFAULT;
  1827. if (copy_from_user(&zone, argp, sizeof zone))
  1828. goto out;
  1829. r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
  1830. break;
  1831. }
  1832. case KVM_UNREGISTER_COALESCED_MMIO: {
  1833. struct kvm_coalesced_mmio_zone zone;
  1834. r = -EFAULT;
  1835. if (copy_from_user(&zone, argp, sizeof zone))
  1836. goto out;
  1837. r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
  1838. break;
  1839. }
  1840. #endif
  1841. case KVM_IRQFD: {
  1842. struct kvm_irqfd data;
  1843. r = -EFAULT;
  1844. if (copy_from_user(&data, argp, sizeof data))
  1845. goto out;
  1846. r = kvm_irqfd(kvm, &data);
  1847. break;
  1848. }
  1849. case KVM_IOEVENTFD: {
  1850. struct kvm_ioeventfd data;
  1851. r = -EFAULT;
  1852. if (copy_from_user(&data, argp, sizeof data))
  1853. goto out;
  1854. r = kvm_ioeventfd(kvm, &data);
  1855. break;
  1856. }
  1857. #ifdef CONFIG_KVM_APIC_ARCHITECTURE
  1858. case KVM_SET_BOOT_CPU_ID:
  1859. r = 0;
  1860. mutex_lock(&kvm->lock);
  1861. if (atomic_read(&kvm->online_vcpus) != 0)
  1862. r = -EBUSY;
  1863. else
  1864. kvm->bsp_vcpu_id = arg;
  1865. mutex_unlock(&kvm->lock);
  1866. break;
  1867. #endif
  1868. #ifdef CONFIG_HAVE_KVM_MSI
  1869. case KVM_SIGNAL_MSI: {
  1870. struct kvm_msi msi;
  1871. r = -EFAULT;
  1872. if (copy_from_user(&msi, argp, sizeof msi))
  1873. goto out;
  1874. r = kvm_send_userspace_msi(kvm, &msi);
  1875. break;
  1876. }
  1877. #endif
  1878. #ifdef __KVM_HAVE_IRQ_LINE
  1879. case KVM_IRQ_LINE_STATUS:
  1880. case KVM_IRQ_LINE: {
  1881. struct kvm_irq_level irq_event;
  1882. r = -EFAULT;
  1883. if (copy_from_user(&irq_event, argp, sizeof irq_event))
  1884. goto out;
  1885. r = kvm_vm_ioctl_irq_line(kvm, &irq_event);
  1886. if (r)
  1887. goto out;
  1888. r = -EFAULT;
  1889. if (ioctl == KVM_IRQ_LINE_STATUS) {
  1890. if (copy_to_user(argp, &irq_event, sizeof irq_event))
  1891. goto out;
  1892. }
  1893. r = 0;
  1894. break;
  1895. }
  1896. #endif
  1897. default:
  1898. r = kvm_arch_vm_ioctl(filp, ioctl, arg);
  1899. if (r == -ENOTTY)
  1900. r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
  1901. }
  1902. out:
  1903. return r;
  1904. }
  1905. #ifdef CONFIG_COMPAT
  1906. struct compat_kvm_dirty_log {
  1907. __u32 slot;
  1908. __u32 padding1;
  1909. union {
  1910. compat_uptr_t dirty_bitmap; /* one bit per page */
  1911. __u64 padding2;
  1912. };
  1913. };
  1914. static long kvm_vm_compat_ioctl(struct file *filp,
  1915. unsigned int ioctl, unsigned long arg)
  1916. {
  1917. struct kvm *kvm = filp->private_data;
  1918. int r;
  1919. if (kvm->mm != current->mm)
  1920. return -EIO;
  1921. switch (ioctl) {
  1922. case KVM_GET_DIRTY_LOG: {
  1923. struct compat_kvm_dirty_log compat_log;
  1924. struct kvm_dirty_log log;
  1925. r = -EFAULT;
  1926. if (copy_from_user(&compat_log, (void __user *)arg,
  1927. sizeof(compat_log)))
  1928. goto out;
  1929. log.slot = compat_log.slot;
  1930. log.padding1 = compat_log.padding1;
  1931. log.padding2 = compat_log.padding2;
  1932. log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
  1933. r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
  1934. break;
  1935. }
  1936. default:
  1937. r = kvm_vm_ioctl(filp, ioctl, arg);
  1938. }
  1939. out:
  1940. return r;
  1941. }
  1942. #endif
  1943. static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1944. {
  1945. struct page *page[1];
  1946. unsigned long addr;
  1947. int npages;
  1948. gfn_t gfn = vmf->pgoff;
  1949. struct kvm *kvm = vma->vm_file->private_data;
  1950. addr = gfn_to_hva(kvm, gfn);
  1951. if (kvm_is_error_hva(addr))
  1952. return VM_FAULT_SIGBUS;
  1953. npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
  1954. NULL);
  1955. if (unlikely(npages != 1))
  1956. return VM_FAULT_SIGBUS;
  1957. vmf->page = page[0];
  1958. return 0;
  1959. }
  1960. static const struct vm_operations_struct kvm_vm_vm_ops = {
  1961. .fault = kvm_vm_fault,
  1962. };
  1963. static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
  1964. {
  1965. vma->vm_ops = &kvm_vm_vm_ops;
  1966. return 0;
  1967. }
  1968. static struct file_operations kvm_vm_fops = {
  1969. .release = kvm_vm_release,
  1970. .unlocked_ioctl = kvm_vm_ioctl,
  1971. #ifdef CONFIG_COMPAT
  1972. .compat_ioctl = kvm_vm_compat_ioctl,
  1973. #endif
  1974. .mmap = kvm_vm_mmap,
  1975. .llseek = noop_llseek,
  1976. };
  1977. static int kvm_dev_ioctl_create_vm(unsigned long type)
  1978. {
  1979. int r;
  1980. struct kvm *kvm;
  1981. kvm = kvm_create_vm(type);
  1982. if (IS_ERR(kvm))
  1983. return PTR_ERR(kvm);
  1984. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  1985. r = kvm_coalesced_mmio_init(kvm);
  1986. if (r < 0) {
  1987. kvm_put_kvm(kvm);
  1988. return r;
  1989. }
  1990. #endif
  1991. r = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
  1992. if (r < 0)
  1993. kvm_put_kvm(kvm);
  1994. return r;
  1995. }
  1996. static long kvm_dev_ioctl_check_extension_generic(long arg)
  1997. {
  1998. switch (arg) {
  1999. case KVM_CAP_USER_MEMORY:
  2000. case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
  2001. case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
  2002. #ifdef CONFIG_KVM_APIC_ARCHITECTURE
  2003. case KVM_CAP_SET_BOOT_CPU_ID:
  2004. #endif
  2005. case KVM_CAP_INTERNAL_ERROR_DATA:
  2006. #ifdef CONFIG_HAVE_KVM_MSI
  2007. case KVM_CAP_SIGNAL_MSI:
  2008. #endif
  2009. return 1;
  2010. #ifdef KVM_CAP_IRQ_ROUTING
  2011. case KVM_CAP_IRQ_ROUTING:
  2012. return KVM_MAX_IRQ_ROUTES;
  2013. #endif
  2014. default:
  2015. break;
  2016. }
  2017. return kvm_dev_ioctl_check_extension(arg);
  2018. }
  2019. static long kvm_dev_ioctl(struct file *filp,
  2020. unsigned int ioctl, unsigned long arg)
  2021. {
  2022. long r = -EINVAL;
  2023. switch (ioctl) {
  2024. case KVM_GET_API_VERSION:
  2025. r = -EINVAL;
  2026. if (arg)
  2027. goto out;
  2028. r = KVM_API_VERSION;
  2029. break;
  2030. case KVM_CREATE_VM:
  2031. r = kvm_dev_ioctl_create_vm(arg);
  2032. break;
  2033. case KVM_CHECK_EXTENSION:
  2034. r = kvm_dev_ioctl_check_extension_generic(arg);
  2035. break;
  2036. case KVM_GET_VCPU_MMAP_SIZE:
  2037. r = -EINVAL;
  2038. if (arg)
  2039. goto out;
  2040. r = PAGE_SIZE; /* struct kvm_run */
  2041. #ifdef CONFIG_X86
  2042. r += PAGE_SIZE; /* pio data page */
  2043. #endif
  2044. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  2045. r += PAGE_SIZE; /* coalesced mmio ring page */
  2046. #endif
  2047. break;
  2048. case KVM_TRACE_ENABLE:
  2049. case KVM_TRACE_PAUSE:
  2050. case KVM_TRACE_DISABLE:
  2051. r = -EOPNOTSUPP;
  2052. break;
  2053. default:
  2054. return kvm_arch_dev_ioctl(filp, ioctl, arg);
  2055. }
  2056. out:
  2057. return r;
  2058. }
  2059. static struct file_operations kvm_chardev_ops = {
  2060. .unlocked_ioctl = kvm_dev_ioctl,
  2061. .compat_ioctl = kvm_dev_ioctl,
  2062. .llseek = noop_llseek,
  2063. };
  2064. static struct miscdevice kvm_dev = {
  2065. KVM_MINOR,
  2066. "kvm",
  2067. &kvm_chardev_ops,
  2068. };
  2069. static void hardware_enable_nolock(void *junk)
  2070. {
  2071. int cpu = raw_smp_processor_id();
  2072. int r;
  2073. if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
  2074. return;
  2075. cpumask_set_cpu(cpu, cpus_hardware_enabled);
  2076. r = kvm_arch_hardware_enable(NULL);
  2077. if (r) {
  2078. cpumask_clear_cpu(cpu, cpus_hardware_enabled);
  2079. atomic_inc(&hardware_enable_failed);
  2080. printk(KERN_INFO "kvm: enabling virtualization on "
  2081. "CPU%d failed\n", cpu);
  2082. }
  2083. }
  2084. static void hardware_enable(void *junk)
  2085. {
  2086. raw_spin_lock(&kvm_lock);
  2087. hardware_enable_nolock(junk);
  2088. raw_spin_unlock(&kvm_lock);
  2089. }
  2090. static void hardware_disable_nolock(void *junk)
  2091. {
  2092. int cpu = raw_smp_processor_id();
  2093. if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
  2094. return;
  2095. cpumask_clear_cpu(cpu, cpus_hardware_enabled);
  2096. kvm_arch_hardware_disable(NULL);
  2097. }
  2098. static void hardware_disable(void *junk)
  2099. {
  2100. raw_spin_lock(&kvm_lock);
  2101. hardware_disable_nolock(junk);
  2102. raw_spin_unlock(&kvm_lock);
  2103. }
  2104. static void hardware_disable_all_nolock(void)
  2105. {
  2106. BUG_ON(!kvm_usage_count);
  2107. kvm_usage_count--;
  2108. if (!kvm_usage_count)
  2109. on_each_cpu(hardware_disable_nolock, NULL, 1);
  2110. }
  2111. static void hardware_disable_all(void)
  2112. {
  2113. raw_spin_lock(&kvm_lock);
  2114. hardware_disable_all_nolock();
  2115. raw_spin_unlock(&kvm_lock);
  2116. }
  2117. static int hardware_enable_all(void)
  2118. {
  2119. int r = 0;
  2120. raw_spin_lock(&kvm_lock);
  2121. kvm_usage_count++;
  2122. if (kvm_usage_count == 1) {
  2123. atomic_set(&hardware_enable_failed, 0);
  2124. on_each_cpu(hardware_enable_nolock, NULL, 1);
  2125. if (atomic_read(&hardware_enable_failed)) {
  2126. hardware_disable_all_nolock();
  2127. r = -EBUSY;
  2128. }
  2129. }
  2130. raw_spin_unlock(&kvm_lock);
  2131. return r;
  2132. }
  2133. static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
  2134. void *v)
  2135. {
  2136. int cpu = (long)v;
  2137. if (!kvm_usage_count)
  2138. return NOTIFY_OK;
  2139. val &= ~CPU_TASKS_FROZEN;
  2140. switch (val) {
  2141. case CPU_DYING:
  2142. printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
  2143. cpu);
  2144. hardware_disable(NULL);
  2145. break;
  2146. case CPU_STARTING:
  2147. printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
  2148. cpu);
  2149. hardware_enable(NULL);
  2150. break;
  2151. }
  2152. return NOTIFY_OK;
  2153. }
  2154. asmlinkage void kvm_spurious_fault(void)
  2155. {
  2156. /* Fault while not rebooting. We want the trace. */
  2157. BUG();
  2158. }
  2159. EXPORT_SYMBOL_GPL(kvm_spurious_fault);
  2160. static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
  2161. void *v)
  2162. {
  2163. /*
  2164. * Some (well, at least mine) BIOSes hang on reboot if
  2165. * in vmx root mode.
  2166. *
  2167. * And Intel TXT required VMX off for all cpu when system shutdown.
  2168. */
  2169. printk(KERN_INFO "kvm: exiting hardware virtualization\n");
  2170. kvm_rebooting = true;
  2171. on_each_cpu(hardware_disable_nolock, NULL, 1);
  2172. return NOTIFY_OK;
  2173. }
  2174. static struct notifier_block kvm_reboot_notifier = {
  2175. .notifier_call = kvm_reboot,
  2176. .priority = 0,
  2177. };
  2178. static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
  2179. {
  2180. int i;
  2181. for (i = 0; i < bus->dev_count; i++) {
  2182. struct kvm_io_device *pos = bus->range[i].dev;
  2183. kvm_iodevice_destructor(pos);
  2184. }
  2185. kfree(bus);
  2186. }
  2187. int kvm_io_bus_sort_cmp(const void *p1, const void *p2)
  2188. {
  2189. const struct kvm_io_range *r1 = p1;
  2190. const struct kvm_io_range *r2 = p2;
  2191. if (r1->addr < r2->addr)
  2192. return -1;
  2193. if (r1->addr + r1->len > r2->addr + r2->len)
  2194. return 1;
  2195. return 0;
  2196. }
  2197. int kvm_io_bus_insert_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev,
  2198. gpa_t addr, int len)
  2199. {
  2200. bus->range[bus->dev_count++] = (struct kvm_io_range) {
  2201. .addr = addr,
  2202. .len = len,
  2203. .dev = dev,
  2204. };
  2205. sort(bus->range, bus->dev_count, sizeof(struct kvm_io_range),
  2206. kvm_io_bus_sort_cmp, NULL);
  2207. return 0;
  2208. }
  2209. int kvm_io_bus_get_first_dev(struct kvm_io_bus *bus,
  2210. gpa_t addr, int len)
  2211. {
  2212. struct kvm_io_range *range, key;
  2213. int off;
  2214. key = (struct kvm_io_range) {
  2215. .addr = addr,
  2216. .len = len,
  2217. };
  2218. range = bsearch(&key, bus->range, bus->dev_count,
  2219. sizeof(struct kvm_io_range), kvm_io_bus_sort_cmp);
  2220. if (range == NULL)
  2221. return -ENOENT;
  2222. off = range - bus->range;
  2223. while (off > 0 && kvm_io_bus_sort_cmp(&key, &bus->range[off-1]) == 0)
  2224. off--;
  2225. return off;
  2226. }
  2227. /* kvm_io_bus_write - called under kvm->slots_lock */
  2228. int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
  2229. int len, const void *val)
  2230. {
  2231. int idx;
  2232. struct kvm_io_bus *bus;
  2233. struct kvm_io_range range;
  2234. range = (struct kvm_io_range) {
  2235. .addr = addr,
  2236. .len = len,
  2237. };
  2238. bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
  2239. idx = kvm_io_bus_get_first_dev(bus, addr, len);
  2240. if (idx < 0)
  2241. return -EOPNOTSUPP;
  2242. while (idx < bus->dev_count &&
  2243. kvm_io_bus_sort_cmp(&range, &bus->range[idx]) == 0) {
  2244. if (!kvm_iodevice_write(bus->range[idx].dev, addr, len, val))
  2245. return 0;
  2246. idx++;
  2247. }
  2248. return -EOPNOTSUPP;
  2249. }
  2250. /* kvm_io_bus_read - called under kvm->slots_lock */
  2251. int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
  2252. int len, void *val)
  2253. {
  2254. int idx;
  2255. struct kvm_io_bus *bus;
  2256. struct kvm_io_range range;
  2257. range = (struct kvm_io_range) {
  2258. .addr = addr,
  2259. .len = len,
  2260. };
  2261. bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
  2262. idx = kvm_io_bus_get_first_dev(bus, addr, len);
  2263. if (idx < 0)
  2264. return -EOPNOTSUPP;
  2265. while (idx < bus->dev_count &&
  2266. kvm_io_bus_sort_cmp(&range, &bus->range[idx]) == 0) {
  2267. if (!kvm_iodevice_read(bus->range[idx].dev, addr, len, val))
  2268. return 0;
  2269. idx++;
  2270. }
  2271. return -EOPNOTSUPP;
  2272. }
  2273. /* Caller must hold slots_lock. */
  2274. int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
  2275. int len, struct kvm_io_device *dev)
  2276. {
  2277. struct kvm_io_bus *new_bus, *bus;
  2278. bus = kvm->buses[bus_idx];
  2279. if (bus->dev_count > NR_IOBUS_DEVS - 1)
  2280. return -ENOSPC;
  2281. new_bus = kzalloc(sizeof(*bus) + ((bus->dev_count + 1) *
  2282. sizeof(struct kvm_io_range)), GFP_KERNEL);
  2283. if (!new_bus)
  2284. return -ENOMEM;
  2285. memcpy(new_bus, bus, sizeof(*bus) + (bus->dev_count *
  2286. sizeof(struct kvm_io_range)));
  2287. kvm_io_bus_insert_dev(new_bus, dev, addr, len);
  2288. rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
  2289. synchronize_srcu_expedited(&kvm->srcu);
  2290. kfree(bus);
  2291. return 0;
  2292. }
  2293. /* Caller must hold slots_lock. */
  2294. int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
  2295. struct kvm_io_device *dev)
  2296. {
  2297. int i, r;
  2298. struct kvm_io_bus *new_bus, *bus;
  2299. bus = kvm->buses[bus_idx];
  2300. r = -ENOENT;
  2301. for (i = 0; i < bus->dev_count; i++)
  2302. if (bus->range[i].dev == dev) {
  2303. r = 0;
  2304. break;
  2305. }
  2306. if (r)
  2307. return r;
  2308. new_bus = kzalloc(sizeof(*bus) + ((bus->dev_count - 1) *
  2309. sizeof(struct kvm_io_range)), GFP_KERNEL);
  2310. if (!new_bus)
  2311. return -ENOMEM;
  2312. memcpy(new_bus, bus, sizeof(*bus) + i * sizeof(struct kvm_io_range));
  2313. new_bus->dev_count--;
  2314. memcpy(new_bus->range + i, bus->range + i + 1,
  2315. (new_bus->dev_count - i) * sizeof(struct kvm_io_range));
  2316. rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
  2317. synchronize_srcu_expedited(&kvm->srcu);
  2318. kfree(bus);
  2319. return r;
  2320. }
  2321. static struct notifier_block kvm_cpu_notifier = {
  2322. .notifier_call = kvm_cpu_hotplug,
  2323. };
  2324. static int vm_stat_get(void *_offset, u64 *val)
  2325. {
  2326. unsigned offset = (long)_offset;
  2327. struct kvm *kvm;
  2328. *val = 0;
  2329. raw_spin_lock(&kvm_lock);
  2330. list_for_each_entry(kvm, &vm_list, vm_list)
  2331. *val += *(u32 *)((void *)kvm + offset);
  2332. raw_spin_unlock(&kvm_lock);
  2333. return 0;
  2334. }
  2335. DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
  2336. static int vcpu_stat_get(void *_offset, u64 *val)
  2337. {
  2338. unsigned offset = (long)_offset;
  2339. struct kvm *kvm;
  2340. struct kvm_vcpu *vcpu;
  2341. int i;
  2342. *val = 0;
  2343. raw_spin_lock(&kvm_lock);
  2344. list_for_each_entry(kvm, &vm_list, vm_list)
  2345. kvm_for_each_vcpu(i, vcpu, kvm)
  2346. *val += *(u32 *)((void *)vcpu + offset);
  2347. raw_spin_unlock(&kvm_lock);
  2348. return 0;
  2349. }
  2350. DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
  2351. static const struct file_operations *stat_fops[] = {
  2352. [KVM_STAT_VCPU] = &vcpu_stat_fops,
  2353. [KVM_STAT_VM] = &vm_stat_fops,
  2354. };
  2355. static int kvm_init_debug(void)
  2356. {
  2357. int r = -EFAULT;
  2358. struct kvm_stats_debugfs_item *p;
  2359. kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
  2360. if (kvm_debugfs_dir == NULL)
  2361. goto out;
  2362. for (p = debugfs_entries; p->name; ++p) {
  2363. p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
  2364. (void *)(long)p->offset,
  2365. stat_fops[p->kind]);
  2366. if (p->dentry == NULL)
  2367. goto out_dir;
  2368. }
  2369. return 0;
  2370. out_dir:
  2371. debugfs_remove_recursive(kvm_debugfs_dir);
  2372. out:
  2373. return r;
  2374. }
  2375. static void kvm_exit_debug(void)
  2376. {
  2377. struct kvm_stats_debugfs_item *p;
  2378. for (p = debugfs_entries; p->name; ++p)
  2379. debugfs_remove(p->dentry);
  2380. debugfs_remove(kvm_debugfs_dir);
  2381. }
  2382. static int kvm_suspend(void)
  2383. {
  2384. if (kvm_usage_count)
  2385. hardware_disable_nolock(NULL);
  2386. return 0;
  2387. }
  2388. static void kvm_resume(void)
  2389. {
  2390. if (kvm_usage_count) {
  2391. WARN_ON(raw_spin_is_locked(&kvm_lock));
  2392. hardware_enable_nolock(NULL);
  2393. }
  2394. }
  2395. static struct syscore_ops kvm_syscore_ops = {
  2396. .suspend = kvm_suspend,
  2397. .resume = kvm_resume,
  2398. };
  2399. static inline
  2400. struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
  2401. {
  2402. return container_of(pn, struct kvm_vcpu, preempt_notifier);
  2403. }
  2404. static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
  2405. {
  2406. struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
  2407. kvm_arch_vcpu_load(vcpu, cpu);
  2408. }
  2409. static void kvm_sched_out(struct preempt_notifier *pn,
  2410. struct task_struct *next)
  2411. {
  2412. struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
  2413. kvm_arch_vcpu_put(vcpu);
  2414. }
  2415. int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
  2416. struct module *module)
  2417. {
  2418. int r;
  2419. int cpu;
  2420. r = kvm_arch_init(opaque);
  2421. if (r)
  2422. goto out_fail;
  2423. if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
  2424. r = -ENOMEM;
  2425. goto out_free_0;
  2426. }
  2427. r = kvm_arch_hardware_setup();
  2428. if (r < 0)
  2429. goto out_free_0a;
  2430. for_each_online_cpu(cpu) {
  2431. smp_call_function_single(cpu,
  2432. kvm_arch_check_processor_compat,
  2433. &r, 1);
  2434. if (r < 0)
  2435. goto out_free_1;
  2436. }
  2437. r = register_cpu_notifier(&kvm_cpu_notifier);
  2438. if (r)
  2439. goto out_free_2;
  2440. register_reboot_notifier(&kvm_reboot_notifier);
  2441. /* A kmem cache lets us meet the alignment requirements of fx_save. */
  2442. if (!vcpu_align)
  2443. vcpu_align = __alignof__(struct kvm_vcpu);
  2444. kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
  2445. 0, NULL);
  2446. if (!kvm_vcpu_cache) {
  2447. r = -ENOMEM;
  2448. goto out_free_3;
  2449. }
  2450. r = kvm_async_pf_init();
  2451. if (r)
  2452. goto out_free;
  2453. kvm_chardev_ops.owner = module;
  2454. kvm_vm_fops.owner = module;
  2455. kvm_vcpu_fops.owner = module;
  2456. r = misc_register(&kvm_dev);
  2457. if (r) {
  2458. printk(KERN_ERR "kvm: misc device register failed\n");
  2459. goto out_unreg;
  2460. }
  2461. register_syscore_ops(&kvm_syscore_ops);
  2462. kvm_preempt_ops.sched_in = kvm_sched_in;
  2463. kvm_preempt_ops.sched_out = kvm_sched_out;
  2464. r = kvm_init_debug();
  2465. if (r) {
  2466. printk(KERN_ERR "kvm: create debugfs files failed\n");
  2467. goto out_undebugfs;
  2468. }
  2469. return 0;
  2470. out_undebugfs:
  2471. unregister_syscore_ops(&kvm_syscore_ops);
  2472. out_unreg:
  2473. kvm_async_pf_deinit();
  2474. out_free:
  2475. kmem_cache_destroy(kvm_vcpu_cache);
  2476. out_free_3:
  2477. unregister_reboot_notifier(&kvm_reboot_notifier);
  2478. unregister_cpu_notifier(&kvm_cpu_notifier);
  2479. out_free_2:
  2480. out_free_1:
  2481. kvm_arch_hardware_unsetup();
  2482. out_free_0a:
  2483. free_cpumask_var(cpus_hardware_enabled);
  2484. out_free_0:
  2485. kvm_arch_exit();
  2486. out_fail:
  2487. return r;
  2488. }
  2489. EXPORT_SYMBOL_GPL(kvm_init);
  2490. void kvm_exit(void)
  2491. {
  2492. kvm_exit_debug();
  2493. misc_deregister(&kvm_dev);
  2494. kmem_cache_destroy(kvm_vcpu_cache);
  2495. kvm_async_pf_deinit();
  2496. unregister_syscore_ops(&kvm_syscore_ops);
  2497. unregister_reboot_notifier(&kvm_reboot_notifier);
  2498. unregister_cpu_notifier(&kvm_cpu_notifier);
  2499. on_each_cpu(hardware_disable_nolock, NULL, 1);
  2500. kvm_arch_hardware_unsetup();
  2501. kvm_arch_exit();
  2502. free_cpumask_var(cpus_hardware_enabled);
  2503. }
  2504. EXPORT_SYMBOL_GPL(kvm_exit);