vmx.c 141 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322
  1. /*
  2. * Kernel-based Virtual Machine driver for Linux
  3. *
  4. * This module enables machines with Intel VT-x extensions to run virtual
  5. * machines without emulation or binary translation.
  6. *
  7. * Copyright (C) 2006 Qumranet, Inc.
  8. * Copyright 2010 Red Hat, Inc. and/or its affiliates.
  9. *
  10. * Authors:
  11. * Avi Kivity <avi@qumranet.com>
  12. * Yaniv Kamay <yaniv@qumranet.com>
  13. *
  14. * This work is licensed under the terms of the GNU GPL, version 2. See
  15. * the COPYING file in the top-level directory.
  16. *
  17. */
  18. #include "irq.h"
  19. #include "mmu.h"
  20. #include <linux/kvm_host.h>
  21. #include <linux/module.h>
  22. #include <linux/kernel.h>
  23. #include <linux/mm.h>
  24. #include <linux/highmem.h>
  25. #include <linux/sched.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/ftrace_event.h>
  28. #include <linux/slab.h>
  29. #include <linux/tboot.h>
  30. #include "kvm_cache_regs.h"
  31. #include "x86.h"
  32. #include <asm/io.h>
  33. #include <asm/desc.h>
  34. #include <asm/vmx.h>
  35. #include <asm/virtext.h>
  36. #include <asm/mce.h>
  37. #include <asm/i387.h>
  38. #include <asm/xcr.h>
  39. #include "trace.h"
  40. #define __ex(x) __kvm_handle_fault_on_reboot(x)
  41. #define __ex_clear(x, reg) \
  42. ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
  43. MODULE_AUTHOR("Qumranet");
  44. MODULE_LICENSE("GPL");
  45. static int __read_mostly bypass_guest_pf = 1;
  46. module_param(bypass_guest_pf, bool, S_IRUGO);
  47. static int __read_mostly enable_vpid = 1;
  48. module_param_named(vpid, enable_vpid, bool, 0444);
  49. static int __read_mostly flexpriority_enabled = 1;
  50. module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
  51. static int __read_mostly enable_ept = 1;
  52. module_param_named(ept, enable_ept, bool, S_IRUGO);
  53. static int __read_mostly enable_unrestricted_guest = 1;
  54. module_param_named(unrestricted_guest,
  55. enable_unrestricted_guest, bool, S_IRUGO);
  56. static int __read_mostly emulate_invalid_guest_state = 0;
  57. module_param(emulate_invalid_guest_state, bool, S_IRUGO);
  58. static int __read_mostly vmm_exclusive = 1;
  59. module_param(vmm_exclusive, bool, S_IRUGO);
  60. static int __read_mostly yield_on_hlt = 1;
  61. module_param(yield_on_hlt, bool, S_IRUGO);
  62. /*
  63. * If nested=1, nested virtualization is supported, i.e., guests may use
  64. * VMX and be a hypervisor for its own guests. If nested=0, guests may not
  65. * use VMX instructions.
  66. */
  67. static int __read_mostly nested = 0;
  68. module_param(nested, bool, S_IRUGO);
  69. #define KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST \
  70. (X86_CR0_WP | X86_CR0_NE | X86_CR0_NW | X86_CR0_CD)
  71. #define KVM_GUEST_CR0_MASK \
  72. (KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
  73. #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST \
  74. (X86_CR0_WP | X86_CR0_NE)
  75. #define KVM_VM_CR0_ALWAYS_ON \
  76. (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
  77. #define KVM_CR4_GUEST_OWNED_BITS \
  78. (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
  79. | X86_CR4_OSXMMEXCPT)
  80. #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
  81. #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
  82. #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
  83. /*
  84. * These 2 parameters are used to config the controls for Pause-Loop Exiting:
  85. * ple_gap: upper bound on the amount of time between two successive
  86. * executions of PAUSE in a loop. Also indicate if ple enabled.
  87. * According to test, this time is usually smaller than 128 cycles.
  88. * ple_window: upper bound on the amount of time a guest is allowed to execute
  89. * in a PAUSE loop. Tests indicate that most spinlocks are held for
  90. * less than 2^12 cycles
  91. * Time is measured based on a counter that runs at the same rate as the TSC,
  92. * refer SDM volume 3b section 21.6.13 & 22.1.3.
  93. */
  94. #define KVM_VMX_DEFAULT_PLE_GAP 128
  95. #define KVM_VMX_DEFAULT_PLE_WINDOW 4096
  96. static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
  97. module_param(ple_gap, int, S_IRUGO);
  98. static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
  99. module_param(ple_window, int, S_IRUGO);
  100. #define NR_AUTOLOAD_MSRS 1
  101. #define VMCS02_POOL_SIZE 1
  102. struct vmcs {
  103. u32 revision_id;
  104. u32 abort;
  105. char data[0];
  106. };
  107. /*
  108. * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
  109. * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
  110. * loaded on this CPU (so we can clear them if the CPU goes down).
  111. */
  112. struct loaded_vmcs {
  113. struct vmcs *vmcs;
  114. int cpu;
  115. int launched;
  116. struct list_head loaded_vmcss_on_cpu_link;
  117. };
  118. struct shared_msr_entry {
  119. unsigned index;
  120. u64 data;
  121. u64 mask;
  122. };
  123. /*
  124. * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
  125. * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
  126. * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
  127. * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
  128. * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
  129. * More than one of these structures may exist, if L1 runs multiple L2 guests.
  130. * nested_vmx_run() will use the data here to build a vmcs02: a VMCS for the
  131. * underlying hardware which will be used to run L2.
  132. * This structure is packed to ensure that its layout is identical across
  133. * machines (necessary for live migration).
  134. * If there are changes in this struct, VMCS12_REVISION must be changed.
  135. */
  136. struct __packed vmcs12 {
  137. /* According to the Intel spec, a VMCS region must start with the
  138. * following two fields. Then follow implementation-specific data.
  139. */
  140. u32 revision_id;
  141. u32 abort;
  142. };
  143. /*
  144. * VMCS12_REVISION is an arbitrary id that should be changed if the content or
  145. * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
  146. * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
  147. */
  148. #define VMCS12_REVISION 0x11e57ed0
  149. /*
  150. * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
  151. * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
  152. * current implementation, 4K are reserved to avoid future complications.
  153. */
  154. #define VMCS12_SIZE 0x1000
  155. /* Used to remember the last vmcs02 used for some recently used vmcs12s */
  156. struct vmcs02_list {
  157. struct list_head list;
  158. gpa_t vmptr;
  159. struct loaded_vmcs vmcs02;
  160. };
  161. /*
  162. * The nested_vmx structure is part of vcpu_vmx, and holds information we need
  163. * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
  164. */
  165. struct nested_vmx {
  166. /* Has the level1 guest done vmxon? */
  167. bool vmxon;
  168. /* The guest-physical address of the current VMCS L1 keeps for L2 */
  169. gpa_t current_vmptr;
  170. /* The host-usable pointer to the above */
  171. struct page *current_vmcs12_page;
  172. struct vmcs12 *current_vmcs12;
  173. /* vmcs02_list cache of VMCSs recently used to run L2 guests */
  174. struct list_head vmcs02_pool;
  175. int vmcs02_num;
  176. };
  177. struct vcpu_vmx {
  178. struct kvm_vcpu vcpu;
  179. unsigned long host_rsp;
  180. u8 fail;
  181. u8 cpl;
  182. bool nmi_known_unmasked;
  183. u32 exit_intr_info;
  184. u32 idt_vectoring_info;
  185. ulong rflags;
  186. struct shared_msr_entry *guest_msrs;
  187. int nmsrs;
  188. int save_nmsrs;
  189. #ifdef CONFIG_X86_64
  190. u64 msr_host_kernel_gs_base;
  191. u64 msr_guest_kernel_gs_base;
  192. #endif
  193. /*
  194. * loaded_vmcs points to the VMCS currently used in this vcpu. For a
  195. * non-nested (L1) guest, it always points to vmcs01. For a nested
  196. * guest (L2), it points to a different VMCS.
  197. */
  198. struct loaded_vmcs vmcs01;
  199. struct loaded_vmcs *loaded_vmcs;
  200. bool __launched; /* temporary, used in vmx_vcpu_run */
  201. struct msr_autoload {
  202. unsigned nr;
  203. struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
  204. struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
  205. } msr_autoload;
  206. struct {
  207. int loaded;
  208. u16 fs_sel, gs_sel, ldt_sel;
  209. int gs_ldt_reload_needed;
  210. int fs_reload_needed;
  211. } host_state;
  212. struct {
  213. int vm86_active;
  214. ulong save_rflags;
  215. struct kvm_save_segment {
  216. u16 selector;
  217. unsigned long base;
  218. u32 limit;
  219. u32 ar;
  220. } tr, es, ds, fs, gs;
  221. } rmode;
  222. struct {
  223. u32 bitmask; /* 4 bits per segment (1 bit per field) */
  224. struct kvm_save_segment seg[8];
  225. } segment_cache;
  226. int vpid;
  227. bool emulation_required;
  228. /* Support for vnmi-less CPUs */
  229. int soft_vnmi_blocked;
  230. ktime_t entry_time;
  231. s64 vnmi_blocked_time;
  232. u32 exit_reason;
  233. bool rdtscp_enabled;
  234. /* Support for a guest hypervisor (nested VMX) */
  235. struct nested_vmx nested;
  236. };
  237. enum segment_cache_field {
  238. SEG_FIELD_SEL = 0,
  239. SEG_FIELD_BASE = 1,
  240. SEG_FIELD_LIMIT = 2,
  241. SEG_FIELD_AR = 3,
  242. SEG_FIELD_NR = 4
  243. };
  244. static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
  245. {
  246. return container_of(vcpu, struct vcpu_vmx, vcpu);
  247. }
  248. static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
  249. {
  250. return to_vmx(vcpu)->nested.current_vmcs12;
  251. }
  252. static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr)
  253. {
  254. struct page *page = gfn_to_page(vcpu->kvm, addr >> PAGE_SHIFT);
  255. if (is_error_page(page)) {
  256. kvm_release_page_clean(page);
  257. return NULL;
  258. }
  259. return page;
  260. }
  261. static void nested_release_page(struct page *page)
  262. {
  263. kvm_release_page_dirty(page);
  264. }
  265. static void nested_release_page_clean(struct page *page)
  266. {
  267. kvm_release_page_clean(page);
  268. }
  269. static u64 construct_eptp(unsigned long root_hpa);
  270. static void kvm_cpu_vmxon(u64 addr);
  271. static void kvm_cpu_vmxoff(void);
  272. static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3);
  273. static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
  274. static DEFINE_PER_CPU(struct vmcs *, vmxarea);
  275. static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
  276. /*
  277. * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
  278. * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
  279. */
  280. static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
  281. static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
  282. static unsigned long *vmx_io_bitmap_a;
  283. static unsigned long *vmx_io_bitmap_b;
  284. static unsigned long *vmx_msr_bitmap_legacy;
  285. static unsigned long *vmx_msr_bitmap_longmode;
  286. static bool cpu_has_load_ia32_efer;
  287. static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
  288. static DEFINE_SPINLOCK(vmx_vpid_lock);
  289. static struct vmcs_config {
  290. int size;
  291. int order;
  292. u32 revision_id;
  293. u32 pin_based_exec_ctrl;
  294. u32 cpu_based_exec_ctrl;
  295. u32 cpu_based_2nd_exec_ctrl;
  296. u32 vmexit_ctrl;
  297. u32 vmentry_ctrl;
  298. } vmcs_config;
  299. static struct vmx_capability {
  300. u32 ept;
  301. u32 vpid;
  302. } vmx_capability;
  303. #define VMX_SEGMENT_FIELD(seg) \
  304. [VCPU_SREG_##seg] = { \
  305. .selector = GUEST_##seg##_SELECTOR, \
  306. .base = GUEST_##seg##_BASE, \
  307. .limit = GUEST_##seg##_LIMIT, \
  308. .ar_bytes = GUEST_##seg##_AR_BYTES, \
  309. }
  310. static struct kvm_vmx_segment_field {
  311. unsigned selector;
  312. unsigned base;
  313. unsigned limit;
  314. unsigned ar_bytes;
  315. } kvm_vmx_segment_fields[] = {
  316. VMX_SEGMENT_FIELD(CS),
  317. VMX_SEGMENT_FIELD(DS),
  318. VMX_SEGMENT_FIELD(ES),
  319. VMX_SEGMENT_FIELD(FS),
  320. VMX_SEGMENT_FIELD(GS),
  321. VMX_SEGMENT_FIELD(SS),
  322. VMX_SEGMENT_FIELD(TR),
  323. VMX_SEGMENT_FIELD(LDTR),
  324. };
  325. static u64 host_efer;
  326. static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
  327. /*
  328. * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
  329. * away by decrementing the array size.
  330. */
  331. static const u32 vmx_msr_index[] = {
  332. #ifdef CONFIG_X86_64
  333. MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
  334. #endif
  335. MSR_EFER, MSR_TSC_AUX, MSR_STAR,
  336. };
  337. #define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
  338. static inline bool is_page_fault(u32 intr_info)
  339. {
  340. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  341. INTR_INFO_VALID_MASK)) ==
  342. (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
  343. }
  344. static inline bool is_no_device(u32 intr_info)
  345. {
  346. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  347. INTR_INFO_VALID_MASK)) ==
  348. (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
  349. }
  350. static inline bool is_invalid_opcode(u32 intr_info)
  351. {
  352. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  353. INTR_INFO_VALID_MASK)) ==
  354. (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
  355. }
  356. static inline bool is_external_interrupt(u32 intr_info)
  357. {
  358. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
  359. == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
  360. }
  361. static inline bool is_machine_check(u32 intr_info)
  362. {
  363. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  364. INTR_INFO_VALID_MASK)) ==
  365. (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
  366. }
  367. static inline bool cpu_has_vmx_msr_bitmap(void)
  368. {
  369. return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
  370. }
  371. static inline bool cpu_has_vmx_tpr_shadow(void)
  372. {
  373. return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
  374. }
  375. static inline bool vm_need_tpr_shadow(struct kvm *kvm)
  376. {
  377. return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm));
  378. }
  379. static inline bool cpu_has_secondary_exec_ctrls(void)
  380. {
  381. return vmcs_config.cpu_based_exec_ctrl &
  382. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  383. }
  384. static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
  385. {
  386. return vmcs_config.cpu_based_2nd_exec_ctrl &
  387. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  388. }
  389. static inline bool cpu_has_vmx_flexpriority(void)
  390. {
  391. return cpu_has_vmx_tpr_shadow() &&
  392. cpu_has_vmx_virtualize_apic_accesses();
  393. }
  394. static inline bool cpu_has_vmx_ept_execute_only(void)
  395. {
  396. return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
  397. }
  398. static inline bool cpu_has_vmx_eptp_uncacheable(void)
  399. {
  400. return vmx_capability.ept & VMX_EPTP_UC_BIT;
  401. }
  402. static inline bool cpu_has_vmx_eptp_writeback(void)
  403. {
  404. return vmx_capability.ept & VMX_EPTP_WB_BIT;
  405. }
  406. static inline bool cpu_has_vmx_ept_2m_page(void)
  407. {
  408. return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
  409. }
  410. static inline bool cpu_has_vmx_ept_1g_page(void)
  411. {
  412. return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
  413. }
  414. static inline bool cpu_has_vmx_ept_4levels(void)
  415. {
  416. return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
  417. }
  418. static inline bool cpu_has_vmx_invept_individual_addr(void)
  419. {
  420. return vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT;
  421. }
  422. static inline bool cpu_has_vmx_invept_context(void)
  423. {
  424. return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
  425. }
  426. static inline bool cpu_has_vmx_invept_global(void)
  427. {
  428. return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
  429. }
  430. static inline bool cpu_has_vmx_invvpid_single(void)
  431. {
  432. return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
  433. }
  434. static inline bool cpu_has_vmx_invvpid_global(void)
  435. {
  436. return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
  437. }
  438. static inline bool cpu_has_vmx_ept(void)
  439. {
  440. return vmcs_config.cpu_based_2nd_exec_ctrl &
  441. SECONDARY_EXEC_ENABLE_EPT;
  442. }
  443. static inline bool cpu_has_vmx_unrestricted_guest(void)
  444. {
  445. return vmcs_config.cpu_based_2nd_exec_ctrl &
  446. SECONDARY_EXEC_UNRESTRICTED_GUEST;
  447. }
  448. static inline bool cpu_has_vmx_ple(void)
  449. {
  450. return vmcs_config.cpu_based_2nd_exec_ctrl &
  451. SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  452. }
  453. static inline bool vm_need_virtualize_apic_accesses(struct kvm *kvm)
  454. {
  455. return flexpriority_enabled && irqchip_in_kernel(kvm);
  456. }
  457. static inline bool cpu_has_vmx_vpid(void)
  458. {
  459. return vmcs_config.cpu_based_2nd_exec_ctrl &
  460. SECONDARY_EXEC_ENABLE_VPID;
  461. }
  462. static inline bool cpu_has_vmx_rdtscp(void)
  463. {
  464. return vmcs_config.cpu_based_2nd_exec_ctrl &
  465. SECONDARY_EXEC_RDTSCP;
  466. }
  467. static inline bool cpu_has_virtual_nmis(void)
  468. {
  469. return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
  470. }
  471. static inline bool cpu_has_vmx_wbinvd_exit(void)
  472. {
  473. return vmcs_config.cpu_based_2nd_exec_ctrl &
  474. SECONDARY_EXEC_WBINVD_EXITING;
  475. }
  476. static inline bool report_flexpriority(void)
  477. {
  478. return flexpriority_enabled;
  479. }
  480. static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
  481. {
  482. int i;
  483. for (i = 0; i < vmx->nmsrs; ++i)
  484. if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
  485. return i;
  486. return -1;
  487. }
  488. static inline void __invvpid(int ext, u16 vpid, gva_t gva)
  489. {
  490. struct {
  491. u64 vpid : 16;
  492. u64 rsvd : 48;
  493. u64 gva;
  494. } operand = { vpid, 0, gva };
  495. asm volatile (__ex(ASM_VMX_INVVPID)
  496. /* CF==1 or ZF==1 --> rc = -1 */
  497. "; ja 1f ; ud2 ; 1:"
  498. : : "a"(&operand), "c"(ext) : "cc", "memory");
  499. }
  500. static inline void __invept(int ext, u64 eptp, gpa_t gpa)
  501. {
  502. struct {
  503. u64 eptp, gpa;
  504. } operand = {eptp, gpa};
  505. asm volatile (__ex(ASM_VMX_INVEPT)
  506. /* CF==1 or ZF==1 --> rc = -1 */
  507. "; ja 1f ; ud2 ; 1:\n"
  508. : : "a" (&operand), "c" (ext) : "cc", "memory");
  509. }
  510. static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
  511. {
  512. int i;
  513. i = __find_msr_index(vmx, msr);
  514. if (i >= 0)
  515. return &vmx->guest_msrs[i];
  516. return NULL;
  517. }
  518. static void vmcs_clear(struct vmcs *vmcs)
  519. {
  520. u64 phys_addr = __pa(vmcs);
  521. u8 error;
  522. asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
  523. : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
  524. : "cc", "memory");
  525. if (error)
  526. printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
  527. vmcs, phys_addr);
  528. }
  529. static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
  530. {
  531. vmcs_clear(loaded_vmcs->vmcs);
  532. loaded_vmcs->cpu = -1;
  533. loaded_vmcs->launched = 0;
  534. }
  535. static void vmcs_load(struct vmcs *vmcs)
  536. {
  537. u64 phys_addr = __pa(vmcs);
  538. u8 error;
  539. asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
  540. : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
  541. : "cc", "memory");
  542. if (error)
  543. printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
  544. vmcs, phys_addr);
  545. }
  546. static void __loaded_vmcs_clear(void *arg)
  547. {
  548. struct loaded_vmcs *loaded_vmcs = arg;
  549. int cpu = raw_smp_processor_id();
  550. if (loaded_vmcs->cpu != cpu)
  551. return; /* vcpu migration can race with cpu offline */
  552. if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
  553. per_cpu(current_vmcs, cpu) = NULL;
  554. list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
  555. loaded_vmcs_init(loaded_vmcs);
  556. }
  557. static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
  558. {
  559. if (loaded_vmcs->cpu != -1)
  560. smp_call_function_single(
  561. loaded_vmcs->cpu, __loaded_vmcs_clear, loaded_vmcs, 1);
  562. }
  563. static inline void vpid_sync_vcpu_single(struct vcpu_vmx *vmx)
  564. {
  565. if (vmx->vpid == 0)
  566. return;
  567. if (cpu_has_vmx_invvpid_single())
  568. __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
  569. }
  570. static inline void vpid_sync_vcpu_global(void)
  571. {
  572. if (cpu_has_vmx_invvpid_global())
  573. __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
  574. }
  575. static inline void vpid_sync_context(struct vcpu_vmx *vmx)
  576. {
  577. if (cpu_has_vmx_invvpid_single())
  578. vpid_sync_vcpu_single(vmx);
  579. else
  580. vpid_sync_vcpu_global();
  581. }
  582. static inline void ept_sync_global(void)
  583. {
  584. if (cpu_has_vmx_invept_global())
  585. __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
  586. }
  587. static inline void ept_sync_context(u64 eptp)
  588. {
  589. if (enable_ept) {
  590. if (cpu_has_vmx_invept_context())
  591. __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
  592. else
  593. ept_sync_global();
  594. }
  595. }
  596. static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa)
  597. {
  598. if (enable_ept) {
  599. if (cpu_has_vmx_invept_individual_addr())
  600. __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR,
  601. eptp, gpa);
  602. else
  603. ept_sync_context(eptp);
  604. }
  605. }
  606. static __always_inline unsigned long vmcs_readl(unsigned long field)
  607. {
  608. unsigned long value;
  609. asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
  610. : "=a"(value) : "d"(field) : "cc");
  611. return value;
  612. }
  613. static __always_inline u16 vmcs_read16(unsigned long field)
  614. {
  615. return vmcs_readl(field);
  616. }
  617. static __always_inline u32 vmcs_read32(unsigned long field)
  618. {
  619. return vmcs_readl(field);
  620. }
  621. static __always_inline u64 vmcs_read64(unsigned long field)
  622. {
  623. #ifdef CONFIG_X86_64
  624. return vmcs_readl(field);
  625. #else
  626. return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
  627. #endif
  628. }
  629. static noinline void vmwrite_error(unsigned long field, unsigned long value)
  630. {
  631. printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
  632. field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
  633. dump_stack();
  634. }
  635. static void vmcs_writel(unsigned long field, unsigned long value)
  636. {
  637. u8 error;
  638. asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
  639. : "=q"(error) : "a"(value), "d"(field) : "cc");
  640. if (unlikely(error))
  641. vmwrite_error(field, value);
  642. }
  643. static void vmcs_write16(unsigned long field, u16 value)
  644. {
  645. vmcs_writel(field, value);
  646. }
  647. static void vmcs_write32(unsigned long field, u32 value)
  648. {
  649. vmcs_writel(field, value);
  650. }
  651. static void vmcs_write64(unsigned long field, u64 value)
  652. {
  653. vmcs_writel(field, value);
  654. #ifndef CONFIG_X86_64
  655. asm volatile ("");
  656. vmcs_writel(field+1, value >> 32);
  657. #endif
  658. }
  659. static void vmcs_clear_bits(unsigned long field, u32 mask)
  660. {
  661. vmcs_writel(field, vmcs_readl(field) & ~mask);
  662. }
  663. static void vmcs_set_bits(unsigned long field, u32 mask)
  664. {
  665. vmcs_writel(field, vmcs_readl(field) | mask);
  666. }
  667. static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
  668. {
  669. vmx->segment_cache.bitmask = 0;
  670. }
  671. static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
  672. unsigned field)
  673. {
  674. bool ret;
  675. u32 mask = 1 << (seg * SEG_FIELD_NR + field);
  676. if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
  677. vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
  678. vmx->segment_cache.bitmask = 0;
  679. }
  680. ret = vmx->segment_cache.bitmask & mask;
  681. vmx->segment_cache.bitmask |= mask;
  682. return ret;
  683. }
  684. static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
  685. {
  686. u16 *p = &vmx->segment_cache.seg[seg].selector;
  687. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
  688. *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
  689. return *p;
  690. }
  691. static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
  692. {
  693. ulong *p = &vmx->segment_cache.seg[seg].base;
  694. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
  695. *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
  696. return *p;
  697. }
  698. static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
  699. {
  700. u32 *p = &vmx->segment_cache.seg[seg].limit;
  701. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
  702. *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
  703. return *p;
  704. }
  705. static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
  706. {
  707. u32 *p = &vmx->segment_cache.seg[seg].ar;
  708. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
  709. *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
  710. return *p;
  711. }
  712. static void update_exception_bitmap(struct kvm_vcpu *vcpu)
  713. {
  714. u32 eb;
  715. eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
  716. (1u << NM_VECTOR) | (1u << DB_VECTOR);
  717. if ((vcpu->guest_debug &
  718. (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
  719. (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
  720. eb |= 1u << BP_VECTOR;
  721. if (to_vmx(vcpu)->rmode.vm86_active)
  722. eb = ~0;
  723. if (enable_ept)
  724. eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
  725. if (vcpu->fpu_active)
  726. eb &= ~(1u << NM_VECTOR);
  727. vmcs_write32(EXCEPTION_BITMAP, eb);
  728. }
  729. static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
  730. {
  731. unsigned i;
  732. struct msr_autoload *m = &vmx->msr_autoload;
  733. if (msr == MSR_EFER && cpu_has_load_ia32_efer) {
  734. vmcs_clear_bits(VM_ENTRY_CONTROLS, VM_ENTRY_LOAD_IA32_EFER);
  735. vmcs_clear_bits(VM_EXIT_CONTROLS, VM_EXIT_LOAD_IA32_EFER);
  736. return;
  737. }
  738. for (i = 0; i < m->nr; ++i)
  739. if (m->guest[i].index == msr)
  740. break;
  741. if (i == m->nr)
  742. return;
  743. --m->nr;
  744. m->guest[i] = m->guest[m->nr];
  745. m->host[i] = m->host[m->nr];
  746. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
  747. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
  748. }
  749. static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
  750. u64 guest_val, u64 host_val)
  751. {
  752. unsigned i;
  753. struct msr_autoload *m = &vmx->msr_autoload;
  754. if (msr == MSR_EFER && cpu_has_load_ia32_efer) {
  755. vmcs_write64(GUEST_IA32_EFER, guest_val);
  756. vmcs_write64(HOST_IA32_EFER, host_val);
  757. vmcs_set_bits(VM_ENTRY_CONTROLS, VM_ENTRY_LOAD_IA32_EFER);
  758. vmcs_set_bits(VM_EXIT_CONTROLS, VM_EXIT_LOAD_IA32_EFER);
  759. return;
  760. }
  761. for (i = 0; i < m->nr; ++i)
  762. if (m->guest[i].index == msr)
  763. break;
  764. if (i == m->nr) {
  765. ++m->nr;
  766. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
  767. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
  768. }
  769. m->guest[i].index = msr;
  770. m->guest[i].value = guest_val;
  771. m->host[i].index = msr;
  772. m->host[i].value = host_val;
  773. }
  774. static void reload_tss(void)
  775. {
  776. /*
  777. * VT restores TR but not its size. Useless.
  778. */
  779. struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
  780. struct desc_struct *descs;
  781. descs = (void *)gdt->address;
  782. descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
  783. load_TR_desc();
  784. }
  785. static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
  786. {
  787. u64 guest_efer;
  788. u64 ignore_bits;
  789. guest_efer = vmx->vcpu.arch.efer;
  790. /*
  791. * NX is emulated; LMA and LME handled by hardware; SCE meaninless
  792. * outside long mode
  793. */
  794. ignore_bits = EFER_NX | EFER_SCE;
  795. #ifdef CONFIG_X86_64
  796. ignore_bits |= EFER_LMA | EFER_LME;
  797. /* SCE is meaningful only in long mode on Intel */
  798. if (guest_efer & EFER_LMA)
  799. ignore_bits &= ~(u64)EFER_SCE;
  800. #endif
  801. guest_efer &= ~ignore_bits;
  802. guest_efer |= host_efer & ignore_bits;
  803. vmx->guest_msrs[efer_offset].data = guest_efer;
  804. vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
  805. clear_atomic_switch_msr(vmx, MSR_EFER);
  806. /* On ept, can't emulate nx, and must switch nx atomically */
  807. if (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX)) {
  808. guest_efer = vmx->vcpu.arch.efer;
  809. if (!(guest_efer & EFER_LMA))
  810. guest_efer &= ~EFER_LME;
  811. add_atomic_switch_msr(vmx, MSR_EFER, guest_efer, host_efer);
  812. return false;
  813. }
  814. return true;
  815. }
  816. static unsigned long segment_base(u16 selector)
  817. {
  818. struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
  819. struct desc_struct *d;
  820. unsigned long table_base;
  821. unsigned long v;
  822. if (!(selector & ~3))
  823. return 0;
  824. table_base = gdt->address;
  825. if (selector & 4) { /* from ldt */
  826. u16 ldt_selector = kvm_read_ldt();
  827. if (!(ldt_selector & ~3))
  828. return 0;
  829. table_base = segment_base(ldt_selector);
  830. }
  831. d = (struct desc_struct *)(table_base + (selector & ~7));
  832. v = get_desc_base(d);
  833. #ifdef CONFIG_X86_64
  834. if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
  835. v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
  836. #endif
  837. return v;
  838. }
  839. static inline unsigned long kvm_read_tr_base(void)
  840. {
  841. u16 tr;
  842. asm("str %0" : "=g"(tr));
  843. return segment_base(tr);
  844. }
  845. static void vmx_save_host_state(struct kvm_vcpu *vcpu)
  846. {
  847. struct vcpu_vmx *vmx = to_vmx(vcpu);
  848. int i;
  849. if (vmx->host_state.loaded)
  850. return;
  851. vmx->host_state.loaded = 1;
  852. /*
  853. * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
  854. * allow segment selectors with cpl > 0 or ti == 1.
  855. */
  856. vmx->host_state.ldt_sel = kvm_read_ldt();
  857. vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
  858. savesegment(fs, vmx->host_state.fs_sel);
  859. if (!(vmx->host_state.fs_sel & 7)) {
  860. vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
  861. vmx->host_state.fs_reload_needed = 0;
  862. } else {
  863. vmcs_write16(HOST_FS_SELECTOR, 0);
  864. vmx->host_state.fs_reload_needed = 1;
  865. }
  866. savesegment(gs, vmx->host_state.gs_sel);
  867. if (!(vmx->host_state.gs_sel & 7))
  868. vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
  869. else {
  870. vmcs_write16(HOST_GS_SELECTOR, 0);
  871. vmx->host_state.gs_ldt_reload_needed = 1;
  872. }
  873. #ifdef CONFIG_X86_64
  874. vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
  875. vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
  876. #else
  877. vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
  878. vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
  879. #endif
  880. #ifdef CONFIG_X86_64
  881. rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
  882. if (is_long_mode(&vmx->vcpu))
  883. wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
  884. #endif
  885. for (i = 0; i < vmx->save_nmsrs; ++i)
  886. kvm_set_shared_msr(vmx->guest_msrs[i].index,
  887. vmx->guest_msrs[i].data,
  888. vmx->guest_msrs[i].mask);
  889. }
  890. static void __vmx_load_host_state(struct vcpu_vmx *vmx)
  891. {
  892. if (!vmx->host_state.loaded)
  893. return;
  894. ++vmx->vcpu.stat.host_state_reload;
  895. vmx->host_state.loaded = 0;
  896. #ifdef CONFIG_X86_64
  897. if (is_long_mode(&vmx->vcpu))
  898. rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
  899. #endif
  900. if (vmx->host_state.gs_ldt_reload_needed) {
  901. kvm_load_ldt(vmx->host_state.ldt_sel);
  902. #ifdef CONFIG_X86_64
  903. load_gs_index(vmx->host_state.gs_sel);
  904. #else
  905. loadsegment(gs, vmx->host_state.gs_sel);
  906. #endif
  907. }
  908. if (vmx->host_state.fs_reload_needed)
  909. loadsegment(fs, vmx->host_state.fs_sel);
  910. reload_tss();
  911. #ifdef CONFIG_X86_64
  912. wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
  913. #endif
  914. if (current_thread_info()->status & TS_USEDFPU)
  915. clts();
  916. load_gdt(&__get_cpu_var(host_gdt));
  917. }
  918. static void vmx_load_host_state(struct vcpu_vmx *vmx)
  919. {
  920. preempt_disable();
  921. __vmx_load_host_state(vmx);
  922. preempt_enable();
  923. }
  924. /*
  925. * Switches to specified vcpu, until a matching vcpu_put(), but assumes
  926. * vcpu mutex is already taken.
  927. */
  928. static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  929. {
  930. struct vcpu_vmx *vmx = to_vmx(vcpu);
  931. u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
  932. if (!vmm_exclusive)
  933. kvm_cpu_vmxon(phys_addr);
  934. else if (vmx->loaded_vmcs->cpu != cpu)
  935. loaded_vmcs_clear(vmx->loaded_vmcs);
  936. if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
  937. per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
  938. vmcs_load(vmx->loaded_vmcs->vmcs);
  939. }
  940. if (vmx->loaded_vmcs->cpu != cpu) {
  941. struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
  942. unsigned long sysenter_esp;
  943. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  944. local_irq_disable();
  945. list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
  946. &per_cpu(loaded_vmcss_on_cpu, cpu));
  947. local_irq_enable();
  948. /*
  949. * Linux uses per-cpu TSS and GDT, so set these when switching
  950. * processors.
  951. */
  952. vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
  953. vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
  954. rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
  955. vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
  956. vmx->loaded_vmcs->cpu = cpu;
  957. }
  958. }
  959. static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
  960. {
  961. __vmx_load_host_state(to_vmx(vcpu));
  962. if (!vmm_exclusive) {
  963. __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
  964. vcpu->cpu = -1;
  965. kvm_cpu_vmxoff();
  966. }
  967. }
  968. static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
  969. {
  970. ulong cr0;
  971. if (vcpu->fpu_active)
  972. return;
  973. vcpu->fpu_active = 1;
  974. cr0 = vmcs_readl(GUEST_CR0);
  975. cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
  976. cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
  977. vmcs_writel(GUEST_CR0, cr0);
  978. update_exception_bitmap(vcpu);
  979. vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
  980. vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
  981. }
  982. static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
  983. static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
  984. {
  985. vmx_decache_cr0_guest_bits(vcpu);
  986. vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
  987. update_exception_bitmap(vcpu);
  988. vcpu->arch.cr0_guest_owned_bits = 0;
  989. vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
  990. vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
  991. }
  992. static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
  993. {
  994. unsigned long rflags, save_rflags;
  995. if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
  996. __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
  997. rflags = vmcs_readl(GUEST_RFLAGS);
  998. if (to_vmx(vcpu)->rmode.vm86_active) {
  999. rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
  1000. save_rflags = to_vmx(vcpu)->rmode.save_rflags;
  1001. rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
  1002. }
  1003. to_vmx(vcpu)->rflags = rflags;
  1004. }
  1005. return to_vmx(vcpu)->rflags;
  1006. }
  1007. static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  1008. {
  1009. __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
  1010. __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
  1011. to_vmx(vcpu)->rflags = rflags;
  1012. if (to_vmx(vcpu)->rmode.vm86_active) {
  1013. to_vmx(vcpu)->rmode.save_rflags = rflags;
  1014. rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
  1015. }
  1016. vmcs_writel(GUEST_RFLAGS, rflags);
  1017. }
  1018. static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
  1019. {
  1020. u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  1021. int ret = 0;
  1022. if (interruptibility & GUEST_INTR_STATE_STI)
  1023. ret |= KVM_X86_SHADOW_INT_STI;
  1024. if (interruptibility & GUEST_INTR_STATE_MOV_SS)
  1025. ret |= KVM_X86_SHADOW_INT_MOV_SS;
  1026. return ret & mask;
  1027. }
  1028. static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
  1029. {
  1030. u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  1031. u32 interruptibility = interruptibility_old;
  1032. interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
  1033. if (mask & KVM_X86_SHADOW_INT_MOV_SS)
  1034. interruptibility |= GUEST_INTR_STATE_MOV_SS;
  1035. else if (mask & KVM_X86_SHADOW_INT_STI)
  1036. interruptibility |= GUEST_INTR_STATE_STI;
  1037. if ((interruptibility != interruptibility_old))
  1038. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
  1039. }
  1040. static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
  1041. {
  1042. unsigned long rip;
  1043. rip = kvm_rip_read(vcpu);
  1044. rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  1045. kvm_rip_write(vcpu, rip);
  1046. /* skipping an emulated instruction also counts */
  1047. vmx_set_interrupt_shadow(vcpu, 0);
  1048. }
  1049. static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
  1050. {
  1051. /* Ensure that we clear the HLT state in the VMCS. We don't need to
  1052. * explicitly skip the instruction because if the HLT state is set, then
  1053. * the instruction is already executing and RIP has already been
  1054. * advanced. */
  1055. if (!yield_on_hlt &&
  1056. vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
  1057. vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
  1058. }
  1059. static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
  1060. bool has_error_code, u32 error_code,
  1061. bool reinject)
  1062. {
  1063. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1064. u32 intr_info = nr | INTR_INFO_VALID_MASK;
  1065. if (has_error_code) {
  1066. vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
  1067. intr_info |= INTR_INFO_DELIVER_CODE_MASK;
  1068. }
  1069. if (vmx->rmode.vm86_active) {
  1070. int inc_eip = 0;
  1071. if (kvm_exception_is_soft(nr))
  1072. inc_eip = vcpu->arch.event_exit_inst_len;
  1073. if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
  1074. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  1075. return;
  1076. }
  1077. if (kvm_exception_is_soft(nr)) {
  1078. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  1079. vmx->vcpu.arch.event_exit_inst_len);
  1080. intr_info |= INTR_TYPE_SOFT_EXCEPTION;
  1081. } else
  1082. intr_info |= INTR_TYPE_HARD_EXCEPTION;
  1083. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
  1084. vmx_clear_hlt(vcpu);
  1085. }
  1086. static bool vmx_rdtscp_supported(void)
  1087. {
  1088. return cpu_has_vmx_rdtscp();
  1089. }
  1090. /*
  1091. * Swap MSR entry in host/guest MSR entry array.
  1092. */
  1093. static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
  1094. {
  1095. struct shared_msr_entry tmp;
  1096. tmp = vmx->guest_msrs[to];
  1097. vmx->guest_msrs[to] = vmx->guest_msrs[from];
  1098. vmx->guest_msrs[from] = tmp;
  1099. }
  1100. /*
  1101. * Set up the vmcs to automatically save and restore system
  1102. * msrs. Don't touch the 64-bit msrs if the guest is in legacy
  1103. * mode, as fiddling with msrs is very expensive.
  1104. */
  1105. static void setup_msrs(struct vcpu_vmx *vmx)
  1106. {
  1107. int save_nmsrs, index;
  1108. unsigned long *msr_bitmap;
  1109. vmx_load_host_state(vmx);
  1110. save_nmsrs = 0;
  1111. #ifdef CONFIG_X86_64
  1112. if (is_long_mode(&vmx->vcpu)) {
  1113. index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
  1114. if (index >= 0)
  1115. move_msr_up(vmx, index, save_nmsrs++);
  1116. index = __find_msr_index(vmx, MSR_LSTAR);
  1117. if (index >= 0)
  1118. move_msr_up(vmx, index, save_nmsrs++);
  1119. index = __find_msr_index(vmx, MSR_CSTAR);
  1120. if (index >= 0)
  1121. move_msr_up(vmx, index, save_nmsrs++);
  1122. index = __find_msr_index(vmx, MSR_TSC_AUX);
  1123. if (index >= 0 && vmx->rdtscp_enabled)
  1124. move_msr_up(vmx, index, save_nmsrs++);
  1125. /*
  1126. * MSR_STAR is only needed on long mode guests, and only
  1127. * if efer.sce is enabled.
  1128. */
  1129. index = __find_msr_index(vmx, MSR_STAR);
  1130. if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
  1131. move_msr_up(vmx, index, save_nmsrs++);
  1132. }
  1133. #endif
  1134. index = __find_msr_index(vmx, MSR_EFER);
  1135. if (index >= 0 && update_transition_efer(vmx, index))
  1136. move_msr_up(vmx, index, save_nmsrs++);
  1137. vmx->save_nmsrs = save_nmsrs;
  1138. if (cpu_has_vmx_msr_bitmap()) {
  1139. if (is_long_mode(&vmx->vcpu))
  1140. msr_bitmap = vmx_msr_bitmap_longmode;
  1141. else
  1142. msr_bitmap = vmx_msr_bitmap_legacy;
  1143. vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
  1144. }
  1145. }
  1146. /*
  1147. * reads and returns guest's timestamp counter "register"
  1148. * guest_tsc = host_tsc + tsc_offset -- 21.3
  1149. */
  1150. static u64 guest_read_tsc(void)
  1151. {
  1152. u64 host_tsc, tsc_offset;
  1153. rdtscll(host_tsc);
  1154. tsc_offset = vmcs_read64(TSC_OFFSET);
  1155. return host_tsc + tsc_offset;
  1156. }
  1157. /*
  1158. * Empty call-back. Needs to be implemented when VMX enables the SET_TSC_KHZ
  1159. * ioctl. In this case the call-back should update internal vmx state to make
  1160. * the changes effective.
  1161. */
  1162. static void vmx_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz)
  1163. {
  1164. /* Nothing to do here */
  1165. }
  1166. /*
  1167. * writes 'offset' into guest's timestamp counter offset register
  1168. */
  1169. static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
  1170. {
  1171. vmcs_write64(TSC_OFFSET, offset);
  1172. }
  1173. static void vmx_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment)
  1174. {
  1175. u64 offset = vmcs_read64(TSC_OFFSET);
  1176. vmcs_write64(TSC_OFFSET, offset + adjustment);
  1177. }
  1178. static u64 vmx_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
  1179. {
  1180. return target_tsc - native_read_tsc();
  1181. }
  1182. static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
  1183. {
  1184. struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
  1185. return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
  1186. }
  1187. /*
  1188. * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
  1189. * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
  1190. * all guests if the "nested" module option is off, and can also be disabled
  1191. * for a single guest by disabling its VMX cpuid bit.
  1192. */
  1193. static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
  1194. {
  1195. return nested && guest_cpuid_has_vmx(vcpu);
  1196. }
  1197. /*
  1198. * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
  1199. * returned for the various VMX controls MSRs when nested VMX is enabled.
  1200. * The same values should also be used to verify that vmcs12 control fields are
  1201. * valid during nested entry from L1 to L2.
  1202. * Each of these control msrs has a low and high 32-bit half: A low bit is on
  1203. * if the corresponding bit in the (32-bit) control field *must* be on, and a
  1204. * bit in the high half is on if the corresponding bit in the control field
  1205. * may be on. See also vmx_control_verify().
  1206. * TODO: allow these variables to be modified (downgraded) by module options
  1207. * or other means.
  1208. */
  1209. static u32 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high;
  1210. static u32 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high;
  1211. static u32 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high;
  1212. static u32 nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high;
  1213. static u32 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high;
  1214. static __init void nested_vmx_setup_ctls_msrs(void)
  1215. {
  1216. /*
  1217. * Note that as a general rule, the high half of the MSRs (bits in
  1218. * the control fields which may be 1) should be initialized by the
  1219. * intersection of the underlying hardware's MSR (i.e., features which
  1220. * can be supported) and the list of features we want to expose -
  1221. * because they are known to be properly supported in our code.
  1222. * Also, usually, the low half of the MSRs (bits which must be 1) can
  1223. * be set to 0, meaning that L1 may turn off any of these bits. The
  1224. * reason is that if one of these bits is necessary, it will appear
  1225. * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
  1226. * fields of vmcs01 and vmcs02, will turn these bits off - and
  1227. * nested_vmx_exit_handled() will not pass related exits to L1.
  1228. * These rules have exceptions below.
  1229. */
  1230. /* pin-based controls */
  1231. /*
  1232. * According to the Intel spec, if bit 55 of VMX_BASIC is off (as it is
  1233. * in our case), bits 1, 2 and 4 (i.e., 0x16) must be 1 in this MSR.
  1234. */
  1235. nested_vmx_pinbased_ctls_low = 0x16 ;
  1236. nested_vmx_pinbased_ctls_high = 0x16 |
  1237. PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING |
  1238. PIN_BASED_VIRTUAL_NMIS;
  1239. /* exit controls */
  1240. nested_vmx_exit_ctls_low = 0;
  1241. #ifdef CONFIG_X86_64
  1242. nested_vmx_exit_ctls_high = VM_EXIT_HOST_ADDR_SPACE_SIZE;
  1243. #else
  1244. nested_vmx_exit_ctls_high = 0;
  1245. #endif
  1246. /* entry controls */
  1247. rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
  1248. nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high);
  1249. nested_vmx_entry_ctls_low = 0;
  1250. nested_vmx_entry_ctls_high &=
  1251. VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_IA32E_MODE;
  1252. /* cpu-based controls */
  1253. rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
  1254. nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high);
  1255. nested_vmx_procbased_ctls_low = 0;
  1256. nested_vmx_procbased_ctls_high &=
  1257. CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_USE_TSC_OFFSETING |
  1258. CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
  1259. CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
  1260. CPU_BASED_CR3_STORE_EXITING |
  1261. #ifdef CONFIG_X86_64
  1262. CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
  1263. #endif
  1264. CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
  1265. CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_EXITING |
  1266. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  1267. /*
  1268. * We can allow some features even when not supported by the
  1269. * hardware. For example, L1 can specify an MSR bitmap - and we
  1270. * can use it to avoid exits to L1 - even when L0 runs L2
  1271. * without MSR bitmaps.
  1272. */
  1273. nested_vmx_procbased_ctls_high |= CPU_BASED_USE_MSR_BITMAPS;
  1274. /* secondary cpu-based controls */
  1275. rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
  1276. nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high);
  1277. nested_vmx_secondary_ctls_low = 0;
  1278. nested_vmx_secondary_ctls_high &=
  1279. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  1280. }
  1281. static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
  1282. {
  1283. /*
  1284. * Bits 0 in high must be 0, and bits 1 in low must be 1.
  1285. */
  1286. return ((control & high) | low) == control;
  1287. }
  1288. static inline u64 vmx_control_msr(u32 low, u32 high)
  1289. {
  1290. return low | ((u64)high << 32);
  1291. }
  1292. /*
  1293. * If we allow our guest to use VMX instructions (i.e., nested VMX), we should
  1294. * also let it use VMX-specific MSRs.
  1295. * vmx_get_vmx_msr() and vmx_set_vmx_msr() return 1 when we handled a
  1296. * VMX-specific MSR, or 0 when we haven't (and the caller should handle it
  1297. * like all other MSRs).
  1298. */
  1299. static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
  1300. {
  1301. if (!nested_vmx_allowed(vcpu) && msr_index >= MSR_IA32_VMX_BASIC &&
  1302. msr_index <= MSR_IA32_VMX_TRUE_ENTRY_CTLS) {
  1303. /*
  1304. * According to the spec, processors which do not support VMX
  1305. * should throw a #GP(0) when VMX capability MSRs are read.
  1306. */
  1307. kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
  1308. return 1;
  1309. }
  1310. switch (msr_index) {
  1311. case MSR_IA32_FEATURE_CONTROL:
  1312. *pdata = 0;
  1313. break;
  1314. case MSR_IA32_VMX_BASIC:
  1315. /*
  1316. * This MSR reports some information about VMX support. We
  1317. * should return information about the VMX we emulate for the
  1318. * guest, and the VMCS structure we give it - not about the
  1319. * VMX support of the underlying hardware.
  1320. */
  1321. *pdata = VMCS12_REVISION |
  1322. ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
  1323. (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
  1324. break;
  1325. case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
  1326. case MSR_IA32_VMX_PINBASED_CTLS:
  1327. *pdata = vmx_control_msr(nested_vmx_pinbased_ctls_low,
  1328. nested_vmx_pinbased_ctls_high);
  1329. break;
  1330. case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
  1331. case MSR_IA32_VMX_PROCBASED_CTLS:
  1332. *pdata = vmx_control_msr(nested_vmx_procbased_ctls_low,
  1333. nested_vmx_procbased_ctls_high);
  1334. break;
  1335. case MSR_IA32_VMX_TRUE_EXIT_CTLS:
  1336. case MSR_IA32_VMX_EXIT_CTLS:
  1337. *pdata = vmx_control_msr(nested_vmx_exit_ctls_low,
  1338. nested_vmx_exit_ctls_high);
  1339. break;
  1340. case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
  1341. case MSR_IA32_VMX_ENTRY_CTLS:
  1342. *pdata = vmx_control_msr(nested_vmx_entry_ctls_low,
  1343. nested_vmx_entry_ctls_high);
  1344. break;
  1345. case MSR_IA32_VMX_MISC:
  1346. *pdata = 0;
  1347. break;
  1348. /*
  1349. * These MSRs specify bits which the guest must keep fixed (on or off)
  1350. * while L1 is in VMXON mode (in L1's root mode, or running an L2).
  1351. * We picked the standard core2 setting.
  1352. */
  1353. #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
  1354. #define VMXON_CR4_ALWAYSON X86_CR4_VMXE
  1355. case MSR_IA32_VMX_CR0_FIXED0:
  1356. *pdata = VMXON_CR0_ALWAYSON;
  1357. break;
  1358. case MSR_IA32_VMX_CR0_FIXED1:
  1359. *pdata = -1ULL;
  1360. break;
  1361. case MSR_IA32_VMX_CR4_FIXED0:
  1362. *pdata = VMXON_CR4_ALWAYSON;
  1363. break;
  1364. case MSR_IA32_VMX_CR4_FIXED1:
  1365. *pdata = -1ULL;
  1366. break;
  1367. case MSR_IA32_VMX_VMCS_ENUM:
  1368. *pdata = 0x1f;
  1369. break;
  1370. case MSR_IA32_VMX_PROCBASED_CTLS2:
  1371. *pdata = vmx_control_msr(nested_vmx_secondary_ctls_low,
  1372. nested_vmx_secondary_ctls_high);
  1373. break;
  1374. case MSR_IA32_VMX_EPT_VPID_CAP:
  1375. /* Currently, no nested ept or nested vpid */
  1376. *pdata = 0;
  1377. break;
  1378. default:
  1379. return 0;
  1380. }
  1381. return 1;
  1382. }
  1383. static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
  1384. {
  1385. if (!nested_vmx_allowed(vcpu))
  1386. return 0;
  1387. if (msr_index == MSR_IA32_FEATURE_CONTROL)
  1388. /* TODO: the right thing. */
  1389. return 1;
  1390. /*
  1391. * No need to treat VMX capability MSRs specially: If we don't handle
  1392. * them, handle_wrmsr will #GP(0), which is correct (they are readonly)
  1393. */
  1394. return 0;
  1395. }
  1396. /*
  1397. * Reads an msr value (of 'msr_index') into 'pdata'.
  1398. * Returns 0 on success, non-0 otherwise.
  1399. * Assumes vcpu_load() was already called.
  1400. */
  1401. static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
  1402. {
  1403. u64 data;
  1404. struct shared_msr_entry *msr;
  1405. if (!pdata) {
  1406. printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
  1407. return -EINVAL;
  1408. }
  1409. switch (msr_index) {
  1410. #ifdef CONFIG_X86_64
  1411. case MSR_FS_BASE:
  1412. data = vmcs_readl(GUEST_FS_BASE);
  1413. break;
  1414. case MSR_GS_BASE:
  1415. data = vmcs_readl(GUEST_GS_BASE);
  1416. break;
  1417. case MSR_KERNEL_GS_BASE:
  1418. vmx_load_host_state(to_vmx(vcpu));
  1419. data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
  1420. break;
  1421. #endif
  1422. case MSR_EFER:
  1423. return kvm_get_msr_common(vcpu, msr_index, pdata);
  1424. case MSR_IA32_TSC:
  1425. data = guest_read_tsc();
  1426. break;
  1427. case MSR_IA32_SYSENTER_CS:
  1428. data = vmcs_read32(GUEST_SYSENTER_CS);
  1429. break;
  1430. case MSR_IA32_SYSENTER_EIP:
  1431. data = vmcs_readl(GUEST_SYSENTER_EIP);
  1432. break;
  1433. case MSR_IA32_SYSENTER_ESP:
  1434. data = vmcs_readl(GUEST_SYSENTER_ESP);
  1435. break;
  1436. case MSR_TSC_AUX:
  1437. if (!to_vmx(vcpu)->rdtscp_enabled)
  1438. return 1;
  1439. /* Otherwise falls through */
  1440. default:
  1441. vmx_load_host_state(to_vmx(vcpu));
  1442. if (vmx_get_vmx_msr(vcpu, msr_index, pdata))
  1443. return 0;
  1444. msr = find_msr_entry(to_vmx(vcpu), msr_index);
  1445. if (msr) {
  1446. vmx_load_host_state(to_vmx(vcpu));
  1447. data = msr->data;
  1448. break;
  1449. }
  1450. return kvm_get_msr_common(vcpu, msr_index, pdata);
  1451. }
  1452. *pdata = data;
  1453. return 0;
  1454. }
  1455. /*
  1456. * Writes msr value into into the appropriate "register".
  1457. * Returns 0 on success, non-0 otherwise.
  1458. * Assumes vcpu_load() was already called.
  1459. */
  1460. static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
  1461. {
  1462. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1463. struct shared_msr_entry *msr;
  1464. int ret = 0;
  1465. switch (msr_index) {
  1466. case MSR_EFER:
  1467. vmx_load_host_state(vmx);
  1468. ret = kvm_set_msr_common(vcpu, msr_index, data);
  1469. break;
  1470. #ifdef CONFIG_X86_64
  1471. case MSR_FS_BASE:
  1472. vmx_segment_cache_clear(vmx);
  1473. vmcs_writel(GUEST_FS_BASE, data);
  1474. break;
  1475. case MSR_GS_BASE:
  1476. vmx_segment_cache_clear(vmx);
  1477. vmcs_writel(GUEST_GS_BASE, data);
  1478. break;
  1479. case MSR_KERNEL_GS_BASE:
  1480. vmx_load_host_state(vmx);
  1481. vmx->msr_guest_kernel_gs_base = data;
  1482. break;
  1483. #endif
  1484. case MSR_IA32_SYSENTER_CS:
  1485. vmcs_write32(GUEST_SYSENTER_CS, data);
  1486. break;
  1487. case MSR_IA32_SYSENTER_EIP:
  1488. vmcs_writel(GUEST_SYSENTER_EIP, data);
  1489. break;
  1490. case MSR_IA32_SYSENTER_ESP:
  1491. vmcs_writel(GUEST_SYSENTER_ESP, data);
  1492. break;
  1493. case MSR_IA32_TSC:
  1494. kvm_write_tsc(vcpu, data);
  1495. break;
  1496. case MSR_IA32_CR_PAT:
  1497. if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
  1498. vmcs_write64(GUEST_IA32_PAT, data);
  1499. vcpu->arch.pat = data;
  1500. break;
  1501. }
  1502. ret = kvm_set_msr_common(vcpu, msr_index, data);
  1503. break;
  1504. case MSR_TSC_AUX:
  1505. if (!vmx->rdtscp_enabled)
  1506. return 1;
  1507. /* Check reserved bit, higher 32 bits should be zero */
  1508. if ((data >> 32) != 0)
  1509. return 1;
  1510. /* Otherwise falls through */
  1511. default:
  1512. if (vmx_set_vmx_msr(vcpu, msr_index, data))
  1513. break;
  1514. msr = find_msr_entry(vmx, msr_index);
  1515. if (msr) {
  1516. vmx_load_host_state(vmx);
  1517. msr->data = data;
  1518. break;
  1519. }
  1520. ret = kvm_set_msr_common(vcpu, msr_index, data);
  1521. }
  1522. return ret;
  1523. }
  1524. static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
  1525. {
  1526. __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
  1527. switch (reg) {
  1528. case VCPU_REGS_RSP:
  1529. vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
  1530. break;
  1531. case VCPU_REGS_RIP:
  1532. vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
  1533. break;
  1534. case VCPU_EXREG_PDPTR:
  1535. if (enable_ept)
  1536. ept_save_pdptrs(vcpu);
  1537. break;
  1538. default:
  1539. break;
  1540. }
  1541. }
  1542. static void set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
  1543. {
  1544. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
  1545. vmcs_writel(GUEST_DR7, dbg->arch.debugreg[7]);
  1546. else
  1547. vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
  1548. update_exception_bitmap(vcpu);
  1549. }
  1550. static __init int cpu_has_kvm_support(void)
  1551. {
  1552. return cpu_has_vmx();
  1553. }
  1554. static __init int vmx_disabled_by_bios(void)
  1555. {
  1556. u64 msr;
  1557. rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
  1558. if (msr & FEATURE_CONTROL_LOCKED) {
  1559. /* launched w/ TXT and VMX disabled */
  1560. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
  1561. && tboot_enabled())
  1562. return 1;
  1563. /* launched w/o TXT and VMX only enabled w/ TXT */
  1564. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
  1565. && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
  1566. && !tboot_enabled()) {
  1567. printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
  1568. "activate TXT before enabling KVM\n");
  1569. return 1;
  1570. }
  1571. /* launched w/o TXT and VMX disabled */
  1572. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
  1573. && !tboot_enabled())
  1574. return 1;
  1575. }
  1576. return 0;
  1577. }
  1578. static void kvm_cpu_vmxon(u64 addr)
  1579. {
  1580. asm volatile (ASM_VMX_VMXON_RAX
  1581. : : "a"(&addr), "m"(addr)
  1582. : "memory", "cc");
  1583. }
  1584. static int hardware_enable(void *garbage)
  1585. {
  1586. int cpu = raw_smp_processor_id();
  1587. u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
  1588. u64 old, test_bits;
  1589. if (read_cr4() & X86_CR4_VMXE)
  1590. return -EBUSY;
  1591. INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
  1592. rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
  1593. test_bits = FEATURE_CONTROL_LOCKED;
  1594. test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  1595. if (tboot_enabled())
  1596. test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
  1597. if ((old & test_bits) != test_bits) {
  1598. /* enable and lock */
  1599. wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
  1600. }
  1601. write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
  1602. if (vmm_exclusive) {
  1603. kvm_cpu_vmxon(phys_addr);
  1604. ept_sync_global();
  1605. }
  1606. store_gdt(&__get_cpu_var(host_gdt));
  1607. return 0;
  1608. }
  1609. static void vmclear_local_loaded_vmcss(void)
  1610. {
  1611. int cpu = raw_smp_processor_id();
  1612. struct loaded_vmcs *v, *n;
  1613. list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
  1614. loaded_vmcss_on_cpu_link)
  1615. __loaded_vmcs_clear(v);
  1616. }
  1617. /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
  1618. * tricks.
  1619. */
  1620. static void kvm_cpu_vmxoff(void)
  1621. {
  1622. asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
  1623. }
  1624. static void hardware_disable(void *garbage)
  1625. {
  1626. if (vmm_exclusive) {
  1627. vmclear_local_loaded_vmcss();
  1628. kvm_cpu_vmxoff();
  1629. }
  1630. write_cr4(read_cr4() & ~X86_CR4_VMXE);
  1631. }
  1632. static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
  1633. u32 msr, u32 *result)
  1634. {
  1635. u32 vmx_msr_low, vmx_msr_high;
  1636. u32 ctl = ctl_min | ctl_opt;
  1637. rdmsr(msr, vmx_msr_low, vmx_msr_high);
  1638. ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
  1639. ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
  1640. /* Ensure minimum (required) set of control bits are supported. */
  1641. if (ctl_min & ~ctl)
  1642. return -EIO;
  1643. *result = ctl;
  1644. return 0;
  1645. }
  1646. static __init bool allow_1_setting(u32 msr, u32 ctl)
  1647. {
  1648. u32 vmx_msr_low, vmx_msr_high;
  1649. rdmsr(msr, vmx_msr_low, vmx_msr_high);
  1650. return vmx_msr_high & ctl;
  1651. }
  1652. static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
  1653. {
  1654. u32 vmx_msr_low, vmx_msr_high;
  1655. u32 min, opt, min2, opt2;
  1656. u32 _pin_based_exec_control = 0;
  1657. u32 _cpu_based_exec_control = 0;
  1658. u32 _cpu_based_2nd_exec_control = 0;
  1659. u32 _vmexit_control = 0;
  1660. u32 _vmentry_control = 0;
  1661. min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
  1662. opt = PIN_BASED_VIRTUAL_NMIS;
  1663. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
  1664. &_pin_based_exec_control) < 0)
  1665. return -EIO;
  1666. min =
  1667. #ifdef CONFIG_X86_64
  1668. CPU_BASED_CR8_LOAD_EXITING |
  1669. CPU_BASED_CR8_STORE_EXITING |
  1670. #endif
  1671. CPU_BASED_CR3_LOAD_EXITING |
  1672. CPU_BASED_CR3_STORE_EXITING |
  1673. CPU_BASED_USE_IO_BITMAPS |
  1674. CPU_BASED_MOV_DR_EXITING |
  1675. CPU_BASED_USE_TSC_OFFSETING |
  1676. CPU_BASED_MWAIT_EXITING |
  1677. CPU_BASED_MONITOR_EXITING |
  1678. CPU_BASED_INVLPG_EXITING;
  1679. if (yield_on_hlt)
  1680. min |= CPU_BASED_HLT_EXITING;
  1681. opt = CPU_BASED_TPR_SHADOW |
  1682. CPU_BASED_USE_MSR_BITMAPS |
  1683. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  1684. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
  1685. &_cpu_based_exec_control) < 0)
  1686. return -EIO;
  1687. #ifdef CONFIG_X86_64
  1688. if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
  1689. _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
  1690. ~CPU_BASED_CR8_STORE_EXITING;
  1691. #endif
  1692. if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
  1693. min2 = 0;
  1694. opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  1695. SECONDARY_EXEC_WBINVD_EXITING |
  1696. SECONDARY_EXEC_ENABLE_VPID |
  1697. SECONDARY_EXEC_ENABLE_EPT |
  1698. SECONDARY_EXEC_UNRESTRICTED_GUEST |
  1699. SECONDARY_EXEC_PAUSE_LOOP_EXITING |
  1700. SECONDARY_EXEC_RDTSCP;
  1701. if (adjust_vmx_controls(min2, opt2,
  1702. MSR_IA32_VMX_PROCBASED_CTLS2,
  1703. &_cpu_based_2nd_exec_control) < 0)
  1704. return -EIO;
  1705. }
  1706. #ifndef CONFIG_X86_64
  1707. if (!(_cpu_based_2nd_exec_control &
  1708. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
  1709. _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
  1710. #endif
  1711. if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
  1712. /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
  1713. enabled */
  1714. _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
  1715. CPU_BASED_CR3_STORE_EXITING |
  1716. CPU_BASED_INVLPG_EXITING);
  1717. rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
  1718. vmx_capability.ept, vmx_capability.vpid);
  1719. }
  1720. min = 0;
  1721. #ifdef CONFIG_X86_64
  1722. min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
  1723. #endif
  1724. opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
  1725. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
  1726. &_vmexit_control) < 0)
  1727. return -EIO;
  1728. min = 0;
  1729. opt = VM_ENTRY_LOAD_IA32_PAT;
  1730. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
  1731. &_vmentry_control) < 0)
  1732. return -EIO;
  1733. rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
  1734. /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
  1735. if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
  1736. return -EIO;
  1737. #ifdef CONFIG_X86_64
  1738. /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
  1739. if (vmx_msr_high & (1u<<16))
  1740. return -EIO;
  1741. #endif
  1742. /* Require Write-Back (WB) memory type for VMCS accesses. */
  1743. if (((vmx_msr_high >> 18) & 15) != 6)
  1744. return -EIO;
  1745. vmcs_conf->size = vmx_msr_high & 0x1fff;
  1746. vmcs_conf->order = get_order(vmcs_config.size);
  1747. vmcs_conf->revision_id = vmx_msr_low;
  1748. vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
  1749. vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
  1750. vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
  1751. vmcs_conf->vmexit_ctrl = _vmexit_control;
  1752. vmcs_conf->vmentry_ctrl = _vmentry_control;
  1753. cpu_has_load_ia32_efer =
  1754. allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
  1755. VM_ENTRY_LOAD_IA32_EFER)
  1756. && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
  1757. VM_EXIT_LOAD_IA32_EFER);
  1758. return 0;
  1759. }
  1760. static struct vmcs *alloc_vmcs_cpu(int cpu)
  1761. {
  1762. int node = cpu_to_node(cpu);
  1763. struct page *pages;
  1764. struct vmcs *vmcs;
  1765. pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order);
  1766. if (!pages)
  1767. return NULL;
  1768. vmcs = page_address(pages);
  1769. memset(vmcs, 0, vmcs_config.size);
  1770. vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
  1771. return vmcs;
  1772. }
  1773. static struct vmcs *alloc_vmcs(void)
  1774. {
  1775. return alloc_vmcs_cpu(raw_smp_processor_id());
  1776. }
  1777. static void free_vmcs(struct vmcs *vmcs)
  1778. {
  1779. free_pages((unsigned long)vmcs, vmcs_config.order);
  1780. }
  1781. /*
  1782. * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
  1783. */
  1784. static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
  1785. {
  1786. if (!loaded_vmcs->vmcs)
  1787. return;
  1788. loaded_vmcs_clear(loaded_vmcs);
  1789. free_vmcs(loaded_vmcs->vmcs);
  1790. loaded_vmcs->vmcs = NULL;
  1791. }
  1792. static void free_kvm_area(void)
  1793. {
  1794. int cpu;
  1795. for_each_possible_cpu(cpu) {
  1796. free_vmcs(per_cpu(vmxarea, cpu));
  1797. per_cpu(vmxarea, cpu) = NULL;
  1798. }
  1799. }
  1800. static __init int alloc_kvm_area(void)
  1801. {
  1802. int cpu;
  1803. for_each_possible_cpu(cpu) {
  1804. struct vmcs *vmcs;
  1805. vmcs = alloc_vmcs_cpu(cpu);
  1806. if (!vmcs) {
  1807. free_kvm_area();
  1808. return -ENOMEM;
  1809. }
  1810. per_cpu(vmxarea, cpu) = vmcs;
  1811. }
  1812. return 0;
  1813. }
  1814. static __init int hardware_setup(void)
  1815. {
  1816. if (setup_vmcs_config(&vmcs_config) < 0)
  1817. return -EIO;
  1818. if (boot_cpu_has(X86_FEATURE_NX))
  1819. kvm_enable_efer_bits(EFER_NX);
  1820. if (!cpu_has_vmx_vpid())
  1821. enable_vpid = 0;
  1822. if (!cpu_has_vmx_ept() ||
  1823. !cpu_has_vmx_ept_4levels()) {
  1824. enable_ept = 0;
  1825. enable_unrestricted_guest = 0;
  1826. }
  1827. if (!cpu_has_vmx_unrestricted_guest())
  1828. enable_unrestricted_guest = 0;
  1829. if (!cpu_has_vmx_flexpriority())
  1830. flexpriority_enabled = 0;
  1831. if (!cpu_has_vmx_tpr_shadow())
  1832. kvm_x86_ops->update_cr8_intercept = NULL;
  1833. if (enable_ept && !cpu_has_vmx_ept_2m_page())
  1834. kvm_disable_largepages();
  1835. if (!cpu_has_vmx_ple())
  1836. ple_gap = 0;
  1837. if (nested)
  1838. nested_vmx_setup_ctls_msrs();
  1839. return alloc_kvm_area();
  1840. }
  1841. static __exit void hardware_unsetup(void)
  1842. {
  1843. free_kvm_area();
  1844. }
  1845. static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
  1846. {
  1847. struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  1848. if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
  1849. vmcs_write16(sf->selector, save->selector);
  1850. vmcs_writel(sf->base, save->base);
  1851. vmcs_write32(sf->limit, save->limit);
  1852. vmcs_write32(sf->ar_bytes, save->ar);
  1853. } else {
  1854. u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
  1855. << AR_DPL_SHIFT;
  1856. vmcs_write32(sf->ar_bytes, 0x93 | dpl);
  1857. }
  1858. }
  1859. static void enter_pmode(struct kvm_vcpu *vcpu)
  1860. {
  1861. unsigned long flags;
  1862. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1863. vmx->emulation_required = 1;
  1864. vmx->rmode.vm86_active = 0;
  1865. vmx_segment_cache_clear(vmx);
  1866. vmcs_write16(GUEST_TR_SELECTOR, vmx->rmode.tr.selector);
  1867. vmcs_writel(GUEST_TR_BASE, vmx->rmode.tr.base);
  1868. vmcs_write32(GUEST_TR_LIMIT, vmx->rmode.tr.limit);
  1869. vmcs_write32(GUEST_TR_AR_BYTES, vmx->rmode.tr.ar);
  1870. flags = vmcs_readl(GUEST_RFLAGS);
  1871. flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
  1872. flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
  1873. vmcs_writel(GUEST_RFLAGS, flags);
  1874. vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
  1875. (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
  1876. update_exception_bitmap(vcpu);
  1877. if (emulate_invalid_guest_state)
  1878. return;
  1879. fix_pmode_dataseg(VCPU_SREG_ES, &vmx->rmode.es);
  1880. fix_pmode_dataseg(VCPU_SREG_DS, &vmx->rmode.ds);
  1881. fix_pmode_dataseg(VCPU_SREG_GS, &vmx->rmode.gs);
  1882. fix_pmode_dataseg(VCPU_SREG_FS, &vmx->rmode.fs);
  1883. vmx_segment_cache_clear(vmx);
  1884. vmcs_write16(GUEST_SS_SELECTOR, 0);
  1885. vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
  1886. vmcs_write16(GUEST_CS_SELECTOR,
  1887. vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
  1888. vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
  1889. }
  1890. static gva_t rmode_tss_base(struct kvm *kvm)
  1891. {
  1892. if (!kvm->arch.tss_addr) {
  1893. struct kvm_memslots *slots;
  1894. gfn_t base_gfn;
  1895. slots = kvm_memslots(kvm);
  1896. base_gfn = slots->memslots[0].base_gfn +
  1897. kvm->memslots->memslots[0].npages - 3;
  1898. return base_gfn << PAGE_SHIFT;
  1899. }
  1900. return kvm->arch.tss_addr;
  1901. }
  1902. static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
  1903. {
  1904. struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  1905. save->selector = vmcs_read16(sf->selector);
  1906. save->base = vmcs_readl(sf->base);
  1907. save->limit = vmcs_read32(sf->limit);
  1908. save->ar = vmcs_read32(sf->ar_bytes);
  1909. vmcs_write16(sf->selector, save->base >> 4);
  1910. vmcs_write32(sf->base, save->base & 0xffff0);
  1911. vmcs_write32(sf->limit, 0xffff);
  1912. vmcs_write32(sf->ar_bytes, 0xf3);
  1913. if (save->base & 0xf)
  1914. printk_once(KERN_WARNING "kvm: segment base is not paragraph"
  1915. " aligned when entering protected mode (seg=%d)",
  1916. seg);
  1917. }
  1918. static void enter_rmode(struct kvm_vcpu *vcpu)
  1919. {
  1920. unsigned long flags;
  1921. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1922. if (enable_unrestricted_guest)
  1923. return;
  1924. vmx->emulation_required = 1;
  1925. vmx->rmode.vm86_active = 1;
  1926. /*
  1927. * Very old userspace does not call KVM_SET_TSS_ADDR before entering
  1928. * vcpu. Call it here with phys address pointing 16M below 4G.
  1929. */
  1930. if (!vcpu->kvm->arch.tss_addr) {
  1931. printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
  1932. "called before entering vcpu\n");
  1933. srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
  1934. vmx_set_tss_addr(vcpu->kvm, 0xfeffd000);
  1935. vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
  1936. }
  1937. vmx_segment_cache_clear(vmx);
  1938. vmx->rmode.tr.selector = vmcs_read16(GUEST_TR_SELECTOR);
  1939. vmx->rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
  1940. vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
  1941. vmx->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
  1942. vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
  1943. vmx->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
  1944. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  1945. flags = vmcs_readl(GUEST_RFLAGS);
  1946. vmx->rmode.save_rflags = flags;
  1947. flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
  1948. vmcs_writel(GUEST_RFLAGS, flags);
  1949. vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
  1950. update_exception_bitmap(vcpu);
  1951. if (emulate_invalid_guest_state)
  1952. goto continue_rmode;
  1953. vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
  1954. vmcs_write32(GUEST_SS_LIMIT, 0xffff);
  1955. vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
  1956. vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
  1957. vmcs_write32(GUEST_CS_LIMIT, 0xffff);
  1958. if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
  1959. vmcs_writel(GUEST_CS_BASE, 0xf0000);
  1960. vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
  1961. fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.es);
  1962. fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.ds);
  1963. fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.gs);
  1964. fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.fs);
  1965. continue_rmode:
  1966. kvm_mmu_reset_context(vcpu);
  1967. }
  1968. static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
  1969. {
  1970. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1971. struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
  1972. if (!msr)
  1973. return;
  1974. /*
  1975. * Force kernel_gs_base reloading before EFER changes, as control
  1976. * of this msr depends on is_long_mode().
  1977. */
  1978. vmx_load_host_state(to_vmx(vcpu));
  1979. vcpu->arch.efer = efer;
  1980. if (efer & EFER_LMA) {
  1981. vmcs_write32(VM_ENTRY_CONTROLS,
  1982. vmcs_read32(VM_ENTRY_CONTROLS) |
  1983. VM_ENTRY_IA32E_MODE);
  1984. msr->data = efer;
  1985. } else {
  1986. vmcs_write32(VM_ENTRY_CONTROLS,
  1987. vmcs_read32(VM_ENTRY_CONTROLS) &
  1988. ~VM_ENTRY_IA32E_MODE);
  1989. msr->data = efer & ~EFER_LME;
  1990. }
  1991. setup_msrs(vmx);
  1992. }
  1993. #ifdef CONFIG_X86_64
  1994. static void enter_lmode(struct kvm_vcpu *vcpu)
  1995. {
  1996. u32 guest_tr_ar;
  1997. vmx_segment_cache_clear(to_vmx(vcpu));
  1998. guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
  1999. if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
  2000. printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
  2001. __func__);
  2002. vmcs_write32(GUEST_TR_AR_BYTES,
  2003. (guest_tr_ar & ~AR_TYPE_MASK)
  2004. | AR_TYPE_BUSY_64_TSS);
  2005. }
  2006. vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
  2007. }
  2008. static void exit_lmode(struct kvm_vcpu *vcpu)
  2009. {
  2010. vmcs_write32(VM_ENTRY_CONTROLS,
  2011. vmcs_read32(VM_ENTRY_CONTROLS)
  2012. & ~VM_ENTRY_IA32E_MODE);
  2013. vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
  2014. }
  2015. #endif
  2016. static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
  2017. {
  2018. vpid_sync_context(to_vmx(vcpu));
  2019. if (enable_ept) {
  2020. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  2021. return;
  2022. ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
  2023. }
  2024. }
  2025. static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
  2026. {
  2027. ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
  2028. vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
  2029. vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
  2030. }
  2031. static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
  2032. {
  2033. if (enable_ept && is_paging(vcpu))
  2034. vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
  2035. __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
  2036. }
  2037. static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
  2038. {
  2039. ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
  2040. vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
  2041. vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
  2042. }
  2043. static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
  2044. {
  2045. if (!test_bit(VCPU_EXREG_PDPTR,
  2046. (unsigned long *)&vcpu->arch.regs_dirty))
  2047. return;
  2048. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  2049. vmcs_write64(GUEST_PDPTR0, vcpu->arch.mmu.pdptrs[0]);
  2050. vmcs_write64(GUEST_PDPTR1, vcpu->arch.mmu.pdptrs[1]);
  2051. vmcs_write64(GUEST_PDPTR2, vcpu->arch.mmu.pdptrs[2]);
  2052. vmcs_write64(GUEST_PDPTR3, vcpu->arch.mmu.pdptrs[3]);
  2053. }
  2054. }
  2055. static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
  2056. {
  2057. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  2058. vcpu->arch.mmu.pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
  2059. vcpu->arch.mmu.pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
  2060. vcpu->arch.mmu.pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
  2061. vcpu->arch.mmu.pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
  2062. }
  2063. __set_bit(VCPU_EXREG_PDPTR,
  2064. (unsigned long *)&vcpu->arch.regs_avail);
  2065. __set_bit(VCPU_EXREG_PDPTR,
  2066. (unsigned long *)&vcpu->arch.regs_dirty);
  2067. }
  2068. static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
  2069. static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
  2070. unsigned long cr0,
  2071. struct kvm_vcpu *vcpu)
  2072. {
  2073. if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
  2074. vmx_decache_cr3(vcpu);
  2075. if (!(cr0 & X86_CR0_PG)) {
  2076. /* From paging/starting to nonpaging */
  2077. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  2078. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
  2079. (CPU_BASED_CR3_LOAD_EXITING |
  2080. CPU_BASED_CR3_STORE_EXITING));
  2081. vcpu->arch.cr0 = cr0;
  2082. vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
  2083. } else if (!is_paging(vcpu)) {
  2084. /* From nonpaging to paging */
  2085. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  2086. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
  2087. ~(CPU_BASED_CR3_LOAD_EXITING |
  2088. CPU_BASED_CR3_STORE_EXITING));
  2089. vcpu->arch.cr0 = cr0;
  2090. vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
  2091. }
  2092. if (!(cr0 & X86_CR0_WP))
  2093. *hw_cr0 &= ~X86_CR0_WP;
  2094. }
  2095. static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  2096. {
  2097. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2098. unsigned long hw_cr0;
  2099. if (enable_unrestricted_guest)
  2100. hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST)
  2101. | KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
  2102. else
  2103. hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON;
  2104. if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
  2105. enter_pmode(vcpu);
  2106. if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
  2107. enter_rmode(vcpu);
  2108. #ifdef CONFIG_X86_64
  2109. if (vcpu->arch.efer & EFER_LME) {
  2110. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
  2111. enter_lmode(vcpu);
  2112. if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
  2113. exit_lmode(vcpu);
  2114. }
  2115. #endif
  2116. if (enable_ept)
  2117. ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
  2118. if (!vcpu->fpu_active)
  2119. hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
  2120. vmcs_writel(CR0_READ_SHADOW, cr0);
  2121. vmcs_writel(GUEST_CR0, hw_cr0);
  2122. vcpu->arch.cr0 = cr0;
  2123. __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
  2124. }
  2125. static u64 construct_eptp(unsigned long root_hpa)
  2126. {
  2127. u64 eptp;
  2128. /* TODO write the value reading from MSR */
  2129. eptp = VMX_EPT_DEFAULT_MT |
  2130. VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
  2131. eptp |= (root_hpa & PAGE_MASK);
  2132. return eptp;
  2133. }
  2134. static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
  2135. {
  2136. unsigned long guest_cr3;
  2137. u64 eptp;
  2138. guest_cr3 = cr3;
  2139. if (enable_ept) {
  2140. eptp = construct_eptp(cr3);
  2141. vmcs_write64(EPT_POINTER, eptp);
  2142. guest_cr3 = is_paging(vcpu) ? kvm_read_cr3(vcpu) :
  2143. vcpu->kvm->arch.ept_identity_map_addr;
  2144. ept_load_pdptrs(vcpu);
  2145. }
  2146. vmx_flush_tlb(vcpu);
  2147. vmcs_writel(GUEST_CR3, guest_cr3);
  2148. }
  2149. static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  2150. {
  2151. unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ?
  2152. KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
  2153. if (cr4 & X86_CR4_VMXE) {
  2154. /*
  2155. * To use VMXON (and later other VMX instructions), a guest
  2156. * must first be able to turn on cr4.VMXE (see handle_vmon()).
  2157. * So basically the check on whether to allow nested VMX
  2158. * is here.
  2159. */
  2160. if (!nested_vmx_allowed(vcpu))
  2161. return 1;
  2162. } else if (to_vmx(vcpu)->nested.vmxon)
  2163. return 1;
  2164. vcpu->arch.cr4 = cr4;
  2165. if (enable_ept) {
  2166. if (!is_paging(vcpu)) {
  2167. hw_cr4 &= ~X86_CR4_PAE;
  2168. hw_cr4 |= X86_CR4_PSE;
  2169. } else if (!(cr4 & X86_CR4_PAE)) {
  2170. hw_cr4 &= ~X86_CR4_PAE;
  2171. }
  2172. }
  2173. vmcs_writel(CR4_READ_SHADOW, cr4);
  2174. vmcs_writel(GUEST_CR4, hw_cr4);
  2175. return 0;
  2176. }
  2177. static void vmx_get_segment(struct kvm_vcpu *vcpu,
  2178. struct kvm_segment *var, int seg)
  2179. {
  2180. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2181. struct kvm_save_segment *save;
  2182. u32 ar;
  2183. if (vmx->rmode.vm86_active
  2184. && (seg == VCPU_SREG_TR || seg == VCPU_SREG_ES
  2185. || seg == VCPU_SREG_DS || seg == VCPU_SREG_FS
  2186. || seg == VCPU_SREG_GS)
  2187. && !emulate_invalid_guest_state) {
  2188. switch (seg) {
  2189. case VCPU_SREG_TR: save = &vmx->rmode.tr; break;
  2190. case VCPU_SREG_ES: save = &vmx->rmode.es; break;
  2191. case VCPU_SREG_DS: save = &vmx->rmode.ds; break;
  2192. case VCPU_SREG_FS: save = &vmx->rmode.fs; break;
  2193. case VCPU_SREG_GS: save = &vmx->rmode.gs; break;
  2194. default: BUG();
  2195. }
  2196. var->selector = save->selector;
  2197. var->base = save->base;
  2198. var->limit = save->limit;
  2199. ar = save->ar;
  2200. if (seg == VCPU_SREG_TR
  2201. || var->selector == vmx_read_guest_seg_selector(vmx, seg))
  2202. goto use_saved_rmode_seg;
  2203. }
  2204. var->base = vmx_read_guest_seg_base(vmx, seg);
  2205. var->limit = vmx_read_guest_seg_limit(vmx, seg);
  2206. var->selector = vmx_read_guest_seg_selector(vmx, seg);
  2207. ar = vmx_read_guest_seg_ar(vmx, seg);
  2208. use_saved_rmode_seg:
  2209. if ((ar & AR_UNUSABLE_MASK) && !emulate_invalid_guest_state)
  2210. ar = 0;
  2211. var->type = ar & 15;
  2212. var->s = (ar >> 4) & 1;
  2213. var->dpl = (ar >> 5) & 3;
  2214. var->present = (ar >> 7) & 1;
  2215. var->avl = (ar >> 12) & 1;
  2216. var->l = (ar >> 13) & 1;
  2217. var->db = (ar >> 14) & 1;
  2218. var->g = (ar >> 15) & 1;
  2219. var->unusable = (ar >> 16) & 1;
  2220. }
  2221. static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
  2222. {
  2223. struct kvm_segment s;
  2224. if (to_vmx(vcpu)->rmode.vm86_active) {
  2225. vmx_get_segment(vcpu, &s, seg);
  2226. return s.base;
  2227. }
  2228. return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
  2229. }
  2230. static int __vmx_get_cpl(struct kvm_vcpu *vcpu)
  2231. {
  2232. if (!is_protmode(vcpu))
  2233. return 0;
  2234. if (!is_long_mode(vcpu)
  2235. && (kvm_get_rflags(vcpu) & X86_EFLAGS_VM)) /* if virtual 8086 */
  2236. return 3;
  2237. return vmx_read_guest_seg_selector(to_vmx(vcpu), VCPU_SREG_CS) & 3;
  2238. }
  2239. static int vmx_get_cpl(struct kvm_vcpu *vcpu)
  2240. {
  2241. if (!test_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail)) {
  2242. __set_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
  2243. to_vmx(vcpu)->cpl = __vmx_get_cpl(vcpu);
  2244. }
  2245. return to_vmx(vcpu)->cpl;
  2246. }
  2247. static u32 vmx_segment_access_rights(struct kvm_segment *var)
  2248. {
  2249. u32 ar;
  2250. if (var->unusable)
  2251. ar = 1 << 16;
  2252. else {
  2253. ar = var->type & 15;
  2254. ar |= (var->s & 1) << 4;
  2255. ar |= (var->dpl & 3) << 5;
  2256. ar |= (var->present & 1) << 7;
  2257. ar |= (var->avl & 1) << 12;
  2258. ar |= (var->l & 1) << 13;
  2259. ar |= (var->db & 1) << 14;
  2260. ar |= (var->g & 1) << 15;
  2261. }
  2262. if (ar == 0) /* a 0 value means unusable */
  2263. ar = AR_UNUSABLE_MASK;
  2264. return ar;
  2265. }
  2266. static void vmx_set_segment(struct kvm_vcpu *vcpu,
  2267. struct kvm_segment *var, int seg)
  2268. {
  2269. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2270. struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  2271. u32 ar;
  2272. vmx_segment_cache_clear(vmx);
  2273. if (vmx->rmode.vm86_active && seg == VCPU_SREG_TR) {
  2274. vmcs_write16(sf->selector, var->selector);
  2275. vmx->rmode.tr.selector = var->selector;
  2276. vmx->rmode.tr.base = var->base;
  2277. vmx->rmode.tr.limit = var->limit;
  2278. vmx->rmode.tr.ar = vmx_segment_access_rights(var);
  2279. return;
  2280. }
  2281. vmcs_writel(sf->base, var->base);
  2282. vmcs_write32(sf->limit, var->limit);
  2283. vmcs_write16(sf->selector, var->selector);
  2284. if (vmx->rmode.vm86_active && var->s) {
  2285. /*
  2286. * Hack real-mode segments into vm86 compatibility.
  2287. */
  2288. if (var->base == 0xffff0000 && var->selector == 0xf000)
  2289. vmcs_writel(sf->base, 0xf0000);
  2290. ar = 0xf3;
  2291. } else
  2292. ar = vmx_segment_access_rights(var);
  2293. /*
  2294. * Fix the "Accessed" bit in AR field of segment registers for older
  2295. * qemu binaries.
  2296. * IA32 arch specifies that at the time of processor reset the
  2297. * "Accessed" bit in the AR field of segment registers is 1. And qemu
  2298. * is setting it to 0 in the usedland code. This causes invalid guest
  2299. * state vmexit when "unrestricted guest" mode is turned on.
  2300. * Fix for this setup issue in cpu_reset is being pushed in the qemu
  2301. * tree. Newer qemu binaries with that qemu fix would not need this
  2302. * kvm hack.
  2303. */
  2304. if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
  2305. ar |= 0x1; /* Accessed */
  2306. vmcs_write32(sf->ar_bytes, ar);
  2307. __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
  2308. }
  2309. static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
  2310. {
  2311. u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
  2312. *db = (ar >> 14) & 1;
  2313. *l = (ar >> 13) & 1;
  2314. }
  2315. static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  2316. {
  2317. dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
  2318. dt->address = vmcs_readl(GUEST_IDTR_BASE);
  2319. }
  2320. static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  2321. {
  2322. vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
  2323. vmcs_writel(GUEST_IDTR_BASE, dt->address);
  2324. }
  2325. static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  2326. {
  2327. dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
  2328. dt->address = vmcs_readl(GUEST_GDTR_BASE);
  2329. }
  2330. static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  2331. {
  2332. vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
  2333. vmcs_writel(GUEST_GDTR_BASE, dt->address);
  2334. }
  2335. static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
  2336. {
  2337. struct kvm_segment var;
  2338. u32 ar;
  2339. vmx_get_segment(vcpu, &var, seg);
  2340. ar = vmx_segment_access_rights(&var);
  2341. if (var.base != (var.selector << 4))
  2342. return false;
  2343. if (var.limit != 0xffff)
  2344. return false;
  2345. if (ar != 0xf3)
  2346. return false;
  2347. return true;
  2348. }
  2349. static bool code_segment_valid(struct kvm_vcpu *vcpu)
  2350. {
  2351. struct kvm_segment cs;
  2352. unsigned int cs_rpl;
  2353. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  2354. cs_rpl = cs.selector & SELECTOR_RPL_MASK;
  2355. if (cs.unusable)
  2356. return false;
  2357. if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
  2358. return false;
  2359. if (!cs.s)
  2360. return false;
  2361. if (cs.type & AR_TYPE_WRITEABLE_MASK) {
  2362. if (cs.dpl > cs_rpl)
  2363. return false;
  2364. } else {
  2365. if (cs.dpl != cs_rpl)
  2366. return false;
  2367. }
  2368. if (!cs.present)
  2369. return false;
  2370. /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
  2371. return true;
  2372. }
  2373. static bool stack_segment_valid(struct kvm_vcpu *vcpu)
  2374. {
  2375. struct kvm_segment ss;
  2376. unsigned int ss_rpl;
  2377. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  2378. ss_rpl = ss.selector & SELECTOR_RPL_MASK;
  2379. if (ss.unusable)
  2380. return true;
  2381. if (ss.type != 3 && ss.type != 7)
  2382. return false;
  2383. if (!ss.s)
  2384. return false;
  2385. if (ss.dpl != ss_rpl) /* DPL != RPL */
  2386. return false;
  2387. if (!ss.present)
  2388. return false;
  2389. return true;
  2390. }
  2391. static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
  2392. {
  2393. struct kvm_segment var;
  2394. unsigned int rpl;
  2395. vmx_get_segment(vcpu, &var, seg);
  2396. rpl = var.selector & SELECTOR_RPL_MASK;
  2397. if (var.unusable)
  2398. return true;
  2399. if (!var.s)
  2400. return false;
  2401. if (!var.present)
  2402. return false;
  2403. if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
  2404. if (var.dpl < rpl) /* DPL < RPL */
  2405. return false;
  2406. }
  2407. /* TODO: Add other members to kvm_segment_field to allow checking for other access
  2408. * rights flags
  2409. */
  2410. return true;
  2411. }
  2412. static bool tr_valid(struct kvm_vcpu *vcpu)
  2413. {
  2414. struct kvm_segment tr;
  2415. vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
  2416. if (tr.unusable)
  2417. return false;
  2418. if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
  2419. return false;
  2420. if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
  2421. return false;
  2422. if (!tr.present)
  2423. return false;
  2424. return true;
  2425. }
  2426. static bool ldtr_valid(struct kvm_vcpu *vcpu)
  2427. {
  2428. struct kvm_segment ldtr;
  2429. vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
  2430. if (ldtr.unusable)
  2431. return true;
  2432. if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
  2433. return false;
  2434. if (ldtr.type != 2)
  2435. return false;
  2436. if (!ldtr.present)
  2437. return false;
  2438. return true;
  2439. }
  2440. static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
  2441. {
  2442. struct kvm_segment cs, ss;
  2443. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  2444. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  2445. return ((cs.selector & SELECTOR_RPL_MASK) ==
  2446. (ss.selector & SELECTOR_RPL_MASK));
  2447. }
  2448. /*
  2449. * Check if guest state is valid. Returns true if valid, false if
  2450. * not.
  2451. * We assume that registers are always usable
  2452. */
  2453. static bool guest_state_valid(struct kvm_vcpu *vcpu)
  2454. {
  2455. /* real mode guest state checks */
  2456. if (!is_protmode(vcpu)) {
  2457. if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
  2458. return false;
  2459. if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
  2460. return false;
  2461. if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
  2462. return false;
  2463. if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
  2464. return false;
  2465. if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
  2466. return false;
  2467. if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
  2468. return false;
  2469. } else {
  2470. /* protected mode guest state checks */
  2471. if (!cs_ss_rpl_check(vcpu))
  2472. return false;
  2473. if (!code_segment_valid(vcpu))
  2474. return false;
  2475. if (!stack_segment_valid(vcpu))
  2476. return false;
  2477. if (!data_segment_valid(vcpu, VCPU_SREG_DS))
  2478. return false;
  2479. if (!data_segment_valid(vcpu, VCPU_SREG_ES))
  2480. return false;
  2481. if (!data_segment_valid(vcpu, VCPU_SREG_FS))
  2482. return false;
  2483. if (!data_segment_valid(vcpu, VCPU_SREG_GS))
  2484. return false;
  2485. if (!tr_valid(vcpu))
  2486. return false;
  2487. if (!ldtr_valid(vcpu))
  2488. return false;
  2489. }
  2490. /* TODO:
  2491. * - Add checks on RIP
  2492. * - Add checks on RFLAGS
  2493. */
  2494. return true;
  2495. }
  2496. static int init_rmode_tss(struct kvm *kvm)
  2497. {
  2498. gfn_t fn;
  2499. u16 data = 0;
  2500. int r, idx, ret = 0;
  2501. idx = srcu_read_lock(&kvm->srcu);
  2502. fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
  2503. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  2504. if (r < 0)
  2505. goto out;
  2506. data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
  2507. r = kvm_write_guest_page(kvm, fn++, &data,
  2508. TSS_IOPB_BASE_OFFSET, sizeof(u16));
  2509. if (r < 0)
  2510. goto out;
  2511. r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
  2512. if (r < 0)
  2513. goto out;
  2514. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  2515. if (r < 0)
  2516. goto out;
  2517. data = ~0;
  2518. r = kvm_write_guest_page(kvm, fn, &data,
  2519. RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
  2520. sizeof(u8));
  2521. if (r < 0)
  2522. goto out;
  2523. ret = 1;
  2524. out:
  2525. srcu_read_unlock(&kvm->srcu, idx);
  2526. return ret;
  2527. }
  2528. static int init_rmode_identity_map(struct kvm *kvm)
  2529. {
  2530. int i, idx, r, ret;
  2531. pfn_t identity_map_pfn;
  2532. u32 tmp;
  2533. if (!enable_ept)
  2534. return 1;
  2535. if (unlikely(!kvm->arch.ept_identity_pagetable)) {
  2536. printk(KERN_ERR "EPT: identity-mapping pagetable "
  2537. "haven't been allocated!\n");
  2538. return 0;
  2539. }
  2540. if (likely(kvm->arch.ept_identity_pagetable_done))
  2541. return 1;
  2542. ret = 0;
  2543. identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
  2544. idx = srcu_read_lock(&kvm->srcu);
  2545. r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
  2546. if (r < 0)
  2547. goto out;
  2548. /* Set up identity-mapping pagetable for EPT in real mode */
  2549. for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
  2550. tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
  2551. _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
  2552. r = kvm_write_guest_page(kvm, identity_map_pfn,
  2553. &tmp, i * sizeof(tmp), sizeof(tmp));
  2554. if (r < 0)
  2555. goto out;
  2556. }
  2557. kvm->arch.ept_identity_pagetable_done = true;
  2558. ret = 1;
  2559. out:
  2560. srcu_read_unlock(&kvm->srcu, idx);
  2561. return ret;
  2562. }
  2563. static void seg_setup(int seg)
  2564. {
  2565. struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  2566. unsigned int ar;
  2567. vmcs_write16(sf->selector, 0);
  2568. vmcs_writel(sf->base, 0);
  2569. vmcs_write32(sf->limit, 0xffff);
  2570. if (enable_unrestricted_guest) {
  2571. ar = 0x93;
  2572. if (seg == VCPU_SREG_CS)
  2573. ar |= 0x08; /* code segment */
  2574. } else
  2575. ar = 0xf3;
  2576. vmcs_write32(sf->ar_bytes, ar);
  2577. }
  2578. static int alloc_apic_access_page(struct kvm *kvm)
  2579. {
  2580. struct kvm_userspace_memory_region kvm_userspace_mem;
  2581. int r = 0;
  2582. mutex_lock(&kvm->slots_lock);
  2583. if (kvm->arch.apic_access_page)
  2584. goto out;
  2585. kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
  2586. kvm_userspace_mem.flags = 0;
  2587. kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
  2588. kvm_userspace_mem.memory_size = PAGE_SIZE;
  2589. r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
  2590. if (r)
  2591. goto out;
  2592. kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
  2593. out:
  2594. mutex_unlock(&kvm->slots_lock);
  2595. return r;
  2596. }
  2597. static int alloc_identity_pagetable(struct kvm *kvm)
  2598. {
  2599. struct kvm_userspace_memory_region kvm_userspace_mem;
  2600. int r = 0;
  2601. mutex_lock(&kvm->slots_lock);
  2602. if (kvm->arch.ept_identity_pagetable)
  2603. goto out;
  2604. kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
  2605. kvm_userspace_mem.flags = 0;
  2606. kvm_userspace_mem.guest_phys_addr =
  2607. kvm->arch.ept_identity_map_addr;
  2608. kvm_userspace_mem.memory_size = PAGE_SIZE;
  2609. r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
  2610. if (r)
  2611. goto out;
  2612. kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
  2613. kvm->arch.ept_identity_map_addr >> PAGE_SHIFT);
  2614. out:
  2615. mutex_unlock(&kvm->slots_lock);
  2616. return r;
  2617. }
  2618. static void allocate_vpid(struct vcpu_vmx *vmx)
  2619. {
  2620. int vpid;
  2621. vmx->vpid = 0;
  2622. if (!enable_vpid)
  2623. return;
  2624. spin_lock(&vmx_vpid_lock);
  2625. vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
  2626. if (vpid < VMX_NR_VPIDS) {
  2627. vmx->vpid = vpid;
  2628. __set_bit(vpid, vmx_vpid_bitmap);
  2629. }
  2630. spin_unlock(&vmx_vpid_lock);
  2631. }
  2632. static void free_vpid(struct vcpu_vmx *vmx)
  2633. {
  2634. if (!enable_vpid)
  2635. return;
  2636. spin_lock(&vmx_vpid_lock);
  2637. if (vmx->vpid != 0)
  2638. __clear_bit(vmx->vpid, vmx_vpid_bitmap);
  2639. spin_unlock(&vmx_vpid_lock);
  2640. }
  2641. static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, u32 msr)
  2642. {
  2643. int f = sizeof(unsigned long);
  2644. if (!cpu_has_vmx_msr_bitmap())
  2645. return;
  2646. /*
  2647. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  2648. * have the write-low and read-high bitmap offsets the wrong way round.
  2649. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  2650. */
  2651. if (msr <= 0x1fff) {
  2652. __clear_bit(msr, msr_bitmap + 0x000 / f); /* read-low */
  2653. __clear_bit(msr, msr_bitmap + 0x800 / f); /* write-low */
  2654. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  2655. msr &= 0x1fff;
  2656. __clear_bit(msr, msr_bitmap + 0x400 / f); /* read-high */
  2657. __clear_bit(msr, msr_bitmap + 0xc00 / f); /* write-high */
  2658. }
  2659. }
  2660. static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
  2661. {
  2662. if (!longmode_only)
  2663. __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, msr);
  2664. __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, msr);
  2665. }
  2666. /*
  2667. * Sets up the vmcs for emulated real mode.
  2668. */
  2669. static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
  2670. {
  2671. u32 host_sysenter_cs, msr_low, msr_high;
  2672. u32 junk;
  2673. u64 host_pat;
  2674. unsigned long a;
  2675. struct desc_ptr dt;
  2676. int i;
  2677. unsigned long kvm_vmx_return;
  2678. u32 exec_control;
  2679. /* I/O */
  2680. vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
  2681. vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
  2682. if (cpu_has_vmx_msr_bitmap())
  2683. vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
  2684. vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
  2685. /* Control */
  2686. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
  2687. vmcs_config.pin_based_exec_ctrl);
  2688. exec_control = vmcs_config.cpu_based_exec_ctrl;
  2689. if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
  2690. exec_control &= ~CPU_BASED_TPR_SHADOW;
  2691. #ifdef CONFIG_X86_64
  2692. exec_control |= CPU_BASED_CR8_STORE_EXITING |
  2693. CPU_BASED_CR8_LOAD_EXITING;
  2694. #endif
  2695. }
  2696. if (!enable_ept)
  2697. exec_control |= CPU_BASED_CR3_STORE_EXITING |
  2698. CPU_BASED_CR3_LOAD_EXITING |
  2699. CPU_BASED_INVLPG_EXITING;
  2700. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
  2701. if (cpu_has_secondary_exec_ctrls()) {
  2702. exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
  2703. if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
  2704. exec_control &=
  2705. ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  2706. if (vmx->vpid == 0)
  2707. exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
  2708. if (!enable_ept) {
  2709. exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
  2710. enable_unrestricted_guest = 0;
  2711. }
  2712. if (!enable_unrestricted_guest)
  2713. exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
  2714. if (!ple_gap)
  2715. exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  2716. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
  2717. }
  2718. if (ple_gap) {
  2719. vmcs_write32(PLE_GAP, ple_gap);
  2720. vmcs_write32(PLE_WINDOW, ple_window);
  2721. }
  2722. vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
  2723. vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
  2724. vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
  2725. vmcs_writel(HOST_CR0, read_cr0() | X86_CR0_TS); /* 22.2.3 */
  2726. vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
  2727. vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
  2728. vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
  2729. vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  2730. vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  2731. vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
  2732. vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
  2733. vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  2734. #ifdef CONFIG_X86_64
  2735. rdmsrl(MSR_FS_BASE, a);
  2736. vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
  2737. rdmsrl(MSR_GS_BASE, a);
  2738. vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
  2739. #else
  2740. vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
  2741. vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
  2742. #endif
  2743. vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
  2744. native_store_idt(&dt);
  2745. vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
  2746. asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
  2747. vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
  2748. vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
  2749. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
  2750. vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
  2751. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
  2752. vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
  2753. rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
  2754. vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
  2755. rdmsrl(MSR_IA32_SYSENTER_ESP, a);
  2756. vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
  2757. rdmsrl(MSR_IA32_SYSENTER_EIP, a);
  2758. vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
  2759. if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
  2760. rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
  2761. host_pat = msr_low | ((u64) msr_high << 32);
  2762. vmcs_write64(HOST_IA32_PAT, host_pat);
  2763. }
  2764. if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
  2765. rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
  2766. host_pat = msr_low | ((u64) msr_high << 32);
  2767. /* Write the default value follow host pat */
  2768. vmcs_write64(GUEST_IA32_PAT, host_pat);
  2769. /* Keep arch.pat sync with GUEST_IA32_PAT */
  2770. vmx->vcpu.arch.pat = host_pat;
  2771. }
  2772. for (i = 0; i < NR_VMX_MSR; ++i) {
  2773. u32 index = vmx_msr_index[i];
  2774. u32 data_low, data_high;
  2775. int j = vmx->nmsrs;
  2776. if (rdmsr_safe(index, &data_low, &data_high) < 0)
  2777. continue;
  2778. if (wrmsr_safe(index, data_low, data_high) < 0)
  2779. continue;
  2780. vmx->guest_msrs[j].index = i;
  2781. vmx->guest_msrs[j].data = 0;
  2782. vmx->guest_msrs[j].mask = -1ull;
  2783. ++vmx->nmsrs;
  2784. }
  2785. vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
  2786. /* 22.2.1, 20.8.1 */
  2787. vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
  2788. vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
  2789. vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
  2790. if (enable_ept)
  2791. vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
  2792. vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
  2793. kvm_write_tsc(&vmx->vcpu, 0);
  2794. return 0;
  2795. }
  2796. static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
  2797. {
  2798. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2799. u64 msr;
  2800. int ret;
  2801. vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
  2802. vmx->rmode.vm86_active = 0;
  2803. vmx->soft_vnmi_blocked = 0;
  2804. vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
  2805. kvm_set_cr8(&vmx->vcpu, 0);
  2806. msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
  2807. if (kvm_vcpu_is_bsp(&vmx->vcpu))
  2808. msr |= MSR_IA32_APICBASE_BSP;
  2809. kvm_set_apic_base(&vmx->vcpu, msr);
  2810. ret = fx_init(&vmx->vcpu);
  2811. if (ret != 0)
  2812. goto out;
  2813. vmx_segment_cache_clear(vmx);
  2814. seg_setup(VCPU_SREG_CS);
  2815. /*
  2816. * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
  2817. * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
  2818. */
  2819. if (kvm_vcpu_is_bsp(&vmx->vcpu)) {
  2820. vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
  2821. vmcs_writel(GUEST_CS_BASE, 0x000f0000);
  2822. } else {
  2823. vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
  2824. vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
  2825. }
  2826. seg_setup(VCPU_SREG_DS);
  2827. seg_setup(VCPU_SREG_ES);
  2828. seg_setup(VCPU_SREG_FS);
  2829. seg_setup(VCPU_SREG_GS);
  2830. seg_setup(VCPU_SREG_SS);
  2831. vmcs_write16(GUEST_TR_SELECTOR, 0);
  2832. vmcs_writel(GUEST_TR_BASE, 0);
  2833. vmcs_write32(GUEST_TR_LIMIT, 0xffff);
  2834. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  2835. vmcs_write16(GUEST_LDTR_SELECTOR, 0);
  2836. vmcs_writel(GUEST_LDTR_BASE, 0);
  2837. vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
  2838. vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
  2839. vmcs_write32(GUEST_SYSENTER_CS, 0);
  2840. vmcs_writel(GUEST_SYSENTER_ESP, 0);
  2841. vmcs_writel(GUEST_SYSENTER_EIP, 0);
  2842. vmcs_writel(GUEST_RFLAGS, 0x02);
  2843. if (kvm_vcpu_is_bsp(&vmx->vcpu))
  2844. kvm_rip_write(vcpu, 0xfff0);
  2845. else
  2846. kvm_rip_write(vcpu, 0);
  2847. kvm_register_write(vcpu, VCPU_REGS_RSP, 0);
  2848. vmcs_writel(GUEST_DR7, 0x400);
  2849. vmcs_writel(GUEST_GDTR_BASE, 0);
  2850. vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
  2851. vmcs_writel(GUEST_IDTR_BASE, 0);
  2852. vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
  2853. vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
  2854. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
  2855. vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
  2856. /* Special registers */
  2857. vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
  2858. setup_msrs(vmx);
  2859. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
  2860. if (cpu_has_vmx_tpr_shadow()) {
  2861. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
  2862. if (vm_need_tpr_shadow(vmx->vcpu.kvm))
  2863. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
  2864. __pa(vmx->vcpu.arch.apic->regs));
  2865. vmcs_write32(TPR_THRESHOLD, 0);
  2866. }
  2867. if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
  2868. vmcs_write64(APIC_ACCESS_ADDR,
  2869. page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
  2870. if (vmx->vpid != 0)
  2871. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
  2872. vmx->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
  2873. vmx_set_cr0(&vmx->vcpu, kvm_read_cr0(vcpu)); /* enter rmode */
  2874. vmx_set_cr4(&vmx->vcpu, 0);
  2875. vmx_set_efer(&vmx->vcpu, 0);
  2876. vmx_fpu_activate(&vmx->vcpu);
  2877. update_exception_bitmap(&vmx->vcpu);
  2878. vpid_sync_context(vmx);
  2879. ret = 0;
  2880. /* HACK: Don't enable emulation on guest boot/reset */
  2881. vmx->emulation_required = 0;
  2882. out:
  2883. return ret;
  2884. }
  2885. static void enable_irq_window(struct kvm_vcpu *vcpu)
  2886. {
  2887. u32 cpu_based_vm_exec_control;
  2888. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  2889. cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
  2890. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  2891. }
  2892. static void enable_nmi_window(struct kvm_vcpu *vcpu)
  2893. {
  2894. u32 cpu_based_vm_exec_control;
  2895. if (!cpu_has_virtual_nmis()) {
  2896. enable_irq_window(vcpu);
  2897. return;
  2898. }
  2899. if (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
  2900. enable_irq_window(vcpu);
  2901. return;
  2902. }
  2903. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  2904. cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
  2905. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  2906. }
  2907. static void vmx_inject_irq(struct kvm_vcpu *vcpu)
  2908. {
  2909. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2910. uint32_t intr;
  2911. int irq = vcpu->arch.interrupt.nr;
  2912. trace_kvm_inj_virq(irq);
  2913. ++vcpu->stat.irq_injections;
  2914. if (vmx->rmode.vm86_active) {
  2915. int inc_eip = 0;
  2916. if (vcpu->arch.interrupt.soft)
  2917. inc_eip = vcpu->arch.event_exit_inst_len;
  2918. if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
  2919. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  2920. return;
  2921. }
  2922. intr = irq | INTR_INFO_VALID_MASK;
  2923. if (vcpu->arch.interrupt.soft) {
  2924. intr |= INTR_TYPE_SOFT_INTR;
  2925. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  2926. vmx->vcpu.arch.event_exit_inst_len);
  2927. } else
  2928. intr |= INTR_TYPE_EXT_INTR;
  2929. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
  2930. vmx_clear_hlt(vcpu);
  2931. }
  2932. static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
  2933. {
  2934. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2935. if (!cpu_has_virtual_nmis()) {
  2936. /*
  2937. * Tracking the NMI-blocked state in software is built upon
  2938. * finding the next open IRQ window. This, in turn, depends on
  2939. * well-behaving guests: They have to keep IRQs disabled at
  2940. * least as long as the NMI handler runs. Otherwise we may
  2941. * cause NMI nesting, maybe breaking the guest. But as this is
  2942. * highly unlikely, we can live with the residual risk.
  2943. */
  2944. vmx->soft_vnmi_blocked = 1;
  2945. vmx->vnmi_blocked_time = 0;
  2946. }
  2947. ++vcpu->stat.nmi_injections;
  2948. vmx->nmi_known_unmasked = false;
  2949. if (vmx->rmode.vm86_active) {
  2950. if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
  2951. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  2952. return;
  2953. }
  2954. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  2955. INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
  2956. vmx_clear_hlt(vcpu);
  2957. }
  2958. static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
  2959. {
  2960. if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
  2961. return 0;
  2962. return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
  2963. (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
  2964. | GUEST_INTR_STATE_NMI));
  2965. }
  2966. static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
  2967. {
  2968. if (!cpu_has_virtual_nmis())
  2969. return to_vmx(vcpu)->soft_vnmi_blocked;
  2970. if (to_vmx(vcpu)->nmi_known_unmasked)
  2971. return false;
  2972. return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
  2973. }
  2974. static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
  2975. {
  2976. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2977. if (!cpu_has_virtual_nmis()) {
  2978. if (vmx->soft_vnmi_blocked != masked) {
  2979. vmx->soft_vnmi_blocked = masked;
  2980. vmx->vnmi_blocked_time = 0;
  2981. }
  2982. } else {
  2983. vmx->nmi_known_unmasked = !masked;
  2984. if (masked)
  2985. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  2986. GUEST_INTR_STATE_NMI);
  2987. else
  2988. vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
  2989. GUEST_INTR_STATE_NMI);
  2990. }
  2991. }
  2992. static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
  2993. {
  2994. return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
  2995. !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
  2996. (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
  2997. }
  2998. static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
  2999. {
  3000. int ret;
  3001. struct kvm_userspace_memory_region tss_mem = {
  3002. .slot = TSS_PRIVATE_MEMSLOT,
  3003. .guest_phys_addr = addr,
  3004. .memory_size = PAGE_SIZE * 3,
  3005. .flags = 0,
  3006. };
  3007. ret = kvm_set_memory_region(kvm, &tss_mem, 0);
  3008. if (ret)
  3009. return ret;
  3010. kvm->arch.tss_addr = addr;
  3011. if (!init_rmode_tss(kvm))
  3012. return -ENOMEM;
  3013. return 0;
  3014. }
  3015. static int handle_rmode_exception(struct kvm_vcpu *vcpu,
  3016. int vec, u32 err_code)
  3017. {
  3018. /*
  3019. * Instruction with address size override prefix opcode 0x67
  3020. * Cause the #SS fault with 0 error code in VM86 mode.
  3021. */
  3022. if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
  3023. if (emulate_instruction(vcpu, 0) == EMULATE_DONE)
  3024. return 1;
  3025. /*
  3026. * Forward all other exceptions that are valid in real mode.
  3027. * FIXME: Breaks guest debugging in real mode, needs to be fixed with
  3028. * the required debugging infrastructure rework.
  3029. */
  3030. switch (vec) {
  3031. case DB_VECTOR:
  3032. if (vcpu->guest_debug &
  3033. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
  3034. return 0;
  3035. kvm_queue_exception(vcpu, vec);
  3036. return 1;
  3037. case BP_VECTOR:
  3038. /*
  3039. * Update instruction length as we may reinject the exception
  3040. * from user space while in guest debugging mode.
  3041. */
  3042. to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
  3043. vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  3044. if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  3045. return 0;
  3046. /* fall through */
  3047. case DE_VECTOR:
  3048. case OF_VECTOR:
  3049. case BR_VECTOR:
  3050. case UD_VECTOR:
  3051. case DF_VECTOR:
  3052. case SS_VECTOR:
  3053. case GP_VECTOR:
  3054. case MF_VECTOR:
  3055. kvm_queue_exception(vcpu, vec);
  3056. return 1;
  3057. }
  3058. return 0;
  3059. }
  3060. /*
  3061. * Trigger machine check on the host. We assume all the MSRs are already set up
  3062. * by the CPU and that we still run on the same CPU as the MCE occurred on.
  3063. * We pass a fake environment to the machine check handler because we want
  3064. * the guest to be always treated like user space, no matter what context
  3065. * it used internally.
  3066. */
  3067. static void kvm_machine_check(void)
  3068. {
  3069. #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
  3070. struct pt_regs regs = {
  3071. .cs = 3, /* Fake ring 3 no matter what the guest ran on */
  3072. .flags = X86_EFLAGS_IF,
  3073. };
  3074. do_machine_check(&regs, 0);
  3075. #endif
  3076. }
  3077. static int handle_machine_check(struct kvm_vcpu *vcpu)
  3078. {
  3079. /* already handled by vcpu_run */
  3080. return 1;
  3081. }
  3082. static int handle_exception(struct kvm_vcpu *vcpu)
  3083. {
  3084. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3085. struct kvm_run *kvm_run = vcpu->run;
  3086. u32 intr_info, ex_no, error_code;
  3087. unsigned long cr2, rip, dr6;
  3088. u32 vect_info;
  3089. enum emulation_result er;
  3090. vect_info = vmx->idt_vectoring_info;
  3091. intr_info = vmx->exit_intr_info;
  3092. if (is_machine_check(intr_info))
  3093. return handle_machine_check(vcpu);
  3094. if ((vect_info & VECTORING_INFO_VALID_MASK) &&
  3095. !is_page_fault(intr_info)) {
  3096. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  3097. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
  3098. vcpu->run->internal.ndata = 2;
  3099. vcpu->run->internal.data[0] = vect_info;
  3100. vcpu->run->internal.data[1] = intr_info;
  3101. return 0;
  3102. }
  3103. if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
  3104. return 1; /* already handled by vmx_vcpu_run() */
  3105. if (is_no_device(intr_info)) {
  3106. vmx_fpu_activate(vcpu);
  3107. return 1;
  3108. }
  3109. if (is_invalid_opcode(intr_info)) {
  3110. er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
  3111. if (er != EMULATE_DONE)
  3112. kvm_queue_exception(vcpu, UD_VECTOR);
  3113. return 1;
  3114. }
  3115. error_code = 0;
  3116. if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
  3117. error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
  3118. if (is_page_fault(intr_info)) {
  3119. /* EPT won't cause page fault directly */
  3120. if (enable_ept)
  3121. BUG();
  3122. cr2 = vmcs_readl(EXIT_QUALIFICATION);
  3123. trace_kvm_page_fault(cr2, error_code);
  3124. if (kvm_event_needs_reinjection(vcpu))
  3125. kvm_mmu_unprotect_page_virt(vcpu, cr2);
  3126. return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
  3127. }
  3128. if (vmx->rmode.vm86_active &&
  3129. handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
  3130. error_code)) {
  3131. if (vcpu->arch.halt_request) {
  3132. vcpu->arch.halt_request = 0;
  3133. return kvm_emulate_halt(vcpu);
  3134. }
  3135. return 1;
  3136. }
  3137. ex_no = intr_info & INTR_INFO_VECTOR_MASK;
  3138. switch (ex_no) {
  3139. case DB_VECTOR:
  3140. dr6 = vmcs_readl(EXIT_QUALIFICATION);
  3141. if (!(vcpu->guest_debug &
  3142. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
  3143. vcpu->arch.dr6 = dr6 | DR6_FIXED_1;
  3144. kvm_queue_exception(vcpu, DB_VECTOR);
  3145. return 1;
  3146. }
  3147. kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
  3148. kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
  3149. /* fall through */
  3150. case BP_VECTOR:
  3151. /*
  3152. * Update instruction length as we may reinject #BP from
  3153. * user space while in guest debugging mode. Reading it for
  3154. * #DB as well causes no harm, it is not used in that case.
  3155. */
  3156. vmx->vcpu.arch.event_exit_inst_len =
  3157. vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  3158. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  3159. rip = kvm_rip_read(vcpu);
  3160. kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
  3161. kvm_run->debug.arch.exception = ex_no;
  3162. break;
  3163. default:
  3164. kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
  3165. kvm_run->ex.exception = ex_no;
  3166. kvm_run->ex.error_code = error_code;
  3167. break;
  3168. }
  3169. return 0;
  3170. }
  3171. static int handle_external_interrupt(struct kvm_vcpu *vcpu)
  3172. {
  3173. ++vcpu->stat.irq_exits;
  3174. return 1;
  3175. }
  3176. static int handle_triple_fault(struct kvm_vcpu *vcpu)
  3177. {
  3178. vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
  3179. return 0;
  3180. }
  3181. static int handle_io(struct kvm_vcpu *vcpu)
  3182. {
  3183. unsigned long exit_qualification;
  3184. int size, in, string;
  3185. unsigned port;
  3186. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  3187. string = (exit_qualification & 16) != 0;
  3188. in = (exit_qualification & 8) != 0;
  3189. ++vcpu->stat.io_exits;
  3190. if (string || in)
  3191. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  3192. port = exit_qualification >> 16;
  3193. size = (exit_qualification & 7) + 1;
  3194. skip_emulated_instruction(vcpu);
  3195. return kvm_fast_pio_out(vcpu, size, port);
  3196. }
  3197. static void
  3198. vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
  3199. {
  3200. /*
  3201. * Patch in the VMCALL instruction:
  3202. */
  3203. hypercall[0] = 0x0f;
  3204. hypercall[1] = 0x01;
  3205. hypercall[2] = 0xc1;
  3206. }
  3207. static int handle_cr(struct kvm_vcpu *vcpu)
  3208. {
  3209. unsigned long exit_qualification, val;
  3210. int cr;
  3211. int reg;
  3212. int err;
  3213. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  3214. cr = exit_qualification & 15;
  3215. reg = (exit_qualification >> 8) & 15;
  3216. switch ((exit_qualification >> 4) & 3) {
  3217. case 0: /* mov to cr */
  3218. val = kvm_register_read(vcpu, reg);
  3219. trace_kvm_cr_write(cr, val);
  3220. switch (cr) {
  3221. case 0:
  3222. err = kvm_set_cr0(vcpu, val);
  3223. kvm_complete_insn_gp(vcpu, err);
  3224. return 1;
  3225. case 3:
  3226. err = kvm_set_cr3(vcpu, val);
  3227. kvm_complete_insn_gp(vcpu, err);
  3228. return 1;
  3229. case 4:
  3230. err = kvm_set_cr4(vcpu, val);
  3231. kvm_complete_insn_gp(vcpu, err);
  3232. return 1;
  3233. case 8: {
  3234. u8 cr8_prev = kvm_get_cr8(vcpu);
  3235. u8 cr8 = kvm_register_read(vcpu, reg);
  3236. err = kvm_set_cr8(vcpu, cr8);
  3237. kvm_complete_insn_gp(vcpu, err);
  3238. if (irqchip_in_kernel(vcpu->kvm))
  3239. return 1;
  3240. if (cr8_prev <= cr8)
  3241. return 1;
  3242. vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
  3243. return 0;
  3244. }
  3245. };
  3246. break;
  3247. case 2: /* clts */
  3248. vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
  3249. trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
  3250. skip_emulated_instruction(vcpu);
  3251. vmx_fpu_activate(vcpu);
  3252. return 1;
  3253. case 1: /*mov from cr*/
  3254. switch (cr) {
  3255. case 3:
  3256. val = kvm_read_cr3(vcpu);
  3257. kvm_register_write(vcpu, reg, val);
  3258. trace_kvm_cr_read(cr, val);
  3259. skip_emulated_instruction(vcpu);
  3260. return 1;
  3261. case 8:
  3262. val = kvm_get_cr8(vcpu);
  3263. kvm_register_write(vcpu, reg, val);
  3264. trace_kvm_cr_read(cr, val);
  3265. skip_emulated_instruction(vcpu);
  3266. return 1;
  3267. }
  3268. break;
  3269. case 3: /* lmsw */
  3270. val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
  3271. trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
  3272. kvm_lmsw(vcpu, val);
  3273. skip_emulated_instruction(vcpu);
  3274. return 1;
  3275. default:
  3276. break;
  3277. }
  3278. vcpu->run->exit_reason = 0;
  3279. pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
  3280. (int)(exit_qualification >> 4) & 3, cr);
  3281. return 0;
  3282. }
  3283. static int handle_dr(struct kvm_vcpu *vcpu)
  3284. {
  3285. unsigned long exit_qualification;
  3286. int dr, reg;
  3287. /* Do not handle if the CPL > 0, will trigger GP on re-entry */
  3288. if (!kvm_require_cpl(vcpu, 0))
  3289. return 1;
  3290. dr = vmcs_readl(GUEST_DR7);
  3291. if (dr & DR7_GD) {
  3292. /*
  3293. * As the vm-exit takes precedence over the debug trap, we
  3294. * need to emulate the latter, either for the host or the
  3295. * guest debugging itself.
  3296. */
  3297. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
  3298. vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
  3299. vcpu->run->debug.arch.dr7 = dr;
  3300. vcpu->run->debug.arch.pc =
  3301. vmcs_readl(GUEST_CS_BASE) +
  3302. vmcs_readl(GUEST_RIP);
  3303. vcpu->run->debug.arch.exception = DB_VECTOR;
  3304. vcpu->run->exit_reason = KVM_EXIT_DEBUG;
  3305. return 0;
  3306. } else {
  3307. vcpu->arch.dr7 &= ~DR7_GD;
  3308. vcpu->arch.dr6 |= DR6_BD;
  3309. vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
  3310. kvm_queue_exception(vcpu, DB_VECTOR);
  3311. return 1;
  3312. }
  3313. }
  3314. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  3315. dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
  3316. reg = DEBUG_REG_ACCESS_REG(exit_qualification);
  3317. if (exit_qualification & TYPE_MOV_FROM_DR) {
  3318. unsigned long val;
  3319. if (!kvm_get_dr(vcpu, dr, &val))
  3320. kvm_register_write(vcpu, reg, val);
  3321. } else
  3322. kvm_set_dr(vcpu, dr, vcpu->arch.regs[reg]);
  3323. skip_emulated_instruction(vcpu);
  3324. return 1;
  3325. }
  3326. static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
  3327. {
  3328. vmcs_writel(GUEST_DR7, val);
  3329. }
  3330. static int handle_cpuid(struct kvm_vcpu *vcpu)
  3331. {
  3332. kvm_emulate_cpuid(vcpu);
  3333. return 1;
  3334. }
  3335. static int handle_rdmsr(struct kvm_vcpu *vcpu)
  3336. {
  3337. u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
  3338. u64 data;
  3339. if (vmx_get_msr(vcpu, ecx, &data)) {
  3340. trace_kvm_msr_read_ex(ecx);
  3341. kvm_inject_gp(vcpu, 0);
  3342. return 1;
  3343. }
  3344. trace_kvm_msr_read(ecx, data);
  3345. /* FIXME: handling of bits 32:63 of rax, rdx */
  3346. vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
  3347. vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
  3348. skip_emulated_instruction(vcpu);
  3349. return 1;
  3350. }
  3351. static int handle_wrmsr(struct kvm_vcpu *vcpu)
  3352. {
  3353. u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
  3354. u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
  3355. | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
  3356. if (vmx_set_msr(vcpu, ecx, data) != 0) {
  3357. trace_kvm_msr_write_ex(ecx, data);
  3358. kvm_inject_gp(vcpu, 0);
  3359. return 1;
  3360. }
  3361. trace_kvm_msr_write(ecx, data);
  3362. skip_emulated_instruction(vcpu);
  3363. return 1;
  3364. }
  3365. static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
  3366. {
  3367. kvm_make_request(KVM_REQ_EVENT, vcpu);
  3368. return 1;
  3369. }
  3370. static int handle_interrupt_window(struct kvm_vcpu *vcpu)
  3371. {
  3372. u32 cpu_based_vm_exec_control;
  3373. /* clear pending irq */
  3374. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  3375. cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
  3376. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  3377. kvm_make_request(KVM_REQ_EVENT, vcpu);
  3378. ++vcpu->stat.irq_window_exits;
  3379. /*
  3380. * If the user space waits to inject interrupts, exit as soon as
  3381. * possible
  3382. */
  3383. if (!irqchip_in_kernel(vcpu->kvm) &&
  3384. vcpu->run->request_interrupt_window &&
  3385. !kvm_cpu_has_interrupt(vcpu)) {
  3386. vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
  3387. return 0;
  3388. }
  3389. return 1;
  3390. }
  3391. static int handle_halt(struct kvm_vcpu *vcpu)
  3392. {
  3393. skip_emulated_instruction(vcpu);
  3394. return kvm_emulate_halt(vcpu);
  3395. }
  3396. static int handle_vmcall(struct kvm_vcpu *vcpu)
  3397. {
  3398. skip_emulated_instruction(vcpu);
  3399. kvm_emulate_hypercall(vcpu);
  3400. return 1;
  3401. }
  3402. static int handle_vmx_insn(struct kvm_vcpu *vcpu)
  3403. {
  3404. kvm_queue_exception(vcpu, UD_VECTOR);
  3405. return 1;
  3406. }
  3407. static int handle_invd(struct kvm_vcpu *vcpu)
  3408. {
  3409. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  3410. }
  3411. static int handle_invlpg(struct kvm_vcpu *vcpu)
  3412. {
  3413. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  3414. kvm_mmu_invlpg(vcpu, exit_qualification);
  3415. skip_emulated_instruction(vcpu);
  3416. return 1;
  3417. }
  3418. static int handle_wbinvd(struct kvm_vcpu *vcpu)
  3419. {
  3420. skip_emulated_instruction(vcpu);
  3421. kvm_emulate_wbinvd(vcpu);
  3422. return 1;
  3423. }
  3424. static int handle_xsetbv(struct kvm_vcpu *vcpu)
  3425. {
  3426. u64 new_bv = kvm_read_edx_eax(vcpu);
  3427. u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
  3428. if (kvm_set_xcr(vcpu, index, new_bv) == 0)
  3429. skip_emulated_instruction(vcpu);
  3430. return 1;
  3431. }
  3432. static int handle_apic_access(struct kvm_vcpu *vcpu)
  3433. {
  3434. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  3435. }
  3436. static int handle_task_switch(struct kvm_vcpu *vcpu)
  3437. {
  3438. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3439. unsigned long exit_qualification;
  3440. bool has_error_code = false;
  3441. u32 error_code = 0;
  3442. u16 tss_selector;
  3443. int reason, type, idt_v;
  3444. idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
  3445. type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
  3446. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  3447. reason = (u32)exit_qualification >> 30;
  3448. if (reason == TASK_SWITCH_GATE && idt_v) {
  3449. switch (type) {
  3450. case INTR_TYPE_NMI_INTR:
  3451. vcpu->arch.nmi_injected = false;
  3452. vmx_set_nmi_mask(vcpu, true);
  3453. break;
  3454. case INTR_TYPE_EXT_INTR:
  3455. case INTR_TYPE_SOFT_INTR:
  3456. kvm_clear_interrupt_queue(vcpu);
  3457. break;
  3458. case INTR_TYPE_HARD_EXCEPTION:
  3459. if (vmx->idt_vectoring_info &
  3460. VECTORING_INFO_DELIVER_CODE_MASK) {
  3461. has_error_code = true;
  3462. error_code =
  3463. vmcs_read32(IDT_VECTORING_ERROR_CODE);
  3464. }
  3465. /* fall through */
  3466. case INTR_TYPE_SOFT_EXCEPTION:
  3467. kvm_clear_exception_queue(vcpu);
  3468. break;
  3469. default:
  3470. break;
  3471. }
  3472. }
  3473. tss_selector = exit_qualification;
  3474. if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
  3475. type != INTR_TYPE_EXT_INTR &&
  3476. type != INTR_TYPE_NMI_INTR))
  3477. skip_emulated_instruction(vcpu);
  3478. if (kvm_task_switch(vcpu, tss_selector, reason,
  3479. has_error_code, error_code) == EMULATE_FAIL) {
  3480. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  3481. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  3482. vcpu->run->internal.ndata = 0;
  3483. return 0;
  3484. }
  3485. /* clear all local breakpoint enable flags */
  3486. vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
  3487. /*
  3488. * TODO: What about debug traps on tss switch?
  3489. * Are we supposed to inject them and update dr6?
  3490. */
  3491. return 1;
  3492. }
  3493. static int handle_ept_violation(struct kvm_vcpu *vcpu)
  3494. {
  3495. unsigned long exit_qualification;
  3496. gpa_t gpa;
  3497. int gla_validity;
  3498. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  3499. if (exit_qualification & (1 << 6)) {
  3500. printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
  3501. return -EINVAL;
  3502. }
  3503. gla_validity = (exit_qualification >> 7) & 0x3;
  3504. if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
  3505. printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
  3506. printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
  3507. (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
  3508. vmcs_readl(GUEST_LINEAR_ADDRESS));
  3509. printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
  3510. (long unsigned int)exit_qualification);
  3511. vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
  3512. vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
  3513. return 0;
  3514. }
  3515. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  3516. trace_kvm_page_fault(gpa, exit_qualification);
  3517. return kvm_mmu_page_fault(vcpu, gpa, exit_qualification & 0x3, NULL, 0);
  3518. }
  3519. static u64 ept_rsvd_mask(u64 spte, int level)
  3520. {
  3521. int i;
  3522. u64 mask = 0;
  3523. for (i = 51; i > boot_cpu_data.x86_phys_bits; i--)
  3524. mask |= (1ULL << i);
  3525. if (level > 2)
  3526. /* bits 7:3 reserved */
  3527. mask |= 0xf8;
  3528. else if (level == 2) {
  3529. if (spte & (1ULL << 7))
  3530. /* 2MB ref, bits 20:12 reserved */
  3531. mask |= 0x1ff000;
  3532. else
  3533. /* bits 6:3 reserved */
  3534. mask |= 0x78;
  3535. }
  3536. return mask;
  3537. }
  3538. static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte,
  3539. int level)
  3540. {
  3541. printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level);
  3542. /* 010b (write-only) */
  3543. WARN_ON((spte & 0x7) == 0x2);
  3544. /* 110b (write/execute) */
  3545. WARN_ON((spte & 0x7) == 0x6);
  3546. /* 100b (execute-only) and value not supported by logical processor */
  3547. if (!cpu_has_vmx_ept_execute_only())
  3548. WARN_ON((spte & 0x7) == 0x4);
  3549. /* not 000b */
  3550. if ((spte & 0x7)) {
  3551. u64 rsvd_bits = spte & ept_rsvd_mask(spte, level);
  3552. if (rsvd_bits != 0) {
  3553. printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n",
  3554. __func__, rsvd_bits);
  3555. WARN_ON(1);
  3556. }
  3557. if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) {
  3558. u64 ept_mem_type = (spte & 0x38) >> 3;
  3559. if (ept_mem_type == 2 || ept_mem_type == 3 ||
  3560. ept_mem_type == 7) {
  3561. printk(KERN_ERR "%s: ept_mem_type=0x%llx\n",
  3562. __func__, ept_mem_type);
  3563. WARN_ON(1);
  3564. }
  3565. }
  3566. }
  3567. }
  3568. static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
  3569. {
  3570. u64 sptes[4];
  3571. int nr_sptes, i;
  3572. gpa_t gpa;
  3573. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  3574. printk(KERN_ERR "EPT: Misconfiguration.\n");
  3575. printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa);
  3576. nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes);
  3577. for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i)
  3578. ept_misconfig_inspect_spte(vcpu, sptes[i-1], i);
  3579. vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
  3580. vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
  3581. return 0;
  3582. }
  3583. static int handle_nmi_window(struct kvm_vcpu *vcpu)
  3584. {
  3585. u32 cpu_based_vm_exec_control;
  3586. /* clear pending NMI */
  3587. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  3588. cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
  3589. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  3590. ++vcpu->stat.nmi_window_exits;
  3591. kvm_make_request(KVM_REQ_EVENT, vcpu);
  3592. return 1;
  3593. }
  3594. static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
  3595. {
  3596. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3597. enum emulation_result err = EMULATE_DONE;
  3598. int ret = 1;
  3599. u32 cpu_exec_ctrl;
  3600. bool intr_window_requested;
  3601. cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  3602. intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
  3603. while (!guest_state_valid(vcpu)) {
  3604. if (intr_window_requested
  3605. && (kvm_get_rflags(&vmx->vcpu) & X86_EFLAGS_IF))
  3606. return handle_interrupt_window(&vmx->vcpu);
  3607. err = emulate_instruction(vcpu, 0);
  3608. if (err == EMULATE_DO_MMIO) {
  3609. ret = 0;
  3610. goto out;
  3611. }
  3612. if (err != EMULATE_DONE)
  3613. return 0;
  3614. if (signal_pending(current))
  3615. goto out;
  3616. if (need_resched())
  3617. schedule();
  3618. }
  3619. vmx->emulation_required = 0;
  3620. out:
  3621. return ret;
  3622. }
  3623. /*
  3624. * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
  3625. * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
  3626. */
  3627. static int handle_pause(struct kvm_vcpu *vcpu)
  3628. {
  3629. skip_emulated_instruction(vcpu);
  3630. kvm_vcpu_on_spin(vcpu);
  3631. return 1;
  3632. }
  3633. static int handle_invalid_op(struct kvm_vcpu *vcpu)
  3634. {
  3635. kvm_queue_exception(vcpu, UD_VECTOR);
  3636. return 1;
  3637. }
  3638. /*
  3639. * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12.
  3640. * We could reuse a single VMCS for all the L2 guests, but we also want the
  3641. * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this
  3642. * allows keeping them loaded on the processor, and in the future will allow
  3643. * optimizations where prepare_vmcs02 doesn't need to set all the fields on
  3644. * every entry if they never change.
  3645. * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE
  3646. * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first.
  3647. *
  3648. * The following functions allocate and free a vmcs02 in this pool.
  3649. */
  3650. /* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */
  3651. static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx)
  3652. {
  3653. struct vmcs02_list *item;
  3654. list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
  3655. if (item->vmptr == vmx->nested.current_vmptr) {
  3656. list_move(&item->list, &vmx->nested.vmcs02_pool);
  3657. return &item->vmcs02;
  3658. }
  3659. if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) {
  3660. /* Recycle the least recently used VMCS. */
  3661. item = list_entry(vmx->nested.vmcs02_pool.prev,
  3662. struct vmcs02_list, list);
  3663. item->vmptr = vmx->nested.current_vmptr;
  3664. list_move(&item->list, &vmx->nested.vmcs02_pool);
  3665. return &item->vmcs02;
  3666. }
  3667. /* Create a new VMCS */
  3668. item = (struct vmcs02_list *)
  3669. kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL);
  3670. if (!item)
  3671. return NULL;
  3672. item->vmcs02.vmcs = alloc_vmcs();
  3673. if (!item->vmcs02.vmcs) {
  3674. kfree(item);
  3675. return NULL;
  3676. }
  3677. loaded_vmcs_init(&item->vmcs02);
  3678. item->vmptr = vmx->nested.current_vmptr;
  3679. list_add(&(item->list), &(vmx->nested.vmcs02_pool));
  3680. vmx->nested.vmcs02_num++;
  3681. return &item->vmcs02;
  3682. }
  3683. /* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */
  3684. static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr)
  3685. {
  3686. struct vmcs02_list *item;
  3687. list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
  3688. if (item->vmptr == vmptr) {
  3689. free_loaded_vmcs(&item->vmcs02);
  3690. list_del(&item->list);
  3691. kfree(item);
  3692. vmx->nested.vmcs02_num--;
  3693. return;
  3694. }
  3695. }
  3696. /*
  3697. * Free all VMCSs saved for this vcpu, except the one pointed by
  3698. * vmx->loaded_vmcs. These include the VMCSs in vmcs02_pool (except the one
  3699. * currently used, if running L2), and vmcs01 when running L2.
  3700. */
  3701. static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx)
  3702. {
  3703. struct vmcs02_list *item, *n;
  3704. list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) {
  3705. if (vmx->loaded_vmcs != &item->vmcs02)
  3706. free_loaded_vmcs(&item->vmcs02);
  3707. list_del(&item->list);
  3708. kfree(item);
  3709. }
  3710. vmx->nested.vmcs02_num = 0;
  3711. if (vmx->loaded_vmcs != &vmx->vmcs01)
  3712. free_loaded_vmcs(&vmx->vmcs01);
  3713. }
  3714. /*
  3715. * Emulate the VMXON instruction.
  3716. * Currently, we just remember that VMX is active, and do not save or even
  3717. * inspect the argument to VMXON (the so-called "VMXON pointer") because we
  3718. * do not currently need to store anything in that guest-allocated memory
  3719. * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
  3720. * argument is different from the VMXON pointer (which the spec says they do).
  3721. */
  3722. static int handle_vmon(struct kvm_vcpu *vcpu)
  3723. {
  3724. struct kvm_segment cs;
  3725. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3726. /* The Intel VMX Instruction Reference lists a bunch of bits that
  3727. * are prerequisite to running VMXON, most notably cr4.VMXE must be
  3728. * set to 1 (see vmx_set_cr4() for when we allow the guest to set this).
  3729. * Otherwise, we should fail with #UD. We test these now:
  3730. */
  3731. if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) ||
  3732. !kvm_read_cr0_bits(vcpu, X86_CR0_PE) ||
  3733. (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
  3734. kvm_queue_exception(vcpu, UD_VECTOR);
  3735. return 1;
  3736. }
  3737. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  3738. if (is_long_mode(vcpu) && !cs.l) {
  3739. kvm_queue_exception(vcpu, UD_VECTOR);
  3740. return 1;
  3741. }
  3742. if (vmx_get_cpl(vcpu)) {
  3743. kvm_inject_gp(vcpu, 0);
  3744. return 1;
  3745. }
  3746. INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool));
  3747. vmx->nested.vmcs02_num = 0;
  3748. vmx->nested.vmxon = true;
  3749. skip_emulated_instruction(vcpu);
  3750. return 1;
  3751. }
  3752. /*
  3753. * Intel's VMX Instruction Reference specifies a common set of prerequisites
  3754. * for running VMX instructions (except VMXON, whose prerequisites are
  3755. * slightly different). It also specifies what exception to inject otherwise.
  3756. */
  3757. static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
  3758. {
  3759. struct kvm_segment cs;
  3760. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3761. if (!vmx->nested.vmxon) {
  3762. kvm_queue_exception(vcpu, UD_VECTOR);
  3763. return 0;
  3764. }
  3765. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  3766. if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) ||
  3767. (is_long_mode(vcpu) && !cs.l)) {
  3768. kvm_queue_exception(vcpu, UD_VECTOR);
  3769. return 0;
  3770. }
  3771. if (vmx_get_cpl(vcpu)) {
  3772. kvm_inject_gp(vcpu, 0);
  3773. return 0;
  3774. }
  3775. return 1;
  3776. }
  3777. /*
  3778. * Free whatever needs to be freed from vmx->nested when L1 goes down, or
  3779. * just stops using VMX.
  3780. */
  3781. static void free_nested(struct vcpu_vmx *vmx)
  3782. {
  3783. if (!vmx->nested.vmxon)
  3784. return;
  3785. vmx->nested.vmxon = false;
  3786. if (vmx->nested.current_vmptr != -1ull) {
  3787. kunmap(vmx->nested.current_vmcs12_page);
  3788. nested_release_page(vmx->nested.current_vmcs12_page);
  3789. vmx->nested.current_vmptr = -1ull;
  3790. vmx->nested.current_vmcs12 = NULL;
  3791. }
  3792. nested_free_all_saved_vmcss(vmx);
  3793. }
  3794. /* Emulate the VMXOFF instruction */
  3795. static int handle_vmoff(struct kvm_vcpu *vcpu)
  3796. {
  3797. if (!nested_vmx_check_permission(vcpu))
  3798. return 1;
  3799. free_nested(to_vmx(vcpu));
  3800. skip_emulated_instruction(vcpu);
  3801. return 1;
  3802. }
  3803. /*
  3804. * Decode the memory-address operand of a vmx instruction, as recorded on an
  3805. * exit caused by such an instruction (run by a guest hypervisor).
  3806. * On success, returns 0. When the operand is invalid, returns 1 and throws
  3807. * #UD or #GP.
  3808. */
  3809. static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
  3810. unsigned long exit_qualification,
  3811. u32 vmx_instruction_info, gva_t *ret)
  3812. {
  3813. /*
  3814. * According to Vol. 3B, "Information for VM Exits Due to Instruction
  3815. * Execution", on an exit, vmx_instruction_info holds most of the
  3816. * addressing components of the operand. Only the displacement part
  3817. * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
  3818. * For how an actual address is calculated from all these components,
  3819. * refer to Vol. 1, "Operand Addressing".
  3820. */
  3821. int scaling = vmx_instruction_info & 3;
  3822. int addr_size = (vmx_instruction_info >> 7) & 7;
  3823. bool is_reg = vmx_instruction_info & (1u << 10);
  3824. int seg_reg = (vmx_instruction_info >> 15) & 7;
  3825. int index_reg = (vmx_instruction_info >> 18) & 0xf;
  3826. bool index_is_valid = !(vmx_instruction_info & (1u << 22));
  3827. int base_reg = (vmx_instruction_info >> 23) & 0xf;
  3828. bool base_is_valid = !(vmx_instruction_info & (1u << 27));
  3829. if (is_reg) {
  3830. kvm_queue_exception(vcpu, UD_VECTOR);
  3831. return 1;
  3832. }
  3833. /* Addr = segment_base + offset */
  3834. /* offset = base + [index * scale] + displacement */
  3835. *ret = vmx_get_segment_base(vcpu, seg_reg);
  3836. if (base_is_valid)
  3837. *ret += kvm_register_read(vcpu, base_reg);
  3838. if (index_is_valid)
  3839. *ret += kvm_register_read(vcpu, index_reg)<<scaling;
  3840. *ret += exit_qualification; /* holds the displacement */
  3841. if (addr_size == 1) /* 32 bit */
  3842. *ret &= 0xffffffff;
  3843. /*
  3844. * TODO: throw #GP (and return 1) in various cases that the VM*
  3845. * instructions require it - e.g., offset beyond segment limit,
  3846. * unusable or unreadable/unwritable segment, non-canonical 64-bit
  3847. * address, and so on. Currently these are not checked.
  3848. */
  3849. return 0;
  3850. }
  3851. /*
  3852. * The exit handlers return 1 if the exit was handled fully and guest execution
  3853. * may resume. Otherwise they set the kvm_run parameter to indicate what needs
  3854. * to be done to userspace and return 0.
  3855. */
  3856. static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
  3857. [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
  3858. [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
  3859. [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
  3860. [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
  3861. [EXIT_REASON_IO_INSTRUCTION] = handle_io,
  3862. [EXIT_REASON_CR_ACCESS] = handle_cr,
  3863. [EXIT_REASON_DR_ACCESS] = handle_dr,
  3864. [EXIT_REASON_CPUID] = handle_cpuid,
  3865. [EXIT_REASON_MSR_READ] = handle_rdmsr,
  3866. [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
  3867. [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
  3868. [EXIT_REASON_HLT] = handle_halt,
  3869. [EXIT_REASON_INVD] = handle_invd,
  3870. [EXIT_REASON_INVLPG] = handle_invlpg,
  3871. [EXIT_REASON_VMCALL] = handle_vmcall,
  3872. [EXIT_REASON_VMCLEAR] = handle_vmx_insn,
  3873. [EXIT_REASON_VMLAUNCH] = handle_vmx_insn,
  3874. [EXIT_REASON_VMPTRLD] = handle_vmx_insn,
  3875. [EXIT_REASON_VMPTRST] = handle_vmx_insn,
  3876. [EXIT_REASON_VMREAD] = handle_vmx_insn,
  3877. [EXIT_REASON_VMRESUME] = handle_vmx_insn,
  3878. [EXIT_REASON_VMWRITE] = handle_vmx_insn,
  3879. [EXIT_REASON_VMOFF] = handle_vmoff,
  3880. [EXIT_REASON_VMON] = handle_vmon,
  3881. [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
  3882. [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
  3883. [EXIT_REASON_WBINVD] = handle_wbinvd,
  3884. [EXIT_REASON_XSETBV] = handle_xsetbv,
  3885. [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
  3886. [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
  3887. [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
  3888. [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
  3889. [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
  3890. [EXIT_REASON_MWAIT_INSTRUCTION] = handle_invalid_op,
  3891. [EXIT_REASON_MONITOR_INSTRUCTION] = handle_invalid_op,
  3892. };
  3893. static const int kvm_vmx_max_exit_handlers =
  3894. ARRAY_SIZE(kvm_vmx_exit_handlers);
  3895. static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
  3896. {
  3897. *info1 = vmcs_readl(EXIT_QUALIFICATION);
  3898. *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
  3899. }
  3900. /*
  3901. * The guest has exited. See if we can fix it or if we need userspace
  3902. * assistance.
  3903. */
  3904. static int vmx_handle_exit(struct kvm_vcpu *vcpu)
  3905. {
  3906. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3907. u32 exit_reason = vmx->exit_reason;
  3908. u32 vectoring_info = vmx->idt_vectoring_info;
  3909. trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
  3910. /* If guest state is invalid, start emulating */
  3911. if (vmx->emulation_required && emulate_invalid_guest_state)
  3912. return handle_invalid_guest_state(vcpu);
  3913. if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
  3914. vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  3915. vcpu->run->fail_entry.hardware_entry_failure_reason
  3916. = exit_reason;
  3917. return 0;
  3918. }
  3919. if (unlikely(vmx->fail)) {
  3920. vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  3921. vcpu->run->fail_entry.hardware_entry_failure_reason
  3922. = vmcs_read32(VM_INSTRUCTION_ERROR);
  3923. return 0;
  3924. }
  3925. if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
  3926. (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
  3927. exit_reason != EXIT_REASON_EPT_VIOLATION &&
  3928. exit_reason != EXIT_REASON_TASK_SWITCH))
  3929. printk(KERN_WARNING "%s: unexpected, valid vectoring info "
  3930. "(0x%x) and exit reason is 0x%x\n",
  3931. __func__, vectoring_info, exit_reason);
  3932. if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) {
  3933. if (vmx_interrupt_allowed(vcpu)) {
  3934. vmx->soft_vnmi_blocked = 0;
  3935. } else if (vmx->vnmi_blocked_time > 1000000000LL &&
  3936. vcpu->arch.nmi_pending) {
  3937. /*
  3938. * This CPU don't support us in finding the end of an
  3939. * NMI-blocked window if the guest runs with IRQs
  3940. * disabled. So we pull the trigger after 1 s of
  3941. * futile waiting, but inform the user about this.
  3942. */
  3943. printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
  3944. "state on VCPU %d after 1 s timeout\n",
  3945. __func__, vcpu->vcpu_id);
  3946. vmx->soft_vnmi_blocked = 0;
  3947. }
  3948. }
  3949. if (exit_reason < kvm_vmx_max_exit_handlers
  3950. && kvm_vmx_exit_handlers[exit_reason])
  3951. return kvm_vmx_exit_handlers[exit_reason](vcpu);
  3952. else {
  3953. vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
  3954. vcpu->run->hw.hardware_exit_reason = exit_reason;
  3955. }
  3956. return 0;
  3957. }
  3958. static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
  3959. {
  3960. if (irr == -1 || tpr < irr) {
  3961. vmcs_write32(TPR_THRESHOLD, 0);
  3962. return;
  3963. }
  3964. vmcs_write32(TPR_THRESHOLD, irr);
  3965. }
  3966. static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
  3967. {
  3968. u32 exit_intr_info;
  3969. if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
  3970. || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
  3971. return;
  3972. vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  3973. exit_intr_info = vmx->exit_intr_info;
  3974. /* Handle machine checks before interrupts are enabled */
  3975. if (is_machine_check(exit_intr_info))
  3976. kvm_machine_check();
  3977. /* We need to handle NMIs before interrupts are enabled */
  3978. if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
  3979. (exit_intr_info & INTR_INFO_VALID_MASK)) {
  3980. kvm_before_handle_nmi(&vmx->vcpu);
  3981. asm("int $2");
  3982. kvm_after_handle_nmi(&vmx->vcpu);
  3983. }
  3984. }
  3985. static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
  3986. {
  3987. u32 exit_intr_info;
  3988. bool unblock_nmi;
  3989. u8 vector;
  3990. bool idtv_info_valid;
  3991. idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
  3992. if (cpu_has_virtual_nmis()) {
  3993. if (vmx->nmi_known_unmasked)
  3994. return;
  3995. /*
  3996. * Can't use vmx->exit_intr_info since we're not sure what
  3997. * the exit reason is.
  3998. */
  3999. exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  4000. unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
  4001. vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
  4002. /*
  4003. * SDM 3: 27.7.1.2 (September 2008)
  4004. * Re-set bit "block by NMI" before VM entry if vmexit caused by
  4005. * a guest IRET fault.
  4006. * SDM 3: 23.2.2 (September 2008)
  4007. * Bit 12 is undefined in any of the following cases:
  4008. * If the VM exit sets the valid bit in the IDT-vectoring
  4009. * information field.
  4010. * If the VM exit is due to a double fault.
  4011. */
  4012. if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
  4013. vector != DF_VECTOR && !idtv_info_valid)
  4014. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  4015. GUEST_INTR_STATE_NMI);
  4016. else
  4017. vmx->nmi_known_unmasked =
  4018. !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
  4019. & GUEST_INTR_STATE_NMI);
  4020. } else if (unlikely(vmx->soft_vnmi_blocked))
  4021. vmx->vnmi_blocked_time +=
  4022. ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
  4023. }
  4024. static void __vmx_complete_interrupts(struct vcpu_vmx *vmx,
  4025. u32 idt_vectoring_info,
  4026. int instr_len_field,
  4027. int error_code_field)
  4028. {
  4029. u8 vector;
  4030. int type;
  4031. bool idtv_info_valid;
  4032. idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
  4033. vmx->vcpu.arch.nmi_injected = false;
  4034. kvm_clear_exception_queue(&vmx->vcpu);
  4035. kvm_clear_interrupt_queue(&vmx->vcpu);
  4036. if (!idtv_info_valid)
  4037. return;
  4038. kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
  4039. vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
  4040. type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
  4041. switch (type) {
  4042. case INTR_TYPE_NMI_INTR:
  4043. vmx->vcpu.arch.nmi_injected = true;
  4044. /*
  4045. * SDM 3: 27.7.1.2 (September 2008)
  4046. * Clear bit "block by NMI" before VM entry if a NMI
  4047. * delivery faulted.
  4048. */
  4049. vmx_set_nmi_mask(&vmx->vcpu, false);
  4050. break;
  4051. case INTR_TYPE_SOFT_EXCEPTION:
  4052. vmx->vcpu.arch.event_exit_inst_len =
  4053. vmcs_read32(instr_len_field);
  4054. /* fall through */
  4055. case INTR_TYPE_HARD_EXCEPTION:
  4056. if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
  4057. u32 err = vmcs_read32(error_code_field);
  4058. kvm_queue_exception_e(&vmx->vcpu, vector, err);
  4059. } else
  4060. kvm_queue_exception(&vmx->vcpu, vector);
  4061. break;
  4062. case INTR_TYPE_SOFT_INTR:
  4063. vmx->vcpu.arch.event_exit_inst_len =
  4064. vmcs_read32(instr_len_field);
  4065. /* fall through */
  4066. case INTR_TYPE_EXT_INTR:
  4067. kvm_queue_interrupt(&vmx->vcpu, vector,
  4068. type == INTR_TYPE_SOFT_INTR);
  4069. break;
  4070. default:
  4071. break;
  4072. }
  4073. }
  4074. static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
  4075. {
  4076. __vmx_complete_interrupts(vmx, vmx->idt_vectoring_info,
  4077. VM_EXIT_INSTRUCTION_LEN,
  4078. IDT_VECTORING_ERROR_CODE);
  4079. }
  4080. static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
  4081. {
  4082. __vmx_complete_interrupts(to_vmx(vcpu),
  4083. vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
  4084. VM_ENTRY_INSTRUCTION_LEN,
  4085. VM_ENTRY_EXCEPTION_ERROR_CODE);
  4086. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
  4087. }
  4088. #ifdef CONFIG_X86_64
  4089. #define R "r"
  4090. #define Q "q"
  4091. #else
  4092. #define R "e"
  4093. #define Q "l"
  4094. #endif
  4095. static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
  4096. {
  4097. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4098. /* Record the guest's net vcpu time for enforced NMI injections. */
  4099. if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
  4100. vmx->entry_time = ktime_get();
  4101. /* Don't enter VMX if guest state is invalid, let the exit handler
  4102. start emulation until we arrive back to a valid state */
  4103. if (vmx->emulation_required && emulate_invalid_guest_state)
  4104. return;
  4105. if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
  4106. vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
  4107. if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
  4108. vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
  4109. /* When single-stepping over STI and MOV SS, we must clear the
  4110. * corresponding interruptibility bits in the guest state. Otherwise
  4111. * vmentry fails as it then expects bit 14 (BS) in pending debug
  4112. * exceptions being set, but that's not correct for the guest debugging
  4113. * case. */
  4114. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
  4115. vmx_set_interrupt_shadow(vcpu, 0);
  4116. vmx->__launched = vmx->loaded_vmcs->launched;
  4117. asm(
  4118. /* Store host registers */
  4119. "push %%"R"dx; push %%"R"bp;"
  4120. "push %%"R"cx \n\t" /* placeholder for guest rcx */
  4121. "push %%"R"cx \n\t"
  4122. "cmp %%"R"sp, %c[host_rsp](%0) \n\t"
  4123. "je 1f \n\t"
  4124. "mov %%"R"sp, %c[host_rsp](%0) \n\t"
  4125. __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
  4126. "1: \n\t"
  4127. /* Reload cr2 if changed */
  4128. "mov %c[cr2](%0), %%"R"ax \n\t"
  4129. "mov %%cr2, %%"R"dx \n\t"
  4130. "cmp %%"R"ax, %%"R"dx \n\t"
  4131. "je 2f \n\t"
  4132. "mov %%"R"ax, %%cr2 \n\t"
  4133. "2: \n\t"
  4134. /* Check if vmlaunch of vmresume is needed */
  4135. "cmpl $0, %c[launched](%0) \n\t"
  4136. /* Load guest registers. Don't clobber flags. */
  4137. "mov %c[rax](%0), %%"R"ax \n\t"
  4138. "mov %c[rbx](%0), %%"R"bx \n\t"
  4139. "mov %c[rdx](%0), %%"R"dx \n\t"
  4140. "mov %c[rsi](%0), %%"R"si \n\t"
  4141. "mov %c[rdi](%0), %%"R"di \n\t"
  4142. "mov %c[rbp](%0), %%"R"bp \n\t"
  4143. #ifdef CONFIG_X86_64
  4144. "mov %c[r8](%0), %%r8 \n\t"
  4145. "mov %c[r9](%0), %%r9 \n\t"
  4146. "mov %c[r10](%0), %%r10 \n\t"
  4147. "mov %c[r11](%0), %%r11 \n\t"
  4148. "mov %c[r12](%0), %%r12 \n\t"
  4149. "mov %c[r13](%0), %%r13 \n\t"
  4150. "mov %c[r14](%0), %%r14 \n\t"
  4151. "mov %c[r15](%0), %%r15 \n\t"
  4152. #endif
  4153. "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */
  4154. /* Enter guest mode */
  4155. "jne .Llaunched \n\t"
  4156. __ex(ASM_VMX_VMLAUNCH) "\n\t"
  4157. "jmp .Lkvm_vmx_return \n\t"
  4158. ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t"
  4159. ".Lkvm_vmx_return: "
  4160. /* Save guest registers, load host registers, keep flags */
  4161. "mov %0, %c[wordsize](%%"R"sp) \n\t"
  4162. "pop %0 \n\t"
  4163. "mov %%"R"ax, %c[rax](%0) \n\t"
  4164. "mov %%"R"bx, %c[rbx](%0) \n\t"
  4165. "pop"Q" %c[rcx](%0) \n\t"
  4166. "mov %%"R"dx, %c[rdx](%0) \n\t"
  4167. "mov %%"R"si, %c[rsi](%0) \n\t"
  4168. "mov %%"R"di, %c[rdi](%0) \n\t"
  4169. "mov %%"R"bp, %c[rbp](%0) \n\t"
  4170. #ifdef CONFIG_X86_64
  4171. "mov %%r8, %c[r8](%0) \n\t"
  4172. "mov %%r9, %c[r9](%0) \n\t"
  4173. "mov %%r10, %c[r10](%0) \n\t"
  4174. "mov %%r11, %c[r11](%0) \n\t"
  4175. "mov %%r12, %c[r12](%0) \n\t"
  4176. "mov %%r13, %c[r13](%0) \n\t"
  4177. "mov %%r14, %c[r14](%0) \n\t"
  4178. "mov %%r15, %c[r15](%0) \n\t"
  4179. #endif
  4180. "mov %%cr2, %%"R"ax \n\t"
  4181. "mov %%"R"ax, %c[cr2](%0) \n\t"
  4182. "pop %%"R"bp; pop %%"R"dx \n\t"
  4183. "setbe %c[fail](%0) \n\t"
  4184. : : "c"(vmx), "d"((unsigned long)HOST_RSP),
  4185. [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
  4186. [fail]"i"(offsetof(struct vcpu_vmx, fail)),
  4187. [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
  4188. [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
  4189. [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
  4190. [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
  4191. [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
  4192. [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
  4193. [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
  4194. [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
  4195. #ifdef CONFIG_X86_64
  4196. [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
  4197. [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
  4198. [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
  4199. [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
  4200. [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
  4201. [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
  4202. [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
  4203. [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
  4204. #endif
  4205. [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
  4206. [wordsize]"i"(sizeof(ulong))
  4207. : "cc", "memory"
  4208. , R"ax", R"bx", R"di", R"si"
  4209. #ifdef CONFIG_X86_64
  4210. , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
  4211. #endif
  4212. );
  4213. vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
  4214. | (1 << VCPU_EXREG_RFLAGS)
  4215. | (1 << VCPU_EXREG_CPL)
  4216. | (1 << VCPU_EXREG_PDPTR)
  4217. | (1 << VCPU_EXREG_SEGMENTS)
  4218. | (1 << VCPU_EXREG_CR3));
  4219. vcpu->arch.regs_dirty = 0;
  4220. vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
  4221. asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
  4222. vmx->loaded_vmcs->launched = 1;
  4223. vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
  4224. vmx_complete_atomic_exit(vmx);
  4225. vmx_recover_nmi_blocking(vmx);
  4226. vmx_complete_interrupts(vmx);
  4227. }
  4228. #undef R
  4229. #undef Q
  4230. static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
  4231. {
  4232. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4233. free_vpid(vmx);
  4234. free_nested(vmx);
  4235. free_loaded_vmcs(vmx->loaded_vmcs);
  4236. kfree(vmx->guest_msrs);
  4237. kvm_vcpu_uninit(vcpu);
  4238. kmem_cache_free(kvm_vcpu_cache, vmx);
  4239. }
  4240. static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
  4241. {
  4242. int err;
  4243. struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  4244. int cpu;
  4245. if (!vmx)
  4246. return ERR_PTR(-ENOMEM);
  4247. allocate_vpid(vmx);
  4248. err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
  4249. if (err)
  4250. goto free_vcpu;
  4251. vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
  4252. err = -ENOMEM;
  4253. if (!vmx->guest_msrs) {
  4254. goto uninit_vcpu;
  4255. }
  4256. vmx->loaded_vmcs = &vmx->vmcs01;
  4257. vmx->loaded_vmcs->vmcs = alloc_vmcs();
  4258. if (!vmx->loaded_vmcs->vmcs)
  4259. goto free_msrs;
  4260. if (!vmm_exclusive)
  4261. kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id())));
  4262. loaded_vmcs_init(vmx->loaded_vmcs);
  4263. if (!vmm_exclusive)
  4264. kvm_cpu_vmxoff();
  4265. cpu = get_cpu();
  4266. vmx_vcpu_load(&vmx->vcpu, cpu);
  4267. vmx->vcpu.cpu = cpu;
  4268. err = vmx_vcpu_setup(vmx);
  4269. vmx_vcpu_put(&vmx->vcpu);
  4270. put_cpu();
  4271. if (err)
  4272. goto free_vmcs;
  4273. if (vm_need_virtualize_apic_accesses(kvm))
  4274. err = alloc_apic_access_page(kvm);
  4275. if (err)
  4276. goto free_vmcs;
  4277. if (enable_ept) {
  4278. if (!kvm->arch.ept_identity_map_addr)
  4279. kvm->arch.ept_identity_map_addr =
  4280. VMX_EPT_IDENTITY_PAGETABLE_ADDR;
  4281. err = -ENOMEM;
  4282. if (alloc_identity_pagetable(kvm) != 0)
  4283. goto free_vmcs;
  4284. if (!init_rmode_identity_map(kvm))
  4285. goto free_vmcs;
  4286. }
  4287. vmx->nested.current_vmptr = -1ull;
  4288. vmx->nested.current_vmcs12 = NULL;
  4289. return &vmx->vcpu;
  4290. free_vmcs:
  4291. free_vmcs(vmx->loaded_vmcs->vmcs);
  4292. free_msrs:
  4293. kfree(vmx->guest_msrs);
  4294. uninit_vcpu:
  4295. kvm_vcpu_uninit(&vmx->vcpu);
  4296. free_vcpu:
  4297. free_vpid(vmx);
  4298. kmem_cache_free(kvm_vcpu_cache, vmx);
  4299. return ERR_PTR(err);
  4300. }
  4301. static void __init vmx_check_processor_compat(void *rtn)
  4302. {
  4303. struct vmcs_config vmcs_conf;
  4304. *(int *)rtn = 0;
  4305. if (setup_vmcs_config(&vmcs_conf) < 0)
  4306. *(int *)rtn = -EIO;
  4307. if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
  4308. printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
  4309. smp_processor_id());
  4310. *(int *)rtn = -EIO;
  4311. }
  4312. }
  4313. static int get_ept_level(void)
  4314. {
  4315. return VMX_EPT_DEFAULT_GAW + 1;
  4316. }
  4317. static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
  4318. {
  4319. u64 ret;
  4320. /* For VT-d and EPT combination
  4321. * 1. MMIO: always map as UC
  4322. * 2. EPT with VT-d:
  4323. * a. VT-d without snooping control feature: can't guarantee the
  4324. * result, try to trust guest.
  4325. * b. VT-d with snooping control feature: snooping control feature of
  4326. * VT-d engine can guarantee the cache correctness. Just set it
  4327. * to WB to keep consistent with host. So the same as item 3.
  4328. * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
  4329. * consistent with host MTRR
  4330. */
  4331. if (is_mmio)
  4332. ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
  4333. else if (vcpu->kvm->arch.iommu_domain &&
  4334. !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY))
  4335. ret = kvm_get_guest_memory_type(vcpu, gfn) <<
  4336. VMX_EPT_MT_EPTE_SHIFT;
  4337. else
  4338. ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
  4339. | VMX_EPT_IPAT_BIT;
  4340. return ret;
  4341. }
  4342. #define _ER(x) { EXIT_REASON_##x, #x }
  4343. static const struct trace_print_flags vmx_exit_reasons_str[] = {
  4344. _ER(EXCEPTION_NMI),
  4345. _ER(EXTERNAL_INTERRUPT),
  4346. _ER(TRIPLE_FAULT),
  4347. _ER(PENDING_INTERRUPT),
  4348. _ER(NMI_WINDOW),
  4349. _ER(TASK_SWITCH),
  4350. _ER(CPUID),
  4351. _ER(HLT),
  4352. _ER(INVLPG),
  4353. _ER(RDPMC),
  4354. _ER(RDTSC),
  4355. _ER(VMCALL),
  4356. _ER(VMCLEAR),
  4357. _ER(VMLAUNCH),
  4358. _ER(VMPTRLD),
  4359. _ER(VMPTRST),
  4360. _ER(VMREAD),
  4361. _ER(VMRESUME),
  4362. _ER(VMWRITE),
  4363. _ER(VMOFF),
  4364. _ER(VMON),
  4365. _ER(CR_ACCESS),
  4366. _ER(DR_ACCESS),
  4367. _ER(IO_INSTRUCTION),
  4368. _ER(MSR_READ),
  4369. _ER(MSR_WRITE),
  4370. _ER(MWAIT_INSTRUCTION),
  4371. _ER(MONITOR_INSTRUCTION),
  4372. _ER(PAUSE_INSTRUCTION),
  4373. _ER(MCE_DURING_VMENTRY),
  4374. _ER(TPR_BELOW_THRESHOLD),
  4375. _ER(APIC_ACCESS),
  4376. _ER(EPT_VIOLATION),
  4377. _ER(EPT_MISCONFIG),
  4378. _ER(WBINVD),
  4379. { -1, NULL }
  4380. };
  4381. #undef _ER
  4382. static int vmx_get_lpage_level(void)
  4383. {
  4384. if (enable_ept && !cpu_has_vmx_ept_1g_page())
  4385. return PT_DIRECTORY_LEVEL;
  4386. else
  4387. /* For shadow and EPT supported 1GB page */
  4388. return PT_PDPE_LEVEL;
  4389. }
  4390. static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
  4391. {
  4392. struct kvm_cpuid_entry2 *best;
  4393. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4394. u32 exec_control;
  4395. vmx->rdtscp_enabled = false;
  4396. if (vmx_rdtscp_supported()) {
  4397. exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  4398. if (exec_control & SECONDARY_EXEC_RDTSCP) {
  4399. best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
  4400. if (best && (best->edx & bit(X86_FEATURE_RDTSCP)))
  4401. vmx->rdtscp_enabled = true;
  4402. else {
  4403. exec_control &= ~SECONDARY_EXEC_RDTSCP;
  4404. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  4405. exec_control);
  4406. }
  4407. }
  4408. }
  4409. }
  4410. static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
  4411. {
  4412. }
  4413. static int vmx_check_intercept(struct kvm_vcpu *vcpu,
  4414. struct x86_instruction_info *info,
  4415. enum x86_intercept_stage stage)
  4416. {
  4417. return X86EMUL_CONTINUE;
  4418. }
  4419. static struct kvm_x86_ops vmx_x86_ops = {
  4420. .cpu_has_kvm_support = cpu_has_kvm_support,
  4421. .disabled_by_bios = vmx_disabled_by_bios,
  4422. .hardware_setup = hardware_setup,
  4423. .hardware_unsetup = hardware_unsetup,
  4424. .check_processor_compatibility = vmx_check_processor_compat,
  4425. .hardware_enable = hardware_enable,
  4426. .hardware_disable = hardware_disable,
  4427. .cpu_has_accelerated_tpr = report_flexpriority,
  4428. .vcpu_create = vmx_create_vcpu,
  4429. .vcpu_free = vmx_free_vcpu,
  4430. .vcpu_reset = vmx_vcpu_reset,
  4431. .prepare_guest_switch = vmx_save_host_state,
  4432. .vcpu_load = vmx_vcpu_load,
  4433. .vcpu_put = vmx_vcpu_put,
  4434. .set_guest_debug = set_guest_debug,
  4435. .get_msr = vmx_get_msr,
  4436. .set_msr = vmx_set_msr,
  4437. .get_segment_base = vmx_get_segment_base,
  4438. .get_segment = vmx_get_segment,
  4439. .set_segment = vmx_set_segment,
  4440. .get_cpl = vmx_get_cpl,
  4441. .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
  4442. .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
  4443. .decache_cr3 = vmx_decache_cr3,
  4444. .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
  4445. .set_cr0 = vmx_set_cr0,
  4446. .set_cr3 = vmx_set_cr3,
  4447. .set_cr4 = vmx_set_cr4,
  4448. .set_efer = vmx_set_efer,
  4449. .get_idt = vmx_get_idt,
  4450. .set_idt = vmx_set_idt,
  4451. .get_gdt = vmx_get_gdt,
  4452. .set_gdt = vmx_set_gdt,
  4453. .set_dr7 = vmx_set_dr7,
  4454. .cache_reg = vmx_cache_reg,
  4455. .get_rflags = vmx_get_rflags,
  4456. .set_rflags = vmx_set_rflags,
  4457. .fpu_activate = vmx_fpu_activate,
  4458. .fpu_deactivate = vmx_fpu_deactivate,
  4459. .tlb_flush = vmx_flush_tlb,
  4460. .run = vmx_vcpu_run,
  4461. .handle_exit = vmx_handle_exit,
  4462. .skip_emulated_instruction = skip_emulated_instruction,
  4463. .set_interrupt_shadow = vmx_set_interrupt_shadow,
  4464. .get_interrupt_shadow = vmx_get_interrupt_shadow,
  4465. .patch_hypercall = vmx_patch_hypercall,
  4466. .set_irq = vmx_inject_irq,
  4467. .set_nmi = vmx_inject_nmi,
  4468. .queue_exception = vmx_queue_exception,
  4469. .cancel_injection = vmx_cancel_injection,
  4470. .interrupt_allowed = vmx_interrupt_allowed,
  4471. .nmi_allowed = vmx_nmi_allowed,
  4472. .get_nmi_mask = vmx_get_nmi_mask,
  4473. .set_nmi_mask = vmx_set_nmi_mask,
  4474. .enable_nmi_window = enable_nmi_window,
  4475. .enable_irq_window = enable_irq_window,
  4476. .update_cr8_intercept = update_cr8_intercept,
  4477. .set_tss_addr = vmx_set_tss_addr,
  4478. .get_tdp_level = get_ept_level,
  4479. .get_mt_mask = vmx_get_mt_mask,
  4480. .get_exit_info = vmx_get_exit_info,
  4481. .exit_reasons_str = vmx_exit_reasons_str,
  4482. .get_lpage_level = vmx_get_lpage_level,
  4483. .cpuid_update = vmx_cpuid_update,
  4484. .rdtscp_supported = vmx_rdtscp_supported,
  4485. .set_supported_cpuid = vmx_set_supported_cpuid,
  4486. .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
  4487. .set_tsc_khz = vmx_set_tsc_khz,
  4488. .write_tsc_offset = vmx_write_tsc_offset,
  4489. .adjust_tsc_offset = vmx_adjust_tsc_offset,
  4490. .compute_tsc_offset = vmx_compute_tsc_offset,
  4491. .set_tdp_cr3 = vmx_set_cr3,
  4492. .check_intercept = vmx_check_intercept,
  4493. };
  4494. static int __init vmx_init(void)
  4495. {
  4496. int r, i;
  4497. rdmsrl_safe(MSR_EFER, &host_efer);
  4498. for (i = 0; i < NR_VMX_MSR; ++i)
  4499. kvm_define_shared_msr(i, vmx_msr_index[i]);
  4500. vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
  4501. if (!vmx_io_bitmap_a)
  4502. return -ENOMEM;
  4503. vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
  4504. if (!vmx_io_bitmap_b) {
  4505. r = -ENOMEM;
  4506. goto out;
  4507. }
  4508. vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
  4509. if (!vmx_msr_bitmap_legacy) {
  4510. r = -ENOMEM;
  4511. goto out1;
  4512. }
  4513. vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
  4514. if (!vmx_msr_bitmap_longmode) {
  4515. r = -ENOMEM;
  4516. goto out2;
  4517. }
  4518. /*
  4519. * Allow direct access to the PC debug port (it is often used for I/O
  4520. * delays, but the vmexits simply slow things down).
  4521. */
  4522. memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
  4523. clear_bit(0x80, vmx_io_bitmap_a);
  4524. memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
  4525. memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
  4526. memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
  4527. set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
  4528. r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
  4529. __alignof__(struct vcpu_vmx), THIS_MODULE);
  4530. if (r)
  4531. goto out3;
  4532. vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
  4533. vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
  4534. vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
  4535. vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
  4536. vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
  4537. vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
  4538. if (enable_ept) {
  4539. bypass_guest_pf = 0;
  4540. kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
  4541. VMX_EPT_EXECUTABLE_MASK);
  4542. kvm_enable_tdp();
  4543. } else
  4544. kvm_disable_tdp();
  4545. if (bypass_guest_pf)
  4546. kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
  4547. return 0;
  4548. out3:
  4549. free_page((unsigned long)vmx_msr_bitmap_longmode);
  4550. out2:
  4551. free_page((unsigned long)vmx_msr_bitmap_legacy);
  4552. out1:
  4553. free_page((unsigned long)vmx_io_bitmap_b);
  4554. out:
  4555. free_page((unsigned long)vmx_io_bitmap_a);
  4556. return r;
  4557. }
  4558. static void __exit vmx_exit(void)
  4559. {
  4560. free_page((unsigned long)vmx_msr_bitmap_legacy);
  4561. free_page((unsigned long)vmx_msr_bitmap_longmode);
  4562. free_page((unsigned long)vmx_io_bitmap_b);
  4563. free_page((unsigned long)vmx_io_bitmap_a);
  4564. kvm_exit();
  4565. }
  4566. module_init(vmx_init)
  4567. module_exit(vmx_exit)