kvm_main.c 85 KB

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