kvm_main.c 80 KB

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