x86.c 82 KB

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