kvm_main.c 66 KB

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