kvm_main.c 78 KB

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