kvm_main.c 66 KB

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