x86.c 76 KB

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