kvm_main.c 62 KB

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