kvm_main.c 62 KB

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