kvm_main.c 68 KB

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