kvm_main.c 66 KB

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