x86.c 77 KB

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