x86.c 73 KB

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