kvm_main.c 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842
  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 <kvm/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/ioctl.h>
  53. #include <asm/uaccess.h>
  54. #include <asm/pgtable.h>
  55. #include "coalesced_mmio.h"
  56. #include "async_pf.h"
  57. #include "vfio.h"
  58. #define CREATE_TRACE_POINTS
  59. #include <trace/events/kvm.h>
  60. /* Worst case buffer size needed for holding an integer. */
  61. #define ITOA_MAX_LEN 12
  62. MODULE_AUTHOR("Qumranet");
  63. MODULE_LICENSE("GPL");
  64. /* Architectures should define their poll value according to the halt latency */
  65. static unsigned int halt_poll_ns = KVM_HALT_POLL_NS_DEFAULT;
  66. module_param(halt_poll_ns, uint, S_IRUGO | S_IWUSR);
  67. /* Default doubles per-vcpu halt_poll_ns. */
  68. static unsigned int halt_poll_ns_grow = 2;
  69. module_param(halt_poll_ns_grow, uint, S_IRUGO | S_IWUSR);
  70. /* Default resets per-vcpu halt_poll_ns . */
  71. static unsigned int halt_poll_ns_shrink;
  72. module_param(halt_poll_ns_shrink, uint, S_IRUGO | S_IWUSR);
  73. /*
  74. * Ordering of locks:
  75. *
  76. * kvm->lock --> kvm->slots_lock --> kvm->irq_lock
  77. */
  78. DEFINE_SPINLOCK(kvm_lock);
  79. static DEFINE_RAW_SPINLOCK(kvm_count_lock);
  80. LIST_HEAD(vm_list);
  81. static cpumask_var_t cpus_hardware_enabled;
  82. static int kvm_usage_count;
  83. static atomic_t hardware_enable_failed;
  84. struct kmem_cache *kvm_vcpu_cache;
  85. EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
  86. static __read_mostly struct preempt_ops kvm_preempt_ops;
  87. struct dentry *kvm_debugfs_dir;
  88. EXPORT_SYMBOL_GPL(kvm_debugfs_dir);
  89. static int kvm_debugfs_num_entries;
  90. static const struct file_operations *stat_fops_per_vm[];
  91. static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
  92. unsigned long arg);
  93. #ifdef CONFIG_KVM_COMPAT
  94. static long kvm_vcpu_compat_ioctl(struct file *file, unsigned int ioctl,
  95. unsigned long arg);
  96. #endif
  97. static int hardware_enable_all(void);
  98. static void hardware_disable_all(void);
  99. static void kvm_io_bus_destroy(struct kvm_io_bus *bus);
  100. static void kvm_release_pfn_dirty(kvm_pfn_t pfn);
  101. static void mark_page_dirty_in_slot(struct kvm_memory_slot *memslot, gfn_t gfn);
  102. __visible bool kvm_rebooting;
  103. EXPORT_SYMBOL_GPL(kvm_rebooting);
  104. static bool largepages_enabled = true;
  105. bool kvm_is_reserved_pfn(kvm_pfn_t pfn)
  106. {
  107. if (pfn_valid(pfn))
  108. return PageReserved(pfn_to_page(pfn));
  109. return true;
  110. }
  111. /*
  112. * Switches to specified vcpu, until a matching vcpu_put()
  113. */
  114. int vcpu_load(struct kvm_vcpu *vcpu)
  115. {
  116. int cpu;
  117. if (mutex_lock_killable(&vcpu->mutex))
  118. return -EINTR;
  119. cpu = get_cpu();
  120. preempt_notifier_register(&vcpu->preempt_notifier);
  121. kvm_arch_vcpu_load(vcpu, cpu);
  122. put_cpu();
  123. return 0;
  124. }
  125. EXPORT_SYMBOL_GPL(vcpu_load);
  126. void vcpu_put(struct kvm_vcpu *vcpu)
  127. {
  128. preempt_disable();
  129. kvm_arch_vcpu_put(vcpu);
  130. preempt_notifier_unregister(&vcpu->preempt_notifier);
  131. preempt_enable();
  132. mutex_unlock(&vcpu->mutex);
  133. }
  134. EXPORT_SYMBOL_GPL(vcpu_put);
  135. static void ack_flush(void *_completed)
  136. {
  137. }
  138. bool kvm_make_all_cpus_request(struct kvm *kvm, unsigned int req)
  139. {
  140. int i, cpu, me;
  141. cpumask_var_t cpus;
  142. bool called = true;
  143. struct kvm_vcpu *vcpu;
  144. zalloc_cpumask_var(&cpus, GFP_ATOMIC);
  145. me = get_cpu();
  146. kvm_for_each_vcpu(i, vcpu, kvm) {
  147. kvm_make_request(req, vcpu);
  148. cpu = vcpu->cpu;
  149. /* Set ->requests bit before we read ->mode. */
  150. smp_mb__after_atomic();
  151. if (cpus != NULL && cpu != -1 && cpu != me &&
  152. kvm_vcpu_exiting_guest_mode(vcpu) != OUTSIDE_GUEST_MODE)
  153. cpumask_set_cpu(cpu, cpus);
  154. }
  155. if (unlikely(cpus == NULL))
  156. smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
  157. else if (!cpumask_empty(cpus))
  158. smp_call_function_many(cpus, ack_flush, NULL, 1);
  159. else
  160. called = false;
  161. put_cpu();
  162. free_cpumask_var(cpus);
  163. return called;
  164. }
  165. #ifndef CONFIG_HAVE_KVM_ARCH_TLB_FLUSH_ALL
  166. void kvm_flush_remote_tlbs(struct kvm *kvm)
  167. {
  168. /*
  169. * Read tlbs_dirty before setting KVM_REQ_TLB_FLUSH in
  170. * kvm_make_all_cpus_request.
  171. */
  172. long dirty_count = smp_load_acquire(&kvm->tlbs_dirty);
  173. /*
  174. * We want to publish modifications to the page tables before reading
  175. * mode. Pairs with a memory barrier in arch-specific code.
  176. * - x86: smp_mb__after_srcu_read_unlock in vcpu_enter_guest
  177. * and smp_mb in walk_shadow_page_lockless_begin/end.
  178. * - powerpc: smp_mb in kvmppc_prepare_to_enter.
  179. *
  180. * There is already an smp_mb__after_atomic() before
  181. * kvm_make_all_cpus_request() reads vcpu->mode. We reuse that
  182. * barrier here.
  183. */
  184. if (kvm_make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
  185. ++kvm->stat.remote_tlb_flush;
  186. cmpxchg(&kvm->tlbs_dirty, dirty_count, 0);
  187. }
  188. EXPORT_SYMBOL_GPL(kvm_flush_remote_tlbs);
  189. #endif
  190. void kvm_reload_remote_mmus(struct kvm *kvm)
  191. {
  192. kvm_make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
  193. }
  194. int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
  195. {
  196. struct page *page;
  197. int r;
  198. mutex_init(&vcpu->mutex);
  199. vcpu->cpu = -1;
  200. vcpu->kvm = kvm;
  201. vcpu->vcpu_id = id;
  202. vcpu->pid = NULL;
  203. init_swait_queue_head(&vcpu->wq);
  204. kvm_async_pf_vcpu_init(vcpu);
  205. vcpu->pre_pcpu = -1;
  206. INIT_LIST_HEAD(&vcpu->blocked_vcpu_list);
  207. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  208. if (!page) {
  209. r = -ENOMEM;
  210. goto fail;
  211. }
  212. vcpu->run = page_address(page);
  213. kvm_vcpu_set_in_spin_loop(vcpu, false);
  214. kvm_vcpu_set_dy_eligible(vcpu, false);
  215. vcpu->preempted = false;
  216. r = kvm_arch_vcpu_init(vcpu);
  217. if (r < 0)
  218. goto fail_free_run;
  219. return 0;
  220. fail_free_run:
  221. free_page((unsigned long)vcpu->run);
  222. fail:
  223. return r;
  224. }
  225. EXPORT_SYMBOL_GPL(kvm_vcpu_init);
  226. void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
  227. {
  228. put_pid(vcpu->pid);
  229. kvm_arch_vcpu_uninit(vcpu);
  230. free_page((unsigned long)vcpu->run);
  231. }
  232. EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
  233. #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
  234. static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
  235. {
  236. return container_of(mn, struct kvm, mmu_notifier);
  237. }
  238. static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
  239. struct mm_struct *mm,
  240. unsigned long address)
  241. {
  242. struct kvm *kvm = mmu_notifier_to_kvm(mn);
  243. int need_tlb_flush, idx;
  244. /*
  245. * When ->invalidate_page runs, the linux pte has been zapped
  246. * already but the page is still allocated until
  247. * ->invalidate_page returns. So if we increase the sequence
  248. * here the kvm page fault will notice if the spte can't be
  249. * established because the page is going to be freed. If
  250. * instead the kvm page fault establishes the spte before
  251. * ->invalidate_page runs, kvm_unmap_hva will release it
  252. * before returning.
  253. *
  254. * The sequence increase only need to be seen at spin_unlock
  255. * time, and not at spin_lock time.
  256. *
  257. * Increasing the sequence after the spin_unlock would be
  258. * unsafe because the kvm page fault could then establish the
  259. * pte after kvm_unmap_hva returned, without noticing the page
  260. * is going to be freed.
  261. */
  262. idx = srcu_read_lock(&kvm->srcu);
  263. spin_lock(&kvm->mmu_lock);
  264. kvm->mmu_notifier_seq++;
  265. need_tlb_flush = kvm_unmap_hva(kvm, address) | kvm->tlbs_dirty;
  266. /* we've to flush the tlb before the pages can be freed */
  267. if (need_tlb_flush)
  268. kvm_flush_remote_tlbs(kvm);
  269. spin_unlock(&kvm->mmu_lock);
  270. kvm_arch_mmu_notifier_invalidate_page(kvm, address);
  271. srcu_read_unlock(&kvm->srcu, idx);
  272. }
  273. static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
  274. struct mm_struct *mm,
  275. unsigned long address,
  276. pte_t pte)
  277. {
  278. struct kvm *kvm = mmu_notifier_to_kvm(mn);
  279. int idx;
  280. idx = srcu_read_lock(&kvm->srcu);
  281. spin_lock(&kvm->mmu_lock);
  282. kvm->mmu_notifier_seq++;
  283. kvm_set_spte_hva(kvm, address, pte);
  284. spin_unlock(&kvm->mmu_lock);
  285. srcu_read_unlock(&kvm->srcu, idx);
  286. }
  287. static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
  288. struct mm_struct *mm,
  289. unsigned long start,
  290. unsigned long end)
  291. {
  292. struct kvm *kvm = mmu_notifier_to_kvm(mn);
  293. int need_tlb_flush = 0, idx;
  294. idx = srcu_read_lock(&kvm->srcu);
  295. spin_lock(&kvm->mmu_lock);
  296. /*
  297. * The count increase must become visible at unlock time as no
  298. * spte can be established without taking the mmu_lock and
  299. * count is also read inside the mmu_lock critical section.
  300. */
  301. kvm->mmu_notifier_count++;
  302. need_tlb_flush = kvm_unmap_hva_range(kvm, start, end);
  303. need_tlb_flush |= kvm->tlbs_dirty;
  304. /* we've to flush the tlb before the pages can be freed */
  305. if (need_tlb_flush)
  306. kvm_flush_remote_tlbs(kvm);
  307. spin_unlock(&kvm->mmu_lock);
  308. srcu_read_unlock(&kvm->srcu, idx);
  309. }
  310. static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
  311. struct mm_struct *mm,
  312. unsigned long start,
  313. unsigned long end)
  314. {
  315. struct kvm *kvm = mmu_notifier_to_kvm(mn);
  316. spin_lock(&kvm->mmu_lock);
  317. /*
  318. * This sequence increase will notify the kvm page fault that
  319. * the page that is going to be mapped in the spte could have
  320. * been freed.
  321. */
  322. kvm->mmu_notifier_seq++;
  323. smp_wmb();
  324. /*
  325. * The above sequence increase must be visible before the
  326. * below count decrease, which is ensured by the smp_wmb above
  327. * in conjunction with the smp_rmb in mmu_notifier_retry().
  328. */
  329. kvm->mmu_notifier_count--;
  330. spin_unlock(&kvm->mmu_lock);
  331. BUG_ON(kvm->mmu_notifier_count < 0);
  332. }
  333. static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
  334. struct mm_struct *mm,
  335. unsigned long start,
  336. unsigned long end)
  337. {
  338. struct kvm *kvm = mmu_notifier_to_kvm(mn);
  339. int young, idx;
  340. idx = srcu_read_lock(&kvm->srcu);
  341. spin_lock(&kvm->mmu_lock);
  342. young = kvm_age_hva(kvm, start, end);
  343. if (young)
  344. kvm_flush_remote_tlbs(kvm);
  345. spin_unlock(&kvm->mmu_lock);
  346. srcu_read_unlock(&kvm->srcu, idx);
  347. return young;
  348. }
  349. static int kvm_mmu_notifier_clear_young(struct mmu_notifier *mn,
  350. struct mm_struct *mm,
  351. unsigned long start,
  352. unsigned long end)
  353. {
  354. struct kvm *kvm = mmu_notifier_to_kvm(mn);
  355. int young, idx;
  356. idx = srcu_read_lock(&kvm->srcu);
  357. spin_lock(&kvm->mmu_lock);
  358. /*
  359. * Even though we do not flush TLB, this will still adversely
  360. * affect performance on pre-Haswell Intel EPT, where there is
  361. * no EPT Access Bit to clear so that we have to tear down EPT
  362. * tables instead. If we find this unacceptable, we can always
  363. * add a parameter to kvm_age_hva so that it effectively doesn't
  364. * do anything on clear_young.
  365. *
  366. * Also note that currently we never issue secondary TLB flushes
  367. * from clear_young, leaving this job up to the regular system
  368. * cadence. If we find this inaccurate, we might come up with a
  369. * more sophisticated heuristic later.
  370. */
  371. young = kvm_age_hva(kvm, start, end);
  372. spin_unlock(&kvm->mmu_lock);
  373. srcu_read_unlock(&kvm->srcu, idx);
  374. return young;
  375. }
  376. static int kvm_mmu_notifier_test_young(struct mmu_notifier *mn,
  377. struct mm_struct *mm,
  378. unsigned long address)
  379. {
  380. struct kvm *kvm = mmu_notifier_to_kvm(mn);
  381. int young, idx;
  382. idx = srcu_read_lock(&kvm->srcu);
  383. spin_lock(&kvm->mmu_lock);
  384. young = kvm_test_age_hva(kvm, address);
  385. spin_unlock(&kvm->mmu_lock);
  386. srcu_read_unlock(&kvm->srcu, idx);
  387. return young;
  388. }
  389. static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
  390. struct mm_struct *mm)
  391. {
  392. struct kvm *kvm = mmu_notifier_to_kvm(mn);
  393. int idx;
  394. idx = srcu_read_lock(&kvm->srcu);
  395. kvm_arch_flush_shadow_all(kvm);
  396. srcu_read_unlock(&kvm->srcu, idx);
  397. }
  398. static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
  399. .invalidate_page = kvm_mmu_notifier_invalidate_page,
  400. .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
  401. .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
  402. .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
  403. .clear_young = kvm_mmu_notifier_clear_young,
  404. .test_young = kvm_mmu_notifier_test_young,
  405. .change_pte = kvm_mmu_notifier_change_pte,
  406. .release = kvm_mmu_notifier_release,
  407. };
  408. static int kvm_init_mmu_notifier(struct kvm *kvm)
  409. {
  410. kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
  411. return mmu_notifier_register(&kvm->mmu_notifier, current->mm);
  412. }
  413. #else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
  414. static int kvm_init_mmu_notifier(struct kvm *kvm)
  415. {
  416. return 0;
  417. }
  418. #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
  419. static struct kvm_memslots *kvm_alloc_memslots(void)
  420. {
  421. int i;
  422. struct kvm_memslots *slots;
  423. slots = kvm_kvzalloc(sizeof(struct kvm_memslots));
  424. if (!slots)
  425. return NULL;
  426. /*
  427. * Init kvm generation close to the maximum to easily test the
  428. * code of handling generation number wrap-around.
  429. */
  430. slots->generation = -150;
  431. for (i = 0; i < KVM_MEM_SLOTS_NUM; i++)
  432. slots->id_to_index[i] = slots->memslots[i].id = i;
  433. return slots;
  434. }
  435. static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot)
  436. {
  437. if (!memslot->dirty_bitmap)
  438. return;
  439. kvfree(memslot->dirty_bitmap);
  440. memslot->dirty_bitmap = NULL;
  441. }
  442. /*
  443. * Free any memory in @free but not in @dont.
  444. */
  445. static void kvm_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
  446. struct kvm_memory_slot *dont)
  447. {
  448. if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
  449. kvm_destroy_dirty_bitmap(free);
  450. kvm_arch_free_memslot(kvm, free, dont);
  451. free->npages = 0;
  452. }
  453. static void kvm_free_memslots(struct kvm *kvm, struct kvm_memslots *slots)
  454. {
  455. struct kvm_memory_slot *memslot;
  456. if (!slots)
  457. return;
  458. kvm_for_each_memslot(memslot, slots)
  459. kvm_free_memslot(kvm, memslot, NULL);
  460. kvfree(slots);
  461. }
  462. static void kvm_destroy_vm_debugfs(struct kvm *kvm)
  463. {
  464. int i;
  465. if (!kvm->debugfs_dentry)
  466. return;
  467. debugfs_remove_recursive(kvm->debugfs_dentry);
  468. for (i = 0; i < kvm_debugfs_num_entries; i++)
  469. kfree(kvm->debugfs_stat_data[i]);
  470. kfree(kvm->debugfs_stat_data);
  471. }
  472. static int kvm_create_vm_debugfs(struct kvm *kvm, int fd)
  473. {
  474. char dir_name[ITOA_MAX_LEN * 2];
  475. struct kvm_stat_data *stat_data;
  476. struct kvm_stats_debugfs_item *p;
  477. if (!debugfs_initialized())
  478. return 0;
  479. snprintf(dir_name, sizeof(dir_name), "%d-%d", task_pid_nr(current), fd);
  480. kvm->debugfs_dentry = debugfs_create_dir(dir_name,
  481. kvm_debugfs_dir);
  482. if (!kvm->debugfs_dentry)
  483. return -ENOMEM;
  484. kvm->debugfs_stat_data = kcalloc(kvm_debugfs_num_entries,
  485. sizeof(*kvm->debugfs_stat_data),
  486. GFP_KERNEL);
  487. if (!kvm->debugfs_stat_data)
  488. return -ENOMEM;
  489. for (p = debugfs_entries; p->name; p++) {
  490. stat_data = kzalloc(sizeof(*stat_data), GFP_KERNEL);
  491. if (!stat_data)
  492. return -ENOMEM;
  493. stat_data->kvm = kvm;
  494. stat_data->offset = p->offset;
  495. kvm->debugfs_stat_data[p - debugfs_entries] = stat_data;
  496. if (!debugfs_create_file(p->name, 0444,
  497. kvm->debugfs_dentry,
  498. stat_data,
  499. stat_fops_per_vm[p->kind]))
  500. return -ENOMEM;
  501. }
  502. return 0;
  503. }
  504. static struct kvm *kvm_create_vm(unsigned long type)
  505. {
  506. int r, i;
  507. struct kvm *kvm = kvm_arch_alloc_vm();
  508. if (!kvm)
  509. return ERR_PTR(-ENOMEM);
  510. spin_lock_init(&kvm->mmu_lock);
  511. atomic_inc(&current->mm->mm_count);
  512. kvm->mm = current->mm;
  513. kvm_eventfd_init(kvm);
  514. mutex_init(&kvm->lock);
  515. mutex_init(&kvm->irq_lock);
  516. mutex_init(&kvm->slots_lock);
  517. atomic_set(&kvm->users_count, 1);
  518. INIT_LIST_HEAD(&kvm->devices);
  519. r = kvm_arch_init_vm(kvm, type);
  520. if (r)
  521. goto out_err_no_disable;
  522. r = hardware_enable_all();
  523. if (r)
  524. goto out_err_no_disable;
  525. #ifdef CONFIG_HAVE_KVM_IRQFD
  526. INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
  527. #endif
  528. BUILD_BUG_ON(KVM_MEM_SLOTS_NUM > SHRT_MAX);
  529. r = -ENOMEM;
  530. for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
  531. kvm->memslots[i] = kvm_alloc_memslots();
  532. if (!kvm->memslots[i])
  533. goto out_err_no_srcu;
  534. }
  535. if (init_srcu_struct(&kvm->srcu))
  536. goto out_err_no_srcu;
  537. if (init_srcu_struct(&kvm->irq_srcu))
  538. goto out_err_no_irq_srcu;
  539. for (i = 0; i < KVM_NR_BUSES; i++) {
  540. kvm->buses[i] = kzalloc(sizeof(struct kvm_io_bus),
  541. GFP_KERNEL);
  542. if (!kvm->buses[i])
  543. goto out_err;
  544. }
  545. r = kvm_init_mmu_notifier(kvm);
  546. if (r)
  547. goto out_err;
  548. spin_lock(&kvm_lock);
  549. list_add(&kvm->vm_list, &vm_list);
  550. spin_unlock(&kvm_lock);
  551. preempt_notifier_inc();
  552. return kvm;
  553. out_err:
  554. cleanup_srcu_struct(&kvm->irq_srcu);
  555. out_err_no_irq_srcu:
  556. cleanup_srcu_struct(&kvm->srcu);
  557. out_err_no_srcu:
  558. hardware_disable_all();
  559. out_err_no_disable:
  560. for (i = 0; i < KVM_NR_BUSES; i++)
  561. kfree(kvm->buses[i]);
  562. for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++)
  563. kvm_free_memslots(kvm, kvm->memslots[i]);
  564. kvm_arch_free_vm(kvm);
  565. mmdrop(current->mm);
  566. return ERR_PTR(r);
  567. }
  568. /*
  569. * Avoid using vmalloc for a small buffer.
  570. * Should not be used when the size is statically known.
  571. */
  572. void *kvm_kvzalloc(unsigned long size)
  573. {
  574. if (size > PAGE_SIZE)
  575. return vzalloc(size);
  576. else
  577. return kzalloc(size, GFP_KERNEL);
  578. }
  579. static void kvm_destroy_devices(struct kvm *kvm)
  580. {
  581. struct kvm_device *dev, *tmp;
  582. list_for_each_entry_safe(dev, tmp, &kvm->devices, vm_node) {
  583. list_del(&dev->vm_node);
  584. dev->ops->destroy(dev);
  585. }
  586. }
  587. static void kvm_destroy_vm(struct kvm *kvm)
  588. {
  589. int i;
  590. struct mm_struct *mm = kvm->mm;
  591. kvm_destroy_vm_debugfs(kvm);
  592. kvm_arch_sync_events(kvm);
  593. spin_lock(&kvm_lock);
  594. list_del(&kvm->vm_list);
  595. spin_unlock(&kvm_lock);
  596. kvm_free_irq_routing(kvm);
  597. for (i = 0; i < KVM_NR_BUSES; i++)
  598. kvm_io_bus_destroy(kvm->buses[i]);
  599. kvm_coalesced_mmio_free(kvm);
  600. #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
  601. mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
  602. #else
  603. kvm_arch_flush_shadow_all(kvm);
  604. #endif
  605. kvm_arch_destroy_vm(kvm);
  606. kvm_destroy_devices(kvm);
  607. for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++)
  608. kvm_free_memslots(kvm, kvm->memslots[i]);
  609. cleanup_srcu_struct(&kvm->irq_srcu);
  610. cleanup_srcu_struct(&kvm->srcu);
  611. kvm_arch_free_vm(kvm);
  612. preempt_notifier_dec();
  613. hardware_disable_all();
  614. mmdrop(mm);
  615. }
  616. void kvm_get_kvm(struct kvm *kvm)
  617. {
  618. atomic_inc(&kvm->users_count);
  619. }
  620. EXPORT_SYMBOL_GPL(kvm_get_kvm);
  621. void kvm_put_kvm(struct kvm *kvm)
  622. {
  623. if (atomic_dec_and_test(&kvm->users_count))
  624. kvm_destroy_vm(kvm);
  625. }
  626. EXPORT_SYMBOL_GPL(kvm_put_kvm);
  627. static int kvm_vm_release(struct inode *inode, struct file *filp)
  628. {
  629. struct kvm *kvm = filp->private_data;
  630. kvm_irqfd_release(kvm);
  631. kvm_put_kvm(kvm);
  632. return 0;
  633. }
  634. /*
  635. * Allocation size is twice as large as the actual dirty bitmap size.
  636. * See x86's kvm_vm_ioctl_get_dirty_log() why this is needed.
  637. */
  638. static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot)
  639. {
  640. unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot);
  641. memslot->dirty_bitmap = kvm_kvzalloc(dirty_bytes);
  642. if (!memslot->dirty_bitmap)
  643. return -ENOMEM;
  644. return 0;
  645. }
  646. /*
  647. * Insert memslot and re-sort memslots based on their GFN,
  648. * so binary search could be used to lookup GFN.
  649. * Sorting algorithm takes advantage of having initially
  650. * sorted array and known changed memslot position.
  651. */
  652. static void update_memslots(struct kvm_memslots *slots,
  653. struct kvm_memory_slot *new)
  654. {
  655. int id = new->id;
  656. int i = slots->id_to_index[id];
  657. struct kvm_memory_slot *mslots = slots->memslots;
  658. WARN_ON(mslots[i].id != id);
  659. if (!new->npages) {
  660. WARN_ON(!mslots[i].npages);
  661. if (mslots[i].npages)
  662. slots->used_slots--;
  663. } else {
  664. if (!mslots[i].npages)
  665. slots->used_slots++;
  666. }
  667. while (i < KVM_MEM_SLOTS_NUM - 1 &&
  668. new->base_gfn <= mslots[i + 1].base_gfn) {
  669. if (!mslots[i + 1].npages)
  670. break;
  671. mslots[i] = mslots[i + 1];
  672. slots->id_to_index[mslots[i].id] = i;
  673. i++;
  674. }
  675. /*
  676. * The ">=" is needed when creating a slot with base_gfn == 0,
  677. * so that it moves before all those with base_gfn == npages == 0.
  678. *
  679. * On the other hand, if new->npages is zero, the above loop has
  680. * already left i pointing to the beginning of the empty part of
  681. * mslots, and the ">=" would move the hole backwards in this
  682. * case---which is wrong. So skip the loop when deleting a slot.
  683. */
  684. if (new->npages) {
  685. while (i > 0 &&
  686. new->base_gfn >= mslots[i - 1].base_gfn) {
  687. mslots[i] = mslots[i - 1];
  688. slots->id_to_index[mslots[i].id] = i;
  689. i--;
  690. }
  691. } else
  692. WARN_ON_ONCE(i != slots->used_slots);
  693. mslots[i] = *new;
  694. slots->id_to_index[mslots[i].id] = i;
  695. }
  696. static int check_memory_region_flags(const struct kvm_userspace_memory_region *mem)
  697. {
  698. u32 valid_flags = KVM_MEM_LOG_DIRTY_PAGES;
  699. #ifdef __KVM_HAVE_READONLY_MEM
  700. valid_flags |= KVM_MEM_READONLY;
  701. #endif
  702. if (mem->flags & ~valid_flags)
  703. return -EINVAL;
  704. return 0;
  705. }
  706. static struct kvm_memslots *install_new_memslots(struct kvm *kvm,
  707. int as_id, struct kvm_memslots *slots)
  708. {
  709. struct kvm_memslots *old_memslots = __kvm_memslots(kvm, as_id);
  710. /*
  711. * Set the low bit in the generation, which disables SPTE caching
  712. * until the end of synchronize_srcu_expedited.
  713. */
  714. WARN_ON(old_memslots->generation & 1);
  715. slots->generation = old_memslots->generation + 1;
  716. rcu_assign_pointer(kvm->memslots[as_id], slots);
  717. synchronize_srcu_expedited(&kvm->srcu);
  718. /*
  719. * Increment the new memslot generation a second time. This prevents
  720. * vm exits that race with memslot updates from caching a memslot
  721. * generation that will (potentially) be valid forever.
  722. */
  723. slots->generation++;
  724. kvm_arch_memslots_updated(kvm, slots);
  725. return old_memslots;
  726. }
  727. /*
  728. * Allocate some memory and give it an address in the guest physical address
  729. * space.
  730. *
  731. * Discontiguous memory is allowed, mostly for framebuffers.
  732. *
  733. * Must be called holding kvm->slots_lock for write.
  734. */
  735. int __kvm_set_memory_region(struct kvm *kvm,
  736. const struct kvm_userspace_memory_region *mem)
  737. {
  738. int r;
  739. gfn_t base_gfn;
  740. unsigned long npages;
  741. struct kvm_memory_slot *slot;
  742. struct kvm_memory_slot old, new;
  743. struct kvm_memslots *slots = NULL, *old_memslots;
  744. int as_id, id;
  745. enum kvm_mr_change change;
  746. r = check_memory_region_flags(mem);
  747. if (r)
  748. goto out;
  749. r = -EINVAL;
  750. as_id = mem->slot >> 16;
  751. id = (u16)mem->slot;
  752. /* General sanity checks */
  753. if (mem->memory_size & (PAGE_SIZE - 1))
  754. goto out;
  755. if (mem->guest_phys_addr & (PAGE_SIZE - 1))
  756. goto out;
  757. /* We can read the guest memory with __xxx_user() later on. */
  758. if ((id < KVM_USER_MEM_SLOTS) &&
  759. ((mem->userspace_addr & (PAGE_SIZE - 1)) ||
  760. !access_ok(VERIFY_WRITE,
  761. (void __user *)(unsigned long)mem->userspace_addr,
  762. mem->memory_size)))
  763. goto out;
  764. if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_MEM_SLOTS_NUM)
  765. goto out;
  766. if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
  767. goto out;
  768. slot = id_to_memslot(__kvm_memslots(kvm, as_id), id);
  769. base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
  770. npages = mem->memory_size >> PAGE_SHIFT;
  771. if (npages > KVM_MEM_MAX_NR_PAGES)
  772. goto out;
  773. new = old = *slot;
  774. new.id = id;
  775. new.base_gfn = base_gfn;
  776. new.npages = npages;
  777. new.flags = mem->flags;
  778. if (npages) {
  779. if (!old.npages)
  780. change = KVM_MR_CREATE;
  781. else { /* Modify an existing slot. */
  782. if ((mem->userspace_addr != old.userspace_addr) ||
  783. (npages != old.npages) ||
  784. ((new.flags ^ old.flags) & KVM_MEM_READONLY))
  785. goto out;
  786. if (base_gfn != old.base_gfn)
  787. change = KVM_MR_MOVE;
  788. else if (new.flags != old.flags)
  789. change = KVM_MR_FLAGS_ONLY;
  790. else { /* Nothing to change. */
  791. r = 0;
  792. goto out;
  793. }
  794. }
  795. } else {
  796. if (!old.npages)
  797. goto out;
  798. change = KVM_MR_DELETE;
  799. new.base_gfn = 0;
  800. new.flags = 0;
  801. }
  802. if ((change == KVM_MR_CREATE) || (change == KVM_MR_MOVE)) {
  803. /* Check for overlaps */
  804. r = -EEXIST;
  805. kvm_for_each_memslot(slot, __kvm_memslots(kvm, as_id)) {
  806. if ((slot->id >= KVM_USER_MEM_SLOTS) ||
  807. (slot->id == id))
  808. continue;
  809. if (!((base_gfn + npages <= slot->base_gfn) ||
  810. (base_gfn >= slot->base_gfn + slot->npages)))
  811. goto out;
  812. }
  813. }
  814. /* Free page dirty bitmap if unneeded */
  815. if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
  816. new.dirty_bitmap = NULL;
  817. r = -ENOMEM;
  818. if (change == KVM_MR_CREATE) {
  819. new.userspace_addr = mem->userspace_addr;
  820. if (kvm_arch_create_memslot(kvm, &new, npages))
  821. goto out_free;
  822. }
  823. /* Allocate page dirty bitmap if needed */
  824. if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
  825. if (kvm_create_dirty_bitmap(&new) < 0)
  826. goto out_free;
  827. }
  828. slots = kvm_kvzalloc(sizeof(struct kvm_memslots));
  829. if (!slots)
  830. goto out_free;
  831. memcpy(slots, __kvm_memslots(kvm, as_id), sizeof(struct kvm_memslots));
  832. if ((change == KVM_MR_DELETE) || (change == KVM_MR_MOVE)) {
  833. slot = id_to_memslot(slots, id);
  834. slot->flags |= KVM_MEMSLOT_INVALID;
  835. old_memslots = install_new_memslots(kvm, as_id, slots);
  836. /* slot was deleted or moved, clear iommu mapping */
  837. kvm_iommu_unmap_pages(kvm, &old);
  838. /* From this point no new shadow pages pointing to a deleted,
  839. * or moved, memslot will be created.
  840. *
  841. * validation of sp->gfn happens in:
  842. * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
  843. * - kvm_is_visible_gfn (mmu_check_roots)
  844. */
  845. kvm_arch_flush_shadow_memslot(kvm, slot);
  846. /*
  847. * We can re-use the old_memslots from above, the only difference
  848. * from the currently installed memslots is the invalid flag. This
  849. * will get overwritten by update_memslots anyway.
  850. */
  851. slots = old_memslots;
  852. }
  853. r = kvm_arch_prepare_memory_region(kvm, &new, mem, change);
  854. if (r)
  855. goto out_slots;
  856. /* actual memory is freed via old in kvm_free_memslot below */
  857. if (change == KVM_MR_DELETE) {
  858. new.dirty_bitmap = NULL;
  859. memset(&new.arch, 0, sizeof(new.arch));
  860. }
  861. update_memslots(slots, &new);
  862. old_memslots = install_new_memslots(kvm, as_id, slots);
  863. kvm_arch_commit_memory_region(kvm, mem, &old, &new, change);
  864. kvm_free_memslot(kvm, &old, &new);
  865. kvfree(old_memslots);
  866. /*
  867. * IOMMU mapping: New slots need to be mapped. Old slots need to be
  868. * un-mapped and re-mapped if their base changes. Since base change
  869. * unmapping is handled above with slot deletion, mapping alone is
  870. * needed here. Anything else the iommu might care about for existing
  871. * slots (size changes, userspace addr changes and read-only flag
  872. * changes) is disallowed above, so any other attribute changes getting
  873. * here can be skipped.
  874. */
  875. if ((change == KVM_MR_CREATE) || (change == KVM_MR_MOVE)) {
  876. r = kvm_iommu_map_pages(kvm, &new);
  877. return r;
  878. }
  879. return 0;
  880. out_slots:
  881. kvfree(slots);
  882. out_free:
  883. kvm_free_memslot(kvm, &new, &old);
  884. out:
  885. return r;
  886. }
  887. EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
  888. int kvm_set_memory_region(struct kvm *kvm,
  889. const struct kvm_userspace_memory_region *mem)
  890. {
  891. int r;
  892. mutex_lock(&kvm->slots_lock);
  893. r = __kvm_set_memory_region(kvm, mem);
  894. mutex_unlock(&kvm->slots_lock);
  895. return r;
  896. }
  897. EXPORT_SYMBOL_GPL(kvm_set_memory_region);
  898. static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
  899. struct kvm_userspace_memory_region *mem)
  900. {
  901. if ((u16)mem->slot >= KVM_USER_MEM_SLOTS)
  902. return -EINVAL;
  903. return kvm_set_memory_region(kvm, mem);
  904. }
  905. int kvm_get_dirty_log(struct kvm *kvm,
  906. struct kvm_dirty_log *log, int *is_dirty)
  907. {
  908. struct kvm_memslots *slots;
  909. struct kvm_memory_slot *memslot;
  910. int r, i, as_id, id;
  911. unsigned long n;
  912. unsigned long any = 0;
  913. r = -EINVAL;
  914. as_id = log->slot >> 16;
  915. id = (u16)log->slot;
  916. if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_USER_MEM_SLOTS)
  917. goto out;
  918. slots = __kvm_memslots(kvm, as_id);
  919. memslot = id_to_memslot(slots, id);
  920. r = -ENOENT;
  921. if (!memslot->dirty_bitmap)
  922. goto out;
  923. n = kvm_dirty_bitmap_bytes(memslot);
  924. for (i = 0; !any && i < n/sizeof(long); ++i)
  925. any = memslot->dirty_bitmap[i];
  926. r = -EFAULT;
  927. if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
  928. goto out;
  929. if (any)
  930. *is_dirty = 1;
  931. r = 0;
  932. out:
  933. return r;
  934. }
  935. EXPORT_SYMBOL_GPL(kvm_get_dirty_log);
  936. #ifdef CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT
  937. /**
  938. * kvm_get_dirty_log_protect - get a snapshot of dirty pages, and if any pages
  939. * are dirty write protect them for next write.
  940. * @kvm: pointer to kvm instance
  941. * @log: slot id and address to which we copy the log
  942. * @is_dirty: flag set if any page is dirty
  943. *
  944. * We need to keep it in mind that VCPU threads can write to the bitmap
  945. * concurrently. So, to avoid losing track of dirty pages we keep the
  946. * following order:
  947. *
  948. * 1. Take a snapshot of the bit and clear it if needed.
  949. * 2. Write protect the corresponding page.
  950. * 3. Copy the snapshot to the userspace.
  951. * 4. Upon return caller flushes TLB's if needed.
  952. *
  953. * Between 2 and 4, the guest may write to the page using the remaining TLB
  954. * entry. This is not a problem because the page is reported dirty using
  955. * the snapshot taken before and step 4 ensures that writes done after
  956. * exiting to userspace will be logged for the next call.
  957. *
  958. */
  959. int kvm_get_dirty_log_protect(struct kvm *kvm,
  960. struct kvm_dirty_log *log, bool *is_dirty)
  961. {
  962. struct kvm_memslots *slots;
  963. struct kvm_memory_slot *memslot;
  964. int r, i, as_id, id;
  965. unsigned long n;
  966. unsigned long *dirty_bitmap;
  967. unsigned long *dirty_bitmap_buffer;
  968. r = -EINVAL;
  969. as_id = log->slot >> 16;
  970. id = (u16)log->slot;
  971. if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_USER_MEM_SLOTS)
  972. goto out;
  973. slots = __kvm_memslots(kvm, as_id);
  974. memslot = id_to_memslot(slots, id);
  975. dirty_bitmap = memslot->dirty_bitmap;
  976. r = -ENOENT;
  977. if (!dirty_bitmap)
  978. goto out;
  979. n = kvm_dirty_bitmap_bytes(memslot);
  980. dirty_bitmap_buffer = dirty_bitmap + n / sizeof(long);
  981. memset(dirty_bitmap_buffer, 0, n);
  982. spin_lock(&kvm->mmu_lock);
  983. *is_dirty = false;
  984. for (i = 0; i < n / sizeof(long); i++) {
  985. unsigned long mask;
  986. gfn_t offset;
  987. if (!dirty_bitmap[i])
  988. continue;
  989. *is_dirty = true;
  990. mask = xchg(&dirty_bitmap[i], 0);
  991. dirty_bitmap_buffer[i] = mask;
  992. if (mask) {
  993. offset = i * BITS_PER_LONG;
  994. kvm_arch_mmu_enable_log_dirty_pt_masked(kvm, memslot,
  995. offset, mask);
  996. }
  997. }
  998. spin_unlock(&kvm->mmu_lock);
  999. r = -EFAULT;
  1000. if (copy_to_user(log->dirty_bitmap, dirty_bitmap_buffer, n))
  1001. goto out;
  1002. r = 0;
  1003. out:
  1004. return r;
  1005. }
  1006. EXPORT_SYMBOL_GPL(kvm_get_dirty_log_protect);
  1007. #endif
  1008. bool kvm_largepages_enabled(void)
  1009. {
  1010. return largepages_enabled;
  1011. }
  1012. void kvm_disable_largepages(void)
  1013. {
  1014. largepages_enabled = false;
  1015. }
  1016. EXPORT_SYMBOL_GPL(kvm_disable_largepages);
  1017. struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
  1018. {
  1019. return __gfn_to_memslot(kvm_memslots(kvm), gfn);
  1020. }
  1021. EXPORT_SYMBOL_GPL(gfn_to_memslot);
  1022. struct kvm_memory_slot *kvm_vcpu_gfn_to_memslot(struct kvm_vcpu *vcpu, gfn_t gfn)
  1023. {
  1024. return __gfn_to_memslot(kvm_vcpu_memslots(vcpu), gfn);
  1025. }
  1026. bool kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
  1027. {
  1028. struct kvm_memory_slot *memslot = gfn_to_memslot(kvm, gfn);
  1029. if (!memslot || memslot->id >= KVM_USER_MEM_SLOTS ||
  1030. memslot->flags & KVM_MEMSLOT_INVALID)
  1031. return false;
  1032. return true;
  1033. }
  1034. EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
  1035. unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn)
  1036. {
  1037. struct vm_area_struct *vma;
  1038. unsigned long addr, size;
  1039. size = PAGE_SIZE;
  1040. addr = gfn_to_hva(kvm, gfn);
  1041. if (kvm_is_error_hva(addr))
  1042. return PAGE_SIZE;
  1043. down_read(&current->mm->mmap_sem);
  1044. vma = find_vma(current->mm, addr);
  1045. if (!vma)
  1046. goto out;
  1047. size = vma_kernel_pagesize(vma);
  1048. out:
  1049. up_read(&current->mm->mmap_sem);
  1050. return size;
  1051. }
  1052. static bool memslot_is_readonly(struct kvm_memory_slot *slot)
  1053. {
  1054. return slot->flags & KVM_MEM_READONLY;
  1055. }
  1056. static unsigned long __gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
  1057. gfn_t *nr_pages, bool write)
  1058. {
  1059. if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
  1060. return KVM_HVA_ERR_BAD;
  1061. if (memslot_is_readonly(slot) && write)
  1062. return KVM_HVA_ERR_RO_BAD;
  1063. if (nr_pages)
  1064. *nr_pages = slot->npages - (gfn - slot->base_gfn);
  1065. return __gfn_to_hva_memslot(slot, gfn);
  1066. }
  1067. static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
  1068. gfn_t *nr_pages)
  1069. {
  1070. return __gfn_to_hva_many(slot, gfn, nr_pages, true);
  1071. }
  1072. unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot,
  1073. gfn_t gfn)
  1074. {
  1075. return gfn_to_hva_many(slot, gfn, NULL);
  1076. }
  1077. EXPORT_SYMBOL_GPL(gfn_to_hva_memslot);
  1078. unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
  1079. {
  1080. return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL);
  1081. }
  1082. EXPORT_SYMBOL_GPL(gfn_to_hva);
  1083. unsigned long kvm_vcpu_gfn_to_hva(struct kvm_vcpu *vcpu, gfn_t gfn)
  1084. {
  1085. return gfn_to_hva_many(kvm_vcpu_gfn_to_memslot(vcpu, gfn), gfn, NULL);
  1086. }
  1087. EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_hva);
  1088. /*
  1089. * If writable is set to false, the hva returned by this function is only
  1090. * allowed to be read.
  1091. */
  1092. unsigned long gfn_to_hva_memslot_prot(struct kvm_memory_slot *slot,
  1093. gfn_t gfn, bool *writable)
  1094. {
  1095. unsigned long hva = __gfn_to_hva_many(slot, gfn, NULL, false);
  1096. if (!kvm_is_error_hva(hva) && writable)
  1097. *writable = !memslot_is_readonly(slot);
  1098. return hva;
  1099. }
  1100. unsigned long gfn_to_hva_prot(struct kvm *kvm, gfn_t gfn, bool *writable)
  1101. {
  1102. struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
  1103. return gfn_to_hva_memslot_prot(slot, gfn, writable);
  1104. }
  1105. unsigned long kvm_vcpu_gfn_to_hva_prot(struct kvm_vcpu *vcpu, gfn_t gfn, bool *writable)
  1106. {
  1107. struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
  1108. return gfn_to_hva_memslot_prot(slot, gfn, writable);
  1109. }
  1110. static int get_user_page_nowait(unsigned long start, int write,
  1111. struct page **page)
  1112. {
  1113. int flags = FOLL_TOUCH | FOLL_NOWAIT | FOLL_HWPOISON | FOLL_GET;
  1114. if (write)
  1115. flags |= FOLL_WRITE;
  1116. return __get_user_pages(current, current->mm, start, 1, flags, page,
  1117. NULL, NULL);
  1118. }
  1119. static inline int check_user_page_hwpoison(unsigned long addr)
  1120. {
  1121. int rc, flags = FOLL_TOUCH | FOLL_HWPOISON | FOLL_WRITE;
  1122. rc = __get_user_pages(current, current->mm, addr, 1,
  1123. flags, NULL, NULL, NULL);
  1124. return rc == -EHWPOISON;
  1125. }
  1126. /*
  1127. * The atomic path to get the writable pfn which will be stored in @pfn,
  1128. * true indicates success, otherwise false is returned.
  1129. */
  1130. static bool hva_to_pfn_fast(unsigned long addr, bool atomic, bool *async,
  1131. bool write_fault, bool *writable, kvm_pfn_t *pfn)
  1132. {
  1133. struct page *page[1];
  1134. int npages;
  1135. if (!(async || atomic))
  1136. return false;
  1137. /*
  1138. * Fast pin a writable pfn only if it is a write fault request
  1139. * or the caller allows to map a writable pfn for a read fault
  1140. * request.
  1141. */
  1142. if (!(write_fault || writable))
  1143. return false;
  1144. npages = __get_user_pages_fast(addr, 1, 1, page);
  1145. if (npages == 1) {
  1146. *pfn = page_to_pfn(page[0]);
  1147. if (writable)
  1148. *writable = true;
  1149. return true;
  1150. }
  1151. return false;
  1152. }
  1153. /*
  1154. * The slow path to get the pfn of the specified host virtual address,
  1155. * 1 indicates success, -errno is returned if error is detected.
  1156. */
  1157. static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault,
  1158. bool *writable, kvm_pfn_t *pfn)
  1159. {
  1160. struct page *page[1];
  1161. int npages = 0;
  1162. might_sleep();
  1163. if (writable)
  1164. *writable = write_fault;
  1165. if (async) {
  1166. down_read(&current->mm->mmap_sem);
  1167. npages = get_user_page_nowait(addr, write_fault, page);
  1168. up_read(&current->mm->mmap_sem);
  1169. } else
  1170. npages = __get_user_pages_unlocked(current, current->mm, addr, 1,
  1171. write_fault, 0, page,
  1172. FOLL_TOUCH|FOLL_HWPOISON);
  1173. if (npages != 1)
  1174. return npages;
  1175. /* map read fault as writable if possible */
  1176. if (unlikely(!write_fault) && writable) {
  1177. struct page *wpage[1];
  1178. npages = __get_user_pages_fast(addr, 1, 1, wpage);
  1179. if (npages == 1) {
  1180. *writable = true;
  1181. put_page(page[0]);
  1182. page[0] = wpage[0];
  1183. }
  1184. npages = 1;
  1185. }
  1186. *pfn = page_to_pfn(page[0]);
  1187. return npages;
  1188. }
  1189. static bool vma_is_valid(struct vm_area_struct *vma, bool write_fault)
  1190. {
  1191. if (unlikely(!(vma->vm_flags & VM_READ)))
  1192. return false;
  1193. if (write_fault && (unlikely(!(vma->vm_flags & VM_WRITE))))
  1194. return false;
  1195. return true;
  1196. }
  1197. /*
  1198. * Pin guest page in memory and return its pfn.
  1199. * @addr: host virtual address which maps memory to the guest
  1200. * @atomic: whether this function can sleep
  1201. * @async: whether this function need to wait IO complete if the
  1202. * host page is not in the memory
  1203. * @write_fault: whether we should get a writable host page
  1204. * @writable: whether it allows to map a writable host page for !@write_fault
  1205. *
  1206. * The function will map a writable host page for these two cases:
  1207. * 1): @write_fault = true
  1208. * 2): @write_fault = false && @writable, @writable will tell the caller
  1209. * whether the mapping is writable.
  1210. */
  1211. static kvm_pfn_t hva_to_pfn(unsigned long addr, bool atomic, bool *async,
  1212. bool write_fault, bool *writable)
  1213. {
  1214. struct vm_area_struct *vma;
  1215. kvm_pfn_t pfn = 0;
  1216. int npages;
  1217. /* we can do it either atomically or asynchronously, not both */
  1218. BUG_ON(atomic && async);
  1219. if (hva_to_pfn_fast(addr, atomic, async, write_fault, writable, &pfn))
  1220. return pfn;
  1221. if (atomic)
  1222. return KVM_PFN_ERR_FAULT;
  1223. npages = hva_to_pfn_slow(addr, async, write_fault, writable, &pfn);
  1224. if (npages == 1)
  1225. return pfn;
  1226. down_read(&current->mm->mmap_sem);
  1227. if (npages == -EHWPOISON ||
  1228. (!async && check_user_page_hwpoison(addr))) {
  1229. pfn = KVM_PFN_ERR_HWPOISON;
  1230. goto exit;
  1231. }
  1232. vma = find_vma_intersection(current->mm, addr, addr + 1);
  1233. if (vma == NULL)
  1234. pfn = KVM_PFN_ERR_FAULT;
  1235. else if ((vma->vm_flags & VM_PFNMAP)) {
  1236. pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) +
  1237. vma->vm_pgoff;
  1238. BUG_ON(!kvm_is_reserved_pfn(pfn));
  1239. } else {
  1240. if (async && vma_is_valid(vma, write_fault))
  1241. *async = true;
  1242. pfn = KVM_PFN_ERR_FAULT;
  1243. }
  1244. exit:
  1245. up_read(&current->mm->mmap_sem);
  1246. return pfn;
  1247. }
  1248. kvm_pfn_t __gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn,
  1249. bool atomic, bool *async, bool write_fault,
  1250. bool *writable)
  1251. {
  1252. unsigned long addr = __gfn_to_hva_many(slot, gfn, NULL, write_fault);
  1253. if (addr == KVM_HVA_ERR_RO_BAD) {
  1254. if (writable)
  1255. *writable = false;
  1256. return KVM_PFN_ERR_RO_FAULT;
  1257. }
  1258. if (kvm_is_error_hva(addr)) {
  1259. if (writable)
  1260. *writable = false;
  1261. return KVM_PFN_NOSLOT;
  1262. }
  1263. /* Do not map writable pfn in the readonly memslot. */
  1264. if (writable && memslot_is_readonly(slot)) {
  1265. *writable = false;
  1266. writable = NULL;
  1267. }
  1268. return hva_to_pfn(addr, atomic, async, write_fault,
  1269. writable);
  1270. }
  1271. EXPORT_SYMBOL_GPL(__gfn_to_pfn_memslot);
  1272. kvm_pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
  1273. bool *writable)
  1274. {
  1275. return __gfn_to_pfn_memslot(gfn_to_memslot(kvm, gfn), gfn, false, NULL,
  1276. write_fault, writable);
  1277. }
  1278. EXPORT_SYMBOL_GPL(gfn_to_pfn_prot);
  1279. kvm_pfn_t gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn)
  1280. {
  1281. return __gfn_to_pfn_memslot(slot, gfn, false, NULL, true, NULL);
  1282. }
  1283. EXPORT_SYMBOL_GPL(gfn_to_pfn_memslot);
  1284. kvm_pfn_t gfn_to_pfn_memslot_atomic(struct kvm_memory_slot *slot, gfn_t gfn)
  1285. {
  1286. return __gfn_to_pfn_memslot(slot, gfn, true, NULL, true, NULL);
  1287. }
  1288. EXPORT_SYMBOL_GPL(gfn_to_pfn_memslot_atomic);
  1289. kvm_pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn)
  1290. {
  1291. return gfn_to_pfn_memslot_atomic(gfn_to_memslot(kvm, gfn), gfn);
  1292. }
  1293. EXPORT_SYMBOL_GPL(gfn_to_pfn_atomic);
  1294. kvm_pfn_t kvm_vcpu_gfn_to_pfn_atomic(struct kvm_vcpu *vcpu, gfn_t gfn)
  1295. {
  1296. return gfn_to_pfn_memslot_atomic(kvm_vcpu_gfn_to_memslot(vcpu, gfn), gfn);
  1297. }
  1298. EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_pfn_atomic);
  1299. kvm_pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
  1300. {
  1301. return gfn_to_pfn_memslot(gfn_to_memslot(kvm, gfn), gfn);
  1302. }
  1303. EXPORT_SYMBOL_GPL(gfn_to_pfn);
  1304. kvm_pfn_t kvm_vcpu_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn)
  1305. {
  1306. return gfn_to_pfn_memslot(kvm_vcpu_gfn_to_memslot(vcpu, gfn), gfn);
  1307. }
  1308. EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_pfn);
  1309. int gfn_to_page_many_atomic(struct kvm_memory_slot *slot, gfn_t gfn,
  1310. struct page **pages, int nr_pages)
  1311. {
  1312. unsigned long addr;
  1313. gfn_t entry;
  1314. addr = gfn_to_hva_many(slot, gfn, &entry);
  1315. if (kvm_is_error_hva(addr))
  1316. return -1;
  1317. if (entry < nr_pages)
  1318. return 0;
  1319. return __get_user_pages_fast(addr, nr_pages, 1, pages);
  1320. }
  1321. EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic);
  1322. static struct page *kvm_pfn_to_page(kvm_pfn_t pfn)
  1323. {
  1324. if (is_error_noslot_pfn(pfn))
  1325. return KVM_ERR_PTR_BAD_PAGE;
  1326. if (kvm_is_reserved_pfn(pfn)) {
  1327. WARN_ON(1);
  1328. return KVM_ERR_PTR_BAD_PAGE;
  1329. }
  1330. return pfn_to_page(pfn);
  1331. }
  1332. struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
  1333. {
  1334. kvm_pfn_t pfn;
  1335. pfn = gfn_to_pfn(kvm, gfn);
  1336. return kvm_pfn_to_page(pfn);
  1337. }
  1338. EXPORT_SYMBOL_GPL(gfn_to_page);
  1339. struct page *kvm_vcpu_gfn_to_page(struct kvm_vcpu *vcpu, gfn_t gfn)
  1340. {
  1341. kvm_pfn_t pfn;
  1342. pfn = kvm_vcpu_gfn_to_pfn(vcpu, gfn);
  1343. return kvm_pfn_to_page(pfn);
  1344. }
  1345. EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_page);
  1346. void kvm_release_page_clean(struct page *page)
  1347. {
  1348. WARN_ON(is_error_page(page));
  1349. kvm_release_pfn_clean(page_to_pfn(page));
  1350. }
  1351. EXPORT_SYMBOL_GPL(kvm_release_page_clean);
  1352. void kvm_release_pfn_clean(kvm_pfn_t pfn)
  1353. {
  1354. if (!is_error_noslot_pfn(pfn) && !kvm_is_reserved_pfn(pfn))
  1355. put_page(pfn_to_page(pfn));
  1356. }
  1357. EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
  1358. void kvm_release_page_dirty(struct page *page)
  1359. {
  1360. WARN_ON(is_error_page(page));
  1361. kvm_release_pfn_dirty(page_to_pfn(page));
  1362. }
  1363. EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
  1364. static void kvm_release_pfn_dirty(kvm_pfn_t pfn)
  1365. {
  1366. kvm_set_pfn_dirty(pfn);
  1367. kvm_release_pfn_clean(pfn);
  1368. }
  1369. void kvm_set_pfn_dirty(kvm_pfn_t pfn)
  1370. {
  1371. if (!kvm_is_reserved_pfn(pfn)) {
  1372. struct page *page = pfn_to_page(pfn);
  1373. if (!PageReserved(page))
  1374. SetPageDirty(page);
  1375. }
  1376. }
  1377. EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
  1378. void kvm_set_pfn_accessed(kvm_pfn_t pfn)
  1379. {
  1380. if (!kvm_is_reserved_pfn(pfn))
  1381. mark_page_accessed(pfn_to_page(pfn));
  1382. }
  1383. EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
  1384. void kvm_get_pfn(kvm_pfn_t pfn)
  1385. {
  1386. if (!kvm_is_reserved_pfn(pfn))
  1387. get_page(pfn_to_page(pfn));
  1388. }
  1389. EXPORT_SYMBOL_GPL(kvm_get_pfn);
  1390. static int next_segment(unsigned long len, int offset)
  1391. {
  1392. if (len > PAGE_SIZE - offset)
  1393. return PAGE_SIZE - offset;
  1394. else
  1395. return len;
  1396. }
  1397. static int __kvm_read_guest_page(struct kvm_memory_slot *slot, gfn_t gfn,
  1398. void *data, int offset, int len)
  1399. {
  1400. int r;
  1401. unsigned long addr;
  1402. addr = gfn_to_hva_memslot_prot(slot, gfn, NULL);
  1403. if (kvm_is_error_hva(addr))
  1404. return -EFAULT;
  1405. r = __copy_from_user(data, (void __user *)addr + offset, len);
  1406. if (r)
  1407. return -EFAULT;
  1408. return 0;
  1409. }
  1410. int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
  1411. int len)
  1412. {
  1413. struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
  1414. return __kvm_read_guest_page(slot, gfn, data, offset, len);
  1415. }
  1416. EXPORT_SYMBOL_GPL(kvm_read_guest_page);
  1417. int kvm_vcpu_read_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, void *data,
  1418. int offset, int len)
  1419. {
  1420. struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
  1421. return __kvm_read_guest_page(slot, gfn, data, offset, len);
  1422. }
  1423. EXPORT_SYMBOL_GPL(kvm_vcpu_read_guest_page);
  1424. int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
  1425. {
  1426. gfn_t gfn = gpa >> PAGE_SHIFT;
  1427. int seg;
  1428. int offset = offset_in_page(gpa);
  1429. int ret;
  1430. while ((seg = next_segment(len, offset)) != 0) {
  1431. ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
  1432. if (ret < 0)
  1433. return ret;
  1434. offset = 0;
  1435. len -= seg;
  1436. data += seg;
  1437. ++gfn;
  1438. }
  1439. return 0;
  1440. }
  1441. EXPORT_SYMBOL_GPL(kvm_read_guest);
  1442. int kvm_vcpu_read_guest(struct kvm_vcpu *vcpu, gpa_t gpa, void *data, unsigned long len)
  1443. {
  1444. gfn_t gfn = gpa >> PAGE_SHIFT;
  1445. int seg;
  1446. int offset = offset_in_page(gpa);
  1447. int ret;
  1448. while ((seg = next_segment(len, offset)) != 0) {
  1449. ret = kvm_vcpu_read_guest_page(vcpu, gfn, data, offset, seg);
  1450. if (ret < 0)
  1451. return ret;
  1452. offset = 0;
  1453. len -= seg;
  1454. data += seg;
  1455. ++gfn;
  1456. }
  1457. return 0;
  1458. }
  1459. EXPORT_SYMBOL_GPL(kvm_vcpu_read_guest);
  1460. static int __kvm_read_guest_atomic(struct kvm_memory_slot *slot, gfn_t gfn,
  1461. void *data, int offset, unsigned long len)
  1462. {
  1463. int r;
  1464. unsigned long addr;
  1465. addr = gfn_to_hva_memslot_prot(slot, gfn, NULL);
  1466. if (kvm_is_error_hva(addr))
  1467. return -EFAULT;
  1468. pagefault_disable();
  1469. r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
  1470. pagefault_enable();
  1471. if (r)
  1472. return -EFAULT;
  1473. return 0;
  1474. }
  1475. int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
  1476. unsigned long len)
  1477. {
  1478. gfn_t gfn = gpa >> PAGE_SHIFT;
  1479. struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
  1480. int offset = offset_in_page(gpa);
  1481. return __kvm_read_guest_atomic(slot, gfn, data, offset, len);
  1482. }
  1483. EXPORT_SYMBOL_GPL(kvm_read_guest_atomic);
  1484. int kvm_vcpu_read_guest_atomic(struct kvm_vcpu *vcpu, gpa_t gpa,
  1485. void *data, unsigned long len)
  1486. {
  1487. gfn_t gfn = gpa >> PAGE_SHIFT;
  1488. struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
  1489. int offset = offset_in_page(gpa);
  1490. return __kvm_read_guest_atomic(slot, gfn, data, offset, len);
  1491. }
  1492. EXPORT_SYMBOL_GPL(kvm_vcpu_read_guest_atomic);
  1493. static int __kvm_write_guest_page(struct kvm_memory_slot *memslot, gfn_t gfn,
  1494. const void *data, int offset, int len)
  1495. {
  1496. int r;
  1497. unsigned long addr;
  1498. addr = gfn_to_hva_memslot(memslot, gfn);
  1499. if (kvm_is_error_hva(addr))
  1500. return -EFAULT;
  1501. r = __copy_to_user((void __user *)addr + offset, data, len);
  1502. if (r)
  1503. return -EFAULT;
  1504. mark_page_dirty_in_slot(memslot, gfn);
  1505. return 0;
  1506. }
  1507. int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn,
  1508. const void *data, int offset, int len)
  1509. {
  1510. struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
  1511. return __kvm_write_guest_page(slot, gfn, data, offset, len);
  1512. }
  1513. EXPORT_SYMBOL_GPL(kvm_write_guest_page);
  1514. int kvm_vcpu_write_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn,
  1515. const void *data, int offset, int len)
  1516. {
  1517. struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
  1518. return __kvm_write_guest_page(slot, gfn, data, offset, len);
  1519. }
  1520. EXPORT_SYMBOL_GPL(kvm_vcpu_write_guest_page);
  1521. int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
  1522. unsigned long len)
  1523. {
  1524. gfn_t gfn = gpa >> PAGE_SHIFT;
  1525. int seg;
  1526. int offset = offset_in_page(gpa);
  1527. int ret;
  1528. while ((seg = next_segment(len, offset)) != 0) {
  1529. ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
  1530. if (ret < 0)
  1531. return ret;
  1532. offset = 0;
  1533. len -= seg;
  1534. data += seg;
  1535. ++gfn;
  1536. }
  1537. return 0;
  1538. }
  1539. EXPORT_SYMBOL_GPL(kvm_write_guest);
  1540. int kvm_vcpu_write_guest(struct kvm_vcpu *vcpu, gpa_t gpa, const void *data,
  1541. unsigned long len)
  1542. {
  1543. gfn_t gfn = gpa >> PAGE_SHIFT;
  1544. int seg;
  1545. int offset = offset_in_page(gpa);
  1546. int ret;
  1547. while ((seg = next_segment(len, offset)) != 0) {
  1548. ret = kvm_vcpu_write_guest_page(vcpu, gfn, data, offset, seg);
  1549. if (ret < 0)
  1550. return ret;
  1551. offset = 0;
  1552. len -= seg;
  1553. data += seg;
  1554. ++gfn;
  1555. }
  1556. return 0;
  1557. }
  1558. EXPORT_SYMBOL_GPL(kvm_vcpu_write_guest);
  1559. int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
  1560. gpa_t gpa, unsigned long len)
  1561. {
  1562. struct kvm_memslots *slots = kvm_memslots(kvm);
  1563. int offset = offset_in_page(gpa);
  1564. gfn_t start_gfn = gpa >> PAGE_SHIFT;
  1565. gfn_t end_gfn = (gpa + len - 1) >> PAGE_SHIFT;
  1566. gfn_t nr_pages_needed = end_gfn - start_gfn + 1;
  1567. gfn_t nr_pages_avail;
  1568. ghc->gpa = gpa;
  1569. ghc->generation = slots->generation;
  1570. ghc->len = len;
  1571. ghc->memslot = gfn_to_memslot(kvm, start_gfn);
  1572. ghc->hva = gfn_to_hva_many(ghc->memslot, start_gfn, NULL);
  1573. if (!kvm_is_error_hva(ghc->hva) && nr_pages_needed <= 1) {
  1574. ghc->hva += offset;
  1575. } else {
  1576. /*
  1577. * If the requested region crosses two memslots, we still
  1578. * verify that the entire region is valid here.
  1579. */
  1580. while (start_gfn <= end_gfn) {
  1581. ghc->memslot = gfn_to_memslot(kvm, start_gfn);
  1582. ghc->hva = gfn_to_hva_many(ghc->memslot, start_gfn,
  1583. &nr_pages_avail);
  1584. if (kvm_is_error_hva(ghc->hva))
  1585. return -EFAULT;
  1586. start_gfn += nr_pages_avail;
  1587. }
  1588. /* Use the slow path for cross page reads and writes. */
  1589. ghc->memslot = NULL;
  1590. }
  1591. return 0;
  1592. }
  1593. EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init);
  1594. int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
  1595. void *data, unsigned long len)
  1596. {
  1597. struct kvm_memslots *slots = kvm_memslots(kvm);
  1598. int r;
  1599. BUG_ON(len > ghc->len);
  1600. if (slots->generation != ghc->generation)
  1601. kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa, ghc->len);
  1602. if (unlikely(!ghc->memslot))
  1603. return kvm_write_guest(kvm, ghc->gpa, data, len);
  1604. if (kvm_is_error_hva(ghc->hva))
  1605. return -EFAULT;
  1606. r = __copy_to_user((void __user *)ghc->hva, data, len);
  1607. if (r)
  1608. return -EFAULT;
  1609. mark_page_dirty_in_slot(ghc->memslot, ghc->gpa >> PAGE_SHIFT);
  1610. return 0;
  1611. }
  1612. EXPORT_SYMBOL_GPL(kvm_write_guest_cached);
  1613. int kvm_read_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
  1614. void *data, unsigned long len)
  1615. {
  1616. struct kvm_memslots *slots = kvm_memslots(kvm);
  1617. int r;
  1618. BUG_ON(len > ghc->len);
  1619. if (slots->generation != ghc->generation)
  1620. kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa, ghc->len);
  1621. if (unlikely(!ghc->memslot))
  1622. return kvm_read_guest(kvm, ghc->gpa, data, len);
  1623. if (kvm_is_error_hva(ghc->hva))
  1624. return -EFAULT;
  1625. r = __copy_from_user(data, (void __user *)ghc->hva, len);
  1626. if (r)
  1627. return -EFAULT;
  1628. return 0;
  1629. }
  1630. EXPORT_SYMBOL_GPL(kvm_read_guest_cached);
  1631. int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
  1632. {
  1633. const void *zero_page = (const void *) __va(page_to_phys(ZERO_PAGE(0)));
  1634. return kvm_write_guest_page(kvm, gfn, zero_page, offset, len);
  1635. }
  1636. EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
  1637. int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
  1638. {
  1639. gfn_t gfn = gpa >> PAGE_SHIFT;
  1640. int seg;
  1641. int offset = offset_in_page(gpa);
  1642. int ret;
  1643. while ((seg = next_segment(len, offset)) != 0) {
  1644. ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
  1645. if (ret < 0)
  1646. return ret;
  1647. offset = 0;
  1648. len -= seg;
  1649. ++gfn;
  1650. }
  1651. return 0;
  1652. }
  1653. EXPORT_SYMBOL_GPL(kvm_clear_guest);
  1654. static void mark_page_dirty_in_slot(struct kvm_memory_slot *memslot,
  1655. gfn_t gfn)
  1656. {
  1657. if (memslot && memslot->dirty_bitmap) {
  1658. unsigned long rel_gfn = gfn - memslot->base_gfn;
  1659. set_bit_le(rel_gfn, memslot->dirty_bitmap);
  1660. }
  1661. }
  1662. void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
  1663. {
  1664. struct kvm_memory_slot *memslot;
  1665. memslot = gfn_to_memslot(kvm, gfn);
  1666. mark_page_dirty_in_slot(memslot, gfn);
  1667. }
  1668. EXPORT_SYMBOL_GPL(mark_page_dirty);
  1669. void kvm_vcpu_mark_page_dirty(struct kvm_vcpu *vcpu, gfn_t gfn)
  1670. {
  1671. struct kvm_memory_slot *memslot;
  1672. memslot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
  1673. mark_page_dirty_in_slot(memslot, gfn);
  1674. }
  1675. EXPORT_SYMBOL_GPL(kvm_vcpu_mark_page_dirty);
  1676. static void grow_halt_poll_ns(struct kvm_vcpu *vcpu)
  1677. {
  1678. unsigned int old, val, grow;
  1679. old = val = vcpu->halt_poll_ns;
  1680. grow = READ_ONCE(halt_poll_ns_grow);
  1681. /* 10us base */
  1682. if (val == 0 && grow)
  1683. val = 10000;
  1684. else
  1685. val *= grow;
  1686. if (val > halt_poll_ns)
  1687. val = halt_poll_ns;
  1688. vcpu->halt_poll_ns = val;
  1689. trace_kvm_halt_poll_ns_grow(vcpu->vcpu_id, val, old);
  1690. }
  1691. static void shrink_halt_poll_ns(struct kvm_vcpu *vcpu)
  1692. {
  1693. unsigned int old, val, shrink;
  1694. old = val = vcpu->halt_poll_ns;
  1695. shrink = READ_ONCE(halt_poll_ns_shrink);
  1696. if (shrink == 0)
  1697. val = 0;
  1698. else
  1699. val /= shrink;
  1700. vcpu->halt_poll_ns = val;
  1701. trace_kvm_halt_poll_ns_shrink(vcpu->vcpu_id, val, old);
  1702. }
  1703. static int kvm_vcpu_check_block(struct kvm_vcpu *vcpu)
  1704. {
  1705. if (kvm_arch_vcpu_runnable(vcpu)) {
  1706. kvm_make_request(KVM_REQ_UNHALT, vcpu);
  1707. return -EINTR;
  1708. }
  1709. if (kvm_cpu_has_pending_timer(vcpu))
  1710. return -EINTR;
  1711. if (signal_pending(current))
  1712. return -EINTR;
  1713. return 0;
  1714. }
  1715. /*
  1716. * The vCPU has executed a HLT instruction with in-kernel mode enabled.
  1717. */
  1718. void kvm_vcpu_block(struct kvm_vcpu *vcpu)
  1719. {
  1720. ktime_t start, cur;
  1721. DECLARE_SWAITQUEUE(wait);
  1722. bool waited = false;
  1723. u64 block_ns;
  1724. start = cur = ktime_get();
  1725. if (vcpu->halt_poll_ns) {
  1726. ktime_t stop = ktime_add_ns(ktime_get(), vcpu->halt_poll_ns);
  1727. ++vcpu->stat.halt_attempted_poll;
  1728. do {
  1729. /*
  1730. * This sets KVM_REQ_UNHALT if an interrupt
  1731. * arrives.
  1732. */
  1733. if (kvm_vcpu_check_block(vcpu) < 0) {
  1734. ++vcpu->stat.halt_successful_poll;
  1735. if (!vcpu_valid_wakeup(vcpu))
  1736. ++vcpu->stat.halt_poll_invalid;
  1737. goto out;
  1738. }
  1739. cur = ktime_get();
  1740. } while (single_task_running() && ktime_before(cur, stop));
  1741. }
  1742. kvm_arch_vcpu_blocking(vcpu);
  1743. for (;;) {
  1744. prepare_to_swait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
  1745. if (kvm_vcpu_check_block(vcpu) < 0)
  1746. break;
  1747. waited = true;
  1748. schedule();
  1749. }
  1750. finish_swait(&vcpu->wq, &wait);
  1751. cur = ktime_get();
  1752. kvm_arch_vcpu_unblocking(vcpu);
  1753. out:
  1754. block_ns = ktime_to_ns(cur) - ktime_to_ns(start);
  1755. if (!vcpu_valid_wakeup(vcpu))
  1756. shrink_halt_poll_ns(vcpu);
  1757. else if (halt_poll_ns) {
  1758. if (block_ns <= vcpu->halt_poll_ns)
  1759. ;
  1760. /* we had a long block, shrink polling */
  1761. else if (vcpu->halt_poll_ns && block_ns > halt_poll_ns)
  1762. shrink_halt_poll_ns(vcpu);
  1763. /* we had a short halt and our poll time is too small */
  1764. else if (vcpu->halt_poll_ns < halt_poll_ns &&
  1765. block_ns < halt_poll_ns)
  1766. grow_halt_poll_ns(vcpu);
  1767. } else
  1768. vcpu->halt_poll_ns = 0;
  1769. trace_kvm_vcpu_wakeup(block_ns, waited, vcpu_valid_wakeup(vcpu));
  1770. kvm_arch_vcpu_block_finish(vcpu);
  1771. }
  1772. EXPORT_SYMBOL_GPL(kvm_vcpu_block);
  1773. #ifndef CONFIG_S390
  1774. void kvm_vcpu_wake_up(struct kvm_vcpu *vcpu)
  1775. {
  1776. struct swait_queue_head *wqp;
  1777. wqp = kvm_arch_vcpu_wq(vcpu);
  1778. if (swait_active(wqp)) {
  1779. swake_up(wqp);
  1780. ++vcpu->stat.halt_wakeup;
  1781. }
  1782. }
  1783. EXPORT_SYMBOL_GPL(kvm_vcpu_wake_up);
  1784. /*
  1785. * Kick a sleeping VCPU, or a guest VCPU in guest mode, into host kernel mode.
  1786. */
  1787. void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
  1788. {
  1789. int me;
  1790. int cpu = vcpu->cpu;
  1791. kvm_vcpu_wake_up(vcpu);
  1792. me = get_cpu();
  1793. if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
  1794. if (kvm_arch_vcpu_should_kick(vcpu))
  1795. smp_send_reschedule(cpu);
  1796. put_cpu();
  1797. }
  1798. EXPORT_SYMBOL_GPL(kvm_vcpu_kick);
  1799. #endif /* !CONFIG_S390 */
  1800. int kvm_vcpu_yield_to(struct kvm_vcpu *target)
  1801. {
  1802. struct pid *pid;
  1803. struct task_struct *task = NULL;
  1804. int ret = 0;
  1805. rcu_read_lock();
  1806. pid = rcu_dereference(target->pid);
  1807. if (pid)
  1808. task = get_pid_task(pid, PIDTYPE_PID);
  1809. rcu_read_unlock();
  1810. if (!task)
  1811. return ret;
  1812. ret = yield_to(task, 1);
  1813. put_task_struct(task);
  1814. return ret;
  1815. }
  1816. EXPORT_SYMBOL_GPL(kvm_vcpu_yield_to);
  1817. /*
  1818. * Helper that checks whether a VCPU is eligible for directed yield.
  1819. * Most eligible candidate to yield is decided by following heuristics:
  1820. *
  1821. * (a) VCPU which has not done pl-exit or cpu relax intercepted recently
  1822. * (preempted lock holder), indicated by @in_spin_loop.
  1823. * Set at the beiginning and cleared at the end of interception/PLE handler.
  1824. *
  1825. * (b) VCPU which has done pl-exit/ cpu relax intercepted but did not get
  1826. * chance last time (mostly it has become eligible now since we have probably
  1827. * yielded to lockholder in last iteration. This is done by toggling
  1828. * @dy_eligible each time a VCPU checked for eligibility.)
  1829. *
  1830. * Yielding to a recently pl-exited/cpu relax intercepted VCPU before yielding
  1831. * to preempted lock-holder could result in wrong VCPU selection and CPU
  1832. * burning. Giving priority for a potential lock-holder increases lock
  1833. * progress.
  1834. *
  1835. * Since algorithm is based on heuristics, accessing another VCPU data without
  1836. * locking does not harm. It may result in trying to yield to same VCPU, fail
  1837. * and continue with next VCPU and so on.
  1838. */
  1839. static bool kvm_vcpu_eligible_for_directed_yield(struct kvm_vcpu *vcpu)
  1840. {
  1841. #ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
  1842. bool eligible;
  1843. eligible = !vcpu->spin_loop.in_spin_loop ||
  1844. vcpu->spin_loop.dy_eligible;
  1845. if (vcpu->spin_loop.in_spin_loop)
  1846. kvm_vcpu_set_dy_eligible(vcpu, !vcpu->spin_loop.dy_eligible);
  1847. return eligible;
  1848. #else
  1849. return true;
  1850. #endif
  1851. }
  1852. void kvm_vcpu_on_spin(struct kvm_vcpu *me)
  1853. {
  1854. struct kvm *kvm = me->kvm;
  1855. struct kvm_vcpu *vcpu;
  1856. int last_boosted_vcpu = me->kvm->last_boosted_vcpu;
  1857. int yielded = 0;
  1858. int try = 3;
  1859. int pass;
  1860. int i;
  1861. kvm_vcpu_set_in_spin_loop(me, true);
  1862. /*
  1863. * We boost the priority of a VCPU that is runnable but not
  1864. * currently running, because it got preempted by something
  1865. * else and called schedule in __vcpu_run. Hopefully that
  1866. * VCPU is holding the lock that we need and will release it.
  1867. * We approximate round-robin by starting at the last boosted VCPU.
  1868. */
  1869. for (pass = 0; pass < 2 && !yielded && try; pass++) {
  1870. kvm_for_each_vcpu(i, vcpu, kvm) {
  1871. if (!pass && i <= last_boosted_vcpu) {
  1872. i = last_boosted_vcpu;
  1873. continue;
  1874. } else if (pass && i > last_boosted_vcpu)
  1875. break;
  1876. if (!ACCESS_ONCE(vcpu->preempted))
  1877. continue;
  1878. if (vcpu == me)
  1879. continue;
  1880. if (swait_active(&vcpu->wq) && !kvm_arch_vcpu_runnable(vcpu))
  1881. continue;
  1882. if (!kvm_vcpu_eligible_for_directed_yield(vcpu))
  1883. continue;
  1884. yielded = kvm_vcpu_yield_to(vcpu);
  1885. if (yielded > 0) {
  1886. kvm->last_boosted_vcpu = i;
  1887. break;
  1888. } else if (yielded < 0) {
  1889. try--;
  1890. if (!try)
  1891. break;
  1892. }
  1893. }
  1894. }
  1895. kvm_vcpu_set_in_spin_loop(me, false);
  1896. /* Ensure vcpu is not eligible during next spinloop */
  1897. kvm_vcpu_set_dy_eligible(me, false);
  1898. }
  1899. EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
  1900. static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1901. {
  1902. struct kvm_vcpu *vcpu = vma->vm_file->private_data;
  1903. struct page *page;
  1904. if (vmf->pgoff == 0)
  1905. page = virt_to_page(vcpu->run);
  1906. #ifdef CONFIG_X86
  1907. else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
  1908. page = virt_to_page(vcpu->arch.pio_data);
  1909. #endif
  1910. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  1911. else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
  1912. page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
  1913. #endif
  1914. else
  1915. return kvm_arch_vcpu_fault(vcpu, vmf);
  1916. get_page(page);
  1917. vmf->page = page;
  1918. return 0;
  1919. }
  1920. static const struct vm_operations_struct kvm_vcpu_vm_ops = {
  1921. .fault = kvm_vcpu_fault,
  1922. };
  1923. static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
  1924. {
  1925. vma->vm_ops = &kvm_vcpu_vm_ops;
  1926. return 0;
  1927. }
  1928. static int kvm_vcpu_release(struct inode *inode, struct file *filp)
  1929. {
  1930. struct kvm_vcpu *vcpu = filp->private_data;
  1931. kvm_put_kvm(vcpu->kvm);
  1932. return 0;
  1933. }
  1934. static struct file_operations kvm_vcpu_fops = {
  1935. .release = kvm_vcpu_release,
  1936. .unlocked_ioctl = kvm_vcpu_ioctl,
  1937. #ifdef CONFIG_KVM_COMPAT
  1938. .compat_ioctl = kvm_vcpu_compat_ioctl,
  1939. #endif
  1940. .mmap = kvm_vcpu_mmap,
  1941. .llseek = noop_llseek,
  1942. };
  1943. /*
  1944. * Allocates an inode for the vcpu.
  1945. */
  1946. static int create_vcpu_fd(struct kvm_vcpu *vcpu)
  1947. {
  1948. return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR | O_CLOEXEC);
  1949. }
  1950. /*
  1951. * Creates some virtual cpus. Good luck creating more than one.
  1952. */
  1953. static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
  1954. {
  1955. int r;
  1956. struct kvm_vcpu *vcpu;
  1957. if (id >= KVM_MAX_VCPU_ID)
  1958. return -EINVAL;
  1959. vcpu = kvm_arch_vcpu_create(kvm, id);
  1960. if (IS_ERR(vcpu))
  1961. return PTR_ERR(vcpu);
  1962. preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
  1963. r = kvm_arch_vcpu_setup(vcpu);
  1964. if (r)
  1965. goto vcpu_destroy;
  1966. mutex_lock(&kvm->lock);
  1967. if (!kvm_vcpu_compatible(vcpu)) {
  1968. r = -EINVAL;
  1969. goto unlock_vcpu_destroy;
  1970. }
  1971. if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
  1972. r = -EINVAL;
  1973. goto unlock_vcpu_destroy;
  1974. }
  1975. if (kvm_get_vcpu_by_id(kvm, id)) {
  1976. r = -EEXIST;
  1977. goto unlock_vcpu_destroy;
  1978. }
  1979. BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
  1980. /* Now it's all set up, let userspace reach it */
  1981. kvm_get_kvm(kvm);
  1982. r = create_vcpu_fd(vcpu);
  1983. if (r < 0) {
  1984. kvm_put_kvm(kvm);
  1985. goto unlock_vcpu_destroy;
  1986. }
  1987. kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
  1988. /*
  1989. * Pairs with smp_rmb() in kvm_get_vcpu. Write kvm->vcpus
  1990. * before kvm->online_vcpu's incremented value.
  1991. */
  1992. smp_wmb();
  1993. atomic_inc(&kvm->online_vcpus);
  1994. mutex_unlock(&kvm->lock);
  1995. kvm_arch_vcpu_postcreate(vcpu);
  1996. return r;
  1997. unlock_vcpu_destroy:
  1998. mutex_unlock(&kvm->lock);
  1999. vcpu_destroy:
  2000. kvm_arch_vcpu_destroy(vcpu);
  2001. return r;
  2002. }
  2003. static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
  2004. {
  2005. if (sigset) {
  2006. sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
  2007. vcpu->sigset_active = 1;
  2008. vcpu->sigset = *sigset;
  2009. } else
  2010. vcpu->sigset_active = 0;
  2011. return 0;
  2012. }
  2013. static long kvm_vcpu_ioctl(struct file *filp,
  2014. unsigned int ioctl, unsigned long arg)
  2015. {
  2016. struct kvm_vcpu *vcpu = filp->private_data;
  2017. void __user *argp = (void __user *)arg;
  2018. int r;
  2019. struct kvm_fpu *fpu = NULL;
  2020. struct kvm_sregs *kvm_sregs = NULL;
  2021. if (vcpu->kvm->mm != current->mm)
  2022. return -EIO;
  2023. if (unlikely(_IOC_TYPE(ioctl) != KVMIO))
  2024. return -EINVAL;
  2025. #if defined(CONFIG_S390) || defined(CONFIG_PPC) || defined(CONFIG_MIPS)
  2026. /*
  2027. * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
  2028. * so vcpu_load() would break it.
  2029. */
  2030. if (ioctl == KVM_S390_INTERRUPT || ioctl == KVM_S390_IRQ || ioctl == KVM_INTERRUPT)
  2031. return kvm_arch_vcpu_ioctl(filp, ioctl, arg);
  2032. #endif
  2033. r = vcpu_load(vcpu);
  2034. if (r)
  2035. return r;
  2036. switch (ioctl) {
  2037. case KVM_RUN:
  2038. r = -EINVAL;
  2039. if (arg)
  2040. goto out;
  2041. if (unlikely(vcpu->pid != current->pids[PIDTYPE_PID].pid)) {
  2042. /* The thread running this VCPU changed. */
  2043. struct pid *oldpid = vcpu->pid;
  2044. struct pid *newpid = get_task_pid(current, PIDTYPE_PID);
  2045. rcu_assign_pointer(vcpu->pid, newpid);
  2046. if (oldpid)
  2047. synchronize_rcu();
  2048. put_pid(oldpid);
  2049. }
  2050. r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
  2051. trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
  2052. break;
  2053. case KVM_GET_REGS: {
  2054. struct kvm_regs *kvm_regs;
  2055. r = -ENOMEM;
  2056. kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
  2057. if (!kvm_regs)
  2058. goto out;
  2059. r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
  2060. if (r)
  2061. goto out_free1;
  2062. r = -EFAULT;
  2063. if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
  2064. goto out_free1;
  2065. r = 0;
  2066. out_free1:
  2067. kfree(kvm_regs);
  2068. break;
  2069. }
  2070. case KVM_SET_REGS: {
  2071. struct kvm_regs *kvm_regs;
  2072. r = -ENOMEM;
  2073. kvm_regs = memdup_user(argp, sizeof(*kvm_regs));
  2074. if (IS_ERR(kvm_regs)) {
  2075. r = PTR_ERR(kvm_regs);
  2076. goto out;
  2077. }
  2078. r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
  2079. kfree(kvm_regs);
  2080. break;
  2081. }
  2082. case KVM_GET_SREGS: {
  2083. kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
  2084. r = -ENOMEM;
  2085. if (!kvm_sregs)
  2086. goto out;
  2087. r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
  2088. if (r)
  2089. goto out;
  2090. r = -EFAULT;
  2091. if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
  2092. goto out;
  2093. r = 0;
  2094. break;
  2095. }
  2096. case KVM_SET_SREGS: {
  2097. kvm_sregs = memdup_user(argp, sizeof(*kvm_sregs));
  2098. if (IS_ERR(kvm_sregs)) {
  2099. r = PTR_ERR(kvm_sregs);
  2100. kvm_sregs = NULL;
  2101. goto out;
  2102. }
  2103. r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
  2104. break;
  2105. }
  2106. case KVM_GET_MP_STATE: {
  2107. struct kvm_mp_state mp_state;
  2108. r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
  2109. if (r)
  2110. goto out;
  2111. r = -EFAULT;
  2112. if (copy_to_user(argp, &mp_state, sizeof(mp_state)))
  2113. goto out;
  2114. r = 0;
  2115. break;
  2116. }
  2117. case KVM_SET_MP_STATE: {
  2118. struct kvm_mp_state mp_state;
  2119. r = -EFAULT;
  2120. if (copy_from_user(&mp_state, argp, sizeof(mp_state)))
  2121. goto out;
  2122. r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
  2123. break;
  2124. }
  2125. case KVM_TRANSLATE: {
  2126. struct kvm_translation tr;
  2127. r = -EFAULT;
  2128. if (copy_from_user(&tr, argp, sizeof(tr)))
  2129. goto out;
  2130. r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
  2131. if (r)
  2132. goto out;
  2133. r = -EFAULT;
  2134. if (copy_to_user(argp, &tr, sizeof(tr)))
  2135. goto out;
  2136. r = 0;
  2137. break;
  2138. }
  2139. case KVM_SET_GUEST_DEBUG: {
  2140. struct kvm_guest_debug dbg;
  2141. r = -EFAULT;
  2142. if (copy_from_user(&dbg, argp, sizeof(dbg)))
  2143. goto out;
  2144. r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
  2145. break;
  2146. }
  2147. case KVM_SET_SIGNAL_MASK: {
  2148. struct kvm_signal_mask __user *sigmask_arg = argp;
  2149. struct kvm_signal_mask kvm_sigmask;
  2150. sigset_t sigset, *p;
  2151. p = NULL;
  2152. if (argp) {
  2153. r = -EFAULT;
  2154. if (copy_from_user(&kvm_sigmask, argp,
  2155. sizeof(kvm_sigmask)))
  2156. goto out;
  2157. r = -EINVAL;
  2158. if (kvm_sigmask.len != sizeof(sigset))
  2159. goto out;
  2160. r = -EFAULT;
  2161. if (copy_from_user(&sigset, sigmask_arg->sigset,
  2162. sizeof(sigset)))
  2163. goto out;
  2164. p = &sigset;
  2165. }
  2166. r = kvm_vcpu_ioctl_set_sigmask(vcpu, p);
  2167. break;
  2168. }
  2169. case KVM_GET_FPU: {
  2170. fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
  2171. r = -ENOMEM;
  2172. if (!fpu)
  2173. goto out;
  2174. r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
  2175. if (r)
  2176. goto out;
  2177. r = -EFAULT;
  2178. if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
  2179. goto out;
  2180. r = 0;
  2181. break;
  2182. }
  2183. case KVM_SET_FPU: {
  2184. fpu = memdup_user(argp, sizeof(*fpu));
  2185. if (IS_ERR(fpu)) {
  2186. r = PTR_ERR(fpu);
  2187. fpu = NULL;
  2188. goto out;
  2189. }
  2190. r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
  2191. break;
  2192. }
  2193. default:
  2194. r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
  2195. }
  2196. out:
  2197. vcpu_put(vcpu);
  2198. kfree(fpu);
  2199. kfree(kvm_sregs);
  2200. return r;
  2201. }
  2202. #ifdef CONFIG_KVM_COMPAT
  2203. static long kvm_vcpu_compat_ioctl(struct file *filp,
  2204. unsigned int ioctl, unsigned long arg)
  2205. {
  2206. struct kvm_vcpu *vcpu = filp->private_data;
  2207. void __user *argp = compat_ptr(arg);
  2208. int r;
  2209. if (vcpu->kvm->mm != current->mm)
  2210. return -EIO;
  2211. switch (ioctl) {
  2212. case KVM_SET_SIGNAL_MASK: {
  2213. struct kvm_signal_mask __user *sigmask_arg = argp;
  2214. struct kvm_signal_mask kvm_sigmask;
  2215. compat_sigset_t csigset;
  2216. sigset_t sigset;
  2217. if (argp) {
  2218. r = -EFAULT;
  2219. if (copy_from_user(&kvm_sigmask, argp,
  2220. sizeof(kvm_sigmask)))
  2221. goto out;
  2222. r = -EINVAL;
  2223. if (kvm_sigmask.len != sizeof(csigset))
  2224. goto out;
  2225. r = -EFAULT;
  2226. if (copy_from_user(&csigset, sigmask_arg->sigset,
  2227. sizeof(csigset)))
  2228. goto out;
  2229. sigset_from_compat(&sigset, &csigset);
  2230. r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
  2231. } else
  2232. r = kvm_vcpu_ioctl_set_sigmask(vcpu, NULL);
  2233. break;
  2234. }
  2235. default:
  2236. r = kvm_vcpu_ioctl(filp, ioctl, arg);
  2237. }
  2238. out:
  2239. return r;
  2240. }
  2241. #endif
  2242. static int kvm_device_ioctl_attr(struct kvm_device *dev,
  2243. int (*accessor)(struct kvm_device *dev,
  2244. struct kvm_device_attr *attr),
  2245. unsigned long arg)
  2246. {
  2247. struct kvm_device_attr attr;
  2248. if (!accessor)
  2249. return -EPERM;
  2250. if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
  2251. return -EFAULT;
  2252. return accessor(dev, &attr);
  2253. }
  2254. static long kvm_device_ioctl(struct file *filp, unsigned int ioctl,
  2255. unsigned long arg)
  2256. {
  2257. struct kvm_device *dev = filp->private_data;
  2258. switch (ioctl) {
  2259. case KVM_SET_DEVICE_ATTR:
  2260. return kvm_device_ioctl_attr(dev, dev->ops->set_attr, arg);
  2261. case KVM_GET_DEVICE_ATTR:
  2262. return kvm_device_ioctl_attr(dev, dev->ops->get_attr, arg);
  2263. case KVM_HAS_DEVICE_ATTR:
  2264. return kvm_device_ioctl_attr(dev, dev->ops->has_attr, arg);
  2265. default:
  2266. if (dev->ops->ioctl)
  2267. return dev->ops->ioctl(dev, ioctl, arg);
  2268. return -ENOTTY;
  2269. }
  2270. }
  2271. static int kvm_device_release(struct inode *inode, struct file *filp)
  2272. {
  2273. struct kvm_device *dev = filp->private_data;
  2274. struct kvm *kvm = dev->kvm;
  2275. kvm_put_kvm(kvm);
  2276. return 0;
  2277. }
  2278. static const struct file_operations kvm_device_fops = {
  2279. .unlocked_ioctl = kvm_device_ioctl,
  2280. #ifdef CONFIG_KVM_COMPAT
  2281. .compat_ioctl = kvm_device_ioctl,
  2282. #endif
  2283. .release = kvm_device_release,
  2284. };
  2285. struct kvm_device *kvm_device_from_filp(struct file *filp)
  2286. {
  2287. if (filp->f_op != &kvm_device_fops)
  2288. return NULL;
  2289. return filp->private_data;
  2290. }
  2291. static struct kvm_device_ops *kvm_device_ops_table[KVM_DEV_TYPE_MAX] = {
  2292. #ifdef CONFIG_KVM_MPIC
  2293. [KVM_DEV_TYPE_FSL_MPIC_20] = &kvm_mpic_ops,
  2294. [KVM_DEV_TYPE_FSL_MPIC_42] = &kvm_mpic_ops,
  2295. #endif
  2296. #ifdef CONFIG_KVM_XICS
  2297. [KVM_DEV_TYPE_XICS] = &kvm_xics_ops,
  2298. #endif
  2299. };
  2300. int kvm_register_device_ops(struct kvm_device_ops *ops, u32 type)
  2301. {
  2302. if (type >= ARRAY_SIZE(kvm_device_ops_table))
  2303. return -ENOSPC;
  2304. if (kvm_device_ops_table[type] != NULL)
  2305. return -EEXIST;
  2306. kvm_device_ops_table[type] = ops;
  2307. return 0;
  2308. }
  2309. void kvm_unregister_device_ops(u32 type)
  2310. {
  2311. if (kvm_device_ops_table[type] != NULL)
  2312. kvm_device_ops_table[type] = NULL;
  2313. }
  2314. static int kvm_ioctl_create_device(struct kvm *kvm,
  2315. struct kvm_create_device *cd)
  2316. {
  2317. struct kvm_device_ops *ops = NULL;
  2318. struct kvm_device *dev;
  2319. bool test = cd->flags & KVM_CREATE_DEVICE_TEST;
  2320. int ret;
  2321. if (cd->type >= ARRAY_SIZE(kvm_device_ops_table))
  2322. return -ENODEV;
  2323. ops = kvm_device_ops_table[cd->type];
  2324. if (ops == NULL)
  2325. return -ENODEV;
  2326. if (test)
  2327. return 0;
  2328. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  2329. if (!dev)
  2330. return -ENOMEM;
  2331. dev->ops = ops;
  2332. dev->kvm = kvm;
  2333. ret = ops->create(dev, cd->type);
  2334. if (ret < 0) {
  2335. kfree(dev);
  2336. return ret;
  2337. }
  2338. ret = anon_inode_getfd(ops->name, &kvm_device_fops, dev, O_RDWR | O_CLOEXEC);
  2339. if (ret < 0) {
  2340. ops->destroy(dev);
  2341. return ret;
  2342. }
  2343. list_add(&dev->vm_node, &kvm->devices);
  2344. kvm_get_kvm(kvm);
  2345. cd->fd = ret;
  2346. return 0;
  2347. }
  2348. static long kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg)
  2349. {
  2350. switch (arg) {
  2351. case KVM_CAP_USER_MEMORY:
  2352. case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
  2353. case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
  2354. case KVM_CAP_INTERNAL_ERROR_DATA:
  2355. #ifdef CONFIG_HAVE_KVM_MSI
  2356. case KVM_CAP_SIGNAL_MSI:
  2357. #endif
  2358. #ifdef CONFIG_HAVE_KVM_IRQFD
  2359. case KVM_CAP_IRQFD:
  2360. case KVM_CAP_IRQFD_RESAMPLE:
  2361. #endif
  2362. case KVM_CAP_IOEVENTFD_ANY_LENGTH:
  2363. case KVM_CAP_CHECK_EXTENSION_VM:
  2364. return 1;
  2365. #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING
  2366. case KVM_CAP_IRQ_ROUTING:
  2367. return KVM_MAX_IRQ_ROUTES;
  2368. #endif
  2369. #if KVM_ADDRESS_SPACE_NUM > 1
  2370. case KVM_CAP_MULTI_ADDRESS_SPACE:
  2371. return KVM_ADDRESS_SPACE_NUM;
  2372. #endif
  2373. case KVM_CAP_MAX_VCPU_ID:
  2374. return KVM_MAX_VCPU_ID;
  2375. default:
  2376. break;
  2377. }
  2378. return kvm_vm_ioctl_check_extension(kvm, arg);
  2379. }
  2380. static long kvm_vm_ioctl(struct file *filp,
  2381. unsigned int ioctl, unsigned long arg)
  2382. {
  2383. struct kvm *kvm = filp->private_data;
  2384. void __user *argp = (void __user *)arg;
  2385. int r;
  2386. if (kvm->mm != current->mm)
  2387. return -EIO;
  2388. switch (ioctl) {
  2389. case KVM_CREATE_VCPU:
  2390. r = kvm_vm_ioctl_create_vcpu(kvm, arg);
  2391. break;
  2392. case KVM_SET_USER_MEMORY_REGION: {
  2393. struct kvm_userspace_memory_region kvm_userspace_mem;
  2394. r = -EFAULT;
  2395. if (copy_from_user(&kvm_userspace_mem, argp,
  2396. sizeof(kvm_userspace_mem)))
  2397. goto out;
  2398. r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem);
  2399. break;
  2400. }
  2401. case KVM_GET_DIRTY_LOG: {
  2402. struct kvm_dirty_log log;
  2403. r = -EFAULT;
  2404. if (copy_from_user(&log, argp, sizeof(log)))
  2405. goto out;
  2406. r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
  2407. break;
  2408. }
  2409. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  2410. case KVM_REGISTER_COALESCED_MMIO: {
  2411. struct kvm_coalesced_mmio_zone zone;
  2412. r = -EFAULT;
  2413. if (copy_from_user(&zone, argp, sizeof(zone)))
  2414. goto out;
  2415. r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
  2416. break;
  2417. }
  2418. case KVM_UNREGISTER_COALESCED_MMIO: {
  2419. struct kvm_coalesced_mmio_zone zone;
  2420. r = -EFAULT;
  2421. if (copy_from_user(&zone, argp, sizeof(zone)))
  2422. goto out;
  2423. r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
  2424. break;
  2425. }
  2426. #endif
  2427. case KVM_IRQFD: {
  2428. struct kvm_irqfd data;
  2429. r = -EFAULT;
  2430. if (copy_from_user(&data, argp, sizeof(data)))
  2431. goto out;
  2432. r = kvm_irqfd(kvm, &data);
  2433. break;
  2434. }
  2435. case KVM_IOEVENTFD: {
  2436. struct kvm_ioeventfd data;
  2437. r = -EFAULT;
  2438. if (copy_from_user(&data, argp, sizeof(data)))
  2439. goto out;
  2440. r = kvm_ioeventfd(kvm, &data);
  2441. break;
  2442. }
  2443. #ifdef CONFIG_HAVE_KVM_MSI
  2444. case KVM_SIGNAL_MSI: {
  2445. struct kvm_msi msi;
  2446. r = -EFAULT;
  2447. if (copy_from_user(&msi, argp, sizeof(msi)))
  2448. goto out;
  2449. r = kvm_send_userspace_msi(kvm, &msi);
  2450. break;
  2451. }
  2452. #endif
  2453. #ifdef __KVM_HAVE_IRQ_LINE
  2454. case KVM_IRQ_LINE_STATUS:
  2455. case KVM_IRQ_LINE: {
  2456. struct kvm_irq_level irq_event;
  2457. r = -EFAULT;
  2458. if (copy_from_user(&irq_event, argp, sizeof(irq_event)))
  2459. goto out;
  2460. r = kvm_vm_ioctl_irq_line(kvm, &irq_event,
  2461. ioctl == KVM_IRQ_LINE_STATUS);
  2462. if (r)
  2463. goto out;
  2464. r = -EFAULT;
  2465. if (ioctl == KVM_IRQ_LINE_STATUS) {
  2466. if (copy_to_user(argp, &irq_event, sizeof(irq_event)))
  2467. goto out;
  2468. }
  2469. r = 0;
  2470. break;
  2471. }
  2472. #endif
  2473. #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING
  2474. case KVM_SET_GSI_ROUTING: {
  2475. struct kvm_irq_routing routing;
  2476. struct kvm_irq_routing __user *urouting;
  2477. struct kvm_irq_routing_entry *entries = NULL;
  2478. r = -EFAULT;
  2479. if (copy_from_user(&routing, argp, sizeof(routing)))
  2480. goto out;
  2481. r = -EINVAL;
  2482. if (routing.nr > KVM_MAX_IRQ_ROUTES)
  2483. goto out;
  2484. if (routing.flags)
  2485. goto out;
  2486. if (routing.nr) {
  2487. r = -ENOMEM;
  2488. entries = vmalloc(routing.nr * sizeof(*entries));
  2489. if (!entries)
  2490. goto out;
  2491. r = -EFAULT;
  2492. urouting = argp;
  2493. if (copy_from_user(entries, urouting->entries,
  2494. routing.nr * sizeof(*entries)))
  2495. goto out_free_irq_routing;
  2496. }
  2497. r = kvm_set_irq_routing(kvm, entries, routing.nr,
  2498. routing.flags);
  2499. out_free_irq_routing:
  2500. vfree(entries);
  2501. break;
  2502. }
  2503. #endif /* CONFIG_HAVE_KVM_IRQ_ROUTING */
  2504. case KVM_CREATE_DEVICE: {
  2505. struct kvm_create_device cd;
  2506. r = -EFAULT;
  2507. if (copy_from_user(&cd, argp, sizeof(cd)))
  2508. goto out;
  2509. r = kvm_ioctl_create_device(kvm, &cd);
  2510. if (r)
  2511. goto out;
  2512. r = -EFAULT;
  2513. if (copy_to_user(argp, &cd, sizeof(cd)))
  2514. goto out;
  2515. r = 0;
  2516. break;
  2517. }
  2518. case KVM_CHECK_EXTENSION:
  2519. r = kvm_vm_ioctl_check_extension_generic(kvm, arg);
  2520. break;
  2521. default:
  2522. r = kvm_arch_vm_ioctl(filp, ioctl, arg);
  2523. }
  2524. out:
  2525. return r;
  2526. }
  2527. #ifdef CONFIG_KVM_COMPAT
  2528. struct compat_kvm_dirty_log {
  2529. __u32 slot;
  2530. __u32 padding1;
  2531. union {
  2532. compat_uptr_t dirty_bitmap; /* one bit per page */
  2533. __u64 padding2;
  2534. };
  2535. };
  2536. static long kvm_vm_compat_ioctl(struct file *filp,
  2537. unsigned int ioctl, unsigned long arg)
  2538. {
  2539. struct kvm *kvm = filp->private_data;
  2540. int r;
  2541. if (kvm->mm != current->mm)
  2542. return -EIO;
  2543. switch (ioctl) {
  2544. case KVM_GET_DIRTY_LOG: {
  2545. struct compat_kvm_dirty_log compat_log;
  2546. struct kvm_dirty_log log;
  2547. r = -EFAULT;
  2548. if (copy_from_user(&compat_log, (void __user *)arg,
  2549. sizeof(compat_log)))
  2550. goto out;
  2551. log.slot = compat_log.slot;
  2552. log.padding1 = compat_log.padding1;
  2553. log.padding2 = compat_log.padding2;
  2554. log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
  2555. r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
  2556. break;
  2557. }
  2558. default:
  2559. r = kvm_vm_ioctl(filp, ioctl, arg);
  2560. }
  2561. out:
  2562. return r;
  2563. }
  2564. #endif
  2565. static struct file_operations kvm_vm_fops = {
  2566. .release = kvm_vm_release,
  2567. .unlocked_ioctl = kvm_vm_ioctl,
  2568. #ifdef CONFIG_KVM_COMPAT
  2569. .compat_ioctl = kvm_vm_compat_ioctl,
  2570. #endif
  2571. .llseek = noop_llseek,
  2572. };
  2573. static int kvm_dev_ioctl_create_vm(unsigned long type)
  2574. {
  2575. int r;
  2576. struct kvm *kvm;
  2577. struct file *file;
  2578. kvm = kvm_create_vm(type);
  2579. if (IS_ERR(kvm))
  2580. return PTR_ERR(kvm);
  2581. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  2582. r = kvm_coalesced_mmio_init(kvm);
  2583. if (r < 0) {
  2584. kvm_put_kvm(kvm);
  2585. return r;
  2586. }
  2587. #endif
  2588. r = get_unused_fd_flags(O_CLOEXEC);
  2589. if (r < 0) {
  2590. kvm_put_kvm(kvm);
  2591. return r;
  2592. }
  2593. file = anon_inode_getfile("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
  2594. if (IS_ERR(file)) {
  2595. put_unused_fd(r);
  2596. kvm_put_kvm(kvm);
  2597. return PTR_ERR(file);
  2598. }
  2599. if (kvm_create_vm_debugfs(kvm, r) < 0) {
  2600. put_unused_fd(r);
  2601. fput(file);
  2602. return -ENOMEM;
  2603. }
  2604. fd_install(r, file);
  2605. return r;
  2606. }
  2607. static long kvm_dev_ioctl(struct file *filp,
  2608. unsigned int ioctl, unsigned long arg)
  2609. {
  2610. long r = -EINVAL;
  2611. switch (ioctl) {
  2612. case KVM_GET_API_VERSION:
  2613. if (arg)
  2614. goto out;
  2615. r = KVM_API_VERSION;
  2616. break;
  2617. case KVM_CREATE_VM:
  2618. r = kvm_dev_ioctl_create_vm(arg);
  2619. break;
  2620. case KVM_CHECK_EXTENSION:
  2621. r = kvm_vm_ioctl_check_extension_generic(NULL, arg);
  2622. break;
  2623. case KVM_GET_VCPU_MMAP_SIZE:
  2624. if (arg)
  2625. goto out;
  2626. r = PAGE_SIZE; /* struct kvm_run */
  2627. #ifdef CONFIG_X86
  2628. r += PAGE_SIZE; /* pio data page */
  2629. #endif
  2630. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  2631. r += PAGE_SIZE; /* coalesced mmio ring page */
  2632. #endif
  2633. break;
  2634. case KVM_TRACE_ENABLE:
  2635. case KVM_TRACE_PAUSE:
  2636. case KVM_TRACE_DISABLE:
  2637. r = -EOPNOTSUPP;
  2638. break;
  2639. default:
  2640. return kvm_arch_dev_ioctl(filp, ioctl, arg);
  2641. }
  2642. out:
  2643. return r;
  2644. }
  2645. static struct file_operations kvm_chardev_ops = {
  2646. .unlocked_ioctl = kvm_dev_ioctl,
  2647. .compat_ioctl = kvm_dev_ioctl,
  2648. .llseek = noop_llseek,
  2649. };
  2650. static struct miscdevice kvm_dev = {
  2651. KVM_MINOR,
  2652. "kvm",
  2653. &kvm_chardev_ops,
  2654. };
  2655. static void hardware_enable_nolock(void *junk)
  2656. {
  2657. int cpu = raw_smp_processor_id();
  2658. int r;
  2659. if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
  2660. return;
  2661. cpumask_set_cpu(cpu, cpus_hardware_enabled);
  2662. r = kvm_arch_hardware_enable();
  2663. if (r) {
  2664. cpumask_clear_cpu(cpu, cpus_hardware_enabled);
  2665. atomic_inc(&hardware_enable_failed);
  2666. pr_info("kvm: enabling virtualization on CPU%d failed\n", cpu);
  2667. }
  2668. }
  2669. static void hardware_enable(void)
  2670. {
  2671. raw_spin_lock(&kvm_count_lock);
  2672. if (kvm_usage_count)
  2673. hardware_enable_nolock(NULL);
  2674. raw_spin_unlock(&kvm_count_lock);
  2675. }
  2676. static void hardware_disable_nolock(void *junk)
  2677. {
  2678. int cpu = raw_smp_processor_id();
  2679. if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
  2680. return;
  2681. cpumask_clear_cpu(cpu, cpus_hardware_enabled);
  2682. kvm_arch_hardware_disable();
  2683. }
  2684. static void hardware_disable(void)
  2685. {
  2686. raw_spin_lock(&kvm_count_lock);
  2687. if (kvm_usage_count)
  2688. hardware_disable_nolock(NULL);
  2689. raw_spin_unlock(&kvm_count_lock);
  2690. }
  2691. static void hardware_disable_all_nolock(void)
  2692. {
  2693. BUG_ON(!kvm_usage_count);
  2694. kvm_usage_count--;
  2695. if (!kvm_usage_count)
  2696. on_each_cpu(hardware_disable_nolock, NULL, 1);
  2697. }
  2698. static void hardware_disable_all(void)
  2699. {
  2700. raw_spin_lock(&kvm_count_lock);
  2701. hardware_disable_all_nolock();
  2702. raw_spin_unlock(&kvm_count_lock);
  2703. }
  2704. static int hardware_enable_all(void)
  2705. {
  2706. int r = 0;
  2707. raw_spin_lock(&kvm_count_lock);
  2708. kvm_usage_count++;
  2709. if (kvm_usage_count == 1) {
  2710. atomic_set(&hardware_enable_failed, 0);
  2711. on_each_cpu(hardware_enable_nolock, NULL, 1);
  2712. if (atomic_read(&hardware_enable_failed)) {
  2713. hardware_disable_all_nolock();
  2714. r = -EBUSY;
  2715. }
  2716. }
  2717. raw_spin_unlock(&kvm_count_lock);
  2718. return r;
  2719. }
  2720. static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
  2721. void *v)
  2722. {
  2723. val &= ~CPU_TASKS_FROZEN;
  2724. switch (val) {
  2725. case CPU_DYING:
  2726. hardware_disable();
  2727. break;
  2728. case CPU_STARTING:
  2729. hardware_enable();
  2730. break;
  2731. }
  2732. return NOTIFY_OK;
  2733. }
  2734. static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
  2735. void *v)
  2736. {
  2737. /*
  2738. * Some (well, at least mine) BIOSes hang on reboot if
  2739. * in vmx root mode.
  2740. *
  2741. * And Intel TXT required VMX off for all cpu when system shutdown.
  2742. */
  2743. pr_info("kvm: exiting hardware virtualization\n");
  2744. kvm_rebooting = true;
  2745. on_each_cpu(hardware_disable_nolock, NULL, 1);
  2746. return NOTIFY_OK;
  2747. }
  2748. static struct notifier_block kvm_reboot_notifier = {
  2749. .notifier_call = kvm_reboot,
  2750. .priority = 0,
  2751. };
  2752. static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
  2753. {
  2754. int i;
  2755. for (i = 0; i < bus->dev_count; i++) {
  2756. struct kvm_io_device *pos = bus->range[i].dev;
  2757. kvm_iodevice_destructor(pos);
  2758. }
  2759. kfree(bus);
  2760. }
  2761. static inline int kvm_io_bus_cmp(const struct kvm_io_range *r1,
  2762. const struct kvm_io_range *r2)
  2763. {
  2764. gpa_t addr1 = r1->addr;
  2765. gpa_t addr2 = r2->addr;
  2766. if (addr1 < addr2)
  2767. return -1;
  2768. /* If r2->len == 0, match the exact address. If r2->len != 0,
  2769. * accept any overlapping write. Any order is acceptable for
  2770. * overlapping ranges, because kvm_io_bus_get_first_dev ensures
  2771. * we process all of them.
  2772. */
  2773. if (r2->len) {
  2774. addr1 += r1->len;
  2775. addr2 += r2->len;
  2776. }
  2777. if (addr1 > addr2)
  2778. return 1;
  2779. return 0;
  2780. }
  2781. static int kvm_io_bus_sort_cmp(const void *p1, const void *p2)
  2782. {
  2783. return kvm_io_bus_cmp(p1, p2);
  2784. }
  2785. static int kvm_io_bus_insert_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev,
  2786. gpa_t addr, int len)
  2787. {
  2788. bus->range[bus->dev_count++] = (struct kvm_io_range) {
  2789. .addr = addr,
  2790. .len = len,
  2791. .dev = dev,
  2792. };
  2793. sort(bus->range, bus->dev_count, sizeof(struct kvm_io_range),
  2794. kvm_io_bus_sort_cmp, NULL);
  2795. return 0;
  2796. }
  2797. static int kvm_io_bus_get_first_dev(struct kvm_io_bus *bus,
  2798. gpa_t addr, int len)
  2799. {
  2800. struct kvm_io_range *range, key;
  2801. int off;
  2802. key = (struct kvm_io_range) {
  2803. .addr = addr,
  2804. .len = len,
  2805. };
  2806. range = bsearch(&key, bus->range, bus->dev_count,
  2807. sizeof(struct kvm_io_range), kvm_io_bus_sort_cmp);
  2808. if (range == NULL)
  2809. return -ENOENT;
  2810. off = range - bus->range;
  2811. while (off > 0 && kvm_io_bus_cmp(&key, &bus->range[off-1]) == 0)
  2812. off--;
  2813. return off;
  2814. }
  2815. static int __kvm_io_bus_write(struct kvm_vcpu *vcpu, struct kvm_io_bus *bus,
  2816. struct kvm_io_range *range, const void *val)
  2817. {
  2818. int idx;
  2819. idx = kvm_io_bus_get_first_dev(bus, range->addr, range->len);
  2820. if (idx < 0)
  2821. return -EOPNOTSUPP;
  2822. while (idx < bus->dev_count &&
  2823. kvm_io_bus_cmp(range, &bus->range[idx]) == 0) {
  2824. if (!kvm_iodevice_write(vcpu, bus->range[idx].dev, range->addr,
  2825. range->len, val))
  2826. return idx;
  2827. idx++;
  2828. }
  2829. return -EOPNOTSUPP;
  2830. }
  2831. /* kvm_io_bus_write - called under kvm->slots_lock */
  2832. int kvm_io_bus_write(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr,
  2833. int len, const void *val)
  2834. {
  2835. struct kvm_io_bus *bus;
  2836. struct kvm_io_range range;
  2837. int r;
  2838. range = (struct kvm_io_range) {
  2839. .addr = addr,
  2840. .len = len,
  2841. };
  2842. bus = srcu_dereference(vcpu->kvm->buses[bus_idx], &vcpu->kvm->srcu);
  2843. r = __kvm_io_bus_write(vcpu, bus, &range, val);
  2844. return r < 0 ? r : 0;
  2845. }
  2846. /* kvm_io_bus_write_cookie - called under kvm->slots_lock */
  2847. int kvm_io_bus_write_cookie(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx,
  2848. gpa_t addr, int len, const void *val, long cookie)
  2849. {
  2850. struct kvm_io_bus *bus;
  2851. struct kvm_io_range range;
  2852. range = (struct kvm_io_range) {
  2853. .addr = addr,
  2854. .len = len,
  2855. };
  2856. bus = srcu_dereference(vcpu->kvm->buses[bus_idx], &vcpu->kvm->srcu);
  2857. /* First try the device referenced by cookie. */
  2858. if ((cookie >= 0) && (cookie < bus->dev_count) &&
  2859. (kvm_io_bus_cmp(&range, &bus->range[cookie]) == 0))
  2860. if (!kvm_iodevice_write(vcpu, bus->range[cookie].dev, addr, len,
  2861. val))
  2862. return cookie;
  2863. /*
  2864. * cookie contained garbage; fall back to search and return the
  2865. * correct cookie value.
  2866. */
  2867. return __kvm_io_bus_write(vcpu, bus, &range, val);
  2868. }
  2869. static int __kvm_io_bus_read(struct kvm_vcpu *vcpu, struct kvm_io_bus *bus,
  2870. struct kvm_io_range *range, void *val)
  2871. {
  2872. int idx;
  2873. idx = kvm_io_bus_get_first_dev(bus, range->addr, range->len);
  2874. if (idx < 0)
  2875. return -EOPNOTSUPP;
  2876. while (idx < bus->dev_count &&
  2877. kvm_io_bus_cmp(range, &bus->range[idx]) == 0) {
  2878. if (!kvm_iodevice_read(vcpu, bus->range[idx].dev, range->addr,
  2879. range->len, val))
  2880. return idx;
  2881. idx++;
  2882. }
  2883. return -EOPNOTSUPP;
  2884. }
  2885. EXPORT_SYMBOL_GPL(kvm_io_bus_write);
  2886. /* kvm_io_bus_read - called under kvm->slots_lock */
  2887. int kvm_io_bus_read(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr,
  2888. int len, void *val)
  2889. {
  2890. struct kvm_io_bus *bus;
  2891. struct kvm_io_range range;
  2892. int r;
  2893. range = (struct kvm_io_range) {
  2894. .addr = addr,
  2895. .len = len,
  2896. };
  2897. bus = srcu_dereference(vcpu->kvm->buses[bus_idx], &vcpu->kvm->srcu);
  2898. r = __kvm_io_bus_read(vcpu, bus, &range, val);
  2899. return r < 0 ? r : 0;
  2900. }
  2901. /* Caller must hold slots_lock. */
  2902. int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
  2903. int len, struct kvm_io_device *dev)
  2904. {
  2905. struct kvm_io_bus *new_bus, *bus;
  2906. bus = kvm->buses[bus_idx];
  2907. /* exclude ioeventfd which is limited by maximum fd */
  2908. if (bus->dev_count - bus->ioeventfd_count > NR_IOBUS_DEVS - 1)
  2909. return -ENOSPC;
  2910. new_bus = kmalloc(sizeof(*bus) + ((bus->dev_count + 1) *
  2911. sizeof(struct kvm_io_range)), GFP_KERNEL);
  2912. if (!new_bus)
  2913. return -ENOMEM;
  2914. memcpy(new_bus, bus, sizeof(*bus) + (bus->dev_count *
  2915. sizeof(struct kvm_io_range)));
  2916. kvm_io_bus_insert_dev(new_bus, dev, addr, len);
  2917. rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
  2918. synchronize_srcu_expedited(&kvm->srcu);
  2919. kfree(bus);
  2920. return 0;
  2921. }
  2922. /* Caller must hold slots_lock. */
  2923. int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
  2924. struct kvm_io_device *dev)
  2925. {
  2926. int i, r;
  2927. struct kvm_io_bus *new_bus, *bus;
  2928. bus = kvm->buses[bus_idx];
  2929. r = -ENOENT;
  2930. for (i = 0; i < bus->dev_count; i++)
  2931. if (bus->range[i].dev == dev) {
  2932. r = 0;
  2933. break;
  2934. }
  2935. if (r)
  2936. return r;
  2937. new_bus = kmalloc(sizeof(*bus) + ((bus->dev_count - 1) *
  2938. sizeof(struct kvm_io_range)), GFP_KERNEL);
  2939. if (!new_bus)
  2940. return -ENOMEM;
  2941. memcpy(new_bus, bus, sizeof(*bus) + i * sizeof(struct kvm_io_range));
  2942. new_bus->dev_count--;
  2943. memcpy(new_bus->range + i, bus->range + i + 1,
  2944. (new_bus->dev_count - i) * sizeof(struct kvm_io_range));
  2945. rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
  2946. synchronize_srcu_expedited(&kvm->srcu);
  2947. kfree(bus);
  2948. return r;
  2949. }
  2950. static struct notifier_block kvm_cpu_notifier = {
  2951. .notifier_call = kvm_cpu_hotplug,
  2952. };
  2953. static int kvm_debugfs_open(struct inode *inode, struct file *file,
  2954. int (*get)(void *, u64 *), int (*set)(void *, u64),
  2955. const char *fmt)
  2956. {
  2957. struct kvm_stat_data *stat_data = (struct kvm_stat_data *)
  2958. inode->i_private;
  2959. /* The debugfs files are a reference to the kvm struct which
  2960. * is still valid when kvm_destroy_vm is called.
  2961. * To avoid the race between open and the removal of the debugfs
  2962. * directory we test against the users count.
  2963. */
  2964. if (!atomic_add_unless(&stat_data->kvm->users_count, 1, 0))
  2965. return -ENOENT;
  2966. if (simple_attr_open(inode, file, get, set, fmt)) {
  2967. kvm_put_kvm(stat_data->kvm);
  2968. return -ENOMEM;
  2969. }
  2970. return 0;
  2971. }
  2972. static int kvm_debugfs_release(struct inode *inode, struct file *file)
  2973. {
  2974. struct kvm_stat_data *stat_data = (struct kvm_stat_data *)
  2975. inode->i_private;
  2976. simple_attr_release(inode, file);
  2977. kvm_put_kvm(stat_data->kvm);
  2978. return 0;
  2979. }
  2980. static int vm_stat_get_per_vm(void *data, u64 *val)
  2981. {
  2982. struct kvm_stat_data *stat_data = (struct kvm_stat_data *)data;
  2983. *val = *(u32 *)((void *)stat_data->kvm + stat_data->offset);
  2984. return 0;
  2985. }
  2986. static int vm_stat_get_per_vm_open(struct inode *inode, struct file *file)
  2987. {
  2988. __simple_attr_check_format("%llu\n", 0ull);
  2989. return kvm_debugfs_open(inode, file, vm_stat_get_per_vm,
  2990. NULL, "%llu\n");
  2991. }
  2992. static const struct file_operations vm_stat_get_per_vm_fops = {
  2993. .owner = THIS_MODULE,
  2994. .open = vm_stat_get_per_vm_open,
  2995. .release = kvm_debugfs_release,
  2996. .read = simple_attr_read,
  2997. .write = simple_attr_write,
  2998. .llseek = generic_file_llseek,
  2999. };
  3000. static int vcpu_stat_get_per_vm(void *data, u64 *val)
  3001. {
  3002. int i;
  3003. struct kvm_stat_data *stat_data = (struct kvm_stat_data *)data;
  3004. struct kvm_vcpu *vcpu;
  3005. *val = 0;
  3006. kvm_for_each_vcpu(i, vcpu, stat_data->kvm)
  3007. *val += *(u32 *)((void *)vcpu + stat_data->offset);
  3008. return 0;
  3009. }
  3010. static int vcpu_stat_get_per_vm_open(struct inode *inode, struct file *file)
  3011. {
  3012. __simple_attr_check_format("%llu\n", 0ull);
  3013. return kvm_debugfs_open(inode, file, vcpu_stat_get_per_vm,
  3014. NULL, "%llu\n");
  3015. }
  3016. static const struct file_operations vcpu_stat_get_per_vm_fops = {
  3017. .owner = THIS_MODULE,
  3018. .open = vcpu_stat_get_per_vm_open,
  3019. .release = kvm_debugfs_release,
  3020. .read = simple_attr_read,
  3021. .write = simple_attr_write,
  3022. .llseek = generic_file_llseek,
  3023. };
  3024. static const struct file_operations *stat_fops_per_vm[] = {
  3025. [KVM_STAT_VCPU] = &vcpu_stat_get_per_vm_fops,
  3026. [KVM_STAT_VM] = &vm_stat_get_per_vm_fops,
  3027. };
  3028. static int vm_stat_get(void *_offset, u64 *val)
  3029. {
  3030. unsigned offset = (long)_offset;
  3031. struct kvm *kvm;
  3032. struct kvm_stat_data stat_tmp = {.offset = offset};
  3033. u64 tmp_val;
  3034. *val = 0;
  3035. spin_lock(&kvm_lock);
  3036. list_for_each_entry(kvm, &vm_list, vm_list) {
  3037. stat_tmp.kvm = kvm;
  3038. vm_stat_get_per_vm((void *)&stat_tmp, &tmp_val);
  3039. *val += tmp_val;
  3040. }
  3041. spin_unlock(&kvm_lock);
  3042. return 0;
  3043. }
  3044. DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
  3045. static int vcpu_stat_get(void *_offset, u64 *val)
  3046. {
  3047. unsigned offset = (long)_offset;
  3048. struct kvm *kvm;
  3049. struct kvm_stat_data stat_tmp = {.offset = offset};
  3050. u64 tmp_val;
  3051. *val = 0;
  3052. spin_lock(&kvm_lock);
  3053. list_for_each_entry(kvm, &vm_list, vm_list) {
  3054. stat_tmp.kvm = kvm;
  3055. vcpu_stat_get_per_vm((void *)&stat_tmp, &tmp_val);
  3056. *val += tmp_val;
  3057. }
  3058. spin_unlock(&kvm_lock);
  3059. return 0;
  3060. }
  3061. DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
  3062. static const struct file_operations *stat_fops[] = {
  3063. [KVM_STAT_VCPU] = &vcpu_stat_fops,
  3064. [KVM_STAT_VM] = &vm_stat_fops,
  3065. };
  3066. static int kvm_init_debug(void)
  3067. {
  3068. int r = -EEXIST;
  3069. struct kvm_stats_debugfs_item *p;
  3070. kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
  3071. if (kvm_debugfs_dir == NULL)
  3072. goto out;
  3073. kvm_debugfs_num_entries = 0;
  3074. for (p = debugfs_entries; p->name; ++p, kvm_debugfs_num_entries++) {
  3075. if (!debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
  3076. (void *)(long)p->offset,
  3077. stat_fops[p->kind]))
  3078. goto out_dir;
  3079. }
  3080. return 0;
  3081. out_dir:
  3082. debugfs_remove_recursive(kvm_debugfs_dir);
  3083. out:
  3084. return r;
  3085. }
  3086. static int kvm_suspend(void)
  3087. {
  3088. if (kvm_usage_count)
  3089. hardware_disable_nolock(NULL);
  3090. return 0;
  3091. }
  3092. static void kvm_resume(void)
  3093. {
  3094. if (kvm_usage_count) {
  3095. WARN_ON(raw_spin_is_locked(&kvm_count_lock));
  3096. hardware_enable_nolock(NULL);
  3097. }
  3098. }
  3099. static struct syscore_ops kvm_syscore_ops = {
  3100. .suspend = kvm_suspend,
  3101. .resume = kvm_resume,
  3102. };
  3103. static inline
  3104. struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
  3105. {
  3106. return container_of(pn, struct kvm_vcpu, preempt_notifier);
  3107. }
  3108. static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
  3109. {
  3110. struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
  3111. if (vcpu->preempted)
  3112. vcpu->preempted = false;
  3113. kvm_arch_sched_in(vcpu, cpu);
  3114. kvm_arch_vcpu_load(vcpu, cpu);
  3115. }
  3116. static void kvm_sched_out(struct preempt_notifier *pn,
  3117. struct task_struct *next)
  3118. {
  3119. struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
  3120. if (current->state == TASK_RUNNING)
  3121. vcpu->preempted = true;
  3122. kvm_arch_vcpu_put(vcpu);
  3123. }
  3124. int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
  3125. struct module *module)
  3126. {
  3127. int r;
  3128. int cpu;
  3129. r = kvm_arch_init(opaque);
  3130. if (r)
  3131. goto out_fail;
  3132. /*
  3133. * kvm_arch_init makes sure there's at most one caller
  3134. * for architectures that support multiple implementations,
  3135. * like intel and amd on x86.
  3136. * kvm_arch_init must be called before kvm_irqfd_init to avoid creating
  3137. * conflicts in case kvm is already setup for another implementation.
  3138. */
  3139. r = kvm_irqfd_init();
  3140. if (r)
  3141. goto out_irqfd;
  3142. if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
  3143. r = -ENOMEM;
  3144. goto out_free_0;
  3145. }
  3146. r = kvm_arch_hardware_setup();
  3147. if (r < 0)
  3148. goto out_free_0a;
  3149. for_each_online_cpu(cpu) {
  3150. smp_call_function_single(cpu,
  3151. kvm_arch_check_processor_compat,
  3152. &r, 1);
  3153. if (r < 0)
  3154. goto out_free_1;
  3155. }
  3156. r = register_cpu_notifier(&kvm_cpu_notifier);
  3157. if (r)
  3158. goto out_free_2;
  3159. register_reboot_notifier(&kvm_reboot_notifier);
  3160. /* A kmem cache lets us meet the alignment requirements of fx_save. */
  3161. if (!vcpu_align)
  3162. vcpu_align = __alignof__(struct kvm_vcpu);
  3163. kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
  3164. 0, NULL);
  3165. if (!kvm_vcpu_cache) {
  3166. r = -ENOMEM;
  3167. goto out_free_3;
  3168. }
  3169. r = kvm_async_pf_init();
  3170. if (r)
  3171. goto out_free;
  3172. kvm_chardev_ops.owner = module;
  3173. kvm_vm_fops.owner = module;
  3174. kvm_vcpu_fops.owner = module;
  3175. r = misc_register(&kvm_dev);
  3176. if (r) {
  3177. pr_err("kvm: misc device register failed\n");
  3178. goto out_unreg;
  3179. }
  3180. register_syscore_ops(&kvm_syscore_ops);
  3181. kvm_preempt_ops.sched_in = kvm_sched_in;
  3182. kvm_preempt_ops.sched_out = kvm_sched_out;
  3183. r = kvm_init_debug();
  3184. if (r) {
  3185. pr_err("kvm: create debugfs files failed\n");
  3186. goto out_undebugfs;
  3187. }
  3188. r = kvm_vfio_ops_init();
  3189. WARN_ON(r);
  3190. return 0;
  3191. out_undebugfs:
  3192. unregister_syscore_ops(&kvm_syscore_ops);
  3193. misc_deregister(&kvm_dev);
  3194. out_unreg:
  3195. kvm_async_pf_deinit();
  3196. out_free:
  3197. kmem_cache_destroy(kvm_vcpu_cache);
  3198. out_free_3:
  3199. unregister_reboot_notifier(&kvm_reboot_notifier);
  3200. unregister_cpu_notifier(&kvm_cpu_notifier);
  3201. out_free_2:
  3202. out_free_1:
  3203. kvm_arch_hardware_unsetup();
  3204. out_free_0a:
  3205. free_cpumask_var(cpus_hardware_enabled);
  3206. out_free_0:
  3207. kvm_irqfd_exit();
  3208. out_irqfd:
  3209. kvm_arch_exit();
  3210. out_fail:
  3211. return r;
  3212. }
  3213. EXPORT_SYMBOL_GPL(kvm_init);
  3214. void kvm_exit(void)
  3215. {
  3216. debugfs_remove_recursive(kvm_debugfs_dir);
  3217. misc_deregister(&kvm_dev);
  3218. kmem_cache_destroy(kvm_vcpu_cache);
  3219. kvm_async_pf_deinit();
  3220. unregister_syscore_ops(&kvm_syscore_ops);
  3221. unregister_reboot_notifier(&kvm_reboot_notifier);
  3222. unregister_cpu_notifier(&kvm_cpu_notifier);
  3223. on_each_cpu(hardware_disable_nolock, NULL, 1);
  3224. kvm_arch_hardware_unsetup();
  3225. kvm_arch_exit();
  3226. kvm_irqfd_exit();
  3227. free_cpumask_var(cpus_hardware_enabled);
  3228. kvm_vfio_ops_exit();
  3229. }
  3230. EXPORT_SYMBOL_GPL(kvm_exit);