kvm_main.c 66 KB

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