x86.c 105 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373
  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. * Copyright (C) 2008 Qumranet, Inc.
  8. * Copyright IBM Corporation, 2008
  9. *
  10. * Authors:
  11. * Avi Kivity <avi@qumranet.com>
  12. * Yaniv Kamay <yaniv@qumranet.com>
  13. * Amit Shah <amit.shah@qumranet.com>
  14. * Ben-Ami Yassour <benami@il.ibm.com>
  15. *
  16. * This work is licensed under the terms of the GNU GPL, version 2. See
  17. * the COPYING file in the top-level directory.
  18. *
  19. */
  20. #include <linux/kvm_host.h>
  21. #include "irq.h"
  22. #include "mmu.h"
  23. #include "i8254.h"
  24. #include "tss.h"
  25. #include "kvm_cache_regs.h"
  26. #include "x86.h"
  27. #include <linux/clocksource.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/kvm.h>
  30. #include <linux/fs.h>
  31. #include <linux/pci.h>
  32. #include <linux/vmalloc.h>
  33. #include <linux/module.h>
  34. #include <linux/mman.h>
  35. #include <linux/highmem.h>
  36. #include <asm/uaccess.h>
  37. #include <asm/msr.h>
  38. #include <asm/desc.h>
  39. #define MAX_IO_MSRS 256
  40. #define CR0_RESERVED_BITS \
  41. (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
  42. | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
  43. | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
  44. #define CR4_RESERVED_BITS \
  45. (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
  46. | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
  47. | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
  48. | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
  49. #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
  50. /* EFER defaults:
  51. * - enable syscall per default because its emulated by KVM
  52. * - enable LME and LMA per default on 64 bit KVM
  53. */
  54. #ifdef CONFIG_X86_64
  55. static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffafeULL;
  56. #else
  57. static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffffeULL;
  58. #endif
  59. #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
  60. #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
  61. static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
  62. struct kvm_cpuid_entry2 __user *entries);
  63. struct kvm_x86_ops *kvm_x86_ops;
  64. EXPORT_SYMBOL_GPL(kvm_x86_ops);
  65. struct kvm_stats_debugfs_item debugfs_entries[] = {
  66. { "pf_fixed", VCPU_STAT(pf_fixed) },
  67. { "pf_guest", VCPU_STAT(pf_guest) },
  68. { "tlb_flush", VCPU_STAT(tlb_flush) },
  69. { "invlpg", VCPU_STAT(invlpg) },
  70. { "exits", VCPU_STAT(exits) },
  71. { "io_exits", VCPU_STAT(io_exits) },
  72. { "mmio_exits", VCPU_STAT(mmio_exits) },
  73. { "signal_exits", VCPU_STAT(signal_exits) },
  74. { "irq_window", VCPU_STAT(irq_window_exits) },
  75. { "nmi_window", VCPU_STAT(nmi_window_exits) },
  76. { "halt_exits", VCPU_STAT(halt_exits) },
  77. { "halt_wakeup", VCPU_STAT(halt_wakeup) },
  78. { "hypercalls", VCPU_STAT(hypercalls) },
  79. { "request_irq", VCPU_STAT(request_irq_exits) },
  80. { "irq_exits", VCPU_STAT(irq_exits) },
  81. { "host_state_reload", VCPU_STAT(host_state_reload) },
  82. { "efer_reload", VCPU_STAT(efer_reload) },
  83. { "fpu_reload", VCPU_STAT(fpu_reload) },
  84. { "insn_emulation", VCPU_STAT(insn_emulation) },
  85. { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
  86. { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
  87. { "mmu_pte_write", VM_STAT(mmu_pte_write) },
  88. { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
  89. { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
  90. { "mmu_flooded", VM_STAT(mmu_flooded) },
  91. { "mmu_recycled", VM_STAT(mmu_recycled) },
  92. { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
  93. { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
  94. { "largepages", VM_STAT(lpages) },
  95. { NULL }
  96. };
  97. static struct kvm_assigned_dev_kernel *kvm_find_assigned_dev(struct list_head *head,
  98. int assigned_dev_id)
  99. {
  100. struct list_head *ptr;
  101. struct kvm_assigned_dev_kernel *match;
  102. list_for_each(ptr, head) {
  103. match = list_entry(ptr, struct kvm_assigned_dev_kernel, list);
  104. if (match->assigned_dev_id == assigned_dev_id)
  105. return match;
  106. }
  107. return NULL;
  108. }
  109. static void kvm_assigned_dev_interrupt_work_handler(struct work_struct *work)
  110. {
  111. struct kvm_assigned_dev_kernel *assigned_dev;
  112. assigned_dev = container_of(work, struct kvm_assigned_dev_kernel,
  113. interrupt_work);
  114. /* This is taken to safely inject irq inside the guest. When
  115. * the interrupt injection (or the ioapic code) uses a
  116. * finer-grained lock, update this
  117. */
  118. mutex_lock(&assigned_dev->kvm->lock);
  119. kvm_set_irq(assigned_dev->kvm,
  120. assigned_dev->guest_irq, 1);
  121. mutex_unlock(&assigned_dev->kvm->lock);
  122. kvm_put_kvm(assigned_dev->kvm);
  123. }
  124. /* FIXME: Implement the OR logic needed to make shared interrupts on
  125. * this line behave properly
  126. */
  127. static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id)
  128. {
  129. struct kvm_assigned_dev_kernel *assigned_dev =
  130. (struct kvm_assigned_dev_kernel *) dev_id;
  131. kvm_get_kvm(assigned_dev->kvm);
  132. schedule_work(&assigned_dev->interrupt_work);
  133. disable_irq_nosync(irq);
  134. return IRQ_HANDLED;
  135. }
  136. /* Ack the irq line for an assigned device */
  137. static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
  138. {
  139. struct kvm_assigned_dev_kernel *dev;
  140. if (kian->gsi == -1)
  141. return;
  142. dev = container_of(kian, struct kvm_assigned_dev_kernel,
  143. ack_notifier);
  144. kvm_set_irq(dev->kvm, dev->guest_irq, 0);
  145. enable_irq(dev->host_irq);
  146. }
  147. static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
  148. struct kvm_assigned_irq
  149. *assigned_irq)
  150. {
  151. int r = 0;
  152. struct kvm_assigned_dev_kernel *match;
  153. mutex_lock(&kvm->lock);
  154. match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
  155. assigned_irq->assigned_dev_id);
  156. if (!match) {
  157. mutex_unlock(&kvm->lock);
  158. return -EINVAL;
  159. }
  160. if (match->irq_requested) {
  161. match->guest_irq = assigned_irq->guest_irq;
  162. match->ack_notifier.gsi = assigned_irq->guest_irq;
  163. mutex_unlock(&kvm->lock);
  164. return 0;
  165. }
  166. INIT_WORK(&match->interrupt_work,
  167. kvm_assigned_dev_interrupt_work_handler);
  168. if (irqchip_in_kernel(kvm)) {
  169. if (!capable(CAP_SYS_RAWIO)) {
  170. return -EPERM;
  171. goto out;
  172. }
  173. if (assigned_irq->host_irq)
  174. match->host_irq = assigned_irq->host_irq;
  175. else
  176. match->host_irq = match->dev->irq;
  177. match->guest_irq = assigned_irq->guest_irq;
  178. match->ack_notifier.gsi = assigned_irq->guest_irq;
  179. match->ack_notifier.irq_acked = kvm_assigned_dev_ack_irq;
  180. kvm_register_irq_ack_notifier(kvm, &match->ack_notifier);
  181. /* Even though this is PCI, we don't want to use shared
  182. * interrupts. Sharing host devices with guest-assigned devices
  183. * on the same interrupt line is not a happy situation: there
  184. * are going to be long delays in accepting, acking, etc.
  185. */
  186. if (request_irq(match->host_irq, kvm_assigned_dev_intr, 0,
  187. "kvm_assigned_device", (void *)match)) {
  188. printk(KERN_INFO "%s: couldn't allocate irq for pv "
  189. "device\n", __func__);
  190. r = -EIO;
  191. goto out;
  192. }
  193. }
  194. match->irq_requested = true;
  195. out:
  196. mutex_unlock(&kvm->lock);
  197. return r;
  198. }
  199. static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
  200. struct kvm_assigned_pci_dev *assigned_dev)
  201. {
  202. int r = 0;
  203. struct kvm_assigned_dev_kernel *match;
  204. struct pci_dev *dev;
  205. mutex_lock(&kvm->lock);
  206. match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
  207. assigned_dev->assigned_dev_id);
  208. if (match) {
  209. /* device already assigned */
  210. r = -EINVAL;
  211. goto out;
  212. }
  213. match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL);
  214. if (match == NULL) {
  215. printk(KERN_INFO "%s: Couldn't allocate memory\n",
  216. __func__);
  217. r = -ENOMEM;
  218. goto out;
  219. }
  220. dev = pci_get_bus_and_slot(assigned_dev->busnr,
  221. assigned_dev->devfn);
  222. if (!dev) {
  223. printk(KERN_INFO "%s: host device not found\n", __func__);
  224. r = -EINVAL;
  225. goto out_free;
  226. }
  227. if (pci_enable_device(dev)) {
  228. printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
  229. r = -EBUSY;
  230. goto out_put;
  231. }
  232. r = pci_request_regions(dev, "kvm_assigned_device");
  233. if (r) {
  234. printk(KERN_INFO "%s: Could not get access to device regions\n",
  235. __func__);
  236. goto out_disable;
  237. }
  238. match->assigned_dev_id = assigned_dev->assigned_dev_id;
  239. match->host_busnr = assigned_dev->busnr;
  240. match->host_devfn = assigned_dev->devfn;
  241. match->dev = dev;
  242. match->kvm = kvm;
  243. list_add(&match->list, &kvm->arch.assigned_dev_head);
  244. out:
  245. mutex_unlock(&kvm->lock);
  246. return r;
  247. out_disable:
  248. pci_disable_device(dev);
  249. out_put:
  250. pci_dev_put(dev);
  251. out_free:
  252. kfree(match);
  253. mutex_unlock(&kvm->lock);
  254. return r;
  255. }
  256. static void kvm_free_assigned_devices(struct kvm *kvm)
  257. {
  258. struct list_head *ptr, *ptr2;
  259. struct kvm_assigned_dev_kernel *assigned_dev;
  260. list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) {
  261. assigned_dev = list_entry(ptr,
  262. struct kvm_assigned_dev_kernel,
  263. list);
  264. if (irqchip_in_kernel(kvm) && assigned_dev->irq_requested) {
  265. free_irq(assigned_dev->host_irq,
  266. (void *)assigned_dev);
  267. kvm_unregister_irq_ack_notifier(kvm,
  268. &assigned_dev->
  269. ack_notifier);
  270. }
  271. if (cancel_work_sync(&assigned_dev->interrupt_work))
  272. /* We had pending work. That means we will have to take
  273. * care of kvm_put_kvm.
  274. */
  275. kvm_put_kvm(kvm);
  276. pci_release_regions(assigned_dev->dev);
  277. pci_disable_device(assigned_dev->dev);
  278. pci_dev_put(assigned_dev->dev);
  279. list_del(&assigned_dev->list);
  280. kfree(assigned_dev);
  281. }
  282. }
  283. unsigned long segment_base(u16 selector)
  284. {
  285. struct descriptor_table gdt;
  286. struct desc_struct *d;
  287. unsigned long table_base;
  288. unsigned long v;
  289. if (selector == 0)
  290. return 0;
  291. asm("sgdt %0" : "=m"(gdt));
  292. table_base = gdt.base;
  293. if (selector & 4) { /* from ldt */
  294. u16 ldt_selector;
  295. asm("sldt %0" : "=g"(ldt_selector));
  296. table_base = segment_base(ldt_selector);
  297. }
  298. d = (struct desc_struct *)(table_base + (selector & ~7));
  299. v = d->base0 | ((unsigned long)d->base1 << 16) |
  300. ((unsigned long)d->base2 << 24);
  301. #ifdef CONFIG_X86_64
  302. if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
  303. v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
  304. #endif
  305. return v;
  306. }
  307. EXPORT_SYMBOL_GPL(segment_base);
  308. u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
  309. {
  310. if (irqchip_in_kernel(vcpu->kvm))
  311. return vcpu->arch.apic_base;
  312. else
  313. return vcpu->arch.apic_base;
  314. }
  315. EXPORT_SYMBOL_GPL(kvm_get_apic_base);
  316. void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
  317. {
  318. /* TODO: reserve bits check */
  319. if (irqchip_in_kernel(vcpu->kvm))
  320. kvm_lapic_set_base(vcpu, data);
  321. else
  322. vcpu->arch.apic_base = data;
  323. }
  324. EXPORT_SYMBOL_GPL(kvm_set_apic_base);
  325. void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
  326. {
  327. WARN_ON(vcpu->arch.exception.pending);
  328. vcpu->arch.exception.pending = true;
  329. vcpu->arch.exception.has_error_code = false;
  330. vcpu->arch.exception.nr = nr;
  331. }
  332. EXPORT_SYMBOL_GPL(kvm_queue_exception);
  333. void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr,
  334. u32 error_code)
  335. {
  336. ++vcpu->stat.pf_guest;
  337. if (vcpu->arch.exception.pending) {
  338. if (vcpu->arch.exception.nr == PF_VECTOR) {
  339. printk(KERN_DEBUG "kvm: inject_page_fault:"
  340. " double fault 0x%lx\n", addr);
  341. vcpu->arch.exception.nr = DF_VECTOR;
  342. vcpu->arch.exception.error_code = 0;
  343. } else if (vcpu->arch.exception.nr == DF_VECTOR) {
  344. /* triple fault -> shutdown */
  345. set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
  346. }
  347. return;
  348. }
  349. vcpu->arch.cr2 = addr;
  350. kvm_queue_exception_e(vcpu, PF_VECTOR, error_code);
  351. }
  352. void kvm_inject_nmi(struct kvm_vcpu *vcpu)
  353. {
  354. vcpu->arch.nmi_pending = 1;
  355. }
  356. EXPORT_SYMBOL_GPL(kvm_inject_nmi);
  357. void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
  358. {
  359. WARN_ON(vcpu->arch.exception.pending);
  360. vcpu->arch.exception.pending = true;
  361. vcpu->arch.exception.has_error_code = true;
  362. vcpu->arch.exception.nr = nr;
  363. vcpu->arch.exception.error_code = error_code;
  364. }
  365. EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
  366. static void __queue_exception(struct kvm_vcpu *vcpu)
  367. {
  368. kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
  369. vcpu->arch.exception.has_error_code,
  370. vcpu->arch.exception.error_code);
  371. }
  372. /*
  373. * Load the pae pdptrs. Return true is they are all valid.
  374. */
  375. int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
  376. {
  377. gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
  378. unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
  379. int i;
  380. int ret;
  381. u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
  382. ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
  383. offset * sizeof(u64), sizeof(pdpte));
  384. if (ret < 0) {
  385. ret = 0;
  386. goto out;
  387. }
  388. for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
  389. if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) {
  390. ret = 0;
  391. goto out;
  392. }
  393. }
  394. ret = 1;
  395. memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs));
  396. out:
  397. return ret;
  398. }
  399. EXPORT_SYMBOL_GPL(load_pdptrs);
  400. static bool pdptrs_changed(struct kvm_vcpu *vcpu)
  401. {
  402. u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
  403. bool changed = true;
  404. int r;
  405. if (is_long_mode(vcpu) || !is_pae(vcpu))
  406. return false;
  407. r = kvm_read_guest(vcpu->kvm, vcpu->arch.cr3 & ~31u, pdpte, sizeof(pdpte));
  408. if (r < 0)
  409. goto out;
  410. changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0;
  411. out:
  412. return changed;
  413. }
  414. void kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  415. {
  416. if (cr0 & CR0_RESERVED_BITS) {
  417. printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
  418. cr0, vcpu->arch.cr0);
  419. kvm_inject_gp(vcpu, 0);
  420. return;
  421. }
  422. if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
  423. printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
  424. kvm_inject_gp(vcpu, 0);
  425. return;
  426. }
  427. if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
  428. printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
  429. "and a clear PE flag\n");
  430. kvm_inject_gp(vcpu, 0);
  431. return;
  432. }
  433. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
  434. #ifdef CONFIG_X86_64
  435. if ((vcpu->arch.shadow_efer & EFER_LME)) {
  436. int cs_db, cs_l;
  437. if (!is_pae(vcpu)) {
  438. printk(KERN_DEBUG "set_cr0: #GP, start paging "
  439. "in long mode while PAE is disabled\n");
  440. kvm_inject_gp(vcpu, 0);
  441. return;
  442. }
  443. kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  444. if (cs_l) {
  445. printk(KERN_DEBUG "set_cr0: #GP, start paging "
  446. "in long mode while CS.L == 1\n");
  447. kvm_inject_gp(vcpu, 0);
  448. return;
  449. }
  450. } else
  451. #endif
  452. if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
  453. printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
  454. "reserved bits\n");
  455. kvm_inject_gp(vcpu, 0);
  456. return;
  457. }
  458. }
  459. kvm_x86_ops->set_cr0(vcpu, cr0);
  460. vcpu->arch.cr0 = cr0;
  461. kvm_mmu_reset_context(vcpu);
  462. return;
  463. }
  464. EXPORT_SYMBOL_GPL(kvm_set_cr0);
  465. void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
  466. {
  467. kvm_set_cr0(vcpu, (vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f));
  468. KVMTRACE_1D(LMSW, vcpu,
  469. (u32)((vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f)),
  470. handler);
  471. }
  472. EXPORT_SYMBOL_GPL(kvm_lmsw);
  473. void kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  474. {
  475. if (cr4 & CR4_RESERVED_BITS) {
  476. printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
  477. kvm_inject_gp(vcpu, 0);
  478. return;
  479. }
  480. if (is_long_mode(vcpu)) {
  481. if (!(cr4 & X86_CR4_PAE)) {
  482. printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
  483. "in long mode\n");
  484. kvm_inject_gp(vcpu, 0);
  485. return;
  486. }
  487. } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
  488. && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
  489. printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
  490. kvm_inject_gp(vcpu, 0);
  491. return;
  492. }
  493. if (cr4 & X86_CR4_VMXE) {
  494. printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
  495. kvm_inject_gp(vcpu, 0);
  496. return;
  497. }
  498. kvm_x86_ops->set_cr4(vcpu, cr4);
  499. vcpu->arch.cr4 = cr4;
  500. kvm_mmu_reset_context(vcpu);
  501. }
  502. EXPORT_SYMBOL_GPL(kvm_set_cr4);
  503. void kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
  504. {
  505. if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) {
  506. kvm_mmu_flush_tlb(vcpu);
  507. return;
  508. }
  509. if (is_long_mode(vcpu)) {
  510. if (cr3 & CR3_L_MODE_RESERVED_BITS) {
  511. printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
  512. kvm_inject_gp(vcpu, 0);
  513. return;
  514. }
  515. } else {
  516. if (is_pae(vcpu)) {
  517. if (cr3 & CR3_PAE_RESERVED_BITS) {
  518. printk(KERN_DEBUG
  519. "set_cr3: #GP, reserved bits\n");
  520. kvm_inject_gp(vcpu, 0);
  521. return;
  522. }
  523. if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
  524. printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
  525. "reserved bits\n");
  526. kvm_inject_gp(vcpu, 0);
  527. return;
  528. }
  529. }
  530. /*
  531. * We don't check reserved bits in nonpae mode, because
  532. * this isn't enforced, and VMware depends on this.
  533. */
  534. }
  535. /*
  536. * Does the new cr3 value map to physical memory? (Note, we
  537. * catch an invalid cr3 even in real-mode, because it would
  538. * cause trouble later on when we turn on paging anyway.)
  539. *
  540. * A real CPU would silently accept an invalid cr3 and would
  541. * attempt to use it - with largely undefined (and often hard
  542. * to debug) behavior on the guest side.
  543. */
  544. if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
  545. kvm_inject_gp(vcpu, 0);
  546. else {
  547. vcpu->arch.cr3 = cr3;
  548. vcpu->arch.mmu.new_cr3(vcpu);
  549. }
  550. }
  551. EXPORT_SYMBOL_GPL(kvm_set_cr3);
  552. void kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
  553. {
  554. if (cr8 & CR8_RESERVED_BITS) {
  555. printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
  556. kvm_inject_gp(vcpu, 0);
  557. return;
  558. }
  559. if (irqchip_in_kernel(vcpu->kvm))
  560. kvm_lapic_set_tpr(vcpu, cr8);
  561. else
  562. vcpu->arch.cr8 = cr8;
  563. }
  564. EXPORT_SYMBOL_GPL(kvm_set_cr8);
  565. unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
  566. {
  567. if (irqchip_in_kernel(vcpu->kvm))
  568. return kvm_lapic_get_cr8(vcpu);
  569. else
  570. return vcpu->arch.cr8;
  571. }
  572. EXPORT_SYMBOL_GPL(kvm_get_cr8);
  573. /*
  574. * List of msr numbers which we expose to userspace through KVM_GET_MSRS
  575. * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
  576. *
  577. * This list is modified at module load time to reflect the
  578. * capabilities of the host cpu.
  579. */
  580. static u32 msrs_to_save[] = {
  581. MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
  582. MSR_K6_STAR,
  583. #ifdef CONFIG_X86_64
  584. MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
  585. #endif
  586. MSR_IA32_TIME_STAMP_COUNTER, MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
  587. MSR_IA32_PERF_STATUS,
  588. };
  589. static unsigned num_msrs_to_save;
  590. static u32 emulated_msrs[] = {
  591. MSR_IA32_MISC_ENABLE,
  592. };
  593. static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
  594. {
  595. if (efer & efer_reserved_bits) {
  596. printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
  597. efer);
  598. kvm_inject_gp(vcpu, 0);
  599. return;
  600. }
  601. if (is_paging(vcpu)
  602. && (vcpu->arch.shadow_efer & EFER_LME) != (efer & EFER_LME)) {
  603. printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
  604. kvm_inject_gp(vcpu, 0);
  605. return;
  606. }
  607. kvm_x86_ops->set_efer(vcpu, efer);
  608. efer &= ~EFER_LMA;
  609. efer |= vcpu->arch.shadow_efer & EFER_LMA;
  610. vcpu->arch.shadow_efer = efer;
  611. }
  612. void kvm_enable_efer_bits(u64 mask)
  613. {
  614. efer_reserved_bits &= ~mask;
  615. }
  616. EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
  617. /*
  618. * Writes msr value into into the appropriate "register".
  619. * Returns 0 on success, non-0 otherwise.
  620. * Assumes vcpu_load() was already called.
  621. */
  622. int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
  623. {
  624. return kvm_x86_ops->set_msr(vcpu, msr_index, data);
  625. }
  626. /*
  627. * Adapt set_msr() to msr_io()'s calling convention
  628. */
  629. static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
  630. {
  631. return kvm_set_msr(vcpu, index, *data);
  632. }
  633. static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
  634. {
  635. static int version;
  636. struct pvclock_wall_clock wc;
  637. struct timespec now, sys, boot;
  638. if (!wall_clock)
  639. return;
  640. version++;
  641. kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
  642. /*
  643. * The guest calculates current wall clock time by adding
  644. * system time (updated by kvm_write_guest_time below) to the
  645. * wall clock specified here. guest system time equals host
  646. * system time for us, thus we must fill in host boot time here.
  647. */
  648. now = current_kernel_time();
  649. ktime_get_ts(&sys);
  650. boot = ns_to_timespec(timespec_to_ns(&now) - timespec_to_ns(&sys));
  651. wc.sec = boot.tv_sec;
  652. wc.nsec = boot.tv_nsec;
  653. wc.version = version;
  654. kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
  655. version++;
  656. kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
  657. }
  658. static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
  659. {
  660. uint32_t quotient, remainder;
  661. /* Don't try to replace with do_div(), this one calculates
  662. * "(dividend << 32) / divisor" */
  663. __asm__ ( "divl %4"
  664. : "=a" (quotient), "=d" (remainder)
  665. : "0" (0), "1" (dividend), "r" (divisor) );
  666. return quotient;
  667. }
  668. static void kvm_set_time_scale(uint32_t tsc_khz, struct pvclock_vcpu_time_info *hv_clock)
  669. {
  670. uint64_t nsecs = 1000000000LL;
  671. int32_t shift = 0;
  672. uint64_t tps64;
  673. uint32_t tps32;
  674. tps64 = tsc_khz * 1000LL;
  675. while (tps64 > nsecs*2) {
  676. tps64 >>= 1;
  677. shift--;
  678. }
  679. tps32 = (uint32_t)tps64;
  680. while (tps32 <= (uint32_t)nsecs) {
  681. tps32 <<= 1;
  682. shift++;
  683. }
  684. hv_clock->tsc_shift = shift;
  685. hv_clock->tsc_to_system_mul = div_frac(nsecs, tps32);
  686. pr_debug("%s: tsc_khz %u, tsc_shift %d, tsc_mul %u\n",
  687. __FUNCTION__, tsc_khz, hv_clock->tsc_shift,
  688. hv_clock->tsc_to_system_mul);
  689. }
  690. static void kvm_write_guest_time(struct kvm_vcpu *v)
  691. {
  692. struct timespec ts;
  693. unsigned long flags;
  694. struct kvm_vcpu_arch *vcpu = &v->arch;
  695. void *shared_kaddr;
  696. if ((!vcpu->time_page))
  697. return;
  698. if (unlikely(vcpu->hv_clock_tsc_khz != tsc_khz)) {
  699. kvm_set_time_scale(tsc_khz, &vcpu->hv_clock);
  700. vcpu->hv_clock_tsc_khz = tsc_khz;
  701. }
  702. /* Keep irq disabled to prevent changes to the clock */
  703. local_irq_save(flags);
  704. kvm_get_msr(v, MSR_IA32_TIME_STAMP_COUNTER,
  705. &vcpu->hv_clock.tsc_timestamp);
  706. ktime_get_ts(&ts);
  707. local_irq_restore(flags);
  708. /* With all the info we got, fill in the values */
  709. vcpu->hv_clock.system_time = ts.tv_nsec +
  710. (NSEC_PER_SEC * (u64)ts.tv_sec);
  711. /*
  712. * The interface expects us to write an even number signaling that the
  713. * update is finished. Since the guest won't see the intermediate
  714. * state, we just increase by 2 at the end.
  715. */
  716. vcpu->hv_clock.version += 2;
  717. shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0);
  718. memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock,
  719. sizeof(vcpu->hv_clock));
  720. kunmap_atomic(shared_kaddr, KM_USER0);
  721. mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT);
  722. }
  723. static bool msr_mtrr_valid(unsigned msr)
  724. {
  725. switch (msr) {
  726. case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR - 1:
  727. case MSR_MTRRfix64K_00000:
  728. case MSR_MTRRfix16K_80000:
  729. case MSR_MTRRfix16K_A0000:
  730. case MSR_MTRRfix4K_C0000:
  731. case MSR_MTRRfix4K_C8000:
  732. case MSR_MTRRfix4K_D0000:
  733. case MSR_MTRRfix4K_D8000:
  734. case MSR_MTRRfix4K_E0000:
  735. case MSR_MTRRfix4K_E8000:
  736. case MSR_MTRRfix4K_F0000:
  737. case MSR_MTRRfix4K_F8000:
  738. case MSR_MTRRdefType:
  739. case MSR_IA32_CR_PAT:
  740. return true;
  741. case 0x2f8:
  742. return true;
  743. }
  744. return false;
  745. }
  746. static int set_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  747. {
  748. if (!msr_mtrr_valid(msr))
  749. return 1;
  750. vcpu->arch.mtrr[msr - 0x200] = data;
  751. return 0;
  752. }
  753. int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  754. {
  755. switch (msr) {
  756. case MSR_EFER:
  757. set_efer(vcpu, data);
  758. break;
  759. case MSR_IA32_MC0_STATUS:
  760. pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
  761. __func__, data);
  762. break;
  763. case MSR_IA32_MCG_STATUS:
  764. pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
  765. __func__, data);
  766. break;
  767. case MSR_IA32_MCG_CTL:
  768. pr_unimpl(vcpu, "%s: MSR_IA32_MCG_CTL 0x%llx, nop\n",
  769. __func__, data);
  770. break;
  771. case MSR_IA32_DEBUGCTLMSR:
  772. if (!data) {
  773. /* We support the non-activated case already */
  774. break;
  775. } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
  776. /* Values other than LBR and BTF are vendor-specific,
  777. thus reserved and should throw a #GP */
  778. return 1;
  779. }
  780. pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
  781. __func__, data);
  782. break;
  783. case MSR_IA32_UCODE_REV:
  784. case MSR_IA32_UCODE_WRITE:
  785. break;
  786. case 0x200 ... 0x2ff:
  787. return set_msr_mtrr(vcpu, msr, data);
  788. case MSR_IA32_APICBASE:
  789. kvm_set_apic_base(vcpu, data);
  790. break;
  791. case MSR_IA32_MISC_ENABLE:
  792. vcpu->arch.ia32_misc_enable_msr = data;
  793. break;
  794. case MSR_KVM_WALL_CLOCK:
  795. vcpu->kvm->arch.wall_clock = data;
  796. kvm_write_wall_clock(vcpu->kvm, data);
  797. break;
  798. case MSR_KVM_SYSTEM_TIME: {
  799. if (vcpu->arch.time_page) {
  800. kvm_release_page_dirty(vcpu->arch.time_page);
  801. vcpu->arch.time_page = NULL;
  802. }
  803. vcpu->arch.time = data;
  804. /* we verify if the enable bit is set... */
  805. if (!(data & 1))
  806. break;
  807. /* ...but clean it before doing the actual write */
  808. vcpu->arch.time_offset = data & ~(PAGE_MASK | 1);
  809. down_read(&current->mm->mmap_sem);
  810. vcpu->arch.time_page =
  811. gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT);
  812. up_read(&current->mm->mmap_sem);
  813. if (is_error_page(vcpu->arch.time_page)) {
  814. kvm_release_page_clean(vcpu->arch.time_page);
  815. vcpu->arch.time_page = NULL;
  816. }
  817. kvm_write_guest_time(vcpu);
  818. break;
  819. }
  820. default:
  821. pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n", msr, data);
  822. return 1;
  823. }
  824. return 0;
  825. }
  826. EXPORT_SYMBOL_GPL(kvm_set_msr_common);
  827. /*
  828. * Reads an msr value (of 'msr_index') into 'pdata'.
  829. * Returns 0 on success, non-0 otherwise.
  830. * Assumes vcpu_load() was already called.
  831. */
  832. int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
  833. {
  834. return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
  835. }
  836. static int get_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  837. {
  838. if (!msr_mtrr_valid(msr))
  839. return 1;
  840. *pdata = vcpu->arch.mtrr[msr - 0x200];
  841. return 0;
  842. }
  843. int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  844. {
  845. u64 data;
  846. switch (msr) {
  847. case 0xc0010010: /* SYSCFG */
  848. case 0xc0010015: /* HWCR */
  849. case MSR_IA32_PLATFORM_ID:
  850. case MSR_IA32_P5_MC_ADDR:
  851. case MSR_IA32_P5_MC_TYPE:
  852. case MSR_IA32_MC0_CTL:
  853. case MSR_IA32_MCG_STATUS:
  854. case MSR_IA32_MCG_CAP:
  855. case MSR_IA32_MCG_CTL:
  856. case MSR_IA32_MC0_MISC:
  857. case MSR_IA32_MC0_MISC+4:
  858. case MSR_IA32_MC0_MISC+8:
  859. case MSR_IA32_MC0_MISC+12:
  860. case MSR_IA32_MC0_MISC+16:
  861. case MSR_IA32_MC0_MISC+20:
  862. case MSR_IA32_UCODE_REV:
  863. case MSR_IA32_EBL_CR_POWERON:
  864. case MSR_IA32_DEBUGCTLMSR:
  865. case MSR_IA32_LASTBRANCHFROMIP:
  866. case MSR_IA32_LASTBRANCHTOIP:
  867. case MSR_IA32_LASTINTFROMIP:
  868. case MSR_IA32_LASTINTTOIP:
  869. data = 0;
  870. break;
  871. case MSR_MTRRcap:
  872. data = 0x500 | KVM_NR_VAR_MTRR;
  873. break;
  874. case 0x200 ... 0x2ff:
  875. return get_msr_mtrr(vcpu, msr, pdata);
  876. case 0xcd: /* fsb frequency */
  877. data = 3;
  878. break;
  879. case MSR_IA32_APICBASE:
  880. data = kvm_get_apic_base(vcpu);
  881. break;
  882. case MSR_IA32_MISC_ENABLE:
  883. data = vcpu->arch.ia32_misc_enable_msr;
  884. break;
  885. case MSR_IA32_PERF_STATUS:
  886. /* TSC increment by tick */
  887. data = 1000ULL;
  888. /* CPU multiplier */
  889. data |= (((uint64_t)4ULL) << 40);
  890. break;
  891. case MSR_EFER:
  892. data = vcpu->arch.shadow_efer;
  893. break;
  894. case MSR_KVM_WALL_CLOCK:
  895. data = vcpu->kvm->arch.wall_clock;
  896. break;
  897. case MSR_KVM_SYSTEM_TIME:
  898. data = vcpu->arch.time;
  899. break;
  900. default:
  901. pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
  902. return 1;
  903. }
  904. *pdata = data;
  905. return 0;
  906. }
  907. EXPORT_SYMBOL_GPL(kvm_get_msr_common);
  908. /*
  909. * Read or write a bunch of msrs. All parameters are kernel addresses.
  910. *
  911. * @return number of msrs set successfully.
  912. */
  913. static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
  914. struct kvm_msr_entry *entries,
  915. int (*do_msr)(struct kvm_vcpu *vcpu,
  916. unsigned index, u64 *data))
  917. {
  918. int i;
  919. vcpu_load(vcpu);
  920. down_read(&vcpu->kvm->slots_lock);
  921. for (i = 0; i < msrs->nmsrs; ++i)
  922. if (do_msr(vcpu, entries[i].index, &entries[i].data))
  923. break;
  924. up_read(&vcpu->kvm->slots_lock);
  925. vcpu_put(vcpu);
  926. return i;
  927. }
  928. /*
  929. * Read or write a bunch of msrs. Parameters are user addresses.
  930. *
  931. * @return number of msrs set successfully.
  932. */
  933. static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
  934. int (*do_msr)(struct kvm_vcpu *vcpu,
  935. unsigned index, u64 *data),
  936. int writeback)
  937. {
  938. struct kvm_msrs msrs;
  939. struct kvm_msr_entry *entries;
  940. int r, n;
  941. unsigned size;
  942. r = -EFAULT;
  943. if (copy_from_user(&msrs, user_msrs, sizeof msrs))
  944. goto out;
  945. r = -E2BIG;
  946. if (msrs.nmsrs >= MAX_IO_MSRS)
  947. goto out;
  948. r = -ENOMEM;
  949. size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
  950. entries = vmalloc(size);
  951. if (!entries)
  952. goto out;
  953. r = -EFAULT;
  954. if (copy_from_user(entries, user_msrs->entries, size))
  955. goto out_free;
  956. r = n = __msr_io(vcpu, &msrs, entries, do_msr);
  957. if (r < 0)
  958. goto out_free;
  959. r = -EFAULT;
  960. if (writeback && copy_to_user(user_msrs->entries, entries, size))
  961. goto out_free;
  962. r = n;
  963. out_free:
  964. vfree(entries);
  965. out:
  966. return r;
  967. }
  968. int kvm_dev_ioctl_check_extension(long ext)
  969. {
  970. int r;
  971. switch (ext) {
  972. case KVM_CAP_IRQCHIP:
  973. case KVM_CAP_HLT:
  974. case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
  975. case KVM_CAP_USER_MEMORY:
  976. case KVM_CAP_SET_TSS_ADDR:
  977. case KVM_CAP_EXT_CPUID:
  978. case KVM_CAP_CLOCKSOURCE:
  979. case KVM_CAP_PIT:
  980. case KVM_CAP_NOP_IO_DELAY:
  981. case KVM_CAP_MP_STATE:
  982. case KVM_CAP_SYNC_MMU:
  983. r = 1;
  984. break;
  985. case KVM_CAP_COALESCED_MMIO:
  986. r = KVM_COALESCED_MMIO_PAGE_OFFSET;
  987. break;
  988. case KVM_CAP_VAPIC:
  989. r = !kvm_x86_ops->cpu_has_accelerated_tpr();
  990. break;
  991. case KVM_CAP_NR_VCPUS:
  992. r = KVM_MAX_VCPUS;
  993. break;
  994. case KVM_CAP_NR_MEMSLOTS:
  995. r = KVM_MEMORY_SLOTS;
  996. break;
  997. case KVM_CAP_PV_MMU:
  998. r = !tdp_enabled;
  999. break;
  1000. default:
  1001. r = 0;
  1002. break;
  1003. }
  1004. return r;
  1005. }
  1006. long kvm_arch_dev_ioctl(struct file *filp,
  1007. unsigned int ioctl, unsigned long arg)
  1008. {
  1009. void __user *argp = (void __user *)arg;
  1010. long r;
  1011. switch (ioctl) {
  1012. case KVM_GET_MSR_INDEX_LIST: {
  1013. struct kvm_msr_list __user *user_msr_list = argp;
  1014. struct kvm_msr_list msr_list;
  1015. unsigned n;
  1016. r = -EFAULT;
  1017. if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
  1018. goto out;
  1019. n = msr_list.nmsrs;
  1020. msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
  1021. if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
  1022. goto out;
  1023. r = -E2BIG;
  1024. if (n < num_msrs_to_save)
  1025. goto out;
  1026. r = -EFAULT;
  1027. if (copy_to_user(user_msr_list->indices, &msrs_to_save,
  1028. num_msrs_to_save * sizeof(u32)))
  1029. goto out;
  1030. if (copy_to_user(user_msr_list->indices
  1031. + num_msrs_to_save * sizeof(u32),
  1032. &emulated_msrs,
  1033. ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
  1034. goto out;
  1035. r = 0;
  1036. break;
  1037. }
  1038. case KVM_GET_SUPPORTED_CPUID: {
  1039. struct kvm_cpuid2 __user *cpuid_arg = argp;
  1040. struct kvm_cpuid2 cpuid;
  1041. r = -EFAULT;
  1042. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  1043. goto out;
  1044. r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
  1045. cpuid_arg->entries);
  1046. if (r)
  1047. goto out;
  1048. r = -EFAULT;
  1049. if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
  1050. goto out;
  1051. r = 0;
  1052. break;
  1053. }
  1054. default:
  1055. r = -EINVAL;
  1056. }
  1057. out:
  1058. return r;
  1059. }
  1060. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  1061. {
  1062. kvm_x86_ops->vcpu_load(vcpu, cpu);
  1063. kvm_write_guest_time(vcpu);
  1064. }
  1065. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  1066. {
  1067. kvm_x86_ops->vcpu_put(vcpu);
  1068. kvm_put_guest_fpu(vcpu);
  1069. }
  1070. static int is_efer_nx(void)
  1071. {
  1072. u64 efer;
  1073. rdmsrl(MSR_EFER, efer);
  1074. return efer & EFER_NX;
  1075. }
  1076. static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
  1077. {
  1078. int i;
  1079. struct kvm_cpuid_entry2 *e, *entry;
  1080. entry = NULL;
  1081. for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
  1082. e = &vcpu->arch.cpuid_entries[i];
  1083. if (e->function == 0x80000001) {
  1084. entry = e;
  1085. break;
  1086. }
  1087. }
  1088. if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
  1089. entry->edx &= ~(1 << 20);
  1090. printk(KERN_INFO "kvm: guest NX capability removed\n");
  1091. }
  1092. }
  1093. /* when an old userspace process fills a new kernel module */
  1094. static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
  1095. struct kvm_cpuid *cpuid,
  1096. struct kvm_cpuid_entry __user *entries)
  1097. {
  1098. int r, i;
  1099. struct kvm_cpuid_entry *cpuid_entries;
  1100. r = -E2BIG;
  1101. if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
  1102. goto out;
  1103. r = -ENOMEM;
  1104. cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
  1105. if (!cpuid_entries)
  1106. goto out;
  1107. r = -EFAULT;
  1108. if (copy_from_user(cpuid_entries, entries,
  1109. cpuid->nent * sizeof(struct kvm_cpuid_entry)))
  1110. goto out_free;
  1111. for (i = 0; i < cpuid->nent; i++) {
  1112. vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
  1113. vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
  1114. vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
  1115. vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
  1116. vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
  1117. vcpu->arch.cpuid_entries[i].index = 0;
  1118. vcpu->arch.cpuid_entries[i].flags = 0;
  1119. vcpu->arch.cpuid_entries[i].padding[0] = 0;
  1120. vcpu->arch.cpuid_entries[i].padding[1] = 0;
  1121. vcpu->arch.cpuid_entries[i].padding[2] = 0;
  1122. }
  1123. vcpu->arch.cpuid_nent = cpuid->nent;
  1124. cpuid_fix_nx_cap(vcpu);
  1125. r = 0;
  1126. out_free:
  1127. vfree(cpuid_entries);
  1128. out:
  1129. return r;
  1130. }
  1131. static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
  1132. struct kvm_cpuid2 *cpuid,
  1133. struct kvm_cpuid_entry2 __user *entries)
  1134. {
  1135. int r;
  1136. r = -E2BIG;
  1137. if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
  1138. goto out;
  1139. r = -EFAULT;
  1140. if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
  1141. cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
  1142. goto out;
  1143. vcpu->arch.cpuid_nent = cpuid->nent;
  1144. return 0;
  1145. out:
  1146. return r;
  1147. }
  1148. static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
  1149. struct kvm_cpuid2 *cpuid,
  1150. struct kvm_cpuid_entry2 __user *entries)
  1151. {
  1152. int r;
  1153. r = -E2BIG;
  1154. if (cpuid->nent < vcpu->arch.cpuid_nent)
  1155. goto out;
  1156. r = -EFAULT;
  1157. if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
  1158. vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
  1159. goto out;
  1160. return 0;
  1161. out:
  1162. cpuid->nent = vcpu->arch.cpuid_nent;
  1163. return r;
  1164. }
  1165. static inline u32 bit(int bitno)
  1166. {
  1167. return 1 << (bitno & 31);
  1168. }
  1169. static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
  1170. u32 index)
  1171. {
  1172. entry->function = function;
  1173. entry->index = index;
  1174. cpuid_count(entry->function, entry->index,
  1175. &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
  1176. entry->flags = 0;
  1177. }
  1178. static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
  1179. u32 index, int *nent, int maxnent)
  1180. {
  1181. const u32 kvm_supported_word0_x86_features = bit(X86_FEATURE_FPU) |
  1182. bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
  1183. bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
  1184. bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
  1185. bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
  1186. bit(X86_FEATURE_SEP) | bit(X86_FEATURE_PGE) |
  1187. bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
  1188. bit(X86_FEATURE_CLFLSH) | bit(X86_FEATURE_MMX) |
  1189. bit(X86_FEATURE_FXSR) | bit(X86_FEATURE_XMM) |
  1190. bit(X86_FEATURE_XMM2) | bit(X86_FEATURE_SELFSNOOP);
  1191. const u32 kvm_supported_word1_x86_features = bit(X86_FEATURE_FPU) |
  1192. bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
  1193. bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
  1194. bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
  1195. bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
  1196. bit(X86_FEATURE_PGE) |
  1197. bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
  1198. bit(X86_FEATURE_MMX) | bit(X86_FEATURE_FXSR) |
  1199. bit(X86_FEATURE_SYSCALL) |
  1200. (bit(X86_FEATURE_NX) && is_efer_nx()) |
  1201. #ifdef CONFIG_X86_64
  1202. bit(X86_FEATURE_LM) |
  1203. #endif
  1204. bit(X86_FEATURE_MMXEXT) |
  1205. bit(X86_FEATURE_3DNOWEXT) |
  1206. bit(X86_FEATURE_3DNOW);
  1207. const u32 kvm_supported_word3_x86_features =
  1208. bit(X86_FEATURE_XMM3) | bit(X86_FEATURE_CX16);
  1209. const u32 kvm_supported_word6_x86_features =
  1210. bit(X86_FEATURE_LAHF_LM) | bit(X86_FEATURE_CMP_LEGACY);
  1211. /* all func 2 cpuid_count() should be called on the same cpu */
  1212. get_cpu();
  1213. do_cpuid_1_ent(entry, function, index);
  1214. ++*nent;
  1215. switch (function) {
  1216. case 0:
  1217. entry->eax = min(entry->eax, (u32)0xb);
  1218. break;
  1219. case 1:
  1220. entry->edx &= kvm_supported_word0_x86_features;
  1221. entry->ecx &= kvm_supported_word3_x86_features;
  1222. break;
  1223. /* function 2 entries are STATEFUL. That is, repeated cpuid commands
  1224. * may return different values. This forces us to get_cpu() before
  1225. * issuing the first command, and also to emulate this annoying behavior
  1226. * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
  1227. case 2: {
  1228. int t, times = entry->eax & 0xff;
  1229. entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
  1230. for (t = 1; t < times && *nent < maxnent; ++t) {
  1231. do_cpuid_1_ent(&entry[t], function, 0);
  1232. entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
  1233. ++*nent;
  1234. }
  1235. break;
  1236. }
  1237. /* function 4 and 0xb have additional index. */
  1238. case 4: {
  1239. int i, cache_type;
  1240. entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
  1241. /* read more entries until cache_type is zero */
  1242. for (i = 1; *nent < maxnent; ++i) {
  1243. cache_type = entry[i - 1].eax & 0x1f;
  1244. if (!cache_type)
  1245. break;
  1246. do_cpuid_1_ent(&entry[i], function, i);
  1247. entry[i].flags |=
  1248. KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
  1249. ++*nent;
  1250. }
  1251. break;
  1252. }
  1253. case 0xb: {
  1254. int i, level_type;
  1255. entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
  1256. /* read more entries until level_type is zero */
  1257. for (i = 1; *nent < maxnent; ++i) {
  1258. level_type = entry[i - 1].ecx & 0xff;
  1259. if (!level_type)
  1260. break;
  1261. do_cpuid_1_ent(&entry[i], function, i);
  1262. entry[i].flags |=
  1263. KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
  1264. ++*nent;
  1265. }
  1266. break;
  1267. }
  1268. case 0x80000000:
  1269. entry->eax = min(entry->eax, 0x8000001a);
  1270. break;
  1271. case 0x80000001:
  1272. entry->edx &= kvm_supported_word1_x86_features;
  1273. entry->ecx &= kvm_supported_word6_x86_features;
  1274. break;
  1275. }
  1276. put_cpu();
  1277. }
  1278. static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
  1279. struct kvm_cpuid_entry2 __user *entries)
  1280. {
  1281. struct kvm_cpuid_entry2 *cpuid_entries;
  1282. int limit, nent = 0, r = -E2BIG;
  1283. u32 func;
  1284. if (cpuid->nent < 1)
  1285. goto out;
  1286. r = -ENOMEM;
  1287. cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
  1288. if (!cpuid_entries)
  1289. goto out;
  1290. do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
  1291. limit = cpuid_entries[0].eax;
  1292. for (func = 1; func <= limit && nent < cpuid->nent; ++func)
  1293. do_cpuid_ent(&cpuid_entries[nent], func, 0,
  1294. &nent, cpuid->nent);
  1295. r = -E2BIG;
  1296. if (nent >= cpuid->nent)
  1297. goto out_free;
  1298. do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
  1299. limit = cpuid_entries[nent - 1].eax;
  1300. for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
  1301. do_cpuid_ent(&cpuid_entries[nent], func, 0,
  1302. &nent, cpuid->nent);
  1303. r = -EFAULT;
  1304. if (copy_to_user(entries, cpuid_entries,
  1305. nent * sizeof(struct kvm_cpuid_entry2)))
  1306. goto out_free;
  1307. cpuid->nent = nent;
  1308. r = 0;
  1309. out_free:
  1310. vfree(cpuid_entries);
  1311. out:
  1312. return r;
  1313. }
  1314. static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
  1315. struct kvm_lapic_state *s)
  1316. {
  1317. vcpu_load(vcpu);
  1318. memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
  1319. vcpu_put(vcpu);
  1320. return 0;
  1321. }
  1322. static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
  1323. struct kvm_lapic_state *s)
  1324. {
  1325. vcpu_load(vcpu);
  1326. memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
  1327. kvm_apic_post_state_restore(vcpu);
  1328. vcpu_put(vcpu);
  1329. return 0;
  1330. }
  1331. static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
  1332. struct kvm_interrupt *irq)
  1333. {
  1334. if (irq->irq < 0 || irq->irq >= 256)
  1335. return -EINVAL;
  1336. if (irqchip_in_kernel(vcpu->kvm))
  1337. return -ENXIO;
  1338. vcpu_load(vcpu);
  1339. set_bit(irq->irq, vcpu->arch.irq_pending);
  1340. set_bit(irq->irq / BITS_PER_LONG, &vcpu->arch.irq_summary);
  1341. vcpu_put(vcpu);
  1342. return 0;
  1343. }
  1344. static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
  1345. struct kvm_tpr_access_ctl *tac)
  1346. {
  1347. if (tac->flags)
  1348. return -EINVAL;
  1349. vcpu->arch.tpr_access_reporting = !!tac->enabled;
  1350. return 0;
  1351. }
  1352. long kvm_arch_vcpu_ioctl(struct file *filp,
  1353. unsigned int ioctl, unsigned long arg)
  1354. {
  1355. struct kvm_vcpu *vcpu = filp->private_data;
  1356. void __user *argp = (void __user *)arg;
  1357. int r;
  1358. struct kvm_lapic_state *lapic = NULL;
  1359. switch (ioctl) {
  1360. case KVM_GET_LAPIC: {
  1361. lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
  1362. r = -ENOMEM;
  1363. if (!lapic)
  1364. goto out;
  1365. r = kvm_vcpu_ioctl_get_lapic(vcpu, lapic);
  1366. if (r)
  1367. goto out;
  1368. r = -EFAULT;
  1369. if (copy_to_user(argp, lapic, sizeof(struct kvm_lapic_state)))
  1370. goto out;
  1371. r = 0;
  1372. break;
  1373. }
  1374. case KVM_SET_LAPIC: {
  1375. lapic = kmalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
  1376. r = -ENOMEM;
  1377. if (!lapic)
  1378. goto out;
  1379. r = -EFAULT;
  1380. if (copy_from_user(lapic, argp, sizeof(struct kvm_lapic_state)))
  1381. goto out;
  1382. r = kvm_vcpu_ioctl_set_lapic(vcpu, lapic);
  1383. if (r)
  1384. goto out;
  1385. r = 0;
  1386. break;
  1387. }
  1388. case KVM_INTERRUPT: {
  1389. struct kvm_interrupt irq;
  1390. r = -EFAULT;
  1391. if (copy_from_user(&irq, argp, sizeof irq))
  1392. goto out;
  1393. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  1394. if (r)
  1395. goto out;
  1396. r = 0;
  1397. break;
  1398. }
  1399. case KVM_SET_CPUID: {
  1400. struct kvm_cpuid __user *cpuid_arg = argp;
  1401. struct kvm_cpuid cpuid;
  1402. r = -EFAULT;
  1403. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  1404. goto out;
  1405. r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
  1406. if (r)
  1407. goto out;
  1408. break;
  1409. }
  1410. case KVM_SET_CPUID2: {
  1411. struct kvm_cpuid2 __user *cpuid_arg = argp;
  1412. struct kvm_cpuid2 cpuid;
  1413. r = -EFAULT;
  1414. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  1415. goto out;
  1416. r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
  1417. cpuid_arg->entries);
  1418. if (r)
  1419. goto out;
  1420. break;
  1421. }
  1422. case KVM_GET_CPUID2: {
  1423. struct kvm_cpuid2 __user *cpuid_arg = argp;
  1424. struct kvm_cpuid2 cpuid;
  1425. r = -EFAULT;
  1426. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  1427. goto out;
  1428. r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
  1429. cpuid_arg->entries);
  1430. if (r)
  1431. goto out;
  1432. r = -EFAULT;
  1433. if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
  1434. goto out;
  1435. r = 0;
  1436. break;
  1437. }
  1438. case KVM_GET_MSRS:
  1439. r = msr_io(vcpu, argp, kvm_get_msr, 1);
  1440. break;
  1441. case KVM_SET_MSRS:
  1442. r = msr_io(vcpu, argp, do_set_msr, 0);
  1443. break;
  1444. case KVM_TPR_ACCESS_REPORTING: {
  1445. struct kvm_tpr_access_ctl tac;
  1446. r = -EFAULT;
  1447. if (copy_from_user(&tac, argp, sizeof tac))
  1448. goto out;
  1449. r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
  1450. if (r)
  1451. goto out;
  1452. r = -EFAULT;
  1453. if (copy_to_user(argp, &tac, sizeof tac))
  1454. goto out;
  1455. r = 0;
  1456. break;
  1457. };
  1458. case KVM_SET_VAPIC_ADDR: {
  1459. struct kvm_vapic_addr va;
  1460. r = -EINVAL;
  1461. if (!irqchip_in_kernel(vcpu->kvm))
  1462. goto out;
  1463. r = -EFAULT;
  1464. if (copy_from_user(&va, argp, sizeof va))
  1465. goto out;
  1466. r = 0;
  1467. kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
  1468. break;
  1469. }
  1470. default:
  1471. r = -EINVAL;
  1472. }
  1473. out:
  1474. if (lapic)
  1475. kfree(lapic);
  1476. return r;
  1477. }
  1478. static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
  1479. {
  1480. int ret;
  1481. if (addr > (unsigned int)(-3 * PAGE_SIZE))
  1482. return -1;
  1483. ret = kvm_x86_ops->set_tss_addr(kvm, addr);
  1484. return ret;
  1485. }
  1486. static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
  1487. u32 kvm_nr_mmu_pages)
  1488. {
  1489. if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
  1490. return -EINVAL;
  1491. down_write(&kvm->slots_lock);
  1492. kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
  1493. kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
  1494. up_write(&kvm->slots_lock);
  1495. return 0;
  1496. }
  1497. static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
  1498. {
  1499. return kvm->arch.n_alloc_mmu_pages;
  1500. }
  1501. gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
  1502. {
  1503. int i;
  1504. struct kvm_mem_alias *alias;
  1505. for (i = 0; i < kvm->arch.naliases; ++i) {
  1506. alias = &kvm->arch.aliases[i];
  1507. if (gfn >= alias->base_gfn
  1508. && gfn < alias->base_gfn + alias->npages)
  1509. return alias->target_gfn + gfn - alias->base_gfn;
  1510. }
  1511. return gfn;
  1512. }
  1513. /*
  1514. * Set a new alias region. Aliases map a portion of physical memory into
  1515. * another portion. This is useful for memory windows, for example the PC
  1516. * VGA region.
  1517. */
  1518. static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
  1519. struct kvm_memory_alias *alias)
  1520. {
  1521. int r, n;
  1522. struct kvm_mem_alias *p;
  1523. r = -EINVAL;
  1524. /* General sanity checks */
  1525. if (alias->memory_size & (PAGE_SIZE - 1))
  1526. goto out;
  1527. if (alias->guest_phys_addr & (PAGE_SIZE - 1))
  1528. goto out;
  1529. if (alias->slot >= KVM_ALIAS_SLOTS)
  1530. goto out;
  1531. if (alias->guest_phys_addr + alias->memory_size
  1532. < alias->guest_phys_addr)
  1533. goto out;
  1534. if (alias->target_phys_addr + alias->memory_size
  1535. < alias->target_phys_addr)
  1536. goto out;
  1537. down_write(&kvm->slots_lock);
  1538. spin_lock(&kvm->mmu_lock);
  1539. p = &kvm->arch.aliases[alias->slot];
  1540. p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
  1541. p->npages = alias->memory_size >> PAGE_SHIFT;
  1542. p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
  1543. for (n = KVM_ALIAS_SLOTS; n > 0; --n)
  1544. if (kvm->arch.aliases[n - 1].npages)
  1545. break;
  1546. kvm->arch.naliases = n;
  1547. spin_unlock(&kvm->mmu_lock);
  1548. kvm_mmu_zap_all(kvm);
  1549. up_write(&kvm->slots_lock);
  1550. return 0;
  1551. out:
  1552. return r;
  1553. }
  1554. static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
  1555. {
  1556. int r;
  1557. r = 0;
  1558. switch (chip->chip_id) {
  1559. case KVM_IRQCHIP_PIC_MASTER:
  1560. memcpy(&chip->chip.pic,
  1561. &pic_irqchip(kvm)->pics[0],
  1562. sizeof(struct kvm_pic_state));
  1563. break;
  1564. case KVM_IRQCHIP_PIC_SLAVE:
  1565. memcpy(&chip->chip.pic,
  1566. &pic_irqchip(kvm)->pics[1],
  1567. sizeof(struct kvm_pic_state));
  1568. break;
  1569. case KVM_IRQCHIP_IOAPIC:
  1570. memcpy(&chip->chip.ioapic,
  1571. ioapic_irqchip(kvm),
  1572. sizeof(struct kvm_ioapic_state));
  1573. break;
  1574. default:
  1575. r = -EINVAL;
  1576. break;
  1577. }
  1578. return r;
  1579. }
  1580. static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
  1581. {
  1582. int r;
  1583. r = 0;
  1584. switch (chip->chip_id) {
  1585. case KVM_IRQCHIP_PIC_MASTER:
  1586. memcpy(&pic_irqchip(kvm)->pics[0],
  1587. &chip->chip.pic,
  1588. sizeof(struct kvm_pic_state));
  1589. break;
  1590. case KVM_IRQCHIP_PIC_SLAVE:
  1591. memcpy(&pic_irqchip(kvm)->pics[1],
  1592. &chip->chip.pic,
  1593. sizeof(struct kvm_pic_state));
  1594. break;
  1595. case KVM_IRQCHIP_IOAPIC:
  1596. memcpy(ioapic_irqchip(kvm),
  1597. &chip->chip.ioapic,
  1598. sizeof(struct kvm_ioapic_state));
  1599. break;
  1600. default:
  1601. r = -EINVAL;
  1602. break;
  1603. }
  1604. kvm_pic_update_irq(pic_irqchip(kvm));
  1605. return r;
  1606. }
  1607. static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
  1608. {
  1609. int r = 0;
  1610. memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state));
  1611. return r;
  1612. }
  1613. static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
  1614. {
  1615. int r = 0;
  1616. memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state));
  1617. kvm_pit_load_count(kvm, 0, ps->channels[0].count);
  1618. return r;
  1619. }
  1620. /*
  1621. * Get (and clear) the dirty memory log for a memory slot.
  1622. */
  1623. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
  1624. struct kvm_dirty_log *log)
  1625. {
  1626. int r;
  1627. int n;
  1628. struct kvm_memory_slot *memslot;
  1629. int is_dirty = 0;
  1630. down_write(&kvm->slots_lock);
  1631. r = kvm_get_dirty_log(kvm, log, &is_dirty);
  1632. if (r)
  1633. goto out;
  1634. /* If nothing is dirty, don't bother messing with page tables. */
  1635. if (is_dirty) {
  1636. kvm_mmu_slot_remove_write_access(kvm, log->slot);
  1637. kvm_flush_remote_tlbs(kvm);
  1638. memslot = &kvm->memslots[log->slot];
  1639. n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
  1640. memset(memslot->dirty_bitmap, 0, n);
  1641. }
  1642. r = 0;
  1643. out:
  1644. up_write(&kvm->slots_lock);
  1645. return r;
  1646. }
  1647. long kvm_arch_vm_ioctl(struct file *filp,
  1648. unsigned int ioctl, unsigned long arg)
  1649. {
  1650. struct kvm *kvm = filp->private_data;
  1651. void __user *argp = (void __user *)arg;
  1652. int r = -EINVAL;
  1653. /*
  1654. * This union makes it completely explicit to gcc-3.x
  1655. * that these two variables' stack usage should be
  1656. * combined, not added together.
  1657. */
  1658. union {
  1659. struct kvm_pit_state ps;
  1660. struct kvm_memory_alias alias;
  1661. } u;
  1662. switch (ioctl) {
  1663. case KVM_SET_TSS_ADDR:
  1664. r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
  1665. if (r < 0)
  1666. goto out;
  1667. break;
  1668. case KVM_SET_MEMORY_REGION: {
  1669. struct kvm_memory_region kvm_mem;
  1670. struct kvm_userspace_memory_region kvm_userspace_mem;
  1671. r = -EFAULT;
  1672. if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
  1673. goto out;
  1674. kvm_userspace_mem.slot = kvm_mem.slot;
  1675. kvm_userspace_mem.flags = kvm_mem.flags;
  1676. kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
  1677. kvm_userspace_mem.memory_size = kvm_mem.memory_size;
  1678. r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
  1679. if (r)
  1680. goto out;
  1681. break;
  1682. }
  1683. case KVM_SET_NR_MMU_PAGES:
  1684. r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
  1685. if (r)
  1686. goto out;
  1687. break;
  1688. case KVM_GET_NR_MMU_PAGES:
  1689. r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
  1690. break;
  1691. case KVM_SET_MEMORY_ALIAS:
  1692. r = -EFAULT;
  1693. if (copy_from_user(&u.alias, argp, sizeof(struct kvm_memory_alias)))
  1694. goto out;
  1695. r = kvm_vm_ioctl_set_memory_alias(kvm, &u.alias);
  1696. if (r)
  1697. goto out;
  1698. break;
  1699. case KVM_CREATE_IRQCHIP:
  1700. r = -ENOMEM;
  1701. kvm->arch.vpic = kvm_create_pic(kvm);
  1702. if (kvm->arch.vpic) {
  1703. r = kvm_ioapic_init(kvm);
  1704. if (r) {
  1705. kfree(kvm->arch.vpic);
  1706. kvm->arch.vpic = NULL;
  1707. goto out;
  1708. }
  1709. } else
  1710. goto out;
  1711. break;
  1712. case KVM_CREATE_PIT:
  1713. r = -ENOMEM;
  1714. kvm->arch.vpit = kvm_create_pit(kvm);
  1715. if (kvm->arch.vpit)
  1716. r = 0;
  1717. break;
  1718. case KVM_IRQ_LINE: {
  1719. struct kvm_irq_level irq_event;
  1720. r = -EFAULT;
  1721. if (copy_from_user(&irq_event, argp, sizeof irq_event))
  1722. goto out;
  1723. if (irqchip_in_kernel(kvm)) {
  1724. mutex_lock(&kvm->lock);
  1725. kvm_set_irq(kvm, irq_event.irq, irq_event.level);
  1726. mutex_unlock(&kvm->lock);
  1727. r = 0;
  1728. }
  1729. break;
  1730. }
  1731. case KVM_GET_IRQCHIP: {
  1732. /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
  1733. struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
  1734. r = -ENOMEM;
  1735. if (!chip)
  1736. goto out;
  1737. r = -EFAULT;
  1738. if (copy_from_user(chip, argp, sizeof *chip))
  1739. goto get_irqchip_out;
  1740. r = -ENXIO;
  1741. if (!irqchip_in_kernel(kvm))
  1742. goto get_irqchip_out;
  1743. r = kvm_vm_ioctl_get_irqchip(kvm, chip);
  1744. if (r)
  1745. goto get_irqchip_out;
  1746. r = -EFAULT;
  1747. if (copy_to_user(argp, chip, sizeof *chip))
  1748. goto get_irqchip_out;
  1749. r = 0;
  1750. get_irqchip_out:
  1751. kfree(chip);
  1752. if (r)
  1753. goto out;
  1754. break;
  1755. }
  1756. case KVM_SET_IRQCHIP: {
  1757. /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
  1758. struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
  1759. r = -ENOMEM;
  1760. if (!chip)
  1761. goto out;
  1762. r = -EFAULT;
  1763. if (copy_from_user(chip, argp, sizeof *chip))
  1764. goto set_irqchip_out;
  1765. r = -ENXIO;
  1766. if (!irqchip_in_kernel(kvm))
  1767. goto set_irqchip_out;
  1768. r = kvm_vm_ioctl_set_irqchip(kvm, chip);
  1769. if (r)
  1770. goto set_irqchip_out;
  1771. r = 0;
  1772. set_irqchip_out:
  1773. kfree(chip);
  1774. if (r)
  1775. goto out;
  1776. break;
  1777. }
  1778. case KVM_ASSIGN_PCI_DEVICE: {
  1779. struct kvm_assigned_pci_dev assigned_dev;
  1780. r = -EFAULT;
  1781. if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
  1782. goto out;
  1783. r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
  1784. if (r)
  1785. goto out;
  1786. break;
  1787. }
  1788. case KVM_ASSIGN_IRQ: {
  1789. struct kvm_assigned_irq assigned_irq;
  1790. r = -EFAULT;
  1791. if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
  1792. goto out;
  1793. r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
  1794. if (r)
  1795. goto out;
  1796. break;
  1797. }
  1798. case KVM_GET_PIT: {
  1799. r = -EFAULT;
  1800. if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
  1801. goto out;
  1802. r = -ENXIO;
  1803. if (!kvm->arch.vpit)
  1804. goto out;
  1805. r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
  1806. if (r)
  1807. goto out;
  1808. r = -EFAULT;
  1809. if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
  1810. goto out;
  1811. r = 0;
  1812. break;
  1813. }
  1814. case KVM_SET_PIT: {
  1815. r = -EFAULT;
  1816. if (copy_from_user(&u.ps, argp, sizeof u.ps))
  1817. goto out;
  1818. r = -ENXIO;
  1819. if (!kvm->arch.vpit)
  1820. goto out;
  1821. r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
  1822. if (r)
  1823. goto out;
  1824. r = 0;
  1825. break;
  1826. }
  1827. default:
  1828. ;
  1829. }
  1830. out:
  1831. return r;
  1832. }
  1833. static void kvm_init_msr_list(void)
  1834. {
  1835. u32 dummy[2];
  1836. unsigned i, j;
  1837. for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
  1838. if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
  1839. continue;
  1840. if (j < i)
  1841. msrs_to_save[j] = msrs_to_save[i];
  1842. j++;
  1843. }
  1844. num_msrs_to_save = j;
  1845. }
  1846. /*
  1847. * Only apic need an MMIO device hook, so shortcut now..
  1848. */
  1849. static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
  1850. gpa_t addr, int len,
  1851. int is_write)
  1852. {
  1853. struct kvm_io_device *dev;
  1854. if (vcpu->arch.apic) {
  1855. dev = &vcpu->arch.apic->dev;
  1856. if (dev->in_range(dev, addr, len, is_write))
  1857. return dev;
  1858. }
  1859. return NULL;
  1860. }
  1861. static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
  1862. gpa_t addr, int len,
  1863. int is_write)
  1864. {
  1865. struct kvm_io_device *dev;
  1866. dev = vcpu_find_pervcpu_dev(vcpu, addr, len, is_write);
  1867. if (dev == NULL)
  1868. dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr, len,
  1869. is_write);
  1870. return dev;
  1871. }
  1872. int emulator_read_std(unsigned long addr,
  1873. void *val,
  1874. unsigned int bytes,
  1875. struct kvm_vcpu *vcpu)
  1876. {
  1877. void *data = val;
  1878. int r = X86EMUL_CONTINUE;
  1879. while (bytes) {
  1880. gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
  1881. unsigned offset = addr & (PAGE_SIZE-1);
  1882. unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
  1883. int ret;
  1884. if (gpa == UNMAPPED_GVA) {
  1885. r = X86EMUL_PROPAGATE_FAULT;
  1886. goto out;
  1887. }
  1888. ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy);
  1889. if (ret < 0) {
  1890. r = X86EMUL_UNHANDLEABLE;
  1891. goto out;
  1892. }
  1893. bytes -= tocopy;
  1894. data += tocopy;
  1895. addr += tocopy;
  1896. }
  1897. out:
  1898. return r;
  1899. }
  1900. EXPORT_SYMBOL_GPL(emulator_read_std);
  1901. static int emulator_read_emulated(unsigned long addr,
  1902. void *val,
  1903. unsigned int bytes,
  1904. struct kvm_vcpu *vcpu)
  1905. {
  1906. struct kvm_io_device *mmio_dev;
  1907. gpa_t gpa;
  1908. if (vcpu->mmio_read_completed) {
  1909. memcpy(val, vcpu->mmio_data, bytes);
  1910. vcpu->mmio_read_completed = 0;
  1911. return X86EMUL_CONTINUE;
  1912. }
  1913. gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
  1914. /* For APIC access vmexit */
  1915. if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
  1916. goto mmio;
  1917. if (emulator_read_std(addr, val, bytes, vcpu)
  1918. == X86EMUL_CONTINUE)
  1919. return X86EMUL_CONTINUE;
  1920. if (gpa == UNMAPPED_GVA)
  1921. return X86EMUL_PROPAGATE_FAULT;
  1922. mmio:
  1923. /*
  1924. * Is this MMIO handled locally?
  1925. */
  1926. mutex_lock(&vcpu->kvm->lock);
  1927. mmio_dev = vcpu_find_mmio_dev(vcpu, gpa, bytes, 0);
  1928. if (mmio_dev) {
  1929. kvm_iodevice_read(mmio_dev, gpa, bytes, val);
  1930. mutex_unlock(&vcpu->kvm->lock);
  1931. return X86EMUL_CONTINUE;
  1932. }
  1933. mutex_unlock(&vcpu->kvm->lock);
  1934. vcpu->mmio_needed = 1;
  1935. vcpu->mmio_phys_addr = gpa;
  1936. vcpu->mmio_size = bytes;
  1937. vcpu->mmio_is_write = 0;
  1938. return X86EMUL_UNHANDLEABLE;
  1939. }
  1940. int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
  1941. const void *val, int bytes)
  1942. {
  1943. int ret;
  1944. ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
  1945. if (ret < 0)
  1946. return 0;
  1947. kvm_mmu_pte_write(vcpu, gpa, val, bytes);
  1948. return 1;
  1949. }
  1950. static int emulator_write_emulated_onepage(unsigned long addr,
  1951. const void *val,
  1952. unsigned int bytes,
  1953. struct kvm_vcpu *vcpu)
  1954. {
  1955. struct kvm_io_device *mmio_dev;
  1956. gpa_t gpa;
  1957. gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
  1958. if (gpa == UNMAPPED_GVA) {
  1959. kvm_inject_page_fault(vcpu, addr, 2);
  1960. return X86EMUL_PROPAGATE_FAULT;
  1961. }
  1962. /* For APIC access vmexit */
  1963. if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
  1964. goto mmio;
  1965. if (emulator_write_phys(vcpu, gpa, val, bytes))
  1966. return X86EMUL_CONTINUE;
  1967. mmio:
  1968. /*
  1969. * Is this MMIO handled locally?
  1970. */
  1971. mutex_lock(&vcpu->kvm->lock);
  1972. mmio_dev = vcpu_find_mmio_dev(vcpu, gpa, bytes, 1);
  1973. if (mmio_dev) {
  1974. kvm_iodevice_write(mmio_dev, gpa, bytes, val);
  1975. mutex_unlock(&vcpu->kvm->lock);
  1976. return X86EMUL_CONTINUE;
  1977. }
  1978. mutex_unlock(&vcpu->kvm->lock);
  1979. vcpu->mmio_needed = 1;
  1980. vcpu->mmio_phys_addr = gpa;
  1981. vcpu->mmio_size = bytes;
  1982. vcpu->mmio_is_write = 1;
  1983. memcpy(vcpu->mmio_data, val, bytes);
  1984. return X86EMUL_CONTINUE;
  1985. }
  1986. int emulator_write_emulated(unsigned long addr,
  1987. const void *val,
  1988. unsigned int bytes,
  1989. struct kvm_vcpu *vcpu)
  1990. {
  1991. /* Crossing a page boundary? */
  1992. if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
  1993. int rc, now;
  1994. now = -addr & ~PAGE_MASK;
  1995. rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
  1996. if (rc != X86EMUL_CONTINUE)
  1997. return rc;
  1998. addr += now;
  1999. val += now;
  2000. bytes -= now;
  2001. }
  2002. return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
  2003. }
  2004. EXPORT_SYMBOL_GPL(emulator_write_emulated);
  2005. static int emulator_cmpxchg_emulated(unsigned long addr,
  2006. const void *old,
  2007. const void *new,
  2008. unsigned int bytes,
  2009. struct kvm_vcpu *vcpu)
  2010. {
  2011. static int reported;
  2012. if (!reported) {
  2013. reported = 1;
  2014. printk(KERN_WARNING "kvm: emulating exchange as write\n");
  2015. }
  2016. #ifndef CONFIG_X86_64
  2017. /* guests cmpxchg8b have to be emulated atomically */
  2018. if (bytes == 8) {
  2019. gpa_t gpa;
  2020. struct page *page;
  2021. char *kaddr;
  2022. u64 val;
  2023. gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
  2024. if (gpa == UNMAPPED_GVA ||
  2025. (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
  2026. goto emul_write;
  2027. if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
  2028. goto emul_write;
  2029. val = *(u64 *)new;
  2030. down_read(&current->mm->mmap_sem);
  2031. page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
  2032. up_read(&current->mm->mmap_sem);
  2033. kaddr = kmap_atomic(page, KM_USER0);
  2034. set_64bit((u64 *)(kaddr + offset_in_page(gpa)), val);
  2035. kunmap_atomic(kaddr, KM_USER0);
  2036. kvm_release_page_dirty(page);
  2037. }
  2038. emul_write:
  2039. #endif
  2040. return emulator_write_emulated(addr, new, bytes, vcpu);
  2041. }
  2042. static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
  2043. {
  2044. return kvm_x86_ops->get_segment_base(vcpu, seg);
  2045. }
  2046. int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
  2047. {
  2048. return X86EMUL_CONTINUE;
  2049. }
  2050. int emulate_clts(struct kvm_vcpu *vcpu)
  2051. {
  2052. KVMTRACE_0D(CLTS, vcpu, handler);
  2053. kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 & ~X86_CR0_TS);
  2054. return X86EMUL_CONTINUE;
  2055. }
  2056. int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
  2057. {
  2058. struct kvm_vcpu *vcpu = ctxt->vcpu;
  2059. switch (dr) {
  2060. case 0 ... 3:
  2061. *dest = kvm_x86_ops->get_dr(vcpu, dr);
  2062. return X86EMUL_CONTINUE;
  2063. default:
  2064. pr_unimpl(vcpu, "%s: unexpected dr %u\n", __func__, dr);
  2065. return X86EMUL_UNHANDLEABLE;
  2066. }
  2067. }
  2068. int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
  2069. {
  2070. unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
  2071. int exception;
  2072. kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
  2073. if (exception) {
  2074. /* FIXME: better handling */
  2075. return X86EMUL_UNHANDLEABLE;
  2076. }
  2077. return X86EMUL_CONTINUE;
  2078. }
  2079. void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
  2080. {
  2081. u8 opcodes[4];
  2082. unsigned long rip = kvm_rip_read(vcpu);
  2083. unsigned long rip_linear;
  2084. if (!printk_ratelimit())
  2085. return;
  2086. rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
  2087. emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
  2088. printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
  2089. context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
  2090. }
  2091. EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
  2092. static struct x86_emulate_ops emulate_ops = {
  2093. .read_std = emulator_read_std,
  2094. .read_emulated = emulator_read_emulated,
  2095. .write_emulated = emulator_write_emulated,
  2096. .cmpxchg_emulated = emulator_cmpxchg_emulated,
  2097. };
  2098. static void cache_all_regs(struct kvm_vcpu *vcpu)
  2099. {
  2100. kvm_register_read(vcpu, VCPU_REGS_RAX);
  2101. kvm_register_read(vcpu, VCPU_REGS_RSP);
  2102. kvm_register_read(vcpu, VCPU_REGS_RIP);
  2103. vcpu->arch.regs_dirty = ~0;
  2104. }
  2105. int emulate_instruction(struct kvm_vcpu *vcpu,
  2106. struct kvm_run *run,
  2107. unsigned long cr2,
  2108. u16 error_code,
  2109. int emulation_type)
  2110. {
  2111. int r;
  2112. struct decode_cache *c;
  2113. kvm_clear_exception_queue(vcpu);
  2114. vcpu->arch.mmio_fault_cr2 = cr2;
  2115. /*
  2116. * TODO: fix x86_emulate.c to use guest_read/write_register
  2117. * instead of direct ->regs accesses, can save hundred cycles
  2118. * on Intel for instructions that don't read/change RSP, for
  2119. * for example.
  2120. */
  2121. cache_all_regs(vcpu);
  2122. vcpu->mmio_is_write = 0;
  2123. vcpu->arch.pio.string = 0;
  2124. if (!(emulation_type & EMULTYPE_NO_DECODE)) {
  2125. int cs_db, cs_l;
  2126. kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  2127. vcpu->arch.emulate_ctxt.vcpu = vcpu;
  2128. vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
  2129. vcpu->arch.emulate_ctxt.mode =
  2130. (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
  2131. ? X86EMUL_MODE_REAL : cs_l
  2132. ? X86EMUL_MODE_PROT64 : cs_db
  2133. ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
  2134. r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
  2135. /* Reject the instructions other than VMCALL/VMMCALL when
  2136. * try to emulate invalid opcode */
  2137. c = &vcpu->arch.emulate_ctxt.decode;
  2138. if ((emulation_type & EMULTYPE_TRAP_UD) &&
  2139. (!(c->twobyte && c->b == 0x01 &&
  2140. (c->modrm_reg == 0 || c->modrm_reg == 3) &&
  2141. c->modrm_mod == 3 && c->modrm_rm == 1)))
  2142. return EMULATE_FAIL;
  2143. ++vcpu->stat.insn_emulation;
  2144. if (r) {
  2145. ++vcpu->stat.insn_emulation_fail;
  2146. if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
  2147. return EMULATE_DONE;
  2148. return EMULATE_FAIL;
  2149. }
  2150. }
  2151. r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
  2152. if (vcpu->arch.pio.string)
  2153. return EMULATE_DO_MMIO;
  2154. if ((r || vcpu->mmio_is_write) && run) {
  2155. run->exit_reason = KVM_EXIT_MMIO;
  2156. run->mmio.phys_addr = vcpu->mmio_phys_addr;
  2157. memcpy(run->mmio.data, vcpu->mmio_data, 8);
  2158. run->mmio.len = vcpu->mmio_size;
  2159. run->mmio.is_write = vcpu->mmio_is_write;
  2160. }
  2161. if (r) {
  2162. if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
  2163. return EMULATE_DONE;
  2164. if (!vcpu->mmio_needed) {
  2165. kvm_report_emulation_failure(vcpu, "mmio");
  2166. return EMULATE_FAIL;
  2167. }
  2168. return EMULATE_DO_MMIO;
  2169. }
  2170. kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
  2171. if (vcpu->mmio_is_write) {
  2172. vcpu->mmio_needed = 0;
  2173. return EMULATE_DO_MMIO;
  2174. }
  2175. return EMULATE_DONE;
  2176. }
  2177. EXPORT_SYMBOL_GPL(emulate_instruction);
  2178. static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
  2179. {
  2180. int i;
  2181. for (i = 0; i < ARRAY_SIZE(vcpu->arch.pio.guest_pages); ++i)
  2182. if (vcpu->arch.pio.guest_pages[i]) {
  2183. kvm_release_page_dirty(vcpu->arch.pio.guest_pages[i]);
  2184. vcpu->arch.pio.guest_pages[i] = NULL;
  2185. }
  2186. }
  2187. static int pio_copy_data(struct kvm_vcpu *vcpu)
  2188. {
  2189. void *p = vcpu->arch.pio_data;
  2190. void *q;
  2191. unsigned bytes;
  2192. int nr_pages = vcpu->arch.pio.guest_pages[1] ? 2 : 1;
  2193. q = vmap(vcpu->arch.pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
  2194. PAGE_KERNEL);
  2195. if (!q) {
  2196. free_pio_guest_pages(vcpu);
  2197. return -ENOMEM;
  2198. }
  2199. q += vcpu->arch.pio.guest_page_offset;
  2200. bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count;
  2201. if (vcpu->arch.pio.in)
  2202. memcpy(q, p, bytes);
  2203. else
  2204. memcpy(p, q, bytes);
  2205. q -= vcpu->arch.pio.guest_page_offset;
  2206. vunmap(q);
  2207. free_pio_guest_pages(vcpu);
  2208. return 0;
  2209. }
  2210. int complete_pio(struct kvm_vcpu *vcpu)
  2211. {
  2212. struct kvm_pio_request *io = &vcpu->arch.pio;
  2213. long delta;
  2214. int r;
  2215. unsigned long val;
  2216. if (!io->string) {
  2217. if (io->in) {
  2218. val = kvm_register_read(vcpu, VCPU_REGS_RAX);
  2219. memcpy(&val, vcpu->arch.pio_data, io->size);
  2220. kvm_register_write(vcpu, VCPU_REGS_RAX, val);
  2221. }
  2222. } else {
  2223. if (io->in) {
  2224. r = pio_copy_data(vcpu);
  2225. if (r)
  2226. return r;
  2227. }
  2228. delta = 1;
  2229. if (io->rep) {
  2230. delta *= io->cur_count;
  2231. /*
  2232. * The size of the register should really depend on
  2233. * current address size.
  2234. */
  2235. val = kvm_register_read(vcpu, VCPU_REGS_RCX);
  2236. val -= delta;
  2237. kvm_register_write(vcpu, VCPU_REGS_RCX, val);
  2238. }
  2239. if (io->down)
  2240. delta = -delta;
  2241. delta *= io->size;
  2242. if (io->in) {
  2243. val = kvm_register_read(vcpu, VCPU_REGS_RDI);
  2244. val += delta;
  2245. kvm_register_write(vcpu, VCPU_REGS_RDI, val);
  2246. } else {
  2247. val = kvm_register_read(vcpu, VCPU_REGS_RSI);
  2248. val += delta;
  2249. kvm_register_write(vcpu, VCPU_REGS_RSI, val);
  2250. }
  2251. }
  2252. io->count -= io->cur_count;
  2253. io->cur_count = 0;
  2254. return 0;
  2255. }
  2256. static void kernel_pio(struct kvm_io_device *pio_dev,
  2257. struct kvm_vcpu *vcpu,
  2258. void *pd)
  2259. {
  2260. /* TODO: String I/O for in kernel device */
  2261. mutex_lock(&vcpu->kvm->lock);
  2262. if (vcpu->arch.pio.in)
  2263. kvm_iodevice_read(pio_dev, vcpu->arch.pio.port,
  2264. vcpu->arch.pio.size,
  2265. pd);
  2266. else
  2267. kvm_iodevice_write(pio_dev, vcpu->arch.pio.port,
  2268. vcpu->arch.pio.size,
  2269. pd);
  2270. mutex_unlock(&vcpu->kvm->lock);
  2271. }
  2272. static void pio_string_write(struct kvm_io_device *pio_dev,
  2273. struct kvm_vcpu *vcpu)
  2274. {
  2275. struct kvm_pio_request *io = &vcpu->arch.pio;
  2276. void *pd = vcpu->arch.pio_data;
  2277. int i;
  2278. mutex_lock(&vcpu->kvm->lock);
  2279. for (i = 0; i < io->cur_count; i++) {
  2280. kvm_iodevice_write(pio_dev, io->port,
  2281. io->size,
  2282. pd);
  2283. pd += io->size;
  2284. }
  2285. mutex_unlock(&vcpu->kvm->lock);
  2286. }
  2287. static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
  2288. gpa_t addr, int len,
  2289. int is_write)
  2290. {
  2291. return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr, len, is_write);
  2292. }
  2293. int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
  2294. int size, unsigned port)
  2295. {
  2296. struct kvm_io_device *pio_dev;
  2297. unsigned long val;
  2298. vcpu->run->exit_reason = KVM_EXIT_IO;
  2299. vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
  2300. vcpu->run->io.size = vcpu->arch.pio.size = size;
  2301. vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
  2302. vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = 1;
  2303. vcpu->run->io.port = vcpu->arch.pio.port = port;
  2304. vcpu->arch.pio.in = in;
  2305. vcpu->arch.pio.string = 0;
  2306. vcpu->arch.pio.down = 0;
  2307. vcpu->arch.pio.guest_page_offset = 0;
  2308. vcpu->arch.pio.rep = 0;
  2309. if (vcpu->run->io.direction == KVM_EXIT_IO_IN)
  2310. KVMTRACE_2D(IO_READ, vcpu, vcpu->run->io.port, (u32)size,
  2311. handler);
  2312. else
  2313. KVMTRACE_2D(IO_WRITE, vcpu, vcpu->run->io.port, (u32)size,
  2314. handler);
  2315. val = kvm_register_read(vcpu, VCPU_REGS_RAX);
  2316. memcpy(vcpu->arch.pio_data, &val, 4);
  2317. kvm_x86_ops->skip_emulated_instruction(vcpu);
  2318. pio_dev = vcpu_find_pio_dev(vcpu, port, size, !in);
  2319. if (pio_dev) {
  2320. kernel_pio(pio_dev, vcpu, vcpu->arch.pio_data);
  2321. complete_pio(vcpu);
  2322. return 1;
  2323. }
  2324. return 0;
  2325. }
  2326. EXPORT_SYMBOL_GPL(kvm_emulate_pio);
  2327. int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
  2328. int size, unsigned long count, int down,
  2329. gva_t address, int rep, unsigned port)
  2330. {
  2331. unsigned now, in_page;
  2332. int i, ret = 0;
  2333. int nr_pages = 1;
  2334. struct page *page;
  2335. struct kvm_io_device *pio_dev;
  2336. vcpu->run->exit_reason = KVM_EXIT_IO;
  2337. vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
  2338. vcpu->run->io.size = vcpu->arch.pio.size = size;
  2339. vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
  2340. vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = count;
  2341. vcpu->run->io.port = vcpu->arch.pio.port = port;
  2342. vcpu->arch.pio.in = in;
  2343. vcpu->arch.pio.string = 1;
  2344. vcpu->arch.pio.down = down;
  2345. vcpu->arch.pio.guest_page_offset = offset_in_page(address);
  2346. vcpu->arch.pio.rep = rep;
  2347. if (vcpu->run->io.direction == KVM_EXIT_IO_IN)
  2348. KVMTRACE_2D(IO_READ, vcpu, vcpu->run->io.port, (u32)size,
  2349. handler);
  2350. else
  2351. KVMTRACE_2D(IO_WRITE, vcpu, vcpu->run->io.port, (u32)size,
  2352. handler);
  2353. if (!count) {
  2354. kvm_x86_ops->skip_emulated_instruction(vcpu);
  2355. return 1;
  2356. }
  2357. if (!down)
  2358. in_page = PAGE_SIZE - offset_in_page(address);
  2359. else
  2360. in_page = offset_in_page(address) + size;
  2361. now = min(count, (unsigned long)in_page / size);
  2362. if (!now) {
  2363. /*
  2364. * String I/O straddles page boundary. Pin two guest pages
  2365. * so that we satisfy atomicity constraints. Do just one
  2366. * transaction to avoid complexity.
  2367. */
  2368. nr_pages = 2;
  2369. now = 1;
  2370. }
  2371. if (down) {
  2372. /*
  2373. * String I/O in reverse. Yuck. Kill the guest, fix later.
  2374. */
  2375. pr_unimpl(vcpu, "guest string pio down\n");
  2376. kvm_inject_gp(vcpu, 0);
  2377. return 1;
  2378. }
  2379. vcpu->run->io.count = now;
  2380. vcpu->arch.pio.cur_count = now;
  2381. if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count)
  2382. kvm_x86_ops->skip_emulated_instruction(vcpu);
  2383. for (i = 0; i < nr_pages; ++i) {
  2384. page = gva_to_page(vcpu, address + i * PAGE_SIZE);
  2385. vcpu->arch.pio.guest_pages[i] = page;
  2386. if (!page) {
  2387. kvm_inject_gp(vcpu, 0);
  2388. free_pio_guest_pages(vcpu);
  2389. return 1;
  2390. }
  2391. }
  2392. pio_dev = vcpu_find_pio_dev(vcpu, port,
  2393. vcpu->arch.pio.cur_count,
  2394. !vcpu->arch.pio.in);
  2395. if (!vcpu->arch.pio.in) {
  2396. /* string PIO write */
  2397. ret = pio_copy_data(vcpu);
  2398. if (ret >= 0 && pio_dev) {
  2399. pio_string_write(pio_dev, vcpu);
  2400. complete_pio(vcpu);
  2401. if (vcpu->arch.pio.count == 0)
  2402. ret = 1;
  2403. }
  2404. } else if (pio_dev)
  2405. pr_unimpl(vcpu, "no string pio read support yet, "
  2406. "port %x size %d count %ld\n",
  2407. port, size, count);
  2408. return ret;
  2409. }
  2410. EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
  2411. int kvm_arch_init(void *opaque)
  2412. {
  2413. int r;
  2414. struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
  2415. if (kvm_x86_ops) {
  2416. printk(KERN_ERR "kvm: already loaded the other module\n");
  2417. r = -EEXIST;
  2418. goto out;
  2419. }
  2420. if (!ops->cpu_has_kvm_support()) {
  2421. printk(KERN_ERR "kvm: no hardware support\n");
  2422. r = -EOPNOTSUPP;
  2423. goto out;
  2424. }
  2425. if (ops->disabled_by_bios()) {
  2426. printk(KERN_ERR "kvm: disabled by bios\n");
  2427. r = -EOPNOTSUPP;
  2428. goto out;
  2429. }
  2430. r = kvm_mmu_module_init();
  2431. if (r)
  2432. goto out;
  2433. kvm_init_msr_list();
  2434. kvm_x86_ops = ops;
  2435. kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
  2436. kvm_mmu_set_base_ptes(PT_PRESENT_MASK);
  2437. kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
  2438. PT_DIRTY_MASK, PT64_NX_MASK, 0);
  2439. return 0;
  2440. out:
  2441. return r;
  2442. }
  2443. void kvm_arch_exit(void)
  2444. {
  2445. kvm_x86_ops = NULL;
  2446. kvm_mmu_module_exit();
  2447. }
  2448. int kvm_emulate_halt(struct kvm_vcpu *vcpu)
  2449. {
  2450. ++vcpu->stat.halt_exits;
  2451. KVMTRACE_0D(HLT, vcpu, handler);
  2452. if (irqchip_in_kernel(vcpu->kvm)) {
  2453. vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
  2454. up_read(&vcpu->kvm->slots_lock);
  2455. kvm_vcpu_block(vcpu);
  2456. down_read(&vcpu->kvm->slots_lock);
  2457. if (vcpu->arch.mp_state != KVM_MP_STATE_RUNNABLE)
  2458. return -EINTR;
  2459. return 1;
  2460. } else {
  2461. vcpu->run->exit_reason = KVM_EXIT_HLT;
  2462. return 0;
  2463. }
  2464. }
  2465. EXPORT_SYMBOL_GPL(kvm_emulate_halt);
  2466. static inline gpa_t hc_gpa(struct kvm_vcpu *vcpu, unsigned long a0,
  2467. unsigned long a1)
  2468. {
  2469. if (is_long_mode(vcpu))
  2470. return a0;
  2471. else
  2472. return a0 | ((gpa_t)a1 << 32);
  2473. }
  2474. int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
  2475. {
  2476. unsigned long nr, a0, a1, a2, a3, ret;
  2477. int r = 1;
  2478. nr = kvm_register_read(vcpu, VCPU_REGS_RAX);
  2479. a0 = kvm_register_read(vcpu, VCPU_REGS_RBX);
  2480. a1 = kvm_register_read(vcpu, VCPU_REGS_RCX);
  2481. a2 = kvm_register_read(vcpu, VCPU_REGS_RDX);
  2482. a3 = kvm_register_read(vcpu, VCPU_REGS_RSI);
  2483. KVMTRACE_1D(VMMCALL, vcpu, (u32)nr, handler);
  2484. if (!is_long_mode(vcpu)) {
  2485. nr &= 0xFFFFFFFF;
  2486. a0 &= 0xFFFFFFFF;
  2487. a1 &= 0xFFFFFFFF;
  2488. a2 &= 0xFFFFFFFF;
  2489. a3 &= 0xFFFFFFFF;
  2490. }
  2491. switch (nr) {
  2492. case KVM_HC_VAPIC_POLL_IRQ:
  2493. ret = 0;
  2494. break;
  2495. case KVM_HC_MMU_OP:
  2496. r = kvm_pv_mmu_op(vcpu, a0, hc_gpa(vcpu, a1, a2), &ret);
  2497. break;
  2498. default:
  2499. ret = -KVM_ENOSYS;
  2500. break;
  2501. }
  2502. kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
  2503. ++vcpu->stat.hypercalls;
  2504. return r;
  2505. }
  2506. EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
  2507. int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
  2508. {
  2509. char instruction[3];
  2510. int ret = 0;
  2511. unsigned long rip = kvm_rip_read(vcpu);
  2512. /*
  2513. * Blow out the MMU to ensure that no other VCPU has an active mapping
  2514. * to ensure that the updated hypercall appears atomically across all
  2515. * VCPUs.
  2516. */
  2517. kvm_mmu_zap_all(vcpu->kvm);
  2518. kvm_x86_ops->patch_hypercall(vcpu, instruction);
  2519. if (emulator_write_emulated(rip, instruction, 3, vcpu)
  2520. != X86EMUL_CONTINUE)
  2521. ret = -EFAULT;
  2522. return ret;
  2523. }
  2524. static u64 mk_cr_64(u64 curr_cr, u32 new_val)
  2525. {
  2526. return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
  2527. }
  2528. void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
  2529. {
  2530. struct descriptor_table dt = { limit, base };
  2531. kvm_x86_ops->set_gdt(vcpu, &dt);
  2532. }
  2533. void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
  2534. {
  2535. struct descriptor_table dt = { limit, base };
  2536. kvm_x86_ops->set_idt(vcpu, &dt);
  2537. }
  2538. void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
  2539. unsigned long *rflags)
  2540. {
  2541. kvm_lmsw(vcpu, msw);
  2542. *rflags = kvm_x86_ops->get_rflags(vcpu);
  2543. }
  2544. unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
  2545. {
  2546. unsigned long value;
  2547. kvm_x86_ops->decache_cr4_guest_bits(vcpu);
  2548. switch (cr) {
  2549. case 0:
  2550. value = vcpu->arch.cr0;
  2551. break;
  2552. case 2:
  2553. value = vcpu->arch.cr2;
  2554. break;
  2555. case 3:
  2556. value = vcpu->arch.cr3;
  2557. break;
  2558. case 4:
  2559. value = vcpu->arch.cr4;
  2560. break;
  2561. case 8:
  2562. value = kvm_get_cr8(vcpu);
  2563. break;
  2564. default:
  2565. vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
  2566. return 0;
  2567. }
  2568. KVMTRACE_3D(CR_READ, vcpu, (u32)cr, (u32)value,
  2569. (u32)((u64)value >> 32), handler);
  2570. return value;
  2571. }
  2572. void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
  2573. unsigned long *rflags)
  2574. {
  2575. KVMTRACE_3D(CR_WRITE, vcpu, (u32)cr, (u32)val,
  2576. (u32)((u64)val >> 32), handler);
  2577. switch (cr) {
  2578. case 0:
  2579. kvm_set_cr0(vcpu, mk_cr_64(vcpu->arch.cr0, val));
  2580. *rflags = kvm_x86_ops->get_rflags(vcpu);
  2581. break;
  2582. case 2:
  2583. vcpu->arch.cr2 = val;
  2584. break;
  2585. case 3:
  2586. kvm_set_cr3(vcpu, val);
  2587. break;
  2588. case 4:
  2589. kvm_set_cr4(vcpu, mk_cr_64(vcpu->arch.cr4, val));
  2590. break;
  2591. case 8:
  2592. kvm_set_cr8(vcpu, val & 0xfUL);
  2593. break;
  2594. default:
  2595. vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
  2596. }
  2597. }
  2598. static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
  2599. {
  2600. struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
  2601. int j, nent = vcpu->arch.cpuid_nent;
  2602. e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
  2603. /* when no next entry is found, the current entry[i] is reselected */
  2604. for (j = i + 1; j == i; j = (j + 1) % nent) {
  2605. struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
  2606. if (ej->function == e->function) {
  2607. ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
  2608. return j;
  2609. }
  2610. }
  2611. return 0; /* silence gcc, even though control never reaches here */
  2612. }
  2613. /* find an entry with matching function, matching index (if needed), and that
  2614. * should be read next (if it's stateful) */
  2615. static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
  2616. u32 function, u32 index)
  2617. {
  2618. if (e->function != function)
  2619. return 0;
  2620. if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
  2621. return 0;
  2622. if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
  2623. !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
  2624. return 0;
  2625. return 1;
  2626. }
  2627. void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
  2628. {
  2629. int i;
  2630. u32 function, index;
  2631. struct kvm_cpuid_entry2 *e, *best;
  2632. function = kvm_register_read(vcpu, VCPU_REGS_RAX);
  2633. index = kvm_register_read(vcpu, VCPU_REGS_RCX);
  2634. kvm_register_write(vcpu, VCPU_REGS_RAX, 0);
  2635. kvm_register_write(vcpu, VCPU_REGS_RBX, 0);
  2636. kvm_register_write(vcpu, VCPU_REGS_RCX, 0);
  2637. kvm_register_write(vcpu, VCPU_REGS_RDX, 0);
  2638. best = NULL;
  2639. for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
  2640. e = &vcpu->arch.cpuid_entries[i];
  2641. if (is_matching_cpuid_entry(e, function, index)) {
  2642. if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
  2643. move_to_next_stateful_cpuid_entry(vcpu, i);
  2644. best = e;
  2645. break;
  2646. }
  2647. /*
  2648. * Both basic or both extended?
  2649. */
  2650. if (((e->function ^ function) & 0x80000000) == 0)
  2651. if (!best || e->function > best->function)
  2652. best = e;
  2653. }
  2654. if (best) {
  2655. kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax);
  2656. kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx);
  2657. kvm_register_write(vcpu, VCPU_REGS_RCX, best->ecx);
  2658. kvm_register_write(vcpu, VCPU_REGS_RDX, best->edx);
  2659. }
  2660. kvm_x86_ops->skip_emulated_instruction(vcpu);
  2661. KVMTRACE_5D(CPUID, vcpu, function,
  2662. (u32)kvm_register_read(vcpu, VCPU_REGS_RAX),
  2663. (u32)kvm_register_read(vcpu, VCPU_REGS_RBX),
  2664. (u32)kvm_register_read(vcpu, VCPU_REGS_RCX),
  2665. (u32)kvm_register_read(vcpu, VCPU_REGS_RDX), handler);
  2666. }
  2667. EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
  2668. /*
  2669. * Check if userspace requested an interrupt window, and that the
  2670. * interrupt window is open.
  2671. *
  2672. * No need to exit to userspace if we already have an interrupt queued.
  2673. */
  2674. static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
  2675. struct kvm_run *kvm_run)
  2676. {
  2677. return (!vcpu->arch.irq_summary &&
  2678. kvm_run->request_interrupt_window &&
  2679. vcpu->arch.interrupt_window_open &&
  2680. (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
  2681. }
  2682. static void post_kvm_run_save(struct kvm_vcpu *vcpu,
  2683. struct kvm_run *kvm_run)
  2684. {
  2685. kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
  2686. kvm_run->cr8 = kvm_get_cr8(vcpu);
  2687. kvm_run->apic_base = kvm_get_apic_base(vcpu);
  2688. if (irqchip_in_kernel(vcpu->kvm))
  2689. kvm_run->ready_for_interrupt_injection = 1;
  2690. else
  2691. kvm_run->ready_for_interrupt_injection =
  2692. (vcpu->arch.interrupt_window_open &&
  2693. vcpu->arch.irq_summary == 0);
  2694. }
  2695. static void vapic_enter(struct kvm_vcpu *vcpu)
  2696. {
  2697. struct kvm_lapic *apic = vcpu->arch.apic;
  2698. struct page *page;
  2699. if (!apic || !apic->vapic_addr)
  2700. return;
  2701. down_read(&current->mm->mmap_sem);
  2702. page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
  2703. up_read(&current->mm->mmap_sem);
  2704. vcpu->arch.apic->vapic_page = page;
  2705. }
  2706. static void vapic_exit(struct kvm_vcpu *vcpu)
  2707. {
  2708. struct kvm_lapic *apic = vcpu->arch.apic;
  2709. if (!apic || !apic->vapic_addr)
  2710. return;
  2711. down_read(&vcpu->kvm->slots_lock);
  2712. kvm_release_page_dirty(apic->vapic_page);
  2713. mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
  2714. up_read(&vcpu->kvm->slots_lock);
  2715. }
  2716. static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2717. {
  2718. int r;
  2719. if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED)) {
  2720. pr_debug("vcpu %d received sipi with vector # %x\n",
  2721. vcpu->vcpu_id, vcpu->arch.sipi_vector);
  2722. kvm_lapic_reset(vcpu);
  2723. r = kvm_x86_ops->vcpu_reset(vcpu);
  2724. if (r)
  2725. return r;
  2726. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  2727. }
  2728. down_read(&vcpu->kvm->slots_lock);
  2729. vapic_enter(vcpu);
  2730. again:
  2731. if (vcpu->requests)
  2732. if (test_and_clear_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
  2733. kvm_mmu_unload(vcpu);
  2734. r = kvm_mmu_reload(vcpu);
  2735. if (unlikely(r))
  2736. goto out;
  2737. if (vcpu->requests) {
  2738. if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests))
  2739. __kvm_migrate_timers(vcpu);
  2740. if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
  2741. kvm_x86_ops->tlb_flush(vcpu);
  2742. if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS,
  2743. &vcpu->requests)) {
  2744. kvm_run->exit_reason = KVM_EXIT_TPR_ACCESS;
  2745. r = 0;
  2746. goto out;
  2747. }
  2748. if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests)) {
  2749. kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
  2750. r = 0;
  2751. goto out;
  2752. }
  2753. }
  2754. clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests);
  2755. kvm_inject_pending_timer_irqs(vcpu);
  2756. preempt_disable();
  2757. kvm_x86_ops->prepare_guest_switch(vcpu);
  2758. kvm_load_guest_fpu(vcpu);
  2759. local_irq_disable();
  2760. if (vcpu->requests || need_resched()) {
  2761. local_irq_enable();
  2762. preempt_enable();
  2763. r = 1;
  2764. goto out;
  2765. }
  2766. if (signal_pending(current)) {
  2767. local_irq_enable();
  2768. preempt_enable();
  2769. r = -EINTR;
  2770. kvm_run->exit_reason = KVM_EXIT_INTR;
  2771. ++vcpu->stat.signal_exits;
  2772. goto out;
  2773. }
  2774. if (vcpu->guest_debug.enabled)
  2775. kvm_x86_ops->guest_debug_pre(vcpu);
  2776. vcpu->guest_mode = 1;
  2777. /*
  2778. * Make sure that guest_mode assignment won't happen after
  2779. * testing the pending IRQ vector bitmap.
  2780. */
  2781. smp_wmb();
  2782. if (vcpu->arch.exception.pending)
  2783. __queue_exception(vcpu);
  2784. else if (irqchip_in_kernel(vcpu->kvm))
  2785. kvm_x86_ops->inject_pending_irq(vcpu);
  2786. else
  2787. kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
  2788. kvm_lapic_sync_to_vapic(vcpu);
  2789. up_read(&vcpu->kvm->slots_lock);
  2790. kvm_guest_enter();
  2791. KVMTRACE_0D(VMENTRY, vcpu, entryexit);
  2792. kvm_x86_ops->run(vcpu, kvm_run);
  2793. vcpu->guest_mode = 0;
  2794. local_irq_enable();
  2795. ++vcpu->stat.exits;
  2796. /*
  2797. * We must have an instruction between local_irq_enable() and
  2798. * kvm_guest_exit(), so the timer interrupt isn't delayed by
  2799. * the interrupt shadow. The stat.exits increment will do nicely.
  2800. * But we need to prevent reordering, hence this barrier():
  2801. */
  2802. barrier();
  2803. kvm_guest_exit();
  2804. preempt_enable();
  2805. down_read(&vcpu->kvm->slots_lock);
  2806. /*
  2807. * Profile KVM exit RIPs:
  2808. */
  2809. if (unlikely(prof_on == KVM_PROFILING)) {
  2810. unsigned long rip = kvm_rip_read(vcpu);
  2811. profile_hit(KVM_PROFILING, (void *)rip);
  2812. }
  2813. if (vcpu->arch.exception.pending && kvm_x86_ops->exception_injected(vcpu))
  2814. vcpu->arch.exception.pending = false;
  2815. kvm_lapic_sync_from_vapic(vcpu);
  2816. r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
  2817. if (r > 0) {
  2818. if (dm_request_for_irq_injection(vcpu, kvm_run)) {
  2819. r = -EINTR;
  2820. kvm_run->exit_reason = KVM_EXIT_INTR;
  2821. ++vcpu->stat.request_irq_exits;
  2822. goto out;
  2823. }
  2824. if (!need_resched())
  2825. goto again;
  2826. }
  2827. out:
  2828. up_read(&vcpu->kvm->slots_lock);
  2829. if (r > 0) {
  2830. kvm_resched(vcpu);
  2831. down_read(&vcpu->kvm->slots_lock);
  2832. goto again;
  2833. }
  2834. post_kvm_run_save(vcpu, kvm_run);
  2835. vapic_exit(vcpu);
  2836. return r;
  2837. }
  2838. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2839. {
  2840. int r;
  2841. sigset_t sigsaved;
  2842. vcpu_load(vcpu);
  2843. if (vcpu->sigset_active)
  2844. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  2845. if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
  2846. kvm_vcpu_block(vcpu);
  2847. r = -EAGAIN;
  2848. goto out;
  2849. }
  2850. /* re-sync apic's tpr */
  2851. if (!irqchip_in_kernel(vcpu->kvm))
  2852. kvm_set_cr8(vcpu, kvm_run->cr8);
  2853. if (vcpu->arch.pio.cur_count) {
  2854. r = complete_pio(vcpu);
  2855. if (r)
  2856. goto out;
  2857. }
  2858. #if CONFIG_HAS_IOMEM
  2859. if (vcpu->mmio_needed) {
  2860. memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
  2861. vcpu->mmio_read_completed = 1;
  2862. vcpu->mmio_needed = 0;
  2863. down_read(&vcpu->kvm->slots_lock);
  2864. r = emulate_instruction(vcpu, kvm_run,
  2865. vcpu->arch.mmio_fault_cr2, 0,
  2866. EMULTYPE_NO_DECODE);
  2867. up_read(&vcpu->kvm->slots_lock);
  2868. if (r == EMULATE_DO_MMIO) {
  2869. /*
  2870. * Read-modify-write. Back to userspace.
  2871. */
  2872. r = 0;
  2873. goto out;
  2874. }
  2875. }
  2876. #endif
  2877. if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL)
  2878. kvm_register_write(vcpu, VCPU_REGS_RAX,
  2879. kvm_run->hypercall.ret);
  2880. r = __vcpu_run(vcpu, kvm_run);
  2881. out:
  2882. if (vcpu->sigset_active)
  2883. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  2884. vcpu_put(vcpu);
  2885. return r;
  2886. }
  2887. int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  2888. {
  2889. vcpu_load(vcpu);
  2890. regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
  2891. regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX);
  2892. regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX);
  2893. regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX);
  2894. regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI);
  2895. regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI);
  2896. regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
  2897. regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP);
  2898. #ifdef CONFIG_X86_64
  2899. regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8);
  2900. regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9);
  2901. regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10);
  2902. regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11);
  2903. regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12);
  2904. regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13);
  2905. regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14);
  2906. regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15);
  2907. #endif
  2908. regs->rip = kvm_rip_read(vcpu);
  2909. regs->rflags = kvm_x86_ops->get_rflags(vcpu);
  2910. /*
  2911. * Don't leak debug flags in case they were set for guest debugging
  2912. */
  2913. if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
  2914. regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
  2915. vcpu_put(vcpu);
  2916. return 0;
  2917. }
  2918. int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  2919. {
  2920. vcpu_load(vcpu);
  2921. kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax);
  2922. kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx);
  2923. kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx);
  2924. kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx);
  2925. kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi);
  2926. kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi);
  2927. kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp);
  2928. kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp);
  2929. #ifdef CONFIG_X86_64
  2930. kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8);
  2931. kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9);
  2932. kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10);
  2933. kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11);
  2934. kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12);
  2935. kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13);
  2936. kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14);
  2937. kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15);
  2938. #endif
  2939. kvm_rip_write(vcpu, regs->rip);
  2940. kvm_x86_ops->set_rflags(vcpu, regs->rflags);
  2941. vcpu->arch.exception.pending = false;
  2942. vcpu_put(vcpu);
  2943. return 0;
  2944. }
  2945. void kvm_get_segment(struct kvm_vcpu *vcpu,
  2946. struct kvm_segment *var, int seg)
  2947. {
  2948. kvm_x86_ops->get_segment(vcpu, var, seg);
  2949. }
  2950. void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
  2951. {
  2952. struct kvm_segment cs;
  2953. kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
  2954. *db = cs.db;
  2955. *l = cs.l;
  2956. }
  2957. EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
  2958. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  2959. struct kvm_sregs *sregs)
  2960. {
  2961. struct descriptor_table dt;
  2962. int pending_vec;
  2963. vcpu_load(vcpu);
  2964. kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  2965. kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  2966. kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  2967. kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  2968. kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  2969. kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  2970. kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  2971. kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  2972. kvm_x86_ops->get_idt(vcpu, &dt);
  2973. sregs->idt.limit = dt.limit;
  2974. sregs->idt.base = dt.base;
  2975. kvm_x86_ops->get_gdt(vcpu, &dt);
  2976. sregs->gdt.limit = dt.limit;
  2977. sregs->gdt.base = dt.base;
  2978. kvm_x86_ops->decache_cr4_guest_bits(vcpu);
  2979. sregs->cr0 = vcpu->arch.cr0;
  2980. sregs->cr2 = vcpu->arch.cr2;
  2981. sregs->cr3 = vcpu->arch.cr3;
  2982. sregs->cr4 = vcpu->arch.cr4;
  2983. sregs->cr8 = kvm_get_cr8(vcpu);
  2984. sregs->efer = vcpu->arch.shadow_efer;
  2985. sregs->apic_base = kvm_get_apic_base(vcpu);
  2986. if (irqchip_in_kernel(vcpu->kvm)) {
  2987. memset(sregs->interrupt_bitmap, 0,
  2988. sizeof sregs->interrupt_bitmap);
  2989. pending_vec = kvm_x86_ops->get_irq(vcpu);
  2990. if (pending_vec >= 0)
  2991. set_bit(pending_vec,
  2992. (unsigned long *)sregs->interrupt_bitmap);
  2993. } else
  2994. memcpy(sregs->interrupt_bitmap, vcpu->arch.irq_pending,
  2995. sizeof sregs->interrupt_bitmap);
  2996. vcpu_put(vcpu);
  2997. return 0;
  2998. }
  2999. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  3000. struct kvm_mp_state *mp_state)
  3001. {
  3002. vcpu_load(vcpu);
  3003. mp_state->mp_state = vcpu->arch.mp_state;
  3004. vcpu_put(vcpu);
  3005. return 0;
  3006. }
  3007. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  3008. struct kvm_mp_state *mp_state)
  3009. {
  3010. vcpu_load(vcpu);
  3011. vcpu->arch.mp_state = mp_state->mp_state;
  3012. vcpu_put(vcpu);
  3013. return 0;
  3014. }
  3015. static void kvm_set_segment(struct kvm_vcpu *vcpu,
  3016. struct kvm_segment *var, int seg)
  3017. {
  3018. kvm_x86_ops->set_segment(vcpu, var, seg);
  3019. }
  3020. static void seg_desct_to_kvm_desct(struct desc_struct *seg_desc, u16 selector,
  3021. struct kvm_segment *kvm_desct)
  3022. {
  3023. kvm_desct->base = seg_desc->base0;
  3024. kvm_desct->base |= seg_desc->base1 << 16;
  3025. kvm_desct->base |= seg_desc->base2 << 24;
  3026. kvm_desct->limit = seg_desc->limit0;
  3027. kvm_desct->limit |= seg_desc->limit << 16;
  3028. if (seg_desc->g) {
  3029. kvm_desct->limit <<= 12;
  3030. kvm_desct->limit |= 0xfff;
  3031. }
  3032. kvm_desct->selector = selector;
  3033. kvm_desct->type = seg_desc->type;
  3034. kvm_desct->present = seg_desc->p;
  3035. kvm_desct->dpl = seg_desc->dpl;
  3036. kvm_desct->db = seg_desc->d;
  3037. kvm_desct->s = seg_desc->s;
  3038. kvm_desct->l = seg_desc->l;
  3039. kvm_desct->g = seg_desc->g;
  3040. kvm_desct->avl = seg_desc->avl;
  3041. if (!selector)
  3042. kvm_desct->unusable = 1;
  3043. else
  3044. kvm_desct->unusable = 0;
  3045. kvm_desct->padding = 0;
  3046. }
  3047. static void get_segment_descritptor_dtable(struct kvm_vcpu *vcpu,
  3048. u16 selector,
  3049. struct descriptor_table *dtable)
  3050. {
  3051. if (selector & 1 << 2) {
  3052. struct kvm_segment kvm_seg;
  3053. kvm_get_segment(vcpu, &kvm_seg, VCPU_SREG_LDTR);
  3054. if (kvm_seg.unusable)
  3055. dtable->limit = 0;
  3056. else
  3057. dtable->limit = kvm_seg.limit;
  3058. dtable->base = kvm_seg.base;
  3059. }
  3060. else
  3061. kvm_x86_ops->get_gdt(vcpu, dtable);
  3062. }
  3063. /* allowed just for 8 bytes segments */
  3064. static int load_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
  3065. struct desc_struct *seg_desc)
  3066. {
  3067. gpa_t gpa;
  3068. struct descriptor_table dtable;
  3069. u16 index = selector >> 3;
  3070. get_segment_descritptor_dtable(vcpu, selector, &dtable);
  3071. if (dtable.limit < index * 8 + 7) {
  3072. kvm_queue_exception_e(vcpu, GP_VECTOR, selector & 0xfffc);
  3073. return 1;
  3074. }
  3075. gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, dtable.base);
  3076. gpa += index * 8;
  3077. return kvm_read_guest(vcpu->kvm, gpa, seg_desc, 8);
  3078. }
  3079. /* allowed just for 8 bytes segments */
  3080. static int save_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
  3081. struct desc_struct *seg_desc)
  3082. {
  3083. gpa_t gpa;
  3084. struct descriptor_table dtable;
  3085. u16 index = selector >> 3;
  3086. get_segment_descritptor_dtable(vcpu, selector, &dtable);
  3087. if (dtable.limit < index * 8 + 7)
  3088. return 1;
  3089. gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, dtable.base);
  3090. gpa += index * 8;
  3091. return kvm_write_guest(vcpu->kvm, gpa, seg_desc, 8);
  3092. }
  3093. static u32 get_tss_base_addr(struct kvm_vcpu *vcpu,
  3094. struct desc_struct *seg_desc)
  3095. {
  3096. u32 base_addr;
  3097. base_addr = seg_desc->base0;
  3098. base_addr |= (seg_desc->base1 << 16);
  3099. base_addr |= (seg_desc->base2 << 24);
  3100. return vcpu->arch.mmu.gva_to_gpa(vcpu, base_addr);
  3101. }
  3102. static u16 get_segment_selector(struct kvm_vcpu *vcpu, int seg)
  3103. {
  3104. struct kvm_segment kvm_seg;
  3105. kvm_get_segment(vcpu, &kvm_seg, seg);
  3106. return kvm_seg.selector;
  3107. }
  3108. static int load_segment_descriptor_to_kvm_desct(struct kvm_vcpu *vcpu,
  3109. u16 selector,
  3110. struct kvm_segment *kvm_seg)
  3111. {
  3112. struct desc_struct seg_desc;
  3113. if (load_guest_segment_descriptor(vcpu, selector, &seg_desc))
  3114. return 1;
  3115. seg_desct_to_kvm_desct(&seg_desc, selector, kvm_seg);
  3116. return 0;
  3117. }
  3118. int kvm_load_realmode_segment(struct kvm_vcpu *vcpu, u16 selector, int seg)
  3119. {
  3120. struct kvm_segment segvar = {
  3121. .base = selector << 4,
  3122. .limit = 0xffff,
  3123. .selector = selector,
  3124. .type = 3,
  3125. .present = 1,
  3126. .dpl = 3,
  3127. .db = 0,
  3128. .s = 1,
  3129. .l = 0,
  3130. .g = 0,
  3131. .avl = 0,
  3132. .unusable = 0,
  3133. };
  3134. kvm_x86_ops->set_segment(vcpu, &segvar, seg);
  3135. return 0;
  3136. }
  3137. int kvm_load_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
  3138. int type_bits, int seg)
  3139. {
  3140. struct kvm_segment kvm_seg;
  3141. if (!(vcpu->arch.cr0 & X86_CR0_PE))
  3142. return kvm_load_realmode_segment(vcpu, selector, seg);
  3143. if (load_segment_descriptor_to_kvm_desct(vcpu, selector, &kvm_seg))
  3144. return 1;
  3145. kvm_seg.type |= type_bits;
  3146. if (seg != VCPU_SREG_SS && seg != VCPU_SREG_CS &&
  3147. seg != VCPU_SREG_LDTR)
  3148. if (!kvm_seg.s)
  3149. kvm_seg.unusable = 1;
  3150. kvm_set_segment(vcpu, &kvm_seg, seg);
  3151. return 0;
  3152. }
  3153. static void save_state_to_tss32(struct kvm_vcpu *vcpu,
  3154. struct tss_segment_32 *tss)
  3155. {
  3156. tss->cr3 = vcpu->arch.cr3;
  3157. tss->eip = kvm_rip_read(vcpu);
  3158. tss->eflags = kvm_x86_ops->get_rflags(vcpu);
  3159. tss->eax = kvm_register_read(vcpu, VCPU_REGS_RAX);
  3160. tss->ecx = kvm_register_read(vcpu, VCPU_REGS_RCX);
  3161. tss->edx = kvm_register_read(vcpu, VCPU_REGS_RDX);
  3162. tss->ebx = kvm_register_read(vcpu, VCPU_REGS_RBX);
  3163. tss->esp = kvm_register_read(vcpu, VCPU_REGS_RSP);
  3164. tss->ebp = kvm_register_read(vcpu, VCPU_REGS_RBP);
  3165. tss->esi = kvm_register_read(vcpu, VCPU_REGS_RSI);
  3166. tss->edi = kvm_register_read(vcpu, VCPU_REGS_RDI);
  3167. tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
  3168. tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
  3169. tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
  3170. tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
  3171. tss->fs = get_segment_selector(vcpu, VCPU_SREG_FS);
  3172. tss->gs = get_segment_selector(vcpu, VCPU_SREG_GS);
  3173. tss->ldt_selector = get_segment_selector(vcpu, VCPU_SREG_LDTR);
  3174. tss->prev_task_link = get_segment_selector(vcpu, VCPU_SREG_TR);
  3175. }
  3176. static int load_state_from_tss32(struct kvm_vcpu *vcpu,
  3177. struct tss_segment_32 *tss)
  3178. {
  3179. kvm_set_cr3(vcpu, tss->cr3);
  3180. kvm_rip_write(vcpu, tss->eip);
  3181. kvm_x86_ops->set_rflags(vcpu, tss->eflags | 2);
  3182. kvm_register_write(vcpu, VCPU_REGS_RAX, tss->eax);
  3183. kvm_register_write(vcpu, VCPU_REGS_RCX, tss->ecx);
  3184. kvm_register_write(vcpu, VCPU_REGS_RDX, tss->edx);
  3185. kvm_register_write(vcpu, VCPU_REGS_RBX, tss->ebx);
  3186. kvm_register_write(vcpu, VCPU_REGS_RSP, tss->esp);
  3187. kvm_register_write(vcpu, VCPU_REGS_RBP, tss->ebp);
  3188. kvm_register_write(vcpu, VCPU_REGS_RSI, tss->esi);
  3189. kvm_register_write(vcpu, VCPU_REGS_RDI, tss->edi);
  3190. if (kvm_load_segment_descriptor(vcpu, tss->ldt_selector, 0, VCPU_SREG_LDTR))
  3191. return 1;
  3192. if (kvm_load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES))
  3193. return 1;
  3194. if (kvm_load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS))
  3195. return 1;
  3196. if (kvm_load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS))
  3197. return 1;
  3198. if (kvm_load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS))
  3199. return 1;
  3200. if (kvm_load_segment_descriptor(vcpu, tss->fs, 1, VCPU_SREG_FS))
  3201. return 1;
  3202. if (kvm_load_segment_descriptor(vcpu, tss->gs, 1, VCPU_SREG_GS))
  3203. return 1;
  3204. return 0;
  3205. }
  3206. static void save_state_to_tss16(struct kvm_vcpu *vcpu,
  3207. struct tss_segment_16 *tss)
  3208. {
  3209. tss->ip = kvm_rip_read(vcpu);
  3210. tss->flag = kvm_x86_ops->get_rflags(vcpu);
  3211. tss->ax = kvm_register_read(vcpu, VCPU_REGS_RAX);
  3212. tss->cx = kvm_register_read(vcpu, VCPU_REGS_RCX);
  3213. tss->dx = kvm_register_read(vcpu, VCPU_REGS_RDX);
  3214. tss->bx = kvm_register_read(vcpu, VCPU_REGS_RBX);
  3215. tss->sp = kvm_register_read(vcpu, VCPU_REGS_RSP);
  3216. tss->bp = kvm_register_read(vcpu, VCPU_REGS_RBP);
  3217. tss->si = kvm_register_read(vcpu, VCPU_REGS_RSI);
  3218. tss->di = kvm_register_read(vcpu, VCPU_REGS_RDI);
  3219. tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
  3220. tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
  3221. tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
  3222. tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
  3223. tss->ldt = get_segment_selector(vcpu, VCPU_SREG_LDTR);
  3224. tss->prev_task_link = get_segment_selector(vcpu, VCPU_SREG_TR);
  3225. }
  3226. static int load_state_from_tss16(struct kvm_vcpu *vcpu,
  3227. struct tss_segment_16 *tss)
  3228. {
  3229. kvm_rip_write(vcpu, tss->ip);
  3230. kvm_x86_ops->set_rflags(vcpu, tss->flag | 2);
  3231. kvm_register_write(vcpu, VCPU_REGS_RAX, tss->ax);
  3232. kvm_register_write(vcpu, VCPU_REGS_RCX, tss->cx);
  3233. kvm_register_write(vcpu, VCPU_REGS_RDX, tss->dx);
  3234. kvm_register_write(vcpu, VCPU_REGS_RBX, tss->bx);
  3235. kvm_register_write(vcpu, VCPU_REGS_RSP, tss->sp);
  3236. kvm_register_write(vcpu, VCPU_REGS_RBP, tss->bp);
  3237. kvm_register_write(vcpu, VCPU_REGS_RSI, tss->si);
  3238. kvm_register_write(vcpu, VCPU_REGS_RDI, tss->di);
  3239. if (kvm_load_segment_descriptor(vcpu, tss->ldt, 0, VCPU_SREG_LDTR))
  3240. return 1;
  3241. if (kvm_load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES))
  3242. return 1;
  3243. if (kvm_load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS))
  3244. return 1;
  3245. if (kvm_load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS))
  3246. return 1;
  3247. if (kvm_load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS))
  3248. return 1;
  3249. return 0;
  3250. }
  3251. static int kvm_task_switch_16(struct kvm_vcpu *vcpu, u16 tss_selector,
  3252. u32 old_tss_base,
  3253. struct desc_struct *nseg_desc)
  3254. {
  3255. struct tss_segment_16 tss_segment_16;
  3256. int ret = 0;
  3257. if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_16,
  3258. sizeof tss_segment_16))
  3259. goto out;
  3260. save_state_to_tss16(vcpu, &tss_segment_16);
  3261. if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_16,
  3262. sizeof tss_segment_16))
  3263. goto out;
  3264. if (kvm_read_guest(vcpu->kvm, get_tss_base_addr(vcpu, nseg_desc),
  3265. &tss_segment_16, sizeof tss_segment_16))
  3266. goto out;
  3267. if (load_state_from_tss16(vcpu, &tss_segment_16))
  3268. goto out;
  3269. ret = 1;
  3270. out:
  3271. return ret;
  3272. }
  3273. static int kvm_task_switch_32(struct kvm_vcpu *vcpu, u16 tss_selector,
  3274. u32 old_tss_base,
  3275. struct desc_struct *nseg_desc)
  3276. {
  3277. struct tss_segment_32 tss_segment_32;
  3278. int ret = 0;
  3279. if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_32,
  3280. sizeof tss_segment_32))
  3281. goto out;
  3282. save_state_to_tss32(vcpu, &tss_segment_32);
  3283. if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_32,
  3284. sizeof tss_segment_32))
  3285. goto out;
  3286. if (kvm_read_guest(vcpu->kvm, get_tss_base_addr(vcpu, nseg_desc),
  3287. &tss_segment_32, sizeof tss_segment_32))
  3288. goto out;
  3289. if (load_state_from_tss32(vcpu, &tss_segment_32))
  3290. goto out;
  3291. ret = 1;
  3292. out:
  3293. return ret;
  3294. }
  3295. int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason)
  3296. {
  3297. struct kvm_segment tr_seg;
  3298. struct desc_struct cseg_desc;
  3299. struct desc_struct nseg_desc;
  3300. int ret = 0;
  3301. u32 old_tss_base = get_segment_base(vcpu, VCPU_SREG_TR);
  3302. u16 old_tss_sel = get_segment_selector(vcpu, VCPU_SREG_TR);
  3303. old_tss_base = vcpu->arch.mmu.gva_to_gpa(vcpu, old_tss_base);
  3304. /* FIXME: Handle errors. Failure to read either TSS or their
  3305. * descriptors should generate a pagefault.
  3306. */
  3307. if (load_guest_segment_descriptor(vcpu, tss_selector, &nseg_desc))
  3308. goto out;
  3309. if (load_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc))
  3310. goto out;
  3311. if (reason != TASK_SWITCH_IRET) {
  3312. int cpl;
  3313. cpl = kvm_x86_ops->get_cpl(vcpu);
  3314. if ((tss_selector & 3) > nseg_desc.dpl || cpl > nseg_desc.dpl) {
  3315. kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
  3316. return 1;
  3317. }
  3318. }
  3319. if (!nseg_desc.p || (nseg_desc.limit0 | nseg_desc.limit << 16) < 0x67) {
  3320. kvm_queue_exception_e(vcpu, TS_VECTOR, tss_selector & 0xfffc);
  3321. return 1;
  3322. }
  3323. if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
  3324. cseg_desc.type &= ~(1 << 1); //clear the B flag
  3325. save_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc);
  3326. }
  3327. if (reason == TASK_SWITCH_IRET) {
  3328. u32 eflags = kvm_x86_ops->get_rflags(vcpu);
  3329. kvm_x86_ops->set_rflags(vcpu, eflags & ~X86_EFLAGS_NT);
  3330. }
  3331. kvm_x86_ops->skip_emulated_instruction(vcpu);
  3332. if (nseg_desc.type & 8)
  3333. ret = kvm_task_switch_32(vcpu, tss_selector, old_tss_base,
  3334. &nseg_desc);
  3335. else
  3336. ret = kvm_task_switch_16(vcpu, tss_selector, old_tss_base,
  3337. &nseg_desc);
  3338. if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) {
  3339. u32 eflags = kvm_x86_ops->get_rflags(vcpu);
  3340. kvm_x86_ops->set_rflags(vcpu, eflags | X86_EFLAGS_NT);
  3341. }
  3342. if (reason != TASK_SWITCH_IRET) {
  3343. nseg_desc.type |= (1 << 1);
  3344. save_guest_segment_descriptor(vcpu, tss_selector,
  3345. &nseg_desc);
  3346. }
  3347. kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 | X86_CR0_TS);
  3348. seg_desct_to_kvm_desct(&nseg_desc, tss_selector, &tr_seg);
  3349. tr_seg.type = 11;
  3350. kvm_set_segment(vcpu, &tr_seg, VCPU_SREG_TR);
  3351. out:
  3352. return ret;
  3353. }
  3354. EXPORT_SYMBOL_GPL(kvm_task_switch);
  3355. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  3356. struct kvm_sregs *sregs)
  3357. {
  3358. int mmu_reset_needed = 0;
  3359. int i, pending_vec, max_bits;
  3360. struct descriptor_table dt;
  3361. vcpu_load(vcpu);
  3362. dt.limit = sregs->idt.limit;
  3363. dt.base = sregs->idt.base;
  3364. kvm_x86_ops->set_idt(vcpu, &dt);
  3365. dt.limit = sregs->gdt.limit;
  3366. dt.base = sregs->gdt.base;
  3367. kvm_x86_ops->set_gdt(vcpu, &dt);
  3368. vcpu->arch.cr2 = sregs->cr2;
  3369. mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3;
  3370. vcpu->arch.cr3 = sregs->cr3;
  3371. kvm_set_cr8(vcpu, sregs->cr8);
  3372. mmu_reset_needed |= vcpu->arch.shadow_efer != sregs->efer;
  3373. kvm_x86_ops->set_efer(vcpu, sregs->efer);
  3374. kvm_set_apic_base(vcpu, sregs->apic_base);
  3375. kvm_x86_ops->decache_cr4_guest_bits(vcpu);
  3376. mmu_reset_needed |= vcpu->arch.cr0 != sregs->cr0;
  3377. kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
  3378. vcpu->arch.cr0 = sregs->cr0;
  3379. mmu_reset_needed |= vcpu->arch.cr4 != sregs->cr4;
  3380. kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
  3381. if (!is_long_mode(vcpu) && is_pae(vcpu))
  3382. load_pdptrs(vcpu, vcpu->arch.cr3);
  3383. if (mmu_reset_needed)
  3384. kvm_mmu_reset_context(vcpu);
  3385. if (!irqchip_in_kernel(vcpu->kvm)) {
  3386. memcpy(vcpu->arch.irq_pending, sregs->interrupt_bitmap,
  3387. sizeof vcpu->arch.irq_pending);
  3388. vcpu->arch.irq_summary = 0;
  3389. for (i = 0; i < ARRAY_SIZE(vcpu->arch.irq_pending); ++i)
  3390. if (vcpu->arch.irq_pending[i])
  3391. __set_bit(i, &vcpu->arch.irq_summary);
  3392. } else {
  3393. max_bits = (sizeof sregs->interrupt_bitmap) << 3;
  3394. pending_vec = find_first_bit(
  3395. (const unsigned long *)sregs->interrupt_bitmap,
  3396. max_bits);
  3397. /* Only pending external irq is handled here */
  3398. if (pending_vec < max_bits) {
  3399. kvm_x86_ops->set_irq(vcpu, pending_vec);
  3400. pr_debug("Set back pending irq %d\n",
  3401. pending_vec);
  3402. }
  3403. }
  3404. kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  3405. kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  3406. kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  3407. kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  3408. kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  3409. kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  3410. kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  3411. kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  3412. vcpu_put(vcpu);
  3413. return 0;
  3414. }
  3415. int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
  3416. struct kvm_debug_guest *dbg)
  3417. {
  3418. int r;
  3419. vcpu_load(vcpu);
  3420. r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
  3421. vcpu_put(vcpu);
  3422. return r;
  3423. }
  3424. /*
  3425. * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
  3426. * we have asm/x86/processor.h
  3427. */
  3428. struct fxsave {
  3429. u16 cwd;
  3430. u16 swd;
  3431. u16 twd;
  3432. u16 fop;
  3433. u64 rip;
  3434. u64 rdp;
  3435. u32 mxcsr;
  3436. u32 mxcsr_mask;
  3437. u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
  3438. #ifdef CONFIG_X86_64
  3439. u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
  3440. #else
  3441. u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
  3442. #endif
  3443. };
  3444. /*
  3445. * Translate a guest virtual address to a guest physical address.
  3446. */
  3447. int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  3448. struct kvm_translation *tr)
  3449. {
  3450. unsigned long vaddr = tr->linear_address;
  3451. gpa_t gpa;
  3452. vcpu_load(vcpu);
  3453. down_read(&vcpu->kvm->slots_lock);
  3454. gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, vaddr);
  3455. up_read(&vcpu->kvm->slots_lock);
  3456. tr->physical_address = gpa;
  3457. tr->valid = gpa != UNMAPPED_GVA;
  3458. tr->writeable = 1;
  3459. tr->usermode = 0;
  3460. vcpu_put(vcpu);
  3461. return 0;
  3462. }
  3463. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  3464. {
  3465. struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
  3466. vcpu_load(vcpu);
  3467. memcpy(fpu->fpr, fxsave->st_space, 128);
  3468. fpu->fcw = fxsave->cwd;
  3469. fpu->fsw = fxsave->swd;
  3470. fpu->ftwx = fxsave->twd;
  3471. fpu->last_opcode = fxsave->fop;
  3472. fpu->last_ip = fxsave->rip;
  3473. fpu->last_dp = fxsave->rdp;
  3474. memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
  3475. vcpu_put(vcpu);
  3476. return 0;
  3477. }
  3478. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  3479. {
  3480. struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
  3481. vcpu_load(vcpu);
  3482. memcpy(fxsave->st_space, fpu->fpr, 128);
  3483. fxsave->cwd = fpu->fcw;
  3484. fxsave->swd = fpu->fsw;
  3485. fxsave->twd = fpu->ftwx;
  3486. fxsave->fop = fpu->last_opcode;
  3487. fxsave->rip = fpu->last_ip;
  3488. fxsave->rdp = fpu->last_dp;
  3489. memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
  3490. vcpu_put(vcpu);
  3491. return 0;
  3492. }
  3493. void fx_init(struct kvm_vcpu *vcpu)
  3494. {
  3495. unsigned after_mxcsr_mask;
  3496. /*
  3497. * Touch the fpu the first time in non atomic context as if
  3498. * this is the first fpu instruction the exception handler
  3499. * will fire before the instruction returns and it'll have to
  3500. * allocate ram with GFP_KERNEL.
  3501. */
  3502. if (!used_math())
  3503. kvm_fx_save(&vcpu->arch.host_fx_image);
  3504. /* Initialize guest FPU by resetting ours and saving into guest's */
  3505. preempt_disable();
  3506. kvm_fx_save(&vcpu->arch.host_fx_image);
  3507. kvm_fx_finit();
  3508. kvm_fx_save(&vcpu->arch.guest_fx_image);
  3509. kvm_fx_restore(&vcpu->arch.host_fx_image);
  3510. preempt_enable();
  3511. vcpu->arch.cr0 |= X86_CR0_ET;
  3512. after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
  3513. vcpu->arch.guest_fx_image.mxcsr = 0x1f80;
  3514. memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask,
  3515. 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
  3516. }
  3517. EXPORT_SYMBOL_GPL(fx_init);
  3518. void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
  3519. {
  3520. if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
  3521. return;
  3522. vcpu->guest_fpu_loaded = 1;
  3523. kvm_fx_save(&vcpu->arch.host_fx_image);
  3524. kvm_fx_restore(&vcpu->arch.guest_fx_image);
  3525. }
  3526. EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
  3527. void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
  3528. {
  3529. if (!vcpu->guest_fpu_loaded)
  3530. return;
  3531. vcpu->guest_fpu_loaded = 0;
  3532. kvm_fx_save(&vcpu->arch.guest_fx_image);
  3533. kvm_fx_restore(&vcpu->arch.host_fx_image);
  3534. ++vcpu->stat.fpu_reload;
  3535. }
  3536. EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
  3537. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  3538. {
  3539. kvm_x86_ops->vcpu_free(vcpu);
  3540. }
  3541. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
  3542. unsigned int id)
  3543. {
  3544. return kvm_x86_ops->vcpu_create(kvm, id);
  3545. }
  3546. int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
  3547. {
  3548. int r;
  3549. /* We do fxsave: this must be aligned. */
  3550. BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF);
  3551. vcpu_load(vcpu);
  3552. r = kvm_arch_vcpu_reset(vcpu);
  3553. if (r == 0)
  3554. r = kvm_mmu_setup(vcpu);
  3555. vcpu_put(vcpu);
  3556. if (r < 0)
  3557. goto free_vcpu;
  3558. return 0;
  3559. free_vcpu:
  3560. kvm_x86_ops->vcpu_free(vcpu);
  3561. return r;
  3562. }
  3563. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  3564. {
  3565. vcpu_load(vcpu);
  3566. kvm_mmu_unload(vcpu);
  3567. vcpu_put(vcpu);
  3568. kvm_x86_ops->vcpu_free(vcpu);
  3569. }
  3570. int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
  3571. {
  3572. return kvm_x86_ops->vcpu_reset(vcpu);
  3573. }
  3574. void kvm_arch_hardware_enable(void *garbage)
  3575. {
  3576. kvm_x86_ops->hardware_enable(garbage);
  3577. }
  3578. void kvm_arch_hardware_disable(void *garbage)
  3579. {
  3580. kvm_x86_ops->hardware_disable(garbage);
  3581. }
  3582. int kvm_arch_hardware_setup(void)
  3583. {
  3584. return kvm_x86_ops->hardware_setup();
  3585. }
  3586. void kvm_arch_hardware_unsetup(void)
  3587. {
  3588. kvm_x86_ops->hardware_unsetup();
  3589. }
  3590. void kvm_arch_check_processor_compat(void *rtn)
  3591. {
  3592. kvm_x86_ops->check_processor_compatibility(rtn);
  3593. }
  3594. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  3595. {
  3596. struct page *page;
  3597. struct kvm *kvm;
  3598. int r;
  3599. BUG_ON(vcpu->kvm == NULL);
  3600. kvm = vcpu->kvm;
  3601. vcpu->arch.mmu.root_hpa = INVALID_PAGE;
  3602. if (!irqchip_in_kernel(kvm) || vcpu->vcpu_id == 0)
  3603. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  3604. else
  3605. vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
  3606. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  3607. if (!page) {
  3608. r = -ENOMEM;
  3609. goto fail;
  3610. }
  3611. vcpu->arch.pio_data = page_address(page);
  3612. r = kvm_mmu_create(vcpu);
  3613. if (r < 0)
  3614. goto fail_free_pio_data;
  3615. if (irqchip_in_kernel(kvm)) {
  3616. r = kvm_create_lapic(vcpu);
  3617. if (r < 0)
  3618. goto fail_mmu_destroy;
  3619. }
  3620. return 0;
  3621. fail_mmu_destroy:
  3622. kvm_mmu_destroy(vcpu);
  3623. fail_free_pio_data:
  3624. free_page((unsigned long)vcpu->arch.pio_data);
  3625. fail:
  3626. return r;
  3627. }
  3628. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
  3629. {
  3630. kvm_free_lapic(vcpu);
  3631. down_read(&vcpu->kvm->slots_lock);
  3632. kvm_mmu_destroy(vcpu);
  3633. up_read(&vcpu->kvm->slots_lock);
  3634. free_page((unsigned long)vcpu->arch.pio_data);
  3635. }
  3636. struct kvm *kvm_arch_create_vm(void)
  3637. {
  3638. struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
  3639. if (!kvm)
  3640. return ERR_PTR(-ENOMEM);
  3641. INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
  3642. INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
  3643. return kvm;
  3644. }
  3645. static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
  3646. {
  3647. vcpu_load(vcpu);
  3648. kvm_mmu_unload(vcpu);
  3649. vcpu_put(vcpu);
  3650. }
  3651. static void kvm_free_vcpus(struct kvm *kvm)
  3652. {
  3653. unsigned int i;
  3654. /*
  3655. * Unpin any mmu pages first.
  3656. */
  3657. for (i = 0; i < KVM_MAX_VCPUS; ++i)
  3658. if (kvm->vcpus[i])
  3659. kvm_unload_vcpu_mmu(kvm->vcpus[i]);
  3660. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  3661. if (kvm->vcpus[i]) {
  3662. kvm_arch_vcpu_free(kvm->vcpus[i]);
  3663. kvm->vcpus[i] = NULL;
  3664. }
  3665. }
  3666. }
  3667. void kvm_arch_destroy_vm(struct kvm *kvm)
  3668. {
  3669. kvm_free_assigned_devices(kvm);
  3670. kvm_free_pit(kvm);
  3671. kfree(kvm->arch.vpic);
  3672. kfree(kvm->arch.vioapic);
  3673. kvm_free_vcpus(kvm);
  3674. kvm_free_physmem(kvm);
  3675. if (kvm->arch.apic_access_page)
  3676. put_page(kvm->arch.apic_access_page);
  3677. if (kvm->arch.ept_identity_pagetable)
  3678. put_page(kvm->arch.ept_identity_pagetable);
  3679. kfree(kvm);
  3680. }
  3681. int kvm_arch_set_memory_region(struct kvm *kvm,
  3682. struct kvm_userspace_memory_region *mem,
  3683. struct kvm_memory_slot old,
  3684. int user_alloc)
  3685. {
  3686. int npages = mem->memory_size >> PAGE_SHIFT;
  3687. struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot];
  3688. /*To keep backward compatibility with older userspace,
  3689. *x86 needs to hanlde !user_alloc case.
  3690. */
  3691. if (!user_alloc) {
  3692. if (npages && !old.rmap) {
  3693. unsigned long userspace_addr;
  3694. down_write(&current->mm->mmap_sem);
  3695. userspace_addr = do_mmap(NULL, 0,
  3696. npages * PAGE_SIZE,
  3697. PROT_READ | PROT_WRITE,
  3698. MAP_PRIVATE | MAP_ANONYMOUS,
  3699. 0);
  3700. up_write(&current->mm->mmap_sem);
  3701. if (IS_ERR((void *)userspace_addr))
  3702. return PTR_ERR((void *)userspace_addr);
  3703. /* set userspace_addr atomically for kvm_hva_to_rmapp */
  3704. spin_lock(&kvm->mmu_lock);
  3705. memslot->userspace_addr = userspace_addr;
  3706. spin_unlock(&kvm->mmu_lock);
  3707. } else {
  3708. if (!old.user_alloc && old.rmap) {
  3709. int ret;
  3710. down_write(&current->mm->mmap_sem);
  3711. ret = do_munmap(current->mm, old.userspace_addr,
  3712. old.npages * PAGE_SIZE);
  3713. up_write(&current->mm->mmap_sem);
  3714. if (ret < 0)
  3715. printk(KERN_WARNING
  3716. "kvm_vm_ioctl_set_memory_region: "
  3717. "failed to munmap memory\n");
  3718. }
  3719. }
  3720. }
  3721. if (!kvm->arch.n_requested_mmu_pages) {
  3722. unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
  3723. kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
  3724. }
  3725. kvm_mmu_slot_remove_write_access(kvm, mem->slot);
  3726. kvm_flush_remote_tlbs(kvm);
  3727. return 0;
  3728. }
  3729. void kvm_arch_flush_shadow(struct kvm *kvm)
  3730. {
  3731. kvm_mmu_zap_all(kvm);
  3732. }
  3733. int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
  3734. {
  3735. return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE
  3736. || vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED;
  3737. }
  3738. static void vcpu_kick_intr(void *info)
  3739. {
  3740. #ifdef DEBUG
  3741. struct kvm_vcpu *vcpu = (struct kvm_vcpu *)info;
  3742. printk(KERN_DEBUG "vcpu_kick_intr %p \n", vcpu);
  3743. #endif
  3744. }
  3745. void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
  3746. {
  3747. int ipi_pcpu = vcpu->cpu;
  3748. int cpu = get_cpu();
  3749. if (waitqueue_active(&vcpu->wq)) {
  3750. wake_up_interruptible(&vcpu->wq);
  3751. ++vcpu->stat.halt_wakeup;
  3752. }
  3753. /*
  3754. * We may be called synchronously with irqs disabled in guest mode,
  3755. * So need not to call smp_call_function_single() in that case.
  3756. */
  3757. if (vcpu->guest_mode && vcpu->cpu != cpu)
  3758. smp_call_function_single(ipi_pcpu, vcpu_kick_intr, vcpu, 0);
  3759. put_cpu();
  3760. }