kvm_main.c 66 KB

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