kvm_main.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953
  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. unsigned long i;
  603. struct kvm_memory_slot *memslot;
  604. struct kvm_memory_slot old, new;
  605. struct kvm_memslots *slots, *old_memslots;
  606. r = check_memory_region_flags(mem);
  607. if (r)
  608. goto out;
  609. r = -EINVAL;
  610. /* General sanity checks */
  611. if (mem->memory_size & (PAGE_SIZE - 1))
  612. goto out;
  613. if (mem->guest_phys_addr & (PAGE_SIZE - 1))
  614. goto out;
  615. /* We can read the guest memory with __xxx_user() later on. */
  616. if (user_alloc &&
  617. ((mem->userspace_addr & (PAGE_SIZE - 1)) ||
  618. !access_ok(VERIFY_WRITE,
  619. (void __user *)(unsigned long)mem->userspace_addr,
  620. mem->memory_size)))
  621. goto out;
  622. if (mem->slot >= KVM_MEM_SLOTS_NUM)
  623. goto out;
  624. if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
  625. goto out;
  626. memslot = id_to_memslot(kvm->memslots, mem->slot);
  627. base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
  628. npages = mem->memory_size >> PAGE_SHIFT;
  629. r = -EINVAL;
  630. if (npages > KVM_MEM_MAX_NR_PAGES)
  631. goto out;
  632. if (!npages)
  633. mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
  634. new = old = *memslot;
  635. new.id = mem->slot;
  636. new.base_gfn = base_gfn;
  637. new.npages = npages;
  638. new.flags = mem->flags;
  639. /* Disallow changing a memory slot's size. */
  640. r = -EINVAL;
  641. if (npages && old.npages && npages != old.npages)
  642. goto out_free;
  643. /* Check for overlaps */
  644. r = -EEXIST;
  645. for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
  646. struct kvm_memory_slot *s = &kvm->memslots->memslots[i];
  647. if (s == memslot || !s->npages)
  648. continue;
  649. if (!((base_gfn + npages <= s->base_gfn) ||
  650. (base_gfn >= s->base_gfn + s->npages)))
  651. goto out_free;
  652. }
  653. /* Free page dirty bitmap if unneeded */
  654. if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
  655. new.dirty_bitmap = NULL;
  656. r = -ENOMEM;
  657. /* Allocate if a slot is being created */
  658. if (npages && !old.npages) {
  659. new.user_alloc = user_alloc;
  660. new.userspace_addr = mem->userspace_addr;
  661. if (kvm_arch_create_memslot(&new, npages))
  662. goto out_free;
  663. }
  664. /* Allocate page dirty bitmap if needed */
  665. if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
  666. if (kvm_create_dirty_bitmap(&new) < 0)
  667. goto out_free;
  668. /* destroy any largepage mappings for dirty tracking */
  669. }
  670. if (!npages || base_gfn != old.base_gfn) {
  671. struct kvm_memory_slot *slot;
  672. r = -ENOMEM;
  673. slots = kmemdup(kvm->memslots, sizeof(struct kvm_memslots),
  674. GFP_KERNEL);
  675. if (!slots)
  676. goto out_free;
  677. slot = id_to_memslot(slots, mem->slot);
  678. slot->flags |= KVM_MEMSLOT_INVALID;
  679. update_memslots(slots, NULL);
  680. old_memslots = kvm->memslots;
  681. rcu_assign_pointer(kvm->memslots, slots);
  682. synchronize_srcu_expedited(&kvm->srcu);
  683. /* From this point no new shadow pages pointing to a deleted,
  684. * or moved, memslot will be created.
  685. *
  686. * validation of sp->gfn happens in:
  687. * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
  688. * - kvm_is_visible_gfn (mmu_check_roots)
  689. */
  690. kvm_arch_flush_shadow_memslot(kvm, slot);
  691. kfree(old_memslots);
  692. }
  693. r = kvm_arch_prepare_memory_region(kvm, &new, old, mem, user_alloc);
  694. if (r)
  695. goto out_free;
  696. /* map/unmap the pages in iommu page table */
  697. if (npages) {
  698. r = kvm_iommu_map_pages(kvm, &new);
  699. if (r)
  700. goto out_free;
  701. } else
  702. kvm_iommu_unmap_pages(kvm, &old);
  703. r = -ENOMEM;
  704. slots = kmemdup(kvm->memslots, sizeof(struct kvm_memslots),
  705. GFP_KERNEL);
  706. if (!slots)
  707. goto out_free;
  708. /* actual memory is freed via old in kvm_free_physmem_slot below */
  709. if (!npages) {
  710. new.dirty_bitmap = NULL;
  711. memset(&new.arch, 0, sizeof(new.arch));
  712. }
  713. update_memslots(slots, &new);
  714. old_memslots = kvm->memslots;
  715. rcu_assign_pointer(kvm->memslots, slots);
  716. synchronize_srcu_expedited(&kvm->srcu);
  717. kvm_arch_commit_memory_region(kvm, mem, old, user_alloc);
  718. kvm_free_physmem_slot(&old, &new);
  719. kfree(old_memslots);
  720. return 0;
  721. out_free:
  722. kvm_free_physmem_slot(&new, &old);
  723. out:
  724. return r;
  725. }
  726. EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
  727. int kvm_set_memory_region(struct kvm *kvm,
  728. struct kvm_userspace_memory_region *mem,
  729. int user_alloc)
  730. {
  731. int r;
  732. mutex_lock(&kvm->slots_lock);
  733. r = __kvm_set_memory_region(kvm, mem, user_alloc);
  734. mutex_unlock(&kvm->slots_lock);
  735. return r;
  736. }
  737. EXPORT_SYMBOL_GPL(kvm_set_memory_region);
  738. int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
  739. struct
  740. kvm_userspace_memory_region *mem,
  741. int user_alloc)
  742. {
  743. if (mem->slot >= KVM_MEMORY_SLOTS)
  744. return -EINVAL;
  745. return kvm_set_memory_region(kvm, mem, user_alloc);
  746. }
  747. int kvm_get_dirty_log(struct kvm *kvm,
  748. struct kvm_dirty_log *log, int *is_dirty)
  749. {
  750. struct kvm_memory_slot *memslot;
  751. int r, i;
  752. unsigned long n;
  753. unsigned long any = 0;
  754. r = -EINVAL;
  755. if (log->slot >= KVM_MEMORY_SLOTS)
  756. goto out;
  757. memslot = id_to_memslot(kvm->memslots, log->slot);
  758. r = -ENOENT;
  759. if (!memslot->dirty_bitmap)
  760. goto out;
  761. n = kvm_dirty_bitmap_bytes(memslot);
  762. for (i = 0; !any && i < n/sizeof(long); ++i)
  763. any = memslot->dirty_bitmap[i];
  764. r = -EFAULT;
  765. if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
  766. goto out;
  767. if (any)
  768. *is_dirty = 1;
  769. r = 0;
  770. out:
  771. return r;
  772. }
  773. bool kvm_largepages_enabled(void)
  774. {
  775. return largepages_enabled;
  776. }
  777. void kvm_disable_largepages(void)
  778. {
  779. largepages_enabled = false;
  780. }
  781. EXPORT_SYMBOL_GPL(kvm_disable_largepages);
  782. struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
  783. {
  784. return __gfn_to_memslot(kvm_memslots(kvm), gfn);
  785. }
  786. EXPORT_SYMBOL_GPL(gfn_to_memslot);
  787. int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
  788. {
  789. struct kvm_memory_slot *memslot = gfn_to_memslot(kvm, gfn);
  790. if (!memslot || memslot->id >= KVM_MEMORY_SLOTS ||
  791. memslot->flags & KVM_MEMSLOT_INVALID)
  792. return 0;
  793. return 1;
  794. }
  795. EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
  796. unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn)
  797. {
  798. struct vm_area_struct *vma;
  799. unsigned long addr, size;
  800. size = PAGE_SIZE;
  801. addr = gfn_to_hva(kvm, gfn);
  802. if (kvm_is_error_hva(addr))
  803. return PAGE_SIZE;
  804. down_read(&current->mm->mmap_sem);
  805. vma = find_vma(current->mm, addr);
  806. if (!vma)
  807. goto out;
  808. size = vma_kernel_pagesize(vma);
  809. out:
  810. up_read(&current->mm->mmap_sem);
  811. return size;
  812. }
  813. static bool memslot_is_readonly(struct kvm_memory_slot *slot)
  814. {
  815. return slot->flags & KVM_MEM_READONLY;
  816. }
  817. static unsigned long __gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
  818. gfn_t *nr_pages, bool write)
  819. {
  820. if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
  821. return KVM_HVA_ERR_BAD;
  822. if (memslot_is_readonly(slot) && write)
  823. return KVM_HVA_ERR_RO_BAD;
  824. if (nr_pages)
  825. *nr_pages = slot->npages - (gfn - slot->base_gfn);
  826. return __gfn_to_hva_memslot(slot, gfn);
  827. }
  828. static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
  829. gfn_t *nr_pages)
  830. {
  831. return __gfn_to_hva_many(slot, gfn, nr_pages, true);
  832. }
  833. unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot,
  834. gfn_t gfn)
  835. {
  836. return gfn_to_hva_many(slot, gfn, NULL);
  837. }
  838. EXPORT_SYMBOL_GPL(gfn_to_hva_memslot);
  839. unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
  840. {
  841. return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL);
  842. }
  843. EXPORT_SYMBOL_GPL(gfn_to_hva);
  844. /*
  845. * The hva returned by this function is only allowed to be read.
  846. * It should pair with kvm_read_hva() or kvm_read_hva_atomic().
  847. */
  848. static unsigned long gfn_to_hva_read(struct kvm *kvm, gfn_t gfn)
  849. {
  850. return __gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL, false);
  851. }
  852. static int kvm_read_hva(void *data, void __user *hva, int len)
  853. {
  854. return __copy_from_user(data, hva, len);
  855. }
  856. static int kvm_read_hva_atomic(void *data, void __user *hva, int len)
  857. {
  858. return __copy_from_user_inatomic(data, hva, len);
  859. }
  860. int get_user_page_nowait(struct task_struct *tsk, struct mm_struct *mm,
  861. unsigned long start, int write, struct page **page)
  862. {
  863. int flags = FOLL_TOUCH | FOLL_NOWAIT | FOLL_HWPOISON | FOLL_GET;
  864. if (write)
  865. flags |= FOLL_WRITE;
  866. return __get_user_pages(tsk, mm, start, 1, flags, page, NULL, NULL);
  867. }
  868. static inline int check_user_page_hwpoison(unsigned long addr)
  869. {
  870. int rc, flags = FOLL_TOUCH | FOLL_HWPOISON | FOLL_WRITE;
  871. rc = __get_user_pages(current, current->mm, addr, 1,
  872. flags, NULL, NULL, NULL);
  873. return rc == -EHWPOISON;
  874. }
  875. /*
  876. * The atomic path to get the writable pfn which will be stored in @pfn,
  877. * true indicates success, otherwise false is returned.
  878. */
  879. static bool hva_to_pfn_fast(unsigned long addr, bool atomic, bool *async,
  880. bool write_fault, bool *writable, pfn_t *pfn)
  881. {
  882. struct page *page[1];
  883. int npages;
  884. if (!(async || atomic))
  885. return false;
  886. /*
  887. * Fast pin a writable pfn only if it is a write fault request
  888. * or the caller allows to map a writable pfn for a read fault
  889. * request.
  890. */
  891. if (!(write_fault || writable))
  892. return false;
  893. npages = __get_user_pages_fast(addr, 1, 1, page);
  894. if (npages == 1) {
  895. *pfn = page_to_pfn(page[0]);
  896. if (writable)
  897. *writable = true;
  898. return true;
  899. }
  900. return false;
  901. }
  902. /*
  903. * The slow path to get the pfn of the specified host virtual address,
  904. * 1 indicates success, -errno is returned if error is detected.
  905. */
  906. static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault,
  907. bool *writable, pfn_t *pfn)
  908. {
  909. struct page *page[1];
  910. int npages = 0;
  911. might_sleep();
  912. if (writable)
  913. *writable = write_fault;
  914. if (async) {
  915. down_read(&current->mm->mmap_sem);
  916. npages = get_user_page_nowait(current, current->mm,
  917. addr, write_fault, page);
  918. up_read(&current->mm->mmap_sem);
  919. } else
  920. npages = get_user_pages_fast(addr, 1, write_fault,
  921. page);
  922. if (npages != 1)
  923. return npages;
  924. /* map read fault as writable if possible */
  925. if (unlikely(!write_fault) && writable) {
  926. struct page *wpage[1];
  927. npages = __get_user_pages_fast(addr, 1, 1, wpage);
  928. if (npages == 1) {
  929. *writable = true;
  930. put_page(page[0]);
  931. page[0] = wpage[0];
  932. }
  933. npages = 1;
  934. }
  935. *pfn = page_to_pfn(page[0]);
  936. return npages;
  937. }
  938. static bool vma_is_valid(struct vm_area_struct *vma, bool write_fault)
  939. {
  940. if (unlikely(!(vma->vm_flags & VM_READ)))
  941. return false;
  942. if (write_fault && (unlikely(!(vma->vm_flags & VM_WRITE))))
  943. return false;
  944. return true;
  945. }
  946. /*
  947. * Pin guest page in memory and return its pfn.
  948. * @addr: host virtual address which maps memory to the guest
  949. * @atomic: whether this function can sleep
  950. * @async: whether this function need to wait IO complete if the
  951. * host page is not in the memory
  952. * @write_fault: whether we should get a writable host page
  953. * @writable: whether it allows to map a writable host page for !@write_fault
  954. *
  955. * The function will map a writable host page for these two cases:
  956. * 1): @write_fault = true
  957. * 2): @write_fault = false && @writable, @writable will tell the caller
  958. * whether the mapping is writable.
  959. */
  960. static pfn_t hva_to_pfn(unsigned long addr, bool atomic, bool *async,
  961. bool write_fault, bool *writable)
  962. {
  963. struct vm_area_struct *vma;
  964. pfn_t pfn = 0;
  965. int npages;
  966. /* we can do it either atomically or asynchronously, not both */
  967. BUG_ON(atomic && async);
  968. if (hva_to_pfn_fast(addr, atomic, async, write_fault, writable, &pfn))
  969. return pfn;
  970. if (atomic)
  971. return KVM_PFN_ERR_FAULT;
  972. npages = hva_to_pfn_slow(addr, async, write_fault, writable, &pfn);
  973. if (npages == 1)
  974. return pfn;
  975. down_read(&current->mm->mmap_sem);
  976. if (npages == -EHWPOISON ||
  977. (!async && check_user_page_hwpoison(addr))) {
  978. pfn = KVM_PFN_ERR_HWPOISON;
  979. goto exit;
  980. }
  981. vma = find_vma_intersection(current->mm, addr, addr + 1);
  982. if (vma == NULL)
  983. pfn = KVM_PFN_ERR_FAULT;
  984. else if ((vma->vm_flags & VM_PFNMAP)) {
  985. pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) +
  986. vma->vm_pgoff;
  987. BUG_ON(!kvm_is_mmio_pfn(pfn));
  988. } else {
  989. if (async && vma_is_valid(vma, write_fault))
  990. *async = true;
  991. pfn = KVM_PFN_ERR_FAULT;
  992. }
  993. exit:
  994. up_read(&current->mm->mmap_sem);
  995. return pfn;
  996. }
  997. static pfn_t
  998. __gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn, bool atomic,
  999. bool *async, bool write_fault, bool *writable)
  1000. {
  1001. unsigned long addr = __gfn_to_hva_many(slot, gfn, NULL, write_fault);
  1002. if (addr == KVM_HVA_ERR_RO_BAD)
  1003. return KVM_PFN_ERR_RO_FAULT;
  1004. if (kvm_is_error_hva(addr))
  1005. return KVM_PFN_NOSLOT;
  1006. /* Do not map writable pfn in the readonly memslot. */
  1007. if (writable && memslot_is_readonly(slot)) {
  1008. *writable = false;
  1009. writable = NULL;
  1010. }
  1011. return hva_to_pfn(addr, atomic, async, write_fault,
  1012. writable);
  1013. }
  1014. static pfn_t __gfn_to_pfn(struct kvm *kvm, gfn_t gfn, bool atomic, bool *async,
  1015. bool write_fault, bool *writable)
  1016. {
  1017. struct kvm_memory_slot *slot;
  1018. if (async)
  1019. *async = false;
  1020. slot = gfn_to_memslot(kvm, gfn);
  1021. return __gfn_to_pfn_memslot(slot, gfn, atomic, async, write_fault,
  1022. writable);
  1023. }
  1024. pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn)
  1025. {
  1026. return __gfn_to_pfn(kvm, gfn, true, NULL, true, NULL);
  1027. }
  1028. EXPORT_SYMBOL_GPL(gfn_to_pfn_atomic);
  1029. pfn_t gfn_to_pfn_async(struct kvm *kvm, gfn_t gfn, bool *async,
  1030. bool write_fault, bool *writable)
  1031. {
  1032. return __gfn_to_pfn(kvm, gfn, false, async, write_fault, writable);
  1033. }
  1034. EXPORT_SYMBOL_GPL(gfn_to_pfn_async);
  1035. pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
  1036. {
  1037. return __gfn_to_pfn(kvm, gfn, false, NULL, true, NULL);
  1038. }
  1039. EXPORT_SYMBOL_GPL(gfn_to_pfn);
  1040. pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
  1041. bool *writable)
  1042. {
  1043. return __gfn_to_pfn(kvm, gfn, false, NULL, write_fault, writable);
  1044. }
  1045. EXPORT_SYMBOL_GPL(gfn_to_pfn_prot);
  1046. pfn_t gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn)
  1047. {
  1048. return __gfn_to_pfn_memslot(slot, gfn, false, NULL, true, NULL);
  1049. }
  1050. pfn_t gfn_to_pfn_memslot_atomic(struct kvm_memory_slot *slot, gfn_t gfn)
  1051. {
  1052. return __gfn_to_pfn_memslot(slot, gfn, true, NULL, true, NULL);
  1053. }
  1054. EXPORT_SYMBOL_GPL(gfn_to_pfn_memslot_atomic);
  1055. int gfn_to_page_many_atomic(struct kvm *kvm, gfn_t gfn, struct page **pages,
  1056. int nr_pages)
  1057. {
  1058. unsigned long addr;
  1059. gfn_t entry;
  1060. addr = gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, &entry);
  1061. if (kvm_is_error_hva(addr))
  1062. return -1;
  1063. if (entry < nr_pages)
  1064. return 0;
  1065. return __get_user_pages_fast(addr, nr_pages, 1, pages);
  1066. }
  1067. EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic);
  1068. static struct page *kvm_pfn_to_page(pfn_t pfn)
  1069. {
  1070. if (is_error_noslot_pfn(pfn))
  1071. return KVM_ERR_PTR_BAD_PAGE;
  1072. if (kvm_is_mmio_pfn(pfn)) {
  1073. WARN_ON(1);
  1074. return KVM_ERR_PTR_BAD_PAGE;
  1075. }
  1076. return pfn_to_page(pfn);
  1077. }
  1078. struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
  1079. {
  1080. pfn_t pfn;
  1081. pfn = gfn_to_pfn(kvm, gfn);
  1082. return kvm_pfn_to_page(pfn);
  1083. }
  1084. EXPORT_SYMBOL_GPL(gfn_to_page);
  1085. void kvm_release_page_clean(struct page *page)
  1086. {
  1087. WARN_ON(is_error_page(page));
  1088. kvm_release_pfn_clean(page_to_pfn(page));
  1089. }
  1090. EXPORT_SYMBOL_GPL(kvm_release_page_clean);
  1091. void kvm_release_pfn_clean(pfn_t pfn)
  1092. {
  1093. if (!is_error_noslot_pfn(pfn) && !kvm_is_mmio_pfn(pfn))
  1094. put_page(pfn_to_page(pfn));
  1095. }
  1096. EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
  1097. void kvm_release_page_dirty(struct page *page)
  1098. {
  1099. WARN_ON(is_error_page(page));
  1100. kvm_release_pfn_dirty(page_to_pfn(page));
  1101. }
  1102. EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
  1103. void kvm_release_pfn_dirty(pfn_t pfn)
  1104. {
  1105. kvm_set_pfn_dirty(pfn);
  1106. kvm_release_pfn_clean(pfn);
  1107. }
  1108. EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
  1109. void kvm_set_page_dirty(struct page *page)
  1110. {
  1111. kvm_set_pfn_dirty(page_to_pfn(page));
  1112. }
  1113. EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
  1114. void kvm_set_pfn_dirty(pfn_t pfn)
  1115. {
  1116. if (!kvm_is_mmio_pfn(pfn)) {
  1117. struct page *page = pfn_to_page(pfn);
  1118. if (!PageReserved(page))
  1119. SetPageDirty(page);
  1120. }
  1121. }
  1122. EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
  1123. void kvm_set_pfn_accessed(pfn_t pfn)
  1124. {
  1125. if (!kvm_is_mmio_pfn(pfn))
  1126. mark_page_accessed(pfn_to_page(pfn));
  1127. }
  1128. EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
  1129. void kvm_get_pfn(pfn_t pfn)
  1130. {
  1131. if (!kvm_is_mmio_pfn(pfn))
  1132. get_page(pfn_to_page(pfn));
  1133. }
  1134. EXPORT_SYMBOL_GPL(kvm_get_pfn);
  1135. static int next_segment(unsigned long len, int offset)
  1136. {
  1137. if (len > PAGE_SIZE - offset)
  1138. return PAGE_SIZE - offset;
  1139. else
  1140. return len;
  1141. }
  1142. int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
  1143. int len)
  1144. {
  1145. int r;
  1146. unsigned long addr;
  1147. addr = gfn_to_hva_read(kvm, gfn);
  1148. if (kvm_is_error_hva(addr))
  1149. return -EFAULT;
  1150. r = kvm_read_hva(data, (void __user *)addr + offset, len);
  1151. if (r)
  1152. return -EFAULT;
  1153. return 0;
  1154. }
  1155. EXPORT_SYMBOL_GPL(kvm_read_guest_page);
  1156. int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
  1157. {
  1158. gfn_t gfn = gpa >> PAGE_SHIFT;
  1159. int seg;
  1160. int offset = offset_in_page(gpa);
  1161. int ret;
  1162. while ((seg = next_segment(len, offset)) != 0) {
  1163. ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
  1164. if (ret < 0)
  1165. return ret;
  1166. offset = 0;
  1167. len -= seg;
  1168. data += seg;
  1169. ++gfn;
  1170. }
  1171. return 0;
  1172. }
  1173. EXPORT_SYMBOL_GPL(kvm_read_guest);
  1174. int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
  1175. unsigned long len)
  1176. {
  1177. int r;
  1178. unsigned long addr;
  1179. gfn_t gfn = gpa >> PAGE_SHIFT;
  1180. int offset = offset_in_page(gpa);
  1181. addr = gfn_to_hva_read(kvm, gfn);
  1182. if (kvm_is_error_hva(addr))
  1183. return -EFAULT;
  1184. pagefault_disable();
  1185. r = kvm_read_hva_atomic(data, (void __user *)addr + offset, len);
  1186. pagefault_enable();
  1187. if (r)
  1188. return -EFAULT;
  1189. return 0;
  1190. }
  1191. EXPORT_SYMBOL(kvm_read_guest_atomic);
  1192. int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
  1193. int offset, int len)
  1194. {
  1195. int r;
  1196. unsigned long addr;
  1197. addr = gfn_to_hva(kvm, gfn);
  1198. if (kvm_is_error_hva(addr))
  1199. return -EFAULT;
  1200. r = __copy_to_user((void __user *)addr + offset, data, len);
  1201. if (r)
  1202. return -EFAULT;
  1203. mark_page_dirty(kvm, gfn);
  1204. return 0;
  1205. }
  1206. EXPORT_SYMBOL_GPL(kvm_write_guest_page);
  1207. int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
  1208. unsigned long len)
  1209. {
  1210. gfn_t gfn = gpa >> PAGE_SHIFT;
  1211. int seg;
  1212. int offset = offset_in_page(gpa);
  1213. int ret;
  1214. while ((seg = next_segment(len, offset)) != 0) {
  1215. ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
  1216. if (ret < 0)
  1217. return ret;
  1218. offset = 0;
  1219. len -= seg;
  1220. data += seg;
  1221. ++gfn;
  1222. }
  1223. return 0;
  1224. }
  1225. int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
  1226. gpa_t gpa)
  1227. {
  1228. struct kvm_memslots *slots = kvm_memslots(kvm);
  1229. int offset = offset_in_page(gpa);
  1230. gfn_t gfn = gpa >> PAGE_SHIFT;
  1231. ghc->gpa = gpa;
  1232. ghc->generation = slots->generation;
  1233. ghc->memslot = gfn_to_memslot(kvm, gfn);
  1234. ghc->hva = gfn_to_hva_many(ghc->memslot, gfn, NULL);
  1235. if (!kvm_is_error_hva(ghc->hva))
  1236. ghc->hva += offset;
  1237. else
  1238. return -EFAULT;
  1239. return 0;
  1240. }
  1241. EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init);
  1242. int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
  1243. void *data, unsigned long len)
  1244. {
  1245. struct kvm_memslots *slots = kvm_memslots(kvm);
  1246. int r;
  1247. if (slots->generation != ghc->generation)
  1248. kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa);
  1249. if (kvm_is_error_hva(ghc->hva))
  1250. return -EFAULT;
  1251. r = __copy_to_user((void __user *)ghc->hva, data, len);
  1252. if (r)
  1253. return -EFAULT;
  1254. mark_page_dirty_in_slot(kvm, ghc->memslot, ghc->gpa >> PAGE_SHIFT);
  1255. return 0;
  1256. }
  1257. EXPORT_SYMBOL_GPL(kvm_write_guest_cached);
  1258. int kvm_read_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
  1259. void *data, unsigned long len)
  1260. {
  1261. struct kvm_memslots *slots = kvm_memslots(kvm);
  1262. int r;
  1263. if (slots->generation != ghc->generation)
  1264. kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa);
  1265. if (kvm_is_error_hva(ghc->hva))
  1266. return -EFAULT;
  1267. r = __copy_from_user(data, (void __user *)ghc->hva, len);
  1268. if (r)
  1269. return -EFAULT;
  1270. return 0;
  1271. }
  1272. EXPORT_SYMBOL_GPL(kvm_read_guest_cached);
  1273. int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
  1274. {
  1275. return kvm_write_guest_page(kvm, gfn, (const void *) empty_zero_page,
  1276. offset, len);
  1277. }
  1278. EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
  1279. int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
  1280. {
  1281. gfn_t gfn = gpa >> PAGE_SHIFT;
  1282. int seg;
  1283. int offset = offset_in_page(gpa);
  1284. int ret;
  1285. while ((seg = next_segment(len, offset)) != 0) {
  1286. ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
  1287. if (ret < 0)
  1288. return ret;
  1289. offset = 0;
  1290. len -= seg;
  1291. ++gfn;
  1292. }
  1293. return 0;
  1294. }
  1295. EXPORT_SYMBOL_GPL(kvm_clear_guest);
  1296. void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot,
  1297. gfn_t gfn)
  1298. {
  1299. if (memslot && memslot->dirty_bitmap) {
  1300. unsigned long rel_gfn = gfn - memslot->base_gfn;
  1301. set_bit_le(rel_gfn, memslot->dirty_bitmap);
  1302. }
  1303. }
  1304. void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
  1305. {
  1306. struct kvm_memory_slot *memslot;
  1307. memslot = gfn_to_memslot(kvm, gfn);
  1308. mark_page_dirty_in_slot(kvm, memslot, gfn);
  1309. }
  1310. /*
  1311. * The vCPU has executed a HLT instruction with in-kernel mode enabled.
  1312. */
  1313. void kvm_vcpu_block(struct kvm_vcpu *vcpu)
  1314. {
  1315. DEFINE_WAIT(wait);
  1316. for (;;) {
  1317. prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
  1318. if (kvm_arch_vcpu_runnable(vcpu)) {
  1319. kvm_make_request(KVM_REQ_UNHALT, vcpu);
  1320. break;
  1321. }
  1322. if (kvm_cpu_has_pending_timer(vcpu))
  1323. break;
  1324. if (signal_pending(current))
  1325. break;
  1326. schedule();
  1327. }
  1328. finish_wait(&vcpu->wq, &wait);
  1329. }
  1330. #ifndef CONFIG_S390
  1331. /*
  1332. * Kick a sleeping VCPU, or a guest VCPU in guest mode, into host kernel mode.
  1333. */
  1334. void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
  1335. {
  1336. int me;
  1337. int cpu = vcpu->cpu;
  1338. wait_queue_head_t *wqp;
  1339. wqp = kvm_arch_vcpu_wq(vcpu);
  1340. if (waitqueue_active(wqp)) {
  1341. wake_up_interruptible(wqp);
  1342. ++vcpu->stat.halt_wakeup;
  1343. }
  1344. me = get_cpu();
  1345. if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
  1346. if (kvm_arch_vcpu_should_kick(vcpu))
  1347. smp_send_reschedule(cpu);
  1348. put_cpu();
  1349. }
  1350. #endif /* !CONFIG_S390 */
  1351. void kvm_resched(struct kvm_vcpu *vcpu)
  1352. {
  1353. if (!need_resched())
  1354. return;
  1355. cond_resched();
  1356. }
  1357. EXPORT_SYMBOL_GPL(kvm_resched);
  1358. bool kvm_vcpu_yield_to(struct kvm_vcpu *target)
  1359. {
  1360. struct pid *pid;
  1361. struct task_struct *task = NULL;
  1362. rcu_read_lock();
  1363. pid = rcu_dereference(target->pid);
  1364. if (pid)
  1365. task = get_pid_task(target->pid, PIDTYPE_PID);
  1366. rcu_read_unlock();
  1367. if (!task)
  1368. return false;
  1369. if (task->flags & PF_VCPU) {
  1370. put_task_struct(task);
  1371. return false;
  1372. }
  1373. if (yield_to(task, 1)) {
  1374. put_task_struct(task);
  1375. return true;
  1376. }
  1377. put_task_struct(task);
  1378. return false;
  1379. }
  1380. EXPORT_SYMBOL_GPL(kvm_vcpu_yield_to);
  1381. #ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
  1382. /*
  1383. * Helper that checks whether a VCPU is eligible for directed yield.
  1384. * Most eligible candidate to yield is decided by following heuristics:
  1385. *
  1386. * (a) VCPU which has not done pl-exit or cpu relax intercepted recently
  1387. * (preempted lock holder), indicated by @in_spin_loop.
  1388. * Set at the beiginning and cleared at the end of interception/PLE handler.
  1389. *
  1390. * (b) VCPU which has done pl-exit/ cpu relax intercepted but did not get
  1391. * chance last time (mostly it has become eligible now since we have probably
  1392. * yielded to lockholder in last iteration. This is done by toggling
  1393. * @dy_eligible each time a VCPU checked for eligibility.)
  1394. *
  1395. * Yielding to a recently pl-exited/cpu relax intercepted VCPU before yielding
  1396. * to preempted lock-holder could result in wrong VCPU selection and CPU
  1397. * burning. Giving priority for a potential lock-holder increases lock
  1398. * progress.
  1399. *
  1400. * Since algorithm is based on heuristics, accessing another VCPU data without
  1401. * locking does not harm. It may result in trying to yield to same VCPU, fail
  1402. * and continue with next VCPU and so on.
  1403. */
  1404. bool kvm_vcpu_eligible_for_directed_yield(struct kvm_vcpu *vcpu)
  1405. {
  1406. bool eligible;
  1407. eligible = !vcpu->spin_loop.in_spin_loop ||
  1408. (vcpu->spin_loop.in_spin_loop &&
  1409. vcpu->spin_loop.dy_eligible);
  1410. if (vcpu->spin_loop.in_spin_loop)
  1411. kvm_vcpu_set_dy_eligible(vcpu, !vcpu->spin_loop.dy_eligible);
  1412. return eligible;
  1413. }
  1414. #endif
  1415. void kvm_vcpu_on_spin(struct kvm_vcpu *me)
  1416. {
  1417. struct kvm *kvm = me->kvm;
  1418. struct kvm_vcpu *vcpu;
  1419. int last_boosted_vcpu = me->kvm->last_boosted_vcpu;
  1420. int yielded = 0;
  1421. int pass;
  1422. int i;
  1423. kvm_vcpu_set_in_spin_loop(me, true);
  1424. /*
  1425. * We boost the priority of a VCPU that is runnable but not
  1426. * currently running, because it got preempted by something
  1427. * else and called schedule in __vcpu_run. Hopefully that
  1428. * VCPU is holding the lock that we need and will release it.
  1429. * We approximate round-robin by starting at the last boosted VCPU.
  1430. */
  1431. for (pass = 0; pass < 2 && !yielded; pass++) {
  1432. kvm_for_each_vcpu(i, vcpu, kvm) {
  1433. if (!pass && i <= last_boosted_vcpu) {
  1434. i = last_boosted_vcpu;
  1435. continue;
  1436. } else if (pass && i > last_boosted_vcpu)
  1437. break;
  1438. if (vcpu == me)
  1439. continue;
  1440. if (waitqueue_active(&vcpu->wq))
  1441. continue;
  1442. if (!kvm_vcpu_eligible_for_directed_yield(vcpu))
  1443. continue;
  1444. if (kvm_vcpu_yield_to(vcpu)) {
  1445. kvm->last_boosted_vcpu = i;
  1446. yielded = 1;
  1447. break;
  1448. }
  1449. }
  1450. }
  1451. kvm_vcpu_set_in_spin_loop(me, false);
  1452. /* Ensure vcpu is not eligible during next spinloop */
  1453. kvm_vcpu_set_dy_eligible(me, false);
  1454. }
  1455. EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
  1456. static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1457. {
  1458. struct kvm_vcpu *vcpu = vma->vm_file->private_data;
  1459. struct page *page;
  1460. if (vmf->pgoff == 0)
  1461. page = virt_to_page(vcpu->run);
  1462. #ifdef CONFIG_X86
  1463. else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
  1464. page = virt_to_page(vcpu->arch.pio_data);
  1465. #endif
  1466. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  1467. else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
  1468. page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
  1469. #endif
  1470. else
  1471. return kvm_arch_vcpu_fault(vcpu, vmf);
  1472. get_page(page);
  1473. vmf->page = page;
  1474. return 0;
  1475. }
  1476. static const struct vm_operations_struct kvm_vcpu_vm_ops = {
  1477. .fault = kvm_vcpu_fault,
  1478. };
  1479. static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
  1480. {
  1481. vma->vm_ops = &kvm_vcpu_vm_ops;
  1482. return 0;
  1483. }
  1484. static int kvm_vcpu_release(struct inode *inode, struct file *filp)
  1485. {
  1486. struct kvm_vcpu *vcpu = filp->private_data;
  1487. kvm_put_kvm(vcpu->kvm);
  1488. return 0;
  1489. }
  1490. static struct file_operations kvm_vcpu_fops = {
  1491. .release = kvm_vcpu_release,
  1492. .unlocked_ioctl = kvm_vcpu_ioctl,
  1493. #ifdef CONFIG_COMPAT
  1494. .compat_ioctl = kvm_vcpu_compat_ioctl,
  1495. #endif
  1496. .mmap = kvm_vcpu_mmap,
  1497. .llseek = noop_llseek,
  1498. };
  1499. /*
  1500. * Allocates an inode for the vcpu.
  1501. */
  1502. static int create_vcpu_fd(struct kvm_vcpu *vcpu)
  1503. {
  1504. return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR);
  1505. }
  1506. /*
  1507. * Creates some virtual cpus. Good luck creating more than one.
  1508. */
  1509. static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
  1510. {
  1511. int r;
  1512. struct kvm_vcpu *vcpu, *v;
  1513. vcpu = kvm_arch_vcpu_create(kvm, id);
  1514. if (IS_ERR(vcpu))
  1515. return PTR_ERR(vcpu);
  1516. preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
  1517. r = kvm_arch_vcpu_setup(vcpu);
  1518. if (r)
  1519. goto vcpu_destroy;
  1520. mutex_lock(&kvm->lock);
  1521. if (!kvm_vcpu_compatible(vcpu)) {
  1522. r = -EINVAL;
  1523. goto unlock_vcpu_destroy;
  1524. }
  1525. if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
  1526. r = -EINVAL;
  1527. goto unlock_vcpu_destroy;
  1528. }
  1529. kvm_for_each_vcpu(r, v, kvm)
  1530. if (v->vcpu_id == id) {
  1531. r = -EEXIST;
  1532. goto unlock_vcpu_destroy;
  1533. }
  1534. BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
  1535. /* Now it's all set up, let userspace reach it */
  1536. kvm_get_kvm(kvm);
  1537. r = create_vcpu_fd(vcpu);
  1538. if (r < 0) {
  1539. kvm_put_kvm(kvm);
  1540. goto unlock_vcpu_destroy;
  1541. }
  1542. kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
  1543. smp_wmb();
  1544. atomic_inc(&kvm->online_vcpus);
  1545. mutex_unlock(&kvm->lock);
  1546. kvm_arch_vcpu_postcreate(vcpu);
  1547. return r;
  1548. unlock_vcpu_destroy:
  1549. mutex_unlock(&kvm->lock);
  1550. vcpu_destroy:
  1551. kvm_arch_vcpu_destroy(vcpu);
  1552. return r;
  1553. }
  1554. static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
  1555. {
  1556. if (sigset) {
  1557. sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
  1558. vcpu->sigset_active = 1;
  1559. vcpu->sigset = *sigset;
  1560. } else
  1561. vcpu->sigset_active = 0;
  1562. return 0;
  1563. }
  1564. static long kvm_vcpu_ioctl(struct file *filp,
  1565. unsigned int ioctl, unsigned long arg)
  1566. {
  1567. struct kvm_vcpu *vcpu = filp->private_data;
  1568. void __user *argp = (void __user *)arg;
  1569. int r;
  1570. struct kvm_fpu *fpu = NULL;
  1571. struct kvm_sregs *kvm_sregs = NULL;
  1572. if (vcpu->kvm->mm != current->mm)
  1573. return -EIO;
  1574. #if defined(CONFIG_S390) || defined(CONFIG_PPC)
  1575. /*
  1576. * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
  1577. * so vcpu_load() would break it.
  1578. */
  1579. if (ioctl == KVM_S390_INTERRUPT || ioctl == KVM_INTERRUPT)
  1580. return kvm_arch_vcpu_ioctl(filp, ioctl, arg);
  1581. #endif
  1582. r = vcpu_load(vcpu);
  1583. if (r)
  1584. return r;
  1585. switch (ioctl) {
  1586. case KVM_RUN:
  1587. r = -EINVAL;
  1588. if (arg)
  1589. goto out;
  1590. r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
  1591. trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
  1592. break;
  1593. case KVM_GET_REGS: {
  1594. struct kvm_regs *kvm_regs;
  1595. r = -ENOMEM;
  1596. kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
  1597. if (!kvm_regs)
  1598. goto out;
  1599. r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
  1600. if (r)
  1601. goto out_free1;
  1602. r = -EFAULT;
  1603. if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
  1604. goto out_free1;
  1605. r = 0;
  1606. out_free1:
  1607. kfree(kvm_regs);
  1608. break;
  1609. }
  1610. case KVM_SET_REGS: {
  1611. struct kvm_regs *kvm_regs;
  1612. r = -ENOMEM;
  1613. kvm_regs = memdup_user(argp, sizeof(*kvm_regs));
  1614. if (IS_ERR(kvm_regs)) {
  1615. r = PTR_ERR(kvm_regs);
  1616. goto out;
  1617. }
  1618. r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
  1619. kfree(kvm_regs);
  1620. break;
  1621. }
  1622. case KVM_GET_SREGS: {
  1623. kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
  1624. r = -ENOMEM;
  1625. if (!kvm_sregs)
  1626. goto out;
  1627. r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
  1628. if (r)
  1629. goto out;
  1630. r = -EFAULT;
  1631. if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
  1632. goto out;
  1633. r = 0;
  1634. break;
  1635. }
  1636. case KVM_SET_SREGS: {
  1637. kvm_sregs = memdup_user(argp, sizeof(*kvm_sregs));
  1638. if (IS_ERR(kvm_sregs)) {
  1639. r = PTR_ERR(kvm_sregs);
  1640. kvm_sregs = NULL;
  1641. goto out;
  1642. }
  1643. r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
  1644. break;
  1645. }
  1646. case KVM_GET_MP_STATE: {
  1647. struct kvm_mp_state mp_state;
  1648. r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
  1649. if (r)
  1650. goto out;
  1651. r = -EFAULT;
  1652. if (copy_to_user(argp, &mp_state, sizeof mp_state))
  1653. goto out;
  1654. r = 0;
  1655. break;
  1656. }
  1657. case KVM_SET_MP_STATE: {
  1658. struct kvm_mp_state mp_state;
  1659. r = -EFAULT;
  1660. if (copy_from_user(&mp_state, argp, sizeof mp_state))
  1661. goto out;
  1662. r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
  1663. break;
  1664. }
  1665. case KVM_TRANSLATE: {
  1666. struct kvm_translation tr;
  1667. r = -EFAULT;
  1668. if (copy_from_user(&tr, argp, sizeof tr))
  1669. goto out;
  1670. r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
  1671. if (r)
  1672. goto out;
  1673. r = -EFAULT;
  1674. if (copy_to_user(argp, &tr, sizeof tr))
  1675. goto out;
  1676. r = 0;
  1677. break;
  1678. }
  1679. case KVM_SET_GUEST_DEBUG: {
  1680. struct kvm_guest_debug dbg;
  1681. r = -EFAULT;
  1682. if (copy_from_user(&dbg, argp, sizeof dbg))
  1683. goto out;
  1684. r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
  1685. break;
  1686. }
  1687. case KVM_SET_SIGNAL_MASK: {
  1688. struct kvm_signal_mask __user *sigmask_arg = argp;
  1689. struct kvm_signal_mask kvm_sigmask;
  1690. sigset_t sigset, *p;
  1691. p = NULL;
  1692. if (argp) {
  1693. r = -EFAULT;
  1694. if (copy_from_user(&kvm_sigmask, argp,
  1695. sizeof kvm_sigmask))
  1696. goto out;
  1697. r = -EINVAL;
  1698. if (kvm_sigmask.len != sizeof sigset)
  1699. goto out;
  1700. r = -EFAULT;
  1701. if (copy_from_user(&sigset, sigmask_arg->sigset,
  1702. sizeof sigset))
  1703. goto out;
  1704. p = &sigset;
  1705. }
  1706. r = kvm_vcpu_ioctl_set_sigmask(vcpu, p);
  1707. break;
  1708. }
  1709. case KVM_GET_FPU: {
  1710. fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
  1711. r = -ENOMEM;
  1712. if (!fpu)
  1713. goto out;
  1714. r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
  1715. if (r)
  1716. goto out;
  1717. r = -EFAULT;
  1718. if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
  1719. goto out;
  1720. r = 0;
  1721. break;
  1722. }
  1723. case KVM_SET_FPU: {
  1724. fpu = memdup_user(argp, sizeof(*fpu));
  1725. if (IS_ERR(fpu)) {
  1726. r = PTR_ERR(fpu);
  1727. fpu = NULL;
  1728. goto out;
  1729. }
  1730. r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
  1731. break;
  1732. }
  1733. default:
  1734. r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
  1735. }
  1736. out:
  1737. vcpu_put(vcpu);
  1738. kfree(fpu);
  1739. kfree(kvm_sregs);
  1740. return r;
  1741. }
  1742. #ifdef CONFIG_COMPAT
  1743. static long kvm_vcpu_compat_ioctl(struct file *filp,
  1744. unsigned int ioctl, unsigned long arg)
  1745. {
  1746. struct kvm_vcpu *vcpu = filp->private_data;
  1747. void __user *argp = compat_ptr(arg);
  1748. int r;
  1749. if (vcpu->kvm->mm != current->mm)
  1750. return -EIO;
  1751. switch (ioctl) {
  1752. case KVM_SET_SIGNAL_MASK: {
  1753. struct kvm_signal_mask __user *sigmask_arg = argp;
  1754. struct kvm_signal_mask kvm_sigmask;
  1755. compat_sigset_t csigset;
  1756. sigset_t sigset;
  1757. if (argp) {
  1758. r = -EFAULT;
  1759. if (copy_from_user(&kvm_sigmask, argp,
  1760. sizeof kvm_sigmask))
  1761. goto out;
  1762. r = -EINVAL;
  1763. if (kvm_sigmask.len != sizeof csigset)
  1764. goto out;
  1765. r = -EFAULT;
  1766. if (copy_from_user(&csigset, sigmask_arg->sigset,
  1767. sizeof csigset))
  1768. goto out;
  1769. sigset_from_compat(&sigset, &csigset);
  1770. r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
  1771. } else
  1772. r = kvm_vcpu_ioctl_set_sigmask(vcpu, NULL);
  1773. break;
  1774. }
  1775. default:
  1776. r = kvm_vcpu_ioctl(filp, ioctl, arg);
  1777. }
  1778. out:
  1779. return r;
  1780. }
  1781. #endif
  1782. static long kvm_vm_ioctl(struct file *filp,
  1783. unsigned int ioctl, unsigned long arg)
  1784. {
  1785. struct kvm *kvm = filp->private_data;
  1786. void __user *argp = (void __user *)arg;
  1787. int r;
  1788. if (kvm->mm != current->mm)
  1789. return -EIO;
  1790. switch (ioctl) {
  1791. case KVM_CREATE_VCPU:
  1792. r = kvm_vm_ioctl_create_vcpu(kvm, arg);
  1793. break;
  1794. case KVM_SET_USER_MEMORY_REGION: {
  1795. struct kvm_userspace_memory_region kvm_userspace_mem;
  1796. r = -EFAULT;
  1797. if (copy_from_user(&kvm_userspace_mem, argp,
  1798. sizeof kvm_userspace_mem))
  1799. goto out;
  1800. r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
  1801. break;
  1802. }
  1803. case KVM_GET_DIRTY_LOG: {
  1804. struct kvm_dirty_log log;
  1805. r = -EFAULT;
  1806. if (copy_from_user(&log, argp, sizeof log))
  1807. goto out;
  1808. r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
  1809. break;
  1810. }
  1811. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  1812. case KVM_REGISTER_COALESCED_MMIO: {
  1813. struct kvm_coalesced_mmio_zone zone;
  1814. r = -EFAULT;
  1815. if (copy_from_user(&zone, argp, sizeof zone))
  1816. goto out;
  1817. r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
  1818. break;
  1819. }
  1820. case KVM_UNREGISTER_COALESCED_MMIO: {
  1821. struct kvm_coalesced_mmio_zone zone;
  1822. r = -EFAULT;
  1823. if (copy_from_user(&zone, argp, sizeof zone))
  1824. goto out;
  1825. r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
  1826. break;
  1827. }
  1828. #endif
  1829. case KVM_IRQFD: {
  1830. struct kvm_irqfd data;
  1831. r = -EFAULT;
  1832. if (copy_from_user(&data, argp, sizeof data))
  1833. goto out;
  1834. r = kvm_irqfd(kvm, &data);
  1835. break;
  1836. }
  1837. case KVM_IOEVENTFD: {
  1838. struct kvm_ioeventfd data;
  1839. r = -EFAULT;
  1840. if (copy_from_user(&data, argp, sizeof data))
  1841. goto out;
  1842. r = kvm_ioeventfd(kvm, &data);
  1843. break;
  1844. }
  1845. #ifdef CONFIG_KVM_APIC_ARCHITECTURE
  1846. case KVM_SET_BOOT_CPU_ID:
  1847. r = 0;
  1848. mutex_lock(&kvm->lock);
  1849. if (atomic_read(&kvm->online_vcpus) != 0)
  1850. r = -EBUSY;
  1851. else
  1852. kvm->bsp_vcpu_id = arg;
  1853. mutex_unlock(&kvm->lock);
  1854. break;
  1855. #endif
  1856. #ifdef CONFIG_HAVE_KVM_MSI
  1857. case KVM_SIGNAL_MSI: {
  1858. struct kvm_msi msi;
  1859. r = -EFAULT;
  1860. if (copy_from_user(&msi, argp, sizeof msi))
  1861. goto out;
  1862. r = kvm_send_userspace_msi(kvm, &msi);
  1863. break;
  1864. }
  1865. #endif
  1866. #ifdef __KVM_HAVE_IRQ_LINE
  1867. case KVM_IRQ_LINE_STATUS:
  1868. case KVM_IRQ_LINE: {
  1869. struct kvm_irq_level irq_event;
  1870. r = -EFAULT;
  1871. if (copy_from_user(&irq_event, argp, sizeof irq_event))
  1872. goto out;
  1873. r = kvm_vm_ioctl_irq_line(kvm, &irq_event);
  1874. if (r)
  1875. goto out;
  1876. r = -EFAULT;
  1877. if (ioctl == KVM_IRQ_LINE_STATUS) {
  1878. if (copy_to_user(argp, &irq_event, sizeof irq_event))
  1879. goto out;
  1880. }
  1881. r = 0;
  1882. break;
  1883. }
  1884. #endif
  1885. default:
  1886. r = kvm_arch_vm_ioctl(filp, ioctl, arg);
  1887. if (r == -ENOTTY)
  1888. r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
  1889. }
  1890. out:
  1891. return r;
  1892. }
  1893. #ifdef CONFIG_COMPAT
  1894. struct compat_kvm_dirty_log {
  1895. __u32 slot;
  1896. __u32 padding1;
  1897. union {
  1898. compat_uptr_t dirty_bitmap; /* one bit per page */
  1899. __u64 padding2;
  1900. };
  1901. };
  1902. static long kvm_vm_compat_ioctl(struct file *filp,
  1903. unsigned int ioctl, unsigned long arg)
  1904. {
  1905. struct kvm *kvm = filp->private_data;
  1906. int r;
  1907. if (kvm->mm != current->mm)
  1908. return -EIO;
  1909. switch (ioctl) {
  1910. case KVM_GET_DIRTY_LOG: {
  1911. struct compat_kvm_dirty_log compat_log;
  1912. struct kvm_dirty_log log;
  1913. r = -EFAULT;
  1914. if (copy_from_user(&compat_log, (void __user *)arg,
  1915. sizeof(compat_log)))
  1916. goto out;
  1917. log.slot = compat_log.slot;
  1918. log.padding1 = compat_log.padding1;
  1919. log.padding2 = compat_log.padding2;
  1920. log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
  1921. r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
  1922. break;
  1923. }
  1924. default:
  1925. r = kvm_vm_ioctl(filp, ioctl, arg);
  1926. }
  1927. out:
  1928. return r;
  1929. }
  1930. #endif
  1931. static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1932. {
  1933. struct page *page[1];
  1934. unsigned long addr;
  1935. int npages;
  1936. gfn_t gfn = vmf->pgoff;
  1937. struct kvm *kvm = vma->vm_file->private_data;
  1938. addr = gfn_to_hva(kvm, gfn);
  1939. if (kvm_is_error_hva(addr))
  1940. return VM_FAULT_SIGBUS;
  1941. npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
  1942. NULL);
  1943. if (unlikely(npages != 1))
  1944. return VM_FAULT_SIGBUS;
  1945. vmf->page = page[0];
  1946. return 0;
  1947. }
  1948. static const struct vm_operations_struct kvm_vm_vm_ops = {
  1949. .fault = kvm_vm_fault,
  1950. };
  1951. static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
  1952. {
  1953. vma->vm_ops = &kvm_vm_vm_ops;
  1954. return 0;
  1955. }
  1956. static struct file_operations kvm_vm_fops = {
  1957. .release = kvm_vm_release,
  1958. .unlocked_ioctl = kvm_vm_ioctl,
  1959. #ifdef CONFIG_COMPAT
  1960. .compat_ioctl = kvm_vm_compat_ioctl,
  1961. #endif
  1962. .mmap = kvm_vm_mmap,
  1963. .llseek = noop_llseek,
  1964. };
  1965. static int kvm_dev_ioctl_create_vm(unsigned long type)
  1966. {
  1967. int r;
  1968. struct kvm *kvm;
  1969. kvm = kvm_create_vm(type);
  1970. if (IS_ERR(kvm))
  1971. return PTR_ERR(kvm);
  1972. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  1973. r = kvm_coalesced_mmio_init(kvm);
  1974. if (r < 0) {
  1975. kvm_put_kvm(kvm);
  1976. return r;
  1977. }
  1978. #endif
  1979. r = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
  1980. if (r < 0)
  1981. kvm_put_kvm(kvm);
  1982. return r;
  1983. }
  1984. static long kvm_dev_ioctl_check_extension_generic(long arg)
  1985. {
  1986. switch (arg) {
  1987. case KVM_CAP_USER_MEMORY:
  1988. case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
  1989. case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
  1990. #ifdef CONFIG_KVM_APIC_ARCHITECTURE
  1991. case KVM_CAP_SET_BOOT_CPU_ID:
  1992. #endif
  1993. case KVM_CAP_INTERNAL_ERROR_DATA:
  1994. #ifdef CONFIG_HAVE_KVM_MSI
  1995. case KVM_CAP_SIGNAL_MSI:
  1996. #endif
  1997. return 1;
  1998. #ifdef KVM_CAP_IRQ_ROUTING
  1999. case KVM_CAP_IRQ_ROUTING:
  2000. return KVM_MAX_IRQ_ROUTES;
  2001. #endif
  2002. default:
  2003. break;
  2004. }
  2005. return kvm_dev_ioctl_check_extension(arg);
  2006. }
  2007. static long kvm_dev_ioctl(struct file *filp,
  2008. unsigned int ioctl, unsigned long arg)
  2009. {
  2010. long r = -EINVAL;
  2011. switch (ioctl) {
  2012. case KVM_GET_API_VERSION:
  2013. r = -EINVAL;
  2014. if (arg)
  2015. goto out;
  2016. r = KVM_API_VERSION;
  2017. break;
  2018. case KVM_CREATE_VM:
  2019. r = kvm_dev_ioctl_create_vm(arg);
  2020. break;
  2021. case KVM_CHECK_EXTENSION:
  2022. r = kvm_dev_ioctl_check_extension_generic(arg);
  2023. break;
  2024. case KVM_GET_VCPU_MMAP_SIZE:
  2025. r = -EINVAL;
  2026. if (arg)
  2027. goto out;
  2028. r = PAGE_SIZE; /* struct kvm_run */
  2029. #ifdef CONFIG_X86
  2030. r += PAGE_SIZE; /* pio data page */
  2031. #endif
  2032. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  2033. r += PAGE_SIZE; /* coalesced mmio ring page */
  2034. #endif
  2035. break;
  2036. case KVM_TRACE_ENABLE:
  2037. case KVM_TRACE_PAUSE:
  2038. case KVM_TRACE_DISABLE:
  2039. r = -EOPNOTSUPP;
  2040. break;
  2041. default:
  2042. return kvm_arch_dev_ioctl(filp, ioctl, arg);
  2043. }
  2044. out:
  2045. return r;
  2046. }
  2047. static struct file_operations kvm_chardev_ops = {
  2048. .unlocked_ioctl = kvm_dev_ioctl,
  2049. .compat_ioctl = kvm_dev_ioctl,
  2050. .llseek = noop_llseek,
  2051. };
  2052. static struct miscdevice kvm_dev = {
  2053. KVM_MINOR,
  2054. "kvm",
  2055. &kvm_chardev_ops,
  2056. };
  2057. static void hardware_enable_nolock(void *junk)
  2058. {
  2059. int cpu = raw_smp_processor_id();
  2060. int r;
  2061. if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
  2062. return;
  2063. cpumask_set_cpu(cpu, cpus_hardware_enabled);
  2064. r = kvm_arch_hardware_enable(NULL);
  2065. if (r) {
  2066. cpumask_clear_cpu(cpu, cpus_hardware_enabled);
  2067. atomic_inc(&hardware_enable_failed);
  2068. printk(KERN_INFO "kvm: enabling virtualization on "
  2069. "CPU%d failed\n", cpu);
  2070. }
  2071. }
  2072. static void hardware_enable(void *junk)
  2073. {
  2074. raw_spin_lock(&kvm_lock);
  2075. hardware_enable_nolock(junk);
  2076. raw_spin_unlock(&kvm_lock);
  2077. }
  2078. static void hardware_disable_nolock(void *junk)
  2079. {
  2080. int cpu = raw_smp_processor_id();
  2081. if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
  2082. return;
  2083. cpumask_clear_cpu(cpu, cpus_hardware_enabled);
  2084. kvm_arch_hardware_disable(NULL);
  2085. }
  2086. static void hardware_disable(void *junk)
  2087. {
  2088. raw_spin_lock(&kvm_lock);
  2089. hardware_disable_nolock(junk);
  2090. raw_spin_unlock(&kvm_lock);
  2091. }
  2092. static void hardware_disable_all_nolock(void)
  2093. {
  2094. BUG_ON(!kvm_usage_count);
  2095. kvm_usage_count--;
  2096. if (!kvm_usage_count)
  2097. on_each_cpu(hardware_disable_nolock, NULL, 1);
  2098. }
  2099. static void hardware_disable_all(void)
  2100. {
  2101. raw_spin_lock(&kvm_lock);
  2102. hardware_disable_all_nolock();
  2103. raw_spin_unlock(&kvm_lock);
  2104. }
  2105. static int hardware_enable_all(void)
  2106. {
  2107. int r = 0;
  2108. raw_spin_lock(&kvm_lock);
  2109. kvm_usage_count++;
  2110. if (kvm_usage_count == 1) {
  2111. atomic_set(&hardware_enable_failed, 0);
  2112. on_each_cpu(hardware_enable_nolock, NULL, 1);
  2113. if (atomic_read(&hardware_enable_failed)) {
  2114. hardware_disable_all_nolock();
  2115. r = -EBUSY;
  2116. }
  2117. }
  2118. raw_spin_unlock(&kvm_lock);
  2119. return r;
  2120. }
  2121. static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
  2122. void *v)
  2123. {
  2124. int cpu = (long)v;
  2125. if (!kvm_usage_count)
  2126. return NOTIFY_OK;
  2127. val &= ~CPU_TASKS_FROZEN;
  2128. switch (val) {
  2129. case CPU_DYING:
  2130. printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
  2131. cpu);
  2132. hardware_disable(NULL);
  2133. break;
  2134. case CPU_STARTING:
  2135. printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
  2136. cpu);
  2137. hardware_enable(NULL);
  2138. break;
  2139. }
  2140. return NOTIFY_OK;
  2141. }
  2142. asmlinkage void kvm_spurious_fault(void)
  2143. {
  2144. /* Fault while not rebooting. We want the trace. */
  2145. BUG();
  2146. }
  2147. EXPORT_SYMBOL_GPL(kvm_spurious_fault);
  2148. static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
  2149. void *v)
  2150. {
  2151. /*
  2152. * Some (well, at least mine) BIOSes hang on reboot if
  2153. * in vmx root mode.
  2154. *
  2155. * And Intel TXT required VMX off for all cpu when system shutdown.
  2156. */
  2157. printk(KERN_INFO "kvm: exiting hardware virtualization\n");
  2158. kvm_rebooting = true;
  2159. on_each_cpu(hardware_disable_nolock, NULL, 1);
  2160. return NOTIFY_OK;
  2161. }
  2162. static struct notifier_block kvm_reboot_notifier = {
  2163. .notifier_call = kvm_reboot,
  2164. .priority = 0,
  2165. };
  2166. static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
  2167. {
  2168. int i;
  2169. for (i = 0; i < bus->dev_count; i++) {
  2170. struct kvm_io_device *pos = bus->range[i].dev;
  2171. kvm_iodevice_destructor(pos);
  2172. }
  2173. kfree(bus);
  2174. }
  2175. int kvm_io_bus_sort_cmp(const void *p1, const void *p2)
  2176. {
  2177. const struct kvm_io_range *r1 = p1;
  2178. const struct kvm_io_range *r2 = p2;
  2179. if (r1->addr < r2->addr)
  2180. return -1;
  2181. if (r1->addr + r1->len > r2->addr + r2->len)
  2182. return 1;
  2183. return 0;
  2184. }
  2185. int kvm_io_bus_insert_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev,
  2186. gpa_t addr, int len)
  2187. {
  2188. bus->range[bus->dev_count++] = (struct kvm_io_range) {
  2189. .addr = addr,
  2190. .len = len,
  2191. .dev = dev,
  2192. };
  2193. sort(bus->range, bus->dev_count, sizeof(struct kvm_io_range),
  2194. kvm_io_bus_sort_cmp, NULL);
  2195. return 0;
  2196. }
  2197. int kvm_io_bus_get_first_dev(struct kvm_io_bus *bus,
  2198. gpa_t addr, int len)
  2199. {
  2200. struct kvm_io_range *range, key;
  2201. int off;
  2202. key = (struct kvm_io_range) {
  2203. .addr = addr,
  2204. .len = len,
  2205. };
  2206. range = bsearch(&key, bus->range, bus->dev_count,
  2207. sizeof(struct kvm_io_range), kvm_io_bus_sort_cmp);
  2208. if (range == NULL)
  2209. return -ENOENT;
  2210. off = range - bus->range;
  2211. while (off > 0 && kvm_io_bus_sort_cmp(&key, &bus->range[off-1]) == 0)
  2212. off--;
  2213. return off;
  2214. }
  2215. /* kvm_io_bus_write - called under kvm->slots_lock */
  2216. int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
  2217. int len, const void *val)
  2218. {
  2219. int idx;
  2220. struct kvm_io_bus *bus;
  2221. struct kvm_io_range range;
  2222. range = (struct kvm_io_range) {
  2223. .addr = addr,
  2224. .len = len,
  2225. };
  2226. bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
  2227. idx = kvm_io_bus_get_first_dev(bus, addr, len);
  2228. if (idx < 0)
  2229. return -EOPNOTSUPP;
  2230. while (idx < bus->dev_count &&
  2231. kvm_io_bus_sort_cmp(&range, &bus->range[idx]) == 0) {
  2232. if (!kvm_iodevice_write(bus->range[idx].dev, addr, len, val))
  2233. return 0;
  2234. idx++;
  2235. }
  2236. return -EOPNOTSUPP;
  2237. }
  2238. /* kvm_io_bus_read - called under kvm->slots_lock */
  2239. int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
  2240. int len, void *val)
  2241. {
  2242. int idx;
  2243. struct kvm_io_bus *bus;
  2244. struct kvm_io_range range;
  2245. range = (struct kvm_io_range) {
  2246. .addr = addr,
  2247. .len = len,
  2248. };
  2249. bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
  2250. idx = kvm_io_bus_get_first_dev(bus, addr, len);
  2251. if (idx < 0)
  2252. return -EOPNOTSUPP;
  2253. while (idx < bus->dev_count &&
  2254. kvm_io_bus_sort_cmp(&range, &bus->range[idx]) == 0) {
  2255. if (!kvm_iodevice_read(bus->range[idx].dev, addr, len, val))
  2256. return 0;
  2257. idx++;
  2258. }
  2259. return -EOPNOTSUPP;
  2260. }
  2261. /* Caller must hold slots_lock. */
  2262. int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
  2263. int len, struct kvm_io_device *dev)
  2264. {
  2265. struct kvm_io_bus *new_bus, *bus;
  2266. bus = kvm->buses[bus_idx];
  2267. if (bus->dev_count > NR_IOBUS_DEVS - 1)
  2268. return -ENOSPC;
  2269. new_bus = kzalloc(sizeof(*bus) + ((bus->dev_count + 1) *
  2270. sizeof(struct kvm_io_range)), GFP_KERNEL);
  2271. if (!new_bus)
  2272. return -ENOMEM;
  2273. memcpy(new_bus, bus, sizeof(*bus) + (bus->dev_count *
  2274. sizeof(struct kvm_io_range)));
  2275. kvm_io_bus_insert_dev(new_bus, dev, addr, len);
  2276. rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
  2277. synchronize_srcu_expedited(&kvm->srcu);
  2278. kfree(bus);
  2279. return 0;
  2280. }
  2281. /* Caller must hold slots_lock. */
  2282. int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
  2283. struct kvm_io_device *dev)
  2284. {
  2285. int i, r;
  2286. struct kvm_io_bus *new_bus, *bus;
  2287. bus = kvm->buses[bus_idx];
  2288. r = -ENOENT;
  2289. for (i = 0; i < bus->dev_count; i++)
  2290. if (bus->range[i].dev == dev) {
  2291. r = 0;
  2292. break;
  2293. }
  2294. if (r)
  2295. return r;
  2296. new_bus = kzalloc(sizeof(*bus) + ((bus->dev_count - 1) *
  2297. sizeof(struct kvm_io_range)), GFP_KERNEL);
  2298. if (!new_bus)
  2299. return -ENOMEM;
  2300. memcpy(new_bus, bus, sizeof(*bus) + i * sizeof(struct kvm_io_range));
  2301. new_bus->dev_count--;
  2302. memcpy(new_bus->range + i, bus->range + i + 1,
  2303. (new_bus->dev_count - i) * sizeof(struct kvm_io_range));
  2304. rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
  2305. synchronize_srcu_expedited(&kvm->srcu);
  2306. kfree(bus);
  2307. return r;
  2308. }
  2309. static struct notifier_block kvm_cpu_notifier = {
  2310. .notifier_call = kvm_cpu_hotplug,
  2311. };
  2312. static int vm_stat_get(void *_offset, u64 *val)
  2313. {
  2314. unsigned offset = (long)_offset;
  2315. struct kvm *kvm;
  2316. *val = 0;
  2317. raw_spin_lock(&kvm_lock);
  2318. list_for_each_entry(kvm, &vm_list, vm_list)
  2319. *val += *(u32 *)((void *)kvm + offset);
  2320. raw_spin_unlock(&kvm_lock);
  2321. return 0;
  2322. }
  2323. DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
  2324. static int vcpu_stat_get(void *_offset, u64 *val)
  2325. {
  2326. unsigned offset = (long)_offset;
  2327. struct kvm *kvm;
  2328. struct kvm_vcpu *vcpu;
  2329. int i;
  2330. *val = 0;
  2331. raw_spin_lock(&kvm_lock);
  2332. list_for_each_entry(kvm, &vm_list, vm_list)
  2333. kvm_for_each_vcpu(i, vcpu, kvm)
  2334. *val += *(u32 *)((void *)vcpu + offset);
  2335. raw_spin_unlock(&kvm_lock);
  2336. return 0;
  2337. }
  2338. DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
  2339. static const struct file_operations *stat_fops[] = {
  2340. [KVM_STAT_VCPU] = &vcpu_stat_fops,
  2341. [KVM_STAT_VM] = &vm_stat_fops,
  2342. };
  2343. static int kvm_init_debug(void)
  2344. {
  2345. int r = -EFAULT;
  2346. struct kvm_stats_debugfs_item *p;
  2347. kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
  2348. if (kvm_debugfs_dir == NULL)
  2349. goto out;
  2350. for (p = debugfs_entries; p->name; ++p) {
  2351. p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
  2352. (void *)(long)p->offset,
  2353. stat_fops[p->kind]);
  2354. if (p->dentry == NULL)
  2355. goto out_dir;
  2356. }
  2357. return 0;
  2358. out_dir:
  2359. debugfs_remove_recursive(kvm_debugfs_dir);
  2360. out:
  2361. return r;
  2362. }
  2363. static void kvm_exit_debug(void)
  2364. {
  2365. struct kvm_stats_debugfs_item *p;
  2366. for (p = debugfs_entries; p->name; ++p)
  2367. debugfs_remove(p->dentry);
  2368. debugfs_remove(kvm_debugfs_dir);
  2369. }
  2370. static int kvm_suspend(void)
  2371. {
  2372. if (kvm_usage_count)
  2373. hardware_disable_nolock(NULL);
  2374. return 0;
  2375. }
  2376. static void kvm_resume(void)
  2377. {
  2378. if (kvm_usage_count) {
  2379. WARN_ON(raw_spin_is_locked(&kvm_lock));
  2380. hardware_enable_nolock(NULL);
  2381. }
  2382. }
  2383. static struct syscore_ops kvm_syscore_ops = {
  2384. .suspend = kvm_suspend,
  2385. .resume = kvm_resume,
  2386. };
  2387. static inline
  2388. struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
  2389. {
  2390. return container_of(pn, struct kvm_vcpu, preempt_notifier);
  2391. }
  2392. static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
  2393. {
  2394. struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
  2395. kvm_arch_vcpu_load(vcpu, cpu);
  2396. }
  2397. static void kvm_sched_out(struct preempt_notifier *pn,
  2398. struct task_struct *next)
  2399. {
  2400. struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
  2401. kvm_arch_vcpu_put(vcpu);
  2402. }
  2403. int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
  2404. struct module *module)
  2405. {
  2406. int r;
  2407. int cpu;
  2408. r = kvm_arch_init(opaque);
  2409. if (r)
  2410. goto out_fail;
  2411. if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
  2412. r = -ENOMEM;
  2413. goto out_free_0;
  2414. }
  2415. r = kvm_arch_hardware_setup();
  2416. if (r < 0)
  2417. goto out_free_0a;
  2418. for_each_online_cpu(cpu) {
  2419. smp_call_function_single(cpu,
  2420. kvm_arch_check_processor_compat,
  2421. &r, 1);
  2422. if (r < 0)
  2423. goto out_free_1;
  2424. }
  2425. r = register_cpu_notifier(&kvm_cpu_notifier);
  2426. if (r)
  2427. goto out_free_2;
  2428. register_reboot_notifier(&kvm_reboot_notifier);
  2429. /* A kmem cache lets us meet the alignment requirements of fx_save. */
  2430. if (!vcpu_align)
  2431. vcpu_align = __alignof__(struct kvm_vcpu);
  2432. kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
  2433. 0, NULL);
  2434. if (!kvm_vcpu_cache) {
  2435. r = -ENOMEM;
  2436. goto out_free_3;
  2437. }
  2438. r = kvm_async_pf_init();
  2439. if (r)
  2440. goto out_free;
  2441. kvm_chardev_ops.owner = module;
  2442. kvm_vm_fops.owner = module;
  2443. kvm_vcpu_fops.owner = module;
  2444. r = misc_register(&kvm_dev);
  2445. if (r) {
  2446. printk(KERN_ERR "kvm: misc device register failed\n");
  2447. goto out_unreg;
  2448. }
  2449. register_syscore_ops(&kvm_syscore_ops);
  2450. kvm_preempt_ops.sched_in = kvm_sched_in;
  2451. kvm_preempt_ops.sched_out = kvm_sched_out;
  2452. r = kvm_init_debug();
  2453. if (r) {
  2454. printk(KERN_ERR "kvm: create debugfs files failed\n");
  2455. goto out_undebugfs;
  2456. }
  2457. return 0;
  2458. out_undebugfs:
  2459. unregister_syscore_ops(&kvm_syscore_ops);
  2460. out_unreg:
  2461. kvm_async_pf_deinit();
  2462. out_free:
  2463. kmem_cache_destroy(kvm_vcpu_cache);
  2464. out_free_3:
  2465. unregister_reboot_notifier(&kvm_reboot_notifier);
  2466. unregister_cpu_notifier(&kvm_cpu_notifier);
  2467. out_free_2:
  2468. out_free_1:
  2469. kvm_arch_hardware_unsetup();
  2470. out_free_0a:
  2471. free_cpumask_var(cpus_hardware_enabled);
  2472. out_free_0:
  2473. kvm_arch_exit();
  2474. out_fail:
  2475. return r;
  2476. }
  2477. EXPORT_SYMBOL_GPL(kvm_init);
  2478. void kvm_exit(void)
  2479. {
  2480. kvm_exit_debug();
  2481. misc_deregister(&kvm_dev);
  2482. kmem_cache_destroy(kvm_vcpu_cache);
  2483. kvm_async_pf_deinit();
  2484. unregister_syscore_ops(&kvm_syscore_ops);
  2485. unregister_reboot_notifier(&kvm_reboot_notifier);
  2486. unregister_cpu_notifier(&kvm_cpu_notifier);
  2487. on_each_cpu(hardware_disable_nolock, NULL, 1);
  2488. kvm_arch_hardware_unsetup();
  2489. kvm_arch_exit();
  2490. free_cpumask_var(cpus_hardware_enabled);
  2491. }
  2492. EXPORT_SYMBOL_GPL(kvm_exit);