kvm_main.c 78 KB

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