kvm_main.c 78 KB

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