x86.c 119 KB

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