kvm_main.c 66 KB

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