kvm_main.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017
  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. * KVM_SET_USER_MEMORY_REGION ioctl allows the following operations:
  604. * - create a new memory slot
  605. * - delete an existing memory slot
  606. * - modify an existing memory slot
  607. * -- move it in the guest physical memory space
  608. * -- just change its flags
  609. *
  610. * Since flags can be changed by some of these operations, the following
  611. * differentiation is the best we can do for __kvm_set_memory_region():
  612. */
  613. enum kvm_mr_change {
  614. KVM_MR_CREATE,
  615. KVM_MR_DELETE,
  616. KVM_MR_MOVE,
  617. KVM_MR_FLAGS_ONLY,
  618. };
  619. /*
  620. * Allocate some memory and give it an address in the guest physical address
  621. * space.
  622. *
  623. * Discontiguous memory is allowed, mostly for framebuffers.
  624. *
  625. * Must be called holding mmap_sem for write.
  626. */
  627. int __kvm_set_memory_region(struct kvm *kvm,
  628. struct kvm_userspace_memory_region *mem)
  629. {
  630. int r;
  631. gfn_t base_gfn;
  632. unsigned long npages;
  633. struct kvm_memory_slot *slot;
  634. struct kvm_memory_slot old, new;
  635. struct kvm_memslots *slots = NULL, *old_memslots;
  636. enum kvm_mr_change change;
  637. r = check_memory_region_flags(mem);
  638. if (r)
  639. goto out;
  640. r = -EINVAL;
  641. /* General sanity checks */
  642. if (mem->memory_size & (PAGE_SIZE - 1))
  643. goto out;
  644. if (mem->guest_phys_addr & (PAGE_SIZE - 1))
  645. goto out;
  646. /* We can read the guest memory with __xxx_user() later on. */
  647. if ((mem->slot < KVM_USER_MEM_SLOTS) &&
  648. ((mem->userspace_addr & (PAGE_SIZE - 1)) ||
  649. !access_ok(VERIFY_WRITE,
  650. (void __user *)(unsigned long)mem->userspace_addr,
  651. mem->memory_size)))
  652. goto out;
  653. if (mem->slot >= KVM_MEM_SLOTS_NUM)
  654. goto out;
  655. if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
  656. goto out;
  657. slot = id_to_memslot(kvm->memslots, mem->slot);
  658. base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
  659. npages = mem->memory_size >> PAGE_SHIFT;
  660. r = -EINVAL;
  661. if (npages > KVM_MEM_MAX_NR_PAGES)
  662. goto out;
  663. if (!npages)
  664. mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
  665. new = old = *slot;
  666. new.id = mem->slot;
  667. new.base_gfn = base_gfn;
  668. new.npages = npages;
  669. new.flags = mem->flags;
  670. r = -EINVAL;
  671. if (npages) {
  672. if (!old.npages)
  673. change = KVM_MR_CREATE;
  674. else { /* Modify an existing slot. */
  675. if ((mem->userspace_addr != old.userspace_addr) ||
  676. (npages != old.npages) ||
  677. ((new.flags ^ old.flags) & KVM_MEM_READONLY))
  678. goto out;
  679. if (base_gfn != old.base_gfn)
  680. change = KVM_MR_MOVE;
  681. else if (new.flags != old.flags)
  682. change = KVM_MR_FLAGS_ONLY;
  683. else { /* Nothing to change. */
  684. r = 0;
  685. goto out;
  686. }
  687. }
  688. } else if (old.npages) {
  689. change = KVM_MR_DELETE;
  690. } else /* Modify a non-existent slot: disallowed. */
  691. goto out;
  692. if ((change == KVM_MR_CREATE) || (change == KVM_MR_MOVE)) {
  693. /* Check for overlaps */
  694. r = -EEXIST;
  695. kvm_for_each_memslot(slot, kvm->memslots) {
  696. if ((slot->id >= KVM_USER_MEM_SLOTS) ||
  697. (slot->id == mem->slot))
  698. continue;
  699. if (!((base_gfn + npages <= slot->base_gfn) ||
  700. (base_gfn >= slot->base_gfn + slot->npages)))
  701. goto out;
  702. }
  703. }
  704. /* Free page dirty bitmap if unneeded */
  705. if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
  706. new.dirty_bitmap = NULL;
  707. r = -ENOMEM;
  708. if (change == KVM_MR_CREATE) {
  709. new.userspace_addr = mem->userspace_addr;
  710. if (kvm_arch_create_memslot(&new, npages))
  711. goto out_free;
  712. }
  713. /* Allocate page dirty bitmap if needed */
  714. if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
  715. if (kvm_create_dirty_bitmap(&new) < 0)
  716. goto out_free;
  717. }
  718. if ((change == KVM_MR_DELETE) || (change == KVM_MR_MOVE)) {
  719. r = -ENOMEM;
  720. slots = kmemdup(kvm->memslots, sizeof(struct kvm_memslots),
  721. GFP_KERNEL);
  722. if (!slots)
  723. goto out_free;
  724. slot = id_to_memslot(slots, mem->slot);
  725. slot->flags |= KVM_MEMSLOT_INVALID;
  726. old_memslots = install_new_memslots(kvm, slots, NULL);
  727. /* slot was deleted or moved, clear iommu mapping */
  728. kvm_iommu_unmap_pages(kvm, &old);
  729. /* From this point no new shadow pages pointing to a deleted,
  730. * or moved, memslot will be created.
  731. *
  732. * validation of sp->gfn happens in:
  733. * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
  734. * - kvm_is_visible_gfn (mmu_check_roots)
  735. */
  736. kvm_arch_flush_shadow_memslot(kvm, slot);
  737. slots = old_memslots;
  738. }
  739. r = kvm_arch_prepare_memory_region(kvm, &new, old, mem);
  740. if (r)
  741. goto out_slots;
  742. r = -ENOMEM;
  743. /*
  744. * We can re-use the old_memslots from above, the only difference
  745. * from the currently installed memslots is the invalid flag. This
  746. * will get overwritten by update_memslots anyway.
  747. */
  748. if (!slots) {
  749. slots = kmemdup(kvm->memslots, sizeof(struct kvm_memslots),
  750. GFP_KERNEL);
  751. if (!slots)
  752. goto out_free;
  753. }
  754. /*
  755. * IOMMU mapping: New slots need to be mapped. Old slots need to be
  756. * un-mapped and re-mapped if their base changes. Since base change
  757. * unmapping is handled above with slot deletion, mapping alone is
  758. * needed here. Anything else the iommu might care about for existing
  759. * slots (size changes, userspace addr changes and read-only flag
  760. * changes) is disallowed above, so any other attribute changes getting
  761. * here can be skipped.
  762. */
  763. if ((change == KVM_MR_CREATE) || (change == KVM_MR_MOVE)) {
  764. r = kvm_iommu_map_pages(kvm, &new);
  765. if (r)
  766. goto out_slots;
  767. }
  768. /* actual memory is freed via old in kvm_free_physmem_slot below */
  769. if (change == KVM_MR_DELETE) {
  770. new.dirty_bitmap = NULL;
  771. memset(&new.arch, 0, sizeof(new.arch));
  772. }
  773. old_memslots = install_new_memslots(kvm, slots, &new);
  774. kvm_arch_commit_memory_region(kvm, mem, old);
  775. kvm_free_physmem_slot(&old, &new);
  776. kfree(old_memslots);
  777. return 0;
  778. out_slots:
  779. kfree(slots);
  780. out_free:
  781. kvm_free_physmem_slot(&new, &old);
  782. out:
  783. return r;
  784. }
  785. EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
  786. int kvm_set_memory_region(struct kvm *kvm,
  787. struct kvm_userspace_memory_region *mem)
  788. {
  789. int r;
  790. mutex_lock(&kvm->slots_lock);
  791. r = __kvm_set_memory_region(kvm, mem);
  792. mutex_unlock(&kvm->slots_lock);
  793. return r;
  794. }
  795. EXPORT_SYMBOL_GPL(kvm_set_memory_region);
  796. int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
  797. struct kvm_userspace_memory_region *mem)
  798. {
  799. if (mem->slot >= KVM_USER_MEM_SLOTS)
  800. return -EINVAL;
  801. return kvm_set_memory_region(kvm, mem);
  802. }
  803. int kvm_get_dirty_log(struct kvm *kvm,
  804. struct kvm_dirty_log *log, int *is_dirty)
  805. {
  806. struct kvm_memory_slot *memslot;
  807. int r, i;
  808. unsigned long n;
  809. unsigned long any = 0;
  810. r = -EINVAL;
  811. if (log->slot >= KVM_USER_MEM_SLOTS)
  812. goto out;
  813. memslot = id_to_memslot(kvm->memslots, log->slot);
  814. r = -ENOENT;
  815. if (!memslot->dirty_bitmap)
  816. goto out;
  817. n = kvm_dirty_bitmap_bytes(memslot);
  818. for (i = 0; !any && i < n/sizeof(long); ++i)
  819. any = memslot->dirty_bitmap[i];
  820. r = -EFAULT;
  821. if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
  822. goto out;
  823. if (any)
  824. *is_dirty = 1;
  825. r = 0;
  826. out:
  827. return r;
  828. }
  829. bool kvm_largepages_enabled(void)
  830. {
  831. return largepages_enabled;
  832. }
  833. void kvm_disable_largepages(void)
  834. {
  835. largepages_enabled = false;
  836. }
  837. EXPORT_SYMBOL_GPL(kvm_disable_largepages);
  838. struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
  839. {
  840. return __gfn_to_memslot(kvm_memslots(kvm), gfn);
  841. }
  842. EXPORT_SYMBOL_GPL(gfn_to_memslot);
  843. int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
  844. {
  845. struct kvm_memory_slot *memslot = gfn_to_memslot(kvm, gfn);
  846. if (!memslot || memslot->id >= KVM_USER_MEM_SLOTS ||
  847. memslot->flags & KVM_MEMSLOT_INVALID)
  848. return 0;
  849. return 1;
  850. }
  851. EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
  852. unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn)
  853. {
  854. struct vm_area_struct *vma;
  855. unsigned long addr, size;
  856. size = PAGE_SIZE;
  857. addr = gfn_to_hva(kvm, gfn);
  858. if (kvm_is_error_hva(addr))
  859. return PAGE_SIZE;
  860. down_read(&current->mm->mmap_sem);
  861. vma = find_vma(current->mm, addr);
  862. if (!vma)
  863. goto out;
  864. size = vma_kernel_pagesize(vma);
  865. out:
  866. up_read(&current->mm->mmap_sem);
  867. return size;
  868. }
  869. static bool memslot_is_readonly(struct kvm_memory_slot *slot)
  870. {
  871. return slot->flags & KVM_MEM_READONLY;
  872. }
  873. static unsigned long __gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
  874. gfn_t *nr_pages, bool write)
  875. {
  876. if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
  877. return KVM_HVA_ERR_BAD;
  878. if (memslot_is_readonly(slot) && write)
  879. return KVM_HVA_ERR_RO_BAD;
  880. if (nr_pages)
  881. *nr_pages = slot->npages - (gfn - slot->base_gfn);
  882. return __gfn_to_hva_memslot(slot, gfn);
  883. }
  884. static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
  885. gfn_t *nr_pages)
  886. {
  887. return __gfn_to_hva_many(slot, gfn, nr_pages, true);
  888. }
  889. unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot,
  890. gfn_t gfn)
  891. {
  892. return gfn_to_hva_many(slot, gfn, NULL);
  893. }
  894. EXPORT_SYMBOL_GPL(gfn_to_hva_memslot);
  895. unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
  896. {
  897. return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL);
  898. }
  899. EXPORT_SYMBOL_GPL(gfn_to_hva);
  900. /*
  901. * The hva returned by this function is only allowed to be read.
  902. * It should pair with kvm_read_hva() or kvm_read_hva_atomic().
  903. */
  904. static unsigned long gfn_to_hva_read(struct kvm *kvm, gfn_t gfn)
  905. {
  906. return __gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL, false);
  907. }
  908. static int kvm_read_hva(void *data, void __user *hva, int len)
  909. {
  910. return __copy_from_user(data, hva, len);
  911. }
  912. static int kvm_read_hva_atomic(void *data, void __user *hva, int len)
  913. {
  914. return __copy_from_user_inatomic(data, hva, len);
  915. }
  916. int get_user_page_nowait(struct task_struct *tsk, struct mm_struct *mm,
  917. unsigned long start, int write, struct page **page)
  918. {
  919. int flags = FOLL_TOUCH | FOLL_NOWAIT | FOLL_HWPOISON | FOLL_GET;
  920. if (write)
  921. flags |= FOLL_WRITE;
  922. return __get_user_pages(tsk, mm, start, 1, flags, page, NULL, NULL);
  923. }
  924. static inline int check_user_page_hwpoison(unsigned long addr)
  925. {
  926. int rc, flags = FOLL_TOUCH | FOLL_HWPOISON | FOLL_WRITE;
  927. rc = __get_user_pages(current, current->mm, addr, 1,
  928. flags, NULL, NULL, NULL);
  929. return rc == -EHWPOISON;
  930. }
  931. /*
  932. * The atomic path to get the writable pfn which will be stored in @pfn,
  933. * true indicates success, otherwise false is returned.
  934. */
  935. static bool hva_to_pfn_fast(unsigned long addr, bool atomic, bool *async,
  936. bool write_fault, bool *writable, pfn_t *pfn)
  937. {
  938. struct page *page[1];
  939. int npages;
  940. if (!(async || atomic))
  941. return false;
  942. /*
  943. * Fast pin a writable pfn only if it is a write fault request
  944. * or the caller allows to map a writable pfn for a read fault
  945. * request.
  946. */
  947. if (!(write_fault || writable))
  948. return false;
  949. npages = __get_user_pages_fast(addr, 1, 1, page);
  950. if (npages == 1) {
  951. *pfn = page_to_pfn(page[0]);
  952. if (writable)
  953. *writable = true;
  954. return true;
  955. }
  956. return false;
  957. }
  958. /*
  959. * The slow path to get the pfn of the specified host virtual address,
  960. * 1 indicates success, -errno is returned if error is detected.
  961. */
  962. static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault,
  963. bool *writable, pfn_t *pfn)
  964. {
  965. struct page *page[1];
  966. int npages = 0;
  967. might_sleep();
  968. if (writable)
  969. *writable = write_fault;
  970. if (async) {
  971. down_read(&current->mm->mmap_sem);
  972. npages = get_user_page_nowait(current, current->mm,
  973. addr, write_fault, page);
  974. up_read(&current->mm->mmap_sem);
  975. } else
  976. npages = get_user_pages_fast(addr, 1, write_fault,
  977. page);
  978. if (npages != 1)
  979. return npages;
  980. /* map read fault as writable if possible */
  981. if (unlikely(!write_fault) && writable) {
  982. struct page *wpage[1];
  983. npages = __get_user_pages_fast(addr, 1, 1, wpage);
  984. if (npages == 1) {
  985. *writable = true;
  986. put_page(page[0]);
  987. page[0] = wpage[0];
  988. }
  989. npages = 1;
  990. }
  991. *pfn = page_to_pfn(page[0]);
  992. return npages;
  993. }
  994. static bool vma_is_valid(struct vm_area_struct *vma, bool write_fault)
  995. {
  996. if (unlikely(!(vma->vm_flags & VM_READ)))
  997. return false;
  998. if (write_fault && (unlikely(!(vma->vm_flags & VM_WRITE))))
  999. return false;
  1000. return true;
  1001. }
  1002. /*
  1003. * Pin guest page in memory and return its pfn.
  1004. * @addr: host virtual address which maps memory to the guest
  1005. * @atomic: whether this function can sleep
  1006. * @async: whether this function need to wait IO complete if the
  1007. * host page is not in the memory
  1008. * @write_fault: whether we should get a writable host page
  1009. * @writable: whether it allows to map a writable host page for !@write_fault
  1010. *
  1011. * The function will map a writable host page for these two cases:
  1012. * 1): @write_fault = true
  1013. * 2): @write_fault = false && @writable, @writable will tell the caller
  1014. * whether the mapping is writable.
  1015. */
  1016. static pfn_t hva_to_pfn(unsigned long addr, bool atomic, bool *async,
  1017. bool write_fault, bool *writable)
  1018. {
  1019. struct vm_area_struct *vma;
  1020. pfn_t pfn = 0;
  1021. int npages;
  1022. /* we can do it either atomically or asynchronously, not both */
  1023. BUG_ON(atomic && async);
  1024. if (hva_to_pfn_fast(addr, atomic, async, write_fault, writable, &pfn))
  1025. return pfn;
  1026. if (atomic)
  1027. return KVM_PFN_ERR_FAULT;
  1028. npages = hva_to_pfn_slow(addr, async, write_fault, writable, &pfn);
  1029. if (npages == 1)
  1030. return pfn;
  1031. down_read(&current->mm->mmap_sem);
  1032. if (npages == -EHWPOISON ||
  1033. (!async && check_user_page_hwpoison(addr))) {
  1034. pfn = KVM_PFN_ERR_HWPOISON;
  1035. goto exit;
  1036. }
  1037. vma = find_vma_intersection(current->mm, addr, addr + 1);
  1038. if (vma == NULL)
  1039. pfn = KVM_PFN_ERR_FAULT;
  1040. else if ((vma->vm_flags & VM_PFNMAP)) {
  1041. pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) +
  1042. vma->vm_pgoff;
  1043. BUG_ON(!kvm_is_mmio_pfn(pfn));
  1044. } else {
  1045. if (async && vma_is_valid(vma, write_fault))
  1046. *async = true;
  1047. pfn = KVM_PFN_ERR_FAULT;
  1048. }
  1049. exit:
  1050. up_read(&current->mm->mmap_sem);
  1051. return pfn;
  1052. }
  1053. static pfn_t
  1054. __gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn, bool atomic,
  1055. bool *async, bool write_fault, bool *writable)
  1056. {
  1057. unsigned long addr = __gfn_to_hva_many(slot, gfn, NULL, write_fault);
  1058. if (addr == KVM_HVA_ERR_RO_BAD)
  1059. return KVM_PFN_ERR_RO_FAULT;
  1060. if (kvm_is_error_hva(addr))
  1061. return KVM_PFN_NOSLOT;
  1062. /* Do not map writable pfn in the readonly memslot. */
  1063. if (writable && memslot_is_readonly(slot)) {
  1064. *writable = false;
  1065. writable = NULL;
  1066. }
  1067. return hva_to_pfn(addr, atomic, async, write_fault,
  1068. writable);
  1069. }
  1070. static pfn_t __gfn_to_pfn(struct kvm *kvm, gfn_t gfn, bool atomic, bool *async,
  1071. bool write_fault, bool *writable)
  1072. {
  1073. struct kvm_memory_slot *slot;
  1074. if (async)
  1075. *async = false;
  1076. slot = gfn_to_memslot(kvm, gfn);
  1077. return __gfn_to_pfn_memslot(slot, gfn, atomic, async, write_fault,
  1078. writable);
  1079. }
  1080. pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn)
  1081. {
  1082. return __gfn_to_pfn(kvm, gfn, true, NULL, true, NULL);
  1083. }
  1084. EXPORT_SYMBOL_GPL(gfn_to_pfn_atomic);
  1085. pfn_t gfn_to_pfn_async(struct kvm *kvm, gfn_t gfn, bool *async,
  1086. bool write_fault, bool *writable)
  1087. {
  1088. return __gfn_to_pfn(kvm, gfn, false, async, write_fault, writable);
  1089. }
  1090. EXPORT_SYMBOL_GPL(gfn_to_pfn_async);
  1091. pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
  1092. {
  1093. return __gfn_to_pfn(kvm, gfn, false, NULL, true, NULL);
  1094. }
  1095. EXPORT_SYMBOL_GPL(gfn_to_pfn);
  1096. pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
  1097. bool *writable)
  1098. {
  1099. return __gfn_to_pfn(kvm, gfn, false, NULL, write_fault, writable);
  1100. }
  1101. EXPORT_SYMBOL_GPL(gfn_to_pfn_prot);
  1102. pfn_t gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn)
  1103. {
  1104. return __gfn_to_pfn_memslot(slot, gfn, false, NULL, true, NULL);
  1105. }
  1106. pfn_t gfn_to_pfn_memslot_atomic(struct kvm_memory_slot *slot, gfn_t gfn)
  1107. {
  1108. return __gfn_to_pfn_memslot(slot, gfn, true, NULL, true, NULL);
  1109. }
  1110. EXPORT_SYMBOL_GPL(gfn_to_pfn_memslot_atomic);
  1111. int gfn_to_page_many_atomic(struct kvm *kvm, gfn_t gfn, struct page **pages,
  1112. int nr_pages)
  1113. {
  1114. unsigned long addr;
  1115. gfn_t entry;
  1116. addr = gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, &entry);
  1117. if (kvm_is_error_hva(addr))
  1118. return -1;
  1119. if (entry < nr_pages)
  1120. return 0;
  1121. return __get_user_pages_fast(addr, nr_pages, 1, pages);
  1122. }
  1123. EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic);
  1124. static struct page *kvm_pfn_to_page(pfn_t pfn)
  1125. {
  1126. if (is_error_noslot_pfn(pfn))
  1127. return KVM_ERR_PTR_BAD_PAGE;
  1128. if (kvm_is_mmio_pfn(pfn)) {
  1129. WARN_ON(1);
  1130. return KVM_ERR_PTR_BAD_PAGE;
  1131. }
  1132. return pfn_to_page(pfn);
  1133. }
  1134. struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
  1135. {
  1136. pfn_t pfn;
  1137. pfn = gfn_to_pfn(kvm, gfn);
  1138. return kvm_pfn_to_page(pfn);
  1139. }
  1140. EXPORT_SYMBOL_GPL(gfn_to_page);
  1141. void kvm_release_page_clean(struct page *page)
  1142. {
  1143. WARN_ON(is_error_page(page));
  1144. kvm_release_pfn_clean(page_to_pfn(page));
  1145. }
  1146. EXPORT_SYMBOL_GPL(kvm_release_page_clean);
  1147. void kvm_release_pfn_clean(pfn_t pfn)
  1148. {
  1149. if (!is_error_noslot_pfn(pfn) && !kvm_is_mmio_pfn(pfn))
  1150. put_page(pfn_to_page(pfn));
  1151. }
  1152. EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
  1153. void kvm_release_page_dirty(struct page *page)
  1154. {
  1155. WARN_ON(is_error_page(page));
  1156. kvm_release_pfn_dirty(page_to_pfn(page));
  1157. }
  1158. EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
  1159. void kvm_release_pfn_dirty(pfn_t pfn)
  1160. {
  1161. kvm_set_pfn_dirty(pfn);
  1162. kvm_release_pfn_clean(pfn);
  1163. }
  1164. EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
  1165. void kvm_set_page_dirty(struct page *page)
  1166. {
  1167. kvm_set_pfn_dirty(page_to_pfn(page));
  1168. }
  1169. EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
  1170. void kvm_set_pfn_dirty(pfn_t pfn)
  1171. {
  1172. if (!kvm_is_mmio_pfn(pfn)) {
  1173. struct page *page = pfn_to_page(pfn);
  1174. if (!PageReserved(page))
  1175. SetPageDirty(page);
  1176. }
  1177. }
  1178. EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
  1179. void kvm_set_pfn_accessed(pfn_t pfn)
  1180. {
  1181. if (!kvm_is_mmio_pfn(pfn))
  1182. mark_page_accessed(pfn_to_page(pfn));
  1183. }
  1184. EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
  1185. void kvm_get_pfn(pfn_t pfn)
  1186. {
  1187. if (!kvm_is_mmio_pfn(pfn))
  1188. get_page(pfn_to_page(pfn));
  1189. }
  1190. EXPORT_SYMBOL_GPL(kvm_get_pfn);
  1191. static int next_segment(unsigned long len, int offset)
  1192. {
  1193. if (len > PAGE_SIZE - offset)
  1194. return PAGE_SIZE - offset;
  1195. else
  1196. return len;
  1197. }
  1198. int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
  1199. int len)
  1200. {
  1201. int r;
  1202. unsigned long addr;
  1203. addr = gfn_to_hva_read(kvm, gfn);
  1204. if (kvm_is_error_hva(addr))
  1205. return -EFAULT;
  1206. r = kvm_read_hva(data, (void __user *)addr + offset, len);
  1207. if (r)
  1208. return -EFAULT;
  1209. return 0;
  1210. }
  1211. EXPORT_SYMBOL_GPL(kvm_read_guest_page);
  1212. int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
  1213. {
  1214. gfn_t gfn = gpa >> PAGE_SHIFT;
  1215. int seg;
  1216. int offset = offset_in_page(gpa);
  1217. int ret;
  1218. while ((seg = next_segment(len, offset)) != 0) {
  1219. ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
  1220. if (ret < 0)
  1221. return ret;
  1222. offset = 0;
  1223. len -= seg;
  1224. data += seg;
  1225. ++gfn;
  1226. }
  1227. return 0;
  1228. }
  1229. EXPORT_SYMBOL_GPL(kvm_read_guest);
  1230. int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
  1231. unsigned long len)
  1232. {
  1233. int r;
  1234. unsigned long addr;
  1235. gfn_t gfn = gpa >> PAGE_SHIFT;
  1236. int offset = offset_in_page(gpa);
  1237. addr = gfn_to_hva_read(kvm, gfn);
  1238. if (kvm_is_error_hva(addr))
  1239. return -EFAULT;
  1240. pagefault_disable();
  1241. r = kvm_read_hva_atomic(data, (void __user *)addr + offset, len);
  1242. pagefault_enable();
  1243. if (r)
  1244. return -EFAULT;
  1245. return 0;
  1246. }
  1247. EXPORT_SYMBOL(kvm_read_guest_atomic);
  1248. int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
  1249. int offset, int len)
  1250. {
  1251. int r;
  1252. unsigned long addr;
  1253. addr = gfn_to_hva(kvm, gfn);
  1254. if (kvm_is_error_hva(addr))
  1255. return -EFAULT;
  1256. r = __copy_to_user((void __user *)addr + offset, data, len);
  1257. if (r)
  1258. return -EFAULT;
  1259. mark_page_dirty(kvm, gfn);
  1260. return 0;
  1261. }
  1262. EXPORT_SYMBOL_GPL(kvm_write_guest_page);
  1263. int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
  1264. unsigned long len)
  1265. {
  1266. gfn_t gfn = gpa >> PAGE_SHIFT;
  1267. int seg;
  1268. int offset = offset_in_page(gpa);
  1269. int ret;
  1270. while ((seg = next_segment(len, offset)) != 0) {
  1271. ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
  1272. if (ret < 0)
  1273. return ret;
  1274. offset = 0;
  1275. len -= seg;
  1276. data += seg;
  1277. ++gfn;
  1278. }
  1279. return 0;
  1280. }
  1281. int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
  1282. gpa_t gpa)
  1283. {
  1284. struct kvm_memslots *slots = kvm_memslots(kvm);
  1285. int offset = offset_in_page(gpa);
  1286. gfn_t gfn = gpa >> PAGE_SHIFT;
  1287. ghc->gpa = gpa;
  1288. ghc->generation = slots->generation;
  1289. ghc->memslot = gfn_to_memslot(kvm, gfn);
  1290. ghc->hva = gfn_to_hva_many(ghc->memslot, gfn, NULL);
  1291. if (!kvm_is_error_hva(ghc->hva))
  1292. ghc->hva += offset;
  1293. else
  1294. return -EFAULT;
  1295. return 0;
  1296. }
  1297. EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init);
  1298. int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
  1299. void *data, unsigned long len)
  1300. {
  1301. struct kvm_memslots *slots = kvm_memslots(kvm);
  1302. int r;
  1303. if (slots->generation != ghc->generation)
  1304. kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa);
  1305. if (kvm_is_error_hva(ghc->hva))
  1306. return -EFAULT;
  1307. r = __copy_to_user((void __user *)ghc->hva, data, len);
  1308. if (r)
  1309. return -EFAULT;
  1310. mark_page_dirty_in_slot(kvm, ghc->memslot, ghc->gpa >> PAGE_SHIFT);
  1311. return 0;
  1312. }
  1313. EXPORT_SYMBOL_GPL(kvm_write_guest_cached);
  1314. int kvm_read_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
  1315. void *data, unsigned long len)
  1316. {
  1317. struct kvm_memslots *slots = kvm_memslots(kvm);
  1318. int r;
  1319. if (slots->generation != ghc->generation)
  1320. kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa);
  1321. if (kvm_is_error_hva(ghc->hva))
  1322. return -EFAULT;
  1323. r = __copy_from_user(data, (void __user *)ghc->hva, len);
  1324. if (r)
  1325. return -EFAULT;
  1326. return 0;
  1327. }
  1328. EXPORT_SYMBOL_GPL(kvm_read_guest_cached);
  1329. int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
  1330. {
  1331. return kvm_write_guest_page(kvm, gfn, (const void *) empty_zero_page,
  1332. offset, len);
  1333. }
  1334. EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
  1335. int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
  1336. {
  1337. gfn_t gfn = gpa >> PAGE_SHIFT;
  1338. int seg;
  1339. int offset = offset_in_page(gpa);
  1340. int ret;
  1341. while ((seg = next_segment(len, offset)) != 0) {
  1342. ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
  1343. if (ret < 0)
  1344. return ret;
  1345. offset = 0;
  1346. len -= seg;
  1347. ++gfn;
  1348. }
  1349. return 0;
  1350. }
  1351. EXPORT_SYMBOL_GPL(kvm_clear_guest);
  1352. void mark_page_dirty_in_slot(struct kvm *kvm, struct kvm_memory_slot *memslot,
  1353. gfn_t gfn)
  1354. {
  1355. if (memslot && memslot->dirty_bitmap) {
  1356. unsigned long rel_gfn = gfn - memslot->base_gfn;
  1357. set_bit_le(rel_gfn, memslot->dirty_bitmap);
  1358. }
  1359. }
  1360. void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
  1361. {
  1362. struct kvm_memory_slot *memslot;
  1363. memslot = gfn_to_memslot(kvm, gfn);
  1364. mark_page_dirty_in_slot(kvm, memslot, gfn);
  1365. }
  1366. /*
  1367. * The vCPU has executed a HLT instruction with in-kernel mode enabled.
  1368. */
  1369. void kvm_vcpu_block(struct kvm_vcpu *vcpu)
  1370. {
  1371. DEFINE_WAIT(wait);
  1372. for (;;) {
  1373. prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
  1374. if (kvm_arch_vcpu_runnable(vcpu)) {
  1375. kvm_make_request(KVM_REQ_UNHALT, vcpu);
  1376. break;
  1377. }
  1378. if (kvm_cpu_has_pending_timer(vcpu))
  1379. break;
  1380. if (signal_pending(current))
  1381. break;
  1382. schedule();
  1383. }
  1384. finish_wait(&vcpu->wq, &wait);
  1385. }
  1386. #ifndef CONFIG_S390
  1387. /*
  1388. * Kick a sleeping VCPU, or a guest VCPU in guest mode, into host kernel mode.
  1389. */
  1390. void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
  1391. {
  1392. int me;
  1393. int cpu = vcpu->cpu;
  1394. wait_queue_head_t *wqp;
  1395. wqp = kvm_arch_vcpu_wq(vcpu);
  1396. if (waitqueue_active(wqp)) {
  1397. wake_up_interruptible(wqp);
  1398. ++vcpu->stat.halt_wakeup;
  1399. }
  1400. me = get_cpu();
  1401. if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
  1402. if (kvm_arch_vcpu_should_kick(vcpu))
  1403. smp_send_reschedule(cpu);
  1404. put_cpu();
  1405. }
  1406. #endif /* !CONFIG_S390 */
  1407. void kvm_resched(struct kvm_vcpu *vcpu)
  1408. {
  1409. if (!need_resched())
  1410. return;
  1411. cond_resched();
  1412. }
  1413. EXPORT_SYMBOL_GPL(kvm_resched);
  1414. bool kvm_vcpu_yield_to(struct kvm_vcpu *target)
  1415. {
  1416. struct pid *pid;
  1417. struct task_struct *task = NULL;
  1418. bool ret = false;
  1419. rcu_read_lock();
  1420. pid = rcu_dereference(target->pid);
  1421. if (pid)
  1422. task = get_pid_task(target->pid, PIDTYPE_PID);
  1423. rcu_read_unlock();
  1424. if (!task)
  1425. return ret;
  1426. if (task->flags & PF_VCPU) {
  1427. put_task_struct(task);
  1428. return ret;
  1429. }
  1430. ret = yield_to(task, 1);
  1431. put_task_struct(task);
  1432. return ret;
  1433. }
  1434. EXPORT_SYMBOL_GPL(kvm_vcpu_yield_to);
  1435. #ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
  1436. /*
  1437. * Helper that checks whether a VCPU is eligible for directed yield.
  1438. * Most eligible candidate to yield is decided by following heuristics:
  1439. *
  1440. * (a) VCPU which has not done pl-exit or cpu relax intercepted recently
  1441. * (preempted lock holder), indicated by @in_spin_loop.
  1442. * Set at the beiginning and cleared at the end of interception/PLE handler.
  1443. *
  1444. * (b) VCPU which has done pl-exit/ cpu relax intercepted but did not get
  1445. * chance last time (mostly it has become eligible now since we have probably
  1446. * yielded to lockholder in last iteration. This is done by toggling
  1447. * @dy_eligible each time a VCPU checked for eligibility.)
  1448. *
  1449. * Yielding to a recently pl-exited/cpu relax intercepted VCPU before yielding
  1450. * to preempted lock-holder could result in wrong VCPU selection and CPU
  1451. * burning. Giving priority for a potential lock-holder increases lock
  1452. * progress.
  1453. *
  1454. * Since algorithm is based on heuristics, accessing another VCPU data without
  1455. * locking does not harm. It may result in trying to yield to same VCPU, fail
  1456. * and continue with next VCPU and so on.
  1457. */
  1458. bool kvm_vcpu_eligible_for_directed_yield(struct kvm_vcpu *vcpu)
  1459. {
  1460. bool eligible;
  1461. eligible = !vcpu->spin_loop.in_spin_loop ||
  1462. (vcpu->spin_loop.in_spin_loop &&
  1463. vcpu->spin_loop.dy_eligible);
  1464. if (vcpu->spin_loop.in_spin_loop)
  1465. kvm_vcpu_set_dy_eligible(vcpu, !vcpu->spin_loop.dy_eligible);
  1466. return eligible;
  1467. }
  1468. #endif
  1469. void kvm_vcpu_on_spin(struct kvm_vcpu *me)
  1470. {
  1471. struct kvm *kvm = me->kvm;
  1472. struct kvm_vcpu *vcpu;
  1473. int last_boosted_vcpu = me->kvm->last_boosted_vcpu;
  1474. int yielded = 0;
  1475. int try = 3;
  1476. int pass;
  1477. int i;
  1478. kvm_vcpu_set_in_spin_loop(me, true);
  1479. /*
  1480. * We boost the priority of a VCPU that is runnable but not
  1481. * currently running, because it got preempted by something
  1482. * else and called schedule in __vcpu_run. Hopefully that
  1483. * VCPU is holding the lock that we need and will release it.
  1484. * We approximate round-robin by starting at the last boosted VCPU.
  1485. */
  1486. for (pass = 0; pass < 2 && !yielded && try; pass++) {
  1487. kvm_for_each_vcpu(i, vcpu, kvm) {
  1488. if (!pass && i <= last_boosted_vcpu) {
  1489. i = last_boosted_vcpu;
  1490. continue;
  1491. } else if (pass && i > last_boosted_vcpu)
  1492. break;
  1493. if (vcpu == me)
  1494. continue;
  1495. if (waitqueue_active(&vcpu->wq))
  1496. continue;
  1497. if (!kvm_vcpu_eligible_for_directed_yield(vcpu))
  1498. continue;
  1499. yielded = kvm_vcpu_yield_to(vcpu);
  1500. if (yielded > 0) {
  1501. kvm->last_boosted_vcpu = i;
  1502. break;
  1503. } else if (yielded < 0) {
  1504. try--;
  1505. if (!try)
  1506. break;
  1507. }
  1508. }
  1509. }
  1510. kvm_vcpu_set_in_spin_loop(me, false);
  1511. /* Ensure vcpu is not eligible during next spinloop */
  1512. kvm_vcpu_set_dy_eligible(me, false);
  1513. }
  1514. EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
  1515. static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1516. {
  1517. struct kvm_vcpu *vcpu = vma->vm_file->private_data;
  1518. struct page *page;
  1519. if (vmf->pgoff == 0)
  1520. page = virt_to_page(vcpu->run);
  1521. #ifdef CONFIG_X86
  1522. else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
  1523. page = virt_to_page(vcpu->arch.pio_data);
  1524. #endif
  1525. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  1526. else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
  1527. page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
  1528. #endif
  1529. else
  1530. return kvm_arch_vcpu_fault(vcpu, vmf);
  1531. get_page(page);
  1532. vmf->page = page;
  1533. return 0;
  1534. }
  1535. static const struct vm_operations_struct kvm_vcpu_vm_ops = {
  1536. .fault = kvm_vcpu_fault,
  1537. };
  1538. static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
  1539. {
  1540. vma->vm_ops = &kvm_vcpu_vm_ops;
  1541. return 0;
  1542. }
  1543. static int kvm_vcpu_release(struct inode *inode, struct file *filp)
  1544. {
  1545. struct kvm_vcpu *vcpu = filp->private_data;
  1546. kvm_put_kvm(vcpu->kvm);
  1547. return 0;
  1548. }
  1549. static struct file_operations kvm_vcpu_fops = {
  1550. .release = kvm_vcpu_release,
  1551. .unlocked_ioctl = kvm_vcpu_ioctl,
  1552. #ifdef CONFIG_COMPAT
  1553. .compat_ioctl = kvm_vcpu_compat_ioctl,
  1554. #endif
  1555. .mmap = kvm_vcpu_mmap,
  1556. .llseek = noop_llseek,
  1557. };
  1558. /*
  1559. * Allocates an inode for the vcpu.
  1560. */
  1561. static int create_vcpu_fd(struct kvm_vcpu *vcpu)
  1562. {
  1563. return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR);
  1564. }
  1565. /*
  1566. * Creates some virtual cpus. Good luck creating more than one.
  1567. */
  1568. static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
  1569. {
  1570. int r;
  1571. struct kvm_vcpu *vcpu, *v;
  1572. vcpu = kvm_arch_vcpu_create(kvm, id);
  1573. if (IS_ERR(vcpu))
  1574. return PTR_ERR(vcpu);
  1575. preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
  1576. r = kvm_arch_vcpu_setup(vcpu);
  1577. if (r)
  1578. goto vcpu_destroy;
  1579. mutex_lock(&kvm->lock);
  1580. if (!kvm_vcpu_compatible(vcpu)) {
  1581. r = -EINVAL;
  1582. goto unlock_vcpu_destroy;
  1583. }
  1584. if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
  1585. r = -EINVAL;
  1586. goto unlock_vcpu_destroy;
  1587. }
  1588. kvm_for_each_vcpu(r, v, kvm)
  1589. if (v->vcpu_id == id) {
  1590. r = -EEXIST;
  1591. goto unlock_vcpu_destroy;
  1592. }
  1593. BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
  1594. /* Now it's all set up, let userspace reach it */
  1595. kvm_get_kvm(kvm);
  1596. r = create_vcpu_fd(vcpu);
  1597. if (r < 0) {
  1598. kvm_put_kvm(kvm);
  1599. goto unlock_vcpu_destroy;
  1600. }
  1601. kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
  1602. smp_wmb();
  1603. atomic_inc(&kvm->online_vcpus);
  1604. mutex_unlock(&kvm->lock);
  1605. kvm_arch_vcpu_postcreate(vcpu);
  1606. return r;
  1607. unlock_vcpu_destroy:
  1608. mutex_unlock(&kvm->lock);
  1609. vcpu_destroy:
  1610. kvm_arch_vcpu_destroy(vcpu);
  1611. return r;
  1612. }
  1613. static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
  1614. {
  1615. if (sigset) {
  1616. sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
  1617. vcpu->sigset_active = 1;
  1618. vcpu->sigset = *sigset;
  1619. } else
  1620. vcpu->sigset_active = 0;
  1621. return 0;
  1622. }
  1623. static long kvm_vcpu_ioctl(struct file *filp,
  1624. unsigned int ioctl, unsigned long arg)
  1625. {
  1626. struct kvm_vcpu *vcpu = filp->private_data;
  1627. void __user *argp = (void __user *)arg;
  1628. int r;
  1629. struct kvm_fpu *fpu = NULL;
  1630. struct kvm_sregs *kvm_sregs = NULL;
  1631. if (vcpu->kvm->mm != current->mm)
  1632. return -EIO;
  1633. #if defined(CONFIG_S390) || defined(CONFIG_PPC)
  1634. /*
  1635. * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
  1636. * so vcpu_load() would break it.
  1637. */
  1638. if (ioctl == KVM_S390_INTERRUPT || ioctl == KVM_INTERRUPT)
  1639. return kvm_arch_vcpu_ioctl(filp, ioctl, arg);
  1640. #endif
  1641. r = vcpu_load(vcpu);
  1642. if (r)
  1643. return r;
  1644. switch (ioctl) {
  1645. case KVM_RUN:
  1646. r = -EINVAL;
  1647. if (arg)
  1648. goto out;
  1649. r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
  1650. trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
  1651. break;
  1652. case KVM_GET_REGS: {
  1653. struct kvm_regs *kvm_regs;
  1654. r = -ENOMEM;
  1655. kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
  1656. if (!kvm_regs)
  1657. goto out;
  1658. r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
  1659. if (r)
  1660. goto out_free1;
  1661. r = -EFAULT;
  1662. if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
  1663. goto out_free1;
  1664. r = 0;
  1665. out_free1:
  1666. kfree(kvm_regs);
  1667. break;
  1668. }
  1669. case KVM_SET_REGS: {
  1670. struct kvm_regs *kvm_regs;
  1671. r = -ENOMEM;
  1672. kvm_regs = memdup_user(argp, sizeof(*kvm_regs));
  1673. if (IS_ERR(kvm_regs)) {
  1674. r = PTR_ERR(kvm_regs);
  1675. goto out;
  1676. }
  1677. r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
  1678. kfree(kvm_regs);
  1679. break;
  1680. }
  1681. case KVM_GET_SREGS: {
  1682. kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
  1683. r = -ENOMEM;
  1684. if (!kvm_sregs)
  1685. goto out;
  1686. r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
  1687. if (r)
  1688. goto out;
  1689. r = -EFAULT;
  1690. if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
  1691. goto out;
  1692. r = 0;
  1693. break;
  1694. }
  1695. case KVM_SET_SREGS: {
  1696. kvm_sregs = memdup_user(argp, sizeof(*kvm_sregs));
  1697. if (IS_ERR(kvm_sregs)) {
  1698. r = PTR_ERR(kvm_sregs);
  1699. kvm_sregs = NULL;
  1700. goto out;
  1701. }
  1702. r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
  1703. break;
  1704. }
  1705. case KVM_GET_MP_STATE: {
  1706. struct kvm_mp_state mp_state;
  1707. r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
  1708. if (r)
  1709. goto out;
  1710. r = -EFAULT;
  1711. if (copy_to_user(argp, &mp_state, sizeof mp_state))
  1712. goto out;
  1713. r = 0;
  1714. break;
  1715. }
  1716. case KVM_SET_MP_STATE: {
  1717. struct kvm_mp_state mp_state;
  1718. r = -EFAULT;
  1719. if (copy_from_user(&mp_state, argp, sizeof mp_state))
  1720. goto out;
  1721. r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
  1722. break;
  1723. }
  1724. case KVM_TRANSLATE: {
  1725. struct kvm_translation tr;
  1726. r = -EFAULT;
  1727. if (copy_from_user(&tr, argp, sizeof tr))
  1728. goto out;
  1729. r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
  1730. if (r)
  1731. goto out;
  1732. r = -EFAULT;
  1733. if (copy_to_user(argp, &tr, sizeof tr))
  1734. goto out;
  1735. r = 0;
  1736. break;
  1737. }
  1738. case KVM_SET_GUEST_DEBUG: {
  1739. struct kvm_guest_debug dbg;
  1740. r = -EFAULT;
  1741. if (copy_from_user(&dbg, argp, sizeof dbg))
  1742. goto out;
  1743. r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
  1744. break;
  1745. }
  1746. case KVM_SET_SIGNAL_MASK: {
  1747. struct kvm_signal_mask __user *sigmask_arg = argp;
  1748. struct kvm_signal_mask kvm_sigmask;
  1749. sigset_t sigset, *p;
  1750. p = NULL;
  1751. if (argp) {
  1752. r = -EFAULT;
  1753. if (copy_from_user(&kvm_sigmask, argp,
  1754. sizeof kvm_sigmask))
  1755. goto out;
  1756. r = -EINVAL;
  1757. if (kvm_sigmask.len != sizeof sigset)
  1758. goto out;
  1759. r = -EFAULT;
  1760. if (copy_from_user(&sigset, sigmask_arg->sigset,
  1761. sizeof sigset))
  1762. goto out;
  1763. p = &sigset;
  1764. }
  1765. r = kvm_vcpu_ioctl_set_sigmask(vcpu, p);
  1766. break;
  1767. }
  1768. case KVM_GET_FPU: {
  1769. fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
  1770. r = -ENOMEM;
  1771. if (!fpu)
  1772. goto out;
  1773. r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
  1774. if (r)
  1775. goto out;
  1776. r = -EFAULT;
  1777. if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
  1778. goto out;
  1779. r = 0;
  1780. break;
  1781. }
  1782. case KVM_SET_FPU: {
  1783. fpu = memdup_user(argp, sizeof(*fpu));
  1784. if (IS_ERR(fpu)) {
  1785. r = PTR_ERR(fpu);
  1786. fpu = NULL;
  1787. goto out;
  1788. }
  1789. r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
  1790. break;
  1791. }
  1792. default:
  1793. r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
  1794. }
  1795. out:
  1796. vcpu_put(vcpu);
  1797. kfree(fpu);
  1798. kfree(kvm_sregs);
  1799. return r;
  1800. }
  1801. #ifdef CONFIG_COMPAT
  1802. static long kvm_vcpu_compat_ioctl(struct file *filp,
  1803. unsigned int ioctl, unsigned long arg)
  1804. {
  1805. struct kvm_vcpu *vcpu = filp->private_data;
  1806. void __user *argp = compat_ptr(arg);
  1807. int r;
  1808. if (vcpu->kvm->mm != current->mm)
  1809. return -EIO;
  1810. switch (ioctl) {
  1811. case KVM_SET_SIGNAL_MASK: {
  1812. struct kvm_signal_mask __user *sigmask_arg = argp;
  1813. struct kvm_signal_mask kvm_sigmask;
  1814. compat_sigset_t csigset;
  1815. sigset_t sigset;
  1816. if (argp) {
  1817. r = -EFAULT;
  1818. if (copy_from_user(&kvm_sigmask, argp,
  1819. sizeof kvm_sigmask))
  1820. goto out;
  1821. r = -EINVAL;
  1822. if (kvm_sigmask.len != sizeof csigset)
  1823. goto out;
  1824. r = -EFAULT;
  1825. if (copy_from_user(&csigset, sigmask_arg->sigset,
  1826. sizeof csigset))
  1827. goto out;
  1828. sigset_from_compat(&sigset, &csigset);
  1829. r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
  1830. } else
  1831. r = kvm_vcpu_ioctl_set_sigmask(vcpu, NULL);
  1832. break;
  1833. }
  1834. default:
  1835. r = kvm_vcpu_ioctl(filp, ioctl, arg);
  1836. }
  1837. out:
  1838. return r;
  1839. }
  1840. #endif
  1841. static long kvm_vm_ioctl(struct file *filp,
  1842. unsigned int ioctl, unsigned long arg)
  1843. {
  1844. struct kvm *kvm = filp->private_data;
  1845. void __user *argp = (void __user *)arg;
  1846. int r;
  1847. if (kvm->mm != current->mm)
  1848. return -EIO;
  1849. switch (ioctl) {
  1850. case KVM_CREATE_VCPU:
  1851. r = kvm_vm_ioctl_create_vcpu(kvm, arg);
  1852. break;
  1853. case KVM_SET_USER_MEMORY_REGION: {
  1854. struct kvm_userspace_memory_region kvm_userspace_mem;
  1855. r = -EFAULT;
  1856. if (copy_from_user(&kvm_userspace_mem, argp,
  1857. sizeof kvm_userspace_mem))
  1858. goto out;
  1859. r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem);
  1860. break;
  1861. }
  1862. case KVM_GET_DIRTY_LOG: {
  1863. struct kvm_dirty_log log;
  1864. r = -EFAULT;
  1865. if (copy_from_user(&log, argp, sizeof log))
  1866. goto out;
  1867. r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
  1868. break;
  1869. }
  1870. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  1871. case KVM_REGISTER_COALESCED_MMIO: {
  1872. struct kvm_coalesced_mmio_zone zone;
  1873. r = -EFAULT;
  1874. if (copy_from_user(&zone, argp, sizeof zone))
  1875. goto out;
  1876. r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
  1877. break;
  1878. }
  1879. case KVM_UNREGISTER_COALESCED_MMIO: {
  1880. struct kvm_coalesced_mmio_zone zone;
  1881. r = -EFAULT;
  1882. if (copy_from_user(&zone, argp, sizeof zone))
  1883. goto out;
  1884. r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
  1885. break;
  1886. }
  1887. #endif
  1888. case KVM_IRQFD: {
  1889. struct kvm_irqfd data;
  1890. r = -EFAULT;
  1891. if (copy_from_user(&data, argp, sizeof data))
  1892. goto out;
  1893. r = kvm_irqfd(kvm, &data);
  1894. break;
  1895. }
  1896. case KVM_IOEVENTFD: {
  1897. struct kvm_ioeventfd data;
  1898. r = -EFAULT;
  1899. if (copy_from_user(&data, argp, sizeof data))
  1900. goto out;
  1901. r = kvm_ioeventfd(kvm, &data);
  1902. break;
  1903. }
  1904. #ifdef CONFIG_KVM_APIC_ARCHITECTURE
  1905. case KVM_SET_BOOT_CPU_ID:
  1906. r = 0;
  1907. mutex_lock(&kvm->lock);
  1908. if (atomic_read(&kvm->online_vcpus) != 0)
  1909. r = -EBUSY;
  1910. else
  1911. kvm->bsp_vcpu_id = arg;
  1912. mutex_unlock(&kvm->lock);
  1913. break;
  1914. #endif
  1915. #ifdef CONFIG_HAVE_KVM_MSI
  1916. case KVM_SIGNAL_MSI: {
  1917. struct kvm_msi msi;
  1918. r = -EFAULT;
  1919. if (copy_from_user(&msi, argp, sizeof msi))
  1920. goto out;
  1921. r = kvm_send_userspace_msi(kvm, &msi);
  1922. break;
  1923. }
  1924. #endif
  1925. #ifdef __KVM_HAVE_IRQ_LINE
  1926. case KVM_IRQ_LINE_STATUS:
  1927. case KVM_IRQ_LINE: {
  1928. struct kvm_irq_level irq_event;
  1929. r = -EFAULT;
  1930. if (copy_from_user(&irq_event, argp, sizeof irq_event))
  1931. goto out;
  1932. r = kvm_vm_ioctl_irq_line(kvm, &irq_event);
  1933. if (r)
  1934. goto out;
  1935. r = -EFAULT;
  1936. if (ioctl == KVM_IRQ_LINE_STATUS) {
  1937. if (copy_to_user(argp, &irq_event, sizeof irq_event))
  1938. goto out;
  1939. }
  1940. r = 0;
  1941. break;
  1942. }
  1943. #endif
  1944. default:
  1945. r = kvm_arch_vm_ioctl(filp, ioctl, arg);
  1946. if (r == -ENOTTY)
  1947. r = kvm_vm_ioctl_assigned_device(kvm, ioctl, arg);
  1948. }
  1949. out:
  1950. return r;
  1951. }
  1952. #ifdef CONFIG_COMPAT
  1953. struct compat_kvm_dirty_log {
  1954. __u32 slot;
  1955. __u32 padding1;
  1956. union {
  1957. compat_uptr_t dirty_bitmap; /* one bit per page */
  1958. __u64 padding2;
  1959. };
  1960. };
  1961. static long kvm_vm_compat_ioctl(struct file *filp,
  1962. unsigned int ioctl, unsigned long arg)
  1963. {
  1964. struct kvm *kvm = filp->private_data;
  1965. int r;
  1966. if (kvm->mm != current->mm)
  1967. return -EIO;
  1968. switch (ioctl) {
  1969. case KVM_GET_DIRTY_LOG: {
  1970. struct compat_kvm_dirty_log compat_log;
  1971. struct kvm_dirty_log log;
  1972. r = -EFAULT;
  1973. if (copy_from_user(&compat_log, (void __user *)arg,
  1974. sizeof(compat_log)))
  1975. goto out;
  1976. log.slot = compat_log.slot;
  1977. log.padding1 = compat_log.padding1;
  1978. log.padding2 = compat_log.padding2;
  1979. log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
  1980. r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
  1981. break;
  1982. }
  1983. default:
  1984. r = kvm_vm_ioctl(filp, ioctl, arg);
  1985. }
  1986. out:
  1987. return r;
  1988. }
  1989. #endif
  1990. static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1991. {
  1992. struct page *page[1];
  1993. unsigned long addr;
  1994. int npages;
  1995. gfn_t gfn = vmf->pgoff;
  1996. struct kvm *kvm = vma->vm_file->private_data;
  1997. addr = gfn_to_hva(kvm, gfn);
  1998. if (kvm_is_error_hva(addr))
  1999. return VM_FAULT_SIGBUS;
  2000. npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
  2001. NULL);
  2002. if (unlikely(npages != 1))
  2003. return VM_FAULT_SIGBUS;
  2004. vmf->page = page[0];
  2005. return 0;
  2006. }
  2007. static const struct vm_operations_struct kvm_vm_vm_ops = {
  2008. .fault = kvm_vm_fault,
  2009. };
  2010. static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
  2011. {
  2012. vma->vm_ops = &kvm_vm_vm_ops;
  2013. return 0;
  2014. }
  2015. static struct file_operations kvm_vm_fops = {
  2016. .release = kvm_vm_release,
  2017. .unlocked_ioctl = kvm_vm_ioctl,
  2018. #ifdef CONFIG_COMPAT
  2019. .compat_ioctl = kvm_vm_compat_ioctl,
  2020. #endif
  2021. .mmap = kvm_vm_mmap,
  2022. .llseek = noop_llseek,
  2023. };
  2024. static int kvm_dev_ioctl_create_vm(unsigned long type)
  2025. {
  2026. int r;
  2027. struct kvm *kvm;
  2028. kvm = kvm_create_vm(type);
  2029. if (IS_ERR(kvm))
  2030. return PTR_ERR(kvm);
  2031. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  2032. r = kvm_coalesced_mmio_init(kvm);
  2033. if (r < 0) {
  2034. kvm_put_kvm(kvm);
  2035. return r;
  2036. }
  2037. #endif
  2038. r = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
  2039. if (r < 0)
  2040. kvm_put_kvm(kvm);
  2041. return r;
  2042. }
  2043. static long kvm_dev_ioctl_check_extension_generic(long arg)
  2044. {
  2045. switch (arg) {
  2046. case KVM_CAP_USER_MEMORY:
  2047. case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
  2048. case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
  2049. #ifdef CONFIG_KVM_APIC_ARCHITECTURE
  2050. case KVM_CAP_SET_BOOT_CPU_ID:
  2051. #endif
  2052. case KVM_CAP_INTERNAL_ERROR_DATA:
  2053. #ifdef CONFIG_HAVE_KVM_MSI
  2054. case KVM_CAP_SIGNAL_MSI:
  2055. #endif
  2056. return 1;
  2057. #ifdef KVM_CAP_IRQ_ROUTING
  2058. case KVM_CAP_IRQ_ROUTING:
  2059. return KVM_MAX_IRQ_ROUTES;
  2060. #endif
  2061. default:
  2062. break;
  2063. }
  2064. return kvm_dev_ioctl_check_extension(arg);
  2065. }
  2066. static long kvm_dev_ioctl(struct file *filp,
  2067. unsigned int ioctl, unsigned long arg)
  2068. {
  2069. long r = -EINVAL;
  2070. switch (ioctl) {
  2071. case KVM_GET_API_VERSION:
  2072. r = -EINVAL;
  2073. if (arg)
  2074. goto out;
  2075. r = KVM_API_VERSION;
  2076. break;
  2077. case KVM_CREATE_VM:
  2078. r = kvm_dev_ioctl_create_vm(arg);
  2079. break;
  2080. case KVM_CHECK_EXTENSION:
  2081. r = kvm_dev_ioctl_check_extension_generic(arg);
  2082. break;
  2083. case KVM_GET_VCPU_MMAP_SIZE:
  2084. r = -EINVAL;
  2085. if (arg)
  2086. goto out;
  2087. r = PAGE_SIZE; /* struct kvm_run */
  2088. #ifdef CONFIG_X86
  2089. r += PAGE_SIZE; /* pio data page */
  2090. #endif
  2091. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  2092. r += PAGE_SIZE; /* coalesced mmio ring page */
  2093. #endif
  2094. break;
  2095. case KVM_TRACE_ENABLE:
  2096. case KVM_TRACE_PAUSE:
  2097. case KVM_TRACE_DISABLE:
  2098. r = -EOPNOTSUPP;
  2099. break;
  2100. default:
  2101. return kvm_arch_dev_ioctl(filp, ioctl, arg);
  2102. }
  2103. out:
  2104. return r;
  2105. }
  2106. static struct file_operations kvm_chardev_ops = {
  2107. .unlocked_ioctl = kvm_dev_ioctl,
  2108. .compat_ioctl = kvm_dev_ioctl,
  2109. .llseek = noop_llseek,
  2110. };
  2111. static struct miscdevice kvm_dev = {
  2112. KVM_MINOR,
  2113. "kvm",
  2114. &kvm_chardev_ops,
  2115. };
  2116. static void hardware_enable_nolock(void *junk)
  2117. {
  2118. int cpu = raw_smp_processor_id();
  2119. int r;
  2120. if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
  2121. return;
  2122. cpumask_set_cpu(cpu, cpus_hardware_enabled);
  2123. r = kvm_arch_hardware_enable(NULL);
  2124. if (r) {
  2125. cpumask_clear_cpu(cpu, cpus_hardware_enabled);
  2126. atomic_inc(&hardware_enable_failed);
  2127. printk(KERN_INFO "kvm: enabling virtualization on "
  2128. "CPU%d failed\n", cpu);
  2129. }
  2130. }
  2131. static void hardware_enable(void *junk)
  2132. {
  2133. raw_spin_lock(&kvm_lock);
  2134. hardware_enable_nolock(junk);
  2135. raw_spin_unlock(&kvm_lock);
  2136. }
  2137. static void hardware_disable_nolock(void *junk)
  2138. {
  2139. int cpu = raw_smp_processor_id();
  2140. if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
  2141. return;
  2142. cpumask_clear_cpu(cpu, cpus_hardware_enabled);
  2143. kvm_arch_hardware_disable(NULL);
  2144. }
  2145. static void hardware_disable(void *junk)
  2146. {
  2147. raw_spin_lock(&kvm_lock);
  2148. hardware_disable_nolock(junk);
  2149. raw_spin_unlock(&kvm_lock);
  2150. }
  2151. static void hardware_disable_all_nolock(void)
  2152. {
  2153. BUG_ON(!kvm_usage_count);
  2154. kvm_usage_count--;
  2155. if (!kvm_usage_count)
  2156. on_each_cpu(hardware_disable_nolock, NULL, 1);
  2157. }
  2158. static void hardware_disable_all(void)
  2159. {
  2160. raw_spin_lock(&kvm_lock);
  2161. hardware_disable_all_nolock();
  2162. raw_spin_unlock(&kvm_lock);
  2163. }
  2164. static int hardware_enable_all(void)
  2165. {
  2166. int r = 0;
  2167. raw_spin_lock(&kvm_lock);
  2168. kvm_usage_count++;
  2169. if (kvm_usage_count == 1) {
  2170. atomic_set(&hardware_enable_failed, 0);
  2171. on_each_cpu(hardware_enable_nolock, NULL, 1);
  2172. if (atomic_read(&hardware_enable_failed)) {
  2173. hardware_disable_all_nolock();
  2174. r = -EBUSY;
  2175. }
  2176. }
  2177. raw_spin_unlock(&kvm_lock);
  2178. return r;
  2179. }
  2180. static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
  2181. void *v)
  2182. {
  2183. int cpu = (long)v;
  2184. if (!kvm_usage_count)
  2185. return NOTIFY_OK;
  2186. val &= ~CPU_TASKS_FROZEN;
  2187. switch (val) {
  2188. case CPU_DYING:
  2189. printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
  2190. cpu);
  2191. hardware_disable(NULL);
  2192. break;
  2193. case CPU_STARTING:
  2194. printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
  2195. cpu);
  2196. hardware_enable(NULL);
  2197. break;
  2198. }
  2199. return NOTIFY_OK;
  2200. }
  2201. asmlinkage void kvm_spurious_fault(void)
  2202. {
  2203. /* Fault while not rebooting. We want the trace. */
  2204. BUG();
  2205. }
  2206. EXPORT_SYMBOL_GPL(kvm_spurious_fault);
  2207. static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
  2208. void *v)
  2209. {
  2210. /*
  2211. * Some (well, at least mine) BIOSes hang on reboot if
  2212. * in vmx root mode.
  2213. *
  2214. * And Intel TXT required VMX off for all cpu when system shutdown.
  2215. */
  2216. printk(KERN_INFO "kvm: exiting hardware virtualization\n");
  2217. kvm_rebooting = true;
  2218. on_each_cpu(hardware_disable_nolock, NULL, 1);
  2219. return NOTIFY_OK;
  2220. }
  2221. static struct notifier_block kvm_reboot_notifier = {
  2222. .notifier_call = kvm_reboot,
  2223. .priority = 0,
  2224. };
  2225. static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
  2226. {
  2227. int i;
  2228. for (i = 0; i < bus->dev_count; i++) {
  2229. struct kvm_io_device *pos = bus->range[i].dev;
  2230. kvm_iodevice_destructor(pos);
  2231. }
  2232. kfree(bus);
  2233. }
  2234. int kvm_io_bus_sort_cmp(const void *p1, const void *p2)
  2235. {
  2236. const struct kvm_io_range *r1 = p1;
  2237. const struct kvm_io_range *r2 = p2;
  2238. if (r1->addr < r2->addr)
  2239. return -1;
  2240. if (r1->addr + r1->len > r2->addr + r2->len)
  2241. return 1;
  2242. return 0;
  2243. }
  2244. int kvm_io_bus_insert_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev,
  2245. gpa_t addr, int len)
  2246. {
  2247. bus->range[bus->dev_count++] = (struct kvm_io_range) {
  2248. .addr = addr,
  2249. .len = len,
  2250. .dev = dev,
  2251. };
  2252. sort(bus->range, bus->dev_count, sizeof(struct kvm_io_range),
  2253. kvm_io_bus_sort_cmp, NULL);
  2254. return 0;
  2255. }
  2256. int kvm_io_bus_get_first_dev(struct kvm_io_bus *bus,
  2257. gpa_t addr, int len)
  2258. {
  2259. struct kvm_io_range *range, key;
  2260. int off;
  2261. key = (struct kvm_io_range) {
  2262. .addr = addr,
  2263. .len = len,
  2264. };
  2265. range = bsearch(&key, bus->range, bus->dev_count,
  2266. sizeof(struct kvm_io_range), kvm_io_bus_sort_cmp);
  2267. if (range == NULL)
  2268. return -ENOENT;
  2269. off = range - bus->range;
  2270. while (off > 0 && kvm_io_bus_sort_cmp(&key, &bus->range[off-1]) == 0)
  2271. off--;
  2272. return off;
  2273. }
  2274. /* kvm_io_bus_write - called under kvm->slots_lock */
  2275. int kvm_io_bus_write(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
  2276. int len, const void *val)
  2277. {
  2278. int idx;
  2279. struct kvm_io_bus *bus;
  2280. struct kvm_io_range range;
  2281. range = (struct kvm_io_range) {
  2282. .addr = addr,
  2283. .len = len,
  2284. };
  2285. bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
  2286. idx = kvm_io_bus_get_first_dev(bus, addr, len);
  2287. if (idx < 0)
  2288. return -EOPNOTSUPP;
  2289. while (idx < bus->dev_count &&
  2290. kvm_io_bus_sort_cmp(&range, &bus->range[idx]) == 0) {
  2291. if (!kvm_iodevice_write(bus->range[idx].dev, addr, len, val))
  2292. return 0;
  2293. idx++;
  2294. }
  2295. return -EOPNOTSUPP;
  2296. }
  2297. /* kvm_io_bus_read - called under kvm->slots_lock */
  2298. int kvm_io_bus_read(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
  2299. int len, void *val)
  2300. {
  2301. int idx;
  2302. struct kvm_io_bus *bus;
  2303. struct kvm_io_range range;
  2304. range = (struct kvm_io_range) {
  2305. .addr = addr,
  2306. .len = len,
  2307. };
  2308. bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
  2309. idx = kvm_io_bus_get_first_dev(bus, addr, len);
  2310. if (idx < 0)
  2311. return -EOPNOTSUPP;
  2312. while (idx < bus->dev_count &&
  2313. kvm_io_bus_sort_cmp(&range, &bus->range[idx]) == 0) {
  2314. if (!kvm_iodevice_read(bus->range[idx].dev, addr, len, val))
  2315. return 0;
  2316. idx++;
  2317. }
  2318. return -EOPNOTSUPP;
  2319. }
  2320. /* Caller must hold slots_lock. */
  2321. int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
  2322. int len, struct kvm_io_device *dev)
  2323. {
  2324. struct kvm_io_bus *new_bus, *bus;
  2325. bus = kvm->buses[bus_idx];
  2326. if (bus->dev_count > NR_IOBUS_DEVS - 1)
  2327. return -ENOSPC;
  2328. new_bus = kzalloc(sizeof(*bus) + ((bus->dev_count + 1) *
  2329. sizeof(struct kvm_io_range)), GFP_KERNEL);
  2330. if (!new_bus)
  2331. return -ENOMEM;
  2332. memcpy(new_bus, bus, sizeof(*bus) + (bus->dev_count *
  2333. sizeof(struct kvm_io_range)));
  2334. kvm_io_bus_insert_dev(new_bus, dev, addr, len);
  2335. rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
  2336. synchronize_srcu_expedited(&kvm->srcu);
  2337. kfree(bus);
  2338. return 0;
  2339. }
  2340. /* Caller must hold slots_lock. */
  2341. int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
  2342. struct kvm_io_device *dev)
  2343. {
  2344. int i, r;
  2345. struct kvm_io_bus *new_bus, *bus;
  2346. bus = kvm->buses[bus_idx];
  2347. r = -ENOENT;
  2348. for (i = 0; i < bus->dev_count; i++)
  2349. if (bus->range[i].dev == dev) {
  2350. r = 0;
  2351. break;
  2352. }
  2353. if (r)
  2354. return r;
  2355. new_bus = kzalloc(sizeof(*bus) + ((bus->dev_count - 1) *
  2356. sizeof(struct kvm_io_range)), GFP_KERNEL);
  2357. if (!new_bus)
  2358. return -ENOMEM;
  2359. memcpy(new_bus, bus, sizeof(*bus) + i * sizeof(struct kvm_io_range));
  2360. new_bus->dev_count--;
  2361. memcpy(new_bus->range + i, bus->range + i + 1,
  2362. (new_bus->dev_count - i) * sizeof(struct kvm_io_range));
  2363. rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
  2364. synchronize_srcu_expedited(&kvm->srcu);
  2365. kfree(bus);
  2366. return r;
  2367. }
  2368. static struct notifier_block kvm_cpu_notifier = {
  2369. .notifier_call = kvm_cpu_hotplug,
  2370. };
  2371. static int vm_stat_get(void *_offset, u64 *val)
  2372. {
  2373. unsigned offset = (long)_offset;
  2374. struct kvm *kvm;
  2375. *val = 0;
  2376. raw_spin_lock(&kvm_lock);
  2377. list_for_each_entry(kvm, &vm_list, vm_list)
  2378. *val += *(u32 *)((void *)kvm + offset);
  2379. raw_spin_unlock(&kvm_lock);
  2380. return 0;
  2381. }
  2382. DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
  2383. static int vcpu_stat_get(void *_offset, u64 *val)
  2384. {
  2385. unsigned offset = (long)_offset;
  2386. struct kvm *kvm;
  2387. struct kvm_vcpu *vcpu;
  2388. int i;
  2389. *val = 0;
  2390. raw_spin_lock(&kvm_lock);
  2391. list_for_each_entry(kvm, &vm_list, vm_list)
  2392. kvm_for_each_vcpu(i, vcpu, kvm)
  2393. *val += *(u32 *)((void *)vcpu + offset);
  2394. raw_spin_unlock(&kvm_lock);
  2395. return 0;
  2396. }
  2397. DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
  2398. static const struct file_operations *stat_fops[] = {
  2399. [KVM_STAT_VCPU] = &vcpu_stat_fops,
  2400. [KVM_STAT_VM] = &vm_stat_fops,
  2401. };
  2402. static int kvm_init_debug(void)
  2403. {
  2404. int r = -EFAULT;
  2405. struct kvm_stats_debugfs_item *p;
  2406. kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
  2407. if (kvm_debugfs_dir == NULL)
  2408. goto out;
  2409. for (p = debugfs_entries; p->name; ++p) {
  2410. p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
  2411. (void *)(long)p->offset,
  2412. stat_fops[p->kind]);
  2413. if (p->dentry == NULL)
  2414. goto out_dir;
  2415. }
  2416. return 0;
  2417. out_dir:
  2418. debugfs_remove_recursive(kvm_debugfs_dir);
  2419. out:
  2420. return r;
  2421. }
  2422. static void kvm_exit_debug(void)
  2423. {
  2424. struct kvm_stats_debugfs_item *p;
  2425. for (p = debugfs_entries; p->name; ++p)
  2426. debugfs_remove(p->dentry);
  2427. debugfs_remove(kvm_debugfs_dir);
  2428. }
  2429. static int kvm_suspend(void)
  2430. {
  2431. if (kvm_usage_count)
  2432. hardware_disable_nolock(NULL);
  2433. return 0;
  2434. }
  2435. static void kvm_resume(void)
  2436. {
  2437. if (kvm_usage_count) {
  2438. WARN_ON(raw_spin_is_locked(&kvm_lock));
  2439. hardware_enable_nolock(NULL);
  2440. }
  2441. }
  2442. static struct syscore_ops kvm_syscore_ops = {
  2443. .suspend = kvm_suspend,
  2444. .resume = kvm_resume,
  2445. };
  2446. static inline
  2447. struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
  2448. {
  2449. return container_of(pn, struct kvm_vcpu, preempt_notifier);
  2450. }
  2451. static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
  2452. {
  2453. struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
  2454. kvm_arch_vcpu_load(vcpu, cpu);
  2455. }
  2456. static void kvm_sched_out(struct preempt_notifier *pn,
  2457. struct task_struct *next)
  2458. {
  2459. struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
  2460. kvm_arch_vcpu_put(vcpu);
  2461. }
  2462. int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
  2463. struct module *module)
  2464. {
  2465. int r;
  2466. int cpu;
  2467. r = kvm_arch_init(opaque);
  2468. if (r)
  2469. goto out_fail;
  2470. if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
  2471. r = -ENOMEM;
  2472. goto out_free_0;
  2473. }
  2474. r = kvm_arch_hardware_setup();
  2475. if (r < 0)
  2476. goto out_free_0a;
  2477. for_each_online_cpu(cpu) {
  2478. smp_call_function_single(cpu,
  2479. kvm_arch_check_processor_compat,
  2480. &r, 1);
  2481. if (r < 0)
  2482. goto out_free_1;
  2483. }
  2484. r = register_cpu_notifier(&kvm_cpu_notifier);
  2485. if (r)
  2486. goto out_free_2;
  2487. register_reboot_notifier(&kvm_reboot_notifier);
  2488. /* A kmem cache lets us meet the alignment requirements of fx_save. */
  2489. if (!vcpu_align)
  2490. vcpu_align = __alignof__(struct kvm_vcpu);
  2491. kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
  2492. 0, NULL);
  2493. if (!kvm_vcpu_cache) {
  2494. r = -ENOMEM;
  2495. goto out_free_3;
  2496. }
  2497. r = kvm_async_pf_init();
  2498. if (r)
  2499. goto out_free;
  2500. kvm_chardev_ops.owner = module;
  2501. kvm_vm_fops.owner = module;
  2502. kvm_vcpu_fops.owner = module;
  2503. r = misc_register(&kvm_dev);
  2504. if (r) {
  2505. printk(KERN_ERR "kvm: misc device register failed\n");
  2506. goto out_unreg;
  2507. }
  2508. register_syscore_ops(&kvm_syscore_ops);
  2509. kvm_preempt_ops.sched_in = kvm_sched_in;
  2510. kvm_preempt_ops.sched_out = kvm_sched_out;
  2511. r = kvm_init_debug();
  2512. if (r) {
  2513. printk(KERN_ERR "kvm: create debugfs files failed\n");
  2514. goto out_undebugfs;
  2515. }
  2516. return 0;
  2517. out_undebugfs:
  2518. unregister_syscore_ops(&kvm_syscore_ops);
  2519. out_unreg:
  2520. kvm_async_pf_deinit();
  2521. out_free:
  2522. kmem_cache_destroy(kvm_vcpu_cache);
  2523. out_free_3:
  2524. unregister_reboot_notifier(&kvm_reboot_notifier);
  2525. unregister_cpu_notifier(&kvm_cpu_notifier);
  2526. out_free_2:
  2527. out_free_1:
  2528. kvm_arch_hardware_unsetup();
  2529. out_free_0a:
  2530. free_cpumask_var(cpus_hardware_enabled);
  2531. out_free_0:
  2532. kvm_arch_exit();
  2533. out_fail:
  2534. return r;
  2535. }
  2536. EXPORT_SYMBOL_GPL(kvm_init);
  2537. void kvm_exit(void)
  2538. {
  2539. kvm_exit_debug();
  2540. misc_deregister(&kvm_dev);
  2541. kmem_cache_destroy(kvm_vcpu_cache);
  2542. kvm_async_pf_deinit();
  2543. unregister_syscore_ops(&kvm_syscore_ops);
  2544. unregister_reboot_notifier(&kvm_reboot_notifier);
  2545. unregister_cpu_notifier(&kvm_cpu_notifier);
  2546. on_each_cpu(hardware_disable_nolock, NULL, 1);
  2547. kvm_arch_hardware_unsetup();
  2548. kvm_arch_exit();
  2549. free_cpumask_var(cpus_hardware_enabled);
  2550. }
  2551. EXPORT_SYMBOL_GPL(kvm_exit);