kvm_main.c 67 KB

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