kvm_main.c 74 KB

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