x86.c 80 KB

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