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. void vcpu_load(struct kvm_vcpu *vcpu)
  116. {
  117. int cpu;
  118. mutex_lock(&vcpu->mutex);
  119. if (unlikely(vcpu->pid != current->pids[PIDTYPE_PID].pid)) {
  120. /* The thread running this VCPU changed. */
  121. struct pid *oldpid = vcpu->pid;
  122. struct pid *newpid = get_task_pid(current, PIDTYPE_PID);
  123. rcu_assign_pointer(vcpu->pid, newpid);
  124. synchronize_rcu();
  125. put_pid(oldpid);
  126. }
  127. cpu = get_cpu();
  128. preempt_notifier_register(&vcpu->preempt_notifier);
  129. kvm_arch_vcpu_load(vcpu, cpu);
  130. put_cpu();
  131. }
  132. void vcpu_put(struct kvm_vcpu *vcpu)
  133. {
  134. preempt_disable();
  135. kvm_arch_vcpu_put(vcpu);
  136. preempt_notifier_unregister(&vcpu->preempt_notifier);
  137. preempt_enable();
  138. mutex_unlock(&vcpu->mutex);
  139. }
  140. static void ack_flush(void *_completed)
  141. {
  142. }
  143. static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
  144. {
  145. int i, cpu, me;
  146. cpumask_var_t cpus;
  147. bool called = true;
  148. struct kvm_vcpu *vcpu;
  149. zalloc_cpumask_var(&cpus, GFP_ATOMIC);
  150. me = get_cpu();
  151. kvm_for_each_vcpu(i, vcpu, kvm) {
  152. kvm_make_request(req, vcpu);
  153. cpu = vcpu->cpu;
  154. /* Set ->requests bit before we read ->mode */
  155. smp_mb();
  156. if (cpus != NULL && cpu != -1 && cpu != me &&
  157. kvm_vcpu_exiting_guest_mode(vcpu) != OUTSIDE_GUEST_MODE)
  158. cpumask_set_cpu(cpu, cpus);
  159. }
  160. if (unlikely(cpus == NULL))
  161. smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
  162. else if (!cpumask_empty(cpus))
  163. smp_call_function_many(cpus, ack_flush, NULL, 1);
  164. else
  165. called = false;
  166. put_cpu();
  167. free_cpumask_var(cpus);
  168. return called;
  169. }
  170. void kvm_flush_remote_tlbs(struct kvm *kvm)
  171. {
  172. long dirty_count = kvm->tlbs_dirty;
  173. smp_mb();
  174. if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
  175. ++kvm->stat.remote_tlb_flush;
  176. cmpxchg(&kvm->tlbs_dirty, dirty_count, 0);
  177. }
  178. void kvm_reload_remote_mmus(struct kvm *kvm)
  179. {
  180. make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
  181. }
  182. int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
  183. {
  184. struct page *page;
  185. int r;
  186. mutex_init(&vcpu->mutex);
  187. vcpu->cpu = -1;
  188. vcpu->kvm = kvm;
  189. vcpu->vcpu_id = id;
  190. vcpu->pid = NULL;
  191. init_waitqueue_head(&vcpu->wq);
  192. kvm_async_pf_vcpu_init(vcpu);
  193. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  194. if (!page) {
  195. r = -ENOMEM;
  196. goto fail;
  197. }
  198. vcpu->run = page_address(page);
  199. kvm_vcpu_set_in_spin_loop(vcpu, false);
  200. kvm_vcpu_set_dy_eligible(vcpu, false);
  201. r = kvm_arch_vcpu_init(vcpu);
  202. if (r < 0)
  203. goto fail_free_run;
  204. return 0;
  205. fail_free_run:
  206. free_page((unsigned long)vcpu->run);
  207. fail:
  208. return r;
  209. }
  210. EXPORT_SYMBOL_GPL(kvm_vcpu_init);
  211. void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
  212. {
  213. put_pid(vcpu->pid);
  214. kvm_arch_vcpu_uninit(vcpu);
  215. free_page((unsigned long)vcpu->run);
  216. }
  217. EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
  218. #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
  219. static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
  220. {
  221. return container_of(mn, struct kvm, mmu_notifier);
  222. }
  223. static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
  224. struct mm_struct *mm,
  225. unsigned long address)
  226. {
  227. struct kvm *kvm = mmu_notifier_to_kvm(mn);
  228. int need_tlb_flush, idx;
  229. /*
  230. * When ->invalidate_page runs, the linux pte has been zapped
  231. * already but the page is still allocated until
  232. * ->invalidate_page returns. So if we increase the sequence
  233. * here the kvm page fault will notice if the spte can't be
  234. * established because the page is going to be freed. If
  235. * instead the kvm page fault establishes the spte before
  236. * ->invalidate_page runs, kvm_unmap_hva will release it
  237. * before returning.
  238. *
  239. * The sequence increase only need to be seen at spin_unlock
  240. * time, and not at spin_lock time.
  241. *
  242. * Increasing the sequence after the spin_unlock would be
  243. * unsafe because the kvm page fault could then establish the
  244. * pte after kvm_unmap_hva returned, without noticing the page
  245. * is going to be freed.
  246. */
  247. idx = srcu_read_lock(&kvm->srcu);
  248. spin_lock(&kvm->mmu_lock);
  249. kvm->mmu_notifier_seq++;
  250. need_tlb_flush = kvm_unmap_hva(kvm, address) | kvm->tlbs_dirty;
  251. /* we've to flush the tlb before the pages can be freed */
  252. if (need_tlb_flush)
  253. kvm_flush_remote_tlbs(kvm);
  254. spin_unlock(&kvm->mmu_lock);
  255. srcu_read_unlock(&kvm->srcu, idx);
  256. }
  257. static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
  258. struct mm_struct *mm,
  259. unsigned long address,
  260. pte_t pte)
  261. {
  262. struct kvm *kvm = mmu_notifier_to_kvm(mn);
  263. int idx;
  264. idx = srcu_read_lock(&kvm->srcu);
  265. spin_lock(&kvm->mmu_lock);
  266. kvm->mmu_notifier_seq++;
  267. kvm_set_spte_hva(kvm, address, pte);
  268. spin_unlock(&kvm->mmu_lock);
  269. srcu_read_unlock(&kvm->srcu, idx);
  270. }
  271. static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
  272. struct mm_struct *mm,
  273. unsigned long start,
  274. unsigned long end)
  275. {
  276. struct kvm *kvm = mmu_notifier_to_kvm(mn);
  277. int need_tlb_flush = 0, idx;
  278. idx = srcu_read_lock(&kvm->srcu);
  279. spin_lock(&kvm->mmu_lock);
  280. /*
  281. * The count increase must become visible at unlock time as no
  282. * spte can be established without taking the mmu_lock and
  283. * count is also read inside the mmu_lock critical section.
  284. */
  285. kvm->mmu_notifier_count++;
  286. need_tlb_flush = kvm_unmap_hva_range(kvm, start, end);
  287. need_tlb_flush |= kvm->tlbs_dirty;
  288. /* we've to flush the tlb before the pages can be freed */
  289. if (need_tlb_flush)
  290. kvm_flush_remote_tlbs(kvm);
  291. spin_unlock(&kvm->mmu_lock);
  292. srcu_read_unlock(&kvm->srcu, idx);
  293. }
  294. static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
  295. struct mm_struct *mm,
  296. unsigned long start,
  297. unsigned long end)
  298. {
  299. struct kvm *kvm = mmu_notifier_to_kvm(mn);
  300. spin_lock(&kvm->mmu_lock);
  301. /*
  302. * This sequence increase will notify the kvm page fault that
  303. * the page that is going to be mapped in the spte could have
  304. * been freed.
  305. */
  306. kvm->mmu_notifier_seq++;
  307. smp_wmb();
  308. /*
  309. * The above sequence increase must be visible before the
  310. * below count decrease, which is ensured by the smp_wmb above
  311. * in conjunction with the smp_rmb in mmu_notifier_retry().
  312. */
  313. kvm->mmu_notifier_count--;
  314. spin_unlock(&kvm->mmu_lock);
  315. BUG_ON(kvm->mmu_notifier_count < 0);
  316. }
  317. static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
  318. struct mm_struct *mm,
  319. unsigned long address)
  320. {
  321. struct kvm *kvm = mmu_notifier_to_kvm(mn);
  322. int young, idx;
  323. idx = srcu_read_lock(&kvm->srcu);
  324. spin_lock(&kvm->mmu_lock);
  325. young = kvm_age_hva(kvm, address);
  326. if (young)
  327. kvm_flush_remote_tlbs(kvm);
  328. spin_unlock(&kvm->mmu_lock);
  329. srcu_read_unlock(&kvm->srcu, idx);
  330. return young;
  331. }
  332. static int kvm_mmu_notifier_test_young(struct mmu_notifier *mn,
  333. struct mm_struct *mm,
  334. unsigned long address)
  335. {
  336. struct kvm *kvm = mmu_notifier_to_kvm(mn);
  337. int young, idx;
  338. idx = srcu_read_lock(&kvm->srcu);
  339. spin_lock(&kvm->mmu_lock);
  340. young = kvm_test_age_hva(kvm, address);
  341. spin_unlock(&kvm->mmu_lock);
  342. srcu_read_unlock(&kvm->srcu, idx);
  343. return young;
  344. }
  345. static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
  346. struct mm_struct *mm)
  347. {
  348. struct kvm *kvm = mmu_notifier_to_kvm(mn);
  349. int idx;
  350. idx = srcu_read_lock(&kvm->srcu);
  351. kvm_arch_flush_shadow_all(kvm);
  352. srcu_read_unlock(&kvm->srcu, idx);
  353. }
  354. static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
  355. .invalidate_page = kvm_mmu_notifier_invalidate_page,
  356. .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
  357. .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
  358. .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
  359. .test_young = kvm_mmu_notifier_test_young,
  360. .change_pte = kvm_mmu_notifier_change_pte,
  361. .release = kvm_mmu_notifier_release,
  362. };
  363. static int kvm_init_mmu_notifier(struct kvm *kvm)
  364. {
  365. kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
  366. return mmu_notifier_register(&kvm->mmu_notifier, current->mm);
  367. }
  368. #else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
  369. static int kvm_init_mmu_notifier(struct kvm *kvm)
  370. {
  371. return 0;
  372. }
  373. #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
  374. static void kvm_init_memslots_id(struct kvm *kvm)
  375. {
  376. int i;
  377. struct kvm_memslots *slots = kvm->memslots;
  378. for (i = 0; i < KVM_MEM_SLOTS_NUM; i++)
  379. slots->id_to_index[i] = slots->memslots[i].id = i;
  380. }
  381. static struct kvm *kvm_create_vm(unsigned long type)
  382. {
  383. int r, i;
  384. struct kvm *kvm = kvm_arch_alloc_vm();
  385. if (!kvm)
  386. return ERR_PTR(-ENOMEM);
  387. r = kvm_arch_init_vm(kvm, type);
  388. if (r)
  389. goto out_err_nodisable;
  390. r = hardware_enable_all();
  391. if (r)
  392. goto out_err_nodisable;
  393. #ifdef CONFIG_HAVE_KVM_IRQCHIP
  394. INIT_HLIST_HEAD(&kvm->mask_notifier_list);
  395. INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
  396. #endif
  397. r = -ENOMEM;
  398. kvm->memslots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
  399. if (!kvm->memslots)
  400. goto out_err_nosrcu;
  401. kvm_init_memslots_id(kvm);
  402. if (init_srcu_struct(&kvm->srcu))
  403. goto out_err_nosrcu;
  404. for (i = 0; i < KVM_NR_BUSES; i++) {
  405. kvm->buses[i] = kzalloc(sizeof(struct kvm_io_bus),
  406. GFP_KERNEL);
  407. if (!kvm->buses[i])
  408. goto out_err;
  409. }
  410. spin_lock_init(&kvm->mmu_lock);
  411. kvm->mm = current->mm;
  412. atomic_inc(&kvm->mm->mm_count);
  413. kvm_eventfd_init(kvm);
  414. mutex_init(&kvm->lock);
  415. mutex_init(&kvm->irq_lock);
  416. mutex_init(&kvm->slots_lock);
  417. atomic_set(&kvm->users_count, 1);
  418. r = kvm_init_mmu_notifier(kvm);
  419. if (r)
  420. goto out_err;
  421. raw_spin_lock(&kvm_lock);
  422. list_add(&kvm->vm_list, &vm_list);
  423. raw_spin_unlock(&kvm_lock);
  424. return kvm;
  425. out_err:
  426. cleanup_srcu_struct(&kvm->srcu);
  427. out_err_nosrcu:
  428. hardware_disable_all();
  429. out_err_nodisable:
  430. for (i = 0; i < KVM_NR_BUSES; i++)
  431. kfree(kvm->buses[i]);
  432. kfree(kvm->memslots);
  433. kvm_arch_free_vm(kvm);
  434. return ERR_PTR(r);
  435. }
  436. /*
  437. * Avoid using vmalloc for a small buffer.
  438. * Should not be used when the size is statically known.
  439. */
  440. void *kvm_kvzalloc(unsigned long size)
  441. {
  442. if (size > PAGE_SIZE)
  443. return vzalloc(size);
  444. else
  445. return kzalloc(size, GFP_KERNEL);
  446. }
  447. void kvm_kvfree(const void *addr)
  448. {
  449. if (is_vmalloc_addr(addr))
  450. vfree(addr);
  451. else
  452. kfree(addr);
  453. }
  454. static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot)
  455. {
  456. if (!memslot->dirty_bitmap)
  457. return;
  458. kvm_kvfree(memslot->dirty_bitmap);
  459. memslot->dirty_bitmap = NULL;
  460. }
  461. /*
  462. * Free any memory in @free but not in @dont.
  463. */
  464. static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
  465. struct kvm_memory_slot *dont)
  466. {
  467. if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
  468. kvm_destroy_dirty_bitmap(free);
  469. kvm_arch_free_memslot(free, dont);
  470. free->npages = 0;
  471. }
  472. void kvm_free_physmem(struct kvm *kvm)
  473. {
  474. struct kvm_memslots *slots = kvm->memslots;
  475. struct kvm_memory_slot *memslot;
  476. kvm_for_each_memslot(memslot, slots)
  477. kvm_free_physmem_slot(memslot, NULL);
  478. kfree(kvm->memslots);
  479. }
  480. static void kvm_destroy_vm(struct kvm *kvm)
  481. {
  482. int i;
  483. struct mm_struct *mm = kvm->mm;
  484. kvm_arch_sync_events(kvm);
  485. raw_spin_lock(&kvm_lock);
  486. list_del(&kvm->vm_list);
  487. raw_spin_unlock(&kvm_lock);
  488. kvm_free_irq_routing(kvm);
  489. for (i = 0; i < KVM_NR_BUSES; i++)
  490. kvm_io_bus_destroy(kvm->buses[i]);
  491. kvm_coalesced_mmio_free(kvm);
  492. #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
  493. mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
  494. #else
  495. kvm_arch_flush_shadow_all(kvm);
  496. #endif
  497. kvm_arch_destroy_vm(kvm);
  498. kvm_free_physmem(kvm);
  499. cleanup_srcu_struct(&kvm->srcu);
  500. kvm_arch_free_vm(kvm);
  501. hardware_disable_all();
  502. mmdrop(mm);
  503. }
  504. void kvm_get_kvm(struct kvm *kvm)
  505. {
  506. atomic_inc(&kvm->users_count);
  507. }
  508. EXPORT_SYMBOL_GPL(kvm_get_kvm);
  509. void kvm_put_kvm(struct kvm *kvm)
  510. {
  511. if (atomic_dec_and_test(&kvm->users_count))
  512. kvm_destroy_vm(kvm);
  513. }
  514. EXPORT_SYMBOL_GPL(kvm_put_kvm);
  515. static int kvm_vm_release(struct inode *inode, struct file *filp)
  516. {
  517. struct kvm *kvm = filp->private_data;
  518. kvm_irqfd_release(kvm);
  519. kvm_put_kvm(kvm);
  520. return 0;
  521. }
  522. /*
  523. * Allocation size is twice as large as the actual dirty bitmap size.
  524. * See x86's kvm_vm_ioctl_get_dirty_log() why this is needed.
  525. */
  526. static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot)
  527. {
  528. #ifndef CONFIG_S390
  529. unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot);
  530. memslot->dirty_bitmap = kvm_kvzalloc(dirty_bytes);
  531. if (!memslot->dirty_bitmap)
  532. return -ENOMEM;
  533. #endif /* !CONFIG_S390 */
  534. return 0;
  535. }
  536. static int cmp_memslot(const void *slot1, const void *slot2)
  537. {
  538. struct kvm_memory_slot *s1, *s2;
  539. s1 = (struct kvm_memory_slot *)slot1;
  540. s2 = (struct kvm_memory_slot *)slot2;
  541. if (s1->npages < s2->npages)
  542. return 1;
  543. if (s1->npages > s2->npages)
  544. return -1;
  545. return 0;
  546. }
  547. /*
  548. * Sort the memslots base on its size, so the larger slots
  549. * will get better fit.
  550. */
  551. static void sort_memslots(struct kvm_memslots *slots)
  552. {
  553. int i;
  554. sort(slots->memslots, KVM_MEM_SLOTS_NUM,
  555. sizeof(struct kvm_memory_slot), cmp_memslot, NULL);
  556. for (i = 0; i < KVM_MEM_SLOTS_NUM; i++)
  557. slots->id_to_index[slots->memslots[i].id] = i;
  558. }
  559. void update_memslots(struct kvm_memslots *slots, struct kvm_memory_slot *new)
  560. {
  561. if (new) {
  562. int id = new->id;
  563. struct kvm_memory_slot *old = id_to_memslot(slots, id);
  564. unsigned long npages = old->npages;
  565. *old = *new;
  566. if (new->npages != npages)
  567. sort_memslots(slots);
  568. }
  569. slots->generation++;
  570. }
  571. static int check_memory_region_flags(struct kvm_userspace_memory_region *mem)
  572. {
  573. u32 valid_flags = KVM_MEM_LOG_DIRTY_PAGES;
  574. #ifdef KVM_CAP_READONLY_MEM
  575. valid_flags |= KVM_MEM_READONLY;
  576. #endif
  577. if (mem->flags & ~valid_flags)
  578. return -EINVAL;
  579. return 0;
  580. }
  581. /*
  582. * Allocate some memory and give it an address in the guest physical address
  583. * space.
  584. *
  585. * Discontiguous memory is allowed, mostly for framebuffers.
  586. *
  587. * Must be called holding mmap_sem for write.
  588. */
  589. int __kvm_set_memory_region(struct kvm *kvm,
  590. struct kvm_userspace_memory_region *mem,
  591. int user_alloc)
  592. {
  593. int r;
  594. gfn_t base_gfn;
  595. unsigned long npages;
  596. unsigned long i;
  597. struct kvm_memory_slot *memslot;
  598. struct kvm_memory_slot old, new;
  599. struct kvm_memslots *slots, *old_memslots;
  600. r = check_memory_region_flags(mem);
  601. if (r)
  602. goto out;
  603. r = -EINVAL;
  604. /* General sanity checks */
  605. if (mem->memory_size & (PAGE_SIZE - 1))
  606. goto out;
  607. if (mem->guest_phys_addr & (PAGE_SIZE - 1))
  608. goto out;
  609. /* We can read the guest memory with __xxx_user() later on. */
  610. if (user_alloc &&
  611. ((mem->userspace_addr & (PAGE_SIZE - 1)) ||
  612. !access_ok(VERIFY_WRITE,
  613. (void __user *)(unsigned long)mem->userspace_addr,
  614. mem->memory_size)))
  615. goto out;
  616. if (mem->slot >= KVM_MEM_SLOTS_NUM)
  617. goto out;
  618. if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
  619. goto out;
  620. memslot = id_to_memslot(kvm->memslots, mem->slot);
  621. base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
  622. npages = mem->memory_size >> PAGE_SHIFT;
  623. r = -EINVAL;
  624. if (npages > KVM_MEM_MAX_NR_PAGES)
  625. goto out;
  626. if (!npages)
  627. mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
  628. new = old = *memslot;
  629. new.id = mem->slot;
  630. new.base_gfn = base_gfn;
  631. new.npages = npages;
  632. new.flags = mem->flags;
  633. /* Disallow changing a memory slot's size. */
  634. r = -EINVAL;
  635. if (npages && old.npages && npages != old.npages)
  636. goto out_free;
  637. /* Check for overlaps */
  638. r = -EEXIST;
  639. for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
  640. struct kvm_memory_slot *s = &kvm->memslots->memslots[i];
  641. if (s == memslot || !s->npages)
  642. continue;
  643. if (!((base_gfn + npages <= s->base_gfn) ||
  644. (base_gfn >= s->base_gfn + s->npages)))
  645. goto out_free;
  646. }
  647. /* Free page dirty bitmap if unneeded */
  648. if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
  649. new.dirty_bitmap = NULL;
  650. r = -ENOMEM;
  651. /* Allocate if a slot is being created */
  652. if (npages && !old.npages) {
  653. new.user_alloc = user_alloc;
  654. new.userspace_addr = mem->userspace_addr;
  655. if (kvm_arch_create_memslot(&new, npages))
  656. goto out_free;
  657. }
  658. /* Allocate page dirty bitmap if needed */
  659. if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
  660. if (kvm_create_dirty_bitmap(&new) < 0)
  661. goto out_free;
  662. /* destroy any largepage mappings for dirty tracking */
  663. }
  664. if (!npages || base_gfn != old.base_gfn) {
  665. struct kvm_memory_slot *slot;
  666. r = -ENOMEM;
  667. slots = kmemdup(kvm->memslots, sizeof(struct kvm_memslots),
  668. GFP_KERNEL);
  669. if (!slots)
  670. goto out_free;
  671. slot = id_to_memslot(slots, mem->slot);
  672. slot->flags |= KVM_MEMSLOT_INVALID;
  673. update_memslots(slots, NULL);
  674. old_memslots = kvm->memslots;
  675. rcu_assign_pointer(kvm->memslots, slots);
  676. synchronize_srcu_expedited(&kvm->srcu);
  677. /* From this point no new shadow pages pointing to a deleted,
  678. * or moved, memslot will be created.
  679. *
  680. * validation of sp->gfn happens in:
  681. * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
  682. * - kvm_is_visible_gfn (mmu_check_roots)
  683. */
  684. kvm_arch_flush_shadow_memslot(kvm, slot);
  685. kfree(old_memslots);
  686. }
  687. r = kvm_arch_prepare_memory_region(kvm, &new, old, mem, user_alloc);
  688. if (r)
  689. goto out_free;
  690. /* map/unmap the pages in iommu page table */
  691. if (npages) {
  692. r = kvm_iommu_map_pages(kvm, &new);
  693. if (r)
  694. goto out_free;
  695. } else
  696. kvm_iommu_unmap_pages(kvm, &old);
  697. r = -ENOMEM;
  698. slots = kmemdup(kvm->memslots, sizeof(struct kvm_memslots),
  699. GFP_KERNEL);
  700. if (!slots)
  701. goto out_free;
  702. /* actual memory is freed via old in kvm_free_physmem_slot below */
  703. if (!npages) {
  704. new.dirty_bitmap = NULL;
  705. memset(&new.arch, 0, sizeof(new.arch));
  706. }
  707. update_memslots(slots, &new);
  708. old_memslots = kvm->memslots;
  709. rcu_assign_pointer(kvm->memslots, slots);
  710. synchronize_srcu_expedited(&kvm->srcu);
  711. kvm_arch_commit_memory_region(kvm, mem, old, user_alloc);
  712. kvm_free_physmem_slot(&old, &new);
  713. kfree(old_memslots);
  714. return 0;
  715. out_free:
  716. kvm_free_physmem_slot(&new, &old);
  717. out:
  718. return r;
  719. }
  720. EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
  721. int kvm_set_memory_region(struct kvm *kvm,
  722. struct kvm_userspace_memory_region *mem,
  723. int user_alloc)
  724. {
  725. int r;
  726. mutex_lock(&kvm->slots_lock);
  727. r = __kvm_set_memory_region(kvm, mem, user_alloc);
  728. mutex_unlock(&kvm->slots_lock);
  729. return r;
  730. }
  731. EXPORT_SYMBOL_GPL(kvm_set_memory_region);
  732. int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
  733. struct
  734. kvm_userspace_memory_region *mem,
  735. int user_alloc)
  736. {
  737. if (mem->slot >= KVM_MEMORY_SLOTS)
  738. return -EINVAL;
  739. return kvm_set_memory_region(kvm, mem, user_alloc);
  740. }
  741. int kvm_get_dirty_log(struct kvm *kvm,
  742. struct kvm_dirty_log *log, int *is_dirty)
  743. {
  744. struct kvm_memory_slot *memslot;
  745. int r, i;
  746. unsigned long n;
  747. unsigned long any = 0;
  748. r = -EINVAL;
  749. if (log->slot >= KVM_MEMORY_SLOTS)
  750. goto out;
  751. memslot = id_to_memslot(kvm->memslots, log->slot);
  752. r = -ENOENT;
  753. if (!memslot->dirty_bitmap)
  754. goto out;
  755. n = kvm_dirty_bitmap_bytes(memslot);
  756. for (i = 0; !any && i < n/sizeof(long); ++i)
  757. any = memslot->dirty_bitmap[i];
  758. r = -EFAULT;
  759. if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
  760. goto out;
  761. if (any)
  762. *is_dirty = 1;
  763. r = 0;
  764. out:
  765. return r;
  766. }
  767. bool kvm_largepages_enabled(void)
  768. {
  769. return largepages_enabled;
  770. }
  771. void kvm_disable_largepages(void)
  772. {
  773. largepages_enabled = false;
  774. }
  775. EXPORT_SYMBOL_GPL(kvm_disable_largepages);
  776. struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
  777. {
  778. return __gfn_to_memslot(kvm_memslots(kvm), gfn);
  779. }
  780. EXPORT_SYMBOL_GPL(gfn_to_memslot);
  781. int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
  782. {
  783. struct kvm_memory_slot *memslot = gfn_to_memslot(kvm, gfn);
  784. if (!memslot || memslot->id >= KVM_MEMORY_SLOTS ||
  785. memslot->flags & KVM_MEMSLOT_INVALID)
  786. return 0;
  787. return 1;
  788. }
  789. EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
  790. unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn)
  791. {
  792. struct vm_area_struct *vma;
  793. unsigned long addr, size;
  794. size = PAGE_SIZE;
  795. addr = gfn_to_hva(kvm, gfn);
  796. if (kvm_is_error_hva(addr))
  797. return PAGE_SIZE;
  798. down_read(&current->mm->mmap_sem);
  799. vma = find_vma(current->mm, addr);
  800. if (!vma)
  801. goto out;
  802. size = vma_kernel_pagesize(vma);
  803. out:
  804. up_read(&current->mm->mmap_sem);
  805. return size;
  806. }
  807. static bool memslot_is_readonly(struct kvm_memory_slot *slot)
  808. {
  809. return slot->flags & KVM_MEM_READONLY;
  810. }
  811. static unsigned long __gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
  812. gfn_t *nr_pages, bool write)
  813. {
  814. if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
  815. return KVM_HVA_ERR_BAD;
  816. if (memslot_is_readonly(slot) && write)
  817. return KVM_HVA_ERR_RO_BAD;
  818. if (nr_pages)
  819. *nr_pages = slot->npages - (gfn - slot->base_gfn);
  820. return __gfn_to_hva_memslot(slot, gfn);
  821. }
  822. static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
  823. gfn_t *nr_pages)
  824. {
  825. return __gfn_to_hva_many(slot, gfn, nr_pages, true);
  826. }
  827. unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot,
  828. gfn_t gfn)
  829. {
  830. return gfn_to_hva_many(slot, gfn, NULL);
  831. }
  832. EXPORT_SYMBOL_GPL(gfn_to_hva_memslot);
  833. unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
  834. {
  835. return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL);
  836. }
  837. EXPORT_SYMBOL_GPL(gfn_to_hva);
  838. /*
  839. * The hva returned by this function is only allowed to be read.
  840. * It should pair with kvm_read_hva() or kvm_read_hva_atomic().
  841. */
  842. static unsigned long gfn_to_hva_read(struct kvm *kvm, gfn_t gfn)
  843. {
  844. return __gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL, false);
  845. }
  846. static int kvm_read_hva(void *data, void __user *hva, int len)
  847. {
  848. return __copy_from_user(data, hva, len);
  849. }
  850. static int kvm_read_hva_atomic(void *data, void __user *hva, int len)
  851. {
  852. return __copy_from_user_inatomic(data, hva, len);
  853. }
  854. int get_user_page_nowait(struct task_struct *tsk, struct mm_struct *mm,
  855. unsigned long start, int write, struct page **page)
  856. {
  857. int flags = FOLL_TOUCH | FOLL_NOWAIT | FOLL_HWPOISON | FOLL_GET;
  858. if (write)
  859. flags |= FOLL_WRITE;
  860. return __get_user_pages(tsk, mm, start, 1, flags, page, NULL, NULL);
  861. }
  862. static inline int check_user_page_hwpoison(unsigned long addr)
  863. {
  864. int rc, flags = FOLL_TOUCH | FOLL_HWPOISON | FOLL_WRITE;
  865. rc = __get_user_pages(current, current->mm, addr, 1,
  866. flags, NULL, NULL, NULL);
  867. return rc == -EHWPOISON;
  868. }
  869. /*
  870. * The atomic path to get the writable pfn which will be stored in @pfn,
  871. * true indicates success, otherwise false is returned.
  872. */
  873. static bool hva_to_pfn_fast(unsigned long addr, bool atomic, bool *async,
  874. bool write_fault, bool *writable, pfn_t *pfn)
  875. {
  876. struct page *page[1];
  877. int npages;
  878. if (!(async || atomic))
  879. return false;
  880. /*
  881. * Fast pin a writable pfn only if it is a write fault request
  882. * or the caller allows to map a writable pfn for a read fault
  883. * request.
  884. */
  885. if (!(write_fault || writable))
  886. return false;
  887. npages = __get_user_pages_fast(addr, 1, 1, page);
  888. if (npages == 1) {
  889. *pfn = page_to_pfn(page[0]);
  890. if (writable)
  891. *writable = true;
  892. return true;
  893. }
  894. return false;
  895. }
  896. /*
  897. * The slow path to get the pfn of the specified host virtual address,
  898. * 1 indicates success, -errno is returned if error is detected.
  899. */
  900. static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault,
  901. bool *writable, pfn_t *pfn)
  902. {
  903. struct page *page[1];
  904. int npages = 0;
  905. might_sleep();
  906. if (writable)
  907. *writable = write_fault;
  908. if (async) {
  909. down_read(&current->mm->mmap_sem);
  910. npages = get_user_page_nowait(current, current->mm,
  911. addr, write_fault, page);
  912. up_read(&current->mm->mmap_sem);
  913. } else
  914. npages = get_user_pages_fast(addr, 1, write_fault,
  915. page);
  916. if (npages != 1)
  917. return npages;
  918. /* map read fault as writable if possible */
  919. if (unlikely(!write_fault) && writable) {
  920. struct page *wpage[1];
  921. npages = __get_user_pages_fast(addr, 1, 1, wpage);
  922. if (npages == 1) {
  923. *writable = true;
  924. put_page(page[0]);
  925. page[0] = wpage[0];
  926. }
  927. npages = 1;
  928. }
  929. *pfn = page_to_pfn(page[0]);
  930. return npages;
  931. }
  932. static bool vma_is_valid(struct vm_area_struct *vma, bool write_fault)
  933. {
  934. if (unlikely(!(vma->vm_flags & VM_READ)))
  935. return false;
  936. if (write_fault && (unlikely(!(vma->vm_flags & VM_WRITE))))
  937. return false;
  938. return true;
  939. }
  940. /*
  941. * Pin guest page in memory and return its pfn.
  942. * @addr: host virtual address which maps memory to the guest
  943. * @atomic: whether this function can sleep
  944. * @async: whether this function need to wait IO complete if the
  945. * host page is not in the memory
  946. * @write_fault: whether we should get a writable host page
  947. * @writable: whether it allows to map a writable host page for !@write_fault
  948. *
  949. * The function will map a writable host page for these two cases:
  950. * 1): @write_fault = true
  951. * 2): @write_fault = false && @writable, @writable will tell the caller
  952. * whether the mapping is writable.
  953. */
  954. static pfn_t hva_to_pfn(unsigned long addr, bool atomic, bool *async,
  955. bool write_fault, bool *writable)
  956. {
  957. struct vm_area_struct *vma;
  958. pfn_t pfn = 0;
  959. int npages;
  960. /* we can do it either atomically or asynchronously, not both */
  961. BUG_ON(atomic && async);
  962. if (hva_to_pfn_fast(addr, atomic, async, write_fault, writable, &pfn))
  963. return pfn;
  964. if (atomic)
  965. return KVM_PFN_ERR_FAULT;
  966. npages = hva_to_pfn_slow(addr, async, write_fault, writable, &pfn);
  967. if (npages == 1)
  968. return pfn;
  969. down_read(&current->mm->mmap_sem);
  970. if (npages == -EHWPOISON ||
  971. (!async && check_user_page_hwpoison(addr))) {
  972. pfn = KVM_PFN_ERR_HWPOISON;
  973. goto exit;
  974. }
  975. vma = find_vma_intersection(current->mm, addr, addr + 1);
  976. if (vma == NULL)
  977. pfn = KVM_PFN_ERR_FAULT;
  978. else if ((vma->vm_flags & VM_PFNMAP)) {
  979. pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) +
  980. vma->vm_pgoff;
  981. BUG_ON(!kvm_is_mmio_pfn(pfn));
  982. } else {
  983. if (async && vma_is_valid(vma, write_fault))
  984. *async = true;
  985. pfn = KVM_PFN_ERR_FAULT;
  986. }
  987. exit:
  988. up_read(&current->mm->mmap_sem);
  989. return pfn;
  990. }
  991. static pfn_t
  992. __gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn, bool atomic,
  993. bool *async, bool write_fault, bool *writable)
  994. {
  995. unsigned long addr = __gfn_to_hva_many(slot, gfn, NULL, write_fault);
  996. if (addr == KVM_HVA_ERR_RO_BAD)
  997. return KVM_PFN_ERR_RO_FAULT;
  998. if (kvm_is_error_hva(addr))
  999. return KVM_PFN_ERR_BAD;
  1000. /* Do not map writable pfn in the readonly memslot. */
  1001. if (writable && memslot_is_readonly(slot)) {
  1002. *writable = false;
  1003. writable = NULL;
  1004. }
  1005. return hva_to_pfn(addr, atomic, async, write_fault,
  1006. writable);
  1007. }
  1008. static pfn_t __gfn_to_pfn(struct kvm *kvm, gfn_t gfn, bool atomic, bool *async,
  1009. bool write_fault, bool *writable)
  1010. {
  1011. struct kvm_memory_slot *slot;
  1012. if (async)
  1013. *async = false;
  1014. slot = gfn_to_memslot(kvm, gfn);
  1015. return __gfn_to_pfn_memslot(slot, gfn, atomic, async, write_fault,
  1016. writable);
  1017. }
  1018. pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn)
  1019. {
  1020. return __gfn_to_pfn(kvm, gfn, true, NULL, true, NULL);
  1021. }
  1022. EXPORT_SYMBOL_GPL(gfn_to_pfn_atomic);
  1023. pfn_t gfn_to_pfn_async(struct kvm *kvm, gfn_t gfn, bool *async,
  1024. bool write_fault, bool *writable)
  1025. {
  1026. return __gfn_to_pfn(kvm, gfn, false, async, write_fault, writable);
  1027. }
  1028. EXPORT_SYMBOL_GPL(gfn_to_pfn_async);
  1029. pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
  1030. {
  1031. return __gfn_to_pfn(kvm, gfn, false, NULL, true, NULL);
  1032. }
  1033. EXPORT_SYMBOL_GPL(gfn_to_pfn);
  1034. pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
  1035. bool *writable)
  1036. {
  1037. return __gfn_to_pfn(kvm, gfn, false, NULL, write_fault, writable);
  1038. }
  1039. EXPORT_SYMBOL_GPL(gfn_to_pfn_prot);
  1040. pfn_t gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn)
  1041. {
  1042. return __gfn_to_pfn_memslot(slot, gfn, false, NULL, true, NULL);
  1043. }
  1044. pfn_t gfn_to_pfn_memslot_atomic(struct kvm_memory_slot *slot, gfn_t gfn)
  1045. {
  1046. return __gfn_to_pfn_memslot(slot, gfn, true, NULL, true, NULL);
  1047. }
  1048. EXPORT_SYMBOL_GPL(gfn_to_pfn_memslot_atomic);
  1049. int gfn_to_page_many_atomic(struct kvm *kvm, gfn_t gfn, struct page **pages,
  1050. int nr_pages)
  1051. {
  1052. unsigned long addr;
  1053. gfn_t entry;
  1054. addr = gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, &entry);
  1055. if (kvm_is_error_hva(addr))
  1056. return -1;
  1057. if (entry < nr_pages)
  1058. return 0;
  1059. return __get_user_pages_fast(addr, nr_pages, 1, pages);
  1060. }
  1061. EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic);
  1062. static struct page *kvm_pfn_to_page(pfn_t pfn)
  1063. {
  1064. if (is_error_pfn(pfn))
  1065. return KVM_ERR_PTR_BAD_PAGE;
  1066. if (kvm_is_mmio_pfn(pfn)) {
  1067. WARN_ON(1);
  1068. return KVM_ERR_PTR_BAD_PAGE;
  1069. }
  1070. return pfn_to_page(pfn);
  1071. }
  1072. struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
  1073. {
  1074. pfn_t pfn;
  1075. pfn = gfn_to_pfn(kvm, gfn);
  1076. return kvm_pfn_to_page(pfn);
  1077. }
  1078. EXPORT_SYMBOL_GPL(gfn_to_page);
  1079. void kvm_release_page_clean(struct page *page)
  1080. {
  1081. WARN_ON(is_error_page(page));
  1082. kvm_release_pfn_clean(page_to_pfn(page));
  1083. }
  1084. EXPORT_SYMBOL_GPL(kvm_release_page_clean);
  1085. void kvm_release_pfn_clean(pfn_t pfn)
  1086. {
  1087. WARN_ON(is_error_pfn(pfn));
  1088. if (!kvm_is_mmio_pfn(pfn))
  1089. put_page(pfn_to_page(pfn));
  1090. }
  1091. EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
  1092. void kvm_release_page_dirty(struct page *page)
  1093. {
  1094. WARN_ON(is_error_page(page));
  1095. kvm_release_pfn_dirty(page_to_pfn(page));
  1096. }
  1097. EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
  1098. void kvm_release_pfn_dirty(pfn_t pfn)
  1099. {
  1100. kvm_set_pfn_dirty(pfn);
  1101. kvm_release_pfn_clean(pfn);
  1102. }
  1103. EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
  1104. void kvm_set_page_dirty(struct page *page)
  1105. {
  1106. kvm_set_pfn_dirty(page_to_pfn(page));
  1107. }
  1108. EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
  1109. void kvm_set_pfn_dirty(pfn_t pfn)
  1110. {
  1111. if (!kvm_is_mmio_pfn(pfn)) {
  1112. struct page *page = pfn_to_page(pfn);
  1113. if (!PageReserved(page))
  1114. SetPageDirty(page);
  1115. }
  1116. }
  1117. EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
  1118. void kvm_set_pfn_accessed(pfn_t pfn)
  1119. {
  1120. if (!kvm_is_mmio_pfn(pfn))
  1121. mark_page_accessed(pfn_to_page(pfn));
  1122. }
  1123. EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
  1124. void kvm_get_pfn(pfn_t pfn)
  1125. {
  1126. if (!kvm_is_mmio_pfn(pfn))
  1127. get_page(pfn_to_page(pfn));
  1128. }
  1129. EXPORT_SYMBOL_GPL(kvm_get_pfn);
  1130. static int next_segment(unsigned long len, int offset)
  1131. {
  1132. if (len > PAGE_SIZE - offset)
  1133. return PAGE_SIZE - offset;
  1134. else
  1135. return len;
  1136. }
  1137. int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
  1138. int len)
  1139. {
  1140. int r;
  1141. unsigned long addr;
  1142. addr = gfn_to_hva_read(kvm, gfn);
  1143. if (kvm_is_error_hva(addr))
  1144. return -EFAULT;
  1145. r = kvm_read_hva(data, (void __user *)addr + offset, len);
  1146. if (r)
  1147. return -EFAULT;
  1148. return 0;
  1149. }
  1150. EXPORT_SYMBOL_GPL(kvm_read_guest_page);
  1151. int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
  1152. {
  1153. gfn_t gfn = gpa >> PAGE_SHIFT;
  1154. int seg;
  1155. int offset = offset_in_page(gpa);
  1156. int ret;
  1157. while ((seg = next_segment(len, offset)) != 0) {
  1158. ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
  1159. if (ret < 0)
  1160. return ret;
  1161. offset = 0;
  1162. len -= seg;
  1163. data += seg;
  1164. ++gfn;
  1165. }
  1166. return 0;
  1167. }
  1168. EXPORT_SYMBOL_GPL(kvm_read_guest);
  1169. int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
  1170. unsigned long len)
  1171. {
  1172. int r;
  1173. unsigned long addr;
  1174. gfn_t gfn = gpa >> PAGE_SHIFT;
  1175. int offset = offset_in_page(gpa);
  1176. addr = gfn_to_hva_read(kvm, gfn);
  1177. if (kvm_is_error_hva(addr))
  1178. return -EFAULT;
  1179. pagefault_disable();
  1180. r = kvm_read_hva_atomic(data, (void __user *)addr + offset, len);
  1181. pagefault_enable();
  1182. if (r)
  1183. return -EFAULT;
  1184. return 0;
  1185. }
  1186. EXPORT_SYMBOL(kvm_read_guest_atomic);
  1187. int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
  1188. int offset, int len)
  1189. {
  1190. int r;
  1191. unsigned long addr;
  1192. addr = gfn_to_hva(kvm, gfn);
  1193. if (kvm_is_error_hva(addr))
  1194. return -EFAULT;
  1195. r = __copy_to_user((void __user *)addr + offset, data, len);
  1196. if (r)
  1197. return -EFAULT;
  1198. mark_page_dirty(kvm, gfn);
  1199. return 0;
  1200. }
  1201. EXPORT_SYMBOL_GPL(kvm_write_guest_page);
  1202. int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
  1203. unsigned long len)
  1204. {
  1205. gfn_t gfn = gpa >> PAGE_SHIFT;
  1206. int seg;
  1207. int offset = offset_in_page(gpa);
  1208. int ret;
  1209. while ((seg = next_segment(len, offset)) != 0) {
  1210. ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
  1211. if (ret < 0)
  1212. return ret;
  1213. offset = 0;
  1214. len -= seg;
  1215. data += seg;
  1216. ++gfn;
  1217. }
  1218. return 0;
  1219. }
  1220. int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
  1221. gpa_t gpa)
  1222. {
  1223. struct kvm_memslots *slots = kvm_memslots(kvm);
  1224. int offset = offset_in_page(gpa);
  1225. gfn_t gfn = gpa >> PAGE_SHIFT;
  1226. ghc->gpa = gpa;
  1227. ghc->generation = slots->generation;
  1228. ghc->memslot = gfn_to_memslot(kvm, gfn);
  1229. ghc->hva = gfn_to_hva_many(ghc->memslot, gfn, NULL);
  1230. if (!kvm_is_error_hva(ghc->hva))
  1231. ghc->hva += offset;
  1232. else
  1233. return -EFAULT;
  1234. return 0;
  1235. }
  1236. EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init);
  1237. int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
  1238. void *data, unsigned long len)
  1239. {
  1240. struct kvm_memslots *slots = kvm_memslots(kvm);
  1241. int r;
  1242. if (slots->generation != ghc->generation)
  1243. kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa);
  1244. if (kvm_is_error_hva(ghc->hva))
  1245. return -EFAULT;
  1246. r = __copy_to_user((void __user *)ghc->hva, data, len);
  1247. if (r)
  1248. return -EFAULT;
  1249. mark_page_dirty_in_slot(kvm, ghc->memslot, ghc->gpa >> PAGE_SHIFT);
  1250. return 0;
  1251. }
  1252. EXPORT_SYMBOL_GPL(kvm_write_guest_cached);
  1253. int kvm_read_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
  1254. void *data, unsigned long len)
  1255. {
  1256. struct kvm_memslots *slots = kvm_memslots(kvm);
  1257. int r;
  1258. if (slots->generation != ghc->generation)
  1259. kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa);
  1260. if (kvm_is_error_hva(ghc->hva))
  1261. return -EFAULT;
  1262. r = __copy_from_user(data, (void __user *)ghc->hva, len);
  1263. if (r)
  1264. return -EFAULT;
  1265. return 0;
  1266. }
  1267. EXPORT_SYMBOL_GPL(kvm_read_guest_cached);
  1268. int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
  1269. {
  1270. return kvm_write_guest_page(kvm, gfn, (const void *) empty_zero_page,
  1271. offset, len);
  1272. }
  1273. EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
  1274. int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
  1275. {
  1276. gfn_t gfn = gpa >> PAGE_SHIFT;
  1277. int seg;
  1278. int offset = offset_in_page(gpa);
  1279. int ret;
  1280. while ((seg = next_segment(len, offset)) != 0) {
  1281. ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
  1282. if (ret < 0)
  1283. return ret;
  1284. offset = 0;
  1285. len -= seg;
  1286. ++gfn;
  1287. }
  1288. return 0;
  1289. }
  1290. EXPORT_SYMBOL_GPL(kvm_clear_guest);
  1291. void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot,
  1292. gfn_t gfn)
  1293. {
  1294. if (memslot && memslot->dirty_bitmap) {
  1295. unsigned long rel_gfn = gfn - memslot->base_gfn;
  1296. /* TODO: introduce set_bit_le() and use it */
  1297. test_and_set_bit_le(rel_gfn, memslot->dirty_bitmap);
  1298. }
  1299. }
  1300. void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
  1301. {
  1302. struct kvm_memory_slot *memslot;
  1303. memslot = gfn_to_memslot(kvm, gfn);
  1304. mark_page_dirty_in_slot(kvm, memslot, gfn);
  1305. }
  1306. /*
  1307. * The vCPU has executed a HLT instruction with in-kernel mode enabled.
  1308. */
  1309. void kvm_vcpu_block(struct kvm_vcpu *vcpu)
  1310. {
  1311. DEFINE_WAIT(wait);
  1312. for (;;) {
  1313. prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
  1314. if (kvm_arch_vcpu_runnable(vcpu)) {
  1315. kvm_make_request(KVM_REQ_UNHALT, vcpu);
  1316. break;
  1317. }
  1318. if (kvm_cpu_has_pending_timer(vcpu))
  1319. break;
  1320. if (signal_pending(current))
  1321. break;
  1322. schedule();
  1323. }
  1324. finish_wait(&vcpu->wq, &wait);
  1325. }
  1326. #ifndef CONFIG_S390
  1327. /*
  1328. * Kick a sleeping VCPU, or a guest VCPU in guest mode, into host kernel mode.
  1329. */
  1330. void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
  1331. {
  1332. int me;
  1333. int cpu = vcpu->cpu;
  1334. wait_queue_head_t *wqp;
  1335. wqp = kvm_arch_vcpu_wq(vcpu);
  1336. if (waitqueue_active(wqp)) {
  1337. wake_up_interruptible(wqp);
  1338. ++vcpu->stat.halt_wakeup;
  1339. }
  1340. me = get_cpu();
  1341. if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
  1342. if (kvm_arch_vcpu_should_kick(vcpu))
  1343. smp_send_reschedule(cpu);
  1344. put_cpu();
  1345. }
  1346. #endif /* !CONFIG_S390 */
  1347. void kvm_resched(struct kvm_vcpu *vcpu)
  1348. {
  1349. if (!need_resched())
  1350. return;
  1351. cond_resched();
  1352. }
  1353. EXPORT_SYMBOL_GPL(kvm_resched);
  1354. bool kvm_vcpu_yield_to(struct kvm_vcpu *target)
  1355. {
  1356. struct pid *pid;
  1357. struct task_struct *task = NULL;
  1358. rcu_read_lock();
  1359. pid = rcu_dereference(target->pid);
  1360. if (pid)
  1361. task = get_pid_task(target->pid, PIDTYPE_PID);
  1362. rcu_read_unlock();
  1363. if (!task)
  1364. return false;
  1365. if (task->flags & PF_VCPU) {
  1366. put_task_struct(task);
  1367. return false;
  1368. }
  1369. if (yield_to(task, 1)) {
  1370. put_task_struct(task);
  1371. return true;
  1372. }
  1373. put_task_struct(task);
  1374. return false;
  1375. }
  1376. EXPORT_SYMBOL_GPL(kvm_vcpu_yield_to);
  1377. #ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
  1378. /*
  1379. * Helper that checks whether a VCPU is eligible for directed yield.
  1380. * Most eligible candidate to yield is decided by following heuristics:
  1381. *
  1382. * (a) VCPU which has not done pl-exit or cpu relax intercepted recently
  1383. * (preempted lock holder), indicated by @in_spin_loop.
  1384. * Set at the beiginning and cleared at the end of interception/PLE handler.
  1385. *
  1386. * (b) VCPU which has done pl-exit/ cpu relax intercepted but did not get
  1387. * chance last time (mostly it has become eligible now since we have probably
  1388. * yielded to lockholder in last iteration. This is done by toggling
  1389. * @dy_eligible each time a VCPU checked for eligibility.)
  1390. *
  1391. * Yielding to a recently pl-exited/cpu relax intercepted VCPU before yielding
  1392. * to preempted lock-holder could result in wrong VCPU selection and CPU
  1393. * burning. Giving priority for a potential lock-holder increases lock
  1394. * progress.
  1395. *
  1396. * Since algorithm is based on heuristics, accessing another VCPU data without
  1397. * locking does not harm. It may result in trying to yield to same VCPU, fail
  1398. * and continue with next VCPU and so on.
  1399. */
  1400. bool kvm_vcpu_eligible_for_directed_yield(struct kvm_vcpu *vcpu)
  1401. {
  1402. bool eligible;
  1403. eligible = !vcpu->spin_loop.in_spin_loop ||
  1404. (vcpu->spin_loop.in_spin_loop &&
  1405. vcpu->spin_loop.dy_eligible);
  1406. if (vcpu->spin_loop.in_spin_loop)
  1407. kvm_vcpu_set_dy_eligible(vcpu, !vcpu->spin_loop.dy_eligible);
  1408. return eligible;
  1409. }
  1410. #endif
  1411. void kvm_vcpu_on_spin(struct kvm_vcpu *me)
  1412. {
  1413. struct kvm *kvm = me->kvm;
  1414. struct kvm_vcpu *vcpu;
  1415. int last_boosted_vcpu = me->kvm->last_boosted_vcpu;
  1416. int yielded = 0;
  1417. int pass;
  1418. int i;
  1419. kvm_vcpu_set_in_spin_loop(me, true);
  1420. /*
  1421. * We boost the priority of a VCPU that is runnable but not
  1422. * currently running, because it got preempted by something
  1423. * else and called schedule in __vcpu_run. Hopefully that
  1424. * VCPU is holding the lock that we need and will release it.
  1425. * We approximate round-robin by starting at the last boosted VCPU.
  1426. */
  1427. for (pass = 0; pass < 2 && !yielded; pass++) {
  1428. kvm_for_each_vcpu(i, vcpu, kvm) {
  1429. if (!pass && i <= last_boosted_vcpu) {
  1430. i = last_boosted_vcpu;
  1431. continue;
  1432. } else if (pass && i > last_boosted_vcpu)
  1433. break;
  1434. if (vcpu == me)
  1435. continue;
  1436. if (waitqueue_active(&vcpu->wq))
  1437. continue;
  1438. if (!kvm_vcpu_eligible_for_directed_yield(vcpu))
  1439. continue;
  1440. if (kvm_vcpu_yield_to(vcpu)) {
  1441. kvm->last_boosted_vcpu = i;
  1442. yielded = 1;
  1443. break;
  1444. }
  1445. }
  1446. }
  1447. kvm_vcpu_set_in_spin_loop(me, false);
  1448. /* Ensure vcpu is not eligible during next spinloop */
  1449. kvm_vcpu_set_dy_eligible(me, false);
  1450. }
  1451. EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
  1452. static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1453. {
  1454. struct kvm_vcpu *vcpu = vma->vm_file->private_data;
  1455. struct page *page;
  1456. if (vmf->pgoff == 0)
  1457. page = virt_to_page(vcpu->run);
  1458. #ifdef CONFIG_X86
  1459. else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
  1460. page = virt_to_page(vcpu->arch.pio_data);
  1461. #endif
  1462. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  1463. else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
  1464. page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
  1465. #endif
  1466. else
  1467. return kvm_arch_vcpu_fault(vcpu, vmf);
  1468. get_page(page);
  1469. vmf->page = page;
  1470. return 0;
  1471. }
  1472. static const struct vm_operations_struct kvm_vcpu_vm_ops = {
  1473. .fault = kvm_vcpu_fault,
  1474. };
  1475. static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
  1476. {
  1477. vma->vm_ops = &kvm_vcpu_vm_ops;
  1478. return 0;
  1479. }
  1480. static int kvm_vcpu_release(struct inode *inode, struct file *filp)
  1481. {
  1482. struct kvm_vcpu *vcpu = filp->private_data;
  1483. kvm_put_kvm(vcpu->kvm);
  1484. return 0;
  1485. }
  1486. static struct file_operations kvm_vcpu_fops = {
  1487. .release = kvm_vcpu_release,
  1488. .unlocked_ioctl = kvm_vcpu_ioctl,
  1489. #ifdef CONFIG_COMPAT
  1490. .compat_ioctl = kvm_vcpu_compat_ioctl,
  1491. #endif
  1492. .mmap = kvm_vcpu_mmap,
  1493. .llseek = noop_llseek,
  1494. };
  1495. /*
  1496. * Allocates an inode for the vcpu.
  1497. */
  1498. static int create_vcpu_fd(struct kvm_vcpu *vcpu)
  1499. {
  1500. return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR);
  1501. }
  1502. /*
  1503. * Creates some virtual cpus. Good luck creating more than one.
  1504. */
  1505. static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
  1506. {
  1507. int r;
  1508. struct kvm_vcpu *vcpu, *v;
  1509. vcpu = kvm_arch_vcpu_create(kvm, id);
  1510. if (IS_ERR(vcpu))
  1511. return PTR_ERR(vcpu);
  1512. preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
  1513. r = kvm_arch_vcpu_setup(vcpu);
  1514. if (r)
  1515. goto vcpu_destroy;
  1516. mutex_lock(&kvm->lock);
  1517. if (!kvm_vcpu_compatible(vcpu)) {
  1518. r = -EINVAL;
  1519. goto unlock_vcpu_destroy;
  1520. }
  1521. if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
  1522. r = -EINVAL;
  1523. goto unlock_vcpu_destroy;
  1524. }
  1525. kvm_for_each_vcpu(r, v, kvm)
  1526. if (v->vcpu_id == id) {
  1527. r = -EEXIST;
  1528. goto unlock_vcpu_destroy;
  1529. }
  1530. BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
  1531. /* Now it's all set up, let userspace reach it */
  1532. kvm_get_kvm(kvm);
  1533. r = create_vcpu_fd(vcpu);
  1534. if (r < 0) {
  1535. kvm_put_kvm(kvm);
  1536. goto unlock_vcpu_destroy;
  1537. }
  1538. kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
  1539. smp_wmb();
  1540. atomic_inc(&kvm->online_vcpus);
  1541. mutex_unlock(&kvm->lock);
  1542. return r;
  1543. unlock_vcpu_destroy:
  1544. mutex_unlock(&kvm->lock);
  1545. vcpu_destroy:
  1546. kvm_arch_vcpu_destroy(vcpu);
  1547. return r;
  1548. }
  1549. static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
  1550. {
  1551. if (sigset) {
  1552. sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
  1553. vcpu->sigset_active = 1;
  1554. vcpu->sigset = *sigset;
  1555. } else
  1556. vcpu->sigset_active = 0;
  1557. return 0;
  1558. }
  1559. static long kvm_vcpu_ioctl(struct file *filp,
  1560. unsigned int ioctl, unsigned long arg)
  1561. {
  1562. struct kvm_vcpu *vcpu = filp->private_data;
  1563. void __user *argp = (void __user *)arg;
  1564. int r;
  1565. struct kvm_fpu *fpu = NULL;
  1566. struct kvm_sregs *kvm_sregs = NULL;
  1567. if (vcpu->kvm->mm != current->mm)
  1568. return -EIO;
  1569. #if defined(CONFIG_S390) || defined(CONFIG_PPC)
  1570. /*
  1571. * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
  1572. * so vcpu_load() would break it.
  1573. */
  1574. if (ioctl == KVM_S390_INTERRUPT || ioctl == KVM_INTERRUPT)
  1575. return kvm_arch_vcpu_ioctl(filp, ioctl, arg);
  1576. #endif
  1577. vcpu_load(vcpu);
  1578. switch (ioctl) {
  1579. case KVM_RUN:
  1580. r = -EINVAL;
  1581. if (arg)
  1582. goto out;
  1583. r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
  1584. trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
  1585. break;
  1586. case KVM_GET_REGS: {
  1587. struct kvm_regs *kvm_regs;
  1588. r = -ENOMEM;
  1589. kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
  1590. if (!kvm_regs)
  1591. goto out;
  1592. r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
  1593. if (r)
  1594. goto out_free1;
  1595. r = -EFAULT;
  1596. if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
  1597. goto out_free1;
  1598. r = 0;
  1599. out_free1:
  1600. kfree(kvm_regs);
  1601. break;
  1602. }
  1603. case KVM_SET_REGS: {
  1604. struct kvm_regs *kvm_regs;
  1605. r = -ENOMEM;
  1606. kvm_regs = memdup_user(argp, sizeof(*kvm_regs));
  1607. if (IS_ERR(kvm_regs)) {
  1608. r = PTR_ERR(kvm_regs);
  1609. goto out;
  1610. }
  1611. r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
  1612. if (r)
  1613. goto out_free2;
  1614. r = 0;
  1615. out_free2:
  1616. kfree(kvm_regs);
  1617. break;
  1618. }
  1619. case KVM_GET_SREGS: {
  1620. kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
  1621. r = -ENOMEM;
  1622. if (!kvm_sregs)
  1623. goto out;
  1624. r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
  1625. if (r)
  1626. goto out;
  1627. r = -EFAULT;
  1628. if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
  1629. goto out;
  1630. r = 0;
  1631. break;
  1632. }
  1633. case KVM_SET_SREGS: {
  1634. kvm_sregs = memdup_user(argp, sizeof(*kvm_sregs));
  1635. if (IS_ERR(kvm_sregs)) {
  1636. r = PTR_ERR(kvm_sregs);
  1637. goto out;
  1638. }
  1639. r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
  1640. if (r)
  1641. goto out;
  1642. r = 0;
  1643. break;
  1644. }
  1645. case KVM_GET_MP_STATE: {
  1646. struct kvm_mp_state mp_state;
  1647. r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
  1648. if (r)
  1649. goto out;
  1650. r = -EFAULT;
  1651. if (copy_to_user(argp, &mp_state, sizeof mp_state))
  1652. goto out;
  1653. r = 0;
  1654. break;
  1655. }
  1656. case KVM_SET_MP_STATE: {
  1657. struct kvm_mp_state mp_state;
  1658. r = -EFAULT;
  1659. if (copy_from_user(&mp_state, argp, sizeof mp_state))
  1660. goto out;
  1661. r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
  1662. if (r)
  1663. goto out;
  1664. r = 0;
  1665. break;
  1666. }
  1667. case KVM_TRANSLATE: {
  1668. struct kvm_translation tr;
  1669. r = -EFAULT;
  1670. if (copy_from_user(&tr, argp, sizeof tr))
  1671. goto out;
  1672. r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
  1673. if (r)
  1674. goto out;
  1675. r = -EFAULT;
  1676. if (copy_to_user(argp, &tr, sizeof tr))
  1677. goto out;
  1678. r = 0;
  1679. break;
  1680. }
  1681. case KVM_SET_GUEST_DEBUG: {
  1682. struct kvm_guest_debug dbg;
  1683. r = -EFAULT;
  1684. if (copy_from_user(&dbg, argp, sizeof dbg))
  1685. goto out;
  1686. r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
  1687. if (r)
  1688. goto out;
  1689. r = 0;
  1690. break;
  1691. }
  1692. case KVM_SET_SIGNAL_MASK: {
  1693. struct kvm_signal_mask __user *sigmask_arg = argp;
  1694. struct kvm_signal_mask kvm_sigmask;
  1695. sigset_t sigset, *p;
  1696. p = NULL;
  1697. if (argp) {
  1698. r = -EFAULT;
  1699. if (copy_from_user(&kvm_sigmask, argp,
  1700. sizeof kvm_sigmask))
  1701. goto out;
  1702. r = -EINVAL;
  1703. if (kvm_sigmask.len != sizeof sigset)
  1704. goto out;
  1705. r = -EFAULT;
  1706. if (copy_from_user(&sigset, sigmask_arg->sigset,
  1707. sizeof sigset))
  1708. goto out;
  1709. p = &sigset;
  1710. }
  1711. r = kvm_vcpu_ioctl_set_sigmask(vcpu, p);
  1712. break;
  1713. }
  1714. case KVM_GET_FPU: {
  1715. fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
  1716. r = -ENOMEM;
  1717. if (!fpu)
  1718. goto out;
  1719. r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
  1720. if (r)
  1721. goto out;
  1722. r = -EFAULT;
  1723. if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
  1724. goto out;
  1725. r = 0;
  1726. break;
  1727. }
  1728. case KVM_SET_FPU: {
  1729. fpu = memdup_user(argp, sizeof(*fpu));
  1730. if (IS_ERR(fpu)) {
  1731. r = PTR_ERR(fpu);
  1732. goto out;
  1733. }
  1734. r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
  1735. if (r)
  1736. goto out;
  1737. r = 0;
  1738. break;
  1739. }
  1740. default:
  1741. r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
  1742. }
  1743. out:
  1744. vcpu_put(vcpu);
  1745. kfree(fpu);
  1746. kfree(kvm_sregs);
  1747. return r;
  1748. }
  1749. #ifdef CONFIG_COMPAT
  1750. static long kvm_vcpu_compat_ioctl(struct file *filp,
  1751. unsigned int ioctl, unsigned long arg)
  1752. {
  1753. struct kvm_vcpu *vcpu = filp->private_data;
  1754. void __user *argp = compat_ptr(arg);
  1755. int r;
  1756. if (vcpu->kvm->mm != current->mm)
  1757. return -EIO;
  1758. switch (ioctl) {
  1759. case KVM_SET_SIGNAL_MASK: {
  1760. struct kvm_signal_mask __user *sigmask_arg = argp;
  1761. struct kvm_signal_mask kvm_sigmask;
  1762. compat_sigset_t csigset;
  1763. sigset_t sigset;
  1764. if (argp) {
  1765. r = -EFAULT;
  1766. if (copy_from_user(&kvm_sigmask, argp,
  1767. sizeof kvm_sigmask))
  1768. goto out;
  1769. r = -EINVAL;
  1770. if (kvm_sigmask.len != sizeof csigset)
  1771. goto out;
  1772. r = -EFAULT;
  1773. if (copy_from_user(&csigset, sigmask_arg->sigset,
  1774. sizeof csigset))
  1775. goto out;
  1776. }
  1777. sigset_from_compat(&sigset, &csigset);
  1778. r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
  1779. break;
  1780. }
  1781. default:
  1782. r = kvm_vcpu_ioctl(filp, ioctl, arg);
  1783. }
  1784. out:
  1785. return r;
  1786. }
  1787. #endif
  1788. static long kvm_vm_ioctl(struct file *filp,
  1789. unsigned int ioctl, unsigned long arg)
  1790. {
  1791. struct kvm *kvm = filp->private_data;
  1792. void __user *argp = (void __user *)arg;
  1793. int r;
  1794. if (kvm->mm != current->mm)
  1795. return -EIO;
  1796. switch (ioctl) {
  1797. case KVM_CREATE_VCPU:
  1798. r = kvm_vm_ioctl_create_vcpu(kvm, arg);
  1799. if (r < 0)
  1800. goto out;
  1801. break;
  1802. case KVM_SET_USER_MEMORY_REGION: {
  1803. struct kvm_userspace_memory_region kvm_userspace_mem;
  1804. r = -EFAULT;
  1805. if (copy_from_user(&kvm_userspace_mem, argp,
  1806. sizeof kvm_userspace_mem))
  1807. goto out;
  1808. r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
  1809. if (r)
  1810. goto out;
  1811. break;
  1812. }
  1813. case KVM_GET_DIRTY_LOG: {
  1814. struct kvm_dirty_log log;
  1815. r = -EFAULT;
  1816. if (copy_from_user(&log, argp, sizeof log))
  1817. goto out;
  1818. r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
  1819. if (r)
  1820. goto out;
  1821. break;
  1822. }
  1823. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  1824. case KVM_REGISTER_COALESCED_MMIO: {
  1825. struct kvm_coalesced_mmio_zone zone;
  1826. r = -EFAULT;
  1827. if (copy_from_user(&zone, argp, sizeof zone))
  1828. goto out;
  1829. r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
  1830. if (r)
  1831. goto out;
  1832. r = 0;
  1833. break;
  1834. }
  1835. case KVM_UNREGISTER_COALESCED_MMIO: {
  1836. struct kvm_coalesced_mmio_zone zone;
  1837. r = -EFAULT;
  1838. if (copy_from_user(&zone, argp, sizeof zone))
  1839. goto out;
  1840. r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
  1841. if (r)
  1842. goto out;
  1843. r = 0;
  1844. break;
  1845. }
  1846. #endif
  1847. case KVM_IRQFD: {
  1848. struct kvm_irqfd data;
  1849. r = -EFAULT;
  1850. if (copy_from_user(&data, argp, sizeof data))
  1851. goto out;
  1852. r = kvm_irqfd(kvm, &data);
  1853. break;
  1854. }
  1855. case KVM_IOEVENTFD: {
  1856. struct kvm_ioeventfd data;
  1857. r = -EFAULT;
  1858. if (copy_from_user(&data, argp, sizeof data))
  1859. goto out;
  1860. r = kvm_ioeventfd(kvm, &data);
  1861. break;
  1862. }
  1863. #ifdef CONFIG_KVM_APIC_ARCHITECTURE
  1864. case KVM_SET_BOOT_CPU_ID:
  1865. r = 0;
  1866. mutex_lock(&kvm->lock);
  1867. if (atomic_read(&kvm->online_vcpus) != 0)
  1868. r = -EBUSY;
  1869. else
  1870. kvm->bsp_vcpu_id = arg;
  1871. mutex_unlock(&kvm->lock);
  1872. break;
  1873. #endif
  1874. #ifdef CONFIG_HAVE_KVM_MSI
  1875. case KVM_SIGNAL_MSI: {
  1876. struct kvm_msi msi;
  1877. r = -EFAULT;
  1878. if (copy_from_user(&msi, argp, sizeof msi))
  1879. goto out;
  1880. r = kvm_send_userspace_msi(kvm, &msi);
  1881. break;
  1882. }
  1883. #endif
  1884. #ifdef __KVM_HAVE_IRQ_LINE
  1885. case KVM_IRQ_LINE_STATUS:
  1886. case KVM_IRQ_LINE: {
  1887. struct kvm_irq_level irq_event;
  1888. r = -EFAULT;
  1889. if (copy_from_user(&irq_event, argp, sizeof irq_event))
  1890. goto out;
  1891. r = kvm_vm_ioctl_irq_line(kvm, &irq_event);
  1892. if (r)
  1893. goto out;
  1894. r = -EFAULT;
  1895. if (ioctl == KVM_IRQ_LINE_STATUS) {
  1896. if (copy_to_user(argp, &irq_event, sizeof irq_event))
  1897. goto out;
  1898. }
  1899. r = 0;
  1900. break;
  1901. }
  1902. #endif
  1903. default:
  1904. r = kvm_arch_vm_ioctl(filp, ioctl, arg);
  1905. if (r == -ENOTTY)
  1906. r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
  1907. }
  1908. out:
  1909. return r;
  1910. }
  1911. #ifdef CONFIG_COMPAT
  1912. struct compat_kvm_dirty_log {
  1913. __u32 slot;
  1914. __u32 padding1;
  1915. union {
  1916. compat_uptr_t dirty_bitmap; /* one bit per page */
  1917. __u64 padding2;
  1918. };
  1919. };
  1920. static long kvm_vm_compat_ioctl(struct file *filp,
  1921. unsigned int ioctl, unsigned long arg)
  1922. {
  1923. struct kvm *kvm = filp->private_data;
  1924. int r;
  1925. if (kvm->mm != current->mm)
  1926. return -EIO;
  1927. switch (ioctl) {
  1928. case KVM_GET_DIRTY_LOG: {
  1929. struct compat_kvm_dirty_log compat_log;
  1930. struct kvm_dirty_log log;
  1931. r = -EFAULT;
  1932. if (copy_from_user(&compat_log, (void __user *)arg,
  1933. sizeof(compat_log)))
  1934. goto out;
  1935. log.slot = compat_log.slot;
  1936. log.padding1 = compat_log.padding1;
  1937. log.padding2 = compat_log.padding2;
  1938. log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
  1939. r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
  1940. if (r)
  1941. goto out;
  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);