kvm_main.c 66 KB

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