kvm_main.c 73 KB

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