kvm_main.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629
  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. *
  9. * Authors:
  10. * Avi Kivity <avi@qumranet.com>
  11. * Yaniv Kamay <yaniv@qumranet.com>
  12. *
  13. * This work is licensed under the terms of the GNU GPL, version 2. See
  14. * the COPYING file in the top-level directory.
  15. *
  16. */
  17. #include "kvm.h"
  18. #include "x86_emulate.h"
  19. #include "segment_descriptor.h"
  20. #include "irq.h"
  21. #include <linux/kvm.h>
  22. #include <linux/module.h>
  23. #include <linux/errno.h>
  24. #include <linux/percpu.h>
  25. #include <linux/gfp.h>
  26. #include <linux/mm.h>
  27. #include <linux/miscdevice.h>
  28. #include <linux/vmalloc.h>
  29. #include <linux/reboot.h>
  30. #include <linux/debugfs.h>
  31. #include <linux/highmem.h>
  32. #include <linux/file.h>
  33. #include <linux/sysdev.h>
  34. #include <linux/cpu.h>
  35. #include <linux/sched.h>
  36. #include <linux/cpumask.h>
  37. #include <linux/smp.h>
  38. #include <linux/anon_inodes.h>
  39. #include <linux/profile.h>
  40. #include <asm/processor.h>
  41. #include <asm/msr.h>
  42. #include <asm/io.h>
  43. #include <asm/uaccess.h>
  44. #include <asm/desc.h>
  45. MODULE_AUTHOR("Qumranet");
  46. MODULE_LICENSE("GPL");
  47. static DEFINE_SPINLOCK(kvm_lock);
  48. static LIST_HEAD(vm_list);
  49. static cpumask_t cpus_hardware_enabled;
  50. struct kvm_x86_ops *kvm_x86_ops;
  51. struct kmem_cache *kvm_vcpu_cache;
  52. EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
  53. static __read_mostly struct preempt_ops kvm_preempt_ops;
  54. #define STAT_OFFSET(x) offsetof(struct kvm_vcpu, stat.x)
  55. static struct kvm_stats_debugfs_item {
  56. const char *name;
  57. int offset;
  58. struct dentry *dentry;
  59. } debugfs_entries[] = {
  60. { "pf_fixed", STAT_OFFSET(pf_fixed) },
  61. { "pf_guest", STAT_OFFSET(pf_guest) },
  62. { "tlb_flush", STAT_OFFSET(tlb_flush) },
  63. { "invlpg", STAT_OFFSET(invlpg) },
  64. { "exits", STAT_OFFSET(exits) },
  65. { "io_exits", STAT_OFFSET(io_exits) },
  66. { "mmio_exits", STAT_OFFSET(mmio_exits) },
  67. { "signal_exits", STAT_OFFSET(signal_exits) },
  68. { "irq_window", STAT_OFFSET(irq_window_exits) },
  69. { "halt_exits", STAT_OFFSET(halt_exits) },
  70. { "halt_wakeup", STAT_OFFSET(halt_wakeup) },
  71. { "request_irq", STAT_OFFSET(request_irq_exits) },
  72. { "irq_exits", STAT_OFFSET(irq_exits) },
  73. { "light_exits", STAT_OFFSET(light_exits) },
  74. { "efer_reload", STAT_OFFSET(efer_reload) },
  75. { NULL }
  76. };
  77. static struct dentry *debugfs_dir;
  78. #define MAX_IO_MSRS 256
  79. #define CR0_RESERVED_BITS \
  80. (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
  81. | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
  82. | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
  83. #define CR4_RESERVED_BITS \
  84. (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
  85. | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
  86. | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
  87. | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
  88. #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
  89. #define EFER_RESERVED_BITS 0xfffffffffffff2fe
  90. #ifdef CONFIG_X86_64
  91. // LDT or TSS descriptor in the GDT. 16 bytes.
  92. struct segment_descriptor_64 {
  93. struct segment_descriptor s;
  94. u32 base_higher;
  95. u32 pad_zero;
  96. };
  97. #endif
  98. static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
  99. unsigned long arg);
  100. unsigned long segment_base(u16 selector)
  101. {
  102. struct descriptor_table gdt;
  103. struct segment_descriptor *d;
  104. unsigned long table_base;
  105. typedef unsigned long ul;
  106. unsigned long v;
  107. if (selector == 0)
  108. return 0;
  109. asm ("sgdt %0" : "=m"(gdt));
  110. table_base = gdt.base;
  111. if (selector & 4) { /* from ldt */
  112. u16 ldt_selector;
  113. asm ("sldt %0" : "=g"(ldt_selector));
  114. table_base = segment_base(ldt_selector);
  115. }
  116. d = (struct segment_descriptor *)(table_base + (selector & ~7));
  117. v = d->base_low | ((ul)d->base_mid << 16) | ((ul)d->base_high << 24);
  118. #ifdef CONFIG_X86_64
  119. if (d->system == 0
  120. && (d->type == 2 || d->type == 9 || d->type == 11))
  121. v |= ((ul)((struct segment_descriptor_64 *)d)->base_higher) << 32;
  122. #endif
  123. return v;
  124. }
  125. EXPORT_SYMBOL_GPL(segment_base);
  126. static inline int valid_vcpu(int n)
  127. {
  128. return likely(n >= 0 && n < KVM_MAX_VCPUS);
  129. }
  130. void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
  131. {
  132. if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
  133. return;
  134. vcpu->guest_fpu_loaded = 1;
  135. fx_save(&vcpu->host_fx_image);
  136. fx_restore(&vcpu->guest_fx_image);
  137. }
  138. EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
  139. void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
  140. {
  141. if (!vcpu->guest_fpu_loaded)
  142. return;
  143. vcpu->guest_fpu_loaded = 0;
  144. fx_save(&vcpu->guest_fx_image);
  145. fx_restore(&vcpu->host_fx_image);
  146. }
  147. EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
  148. /*
  149. * Switches to specified vcpu, until a matching vcpu_put()
  150. */
  151. static void vcpu_load(struct kvm_vcpu *vcpu)
  152. {
  153. int cpu;
  154. mutex_lock(&vcpu->mutex);
  155. cpu = get_cpu();
  156. preempt_notifier_register(&vcpu->preempt_notifier);
  157. kvm_x86_ops->vcpu_load(vcpu, cpu);
  158. put_cpu();
  159. }
  160. static void vcpu_put(struct kvm_vcpu *vcpu)
  161. {
  162. preempt_disable();
  163. kvm_x86_ops->vcpu_put(vcpu);
  164. preempt_notifier_unregister(&vcpu->preempt_notifier);
  165. preempt_enable();
  166. mutex_unlock(&vcpu->mutex);
  167. }
  168. static void ack_flush(void *_completed)
  169. {
  170. }
  171. void kvm_flush_remote_tlbs(struct kvm *kvm)
  172. {
  173. int i, cpu;
  174. cpumask_t cpus;
  175. struct kvm_vcpu *vcpu;
  176. cpus_clear(cpus);
  177. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  178. vcpu = kvm->vcpus[i];
  179. if (!vcpu)
  180. continue;
  181. if (test_and_set_bit(KVM_TLB_FLUSH, &vcpu->requests))
  182. continue;
  183. cpu = vcpu->cpu;
  184. if (cpu != -1 && cpu != raw_smp_processor_id())
  185. cpu_set(cpu, cpus);
  186. }
  187. smp_call_function_mask(cpus, ack_flush, NULL, 1);
  188. }
  189. int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
  190. {
  191. struct page *page;
  192. int r;
  193. mutex_init(&vcpu->mutex);
  194. vcpu->cpu = -1;
  195. vcpu->mmu.root_hpa = INVALID_PAGE;
  196. vcpu->kvm = kvm;
  197. vcpu->vcpu_id = id;
  198. if (!irqchip_in_kernel(kvm) || id == 0)
  199. vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
  200. else
  201. vcpu->mp_state = VCPU_MP_STATE_UNINITIALIZED;
  202. init_waitqueue_head(&vcpu->wq);
  203. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  204. if (!page) {
  205. r = -ENOMEM;
  206. goto fail;
  207. }
  208. vcpu->run = page_address(page);
  209. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  210. if (!page) {
  211. r = -ENOMEM;
  212. goto fail_free_run;
  213. }
  214. vcpu->pio_data = page_address(page);
  215. r = kvm_mmu_create(vcpu);
  216. if (r < 0)
  217. goto fail_free_pio_data;
  218. return 0;
  219. fail_free_pio_data:
  220. free_page((unsigned long)vcpu->pio_data);
  221. fail_free_run:
  222. free_page((unsigned long)vcpu->run);
  223. fail:
  224. return -ENOMEM;
  225. }
  226. EXPORT_SYMBOL_GPL(kvm_vcpu_init);
  227. void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
  228. {
  229. kvm_mmu_destroy(vcpu);
  230. if (vcpu->apic)
  231. hrtimer_cancel(&vcpu->apic->timer.dev);
  232. kvm_free_apic(vcpu->apic);
  233. free_page((unsigned long)vcpu->pio_data);
  234. free_page((unsigned long)vcpu->run);
  235. }
  236. EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
  237. static struct kvm *kvm_create_vm(void)
  238. {
  239. struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
  240. if (!kvm)
  241. return ERR_PTR(-ENOMEM);
  242. kvm_io_bus_init(&kvm->pio_bus);
  243. mutex_init(&kvm->lock);
  244. INIT_LIST_HEAD(&kvm->active_mmu_pages);
  245. kvm_io_bus_init(&kvm->mmio_bus);
  246. spin_lock(&kvm_lock);
  247. list_add(&kvm->vm_list, &vm_list);
  248. spin_unlock(&kvm_lock);
  249. return kvm;
  250. }
  251. /*
  252. * Free any memory in @free but not in @dont.
  253. */
  254. static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
  255. struct kvm_memory_slot *dont)
  256. {
  257. int i;
  258. if (!dont || free->phys_mem != dont->phys_mem)
  259. if (free->phys_mem) {
  260. for (i = 0; i < free->npages; ++i)
  261. if (free->phys_mem[i])
  262. __free_page(free->phys_mem[i]);
  263. vfree(free->phys_mem);
  264. }
  265. if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
  266. vfree(free->dirty_bitmap);
  267. free->phys_mem = NULL;
  268. free->npages = 0;
  269. free->dirty_bitmap = NULL;
  270. }
  271. static void kvm_free_physmem(struct kvm *kvm)
  272. {
  273. int i;
  274. for (i = 0; i < kvm->nmemslots; ++i)
  275. kvm_free_physmem_slot(&kvm->memslots[i], NULL);
  276. }
  277. static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
  278. {
  279. int i;
  280. for (i = 0; i < ARRAY_SIZE(vcpu->pio.guest_pages); ++i)
  281. if (vcpu->pio.guest_pages[i]) {
  282. __free_page(vcpu->pio.guest_pages[i]);
  283. vcpu->pio.guest_pages[i] = NULL;
  284. }
  285. }
  286. static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
  287. {
  288. vcpu_load(vcpu);
  289. kvm_mmu_unload(vcpu);
  290. vcpu_put(vcpu);
  291. }
  292. static void kvm_free_vcpus(struct kvm *kvm)
  293. {
  294. unsigned int i;
  295. /*
  296. * Unpin any mmu pages first.
  297. */
  298. for (i = 0; i < KVM_MAX_VCPUS; ++i)
  299. if (kvm->vcpus[i])
  300. kvm_unload_vcpu_mmu(kvm->vcpus[i]);
  301. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  302. if (kvm->vcpus[i]) {
  303. kvm_x86_ops->vcpu_free(kvm->vcpus[i]);
  304. kvm->vcpus[i] = NULL;
  305. }
  306. }
  307. }
  308. static void kvm_destroy_vm(struct kvm *kvm)
  309. {
  310. spin_lock(&kvm_lock);
  311. list_del(&kvm->vm_list);
  312. spin_unlock(&kvm_lock);
  313. kvm_io_bus_destroy(&kvm->pio_bus);
  314. kvm_io_bus_destroy(&kvm->mmio_bus);
  315. kfree(kvm->vpic);
  316. kfree(kvm->vioapic);
  317. kvm_free_vcpus(kvm);
  318. kvm_free_physmem(kvm);
  319. kfree(kvm);
  320. }
  321. static int kvm_vm_release(struct inode *inode, struct file *filp)
  322. {
  323. struct kvm *kvm = filp->private_data;
  324. kvm_destroy_vm(kvm);
  325. return 0;
  326. }
  327. static void inject_gp(struct kvm_vcpu *vcpu)
  328. {
  329. kvm_x86_ops->inject_gp(vcpu, 0);
  330. }
  331. /*
  332. * Load the pae pdptrs. Return true is they are all valid.
  333. */
  334. static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
  335. {
  336. gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
  337. unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
  338. int i;
  339. u64 *pdpt;
  340. int ret;
  341. struct page *page;
  342. u64 pdpte[ARRAY_SIZE(vcpu->pdptrs)];
  343. mutex_lock(&vcpu->kvm->lock);
  344. page = gfn_to_page(vcpu->kvm, pdpt_gfn);
  345. if (!page) {
  346. ret = 0;
  347. goto out;
  348. }
  349. pdpt = kmap_atomic(page, KM_USER0);
  350. memcpy(pdpte, pdpt+offset, sizeof(pdpte));
  351. kunmap_atomic(pdpt, KM_USER0);
  352. for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
  353. if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) {
  354. ret = 0;
  355. goto out;
  356. }
  357. }
  358. ret = 1;
  359. memcpy(vcpu->pdptrs, pdpte, sizeof(vcpu->pdptrs));
  360. out:
  361. mutex_unlock(&vcpu->kvm->lock);
  362. return ret;
  363. }
  364. void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  365. {
  366. if (cr0 & CR0_RESERVED_BITS) {
  367. printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
  368. cr0, vcpu->cr0);
  369. inject_gp(vcpu);
  370. return;
  371. }
  372. if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
  373. printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
  374. inject_gp(vcpu);
  375. return;
  376. }
  377. if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
  378. printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
  379. "and a clear PE flag\n");
  380. inject_gp(vcpu);
  381. return;
  382. }
  383. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
  384. #ifdef CONFIG_X86_64
  385. if ((vcpu->shadow_efer & EFER_LME)) {
  386. int cs_db, cs_l;
  387. if (!is_pae(vcpu)) {
  388. printk(KERN_DEBUG "set_cr0: #GP, start paging "
  389. "in long mode while PAE is disabled\n");
  390. inject_gp(vcpu);
  391. return;
  392. }
  393. kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  394. if (cs_l) {
  395. printk(KERN_DEBUG "set_cr0: #GP, start paging "
  396. "in long mode while CS.L == 1\n");
  397. inject_gp(vcpu);
  398. return;
  399. }
  400. } else
  401. #endif
  402. if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
  403. printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
  404. "reserved bits\n");
  405. inject_gp(vcpu);
  406. return;
  407. }
  408. }
  409. kvm_x86_ops->set_cr0(vcpu, cr0);
  410. vcpu->cr0 = cr0;
  411. mutex_lock(&vcpu->kvm->lock);
  412. kvm_mmu_reset_context(vcpu);
  413. mutex_unlock(&vcpu->kvm->lock);
  414. return;
  415. }
  416. EXPORT_SYMBOL_GPL(set_cr0);
  417. void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
  418. {
  419. set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
  420. }
  421. EXPORT_SYMBOL_GPL(lmsw);
  422. void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  423. {
  424. if (cr4 & CR4_RESERVED_BITS) {
  425. printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
  426. inject_gp(vcpu);
  427. return;
  428. }
  429. if (is_long_mode(vcpu)) {
  430. if (!(cr4 & X86_CR4_PAE)) {
  431. printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
  432. "in long mode\n");
  433. inject_gp(vcpu);
  434. return;
  435. }
  436. } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
  437. && !load_pdptrs(vcpu, vcpu->cr3)) {
  438. printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
  439. inject_gp(vcpu);
  440. return;
  441. }
  442. if (cr4 & X86_CR4_VMXE) {
  443. printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
  444. inject_gp(vcpu);
  445. return;
  446. }
  447. kvm_x86_ops->set_cr4(vcpu, cr4);
  448. vcpu->cr4 = cr4;
  449. mutex_lock(&vcpu->kvm->lock);
  450. kvm_mmu_reset_context(vcpu);
  451. mutex_unlock(&vcpu->kvm->lock);
  452. }
  453. EXPORT_SYMBOL_GPL(set_cr4);
  454. void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
  455. {
  456. if (is_long_mode(vcpu)) {
  457. if (cr3 & CR3_L_MODE_RESERVED_BITS) {
  458. printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
  459. inject_gp(vcpu);
  460. return;
  461. }
  462. } else {
  463. if (is_pae(vcpu)) {
  464. if (cr3 & CR3_PAE_RESERVED_BITS) {
  465. printk(KERN_DEBUG
  466. "set_cr3: #GP, reserved bits\n");
  467. inject_gp(vcpu);
  468. return;
  469. }
  470. if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
  471. printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
  472. "reserved bits\n");
  473. inject_gp(vcpu);
  474. return;
  475. }
  476. } else {
  477. if (cr3 & CR3_NONPAE_RESERVED_BITS) {
  478. printk(KERN_DEBUG
  479. "set_cr3: #GP, reserved bits\n");
  480. inject_gp(vcpu);
  481. return;
  482. }
  483. }
  484. }
  485. mutex_lock(&vcpu->kvm->lock);
  486. /*
  487. * Does the new cr3 value map to physical memory? (Note, we
  488. * catch an invalid cr3 even in real-mode, because it would
  489. * cause trouble later on when we turn on paging anyway.)
  490. *
  491. * A real CPU would silently accept an invalid cr3 and would
  492. * attempt to use it - with largely undefined (and often hard
  493. * to debug) behavior on the guest side.
  494. */
  495. if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
  496. inject_gp(vcpu);
  497. else {
  498. vcpu->cr3 = cr3;
  499. vcpu->mmu.new_cr3(vcpu);
  500. }
  501. mutex_unlock(&vcpu->kvm->lock);
  502. }
  503. EXPORT_SYMBOL_GPL(set_cr3);
  504. void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
  505. {
  506. if (cr8 & CR8_RESERVED_BITS) {
  507. printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
  508. inject_gp(vcpu);
  509. return;
  510. }
  511. if (irqchip_in_kernel(vcpu->kvm))
  512. kvm_lapic_set_tpr(vcpu, cr8);
  513. else
  514. vcpu->cr8 = cr8;
  515. }
  516. EXPORT_SYMBOL_GPL(set_cr8);
  517. unsigned long get_cr8(struct kvm_vcpu *vcpu)
  518. {
  519. if (irqchip_in_kernel(vcpu->kvm))
  520. return kvm_lapic_get_cr8(vcpu);
  521. else
  522. return vcpu->cr8;
  523. }
  524. EXPORT_SYMBOL_GPL(get_cr8);
  525. u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
  526. {
  527. if (irqchip_in_kernel(vcpu->kvm))
  528. return vcpu->apic_base;
  529. else
  530. return vcpu->apic_base;
  531. }
  532. EXPORT_SYMBOL_GPL(kvm_get_apic_base);
  533. void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
  534. {
  535. /* TODO: reserve bits check */
  536. if (irqchip_in_kernel(vcpu->kvm))
  537. kvm_lapic_set_base(vcpu, data);
  538. else
  539. vcpu->apic_base = data;
  540. }
  541. EXPORT_SYMBOL_GPL(kvm_set_apic_base);
  542. void fx_init(struct kvm_vcpu *vcpu)
  543. {
  544. unsigned after_mxcsr_mask;
  545. /* Initialize guest FPU by resetting ours and saving into guest's */
  546. preempt_disable();
  547. fx_save(&vcpu->host_fx_image);
  548. fpu_init();
  549. fx_save(&vcpu->guest_fx_image);
  550. fx_restore(&vcpu->host_fx_image);
  551. preempt_enable();
  552. vcpu->cr0 |= X86_CR0_ET;
  553. after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
  554. vcpu->guest_fx_image.mxcsr = 0x1f80;
  555. memset((void *)&vcpu->guest_fx_image + after_mxcsr_mask,
  556. 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
  557. }
  558. EXPORT_SYMBOL_GPL(fx_init);
  559. /*
  560. * Allocate some memory and give it an address in the guest physical address
  561. * space.
  562. *
  563. * Discontiguous memory is allowed, mostly for framebuffers.
  564. */
  565. static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
  566. struct kvm_memory_region *mem)
  567. {
  568. int r;
  569. gfn_t base_gfn;
  570. unsigned long npages;
  571. unsigned long i;
  572. struct kvm_memory_slot *memslot;
  573. struct kvm_memory_slot old, new;
  574. r = -EINVAL;
  575. /* General sanity checks */
  576. if (mem->memory_size & (PAGE_SIZE - 1))
  577. goto out;
  578. if (mem->guest_phys_addr & (PAGE_SIZE - 1))
  579. goto out;
  580. if (mem->slot >= KVM_MEMORY_SLOTS)
  581. goto out;
  582. if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
  583. goto out;
  584. memslot = &kvm->memslots[mem->slot];
  585. base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
  586. npages = mem->memory_size >> PAGE_SHIFT;
  587. if (!npages)
  588. mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
  589. mutex_lock(&kvm->lock);
  590. new = old = *memslot;
  591. new.base_gfn = base_gfn;
  592. new.npages = npages;
  593. new.flags = mem->flags;
  594. /* Disallow changing a memory slot's size. */
  595. r = -EINVAL;
  596. if (npages && old.npages && npages != old.npages)
  597. goto out_unlock;
  598. /* Check for overlaps */
  599. r = -EEXIST;
  600. for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
  601. struct kvm_memory_slot *s = &kvm->memslots[i];
  602. if (s == memslot)
  603. continue;
  604. if (!((base_gfn + npages <= s->base_gfn) ||
  605. (base_gfn >= s->base_gfn + s->npages)))
  606. goto out_unlock;
  607. }
  608. /* Deallocate if slot is being removed */
  609. if (!npages)
  610. new.phys_mem = NULL;
  611. /* Free page dirty bitmap if unneeded */
  612. if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
  613. new.dirty_bitmap = NULL;
  614. r = -ENOMEM;
  615. /* Allocate if a slot is being created */
  616. if (npages && !new.phys_mem) {
  617. new.phys_mem = vmalloc(npages * sizeof(struct page *));
  618. if (!new.phys_mem)
  619. goto out_unlock;
  620. memset(new.phys_mem, 0, npages * sizeof(struct page *));
  621. for (i = 0; i < npages; ++i) {
  622. new.phys_mem[i] = alloc_page(GFP_HIGHUSER
  623. | __GFP_ZERO);
  624. if (!new.phys_mem[i])
  625. goto out_unlock;
  626. set_page_private(new.phys_mem[i],0);
  627. }
  628. }
  629. /* Allocate page dirty bitmap if needed */
  630. if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
  631. unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
  632. new.dirty_bitmap = vmalloc(dirty_bytes);
  633. if (!new.dirty_bitmap)
  634. goto out_unlock;
  635. memset(new.dirty_bitmap, 0, dirty_bytes);
  636. }
  637. if (mem->slot >= kvm->nmemslots)
  638. kvm->nmemslots = mem->slot + 1;
  639. *memslot = new;
  640. kvm_mmu_slot_remove_write_access(kvm, mem->slot);
  641. kvm_flush_remote_tlbs(kvm);
  642. mutex_unlock(&kvm->lock);
  643. kvm_free_physmem_slot(&old, &new);
  644. return 0;
  645. out_unlock:
  646. mutex_unlock(&kvm->lock);
  647. kvm_free_physmem_slot(&new, &old);
  648. out:
  649. return r;
  650. }
  651. /*
  652. * Get (and clear) the dirty memory log for a memory slot.
  653. */
  654. static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
  655. struct kvm_dirty_log *log)
  656. {
  657. struct kvm_memory_slot *memslot;
  658. int r, i;
  659. int n;
  660. unsigned long any = 0;
  661. mutex_lock(&kvm->lock);
  662. r = -EINVAL;
  663. if (log->slot >= KVM_MEMORY_SLOTS)
  664. goto out;
  665. memslot = &kvm->memslots[log->slot];
  666. r = -ENOENT;
  667. if (!memslot->dirty_bitmap)
  668. goto out;
  669. n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
  670. for (i = 0; !any && i < n/sizeof(long); ++i)
  671. any = memslot->dirty_bitmap[i];
  672. r = -EFAULT;
  673. if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
  674. goto out;
  675. /* If nothing is dirty, don't bother messing with page tables. */
  676. if (any) {
  677. kvm_mmu_slot_remove_write_access(kvm, log->slot);
  678. kvm_flush_remote_tlbs(kvm);
  679. memset(memslot->dirty_bitmap, 0, n);
  680. }
  681. r = 0;
  682. out:
  683. mutex_unlock(&kvm->lock);
  684. return r;
  685. }
  686. /*
  687. * Set a new alias region. Aliases map a portion of physical memory into
  688. * another portion. This is useful for memory windows, for example the PC
  689. * VGA region.
  690. */
  691. static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
  692. struct kvm_memory_alias *alias)
  693. {
  694. int r, n;
  695. struct kvm_mem_alias *p;
  696. r = -EINVAL;
  697. /* General sanity checks */
  698. if (alias->memory_size & (PAGE_SIZE - 1))
  699. goto out;
  700. if (alias->guest_phys_addr & (PAGE_SIZE - 1))
  701. goto out;
  702. if (alias->slot >= KVM_ALIAS_SLOTS)
  703. goto out;
  704. if (alias->guest_phys_addr + alias->memory_size
  705. < alias->guest_phys_addr)
  706. goto out;
  707. if (alias->target_phys_addr + alias->memory_size
  708. < alias->target_phys_addr)
  709. goto out;
  710. mutex_lock(&kvm->lock);
  711. p = &kvm->aliases[alias->slot];
  712. p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
  713. p->npages = alias->memory_size >> PAGE_SHIFT;
  714. p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
  715. for (n = KVM_ALIAS_SLOTS; n > 0; --n)
  716. if (kvm->aliases[n - 1].npages)
  717. break;
  718. kvm->naliases = n;
  719. kvm_mmu_zap_all(kvm);
  720. mutex_unlock(&kvm->lock);
  721. return 0;
  722. out:
  723. return r;
  724. }
  725. static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
  726. {
  727. int r;
  728. r = 0;
  729. switch (chip->chip_id) {
  730. case KVM_IRQCHIP_PIC_MASTER:
  731. memcpy (&chip->chip.pic,
  732. &pic_irqchip(kvm)->pics[0],
  733. sizeof(struct kvm_pic_state));
  734. break;
  735. case KVM_IRQCHIP_PIC_SLAVE:
  736. memcpy (&chip->chip.pic,
  737. &pic_irqchip(kvm)->pics[1],
  738. sizeof(struct kvm_pic_state));
  739. break;
  740. case KVM_IRQCHIP_IOAPIC:
  741. memcpy (&chip->chip.ioapic,
  742. ioapic_irqchip(kvm),
  743. sizeof(struct kvm_ioapic_state));
  744. break;
  745. default:
  746. r = -EINVAL;
  747. break;
  748. }
  749. return r;
  750. }
  751. static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
  752. {
  753. int r;
  754. r = 0;
  755. switch (chip->chip_id) {
  756. case KVM_IRQCHIP_PIC_MASTER:
  757. memcpy (&pic_irqchip(kvm)->pics[0],
  758. &chip->chip.pic,
  759. sizeof(struct kvm_pic_state));
  760. break;
  761. case KVM_IRQCHIP_PIC_SLAVE:
  762. memcpy (&pic_irqchip(kvm)->pics[1],
  763. &chip->chip.pic,
  764. sizeof(struct kvm_pic_state));
  765. break;
  766. case KVM_IRQCHIP_IOAPIC:
  767. memcpy (ioapic_irqchip(kvm),
  768. &chip->chip.ioapic,
  769. sizeof(struct kvm_ioapic_state));
  770. break;
  771. default:
  772. r = -EINVAL;
  773. break;
  774. }
  775. kvm_pic_update_irq(pic_irqchip(kvm));
  776. return r;
  777. }
  778. static gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
  779. {
  780. int i;
  781. struct kvm_mem_alias *alias;
  782. for (i = 0; i < kvm->naliases; ++i) {
  783. alias = &kvm->aliases[i];
  784. if (gfn >= alias->base_gfn
  785. && gfn < alias->base_gfn + alias->npages)
  786. return alias->target_gfn + gfn - alias->base_gfn;
  787. }
  788. return gfn;
  789. }
  790. static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
  791. {
  792. int i;
  793. for (i = 0; i < kvm->nmemslots; ++i) {
  794. struct kvm_memory_slot *memslot = &kvm->memslots[i];
  795. if (gfn >= memslot->base_gfn
  796. && gfn < memslot->base_gfn + memslot->npages)
  797. return memslot;
  798. }
  799. return NULL;
  800. }
  801. struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
  802. {
  803. gfn = unalias_gfn(kvm, gfn);
  804. return __gfn_to_memslot(kvm, gfn);
  805. }
  806. struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
  807. {
  808. struct kvm_memory_slot *slot;
  809. gfn = unalias_gfn(kvm, gfn);
  810. slot = __gfn_to_memslot(kvm, gfn);
  811. if (!slot)
  812. return NULL;
  813. return slot->phys_mem[gfn - slot->base_gfn];
  814. }
  815. EXPORT_SYMBOL_GPL(gfn_to_page);
  816. /* WARNING: Does not work on aliased pages. */
  817. void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
  818. {
  819. struct kvm_memory_slot *memslot;
  820. memslot = __gfn_to_memslot(kvm, gfn);
  821. if (memslot && memslot->dirty_bitmap) {
  822. unsigned long rel_gfn = gfn - memslot->base_gfn;
  823. /* avoid RMW */
  824. if (!test_bit(rel_gfn, memslot->dirty_bitmap))
  825. set_bit(rel_gfn, memslot->dirty_bitmap);
  826. }
  827. }
  828. int emulator_read_std(unsigned long addr,
  829. void *val,
  830. unsigned int bytes,
  831. struct kvm_vcpu *vcpu)
  832. {
  833. void *data = val;
  834. while (bytes) {
  835. gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
  836. unsigned offset = addr & (PAGE_SIZE-1);
  837. unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
  838. unsigned long pfn;
  839. struct page *page;
  840. void *page_virt;
  841. if (gpa == UNMAPPED_GVA)
  842. return X86EMUL_PROPAGATE_FAULT;
  843. pfn = gpa >> PAGE_SHIFT;
  844. page = gfn_to_page(vcpu->kvm, pfn);
  845. if (!page)
  846. return X86EMUL_UNHANDLEABLE;
  847. page_virt = kmap_atomic(page, KM_USER0);
  848. memcpy(data, page_virt + offset, tocopy);
  849. kunmap_atomic(page_virt, KM_USER0);
  850. bytes -= tocopy;
  851. data += tocopy;
  852. addr += tocopy;
  853. }
  854. return X86EMUL_CONTINUE;
  855. }
  856. EXPORT_SYMBOL_GPL(emulator_read_std);
  857. static int emulator_write_std(unsigned long addr,
  858. const void *val,
  859. unsigned int bytes,
  860. struct kvm_vcpu *vcpu)
  861. {
  862. pr_unimpl(vcpu, "emulator_write_std: addr %lx n %d\n", addr, bytes);
  863. return X86EMUL_UNHANDLEABLE;
  864. }
  865. /*
  866. * Only apic need an MMIO device hook, so shortcut now..
  867. */
  868. static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
  869. gpa_t addr)
  870. {
  871. struct kvm_io_device *dev;
  872. if (vcpu->apic) {
  873. dev = &vcpu->apic->dev;
  874. if (dev->in_range(dev, addr))
  875. return dev;
  876. }
  877. return NULL;
  878. }
  879. static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
  880. gpa_t addr)
  881. {
  882. struct kvm_io_device *dev;
  883. dev = vcpu_find_pervcpu_dev(vcpu, addr);
  884. if (dev == NULL)
  885. dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
  886. return dev;
  887. }
  888. static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
  889. gpa_t addr)
  890. {
  891. return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
  892. }
  893. static int emulator_read_emulated(unsigned long addr,
  894. void *val,
  895. unsigned int bytes,
  896. struct kvm_vcpu *vcpu)
  897. {
  898. struct kvm_io_device *mmio_dev;
  899. gpa_t gpa;
  900. if (vcpu->mmio_read_completed) {
  901. memcpy(val, vcpu->mmio_data, bytes);
  902. vcpu->mmio_read_completed = 0;
  903. return X86EMUL_CONTINUE;
  904. } else if (emulator_read_std(addr, val, bytes, vcpu)
  905. == X86EMUL_CONTINUE)
  906. return X86EMUL_CONTINUE;
  907. gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
  908. if (gpa == UNMAPPED_GVA)
  909. return X86EMUL_PROPAGATE_FAULT;
  910. /*
  911. * Is this MMIO handled locally?
  912. */
  913. mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
  914. if (mmio_dev) {
  915. kvm_iodevice_read(mmio_dev, gpa, bytes, val);
  916. return X86EMUL_CONTINUE;
  917. }
  918. vcpu->mmio_needed = 1;
  919. vcpu->mmio_phys_addr = gpa;
  920. vcpu->mmio_size = bytes;
  921. vcpu->mmio_is_write = 0;
  922. return X86EMUL_UNHANDLEABLE;
  923. }
  924. static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
  925. const void *val, int bytes)
  926. {
  927. struct page *page;
  928. void *virt;
  929. if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT))
  930. return 0;
  931. page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
  932. if (!page)
  933. return 0;
  934. mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT);
  935. virt = kmap_atomic(page, KM_USER0);
  936. kvm_mmu_pte_write(vcpu, gpa, val, bytes);
  937. memcpy(virt + offset_in_page(gpa), val, bytes);
  938. kunmap_atomic(virt, KM_USER0);
  939. return 1;
  940. }
  941. static int emulator_write_emulated_onepage(unsigned long addr,
  942. const void *val,
  943. unsigned int bytes,
  944. struct kvm_vcpu *vcpu)
  945. {
  946. struct kvm_io_device *mmio_dev;
  947. gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
  948. if (gpa == UNMAPPED_GVA) {
  949. kvm_x86_ops->inject_page_fault(vcpu, addr, 2);
  950. return X86EMUL_PROPAGATE_FAULT;
  951. }
  952. if (emulator_write_phys(vcpu, gpa, val, bytes))
  953. return X86EMUL_CONTINUE;
  954. /*
  955. * Is this MMIO handled locally?
  956. */
  957. mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
  958. if (mmio_dev) {
  959. kvm_iodevice_write(mmio_dev, gpa, bytes, val);
  960. return X86EMUL_CONTINUE;
  961. }
  962. vcpu->mmio_needed = 1;
  963. vcpu->mmio_phys_addr = gpa;
  964. vcpu->mmio_size = bytes;
  965. vcpu->mmio_is_write = 1;
  966. memcpy(vcpu->mmio_data, val, bytes);
  967. return X86EMUL_CONTINUE;
  968. }
  969. int emulator_write_emulated(unsigned long addr,
  970. const void *val,
  971. unsigned int bytes,
  972. struct kvm_vcpu *vcpu)
  973. {
  974. /* Crossing a page boundary? */
  975. if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
  976. int rc, now;
  977. now = -addr & ~PAGE_MASK;
  978. rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
  979. if (rc != X86EMUL_CONTINUE)
  980. return rc;
  981. addr += now;
  982. val += now;
  983. bytes -= now;
  984. }
  985. return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
  986. }
  987. EXPORT_SYMBOL_GPL(emulator_write_emulated);
  988. static int emulator_cmpxchg_emulated(unsigned long addr,
  989. const void *old,
  990. const void *new,
  991. unsigned int bytes,
  992. struct kvm_vcpu *vcpu)
  993. {
  994. static int reported;
  995. if (!reported) {
  996. reported = 1;
  997. printk(KERN_WARNING "kvm: emulating exchange as write\n");
  998. }
  999. return emulator_write_emulated(addr, new, bytes, vcpu);
  1000. }
  1001. static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
  1002. {
  1003. return kvm_x86_ops->get_segment_base(vcpu, seg);
  1004. }
  1005. int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
  1006. {
  1007. return X86EMUL_CONTINUE;
  1008. }
  1009. int emulate_clts(struct kvm_vcpu *vcpu)
  1010. {
  1011. vcpu->cr0 &= ~X86_CR0_TS;
  1012. kvm_x86_ops->set_cr0(vcpu, vcpu->cr0);
  1013. return X86EMUL_CONTINUE;
  1014. }
  1015. int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
  1016. {
  1017. struct kvm_vcpu *vcpu = ctxt->vcpu;
  1018. switch (dr) {
  1019. case 0 ... 3:
  1020. *dest = kvm_x86_ops->get_dr(vcpu, dr);
  1021. return X86EMUL_CONTINUE;
  1022. default:
  1023. pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr);
  1024. return X86EMUL_UNHANDLEABLE;
  1025. }
  1026. }
  1027. int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
  1028. {
  1029. unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
  1030. int exception;
  1031. kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
  1032. if (exception) {
  1033. /* FIXME: better handling */
  1034. return X86EMUL_UNHANDLEABLE;
  1035. }
  1036. return X86EMUL_CONTINUE;
  1037. }
  1038. void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
  1039. {
  1040. static int reported;
  1041. u8 opcodes[4];
  1042. unsigned long rip = vcpu->rip;
  1043. unsigned long rip_linear;
  1044. rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
  1045. if (reported)
  1046. return;
  1047. emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
  1048. printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
  1049. context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
  1050. reported = 1;
  1051. }
  1052. EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
  1053. struct x86_emulate_ops emulate_ops = {
  1054. .read_std = emulator_read_std,
  1055. .write_std = emulator_write_std,
  1056. .read_emulated = emulator_read_emulated,
  1057. .write_emulated = emulator_write_emulated,
  1058. .cmpxchg_emulated = emulator_cmpxchg_emulated,
  1059. };
  1060. int emulate_instruction(struct kvm_vcpu *vcpu,
  1061. struct kvm_run *run,
  1062. unsigned long cr2,
  1063. u16 error_code)
  1064. {
  1065. struct x86_emulate_ctxt emulate_ctxt;
  1066. int r;
  1067. int cs_db, cs_l;
  1068. vcpu->mmio_fault_cr2 = cr2;
  1069. kvm_x86_ops->cache_regs(vcpu);
  1070. kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  1071. emulate_ctxt.vcpu = vcpu;
  1072. emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
  1073. emulate_ctxt.cr2 = cr2;
  1074. emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM)
  1075. ? X86EMUL_MODE_REAL : cs_l
  1076. ? X86EMUL_MODE_PROT64 : cs_db
  1077. ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
  1078. if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
  1079. emulate_ctxt.cs_base = 0;
  1080. emulate_ctxt.ds_base = 0;
  1081. emulate_ctxt.es_base = 0;
  1082. emulate_ctxt.ss_base = 0;
  1083. } else {
  1084. emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS);
  1085. emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS);
  1086. emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES);
  1087. emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS);
  1088. }
  1089. emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS);
  1090. emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS);
  1091. vcpu->mmio_is_write = 0;
  1092. vcpu->pio.string = 0;
  1093. r = x86_emulate_memop(&emulate_ctxt, &emulate_ops);
  1094. if (vcpu->pio.string)
  1095. return EMULATE_DO_MMIO;
  1096. if ((r || vcpu->mmio_is_write) && run) {
  1097. run->exit_reason = KVM_EXIT_MMIO;
  1098. run->mmio.phys_addr = vcpu->mmio_phys_addr;
  1099. memcpy(run->mmio.data, vcpu->mmio_data, 8);
  1100. run->mmio.len = vcpu->mmio_size;
  1101. run->mmio.is_write = vcpu->mmio_is_write;
  1102. }
  1103. if (r) {
  1104. if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
  1105. return EMULATE_DONE;
  1106. if (!vcpu->mmio_needed) {
  1107. kvm_report_emulation_failure(vcpu, "mmio");
  1108. return EMULATE_FAIL;
  1109. }
  1110. return EMULATE_DO_MMIO;
  1111. }
  1112. kvm_x86_ops->decache_regs(vcpu);
  1113. kvm_x86_ops->set_rflags(vcpu, emulate_ctxt.eflags);
  1114. if (vcpu->mmio_is_write) {
  1115. vcpu->mmio_needed = 0;
  1116. return EMULATE_DO_MMIO;
  1117. }
  1118. return EMULATE_DONE;
  1119. }
  1120. EXPORT_SYMBOL_GPL(emulate_instruction);
  1121. /*
  1122. * The vCPU has executed a HLT instruction with in-kernel mode enabled.
  1123. */
  1124. static void kvm_vcpu_block(struct kvm_vcpu *vcpu)
  1125. {
  1126. DECLARE_WAITQUEUE(wait, current);
  1127. add_wait_queue(&vcpu->wq, &wait);
  1128. /*
  1129. * We will block until either an interrupt or a signal wakes us up
  1130. */
  1131. while (!kvm_cpu_has_interrupt(vcpu)
  1132. && !signal_pending(current)
  1133. && vcpu->mp_state != VCPU_MP_STATE_RUNNABLE
  1134. && vcpu->mp_state != VCPU_MP_STATE_SIPI_RECEIVED) {
  1135. set_current_state(TASK_INTERRUPTIBLE);
  1136. vcpu_put(vcpu);
  1137. schedule();
  1138. vcpu_load(vcpu);
  1139. }
  1140. __set_current_state(TASK_RUNNING);
  1141. remove_wait_queue(&vcpu->wq, &wait);
  1142. }
  1143. int kvm_emulate_halt(struct kvm_vcpu *vcpu)
  1144. {
  1145. ++vcpu->stat.halt_exits;
  1146. if (irqchip_in_kernel(vcpu->kvm)) {
  1147. vcpu->mp_state = VCPU_MP_STATE_HALTED;
  1148. kvm_vcpu_block(vcpu);
  1149. if (vcpu->mp_state != VCPU_MP_STATE_RUNNABLE)
  1150. return -EINTR;
  1151. return 1;
  1152. } else {
  1153. vcpu->run->exit_reason = KVM_EXIT_HLT;
  1154. return 0;
  1155. }
  1156. }
  1157. EXPORT_SYMBOL_GPL(kvm_emulate_halt);
  1158. int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run)
  1159. {
  1160. unsigned long nr, a0, a1, a2, a3, a4, a5, ret;
  1161. kvm_x86_ops->cache_regs(vcpu);
  1162. ret = -KVM_EINVAL;
  1163. #ifdef CONFIG_X86_64
  1164. if (is_long_mode(vcpu)) {
  1165. nr = vcpu->regs[VCPU_REGS_RAX];
  1166. a0 = vcpu->regs[VCPU_REGS_RDI];
  1167. a1 = vcpu->regs[VCPU_REGS_RSI];
  1168. a2 = vcpu->regs[VCPU_REGS_RDX];
  1169. a3 = vcpu->regs[VCPU_REGS_RCX];
  1170. a4 = vcpu->regs[VCPU_REGS_R8];
  1171. a5 = vcpu->regs[VCPU_REGS_R9];
  1172. } else
  1173. #endif
  1174. {
  1175. nr = vcpu->regs[VCPU_REGS_RBX] & -1u;
  1176. a0 = vcpu->regs[VCPU_REGS_RAX] & -1u;
  1177. a1 = vcpu->regs[VCPU_REGS_RCX] & -1u;
  1178. a2 = vcpu->regs[VCPU_REGS_RDX] & -1u;
  1179. a3 = vcpu->regs[VCPU_REGS_RSI] & -1u;
  1180. a4 = vcpu->regs[VCPU_REGS_RDI] & -1u;
  1181. a5 = vcpu->regs[VCPU_REGS_RBP] & -1u;
  1182. }
  1183. switch (nr) {
  1184. default:
  1185. run->hypercall.nr = nr;
  1186. run->hypercall.args[0] = a0;
  1187. run->hypercall.args[1] = a1;
  1188. run->hypercall.args[2] = a2;
  1189. run->hypercall.args[3] = a3;
  1190. run->hypercall.args[4] = a4;
  1191. run->hypercall.args[5] = a5;
  1192. run->hypercall.ret = ret;
  1193. run->hypercall.longmode = is_long_mode(vcpu);
  1194. kvm_x86_ops->decache_regs(vcpu);
  1195. return 0;
  1196. }
  1197. vcpu->regs[VCPU_REGS_RAX] = ret;
  1198. kvm_x86_ops->decache_regs(vcpu);
  1199. return 1;
  1200. }
  1201. EXPORT_SYMBOL_GPL(kvm_hypercall);
  1202. static u64 mk_cr_64(u64 curr_cr, u32 new_val)
  1203. {
  1204. return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
  1205. }
  1206. void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
  1207. {
  1208. struct descriptor_table dt = { limit, base };
  1209. kvm_x86_ops->set_gdt(vcpu, &dt);
  1210. }
  1211. void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
  1212. {
  1213. struct descriptor_table dt = { limit, base };
  1214. kvm_x86_ops->set_idt(vcpu, &dt);
  1215. }
  1216. void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
  1217. unsigned long *rflags)
  1218. {
  1219. lmsw(vcpu, msw);
  1220. *rflags = kvm_x86_ops->get_rflags(vcpu);
  1221. }
  1222. unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
  1223. {
  1224. kvm_x86_ops->decache_cr4_guest_bits(vcpu);
  1225. switch (cr) {
  1226. case 0:
  1227. return vcpu->cr0;
  1228. case 2:
  1229. return vcpu->cr2;
  1230. case 3:
  1231. return vcpu->cr3;
  1232. case 4:
  1233. return vcpu->cr4;
  1234. default:
  1235. vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
  1236. return 0;
  1237. }
  1238. }
  1239. void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
  1240. unsigned long *rflags)
  1241. {
  1242. switch (cr) {
  1243. case 0:
  1244. set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
  1245. *rflags = kvm_x86_ops->get_rflags(vcpu);
  1246. break;
  1247. case 2:
  1248. vcpu->cr2 = val;
  1249. break;
  1250. case 3:
  1251. set_cr3(vcpu, val);
  1252. break;
  1253. case 4:
  1254. set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
  1255. break;
  1256. default:
  1257. vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
  1258. }
  1259. }
  1260. /*
  1261. * Register the para guest with the host:
  1262. */
  1263. static int vcpu_register_para(struct kvm_vcpu *vcpu, gpa_t para_state_gpa)
  1264. {
  1265. struct kvm_vcpu_para_state *para_state;
  1266. hpa_t para_state_hpa, hypercall_hpa;
  1267. struct page *para_state_page;
  1268. unsigned char *hypercall;
  1269. gpa_t hypercall_gpa;
  1270. printk(KERN_DEBUG "kvm: guest trying to enter paravirtual mode\n");
  1271. printk(KERN_DEBUG ".... para_state_gpa: %08Lx\n", para_state_gpa);
  1272. /*
  1273. * Needs to be page aligned:
  1274. */
  1275. if (para_state_gpa != PAGE_ALIGN(para_state_gpa))
  1276. goto err_gp;
  1277. para_state_hpa = gpa_to_hpa(vcpu, para_state_gpa);
  1278. printk(KERN_DEBUG ".... para_state_hpa: %08Lx\n", para_state_hpa);
  1279. if (is_error_hpa(para_state_hpa))
  1280. goto err_gp;
  1281. mark_page_dirty(vcpu->kvm, para_state_gpa >> PAGE_SHIFT);
  1282. para_state_page = pfn_to_page(para_state_hpa >> PAGE_SHIFT);
  1283. para_state = kmap(para_state_page);
  1284. printk(KERN_DEBUG ".... guest version: %d\n", para_state->guest_version);
  1285. printk(KERN_DEBUG ".... size: %d\n", para_state->size);
  1286. para_state->host_version = KVM_PARA_API_VERSION;
  1287. /*
  1288. * We cannot support guests that try to register themselves
  1289. * with a newer API version than the host supports:
  1290. */
  1291. if (para_state->guest_version > KVM_PARA_API_VERSION) {
  1292. para_state->ret = -KVM_EINVAL;
  1293. goto err_kunmap_skip;
  1294. }
  1295. hypercall_gpa = para_state->hypercall_gpa;
  1296. hypercall_hpa = gpa_to_hpa(vcpu, hypercall_gpa);
  1297. printk(KERN_DEBUG ".... hypercall_hpa: %08Lx\n", hypercall_hpa);
  1298. if (is_error_hpa(hypercall_hpa)) {
  1299. para_state->ret = -KVM_EINVAL;
  1300. goto err_kunmap_skip;
  1301. }
  1302. printk(KERN_DEBUG "kvm: para guest successfully registered.\n");
  1303. vcpu->para_state_page = para_state_page;
  1304. vcpu->para_state_gpa = para_state_gpa;
  1305. vcpu->hypercall_gpa = hypercall_gpa;
  1306. mark_page_dirty(vcpu->kvm, hypercall_gpa >> PAGE_SHIFT);
  1307. hypercall = kmap_atomic(pfn_to_page(hypercall_hpa >> PAGE_SHIFT),
  1308. KM_USER1) + (hypercall_hpa & ~PAGE_MASK);
  1309. kvm_x86_ops->patch_hypercall(vcpu, hypercall);
  1310. kunmap_atomic(hypercall, KM_USER1);
  1311. para_state->ret = 0;
  1312. err_kunmap_skip:
  1313. kunmap(para_state_page);
  1314. return 0;
  1315. err_gp:
  1316. return 1;
  1317. }
  1318. int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  1319. {
  1320. u64 data;
  1321. switch (msr) {
  1322. case 0xc0010010: /* SYSCFG */
  1323. case 0xc0010015: /* HWCR */
  1324. case MSR_IA32_PLATFORM_ID:
  1325. case MSR_IA32_P5_MC_ADDR:
  1326. case MSR_IA32_P5_MC_TYPE:
  1327. case MSR_IA32_MC0_CTL:
  1328. case MSR_IA32_MCG_STATUS:
  1329. case MSR_IA32_MCG_CAP:
  1330. case MSR_IA32_MC0_MISC:
  1331. case MSR_IA32_MC0_MISC+4:
  1332. case MSR_IA32_MC0_MISC+8:
  1333. case MSR_IA32_MC0_MISC+12:
  1334. case MSR_IA32_MC0_MISC+16:
  1335. case MSR_IA32_UCODE_REV:
  1336. case MSR_IA32_PERF_STATUS:
  1337. case MSR_IA32_EBL_CR_POWERON:
  1338. /* MTRR registers */
  1339. case 0xfe:
  1340. case 0x200 ... 0x2ff:
  1341. data = 0;
  1342. break;
  1343. case 0xcd: /* fsb frequency */
  1344. data = 3;
  1345. break;
  1346. case MSR_IA32_APICBASE:
  1347. data = kvm_get_apic_base(vcpu);
  1348. break;
  1349. case MSR_IA32_MISC_ENABLE:
  1350. data = vcpu->ia32_misc_enable_msr;
  1351. break;
  1352. #ifdef CONFIG_X86_64
  1353. case MSR_EFER:
  1354. data = vcpu->shadow_efer;
  1355. break;
  1356. #endif
  1357. default:
  1358. pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
  1359. return 1;
  1360. }
  1361. *pdata = data;
  1362. return 0;
  1363. }
  1364. EXPORT_SYMBOL_GPL(kvm_get_msr_common);
  1365. /*
  1366. * Reads an msr value (of 'msr_index') into 'pdata'.
  1367. * Returns 0 on success, non-0 otherwise.
  1368. * Assumes vcpu_load() was already called.
  1369. */
  1370. int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
  1371. {
  1372. return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
  1373. }
  1374. #ifdef CONFIG_X86_64
  1375. static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
  1376. {
  1377. if (efer & EFER_RESERVED_BITS) {
  1378. printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
  1379. efer);
  1380. inject_gp(vcpu);
  1381. return;
  1382. }
  1383. if (is_paging(vcpu)
  1384. && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
  1385. printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
  1386. inject_gp(vcpu);
  1387. return;
  1388. }
  1389. kvm_x86_ops->set_efer(vcpu, efer);
  1390. efer &= ~EFER_LMA;
  1391. efer |= vcpu->shadow_efer & EFER_LMA;
  1392. vcpu->shadow_efer = efer;
  1393. }
  1394. #endif
  1395. int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  1396. {
  1397. switch (msr) {
  1398. #ifdef CONFIG_X86_64
  1399. case MSR_EFER:
  1400. set_efer(vcpu, data);
  1401. break;
  1402. #endif
  1403. case MSR_IA32_MC0_STATUS:
  1404. pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
  1405. __FUNCTION__, data);
  1406. break;
  1407. case MSR_IA32_MCG_STATUS:
  1408. pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
  1409. __FUNCTION__, data);
  1410. break;
  1411. case MSR_IA32_UCODE_REV:
  1412. case MSR_IA32_UCODE_WRITE:
  1413. case 0x200 ... 0x2ff: /* MTRRs */
  1414. break;
  1415. case MSR_IA32_APICBASE:
  1416. kvm_set_apic_base(vcpu, data);
  1417. break;
  1418. case MSR_IA32_MISC_ENABLE:
  1419. vcpu->ia32_misc_enable_msr = data;
  1420. break;
  1421. /*
  1422. * This is the 'probe whether the host is KVM' logic:
  1423. */
  1424. case MSR_KVM_API_MAGIC:
  1425. return vcpu_register_para(vcpu, data);
  1426. default:
  1427. pr_unimpl(vcpu, "unhandled wrmsr: 0x%x\n", msr);
  1428. return 1;
  1429. }
  1430. return 0;
  1431. }
  1432. EXPORT_SYMBOL_GPL(kvm_set_msr_common);
  1433. /*
  1434. * Writes msr value into into the appropriate "register".
  1435. * Returns 0 on success, non-0 otherwise.
  1436. * Assumes vcpu_load() was already called.
  1437. */
  1438. int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
  1439. {
  1440. return kvm_x86_ops->set_msr(vcpu, msr_index, data);
  1441. }
  1442. void kvm_resched(struct kvm_vcpu *vcpu)
  1443. {
  1444. if (!need_resched())
  1445. return;
  1446. cond_resched();
  1447. }
  1448. EXPORT_SYMBOL_GPL(kvm_resched);
  1449. void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
  1450. {
  1451. int i;
  1452. u32 function;
  1453. struct kvm_cpuid_entry *e, *best;
  1454. kvm_x86_ops->cache_regs(vcpu);
  1455. function = vcpu->regs[VCPU_REGS_RAX];
  1456. vcpu->regs[VCPU_REGS_RAX] = 0;
  1457. vcpu->regs[VCPU_REGS_RBX] = 0;
  1458. vcpu->regs[VCPU_REGS_RCX] = 0;
  1459. vcpu->regs[VCPU_REGS_RDX] = 0;
  1460. best = NULL;
  1461. for (i = 0; i < vcpu->cpuid_nent; ++i) {
  1462. e = &vcpu->cpuid_entries[i];
  1463. if (e->function == function) {
  1464. best = e;
  1465. break;
  1466. }
  1467. /*
  1468. * Both basic or both extended?
  1469. */
  1470. if (((e->function ^ function) & 0x80000000) == 0)
  1471. if (!best || e->function > best->function)
  1472. best = e;
  1473. }
  1474. if (best) {
  1475. vcpu->regs[VCPU_REGS_RAX] = best->eax;
  1476. vcpu->regs[VCPU_REGS_RBX] = best->ebx;
  1477. vcpu->regs[VCPU_REGS_RCX] = best->ecx;
  1478. vcpu->regs[VCPU_REGS_RDX] = best->edx;
  1479. }
  1480. kvm_x86_ops->decache_regs(vcpu);
  1481. kvm_x86_ops->skip_emulated_instruction(vcpu);
  1482. }
  1483. EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
  1484. static int pio_copy_data(struct kvm_vcpu *vcpu)
  1485. {
  1486. void *p = vcpu->pio_data;
  1487. void *q;
  1488. unsigned bytes;
  1489. int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
  1490. q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
  1491. PAGE_KERNEL);
  1492. if (!q) {
  1493. free_pio_guest_pages(vcpu);
  1494. return -ENOMEM;
  1495. }
  1496. q += vcpu->pio.guest_page_offset;
  1497. bytes = vcpu->pio.size * vcpu->pio.cur_count;
  1498. if (vcpu->pio.in)
  1499. memcpy(q, p, bytes);
  1500. else
  1501. memcpy(p, q, bytes);
  1502. q -= vcpu->pio.guest_page_offset;
  1503. vunmap(q);
  1504. free_pio_guest_pages(vcpu);
  1505. return 0;
  1506. }
  1507. static int complete_pio(struct kvm_vcpu *vcpu)
  1508. {
  1509. struct kvm_pio_request *io = &vcpu->pio;
  1510. long delta;
  1511. int r;
  1512. kvm_x86_ops->cache_regs(vcpu);
  1513. if (!io->string) {
  1514. if (io->in)
  1515. memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
  1516. io->size);
  1517. } else {
  1518. if (io->in) {
  1519. r = pio_copy_data(vcpu);
  1520. if (r) {
  1521. kvm_x86_ops->cache_regs(vcpu);
  1522. return r;
  1523. }
  1524. }
  1525. delta = 1;
  1526. if (io->rep) {
  1527. delta *= io->cur_count;
  1528. /*
  1529. * The size of the register should really depend on
  1530. * current address size.
  1531. */
  1532. vcpu->regs[VCPU_REGS_RCX] -= delta;
  1533. }
  1534. if (io->down)
  1535. delta = -delta;
  1536. delta *= io->size;
  1537. if (io->in)
  1538. vcpu->regs[VCPU_REGS_RDI] += delta;
  1539. else
  1540. vcpu->regs[VCPU_REGS_RSI] += delta;
  1541. }
  1542. kvm_x86_ops->decache_regs(vcpu);
  1543. io->count -= io->cur_count;
  1544. io->cur_count = 0;
  1545. return 0;
  1546. }
  1547. static void kernel_pio(struct kvm_io_device *pio_dev,
  1548. struct kvm_vcpu *vcpu,
  1549. void *pd)
  1550. {
  1551. /* TODO: String I/O for in kernel device */
  1552. mutex_lock(&vcpu->kvm->lock);
  1553. if (vcpu->pio.in)
  1554. kvm_iodevice_read(pio_dev, vcpu->pio.port,
  1555. vcpu->pio.size,
  1556. pd);
  1557. else
  1558. kvm_iodevice_write(pio_dev, vcpu->pio.port,
  1559. vcpu->pio.size,
  1560. pd);
  1561. mutex_unlock(&vcpu->kvm->lock);
  1562. }
  1563. static void pio_string_write(struct kvm_io_device *pio_dev,
  1564. struct kvm_vcpu *vcpu)
  1565. {
  1566. struct kvm_pio_request *io = &vcpu->pio;
  1567. void *pd = vcpu->pio_data;
  1568. int i;
  1569. mutex_lock(&vcpu->kvm->lock);
  1570. for (i = 0; i < io->cur_count; i++) {
  1571. kvm_iodevice_write(pio_dev, io->port,
  1572. io->size,
  1573. pd);
  1574. pd += io->size;
  1575. }
  1576. mutex_unlock(&vcpu->kvm->lock);
  1577. }
  1578. int kvm_emulate_pio (struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
  1579. int size, unsigned port)
  1580. {
  1581. struct kvm_io_device *pio_dev;
  1582. vcpu->run->exit_reason = KVM_EXIT_IO;
  1583. vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
  1584. vcpu->run->io.size = vcpu->pio.size = size;
  1585. vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
  1586. vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = 1;
  1587. vcpu->run->io.port = vcpu->pio.port = port;
  1588. vcpu->pio.in = in;
  1589. vcpu->pio.string = 0;
  1590. vcpu->pio.down = 0;
  1591. vcpu->pio.guest_page_offset = 0;
  1592. vcpu->pio.rep = 0;
  1593. kvm_x86_ops->cache_regs(vcpu);
  1594. memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
  1595. kvm_x86_ops->decache_regs(vcpu);
  1596. kvm_x86_ops->skip_emulated_instruction(vcpu);
  1597. pio_dev = vcpu_find_pio_dev(vcpu, port);
  1598. if (pio_dev) {
  1599. kernel_pio(pio_dev, vcpu, vcpu->pio_data);
  1600. complete_pio(vcpu);
  1601. return 1;
  1602. }
  1603. return 0;
  1604. }
  1605. EXPORT_SYMBOL_GPL(kvm_emulate_pio);
  1606. int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
  1607. int size, unsigned long count, int down,
  1608. gva_t address, int rep, unsigned port)
  1609. {
  1610. unsigned now, in_page;
  1611. int i, ret = 0;
  1612. int nr_pages = 1;
  1613. struct page *page;
  1614. struct kvm_io_device *pio_dev;
  1615. vcpu->run->exit_reason = KVM_EXIT_IO;
  1616. vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
  1617. vcpu->run->io.size = vcpu->pio.size = size;
  1618. vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
  1619. vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = count;
  1620. vcpu->run->io.port = vcpu->pio.port = port;
  1621. vcpu->pio.in = in;
  1622. vcpu->pio.string = 1;
  1623. vcpu->pio.down = down;
  1624. vcpu->pio.guest_page_offset = offset_in_page(address);
  1625. vcpu->pio.rep = rep;
  1626. if (!count) {
  1627. kvm_x86_ops->skip_emulated_instruction(vcpu);
  1628. return 1;
  1629. }
  1630. if (!down)
  1631. in_page = PAGE_SIZE - offset_in_page(address);
  1632. else
  1633. in_page = offset_in_page(address) + size;
  1634. now = min(count, (unsigned long)in_page / size);
  1635. if (!now) {
  1636. /*
  1637. * String I/O straddles page boundary. Pin two guest pages
  1638. * so that we satisfy atomicity constraints. Do just one
  1639. * transaction to avoid complexity.
  1640. */
  1641. nr_pages = 2;
  1642. now = 1;
  1643. }
  1644. if (down) {
  1645. /*
  1646. * String I/O in reverse. Yuck. Kill the guest, fix later.
  1647. */
  1648. pr_unimpl(vcpu, "guest string pio down\n");
  1649. inject_gp(vcpu);
  1650. return 1;
  1651. }
  1652. vcpu->run->io.count = now;
  1653. vcpu->pio.cur_count = now;
  1654. if (vcpu->pio.cur_count == vcpu->pio.count)
  1655. kvm_x86_ops->skip_emulated_instruction(vcpu);
  1656. for (i = 0; i < nr_pages; ++i) {
  1657. mutex_lock(&vcpu->kvm->lock);
  1658. page = gva_to_page(vcpu, address + i * PAGE_SIZE);
  1659. if (page)
  1660. get_page(page);
  1661. vcpu->pio.guest_pages[i] = page;
  1662. mutex_unlock(&vcpu->kvm->lock);
  1663. if (!page) {
  1664. inject_gp(vcpu);
  1665. free_pio_guest_pages(vcpu);
  1666. return 1;
  1667. }
  1668. }
  1669. pio_dev = vcpu_find_pio_dev(vcpu, port);
  1670. if (!vcpu->pio.in) {
  1671. /* string PIO write */
  1672. ret = pio_copy_data(vcpu);
  1673. if (ret >= 0 && pio_dev) {
  1674. pio_string_write(pio_dev, vcpu);
  1675. complete_pio(vcpu);
  1676. if (vcpu->pio.count == 0)
  1677. ret = 1;
  1678. }
  1679. } else if (pio_dev)
  1680. pr_unimpl(vcpu, "no string pio read support yet, "
  1681. "port %x size %d count %ld\n",
  1682. port, size, count);
  1683. return ret;
  1684. }
  1685. EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
  1686. /*
  1687. * Check if userspace requested an interrupt window, and that the
  1688. * interrupt window is open.
  1689. *
  1690. * No need to exit to userspace if we already have an interrupt queued.
  1691. */
  1692. static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
  1693. struct kvm_run *kvm_run)
  1694. {
  1695. return (!vcpu->irq_summary &&
  1696. kvm_run->request_interrupt_window &&
  1697. vcpu->interrupt_window_open &&
  1698. (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
  1699. }
  1700. static void post_kvm_run_save(struct kvm_vcpu *vcpu,
  1701. struct kvm_run *kvm_run)
  1702. {
  1703. kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
  1704. kvm_run->cr8 = get_cr8(vcpu);
  1705. kvm_run->apic_base = kvm_get_apic_base(vcpu);
  1706. if (irqchip_in_kernel(vcpu->kvm))
  1707. kvm_run->ready_for_interrupt_injection = 1;
  1708. else
  1709. kvm_run->ready_for_interrupt_injection =
  1710. (vcpu->interrupt_window_open &&
  1711. vcpu->irq_summary == 0);
  1712. }
  1713. static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  1714. {
  1715. int r;
  1716. if (unlikely(vcpu->mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) {
  1717. printk("vcpu %d received sipi with vector # %x\n",
  1718. vcpu->vcpu_id, vcpu->sipi_vector);
  1719. kvm_lapic_reset(vcpu);
  1720. kvm_x86_ops->vcpu_reset(vcpu);
  1721. vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
  1722. }
  1723. preempted:
  1724. if (vcpu->guest_debug.enabled)
  1725. kvm_x86_ops->guest_debug_pre(vcpu);
  1726. again:
  1727. r = kvm_mmu_reload(vcpu);
  1728. if (unlikely(r))
  1729. goto out;
  1730. preempt_disable();
  1731. kvm_x86_ops->prepare_guest_switch(vcpu);
  1732. kvm_load_guest_fpu(vcpu);
  1733. local_irq_disable();
  1734. if (signal_pending(current)) {
  1735. local_irq_enable();
  1736. preempt_enable();
  1737. r = -EINTR;
  1738. kvm_run->exit_reason = KVM_EXIT_INTR;
  1739. ++vcpu->stat.signal_exits;
  1740. goto out;
  1741. }
  1742. if (irqchip_in_kernel(vcpu->kvm))
  1743. kvm_x86_ops->inject_pending_irq(vcpu);
  1744. else if (!vcpu->mmio_read_completed)
  1745. kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
  1746. vcpu->guest_mode = 1;
  1747. kvm_guest_enter();
  1748. if (vcpu->requests)
  1749. if (test_and_clear_bit(KVM_TLB_FLUSH, &vcpu->requests))
  1750. kvm_x86_ops->tlb_flush(vcpu);
  1751. kvm_x86_ops->run(vcpu, kvm_run);
  1752. vcpu->guest_mode = 0;
  1753. local_irq_enable();
  1754. ++vcpu->stat.exits;
  1755. /*
  1756. * We must have an instruction between local_irq_enable() and
  1757. * kvm_guest_exit(), so the timer interrupt isn't delayed by
  1758. * the interrupt shadow. The stat.exits increment will do nicely.
  1759. * But we need to prevent reordering, hence this barrier():
  1760. */
  1761. barrier();
  1762. kvm_guest_exit();
  1763. preempt_enable();
  1764. /*
  1765. * Profile KVM exit RIPs:
  1766. */
  1767. if (unlikely(prof_on == KVM_PROFILING)) {
  1768. kvm_x86_ops->cache_regs(vcpu);
  1769. profile_hit(KVM_PROFILING, (void *)vcpu->rip);
  1770. }
  1771. r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
  1772. if (r > 0) {
  1773. if (dm_request_for_irq_injection(vcpu, kvm_run)) {
  1774. r = -EINTR;
  1775. kvm_run->exit_reason = KVM_EXIT_INTR;
  1776. ++vcpu->stat.request_irq_exits;
  1777. goto out;
  1778. }
  1779. if (!need_resched()) {
  1780. ++vcpu->stat.light_exits;
  1781. goto again;
  1782. }
  1783. }
  1784. out:
  1785. if (r > 0) {
  1786. kvm_resched(vcpu);
  1787. goto preempted;
  1788. }
  1789. post_kvm_run_save(vcpu, kvm_run);
  1790. return r;
  1791. }
  1792. static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  1793. {
  1794. int r;
  1795. sigset_t sigsaved;
  1796. vcpu_load(vcpu);
  1797. if (unlikely(vcpu->mp_state == VCPU_MP_STATE_UNINITIALIZED)) {
  1798. kvm_vcpu_block(vcpu);
  1799. vcpu_put(vcpu);
  1800. return -EAGAIN;
  1801. }
  1802. if (vcpu->sigset_active)
  1803. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  1804. /* re-sync apic's tpr */
  1805. if (!irqchip_in_kernel(vcpu->kvm))
  1806. set_cr8(vcpu, kvm_run->cr8);
  1807. if (vcpu->pio.cur_count) {
  1808. r = complete_pio(vcpu);
  1809. if (r)
  1810. goto out;
  1811. }
  1812. if (vcpu->mmio_needed) {
  1813. memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
  1814. vcpu->mmio_read_completed = 1;
  1815. vcpu->mmio_needed = 0;
  1816. r = emulate_instruction(vcpu, kvm_run,
  1817. vcpu->mmio_fault_cr2, 0);
  1818. if (r == EMULATE_DO_MMIO) {
  1819. /*
  1820. * Read-modify-write. Back to userspace.
  1821. */
  1822. r = 0;
  1823. goto out;
  1824. }
  1825. }
  1826. if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
  1827. kvm_x86_ops->cache_regs(vcpu);
  1828. vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
  1829. kvm_x86_ops->decache_regs(vcpu);
  1830. }
  1831. r = __vcpu_run(vcpu, kvm_run);
  1832. out:
  1833. if (vcpu->sigset_active)
  1834. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  1835. vcpu_put(vcpu);
  1836. return r;
  1837. }
  1838. static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
  1839. struct kvm_regs *regs)
  1840. {
  1841. vcpu_load(vcpu);
  1842. kvm_x86_ops->cache_regs(vcpu);
  1843. regs->rax = vcpu->regs[VCPU_REGS_RAX];
  1844. regs->rbx = vcpu->regs[VCPU_REGS_RBX];
  1845. regs->rcx = vcpu->regs[VCPU_REGS_RCX];
  1846. regs->rdx = vcpu->regs[VCPU_REGS_RDX];
  1847. regs->rsi = vcpu->regs[VCPU_REGS_RSI];
  1848. regs->rdi = vcpu->regs[VCPU_REGS_RDI];
  1849. regs->rsp = vcpu->regs[VCPU_REGS_RSP];
  1850. regs->rbp = vcpu->regs[VCPU_REGS_RBP];
  1851. #ifdef CONFIG_X86_64
  1852. regs->r8 = vcpu->regs[VCPU_REGS_R8];
  1853. regs->r9 = vcpu->regs[VCPU_REGS_R9];
  1854. regs->r10 = vcpu->regs[VCPU_REGS_R10];
  1855. regs->r11 = vcpu->regs[VCPU_REGS_R11];
  1856. regs->r12 = vcpu->regs[VCPU_REGS_R12];
  1857. regs->r13 = vcpu->regs[VCPU_REGS_R13];
  1858. regs->r14 = vcpu->regs[VCPU_REGS_R14];
  1859. regs->r15 = vcpu->regs[VCPU_REGS_R15];
  1860. #endif
  1861. regs->rip = vcpu->rip;
  1862. regs->rflags = kvm_x86_ops->get_rflags(vcpu);
  1863. /*
  1864. * Don't leak debug flags in case they were set for guest debugging
  1865. */
  1866. if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
  1867. regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
  1868. vcpu_put(vcpu);
  1869. return 0;
  1870. }
  1871. static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
  1872. struct kvm_regs *regs)
  1873. {
  1874. vcpu_load(vcpu);
  1875. vcpu->regs[VCPU_REGS_RAX] = regs->rax;
  1876. vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
  1877. vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
  1878. vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
  1879. vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
  1880. vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
  1881. vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
  1882. vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
  1883. #ifdef CONFIG_X86_64
  1884. vcpu->regs[VCPU_REGS_R8] = regs->r8;
  1885. vcpu->regs[VCPU_REGS_R9] = regs->r9;
  1886. vcpu->regs[VCPU_REGS_R10] = regs->r10;
  1887. vcpu->regs[VCPU_REGS_R11] = regs->r11;
  1888. vcpu->regs[VCPU_REGS_R12] = regs->r12;
  1889. vcpu->regs[VCPU_REGS_R13] = regs->r13;
  1890. vcpu->regs[VCPU_REGS_R14] = regs->r14;
  1891. vcpu->regs[VCPU_REGS_R15] = regs->r15;
  1892. #endif
  1893. vcpu->rip = regs->rip;
  1894. kvm_x86_ops->set_rflags(vcpu, regs->rflags);
  1895. kvm_x86_ops->decache_regs(vcpu);
  1896. vcpu_put(vcpu);
  1897. return 0;
  1898. }
  1899. static void get_segment(struct kvm_vcpu *vcpu,
  1900. struct kvm_segment *var, int seg)
  1901. {
  1902. return kvm_x86_ops->get_segment(vcpu, var, seg);
  1903. }
  1904. static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  1905. struct kvm_sregs *sregs)
  1906. {
  1907. struct descriptor_table dt;
  1908. int pending_vec;
  1909. vcpu_load(vcpu);
  1910. get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  1911. get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  1912. get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  1913. get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  1914. get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  1915. get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  1916. get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  1917. get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  1918. kvm_x86_ops->get_idt(vcpu, &dt);
  1919. sregs->idt.limit = dt.limit;
  1920. sregs->idt.base = dt.base;
  1921. kvm_x86_ops->get_gdt(vcpu, &dt);
  1922. sregs->gdt.limit = dt.limit;
  1923. sregs->gdt.base = dt.base;
  1924. kvm_x86_ops->decache_cr4_guest_bits(vcpu);
  1925. sregs->cr0 = vcpu->cr0;
  1926. sregs->cr2 = vcpu->cr2;
  1927. sregs->cr3 = vcpu->cr3;
  1928. sregs->cr4 = vcpu->cr4;
  1929. sregs->cr8 = get_cr8(vcpu);
  1930. sregs->efer = vcpu->shadow_efer;
  1931. sregs->apic_base = kvm_get_apic_base(vcpu);
  1932. if (irqchip_in_kernel(vcpu->kvm)) {
  1933. memset(sregs->interrupt_bitmap, 0,
  1934. sizeof sregs->interrupt_bitmap);
  1935. pending_vec = kvm_x86_ops->get_irq(vcpu);
  1936. if (pending_vec >= 0)
  1937. set_bit(pending_vec, (unsigned long *)sregs->interrupt_bitmap);
  1938. } else
  1939. memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
  1940. sizeof sregs->interrupt_bitmap);
  1941. vcpu_put(vcpu);
  1942. return 0;
  1943. }
  1944. static void set_segment(struct kvm_vcpu *vcpu,
  1945. struct kvm_segment *var, int seg)
  1946. {
  1947. return kvm_x86_ops->set_segment(vcpu, var, seg);
  1948. }
  1949. static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  1950. struct kvm_sregs *sregs)
  1951. {
  1952. int mmu_reset_needed = 0;
  1953. int i, pending_vec, max_bits;
  1954. struct descriptor_table dt;
  1955. vcpu_load(vcpu);
  1956. dt.limit = sregs->idt.limit;
  1957. dt.base = sregs->idt.base;
  1958. kvm_x86_ops->set_idt(vcpu, &dt);
  1959. dt.limit = sregs->gdt.limit;
  1960. dt.base = sregs->gdt.base;
  1961. kvm_x86_ops->set_gdt(vcpu, &dt);
  1962. vcpu->cr2 = sregs->cr2;
  1963. mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
  1964. vcpu->cr3 = sregs->cr3;
  1965. set_cr8(vcpu, sregs->cr8);
  1966. mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
  1967. #ifdef CONFIG_X86_64
  1968. kvm_x86_ops->set_efer(vcpu, sregs->efer);
  1969. #endif
  1970. kvm_set_apic_base(vcpu, sregs->apic_base);
  1971. kvm_x86_ops->decache_cr4_guest_bits(vcpu);
  1972. mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
  1973. vcpu->cr0 = sregs->cr0;
  1974. kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
  1975. mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
  1976. kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
  1977. if (!is_long_mode(vcpu) && is_pae(vcpu))
  1978. load_pdptrs(vcpu, vcpu->cr3);
  1979. if (mmu_reset_needed)
  1980. kvm_mmu_reset_context(vcpu);
  1981. if (!irqchip_in_kernel(vcpu->kvm)) {
  1982. memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
  1983. sizeof vcpu->irq_pending);
  1984. vcpu->irq_summary = 0;
  1985. for (i = 0; i < ARRAY_SIZE(vcpu->irq_pending); ++i)
  1986. if (vcpu->irq_pending[i])
  1987. __set_bit(i, &vcpu->irq_summary);
  1988. } else {
  1989. max_bits = (sizeof sregs->interrupt_bitmap) << 3;
  1990. pending_vec = find_first_bit(
  1991. (const unsigned long *)sregs->interrupt_bitmap,
  1992. max_bits);
  1993. /* Only pending external irq is handled here */
  1994. if (pending_vec < max_bits) {
  1995. kvm_x86_ops->set_irq(vcpu, pending_vec);
  1996. printk("Set back pending irq %d\n", pending_vec);
  1997. }
  1998. }
  1999. set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  2000. set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  2001. set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  2002. set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  2003. set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  2004. set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  2005. set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  2006. set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  2007. vcpu_put(vcpu);
  2008. return 0;
  2009. }
  2010. void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
  2011. {
  2012. struct kvm_segment cs;
  2013. get_segment(vcpu, &cs, VCPU_SREG_CS);
  2014. *db = cs.db;
  2015. *l = cs.l;
  2016. }
  2017. EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
  2018. /*
  2019. * List of msr numbers which we expose to userspace through KVM_GET_MSRS
  2020. * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
  2021. *
  2022. * This list is modified at module load time to reflect the
  2023. * capabilities of the host cpu.
  2024. */
  2025. static u32 msrs_to_save[] = {
  2026. MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
  2027. MSR_K6_STAR,
  2028. #ifdef CONFIG_X86_64
  2029. MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
  2030. #endif
  2031. MSR_IA32_TIME_STAMP_COUNTER,
  2032. };
  2033. static unsigned num_msrs_to_save;
  2034. static u32 emulated_msrs[] = {
  2035. MSR_IA32_MISC_ENABLE,
  2036. };
  2037. static __init void kvm_init_msr_list(void)
  2038. {
  2039. u32 dummy[2];
  2040. unsigned i, j;
  2041. for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
  2042. if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
  2043. continue;
  2044. if (j < i)
  2045. msrs_to_save[j] = msrs_to_save[i];
  2046. j++;
  2047. }
  2048. num_msrs_to_save = j;
  2049. }
  2050. /*
  2051. * Adapt set_msr() to msr_io()'s calling convention
  2052. */
  2053. static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
  2054. {
  2055. return kvm_set_msr(vcpu, index, *data);
  2056. }
  2057. /*
  2058. * Read or write a bunch of msrs. All parameters are kernel addresses.
  2059. *
  2060. * @return number of msrs set successfully.
  2061. */
  2062. static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
  2063. struct kvm_msr_entry *entries,
  2064. int (*do_msr)(struct kvm_vcpu *vcpu,
  2065. unsigned index, u64 *data))
  2066. {
  2067. int i;
  2068. vcpu_load(vcpu);
  2069. for (i = 0; i < msrs->nmsrs; ++i)
  2070. if (do_msr(vcpu, entries[i].index, &entries[i].data))
  2071. break;
  2072. vcpu_put(vcpu);
  2073. return i;
  2074. }
  2075. /*
  2076. * Read or write a bunch of msrs. Parameters are user addresses.
  2077. *
  2078. * @return number of msrs set successfully.
  2079. */
  2080. static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
  2081. int (*do_msr)(struct kvm_vcpu *vcpu,
  2082. unsigned index, u64 *data),
  2083. int writeback)
  2084. {
  2085. struct kvm_msrs msrs;
  2086. struct kvm_msr_entry *entries;
  2087. int r, n;
  2088. unsigned size;
  2089. r = -EFAULT;
  2090. if (copy_from_user(&msrs, user_msrs, sizeof msrs))
  2091. goto out;
  2092. r = -E2BIG;
  2093. if (msrs.nmsrs >= MAX_IO_MSRS)
  2094. goto out;
  2095. r = -ENOMEM;
  2096. size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
  2097. entries = vmalloc(size);
  2098. if (!entries)
  2099. goto out;
  2100. r = -EFAULT;
  2101. if (copy_from_user(entries, user_msrs->entries, size))
  2102. goto out_free;
  2103. r = n = __msr_io(vcpu, &msrs, entries, do_msr);
  2104. if (r < 0)
  2105. goto out_free;
  2106. r = -EFAULT;
  2107. if (writeback && copy_to_user(user_msrs->entries, entries, size))
  2108. goto out_free;
  2109. r = n;
  2110. out_free:
  2111. vfree(entries);
  2112. out:
  2113. return r;
  2114. }
  2115. /*
  2116. * Translate a guest virtual address to a guest physical address.
  2117. */
  2118. static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  2119. struct kvm_translation *tr)
  2120. {
  2121. unsigned long vaddr = tr->linear_address;
  2122. gpa_t gpa;
  2123. vcpu_load(vcpu);
  2124. mutex_lock(&vcpu->kvm->lock);
  2125. gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
  2126. tr->physical_address = gpa;
  2127. tr->valid = gpa != UNMAPPED_GVA;
  2128. tr->writeable = 1;
  2129. tr->usermode = 0;
  2130. mutex_unlock(&vcpu->kvm->lock);
  2131. vcpu_put(vcpu);
  2132. return 0;
  2133. }
  2134. static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
  2135. struct kvm_interrupt *irq)
  2136. {
  2137. if (irq->irq < 0 || irq->irq >= 256)
  2138. return -EINVAL;
  2139. if (irqchip_in_kernel(vcpu->kvm))
  2140. return -ENXIO;
  2141. vcpu_load(vcpu);
  2142. set_bit(irq->irq, vcpu->irq_pending);
  2143. set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
  2144. vcpu_put(vcpu);
  2145. return 0;
  2146. }
  2147. static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
  2148. struct kvm_debug_guest *dbg)
  2149. {
  2150. int r;
  2151. vcpu_load(vcpu);
  2152. r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
  2153. vcpu_put(vcpu);
  2154. return r;
  2155. }
  2156. static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
  2157. unsigned long address,
  2158. int *type)
  2159. {
  2160. struct kvm_vcpu *vcpu = vma->vm_file->private_data;
  2161. unsigned long pgoff;
  2162. struct page *page;
  2163. pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  2164. if (pgoff == 0)
  2165. page = virt_to_page(vcpu->run);
  2166. else if (pgoff == KVM_PIO_PAGE_OFFSET)
  2167. page = virt_to_page(vcpu->pio_data);
  2168. else
  2169. return NOPAGE_SIGBUS;
  2170. get_page(page);
  2171. if (type != NULL)
  2172. *type = VM_FAULT_MINOR;
  2173. return page;
  2174. }
  2175. static struct vm_operations_struct kvm_vcpu_vm_ops = {
  2176. .nopage = kvm_vcpu_nopage,
  2177. };
  2178. static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
  2179. {
  2180. vma->vm_ops = &kvm_vcpu_vm_ops;
  2181. return 0;
  2182. }
  2183. static int kvm_vcpu_release(struct inode *inode, struct file *filp)
  2184. {
  2185. struct kvm_vcpu *vcpu = filp->private_data;
  2186. fput(vcpu->kvm->filp);
  2187. return 0;
  2188. }
  2189. static struct file_operations kvm_vcpu_fops = {
  2190. .release = kvm_vcpu_release,
  2191. .unlocked_ioctl = kvm_vcpu_ioctl,
  2192. .compat_ioctl = kvm_vcpu_ioctl,
  2193. .mmap = kvm_vcpu_mmap,
  2194. };
  2195. /*
  2196. * Allocates an inode for the vcpu.
  2197. */
  2198. static int create_vcpu_fd(struct kvm_vcpu *vcpu)
  2199. {
  2200. int fd, r;
  2201. struct inode *inode;
  2202. struct file *file;
  2203. r = anon_inode_getfd(&fd, &inode, &file,
  2204. "kvm-vcpu", &kvm_vcpu_fops, vcpu);
  2205. if (r)
  2206. return r;
  2207. atomic_inc(&vcpu->kvm->filp->f_count);
  2208. return fd;
  2209. }
  2210. /*
  2211. * Creates some virtual cpus. Good luck creating more than one.
  2212. */
  2213. static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
  2214. {
  2215. int r;
  2216. struct kvm_vcpu *vcpu;
  2217. if (!valid_vcpu(n))
  2218. return -EINVAL;
  2219. vcpu = kvm_x86_ops->vcpu_create(kvm, n);
  2220. if (IS_ERR(vcpu))
  2221. return PTR_ERR(vcpu);
  2222. preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
  2223. /* We do fxsave: this must be aligned. */
  2224. BUG_ON((unsigned long)&vcpu->host_fx_image & 0xF);
  2225. vcpu_load(vcpu);
  2226. r = kvm_mmu_setup(vcpu);
  2227. vcpu_put(vcpu);
  2228. if (r < 0)
  2229. goto free_vcpu;
  2230. mutex_lock(&kvm->lock);
  2231. if (kvm->vcpus[n]) {
  2232. r = -EEXIST;
  2233. mutex_unlock(&kvm->lock);
  2234. goto mmu_unload;
  2235. }
  2236. kvm->vcpus[n] = vcpu;
  2237. mutex_unlock(&kvm->lock);
  2238. /* Now it's all set up, let userspace reach it */
  2239. r = create_vcpu_fd(vcpu);
  2240. if (r < 0)
  2241. goto unlink;
  2242. return r;
  2243. unlink:
  2244. mutex_lock(&kvm->lock);
  2245. kvm->vcpus[n] = NULL;
  2246. mutex_unlock(&kvm->lock);
  2247. mmu_unload:
  2248. vcpu_load(vcpu);
  2249. kvm_mmu_unload(vcpu);
  2250. vcpu_put(vcpu);
  2251. free_vcpu:
  2252. kvm_x86_ops->vcpu_free(vcpu);
  2253. return r;
  2254. }
  2255. static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
  2256. {
  2257. u64 efer;
  2258. int i;
  2259. struct kvm_cpuid_entry *e, *entry;
  2260. rdmsrl(MSR_EFER, efer);
  2261. entry = NULL;
  2262. for (i = 0; i < vcpu->cpuid_nent; ++i) {
  2263. e = &vcpu->cpuid_entries[i];
  2264. if (e->function == 0x80000001) {
  2265. entry = e;
  2266. break;
  2267. }
  2268. }
  2269. if (entry && (entry->edx & (1 << 20)) && !(efer & EFER_NX)) {
  2270. entry->edx &= ~(1 << 20);
  2271. printk(KERN_INFO "kvm: guest NX capability removed\n");
  2272. }
  2273. }
  2274. static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
  2275. struct kvm_cpuid *cpuid,
  2276. struct kvm_cpuid_entry __user *entries)
  2277. {
  2278. int r;
  2279. r = -E2BIG;
  2280. if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
  2281. goto out;
  2282. r = -EFAULT;
  2283. if (copy_from_user(&vcpu->cpuid_entries, entries,
  2284. cpuid->nent * sizeof(struct kvm_cpuid_entry)))
  2285. goto out;
  2286. vcpu->cpuid_nent = cpuid->nent;
  2287. cpuid_fix_nx_cap(vcpu);
  2288. return 0;
  2289. out:
  2290. return r;
  2291. }
  2292. static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
  2293. {
  2294. if (sigset) {
  2295. sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
  2296. vcpu->sigset_active = 1;
  2297. vcpu->sigset = *sigset;
  2298. } else
  2299. vcpu->sigset_active = 0;
  2300. return 0;
  2301. }
  2302. /*
  2303. * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
  2304. * we have asm/x86/processor.h
  2305. */
  2306. struct fxsave {
  2307. u16 cwd;
  2308. u16 swd;
  2309. u16 twd;
  2310. u16 fop;
  2311. u64 rip;
  2312. u64 rdp;
  2313. u32 mxcsr;
  2314. u32 mxcsr_mask;
  2315. u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
  2316. #ifdef CONFIG_X86_64
  2317. u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
  2318. #else
  2319. u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
  2320. #endif
  2321. };
  2322. static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  2323. {
  2324. struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
  2325. vcpu_load(vcpu);
  2326. memcpy(fpu->fpr, fxsave->st_space, 128);
  2327. fpu->fcw = fxsave->cwd;
  2328. fpu->fsw = fxsave->swd;
  2329. fpu->ftwx = fxsave->twd;
  2330. fpu->last_opcode = fxsave->fop;
  2331. fpu->last_ip = fxsave->rip;
  2332. fpu->last_dp = fxsave->rdp;
  2333. memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
  2334. vcpu_put(vcpu);
  2335. return 0;
  2336. }
  2337. static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  2338. {
  2339. struct fxsave *fxsave = (struct fxsave *)&vcpu->guest_fx_image;
  2340. vcpu_load(vcpu);
  2341. memcpy(fxsave->st_space, fpu->fpr, 128);
  2342. fxsave->cwd = fpu->fcw;
  2343. fxsave->swd = fpu->fsw;
  2344. fxsave->twd = fpu->ftwx;
  2345. fxsave->fop = fpu->last_opcode;
  2346. fxsave->rip = fpu->last_ip;
  2347. fxsave->rdp = fpu->last_dp;
  2348. memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
  2349. vcpu_put(vcpu);
  2350. return 0;
  2351. }
  2352. static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
  2353. struct kvm_lapic_state *s)
  2354. {
  2355. vcpu_load(vcpu);
  2356. memcpy(s->regs, vcpu->apic->regs, sizeof *s);
  2357. vcpu_put(vcpu);
  2358. return 0;
  2359. }
  2360. static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
  2361. struct kvm_lapic_state *s)
  2362. {
  2363. vcpu_load(vcpu);
  2364. memcpy(vcpu->apic->regs, s->regs, sizeof *s);
  2365. kvm_apic_post_state_restore(vcpu);
  2366. vcpu_put(vcpu);
  2367. return 0;
  2368. }
  2369. static long kvm_vcpu_ioctl(struct file *filp,
  2370. unsigned int ioctl, unsigned long arg)
  2371. {
  2372. struct kvm_vcpu *vcpu = filp->private_data;
  2373. void __user *argp = (void __user *)arg;
  2374. int r = -EINVAL;
  2375. switch (ioctl) {
  2376. case KVM_RUN:
  2377. r = -EINVAL;
  2378. if (arg)
  2379. goto out;
  2380. r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
  2381. break;
  2382. case KVM_GET_REGS: {
  2383. struct kvm_regs kvm_regs;
  2384. memset(&kvm_regs, 0, sizeof kvm_regs);
  2385. r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
  2386. if (r)
  2387. goto out;
  2388. r = -EFAULT;
  2389. if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
  2390. goto out;
  2391. r = 0;
  2392. break;
  2393. }
  2394. case KVM_SET_REGS: {
  2395. struct kvm_regs kvm_regs;
  2396. r = -EFAULT;
  2397. if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
  2398. goto out;
  2399. r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
  2400. if (r)
  2401. goto out;
  2402. r = 0;
  2403. break;
  2404. }
  2405. case KVM_GET_SREGS: {
  2406. struct kvm_sregs kvm_sregs;
  2407. memset(&kvm_sregs, 0, sizeof kvm_sregs);
  2408. r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
  2409. if (r)
  2410. goto out;
  2411. r = -EFAULT;
  2412. if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
  2413. goto out;
  2414. r = 0;
  2415. break;
  2416. }
  2417. case KVM_SET_SREGS: {
  2418. struct kvm_sregs kvm_sregs;
  2419. r = -EFAULT;
  2420. if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
  2421. goto out;
  2422. r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
  2423. if (r)
  2424. goto out;
  2425. r = 0;
  2426. break;
  2427. }
  2428. case KVM_TRANSLATE: {
  2429. struct kvm_translation tr;
  2430. r = -EFAULT;
  2431. if (copy_from_user(&tr, argp, sizeof tr))
  2432. goto out;
  2433. r = kvm_vcpu_ioctl_translate(vcpu, &tr);
  2434. if (r)
  2435. goto out;
  2436. r = -EFAULT;
  2437. if (copy_to_user(argp, &tr, sizeof tr))
  2438. goto out;
  2439. r = 0;
  2440. break;
  2441. }
  2442. case KVM_INTERRUPT: {
  2443. struct kvm_interrupt irq;
  2444. r = -EFAULT;
  2445. if (copy_from_user(&irq, argp, sizeof irq))
  2446. goto out;
  2447. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  2448. if (r)
  2449. goto out;
  2450. r = 0;
  2451. break;
  2452. }
  2453. case KVM_DEBUG_GUEST: {
  2454. struct kvm_debug_guest dbg;
  2455. r = -EFAULT;
  2456. if (copy_from_user(&dbg, argp, sizeof dbg))
  2457. goto out;
  2458. r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
  2459. if (r)
  2460. goto out;
  2461. r = 0;
  2462. break;
  2463. }
  2464. case KVM_GET_MSRS:
  2465. r = msr_io(vcpu, argp, kvm_get_msr, 1);
  2466. break;
  2467. case KVM_SET_MSRS:
  2468. r = msr_io(vcpu, argp, do_set_msr, 0);
  2469. break;
  2470. case KVM_SET_CPUID: {
  2471. struct kvm_cpuid __user *cpuid_arg = argp;
  2472. struct kvm_cpuid cpuid;
  2473. r = -EFAULT;
  2474. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  2475. goto out;
  2476. r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
  2477. if (r)
  2478. goto out;
  2479. break;
  2480. }
  2481. case KVM_SET_SIGNAL_MASK: {
  2482. struct kvm_signal_mask __user *sigmask_arg = argp;
  2483. struct kvm_signal_mask kvm_sigmask;
  2484. sigset_t sigset, *p;
  2485. p = NULL;
  2486. if (argp) {
  2487. r = -EFAULT;
  2488. if (copy_from_user(&kvm_sigmask, argp,
  2489. sizeof kvm_sigmask))
  2490. goto out;
  2491. r = -EINVAL;
  2492. if (kvm_sigmask.len != sizeof sigset)
  2493. goto out;
  2494. r = -EFAULT;
  2495. if (copy_from_user(&sigset, sigmask_arg->sigset,
  2496. sizeof sigset))
  2497. goto out;
  2498. p = &sigset;
  2499. }
  2500. r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
  2501. break;
  2502. }
  2503. case KVM_GET_FPU: {
  2504. struct kvm_fpu fpu;
  2505. memset(&fpu, 0, sizeof fpu);
  2506. r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
  2507. if (r)
  2508. goto out;
  2509. r = -EFAULT;
  2510. if (copy_to_user(argp, &fpu, sizeof fpu))
  2511. goto out;
  2512. r = 0;
  2513. break;
  2514. }
  2515. case KVM_SET_FPU: {
  2516. struct kvm_fpu fpu;
  2517. r = -EFAULT;
  2518. if (copy_from_user(&fpu, argp, sizeof fpu))
  2519. goto out;
  2520. r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
  2521. if (r)
  2522. goto out;
  2523. r = 0;
  2524. break;
  2525. }
  2526. case KVM_GET_LAPIC: {
  2527. struct kvm_lapic_state lapic;
  2528. memset(&lapic, 0, sizeof lapic);
  2529. r = kvm_vcpu_ioctl_get_lapic(vcpu, &lapic);
  2530. if (r)
  2531. goto out;
  2532. r = -EFAULT;
  2533. if (copy_to_user(argp, &lapic, sizeof lapic))
  2534. goto out;
  2535. r = 0;
  2536. break;
  2537. }
  2538. case KVM_SET_LAPIC: {
  2539. struct kvm_lapic_state lapic;
  2540. r = -EFAULT;
  2541. if (copy_from_user(&lapic, argp, sizeof lapic))
  2542. goto out;
  2543. r = kvm_vcpu_ioctl_set_lapic(vcpu, &lapic);;
  2544. if (r)
  2545. goto out;
  2546. r = 0;
  2547. break;
  2548. }
  2549. default:
  2550. ;
  2551. }
  2552. out:
  2553. return r;
  2554. }
  2555. static long kvm_vm_ioctl(struct file *filp,
  2556. unsigned int ioctl, unsigned long arg)
  2557. {
  2558. struct kvm *kvm = filp->private_data;
  2559. void __user *argp = (void __user *)arg;
  2560. int r = -EINVAL;
  2561. switch (ioctl) {
  2562. case KVM_CREATE_VCPU:
  2563. r = kvm_vm_ioctl_create_vcpu(kvm, arg);
  2564. if (r < 0)
  2565. goto out;
  2566. break;
  2567. case KVM_SET_MEMORY_REGION: {
  2568. struct kvm_memory_region kvm_mem;
  2569. r = -EFAULT;
  2570. if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
  2571. goto out;
  2572. r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem);
  2573. if (r)
  2574. goto out;
  2575. break;
  2576. }
  2577. case KVM_GET_DIRTY_LOG: {
  2578. struct kvm_dirty_log log;
  2579. r = -EFAULT;
  2580. if (copy_from_user(&log, argp, sizeof log))
  2581. goto out;
  2582. r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
  2583. if (r)
  2584. goto out;
  2585. break;
  2586. }
  2587. case KVM_SET_MEMORY_ALIAS: {
  2588. struct kvm_memory_alias alias;
  2589. r = -EFAULT;
  2590. if (copy_from_user(&alias, argp, sizeof alias))
  2591. goto out;
  2592. r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
  2593. if (r)
  2594. goto out;
  2595. break;
  2596. }
  2597. case KVM_CREATE_IRQCHIP:
  2598. r = -ENOMEM;
  2599. kvm->vpic = kvm_create_pic(kvm);
  2600. if (kvm->vpic) {
  2601. r = kvm_ioapic_init(kvm);
  2602. if (r) {
  2603. kfree(kvm->vpic);
  2604. kvm->vpic = NULL;
  2605. goto out;
  2606. }
  2607. }
  2608. else
  2609. goto out;
  2610. break;
  2611. case KVM_IRQ_LINE: {
  2612. struct kvm_irq_level irq_event;
  2613. r = -EFAULT;
  2614. if (copy_from_user(&irq_event, argp, sizeof irq_event))
  2615. goto out;
  2616. if (irqchip_in_kernel(kvm)) {
  2617. mutex_lock(&kvm->lock);
  2618. if (irq_event.irq < 16)
  2619. kvm_pic_set_irq(pic_irqchip(kvm),
  2620. irq_event.irq,
  2621. irq_event.level);
  2622. kvm_ioapic_set_irq(kvm->vioapic,
  2623. irq_event.irq,
  2624. irq_event.level);
  2625. mutex_unlock(&kvm->lock);
  2626. r = 0;
  2627. }
  2628. break;
  2629. }
  2630. case KVM_GET_IRQCHIP: {
  2631. /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
  2632. struct kvm_irqchip chip;
  2633. r = -EFAULT;
  2634. if (copy_from_user(&chip, argp, sizeof chip))
  2635. goto out;
  2636. r = -ENXIO;
  2637. if (!irqchip_in_kernel(kvm))
  2638. goto out;
  2639. r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
  2640. if (r)
  2641. goto out;
  2642. r = -EFAULT;
  2643. if (copy_to_user(argp, &chip, sizeof chip))
  2644. goto out;
  2645. r = 0;
  2646. break;
  2647. }
  2648. case KVM_SET_IRQCHIP: {
  2649. /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
  2650. struct kvm_irqchip chip;
  2651. r = -EFAULT;
  2652. if (copy_from_user(&chip, argp, sizeof chip))
  2653. goto out;
  2654. r = -ENXIO;
  2655. if (!irqchip_in_kernel(kvm))
  2656. goto out;
  2657. r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
  2658. if (r)
  2659. goto out;
  2660. r = 0;
  2661. break;
  2662. }
  2663. default:
  2664. ;
  2665. }
  2666. out:
  2667. return r;
  2668. }
  2669. static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
  2670. unsigned long address,
  2671. int *type)
  2672. {
  2673. struct kvm *kvm = vma->vm_file->private_data;
  2674. unsigned long pgoff;
  2675. struct page *page;
  2676. pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  2677. page = gfn_to_page(kvm, pgoff);
  2678. if (!page)
  2679. return NOPAGE_SIGBUS;
  2680. get_page(page);
  2681. if (type != NULL)
  2682. *type = VM_FAULT_MINOR;
  2683. return page;
  2684. }
  2685. static struct vm_operations_struct kvm_vm_vm_ops = {
  2686. .nopage = kvm_vm_nopage,
  2687. };
  2688. static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
  2689. {
  2690. vma->vm_ops = &kvm_vm_vm_ops;
  2691. return 0;
  2692. }
  2693. static struct file_operations kvm_vm_fops = {
  2694. .release = kvm_vm_release,
  2695. .unlocked_ioctl = kvm_vm_ioctl,
  2696. .compat_ioctl = kvm_vm_ioctl,
  2697. .mmap = kvm_vm_mmap,
  2698. };
  2699. static int kvm_dev_ioctl_create_vm(void)
  2700. {
  2701. int fd, r;
  2702. struct inode *inode;
  2703. struct file *file;
  2704. struct kvm *kvm;
  2705. kvm = kvm_create_vm();
  2706. if (IS_ERR(kvm))
  2707. return PTR_ERR(kvm);
  2708. r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
  2709. if (r) {
  2710. kvm_destroy_vm(kvm);
  2711. return r;
  2712. }
  2713. kvm->filp = file;
  2714. return fd;
  2715. }
  2716. static long kvm_dev_ioctl(struct file *filp,
  2717. unsigned int ioctl, unsigned long arg)
  2718. {
  2719. void __user *argp = (void __user *)arg;
  2720. long r = -EINVAL;
  2721. switch (ioctl) {
  2722. case KVM_GET_API_VERSION:
  2723. r = -EINVAL;
  2724. if (arg)
  2725. goto out;
  2726. r = KVM_API_VERSION;
  2727. break;
  2728. case KVM_CREATE_VM:
  2729. r = -EINVAL;
  2730. if (arg)
  2731. goto out;
  2732. r = kvm_dev_ioctl_create_vm();
  2733. break;
  2734. case KVM_GET_MSR_INDEX_LIST: {
  2735. struct kvm_msr_list __user *user_msr_list = argp;
  2736. struct kvm_msr_list msr_list;
  2737. unsigned n;
  2738. r = -EFAULT;
  2739. if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
  2740. goto out;
  2741. n = msr_list.nmsrs;
  2742. msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
  2743. if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
  2744. goto out;
  2745. r = -E2BIG;
  2746. if (n < num_msrs_to_save)
  2747. goto out;
  2748. r = -EFAULT;
  2749. if (copy_to_user(user_msr_list->indices, &msrs_to_save,
  2750. num_msrs_to_save * sizeof(u32)))
  2751. goto out;
  2752. if (copy_to_user(user_msr_list->indices
  2753. + num_msrs_to_save * sizeof(u32),
  2754. &emulated_msrs,
  2755. ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
  2756. goto out;
  2757. r = 0;
  2758. break;
  2759. }
  2760. case KVM_CHECK_EXTENSION: {
  2761. int ext = (long)argp;
  2762. switch (ext) {
  2763. case KVM_CAP_IRQCHIP:
  2764. case KVM_CAP_HLT:
  2765. r = 1;
  2766. break;
  2767. default:
  2768. r = 0;
  2769. break;
  2770. }
  2771. break;
  2772. }
  2773. case KVM_GET_VCPU_MMAP_SIZE:
  2774. r = -EINVAL;
  2775. if (arg)
  2776. goto out;
  2777. r = 2 * PAGE_SIZE;
  2778. break;
  2779. default:
  2780. ;
  2781. }
  2782. out:
  2783. return r;
  2784. }
  2785. static struct file_operations kvm_chardev_ops = {
  2786. .unlocked_ioctl = kvm_dev_ioctl,
  2787. .compat_ioctl = kvm_dev_ioctl,
  2788. };
  2789. static struct miscdevice kvm_dev = {
  2790. KVM_MINOR,
  2791. "kvm",
  2792. &kvm_chardev_ops,
  2793. };
  2794. /*
  2795. * Make sure that a cpu that is being hot-unplugged does not have any vcpus
  2796. * cached on it.
  2797. */
  2798. static void decache_vcpus_on_cpu(int cpu)
  2799. {
  2800. struct kvm *vm;
  2801. struct kvm_vcpu *vcpu;
  2802. int i;
  2803. spin_lock(&kvm_lock);
  2804. list_for_each_entry(vm, &vm_list, vm_list)
  2805. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  2806. vcpu = vm->vcpus[i];
  2807. if (!vcpu)
  2808. continue;
  2809. /*
  2810. * If the vcpu is locked, then it is running on some
  2811. * other cpu and therefore it is not cached on the
  2812. * cpu in question.
  2813. *
  2814. * If it's not locked, check the last cpu it executed
  2815. * on.
  2816. */
  2817. if (mutex_trylock(&vcpu->mutex)) {
  2818. if (vcpu->cpu == cpu) {
  2819. kvm_x86_ops->vcpu_decache(vcpu);
  2820. vcpu->cpu = -1;
  2821. }
  2822. mutex_unlock(&vcpu->mutex);
  2823. }
  2824. }
  2825. spin_unlock(&kvm_lock);
  2826. }
  2827. static void hardware_enable(void *junk)
  2828. {
  2829. int cpu = raw_smp_processor_id();
  2830. if (cpu_isset(cpu, cpus_hardware_enabled))
  2831. return;
  2832. cpu_set(cpu, cpus_hardware_enabled);
  2833. kvm_x86_ops->hardware_enable(NULL);
  2834. }
  2835. static void hardware_disable(void *junk)
  2836. {
  2837. int cpu = raw_smp_processor_id();
  2838. if (!cpu_isset(cpu, cpus_hardware_enabled))
  2839. return;
  2840. cpu_clear(cpu, cpus_hardware_enabled);
  2841. decache_vcpus_on_cpu(cpu);
  2842. kvm_x86_ops->hardware_disable(NULL);
  2843. }
  2844. static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
  2845. void *v)
  2846. {
  2847. int cpu = (long)v;
  2848. switch (val) {
  2849. case CPU_DYING:
  2850. case CPU_DYING_FROZEN:
  2851. printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
  2852. cpu);
  2853. hardware_disable(NULL);
  2854. break;
  2855. case CPU_UP_CANCELED:
  2856. case CPU_UP_CANCELED_FROZEN:
  2857. printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
  2858. cpu);
  2859. smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
  2860. break;
  2861. case CPU_ONLINE:
  2862. case CPU_ONLINE_FROZEN:
  2863. printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
  2864. cpu);
  2865. smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
  2866. break;
  2867. }
  2868. return NOTIFY_OK;
  2869. }
  2870. static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
  2871. void *v)
  2872. {
  2873. if (val == SYS_RESTART) {
  2874. /*
  2875. * Some (well, at least mine) BIOSes hang on reboot if
  2876. * in vmx root mode.
  2877. */
  2878. printk(KERN_INFO "kvm: exiting hardware virtualization\n");
  2879. on_each_cpu(hardware_disable, NULL, 0, 1);
  2880. }
  2881. return NOTIFY_OK;
  2882. }
  2883. static struct notifier_block kvm_reboot_notifier = {
  2884. .notifier_call = kvm_reboot,
  2885. .priority = 0,
  2886. };
  2887. void kvm_io_bus_init(struct kvm_io_bus *bus)
  2888. {
  2889. memset(bus, 0, sizeof(*bus));
  2890. }
  2891. void kvm_io_bus_destroy(struct kvm_io_bus *bus)
  2892. {
  2893. int i;
  2894. for (i = 0; i < bus->dev_count; i++) {
  2895. struct kvm_io_device *pos = bus->devs[i];
  2896. kvm_iodevice_destructor(pos);
  2897. }
  2898. }
  2899. struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
  2900. {
  2901. int i;
  2902. for (i = 0; i < bus->dev_count; i++) {
  2903. struct kvm_io_device *pos = bus->devs[i];
  2904. if (pos->in_range(pos, addr))
  2905. return pos;
  2906. }
  2907. return NULL;
  2908. }
  2909. void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
  2910. {
  2911. BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
  2912. bus->devs[bus->dev_count++] = dev;
  2913. }
  2914. static struct notifier_block kvm_cpu_notifier = {
  2915. .notifier_call = kvm_cpu_hotplug,
  2916. .priority = 20, /* must be > scheduler priority */
  2917. };
  2918. static u64 stat_get(void *_offset)
  2919. {
  2920. unsigned offset = (long)_offset;
  2921. u64 total = 0;
  2922. struct kvm *kvm;
  2923. struct kvm_vcpu *vcpu;
  2924. int i;
  2925. spin_lock(&kvm_lock);
  2926. list_for_each_entry(kvm, &vm_list, vm_list)
  2927. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  2928. vcpu = kvm->vcpus[i];
  2929. if (vcpu)
  2930. total += *(u32 *)((void *)vcpu + offset);
  2931. }
  2932. spin_unlock(&kvm_lock);
  2933. return total;
  2934. }
  2935. DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, NULL, "%llu\n");
  2936. static __init void kvm_init_debug(void)
  2937. {
  2938. struct kvm_stats_debugfs_item *p;
  2939. debugfs_dir = debugfs_create_dir("kvm", NULL);
  2940. for (p = debugfs_entries; p->name; ++p)
  2941. p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
  2942. (void *)(long)p->offset,
  2943. &stat_fops);
  2944. }
  2945. static void kvm_exit_debug(void)
  2946. {
  2947. struct kvm_stats_debugfs_item *p;
  2948. for (p = debugfs_entries; p->name; ++p)
  2949. debugfs_remove(p->dentry);
  2950. debugfs_remove(debugfs_dir);
  2951. }
  2952. static int kvm_suspend(struct sys_device *dev, pm_message_t state)
  2953. {
  2954. hardware_disable(NULL);
  2955. return 0;
  2956. }
  2957. static int kvm_resume(struct sys_device *dev)
  2958. {
  2959. hardware_enable(NULL);
  2960. return 0;
  2961. }
  2962. static struct sysdev_class kvm_sysdev_class = {
  2963. set_kset_name("kvm"),
  2964. .suspend = kvm_suspend,
  2965. .resume = kvm_resume,
  2966. };
  2967. static struct sys_device kvm_sysdev = {
  2968. .id = 0,
  2969. .cls = &kvm_sysdev_class,
  2970. };
  2971. hpa_t bad_page_address;
  2972. static inline
  2973. struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
  2974. {
  2975. return container_of(pn, struct kvm_vcpu, preempt_notifier);
  2976. }
  2977. static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
  2978. {
  2979. struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
  2980. kvm_x86_ops->vcpu_load(vcpu, cpu);
  2981. }
  2982. static void kvm_sched_out(struct preempt_notifier *pn,
  2983. struct task_struct *next)
  2984. {
  2985. struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
  2986. kvm_x86_ops->vcpu_put(vcpu);
  2987. }
  2988. int kvm_init_x86(struct kvm_x86_ops *ops, unsigned int vcpu_size,
  2989. struct module *module)
  2990. {
  2991. int r;
  2992. int cpu;
  2993. if (kvm_x86_ops) {
  2994. printk(KERN_ERR "kvm: already loaded the other module\n");
  2995. return -EEXIST;
  2996. }
  2997. if (!ops->cpu_has_kvm_support()) {
  2998. printk(KERN_ERR "kvm: no hardware support\n");
  2999. return -EOPNOTSUPP;
  3000. }
  3001. if (ops->disabled_by_bios()) {
  3002. printk(KERN_ERR "kvm: disabled by bios\n");
  3003. return -EOPNOTSUPP;
  3004. }
  3005. kvm_x86_ops = ops;
  3006. r = kvm_x86_ops->hardware_setup();
  3007. if (r < 0)
  3008. goto out;
  3009. for_each_online_cpu(cpu) {
  3010. smp_call_function_single(cpu,
  3011. kvm_x86_ops->check_processor_compatibility,
  3012. &r, 0, 1);
  3013. if (r < 0)
  3014. goto out_free_0;
  3015. }
  3016. on_each_cpu(hardware_enable, NULL, 0, 1);
  3017. r = register_cpu_notifier(&kvm_cpu_notifier);
  3018. if (r)
  3019. goto out_free_1;
  3020. register_reboot_notifier(&kvm_reboot_notifier);
  3021. r = sysdev_class_register(&kvm_sysdev_class);
  3022. if (r)
  3023. goto out_free_2;
  3024. r = sysdev_register(&kvm_sysdev);
  3025. if (r)
  3026. goto out_free_3;
  3027. /* A kmem cache lets us meet the alignment requirements of fx_save. */
  3028. kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
  3029. __alignof__(struct kvm_vcpu), 0, 0);
  3030. if (!kvm_vcpu_cache) {
  3031. r = -ENOMEM;
  3032. goto out_free_4;
  3033. }
  3034. kvm_chardev_ops.owner = module;
  3035. r = misc_register(&kvm_dev);
  3036. if (r) {
  3037. printk (KERN_ERR "kvm: misc device register failed\n");
  3038. goto out_free;
  3039. }
  3040. kvm_preempt_ops.sched_in = kvm_sched_in;
  3041. kvm_preempt_ops.sched_out = kvm_sched_out;
  3042. return r;
  3043. out_free:
  3044. kmem_cache_destroy(kvm_vcpu_cache);
  3045. out_free_4:
  3046. sysdev_unregister(&kvm_sysdev);
  3047. out_free_3:
  3048. sysdev_class_unregister(&kvm_sysdev_class);
  3049. out_free_2:
  3050. unregister_reboot_notifier(&kvm_reboot_notifier);
  3051. unregister_cpu_notifier(&kvm_cpu_notifier);
  3052. out_free_1:
  3053. on_each_cpu(hardware_disable, NULL, 0, 1);
  3054. out_free_0:
  3055. kvm_x86_ops->hardware_unsetup();
  3056. out:
  3057. kvm_x86_ops = NULL;
  3058. return r;
  3059. }
  3060. void kvm_exit_x86(void)
  3061. {
  3062. misc_deregister(&kvm_dev);
  3063. kmem_cache_destroy(kvm_vcpu_cache);
  3064. sysdev_unregister(&kvm_sysdev);
  3065. sysdev_class_unregister(&kvm_sysdev_class);
  3066. unregister_reboot_notifier(&kvm_reboot_notifier);
  3067. unregister_cpu_notifier(&kvm_cpu_notifier);
  3068. on_each_cpu(hardware_disable, NULL, 0, 1);
  3069. kvm_x86_ops->hardware_unsetup();
  3070. kvm_x86_ops = NULL;
  3071. }
  3072. static __init int kvm_init(void)
  3073. {
  3074. static struct page *bad_page;
  3075. int r;
  3076. r = kvm_mmu_module_init();
  3077. if (r)
  3078. goto out4;
  3079. kvm_init_debug();
  3080. kvm_init_msr_list();
  3081. if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
  3082. r = -ENOMEM;
  3083. goto out;
  3084. }
  3085. bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
  3086. memset(__va(bad_page_address), 0, PAGE_SIZE);
  3087. return 0;
  3088. out:
  3089. kvm_exit_debug();
  3090. kvm_mmu_module_exit();
  3091. out4:
  3092. return r;
  3093. }
  3094. static __exit void kvm_exit(void)
  3095. {
  3096. kvm_exit_debug();
  3097. __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
  3098. kvm_mmu_module_exit();
  3099. }
  3100. module_init(kvm_init)
  3101. module_exit(kvm_exit)
  3102. EXPORT_SYMBOL_GPL(kvm_init_x86);
  3103. EXPORT_SYMBOL_GPL(kvm_exit_x86);