kvm_main.c 76 KB

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