kvm_main.c 84 KB

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