kvm_main.c 67 KB

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