kvm_main.c 67 KB

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