kvm_main.c 78 KB

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