x86.c 75 KB

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