x86.c 73 KB

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