kvm_main.c 62 KB

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