kvm_main.c 58 KB

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