vmx.c 263 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326
  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 "cpuid.h"
  21. #include <linux/kvm_host.h>
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/mm.h>
  25. #include <linux/highmem.h>
  26. #include <linux/sched.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/mod_devicetable.h>
  29. #include <linux/ftrace_event.h>
  30. #include <linux/slab.h>
  31. #include <linux/tboot.h>
  32. #include <linux/hrtimer.h>
  33. #include "kvm_cache_regs.h"
  34. #include "x86.h"
  35. #include <asm/io.h>
  36. #include <asm/desc.h>
  37. #include <asm/vmx.h>
  38. #include <asm/virtext.h>
  39. #include <asm/mce.h>
  40. #include <asm/i387.h>
  41. #include <asm/xcr.h>
  42. #include <asm/perf_event.h>
  43. #include <asm/debugreg.h>
  44. #include <asm/kexec.h>
  45. #include "trace.h"
  46. #define __ex(x) __kvm_handle_fault_on_reboot(x)
  47. #define __ex_clear(x, reg) \
  48. ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
  49. MODULE_AUTHOR("Qumranet");
  50. MODULE_LICENSE("GPL");
  51. static const struct x86_cpu_id vmx_cpu_id[] = {
  52. X86_FEATURE_MATCH(X86_FEATURE_VMX),
  53. {}
  54. };
  55. MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
  56. static bool __read_mostly enable_vpid = 1;
  57. module_param_named(vpid, enable_vpid, bool, 0444);
  58. static bool __read_mostly flexpriority_enabled = 1;
  59. module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
  60. static bool __read_mostly enable_ept = 1;
  61. module_param_named(ept, enable_ept, bool, S_IRUGO);
  62. static bool __read_mostly enable_unrestricted_guest = 1;
  63. module_param_named(unrestricted_guest,
  64. enable_unrestricted_guest, bool, S_IRUGO);
  65. static bool __read_mostly enable_ept_ad_bits = 1;
  66. module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
  67. static bool __read_mostly emulate_invalid_guest_state = true;
  68. module_param(emulate_invalid_guest_state, bool, S_IRUGO);
  69. static bool __read_mostly vmm_exclusive = 1;
  70. module_param(vmm_exclusive, bool, S_IRUGO);
  71. static bool __read_mostly fasteoi = 1;
  72. module_param(fasteoi, bool, S_IRUGO);
  73. static bool __read_mostly enable_apicv = 1;
  74. module_param(enable_apicv, bool, S_IRUGO);
  75. static bool __read_mostly enable_shadow_vmcs = 1;
  76. module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
  77. /*
  78. * If nested=1, nested virtualization is supported, i.e., guests may use
  79. * VMX and be a hypervisor for its own guests. If nested=0, guests may not
  80. * use VMX instructions.
  81. */
  82. static bool __read_mostly nested = 0;
  83. module_param(nested, bool, S_IRUGO);
  84. #define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
  85. #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE)
  86. #define KVM_VM_CR0_ALWAYS_ON \
  87. (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
  88. #define KVM_CR4_GUEST_OWNED_BITS \
  89. (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
  90. | X86_CR4_OSXMMEXCPT)
  91. #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
  92. #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
  93. #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
  94. #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
  95. /*
  96. * These 2 parameters are used to config the controls for Pause-Loop Exiting:
  97. * ple_gap: upper bound on the amount of time between two successive
  98. * executions of PAUSE in a loop. Also indicate if ple enabled.
  99. * According to test, this time is usually smaller than 128 cycles.
  100. * ple_window: upper bound on the amount of time a guest is allowed to execute
  101. * in a PAUSE loop. Tests indicate that most spinlocks are held for
  102. * less than 2^12 cycles
  103. * Time is measured based on a counter that runs at the same rate as the TSC,
  104. * refer SDM volume 3b section 21.6.13 & 22.1.3.
  105. */
  106. #define KVM_VMX_DEFAULT_PLE_GAP 128
  107. #define KVM_VMX_DEFAULT_PLE_WINDOW 4096
  108. #define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2
  109. #define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0
  110. #define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \
  111. INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW
  112. static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
  113. module_param(ple_gap, int, S_IRUGO);
  114. static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
  115. module_param(ple_window, int, S_IRUGO);
  116. /* Default doubles per-vcpu window every exit. */
  117. static int ple_window_grow = KVM_VMX_DEFAULT_PLE_WINDOW_GROW;
  118. module_param(ple_window_grow, int, S_IRUGO);
  119. /* Default resets per-vcpu window every exit to ple_window. */
  120. static int ple_window_shrink = KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK;
  121. module_param(ple_window_shrink, int, S_IRUGO);
  122. /* Default is to compute the maximum so we can never overflow. */
  123. static int ple_window_actual_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
  124. static int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
  125. module_param(ple_window_max, int, S_IRUGO);
  126. extern const ulong vmx_return;
  127. #define NR_AUTOLOAD_MSRS 8
  128. #define VMCS02_POOL_SIZE 1
  129. struct vmcs {
  130. u32 revision_id;
  131. u32 abort;
  132. char data[0];
  133. };
  134. /*
  135. * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
  136. * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
  137. * loaded on this CPU (so we can clear them if the CPU goes down).
  138. */
  139. struct loaded_vmcs {
  140. struct vmcs *vmcs;
  141. int cpu;
  142. int launched;
  143. struct list_head loaded_vmcss_on_cpu_link;
  144. };
  145. struct shared_msr_entry {
  146. unsigned index;
  147. u64 data;
  148. u64 mask;
  149. };
  150. /*
  151. * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
  152. * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
  153. * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
  154. * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
  155. * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
  156. * More than one of these structures may exist, if L1 runs multiple L2 guests.
  157. * nested_vmx_run() will use the data here to build a vmcs02: a VMCS for the
  158. * underlying hardware which will be used to run L2.
  159. * This structure is packed to ensure that its layout is identical across
  160. * machines (necessary for live migration).
  161. * If there are changes in this struct, VMCS12_REVISION must be changed.
  162. */
  163. typedef u64 natural_width;
  164. struct __packed vmcs12 {
  165. /* According to the Intel spec, a VMCS region must start with the
  166. * following two fields. Then follow implementation-specific data.
  167. */
  168. u32 revision_id;
  169. u32 abort;
  170. u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
  171. u32 padding[7]; /* room for future expansion */
  172. u64 io_bitmap_a;
  173. u64 io_bitmap_b;
  174. u64 msr_bitmap;
  175. u64 vm_exit_msr_store_addr;
  176. u64 vm_exit_msr_load_addr;
  177. u64 vm_entry_msr_load_addr;
  178. u64 tsc_offset;
  179. u64 virtual_apic_page_addr;
  180. u64 apic_access_addr;
  181. u64 ept_pointer;
  182. u64 guest_physical_address;
  183. u64 vmcs_link_pointer;
  184. u64 guest_ia32_debugctl;
  185. u64 guest_ia32_pat;
  186. u64 guest_ia32_efer;
  187. u64 guest_ia32_perf_global_ctrl;
  188. u64 guest_pdptr0;
  189. u64 guest_pdptr1;
  190. u64 guest_pdptr2;
  191. u64 guest_pdptr3;
  192. u64 guest_bndcfgs;
  193. u64 host_ia32_pat;
  194. u64 host_ia32_efer;
  195. u64 host_ia32_perf_global_ctrl;
  196. u64 padding64[8]; /* room for future expansion */
  197. /*
  198. * To allow migration of L1 (complete with its L2 guests) between
  199. * machines of different natural widths (32 or 64 bit), we cannot have
  200. * unsigned long fields with no explict size. We use u64 (aliased
  201. * natural_width) instead. Luckily, x86 is little-endian.
  202. */
  203. natural_width cr0_guest_host_mask;
  204. natural_width cr4_guest_host_mask;
  205. natural_width cr0_read_shadow;
  206. natural_width cr4_read_shadow;
  207. natural_width cr3_target_value0;
  208. natural_width cr3_target_value1;
  209. natural_width cr3_target_value2;
  210. natural_width cr3_target_value3;
  211. natural_width exit_qualification;
  212. natural_width guest_linear_address;
  213. natural_width guest_cr0;
  214. natural_width guest_cr3;
  215. natural_width guest_cr4;
  216. natural_width guest_es_base;
  217. natural_width guest_cs_base;
  218. natural_width guest_ss_base;
  219. natural_width guest_ds_base;
  220. natural_width guest_fs_base;
  221. natural_width guest_gs_base;
  222. natural_width guest_ldtr_base;
  223. natural_width guest_tr_base;
  224. natural_width guest_gdtr_base;
  225. natural_width guest_idtr_base;
  226. natural_width guest_dr7;
  227. natural_width guest_rsp;
  228. natural_width guest_rip;
  229. natural_width guest_rflags;
  230. natural_width guest_pending_dbg_exceptions;
  231. natural_width guest_sysenter_esp;
  232. natural_width guest_sysenter_eip;
  233. natural_width host_cr0;
  234. natural_width host_cr3;
  235. natural_width host_cr4;
  236. natural_width host_fs_base;
  237. natural_width host_gs_base;
  238. natural_width host_tr_base;
  239. natural_width host_gdtr_base;
  240. natural_width host_idtr_base;
  241. natural_width host_ia32_sysenter_esp;
  242. natural_width host_ia32_sysenter_eip;
  243. natural_width host_rsp;
  244. natural_width host_rip;
  245. natural_width paddingl[8]; /* room for future expansion */
  246. u32 pin_based_vm_exec_control;
  247. u32 cpu_based_vm_exec_control;
  248. u32 exception_bitmap;
  249. u32 page_fault_error_code_mask;
  250. u32 page_fault_error_code_match;
  251. u32 cr3_target_count;
  252. u32 vm_exit_controls;
  253. u32 vm_exit_msr_store_count;
  254. u32 vm_exit_msr_load_count;
  255. u32 vm_entry_controls;
  256. u32 vm_entry_msr_load_count;
  257. u32 vm_entry_intr_info_field;
  258. u32 vm_entry_exception_error_code;
  259. u32 vm_entry_instruction_len;
  260. u32 tpr_threshold;
  261. u32 secondary_vm_exec_control;
  262. u32 vm_instruction_error;
  263. u32 vm_exit_reason;
  264. u32 vm_exit_intr_info;
  265. u32 vm_exit_intr_error_code;
  266. u32 idt_vectoring_info_field;
  267. u32 idt_vectoring_error_code;
  268. u32 vm_exit_instruction_len;
  269. u32 vmx_instruction_info;
  270. u32 guest_es_limit;
  271. u32 guest_cs_limit;
  272. u32 guest_ss_limit;
  273. u32 guest_ds_limit;
  274. u32 guest_fs_limit;
  275. u32 guest_gs_limit;
  276. u32 guest_ldtr_limit;
  277. u32 guest_tr_limit;
  278. u32 guest_gdtr_limit;
  279. u32 guest_idtr_limit;
  280. u32 guest_es_ar_bytes;
  281. u32 guest_cs_ar_bytes;
  282. u32 guest_ss_ar_bytes;
  283. u32 guest_ds_ar_bytes;
  284. u32 guest_fs_ar_bytes;
  285. u32 guest_gs_ar_bytes;
  286. u32 guest_ldtr_ar_bytes;
  287. u32 guest_tr_ar_bytes;
  288. u32 guest_interruptibility_info;
  289. u32 guest_activity_state;
  290. u32 guest_sysenter_cs;
  291. u32 host_ia32_sysenter_cs;
  292. u32 vmx_preemption_timer_value;
  293. u32 padding32[7]; /* room for future expansion */
  294. u16 virtual_processor_id;
  295. u16 guest_es_selector;
  296. u16 guest_cs_selector;
  297. u16 guest_ss_selector;
  298. u16 guest_ds_selector;
  299. u16 guest_fs_selector;
  300. u16 guest_gs_selector;
  301. u16 guest_ldtr_selector;
  302. u16 guest_tr_selector;
  303. u16 host_es_selector;
  304. u16 host_cs_selector;
  305. u16 host_ss_selector;
  306. u16 host_ds_selector;
  307. u16 host_fs_selector;
  308. u16 host_gs_selector;
  309. u16 host_tr_selector;
  310. };
  311. /*
  312. * VMCS12_REVISION is an arbitrary id that should be changed if the content or
  313. * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
  314. * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
  315. */
  316. #define VMCS12_REVISION 0x11e57ed0
  317. /*
  318. * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
  319. * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
  320. * current implementation, 4K are reserved to avoid future complications.
  321. */
  322. #define VMCS12_SIZE 0x1000
  323. /* Used to remember the last vmcs02 used for some recently used vmcs12s */
  324. struct vmcs02_list {
  325. struct list_head list;
  326. gpa_t vmptr;
  327. struct loaded_vmcs vmcs02;
  328. };
  329. /*
  330. * The nested_vmx structure is part of vcpu_vmx, and holds information we need
  331. * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
  332. */
  333. struct nested_vmx {
  334. /* Has the level1 guest done vmxon? */
  335. bool vmxon;
  336. gpa_t vmxon_ptr;
  337. /* The guest-physical address of the current VMCS L1 keeps for L2 */
  338. gpa_t current_vmptr;
  339. /* The host-usable pointer to the above */
  340. struct page *current_vmcs12_page;
  341. struct vmcs12 *current_vmcs12;
  342. struct vmcs *current_shadow_vmcs;
  343. /*
  344. * Indicates if the shadow vmcs must be updated with the
  345. * data hold by vmcs12
  346. */
  347. bool sync_shadow_vmcs;
  348. /* vmcs02_list cache of VMCSs recently used to run L2 guests */
  349. struct list_head vmcs02_pool;
  350. int vmcs02_num;
  351. u64 vmcs01_tsc_offset;
  352. /* L2 must run next, and mustn't decide to exit to L1. */
  353. bool nested_run_pending;
  354. /*
  355. * Guest pages referred to in vmcs02 with host-physical pointers, so
  356. * we must keep them pinned while L2 runs.
  357. */
  358. struct page *apic_access_page;
  359. struct page *virtual_apic_page;
  360. u64 msr_ia32_feature_control;
  361. struct hrtimer preemption_timer;
  362. bool preemption_timer_expired;
  363. /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
  364. u64 vmcs01_debugctl;
  365. };
  366. #define POSTED_INTR_ON 0
  367. /* Posted-Interrupt Descriptor */
  368. struct pi_desc {
  369. u32 pir[8]; /* Posted interrupt requested */
  370. u32 control; /* bit 0 of control is outstanding notification bit */
  371. u32 rsvd[7];
  372. } __aligned(64);
  373. static bool pi_test_and_set_on(struct pi_desc *pi_desc)
  374. {
  375. return test_and_set_bit(POSTED_INTR_ON,
  376. (unsigned long *)&pi_desc->control);
  377. }
  378. static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
  379. {
  380. return test_and_clear_bit(POSTED_INTR_ON,
  381. (unsigned long *)&pi_desc->control);
  382. }
  383. static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
  384. {
  385. return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
  386. }
  387. struct vcpu_vmx {
  388. struct kvm_vcpu vcpu;
  389. unsigned long host_rsp;
  390. u8 fail;
  391. bool nmi_known_unmasked;
  392. u32 exit_intr_info;
  393. u32 idt_vectoring_info;
  394. ulong rflags;
  395. struct shared_msr_entry *guest_msrs;
  396. int nmsrs;
  397. int save_nmsrs;
  398. unsigned long host_idt_base;
  399. #ifdef CONFIG_X86_64
  400. u64 msr_host_kernel_gs_base;
  401. u64 msr_guest_kernel_gs_base;
  402. #endif
  403. u32 vm_entry_controls_shadow;
  404. u32 vm_exit_controls_shadow;
  405. /*
  406. * loaded_vmcs points to the VMCS currently used in this vcpu. For a
  407. * non-nested (L1) guest, it always points to vmcs01. For a nested
  408. * guest (L2), it points to a different VMCS.
  409. */
  410. struct loaded_vmcs vmcs01;
  411. struct loaded_vmcs *loaded_vmcs;
  412. bool __launched; /* temporary, used in vmx_vcpu_run */
  413. struct msr_autoload {
  414. unsigned nr;
  415. struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
  416. struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
  417. } msr_autoload;
  418. struct {
  419. int loaded;
  420. u16 fs_sel, gs_sel, ldt_sel;
  421. #ifdef CONFIG_X86_64
  422. u16 ds_sel, es_sel;
  423. #endif
  424. int gs_ldt_reload_needed;
  425. int fs_reload_needed;
  426. u64 msr_host_bndcfgs;
  427. unsigned long vmcs_host_cr4; /* May not match real cr4 */
  428. } host_state;
  429. struct {
  430. int vm86_active;
  431. ulong save_rflags;
  432. struct kvm_segment segs[8];
  433. } rmode;
  434. struct {
  435. u32 bitmask; /* 4 bits per segment (1 bit per field) */
  436. struct kvm_save_segment {
  437. u16 selector;
  438. unsigned long base;
  439. u32 limit;
  440. u32 ar;
  441. } seg[8];
  442. } segment_cache;
  443. int vpid;
  444. bool emulation_required;
  445. /* Support for vnmi-less CPUs */
  446. int soft_vnmi_blocked;
  447. ktime_t entry_time;
  448. s64 vnmi_blocked_time;
  449. u32 exit_reason;
  450. bool rdtscp_enabled;
  451. /* Posted interrupt descriptor */
  452. struct pi_desc pi_desc;
  453. /* Support for a guest hypervisor (nested VMX) */
  454. struct nested_vmx nested;
  455. /* Dynamic PLE window. */
  456. int ple_window;
  457. bool ple_window_dirty;
  458. };
  459. enum segment_cache_field {
  460. SEG_FIELD_SEL = 0,
  461. SEG_FIELD_BASE = 1,
  462. SEG_FIELD_LIMIT = 2,
  463. SEG_FIELD_AR = 3,
  464. SEG_FIELD_NR = 4
  465. };
  466. static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
  467. {
  468. return container_of(vcpu, struct vcpu_vmx, vcpu);
  469. }
  470. #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
  471. #define FIELD(number, name) [number] = VMCS12_OFFSET(name)
  472. #define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \
  473. [number##_HIGH] = VMCS12_OFFSET(name)+4
  474. static unsigned long shadow_read_only_fields[] = {
  475. /*
  476. * We do NOT shadow fields that are modified when L0
  477. * traps and emulates any vmx instruction (e.g. VMPTRLD,
  478. * VMXON...) executed by L1.
  479. * For example, VM_INSTRUCTION_ERROR is read
  480. * by L1 if a vmx instruction fails (part of the error path).
  481. * Note the code assumes this logic. If for some reason
  482. * we start shadowing these fields then we need to
  483. * force a shadow sync when L0 emulates vmx instructions
  484. * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified
  485. * by nested_vmx_failValid)
  486. */
  487. VM_EXIT_REASON,
  488. VM_EXIT_INTR_INFO,
  489. VM_EXIT_INSTRUCTION_LEN,
  490. IDT_VECTORING_INFO_FIELD,
  491. IDT_VECTORING_ERROR_CODE,
  492. VM_EXIT_INTR_ERROR_CODE,
  493. EXIT_QUALIFICATION,
  494. GUEST_LINEAR_ADDRESS,
  495. GUEST_PHYSICAL_ADDRESS
  496. };
  497. static int max_shadow_read_only_fields =
  498. ARRAY_SIZE(shadow_read_only_fields);
  499. static unsigned long shadow_read_write_fields[] = {
  500. TPR_THRESHOLD,
  501. GUEST_RIP,
  502. GUEST_RSP,
  503. GUEST_CR0,
  504. GUEST_CR3,
  505. GUEST_CR4,
  506. GUEST_INTERRUPTIBILITY_INFO,
  507. GUEST_RFLAGS,
  508. GUEST_CS_SELECTOR,
  509. GUEST_CS_AR_BYTES,
  510. GUEST_CS_LIMIT,
  511. GUEST_CS_BASE,
  512. GUEST_ES_BASE,
  513. GUEST_BNDCFGS,
  514. CR0_GUEST_HOST_MASK,
  515. CR0_READ_SHADOW,
  516. CR4_READ_SHADOW,
  517. TSC_OFFSET,
  518. EXCEPTION_BITMAP,
  519. CPU_BASED_VM_EXEC_CONTROL,
  520. VM_ENTRY_EXCEPTION_ERROR_CODE,
  521. VM_ENTRY_INTR_INFO_FIELD,
  522. VM_ENTRY_INSTRUCTION_LEN,
  523. VM_ENTRY_EXCEPTION_ERROR_CODE,
  524. HOST_FS_BASE,
  525. HOST_GS_BASE,
  526. HOST_FS_SELECTOR,
  527. HOST_GS_SELECTOR
  528. };
  529. static int max_shadow_read_write_fields =
  530. ARRAY_SIZE(shadow_read_write_fields);
  531. static const unsigned short vmcs_field_to_offset_table[] = {
  532. FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
  533. FIELD(GUEST_ES_SELECTOR, guest_es_selector),
  534. FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
  535. FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
  536. FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
  537. FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
  538. FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
  539. FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
  540. FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
  541. FIELD(HOST_ES_SELECTOR, host_es_selector),
  542. FIELD(HOST_CS_SELECTOR, host_cs_selector),
  543. FIELD(HOST_SS_SELECTOR, host_ss_selector),
  544. FIELD(HOST_DS_SELECTOR, host_ds_selector),
  545. FIELD(HOST_FS_SELECTOR, host_fs_selector),
  546. FIELD(HOST_GS_SELECTOR, host_gs_selector),
  547. FIELD(HOST_TR_SELECTOR, host_tr_selector),
  548. FIELD64(IO_BITMAP_A, io_bitmap_a),
  549. FIELD64(IO_BITMAP_B, io_bitmap_b),
  550. FIELD64(MSR_BITMAP, msr_bitmap),
  551. FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
  552. FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
  553. FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
  554. FIELD64(TSC_OFFSET, tsc_offset),
  555. FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
  556. FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
  557. FIELD64(EPT_POINTER, ept_pointer),
  558. FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
  559. FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
  560. FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
  561. FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
  562. FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
  563. FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
  564. FIELD64(GUEST_PDPTR0, guest_pdptr0),
  565. FIELD64(GUEST_PDPTR1, guest_pdptr1),
  566. FIELD64(GUEST_PDPTR2, guest_pdptr2),
  567. FIELD64(GUEST_PDPTR3, guest_pdptr3),
  568. FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
  569. FIELD64(HOST_IA32_PAT, host_ia32_pat),
  570. FIELD64(HOST_IA32_EFER, host_ia32_efer),
  571. FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
  572. FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
  573. FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
  574. FIELD(EXCEPTION_BITMAP, exception_bitmap),
  575. FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
  576. FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
  577. FIELD(CR3_TARGET_COUNT, cr3_target_count),
  578. FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
  579. FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
  580. FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
  581. FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
  582. FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
  583. FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
  584. FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
  585. FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
  586. FIELD(TPR_THRESHOLD, tpr_threshold),
  587. FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
  588. FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
  589. FIELD(VM_EXIT_REASON, vm_exit_reason),
  590. FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
  591. FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
  592. FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
  593. FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
  594. FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
  595. FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
  596. FIELD(GUEST_ES_LIMIT, guest_es_limit),
  597. FIELD(GUEST_CS_LIMIT, guest_cs_limit),
  598. FIELD(GUEST_SS_LIMIT, guest_ss_limit),
  599. FIELD(GUEST_DS_LIMIT, guest_ds_limit),
  600. FIELD(GUEST_FS_LIMIT, guest_fs_limit),
  601. FIELD(GUEST_GS_LIMIT, guest_gs_limit),
  602. FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
  603. FIELD(GUEST_TR_LIMIT, guest_tr_limit),
  604. FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
  605. FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
  606. FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
  607. FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
  608. FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
  609. FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
  610. FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
  611. FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
  612. FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
  613. FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
  614. FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
  615. FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
  616. FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
  617. FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
  618. FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
  619. FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
  620. FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
  621. FIELD(CR0_READ_SHADOW, cr0_read_shadow),
  622. FIELD(CR4_READ_SHADOW, cr4_read_shadow),
  623. FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
  624. FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
  625. FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
  626. FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
  627. FIELD(EXIT_QUALIFICATION, exit_qualification),
  628. FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
  629. FIELD(GUEST_CR0, guest_cr0),
  630. FIELD(GUEST_CR3, guest_cr3),
  631. FIELD(GUEST_CR4, guest_cr4),
  632. FIELD(GUEST_ES_BASE, guest_es_base),
  633. FIELD(GUEST_CS_BASE, guest_cs_base),
  634. FIELD(GUEST_SS_BASE, guest_ss_base),
  635. FIELD(GUEST_DS_BASE, guest_ds_base),
  636. FIELD(GUEST_FS_BASE, guest_fs_base),
  637. FIELD(GUEST_GS_BASE, guest_gs_base),
  638. FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
  639. FIELD(GUEST_TR_BASE, guest_tr_base),
  640. FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
  641. FIELD(GUEST_IDTR_BASE, guest_idtr_base),
  642. FIELD(GUEST_DR7, guest_dr7),
  643. FIELD(GUEST_RSP, guest_rsp),
  644. FIELD(GUEST_RIP, guest_rip),
  645. FIELD(GUEST_RFLAGS, guest_rflags),
  646. FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
  647. FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
  648. FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
  649. FIELD(HOST_CR0, host_cr0),
  650. FIELD(HOST_CR3, host_cr3),
  651. FIELD(HOST_CR4, host_cr4),
  652. FIELD(HOST_FS_BASE, host_fs_base),
  653. FIELD(HOST_GS_BASE, host_gs_base),
  654. FIELD(HOST_TR_BASE, host_tr_base),
  655. FIELD(HOST_GDTR_BASE, host_gdtr_base),
  656. FIELD(HOST_IDTR_BASE, host_idtr_base),
  657. FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
  658. FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
  659. FIELD(HOST_RSP, host_rsp),
  660. FIELD(HOST_RIP, host_rip),
  661. };
  662. static const int max_vmcs_field = ARRAY_SIZE(vmcs_field_to_offset_table);
  663. static inline short vmcs_field_to_offset(unsigned long field)
  664. {
  665. if (field >= max_vmcs_field || vmcs_field_to_offset_table[field] == 0)
  666. return -1;
  667. return vmcs_field_to_offset_table[field];
  668. }
  669. static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
  670. {
  671. return to_vmx(vcpu)->nested.current_vmcs12;
  672. }
  673. static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr)
  674. {
  675. struct page *page = gfn_to_page(vcpu->kvm, addr >> PAGE_SHIFT);
  676. if (is_error_page(page))
  677. return NULL;
  678. return page;
  679. }
  680. static void nested_release_page(struct page *page)
  681. {
  682. kvm_release_page_dirty(page);
  683. }
  684. static void nested_release_page_clean(struct page *page)
  685. {
  686. kvm_release_page_clean(page);
  687. }
  688. static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
  689. static u64 construct_eptp(unsigned long root_hpa);
  690. static void kvm_cpu_vmxon(u64 addr);
  691. static void kvm_cpu_vmxoff(void);
  692. static bool vmx_mpx_supported(void);
  693. static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
  694. static void vmx_set_segment(struct kvm_vcpu *vcpu,
  695. struct kvm_segment *var, int seg);
  696. static void vmx_get_segment(struct kvm_vcpu *vcpu,
  697. struct kvm_segment *var, int seg);
  698. static bool guest_state_valid(struct kvm_vcpu *vcpu);
  699. static u32 vmx_segment_access_rights(struct kvm_segment *var);
  700. static void vmx_sync_pir_to_irr_dummy(struct kvm_vcpu *vcpu);
  701. static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx);
  702. static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
  703. static int alloc_identity_pagetable(struct kvm *kvm);
  704. static DEFINE_PER_CPU(struct vmcs *, vmxarea);
  705. static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
  706. /*
  707. * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
  708. * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
  709. */
  710. static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
  711. static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
  712. static unsigned long *vmx_io_bitmap_a;
  713. static unsigned long *vmx_io_bitmap_b;
  714. static unsigned long *vmx_msr_bitmap_legacy;
  715. static unsigned long *vmx_msr_bitmap_longmode;
  716. static unsigned long *vmx_msr_bitmap_legacy_x2apic;
  717. static unsigned long *vmx_msr_bitmap_longmode_x2apic;
  718. static unsigned long *vmx_vmread_bitmap;
  719. static unsigned long *vmx_vmwrite_bitmap;
  720. static bool cpu_has_load_ia32_efer;
  721. static bool cpu_has_load_perf_global_ctrl;
  722. static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
  723. static DEFINE_SPINLOCK(vmx_vpid_lock);
  724. static struct vmcs_config {
  725. int size;
  726. int order;
  727. u32 revision_id;
  728. u32 pin_based_exec_ctrl;
  729. u32 cpu_based_exec_ctrl;
  730. u32 cpu_based_2nd_exec_ctrl;
  731. u32 vmexit_ctrl;
  732. u32 vmentry_ctrl;
  733. } vmcs_config;
  734. static struct vmx_capability {
  735. u32 ept;
  736. u32 vpid;
  737. } vmx_capability;
  738. #define VMX_SEGMENT_FIELD(seg) \
  739. [VCPU_SREG_##seg] = { \
  740. .selector = GUEST_##seg##_SELECTOR, \
  741. .base = GUEST_##seg##_BASE, \
  742. .limit = GUEST_##seg##_LIMIT, \
  743. .ar_bytes = GUEST_##seg##_AR_BYTES, \
  744. }
  745. static const struct kvm_vmx_segment_field {
  746. unsigned selector;
  747. unsigned base;
  748. unsigned limit;
  749. unsigned ar_bytes;
  750. } kvm_vmx_segment_fields[] = {
  751. VMX_SEGMENT_FIELD(CS),
  752. VMX_SEGMENT_FIELD(DS),
  753. VMX_SEGMENT_FIELD(ES),
  754. VMX_SEGMENT_FIELD(FS),
  755. VMX_SEGMENT_FIELD(GS),
  756. VMX_SEGMENT_FIELD(SS),
  757. VMX_SEGMENT_FIELD(TR),
  758. VMX_SEGMENT_FIELD(LDTR),
  759. };
  760. static u64 host_efer;
  761. static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
  762. /*
  763. * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
  764. * away by decrementing the array size.
  765. */
  766. static const u32 vmx_msr_index[] = {
  767. #ifdef CONFIG_X86_64
  768. MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
  769. #endif
  770. MSR_EFER, MSR_TSC_AUX, MSR_STAR,
  771. };
  772. static inline bool is_page_fault(u32 intr_info)
  773. {
  774. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  775. INTR_INFO_VALID_MASK)) ==
  776. (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
  777. }
  778. static inline bool is_no_device(u32 intr_info)
  779. {
  780. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  781. INTR_INFO_VALID_MASK)) ==
  782. (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
  783. }
  784. static inline bool is_invalid_opcode(u32 intr_info)
  785. {
  786. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  787. INTR_INFO_VALID_MASK)) ==
  788. (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
  789. }
  790. static inline bool is_external_interrupt(u32 intr_info)
  791. {
  792. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
  793. == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
  794. }
  795. static inline bool is_machine_check(u32 intr_info)
  796. {
  797. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  798. INTR_INFO_VALID_MASK)) ==
  799. (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
  800. }
  801. static inline bool cpu_has_vmx_msr_bitmap(void)
  802. {
  803. return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
  804. }
  805. static inline bool cpu_has_vmx_tpr_shadow(void)
  806. {
  807. return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
  808. }
  809. static inline bool vm_need_tpr_shadow(struct kvm *kvm)
  810. {
  811. return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm));
  812. }
  813. static inline bool cpu_has_secondary_exec_ctrls(void)
  814. {
  815. return vmcs_config.cpu_based_exec_ctrl &
  816. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  817. }
  818. static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
  819. {
  820. return vmcs_config.cpu_based_2nd_exec_ctrl &
  821. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  822. }
  823. static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
  824. {
  825. return vmcs_config.cpu_based_2nd_exec_ctrl &
  826. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  827. }
  828. static inline bool cpu_has_vmx_apic_register_virt(void)
  829. {
  830. return vmcs_config.cpu_based_2nd_exec_ctrl &
  831. SECONDARY_EXEC_APIC_REGISTER_VIRT;
  832. }
  833. static inline bool cpu_has_vmx_virtual_intr_delivery(void)
  834. {
  835. return vmcs_config.cpu_based_2nd_exec_ctrl &
  836. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
  837. }
  838. static inline bool cpu_has_vmx_posted_intr(void)
  839. {
  840. return vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
  841. }
  842. static inline bool cpu_has_vmx_apicv(void)
  843. {
  844. return cpu_has_vmx_apic_register_virt() &&
  845. cpu_has_vmx_virtual_intr_delivery() &&
  846. cpu_has_vmx_posted_intr();
  847. }
  848. static inline bool cpu_has_vmx_flexpriority(void)
  849. {
  850. return cpu_has_vmx_tpr_shadow() &&
  851. cpu_has_vmx_virtualize_apic_accesses();
  852. }
  853. static inline bool cpu_has_vmx_ept_execute_only(void)
  854. {
  855. return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
  856. }
  857. static inline bool cpu_has_vmx_eptp_uncacheable(void)
  858. {
  859. return vmx_capability.ept & VMX_EPTP_UC_BIT;
  860. }
  861. static inline bool cpu_has_vmx_eptp_writeback(void)
  862. {
  863. return vmx_capability.ept & VMX_EPTP_WB_BIT;
  864. }
  865. static inline bool cpu_has_vmx_ept_2m_page(void)
  866. {
  867. return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
  868. }
  869. static inline bool cpu_has_vmx_ept_1g_page(void)
  870. {
  871. return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
  872. }
  873. static inline bool cpu_has_vmx_ept_4levels(void)
  874. {
  875. return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
  876. }
  877. static inline bool cpu_has_vmx_ept_ad_bits(void)
  878. {
  879. return vmx_capability.ept & VMX_EPT_AD_BIT;
  880. }
  881. static inline bool cpu_has_vmx_invept_context(void)
  882. {
  883. return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
  884. }
  885. static inline bool cpu_has_vmx_invept_global(void)
  886. {
  887. return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
  888. }
  889. static inline bool cpu_has_vmx_invvpid_single(void)
  890. {
  891. return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
  892. }
  893. static inline bool cpu_has_vmx_invvpid_global(void)
  894. {
  895. return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
  896. }
  897. static inline bool cpu_has_vmx_ept(void)
  898. {
  899. return vmcs_config.cpu_based_2nd_exec_ctrl &
  900. SECONDARY_EXEC_ENABLE_EPT;
  901. }
  902. static inline bool cpu_has_vmx_unrestricted_guest(void)
  903. {
  904. return vmcs_config.cpu_based_2nd_exec_ctrl &
  905. SECONDARY_EXEC_UNRESTRICTED_GUEST;
  906. }
  907. static inline bool cpu_has_vmx_ple(void)
  908. {
  909. return vmcs_config.cpu_based_2nd_exec_ctrl &
  910. SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  911. }
  912. static inline bool vm_need_virtualize_apic_accesses(struct kvm *kvm)
  913. {
  914. return flexpriority_enabled && irqchip_in_kernel(kvm);
  915. }
  916. static inline bool cpu_has_vmx_vpid(void)
  917. {
  918. return vmcs_config.cpu_based_2nd_exec_ctrl &
  919. SECONDARY_EXEC_ENABLE_VPID;
  920. }
  921. static inline bool cpu_has_vmx_rdtscp(void)
  922. {
  923. return vmcs_config.cpu_based_2nd_exec_ctrl &
  924. SECONDARY_EXEC_RDTSCP;
  925. }
  926. static inline bool cpu_has_vmx_invpcid(void)
  927. {
  928. return vmcs_config.cpu_based_2nd_exec_ctrl &
  929. SECONDARY_EXEC_ENABLE_INVPCID;
  930. }
  931. static inline bool cpu_has_virtual_nmis(void)
  932. {
  933. return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
  934. }
  935. static inline bool cpu_has_vmx_wbinvd_exit(void)
  936. {
  937. return vmcs_config.cpu_based_2nd_exec_ctrl &
  938. SECONDARY_EXEC_WBINVD_EXITING;
  939. }
  940. static inline bool cpu_has_vmx_shadow_vmcs(void)
  941. {
  942. u64 vmx_msr;
  943. rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
  944. /* check if the cpu supports writing r/o exit information fields */
  945. if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
  946. return false;
  947. return vmcs_config.cpu_based_2nd_exec_ctrl &
  948. SECONDARY_EXEC_SHADOW_VMCS;
  949. }
  950. static inline bool report_flexpriority(void)
  951. {
  952. return flexpriority_enabled;
  953. }
  954. static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
  955. {
  956. return vmcs12->cpu_based_vm_exec_control & bit;
  957. }
  958. static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
  959. {
  960. return (vmcs12->cpu_based_vm_exec_control &
  961. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
  962. (vmcs12->secondary_vm_exec_control & bit);
  963. }
  964. static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
  965. {
  966. return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
  967. }
  968. static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
  969. {
  970. return vmcs12->pin_based_vm_exec_control &
  971. PIN_BASED_VMX_PREEMPTION_TIMER;
  972. }
  973. static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
  974. {
  975. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
  976. }
  977. static inline bool is_exception(u32 intr_info)
  978. {
  979. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
  980. == (INTR_TYPE_HARD_EXCEPTION | INTR_INFO_VALID_MASK);
  981. }
  982. static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
  983. u32 exit_intr_info,
  984. unsigned long exit_qualification);
  985. static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
  986. struct vmcs12 *vmcs12,
  987. u32 reason, unsigned long qualification);
  988. static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
  989. {
  990. int i;
  991. for (i = 0; i < vmx->nmsrs; ++i)
  992. if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
  993. return i;
  994. return -1;
  995. }
  996. static inline void __invvpid(int ext, u16 vpid, gva_t gva)
  997. {
  998. struct {
  999. u64 vpid : 16;
  1000. u64 rsvd : 48;
  1001. u64 gva;
  1002. } operand = { vpid, 0, gva };
  1003. asm volatile (__ex(ASM_VMX_INVVPID)
  1004. /* CF==1 or ZF==1 --> rc = -1 */
  1005. "; ja 1f ; ud2 ; 1:"
  1006. : : "a"(&operand), "c"(ext) : "cc", "memory");
  1007. }
  1008. static inline void __invept(int ext, u64 eptp, gpa_t gpa)
  1009. {
  1010. struct {
  1011. u64 eptp, gpa;
  1012. } operand = {eptp, gpa};
  1013. asm volatile (__ex(ASM_VMX_INVEPT)
  1014. /* CF==1 or ZF==1 --> rc = -1 */
  1015. "; ja 1f ; ud2 ; 1:\n"
  1016. : : "a" (&operand), "c" (ext) : "cc", "memory");
  1017. }
  1018. static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
  1019. {
  1020. int i;
  1021. i = __find_msr_index(vmx, msr);
  1022. if (i >= 0)
  1023. return &vmx->guest_msrs[i];
  1024. return NULL;
  1025. }
  1026. static void vmcs_clear(struct vmcs *vmcs)
  1027. {
  1028. u64 phys_addr = __pa(vmcs);
  1029. u8 error;
  1030. asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
  1031. : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
  1032. : "cc", "memory");
  1033. if (error)
  1034. printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
  1035. vmcs, phys_addr);
  1036. }
  1037. static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
  1038. {
  1039. vmcs_clear(loaded_vmcs->vmcs);
  1040. loaded_vmcs->cpu = -1;
  1041. loaded_vmcs->launched = 0;
  1042. }
  1043. static void vmcs_load(struct vmcs *vmcs)
  1044. {
  1045. u64 phys_addr = __pa(vmcs);
  1046. u8 error;
  1047. asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
  1048. : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
  1049. : "cc", "memory");
  1050. if (error)
  1051. printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
  1052. vmcs, phys_addr);
  1053. }
  1054. #ifdef CONFIG_KEXEC
  1055. /*
  1056. * This bitmap is used to indicate whether the vmclear
  1057. * operation is enabled on all cpus. All disabled by
  1058. * default.
  1059. */
  1060. static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
  1061. static inline void crash_enable_local_vmclear(int cpu)
  1062. {
  1063. cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
  1064. }
  1065. static inline void crash_disable_local_vmclear(int cpu)
  1066. {
  1067. cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
  1068. }
  1069. static inline int crash_local_vmclear_enabled(int cpu)
  1070. {
  1071. return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
  1072. }
  1073. static void crash_vmclear_local_loaded_vmcss(void)
  1074. {
  1075. int cpu = raw_smp_processor_id();
  1076. struct loaded_vmcs *v;
  1077. if (!crash_local_vmclear_enabled(cpu))
  1078. return;
  1079. list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
  1080. loaded_vmcss_on_cpu_link)
  1081. vmcs_clear(v->vmcs);
  1082. }
  1083. #else
  1084. static inline void crash_enable_local_vmclear(int cpu) { }
  1085. static inline void crash_disable_local_vmclear(int cpu) { }
  1086. #endif /* CONFIG_KEXEC */
  1087. static void __loaded_vmcs_clear(void *arg)
  1088. {
  1089. struct loaded_vmcs *loaded_vmcs = arg;
  1090. int cpu = raw_smp_processor_id();
  1091. if (loaded_vmcs->cpu != cpu)
  1092. return; /* vcpu migration can race with cpu offline */
  1093. if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
  1094. per_cpu(current_vmcs, cpu) = NULL;
  1095. crash_disable_local_vmclear(cpu);
  1096. list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
  1097. /*
  1098. * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
  1099. * is before setting loaded_vmcs->vcpu to -1 which is done in
  1100. * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
  1101. * then adds the vmcs into percpu list before it is deleted.
  1102. */
  1103. smp_wmb();
  1104. loaded_vmcs_init(loaded_vmcs);
  1105. crash_enable_local_vmclear(cpu);
  1106. }
  1107. static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
  1108. {
  1109. int cpu = loaded_vmcs->cpu;
  1110. if (cpu != -1)
  1111. smp_call_function_single(cpu,
  1112. __loaded_vmcs_clear, loaded_vmcs, 1);
  1113. }
  1114. static inline void vpid_sync_vcpu_single(struct vcpu_vmx *vmx)
  1115. {
  1116. if (vmx->vpid == 0)
  1117. return;
  1118. if (cpu_has_vmx_invvpid_single())
  1119. __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
  1120. }
  1121. static inline void vpid_sync_vcpu_global(void)
  1122. {
  1123. if (cpu_has_vmx_invvpid_global())
  1124. __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
  1125. }
  1126. static inline void vpid_sync_context(struct vcpu_vmx *vmx)
  1127. {
  1128. if (cpu_has_vmx_invvpid_single())
  1129. vpid_sync_vcpu_single(vmx);
  1130. else
  1131. vpid_sync_vcpu_global();
  1132. }
  1133. static inline void ept_sync_global(void)
  1134. {
  1135. if (cpu_has_vmx_invept_global())
  1136. __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
  1137. }
  1138. static inline void ept_sync_context(u64 eptp)
  1139. {
  1140. if (enable_ept) {
  1141. if (cpu_has_vmx_invept_context())
  1142. __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
  1143. else
  1144. ept_sync_global();
  1145. }
  1146. }
  1147. static __always_inline unsigned long vmcs_readl(unsigned long field)
  1148. {
  1149. unsigned long value;
  1150. asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
  1151. : "=a"(value) : "d"(field) : "cc");
  1152. return value;
  1153. }
  1154. static __always_inline u16 vmcs_read16(unsigned long field)
  1155. {
  1156. return vmcs_readl(field);
  1157. }
  1158. static __always_inline u32 vmcs_read32(unsigned long field)
  1159. {
  1160. return vmcs_readl(field);
  1161. }
  1162. static __always_inline u64 vmcs_read64(unsigned long field)
  1163. {
  1164. #ifdef CONFIG_X86_64
  1165. return vmcs_readl(field);
  1166. #else
  1167. return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
  1168. #endif
  1169. }
  1170. static noinline void vmwrite_error(unsigned long field, unsigned long value)
  1171. {
  1172. printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
  1173. field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
  1174. dump_stack();
  1175. }
  1176. static void vmcs_writel(unsigned long field, unsigned long value)
  1177. {
  1178. u8 error;
  1179. asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
  1180. : "=q"(error) : "a"(value), "d"(field) : "cc");
  1181. if (unlikely(error))
  1182. vmwrite_error(field, value);
  1183. }
  1184. static void vmcs_write16(unsigned long field, u16 value)
  1185. {
  1186. vmcs_writel(field, value);
  1187. }
  1188. static void vmcs_write32(unsigned long field, u32 value)
  1189. {
  1190. vmcs_writel(field, value);
  1191. }
  1192. static void vmcs_write64(unsigned long field, u64 value)
  1193. {
  1194. vmcs_writel(field, value);
  1195. #ifndef CONFIG_X86_64
  1196. asm volatile ("");
  1197. vmcs_writel(field+1, value >> 32);
  1198. #endif
  1199. }
  1200. static void vmcs_clear_bits(unsigned long field, u32 mask)
  1201. {
  1202. vmcs_writel(field, vmcs_readl(field) & ~mask);
  1203. }
  1204. static void vmcs_set_bits(unsigned long field, u32 mask)
  1205. {
  1206. vmcs_writel(field, vmcs_readl(field) | mask);
  1207. }
  1208. static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
  1209. {
  1210. vmcs_write32(VM_ENTRY_CONTROLS, val);
  1211. vmx->vm_entry_controls_shadow = val;
  1212. }
  1213. static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
  1214. {
  1215. if (vmx->vm_entry_controls_shadow != val)
  1216. vm_entry_controls_init(vmx, val);
  1217. }
  1218. static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
  1219. {
  1220. return vmx->vm_entry_controls_shadow;
  1221. }
  1222. static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
  1223. {
  1224. vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
  1225. }
  1226. static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
  1227. {
  1228. vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
  1229. }
  1230. static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
  1231. {
  1232. vmcs_write32(VM_EXIT_CONTROLS, val);
  1233. vmx->vm_exit_controls_shadow = val;
  1234. }
  1235. static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
  1236. {
  1237. if (vmx->vm_exit_controls_shadow != val)
  1238. vm_exit_controls_init(vmx, val);
  1239. }
  1240. static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
  1241. {
  1242. return vmx->vm_exit_controls_shadow;
  1243. }
  1244. static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
  1245. {
  1246. vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
  1247. }
  1248. static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
  1249. {
  1250. vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
  1251. }
  1252. static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
  1253. {
  1254. vmx->segment_cache.bitmask = 0;
  1255. }
  1256. static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
  1257. unsigned field)
  1258. {
  1259. bool ret;
  1260. u32 mask = 1 << (seg * SEG_FIELD_NR + field);
  1261. if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
  1262. vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
  1263. vmx->segment_cache.bitmask = 0;
  1264. }
  1265. ret = vmx->segment_cache.bitmask & mask;
  1266. vmx->segment_cache.bitmask |= mask;
  1267. return ret;
  1268. }
  1269. static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
  1270. {
  1271. u16 *p = &vmx->segment_cache.seg[seg].selector;
  1272. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
  1273. *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
  1274. return *p;
  1275. }
  1276. static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
  1277. {
  1278. ulong *p = &vmx->segment_cache.seg[seg].base;
  1279. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
  1280. *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
  1281. return *p;
  1282. }
  1283. static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
  1284. {
  1285. u32 *p = &vmx->segment_cache.seg[seg].limit;
  1286. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
  1287. *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
  1288. return *p;
  1289. }
  1290. static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
  1291. {
  1292. u32 *p = &vmx->segment_cache.seg[seg].ar;
  1293. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
  1294. *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
  1295. return *p;
  1296. }
  1297. static void update_exception_bitmap(struct kvm_vcpu *vcpu)
  1298. {
  1299. u32 eb;
  1300. eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
  1301. (1u << NM_VECTOR) | (1u << DB_VECTOR);
  1302. if ((vcpu->guest_debug &
  1303. (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
  1304. (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
  1305. eb |= 1u << BP_VECTOR;
  1306. if (to_vmx(vcpu)->rmode.vm86_active)
  1307. eb = ~0;
  1308. if (enable_ept)
  1309. eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
  1310. if (vcpu->fpu_active)
  1311. eb &= ~(1u << NM_VECTOR);
  1312. /* When we are running a nested L2 guest and L1 specified for it a
  1313. * certain exception bitmap, we must trap the same exceptions and pass
  1314. * them to L1. When running L2, we will only handle the exceptions
  1315. * specified above if L1 did not want them.
  1316. */
  1317. if (is_guest_mode(vcpu))
  1318. eb |= get_vmcs12(vcpu)->exception_bitmap;
  1319. vmcs_write32(EXCEPTION_BITMAP, eb);
  1320. }
  1321. static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
  1322. unsigned long entry, unsigned long exit)
  1323. {
  1324. vm_entry_controls_clearbit(vmx, entry);
  1325. vm_exit_controls_clearbit(vmx, exit);
  1326. }
  1327. static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
  1328. {
  1329. unsigned i;
  1330. struct msr_autoload *m = &vmx->msr_autoload;
  1331. switch (msr) {
  1332. case MSR_EFER:
  1333. if (cpu_has_load_ia32_efer) {
  1334. clear_atomic_switch_msr_special(vmx,
  1335. VM_ENTRY_LOAD_IA32_EFER,
  1336. VM_EXIT_LOAD_IA32_EFER);
  1337. return;
  1338. }
  1339. break;
  1340. case MSR_CORE_PERF_GLOBAL_CTRL:
  1341. if (cpu_has_load_perf_global_ctrl) {
  1342. clear_atomic_switch_msr_special(vmx,
  1343. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
  1344. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
  1345. return;
  1346. }
  1347. break;
  1348. }
  1349. for (i = 0; i < m->nr; ++i)
  1350. if (m->guest[i].index == msr)
  1351. break;
  1352. if (i == m->nr)
  1353. return;
  1354. --m->nr;
  1355. m->guest[i] = m->guest[m->nr];
  1356. m->host[i] = m->host[m->nr];
  1357. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
  1358. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
  1359. }
  1360. static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
  1361. unsigned long entry, unsigned long exit,
  1362. unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
  1363. u64 guest_val, u64 host_val)
  1364. {
  1365. vmcs_write64(guest_val_vmcs, guest_val);
  1366. vmcs_write64(host_val_vmcs, host_val);
  1367. vm_entry_controls_setbit(vmx, entry);
  1368. vm_exit_controls_setbit(vmx, exit);
  1369. }
  1370. static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
  1371. u64 guest_val, u64 host_val)
  1372. {
  1373. unsigned i;
  1374. struct msr_autoload *m = &vmx->msr_autoload;
  1375. switch (msr) {
  1376. case MSR_EFER:
  1377. if (cpu_has_load_ia32_efer) {
  1378. add_atomic_switch_msr_special(vmx,
  1379. VM_ENTRY_LOAD_IA32_EFER,
  1380. VM_EXIT_LOAD_IA32_EFER,
  1381. GUEST_IA32_EFER,
  1382. HOST_IA32_EFER,
  1383. guest_val, host_val);
  1384. return;
  1385. }
  1386. break;
  1387. case MSR_CORE_PERF_GLOBAL_CTRL:
  1388. if (cpu_has_load_perf_global_ctrl) {
  1389. add_atomic_switch_msr_special(vmx,
  1390. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
  1391. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
  1392. GUEST_IA32_PERF_GLOBAL_CTRL,
  1393. HOST_IA32_PERF_GLOBAL_CTRL,
  1394. guest_val, host_val);
  1395. return;
  1396. }
  1397. break;
  1398. }
  1399. for (i = 0; i < m->nr; ++i)
  1400. if (m->guest[i].index == msr)
  1401. break;
  1402. if (i == NR_AUTOLOAD_MSRS) {
  1403. printk_once(KERN_WARNING "Not enough msr switch entries. "
  1404. "Can't add msr %x\n", msr);
  1405. return;
  1406. } else if (i == m->nr) {
  1407. ++m->nr;
  1408. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
  1409. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
  1410. }
  1411. m->guest[i].index = msr;
  1412. m->guest[i].value = guest_val;
  1413. m->host[i].index = msr;
  1414. m->host[i].value = host_val;
  1415. }
  1416. static void reload_tss(void)
  1417. {
  1418. /*
  1419. * VT restores TR but not its size. Useless.
  1420. */
  1421. struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
  1422. struct desc_struct *descs;
  1423. descs = (void *)gdt->address;
  1424. descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
  1425. load_TR_desc();
  1426. }
  1427. static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
  1428. {
  1429. u64 guest_efer;
  1430. u64 ignore_bits;
  1431. guest_efer = vmx->vcpu.arch.efer;
  1432. /*
  1433. * NX is emulated; LMA and LME handled by hardware; SCE meaningless
  1434. * outside long mode
  1435. */
  1436. ignore_bits = EFER_NX | EFER_SCE;
  1437. #ifdef CONFIG_X86_64
  1438. ignore_bits |= EFER_LMA | EFER_LME;
  1439. /* SCE is meaningful only in long mode on Intel */
  1440. if (guest_efer & EFER_LMA)
  1441. ignore_bits &= ~(u64)EFER_SCE;
  1442. #endif
  1443. guest_efer &= ~ignore_bits;
  1444. guest_efer |= host_efer & ignore_bits;
  1445. vmx->guest_msrs[efer_offset].data = guest_efer;
  1446. vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
  1447. clear_atomic_switch_msr(vmx, MSR_EFER);
  1448. /* On ept, can't emulate nx, and must switch nx atomically */
  1449. if (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX)) {
  1450. guest_efer = vmx->vcpu.arch.efer;
  1451. if (!(guest_efer & EFER_LMA))
  1452. guest_efer &= ~EFER_LME;
  1453. add_atomic_switch_msr(vmx, MSR_EFER, guest_efer, host_efer);
  1454. return false;
  1455. }
  1456. return true;
  1457. }
  1458. static unsigned long segment_base(u16 selector)
  1459. {
  1460. struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
  1461. struct desc_struct *d;
  1462. unsigned long table_base;
  1463. unsigned long v;
  1464. if (!(selector & ~3))
  1465. return 0;
  1466. table_base = gdt->address;
  1467. if (selector & 4) { /* from ldt */
  1468. u16 ldt_selector = kvm_read_ldt();
  1469. if (!(ldt_selector & ~3))
  1470. return 0;
  1471. table_base = segment_base(ldt_selector);
  1472. }
  1473. d = (struct desc_struct *)(table_base + (selector & ~7));
  1474. v = get_desc_base(d);
  1475. #ifdef CONFIG_X86_64
  1476. if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
  1477. v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
  1478. #endif
  1479. return v;
  1480. }
  1481. static inline unsigned long kvm_read_tr_base(void)
  1482. {
  1483. u16 tr;
  1484. asm("str %0" : "=g"(tr));
  1485. return segment_base(tr);
  1486. }
  1487. static void vmx_save_host_state(struct kvm_vcpu *vcpu)
  1488. {
  1489. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1490. int i;
  1491. if (vmx->host_state.loaded)
  1492. return;
  1493. vmx->host_state.loaded = 1;
  1494. /*
  1495. * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
  1496. * allow segment selectors with cpl > 0 or ti == 1.
  1497. */
  1498. vmx->host_state.ldt_sel = kvm_read_ldt();
  1499. vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
  1500. savesegment(fs, vmx->host_state.fs_sel);
  1501. if (!(vmx->host_state.fs_sel & 7)) {
  1502. vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
  1503. vmx->host_state.fs_reload_needed = 0;
  1504. } else {
  1505. vmcs_write16(HOST_FS_SELECTOR, 0);
  1506. vmx->host_state.fs_reload_needed = 1;
  1507. }
  1508. savesegment(gs, vmx->host_state.gs_sel);
  1509. if (!(vmx->host_state.gs_sel & 7))
  1510. vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
  1511. else {
  1512. vmcs_write16(HOST_GS_SELECTOR, 0);
  1513. vmx->host_state.gs_ldt_reload_needed = 1;
  1514. }
  1515. #ifdef CONFIG_X86_64
  1516. savesegment(ds, vmx->host_state.ds_sel);
  1517. savesegment(es, vmx->host_state.es_sel);
  1518. #endif
  1519. #ifdef CONFIG_X86_64
  1520. vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
  1521. vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
  1522. #else
  1523. vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
  1524. vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
  1525. #endif
  1526. #ifdef CONFIG_X86_64
  1527. rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
  1528. if (is_long_mode(&vmx->vcpu))
  1529. wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
  1530. #endif
  1531. if (boot_cpu_has(X86_FEATURE_MPX))
  1532. rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
  1533. for (i = 0; i < vmx->save_nmsrs; ++i)
  1534. kvm_set_shared_msr(vmx->guest_msrs[i].index,
  1535. vmx->guest_msrs[i].data,
  1536. vmx->guest_msrs[i].mask);
  1537. }
  1538. static void __vmx_load_host_state(struct vcpu_vmx *vmx)
  1539. {
  1540. if (!vmx->host_state.loaded)
  1541. return;
  1542. ++vmx->vcpu.stat.host_state_reload;
  1543. vmx->host_state.loaded = 0;
  1544. #ifdef CONFIG_X86_64
  1545. if (is_long_mode(&vmx->vcpu))
  1546. rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
  1547. #endif
  1548. if (vmx->host_state.gs_ldt_reload_needed) {
  1549. kvm_load_ldt(vmx->host_state.ldt_sel);
  1550. #ifdef CONFIG_X86_64
  1551. load_gs_index(vmx->host_state.gs_sel);
  1552. #else
  1553. loadsegment(gs, vmx->host_state.gs_sel);
  1554. #endif
  1555. }
  1556. if (vmx->host_state.fs_reload_needed)
  1557. loadsegment(fs, vmx->host_state.fs_sel);
  1558. #ifdef CONFIG_X86_64
  1559. if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
  1560. loadsegment(ds, vmx->host_state.ds_sel);
  1561. loadsegment(es, vmx->host_state.es_sel);
  1562. }
  1563. #endif
  1564. reload_tss();
  1565. #ifdef CONFIG_X86_64
  1566. wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
  1567. #endif
  1568. if (vmx->host_state.msr_host_bndcfgs)
  1569. wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
  1570. /*
  1571. * If the FPU is not active (through the host task or
  1572. * the guest vcpu), then restore the cr0.TS bit.
  1573. */
  1574. if (!user_has_fpu() && !vmx->vcpu.guest_fpu_loaded)
  1575. stts();
  1576. load_gdt(this_cpu_ptr(&host_gdt));
  1577. }
  1578. static void vmx_load_host_state(struct vcpu_vmx *vmx)
  1579. {
  1580. preempt_disable();
  1581. __vmx_load_host_state(vmx);
  1582. preempt_enable();
  1583. }
  1584. /*
  1585. * Switches to specified vcpu, until a matching vcpu_put(), but assumes
  1586. * vcpu mutex is already taken.
  1587. */
  1588. static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  1589. {
  1590. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1591. u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
  1592. if (!vmm_exclusive)
  1593. kvm_cpu_vmxon(phys_addr);
  1594. else if (vmx->loaded_vmcs->cpu != cpu)
  1595. loaded_vmcs_clear(vmx->loaded_vmcs);
  1596. if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
  1597. per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
  1598. vmcs_load(vmx->loaded_vmcs->vmcs);
  1599. }
  1600. if (vmx->loaded_vmcs->cpu != cpu) {
  1601. struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
  1602. unsigned long sysenter_esp;
  1603. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  1604. local_irq_disable();
  1605. crash_disable_local_vmclear(cpu);
  1606. /*
  1607. * Read loaded_vmcs->cpu should be before fetching
  1608. * loaded_vmcs->loaded_vmcss_on_cpu_link.
  1609. * See the comments in __loaded_vmcs_clear().
  1610. */
  1611. smp_rmb();
  1612. list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
  1613. &per_cpu(loaded_vmcss_on_cpu, cpu));
  1614. crash_enable_local_vmclear(cpu);
  1615. local_irq_enable();
  1616. /*
  1617. * Linux uses per-cpu TSS and GDT, so set these when switching
  1618. * processors.
  1619. */
  1620. vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
  1621. vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
  1622. rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
  1623. vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
  1624. vmx->loaded_vmcs->cpu = cpu;
  1625. }
  1626. }
  1627. static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
  1628. {
  1629. __vmx_load_host_state(to_vmx(vcpu));
  1630. if (!vmm_exclusive) {
  1631. __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
  1632. vcpu->cpu = -1;
  1633. kvm_cpu_vmxoff();
  1634. }
  1635. }
  1636. static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
  1637. {
  1638. ulong cr0;
  1639. if (vcpu->fpu_active)
  1640. return;
  1641. vcpu->fpu_active = 1;
  1642. cr0 = vmcs_readl(GUEST_CR0);
  1643. cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
  1644. cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
  1645. vmcs_writel(GUEST_CR0, cr0);
  1646. update_exception_bitmap(vcpu);
  1647. vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
  1648. if (is_guest_mode(vcpu))
  1649. vcpu->arch.cr0_guest_owned_bits &=
  1650. ~get_vmcs12(vcpu)->cr0_guest_host_mask;
  1651. vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
  1652. }
  1653. static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
  1654. /*
  1655. * Return the cr0 value that a nested guest would read. This is a combination
  1656. * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
  1657. * its hypervisor (cr0_read_shadow).
  1658. */
  1659. static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
  1660. {
  1661. return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
  1662. (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
  1663. }
  1664. static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
  1665. {
  1666. return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
  1667. (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
  1668. }
  1669. static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
  1670. {
  1671. /* Note that there is no vcpu->fpu_active = 0 here. The caller must
  1672. * set this *before* calling this function.
  1673. */
  1674. vmx_decache_cr0_guest_bits(vcpu);
  1675. vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
  1676. update_exception_bitmap(vcpu);
  1677. vcpu->arch.cr0_guest_owned_bits = 0;
  1678. vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
  1679. if (is_guest_mode(vcpu)) {
  1680. /*
  1681. * L1's specified read shadow might not contain the TS bit,
  1682. * so now that we turned on shadowing of this bit, we need to
  1683. * set this bit of the shadow. Like in nested_vmx_run we need
  1684. * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet
  1685. * up-to-date here because we just decached cr0.TS (and we'll
  1686. * only update vmcs12->guest_cr0 on nested exit).
  1687. */
  1688. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  1689. vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) |
  1690. (vcpu->arch.cr0 & X86_CR0_TS);
  1691. vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
  1692. } else
  1693. vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
  1694. }
  1695. static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
  1696. {
  1697. unsigned long rflags, save_rflags;
  1698. if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
  1699. __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
  1700. rflags = vmcs_readl(GUEST_RFLAGS);
  1701. if (to_vmx(vcpu)->rmode.vm86_active) {
  1702. rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
  1703. save_rflags = to_vmx(vcpu)->rmode.save_rflags;
  1704. rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
  1705. }
  1706. to_vmx(vcpu)->rflags = rflags;
  1707. }
  1708. return to_vmx(vcpu)->rflags;
  1709. }
  1710. static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  1711. {
  1712. __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
  1713. to_vmx(vcpu)->rflags = rflags;
  1714. if (to_vmx(vcpu)->rmode.vm86_active) {
  1715. to_vmx(vcpu)->rmode.save_rflags = rflags;
  1716. rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
  1717. }
  1718. vmcs_writel(GUEST_RFLAGS, rflags);
  1719. }
  1720. static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
  1721. {
  1722. u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  1723. int ret = 0;
  1724. if (interruptibility & GUEST_INTR_STATE_STI)
  1725. ret |= KVM_X86_SHADOW_INT_STI;
  1726. if (interruptibility & GUEST_INTR_STATE_MOV_SS)
  1727. ret |= KVM_X86_SHADOW_INT_MOV_SS;
  1728. return ret;
  1729. }
  1730. static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
  1731. {
  1732. u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  1733. u32 interruptibility = interruptibility_old;
  1734. interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
  1735. if (mask & KVM_X86_SHADOW_INT_MOV_SS)
  1736. interruptibility |= GUEST_INTR_STATE_MOV_SS;
  1737. else if (mask & KVM_X86_SHADOW_INT_STI)
  1738. interruptibility |= GUEST_INTR_STATE_STI;
  1739. if ((interruptibility != interruptibility_old))
  1740. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
  1741. }
  1742. static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
  1743. {
  1744. unsigned long rip;
  1745. rip = kvm_rip_read(vcpu);
  1746. rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  1747. kvm_rip_write(vcpu, rip);
  1748. /* skipping an emulated instruction also counts */
  1749. vmx_set_interrupt_shadow(vcpu, 0);
  1750. }
  1751. /*
  1752. * KVM wants to inject page-faults which it got to the guest. This function
  1753. * checks whether in a nested guest, we need to inject them to L1 or L2.
  1754. */
  1755. static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned nr)
  1756. {
  1757. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  1758. if (!(vmcs12->exception_bitmap & (1u << nr)))
  1759. return 0;
  1760. nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
  1761. vmcs_read32(VM_EXIT_INTR_INFO),
  1762. vmcs_readl(EXIT_QUALIFICATION));
  1763. return 1;
  1764. }
  1765. static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
  1766. bool has_error_code, u32 error_code,
  1767. bool reinject)
  1768. {
  1769. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1770. u32 intr_info = nr | INTR_INFO_VALID_MASK;
  1771. if (!reinject && is_guest_mode(vcpu) &&
  1772. nested_vmx_check_exception(vcpu, nr))
  1773. return;
  1774. if (has_error_code) {
  1775. vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
  1776. intr_info |= INTR_INFO_DELIVER_CODE_MASK;
  1777. }
  1778. if (vmx->rmode.vm86_active) {
  1779. int inc_eip = 0;
  1780. if (kvm_exception_is_soft(nr))
  1781. inc_eip = vcpu->arch.event_exit_inst_len;
  1782. if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
  1783. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  1784. return;
  1785. }
  1786. if (kvm_exception_is_soft(nr)) {
  1787. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  1788. vmx->vcpu.arch.event_exit_inst_len);
  1789. intr_info |= INTR_TYPE_SOFT_EXCEPTION;
  1790. } else
  1791. intr_info |= INTR_TYPE_HARD_EXCEPTION;
  1792. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
  1793. }
  1794. static bool vmx_rdtscp_supported(void)
  1795. {
  1796. return cpu_has_vmx_rdtscp();
  1797. }
  1798. static bool vmx_invpcid_supported(void)
  1799. {
  1800. return cpu_has_vmx_invpcid() && enable_ept;
  1801. }
  1802. /*
  1803. * Swap MSR entry in host/guest MSR entry array.
  1804. */
  1805. static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
  1806. {
  1807. struct shared_msr_entry tmp;
  1808. tmp = vmx->guest_msrs[to];
  1809. vmx->guest_msrs[to] = vmx->guest_msrs[from];
  1810. vmx->guest_msrs[from] = tmp;
  1811. }
  1812. static void vmx_set_msr_bitmap(struct kvm_vcpu *vcpu)
  1813. {
  1814. unsigned long *msr_bitmap;
  1815. if (irqchip_in_kernel(vcpu->kvm) && apic_x2apic_mode(vcpu->arch.apic)) {
  1816. if (is_long_mode(vcpu))
  1817. msr_bitmap = vmx_msr_bitmap_longmode_x2apic;
  1818. else
  1819. msr_bitmap = vmx_msr_bitmap_legacy_x2apic;
  1820. } else {
  1821. if (is_long_mode(vcpu))
  1822. msr_bitmap = vmx_msr_bitmap_longmode;
  1823. else
  1824. msr_bitmap = vmx_msr_bitmap_legacy;
  1825. }
  1826. vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
  1827. }
  1828. /*
  1829. * Set up the vmcs to automatically save and restore system
  1830. * msrs. Don't touch the 64-bit msrs if the guest is in legacy
  1831. * mode, as fiddling with msrs is very expensive.
  1832. */
  1833. static void setup_msrs(struct vcpu_vmx *vmx)
  1834. {
  1835. int save_nmsrs, index;
  1836. save_nmsrs = 0;
  1837. #ifdef CONFIG_X86_64
  1838. if (is_long_mode(&vmx->vcpu)) {
  1839. index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
  1840. if (index >= 0)
  1841. move_msr_up(vmx, index, save_nmsrs++);
  1842. index = __find_msr_index(vmx, MSR_LSTAR);
  1843. if (index >= 0)
  1844. move_msr_up(vmx, index, save_nmsrs++);
  1845. index = __find_msr_index(vmx, MSR_CSTAR);
  1846. if (index >= 0)
  1847. move_msr_up(vmx, index, save_nmsrs++);
  1848. index = __find_msr_index(vmx, MSR_TSC_AUX);
  1849. if (index >= 0 && vmx->rdtscp_enabled)
  1850. move_msr_up(vmx, index, save_nmsrs++);
  1851. /*
  1852. * MSR_STAR is only needed on long mode guests, and only
  1853. * if efer.sce is enabled.
  1854. */
  1855. index = __find_msr_index(vmx, MSR_STAR);
  1856. if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
  1857. move_msr_up(vmx, index, save_nmsrs++);
  1858. }
  1859. #endif
  1860. index = __find_msr_index(vmx, MSR_EFER);
  1861. if (index >= 0 && update_transition_efer(vmx, index))
  1862. move_msr_up(vmx, index, save_nmsrs++);
  1863. vmx->save_nmsrs = save_nmsrs;
  1864. if (cpu_has_vmx_msr_bitmap())
  1865. vmx_set_msr_bitmap(&vmx->vcpu);
  1866. }
  1867. /*
  1868. * reads and returns guest's timestamp counter "register"
  1869. * guest_tsc = host_tsc + tsc_offset -- 21.3
  1870. */
  1871. static u64 guest_read_tsc(void)
  1872. {
  1873. u64 host_tsc, tsc_offset;
  1874. rdtscll(host_tsc);
  1875. tsc_offset = vmcs_read64(TSC_OFFSET);
  1876. return host_tsc + tsc_offset;
  1877. }
  1878. /*
  1879. * Like guest_read_tsc, but always returns L1's notion of the timestamp
  1880. * counter, even if a nested guest (L2) is currently running.
  1881. */
  1882. static u64 vmx_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
  1883. {
  1884. u64 tsc_offset;
  1885. tsc_offset = is_guest_mode(vcpu) ?
  1886. to_vmx(vcpu)->nested.vmcs01_tsc_offset :
  1887. vmcs_read64(TSC_OFFSET);
  1888. return host_tsc + tsc_offset;
  1889. }
  1890. /*
  1891. * Engage any workarounds for mis-matched TSC rates. Currently limited to
  1892. * software catchup for faster rates on slower CPUs.
  1893. */
  1894. static void vmx_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
  1895. {
  1896. if (!scale)
  1897. return;
  1898. if (user_tsc_khz > tsc_khz) {
  1899. vcpu->arch.tsc_catchup = 1;
  1900. vcpu->arch.tsc_always_catchup = 1;
  1901. } else
  1902. WARN(1, "user requested TSC rate below hardware speed\n");
  1903. }
  1904. static u64 vmx_read_tsc_offset(struct kvm_vcpu *vcpu)
  1905. {
  1906. return vmcs_read64(TSC_OFFSET);
  1907. }
  1908. /*
  1909. * writes 'offset' into guest's timestamp counter offset register
  1910. */
  1911. static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
  1912. {
  1913. if (is_guest_mode(vcpu)) {
  1914. /*
  1915. * We're here if L1 chose not to trap WRMSR to TSC. According
  1916. * to the spec, this should set L1's TSC; The offset that L1
  1917. * set for L2 remains unchanged, and still needs to be added
  1918. * to the newly set TSC to get L2's TSC.
  1919. */
  1920. struct vmcs12 *vmcs12;
  1921. to_vmx(vcpu)->nested.vmcs01_tsc_offset = offset;
  1922. /* recalculate vmcs02.TSC_OFFSET: */
  1923. vmcs12 = get_vmcs12(vcpu);
  1924. vmcs_write64(TSC_OFFSET, offset +
  1925. (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
  1926. vmcs12->tsc_offset : 0));
  1927. } else {
  1928. trace_kvm_write_tsc_offset(vcpu->vcpu_id,
  1929. vmcs_read64(TSC_OFFSET), offset);
  1930. vmcs_write64(TSC_OFFSET, offset);
  1931. }
  1932. }
  1933. static void vmx_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment, bool host)
  1934. {
  1935. u64 offset = vmcs_read64(TSC_OFFSET);
  1936. vmcs_write64(TSC_OFFSET, offset + adjustment);
  1937. if (is_guest_mode(vcpu)) {
  1938. /* Even when running L2, the adjustment needs to apply to L1 */
  1939. to_vmx(vcpu)->nested.vmcs01_tsc_offset += adjustment;
  1940. } else
  1941. trace_kvm_write_tsc_offset(vcpu->vcpu_id, offset,
  1942. offset + adjustment);
  1943. }
  1944. static u64 vmx_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
  1945. {
  1946. return target_tsc - native_read_tsc();
  1947. }
  1948. static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
  1949. {
  1950. struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
  1951. return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
  1952. }
  1953. /*
  1954. * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
  1955. * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
  1956. * all guests if the "nested" module option is off, and can also be disabled
  1957. * for a single guest by disabling its VMX cpuid bit.
  1958. */
  1959. static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
  1960. {
  1961. return nested && guest_cpuid_has_vmx(vcpu);
  1962. }
  1963. /*
  1964. * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
  1965. * returned for the various VMX controls MSRs when nested VMX is enabled.
  1966. * The same values should also be used to verify that vmcs12 control fields are
  1967. * valid during nested entry from L1 to L2.
  1968. * Each of these control msrs has a low and high 32-bit half: A low bit is on
  1969. * if the corresponding bit in the (32-bit) control field *must* be on, and a
  1970. * bit in the high half is on if the corresponding bit in the control field
  1971. * may be on. See also vmx_control_verify().
  1972. * TODO: allow these variables to be modified (downgraded) by module options
  1973. * or other means.
  1974. */
  1975. static u32 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high;
  1976. static u32 nested_vmx_true_procbased_ctls_low;
  1977. static u32 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high;
  1978. static u32 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high;
  1979. static u32 nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high;
  1980. static u32 nested_vmx_true_exit_ctls_low;
  1981. static u32 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high;
  1982. static u32 nested_vmx_true_entry_ctls_low;
  1983. static u32 nested_vmx_misc_low, nested_vmx_misc_high;
  1984. static u32 nested_vmx_ept_caps;
  1985. static __init void nested_vmx_setup_ctls_msrs(void)
  1986. {
  1987. /*
  1988. * Note that as a general rule, the high half of the MSRs (bits in
  1989. * the control fields which may be 1) should be initialized by the
  1990. * intersection of the underlying hardware's MSR (i.e., features which
  1991. * can be supported) and the list of features we want to expose -
  1992. * because they are known to be properly supported in our code.
  1993. * Also, usually, the low half of the MSRs (bits which must be 1) can
  1994. * be set to 0, meaning that L1 may turn off any of these bits. The
  1995. * reason is that if one of these bits is necessary, it will appear
  1996. * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
  1997. * fields of vmcs01 and vmcs02, will turn these bits off - and
  1998. * nested_vmx_exit_handled() will not pass related exits to L1.
  1999. * These rules have exceptions below.
  2000. */
  2001. /* pin-based controls */
  2002. rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
  2003. nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high);
  2004. nested_vmx_pinbased_ctls_low |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  2005. nested_vmx_pinbased_ctls_high &= PIN_BASED_EXT_INTR_MASK |
  2006. PIN_BASED_NMI_EXITING | PIN_BASED_VIRTUAL_NMIS;
  2007. nested_vmx_pinbased_ctls_high |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
  2008. PIN_BASED_VMX_PREEMPTION_TIMER;
  2009. /* exit controls */
  2010. rdmsr(MSR_IA32_VMX_EXIT_CTLS,
  2011. nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high);
  2012. nested_vmx_exit_ctls_low = VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
  2013. nested_vmx_exit_ctls_high &=
  2014. #ifdef CONFIG_X86_64
  2015. VM_EXIT_HOST_ADDR_SPACE_SIZE |
  2016. #endif
  2017. VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
  2018. nested_vmx_exit_ctls_high |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
  2019. VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
  2020. VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
  2021. if (vmx_mpx_supported())
  2022. nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
  2023. /* We support free control of debug control saving. */
  2024. nested_vmx_true_exit_ctls_low = nested_vmx_exit_ctls_low &
  2025. ~VM_EXIT_SAVE_DEBUG_CONTROLS;
  2026. /* entry controls */
  2027. rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
  2028. nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high);
  2029. nested_vmx_entry_ctls_low = VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
  2030. nested_vmx_entry_ctls_high &=
  2031. #ifdef CONFIG_X86_64
  2032. VM_ENTRY_IA32E_MODE |
  2033. #endif
  2034. VM_ENTRY_LOAD_IA32_PAT;
  2035. nested_vmx_entry_ctls_high |= (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR |
  2036. VM_ENTRY_LOAD_IA32_EFER);
  2037. if (vmx_mpx_supported())
  2038. nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
  2039. /* We support free control of debug control loading. */
  2040. nested_vmx_true_entry_ctls_low = nested_vmx_entry_ctls_low &
  2041. ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
  2042. /* cpu-based controls */
  2043. rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
  2044. nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high);
  2045. nested_vmx_procbased_ctls_low = CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  2046. nested_vmx_procbased_ctls_high &=
  2047. CPU_BASED_VIRTUAL_INTR_PENDING |
  2048. CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
  2049. CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
  2050. CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
  2051. CPU_BASED_CR3_STORE_EXITING |
  2052. #ifdef CONFIG_X86_64
  2053. CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
  2054. #endif
  2055. CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
  2056. CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_EXITING |
  2057. CPU_BASED_RDPMC_EXITING | CPU_BASED_RDTSC_EXITING |
  2058. CPU_BASED_PAUSE_EXITING | CPU_BASED_TPR_SHADOW |
  2059. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  2060. /*
  2061. * We can allow some features even when not supported by the
  2062. * hardware. For example, L1 can specify an MSR bitmap - and we
  2063. * can use it to avoid exits to L1 - even when L0 runs L2
  2064. * without MSR bitmaps.
  2065. */
  2066. nested_vmx_procbased_ctls_high |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
  2067. CPU_BASED_USE_MSR_BITMAPS;
  2068. /* We support free control of CR3 access interception. */
  2069. nested_vmx_true_procbased_ctls_low = nested_vmx_procbased_ctls_low &
  2070. ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
  2071. /* secondary cpu-based controls */
  2072. rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
  2073. nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high);
  2074. nested_vmx_secondary_ctls_low = 0;
  2075. nested_vmx_secondary_ctls_high &=
  2076. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  2077. SECONDARY_EXEC_UNRESTRICTED_GUEST |
  2078. SECONDARY_EXEC_WBINVD_EXITING;
  2079. if (enable_ept) {
  2080. /* nested EPT: emulate EPT also to L1 */
  2081. nested_vmx_secondary_ctls_high |= SECONDARY_EXEC_ENABLE_EPT;
  2082. nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
  2083. VMX_EPTP_WB_BIT | VMX_EPT_2MB_PAGE_BIT |
  2084. VMX_EPT_INVEPT_BIT;
  2085. nested_vmx_ept_caps &= vmx_capability.ept;
  2086. /*
  2087. * For nested guests, we don't do anything specific
  2088. * for single context invalidation. Hence, only advertise
  2089. * support for global context invalidation.
  2090. */
  2091. nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT;
  2092. } else
  2093. nested_vmx_ept_caps = 0;
  2094. /* miscellaneous data */
  2095. rdmsr(MSR_IA32_VMX_MISC, nested_vmx_misc_low, nested_vmx_misc_high);
  2096. nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA;
  2097. nested_vmx_misc_low |= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
  2098. VMX_MISC_ACTIVITY_HLT;
  2099. nested_vmx_misc_high = 0;
  2100. }
  2101. static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
  2102. {
  2103. /*
  2104. * Bits 0 in high must be 0, and bits 1 in low must be 1.
  2105. */
  2106. return ((control & high) | low) == control;
  2107. }
  2108. static inline u64 vmx_control_msr(u32 low, u32 high)
  2109. {
  2110. return low | ((u64)high << 32);
  2111. }
  2112. /* Returns 0 on success, non-0 otherwise. */
  2113. static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
  2114. {
  2115. switch (msr_index) {
  2116. case MSR_IA32_VMX_BASIC:
  2117. /*
  2118. * This MSR reports some information about VMX support. We
  2119. * should return information about the VMX we emulate for the
  2120. * guest, and the VMCS structure we give it - not about the
  2121. * VMX support of the underlying hardware.
  2122. */
  2123. *pdata = VMCS12_REVISION | VMX_BASIC_TRUE_CTLS |
  2124. ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
  2125. (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
  2126. break;
  2127. case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
  2128. case MSR_IA32_VMX_PINBASED_CTLS:
  2129. *pdata = vmx_control_msr(nested_vmx_pinbased_ctls_low,
  2130. nested_vmx_pinbased_ctls_high);
  2131. break;
  2132. case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
  2133. *pdata = vmx_control_msr(nested_vmx_true_procbased_ctls_low,
  2134. nested_vmx_procbased_ctls_high);
  2135. break;
  2136. case MSR_IA32_VMX_PROCBASED_CTLS:
  2137. *pdata = vmx_control_msr(nested_vmx_procbased_ctls_low,
  2138. nested_vmx_procbased_ctls_high);
  2139. break;
  2140. case MSR_IA32_VMX_TRUE_EXIT_CTLS:
  2141. *pdata = vmx_control_msr(nested_vmx_true_exit_ctls_low,
  2142. nested_vmx_exit_ctls_high);
  2143. break;
  2144. case MSR_IA32_VMX_EXIT_CTLS:
  2145. *pdata = vmx_control_msr(nested_vmx_exit_ctls_low,
  2146. nested_vmx_exit_ctls_high);
  2147. break;
  2148. case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
  2149. *pdata = vmx_control_msr(nested_vmx_true_entry_ctls_low,
  2150. nested_vmx_entry_ctls_high);
  2151. break;
  2152. case MSR_IA32_VMX_ENTRY_CTLS:
  2153. *pdata = vmx_control_msr(nested_vmx_entry_ctls_low,
  2154. nested_vmx_entry_ctls_high);
  2155. break;
  2156. case MSR_IA32_VMX_MISC:
  2157. *pdata = vmx_control_msr(nested_vmx_misc_low,
  2158. nested_vmx_misc_high);
  2159. break;
  2160. /*
  2161. * These MSRs specify bits which the guest must keep fixed (on or off)
  2162. * while L1 is in VMXON mode (in L1's root mode, or running an L2).
  2163. * We picked the standard core2 setting.
  2164. */
  2165. #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
  2166. #define VMXON_CR4_ALWAYSON X86_CR4_VMXE
  2167. case MSR_IA32_VMX_CR0_FIXED0:
  2168. *pdata = VMXON_CR0_ALWAYSON;
  2169. break;
  2170. case MSR_IA32_VMX_CR0_FIXED1:
  2171. *pdata = -1ULL;
  2172. break;
  2173. case MSR_IA32_VMX_CR4_FIXED0:
  2174. *pdata = VMXON_CR4_ALWAYSON;
  2175. break;
  2176. case MSR_IA32_VMX_CR4_FIXED1:
  2177. *pdata = -1ULL;
  2178. break;
  2179. case MSR_IA32_VMX_VMCS_ENUM:
  2180. *pdata = 0x2e; /* highest index: VMX_PREEMPTION_TIMER_VALUE */
  2181. break;
  2182. case MSR_IA32_VMX_PROCBASED_CTLS2:
  2183. *pdata = vmx_control_msr(nested_vmx_secondary_ctls_low,
  2184. nested_vmx_secondary_ctls_high);
  2185. break;
  2186. case MSR_IA32_VMX_EPT_VPID_CAP:
  2187. /* Currently, no nested vpid support */
  2188. *pdata = nested_vmx_ept_caps;
  2189. break;
  2190. default:
  2191. return 1;
  2192. }
  2193. return 0;
  2194. }
  2195. /*
  2196. * Reads an msr value (of 'msr_index') into 'pdata'.
  2197. * Returns 0 on success, non-0 otherwise.
  2198. * Assumes vcpu_load() was already called.
  2199. */
  2200. static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
  2201. {
  2202. u64 data;
  2203. struct shared_msr_entry *msr;
  2204. if (!pdata) {
  2205. printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
  2206. return -EINVAL;
  2207. }
  2208. switch (msr_index) {
  2209. #ifdef CONFIG_X86_64
  2210. case MSR_FS_BASE:
  2211. data = vmcs_readl(GUEST_FS_BASE);
  2212. break;
  2213. case MSR_GS_BASE:
  2214. data = vmcs_readl(GUEST_GS_BASE);
  2215. break;
  2216. case MSR_KERNEL_GS_BASE:
  2217. vmx_load_host_state(to_vmx(vcpu));
  2218. data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
  2219. break;
  2220. #endif
  2221. case MSR_EFER:
  2222. return kvm_get_msr_common(vcpu, msr_index, pdata);
  2223. case MSR_IA32_TSC:
  2224. data = guest_read_tsc();
  2225. break;
  2226. case MSR_IA32_SYSENTER_CS:
  2227. data = vmcs_read32(GUEST_SYSENTER_CS);
  2228. break;
  2229. case MSR_IA32_SYSENTER_EIP:
  2230. data = vmcs_readl(GUEST_SYSENTER_EIP);
  2231. break;
  2232. case MSR_IA32_SYSENTER_ESP:
  2233. data = vmcs_readl(GUEST_SYSENTER_ESP);
  2234. break;
  2235. case MSR_IA32_BNDCFGS:
  2236. if (!vmx_mpx_supported())
  2237. return 1;
  2238. data = vmcs_read64(GUEST_BNDCFGS);
  2239. break;
  2240. case MSR_IA32_FEATURE_CONTROL:
  2241. if (!nested_vmx_allowed(vcpu))
  2242. return 1;
  2243. data = to_vmx(vcpu)->nested.msr_ia32_feature_control;
  2244. break;
  2245. case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
  2246. if (!nested_vmx_allowed(vcpu))
  2247. return 1;
  2248. return vmx_get_vmx_msr(vcpu, msr_index, pdata);
  2249. case MSR_TSC_AUX:
  2250. if (!to_vmx(vcpu)->rdtscp_enabled)
  2251. return 1;
  2252. /* Otherwise falls through */
  2253. default:
  2254. msr = find_msr_entry(to_vmx(vcpu), msr_index);
  2255. if (msr) {
  2256. data = msr->data;
  2257. break;
  2258. }
  2259. return kvm_get_msr_common(vcpu, msr_index, pdata);
  2260. }
  2261. *pdata = data;
  2262. return 0;
  2263. }
  2264. static void vmx_leave_nested(struct kvm_vcpu *vcpu);
  2265. /*
  2266. * Writes msr value into into the appropriate "register".
  2267. * Returns 0 on success, non-0 otherwise.
  2268. * Assumes vcpu_load() was already called.
  2269. */
  2270. static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  2271. {
  2272. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2273. struct shared_msr_entry *msr;
  2274. int ret = 0;
  2275. u32 msr_index = msr_info->index;
  2276. u64 data = msr_info->data;
  2277. switch (msr_index) {
  2278. case MSR_EFER:
  2279. ret = kvm_set_msr_common(vcpu, msr_info);
  2280. break;
  2281. #ifdef CONFIG_X86_64
  2282. case MSR_FS_BASE:
  2283. vmx_segment_cache_clear(vmx);
  2284. vmcs_writel(GUEST_FS_BASE, data);
  2285. break;
  2286. case MSR_GS_BASE:
  2287. vmx_segment_cache_clear(vmx);
  2288. vmcs_writel(GUEST_GS_BASE, data);
  2289. break;
  2290. case MSR_KERNEL_GS_BASE:
  2291. vmx_load_host_state(vmx);
  2292. vmx->msr_guest_kernel_gs_base = data;
  2293. break;
  2294. #endif
  2295. case MSR_IA32_SYSENTER_CS:
  2296. vmcs_write32(GUEST_SYSENTER_CS, data);
  2297. break;
  2298. case MSR_IA32_SYSENTER_EIP:
  2299. vmcs_writel(GUEST_SYSENTER_EIP, data);
  2300. break;
  2301. case MSR_IA32_SYSENTER_ESP:
  2302. vmcs_writel(GUEST_SYSENTER_ESP, data);
  2303. break;
  2304. case MSR_IA32_BNDCFGS:
  2305. if (!vmx_mpx_supported())
  2306. return 1;
  2307. vmcs_write64(GUEST_BNDCFGS, data);
  2308. break;
  2309. case MSR_IA32_TSC:
  2310. kvm_write_tsc(vcpu, msr_info);
  2311. break;
  2312. case MSR_IA32_CR_PAT:
  2313. if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
  2314. if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
  2315. return 1;
  2316. vmcs_write64(GUEST_IA32_PAT, data);
  2317. vcpu->arch.pat = data;
  2318. break;
  2319. }
  2320. ret = kvm_set_msr_common(vcpu, msr_info);
  2321. break;
  2322. case MSR_IA32_TSC_ADJUST:
  2323. ret = kvm_set_msr_common(vcpu, msr_info);
  2324. break;
  2325. case MSR_IA32_FEATURE_CONTROL:
  2326. if (!nested_vmx_allowed(vcpu) ||
  2327. (to_vmx(vcpu)->nested.msr_ia32_feature_control &
  2328. FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
  2329. return 1;
  2330. vmx->nested.msr_ia32_feature_control = data;
  2331. if (msr_info->host_initiated && data == 0)
  2332. vmx_leave_nested(vcpu);
  2333. break;
  2334. case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
  2335. return 1; /* they are read-only */
  2336. case MSR_TSC_AUX:
  2337. if (!vmx->rdtscp_enabled)
  2338. return 1;
  2339. /* Check reserved bit, higher 32 bits should be zero */
  2340. if ((data >> 32) != 0)
  2341. return 1;
  2342. /* Otherwise falls through */
  2343. default:
  2344. msr = find_msr_entry(vmx, msr_index);
  2345. if (msr) {
  2346. msr->data = data;
  2347. if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
  2348. preempt_disable();
  2349. kvm_set_shared_msr(msr->index, msr->data,
  2350. msr->mask);
  2351. preempt_enable();
  2352. }
  2353. break;
  2354. }
  2355. ret = kvm_set_msr_common(vcpu, msr_info);
  2356. }
  2357. return ret;
  2358. }
  2359. static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
  2360. {
  2361. __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
  2362. switch (reg) {
  2363. case VCPU_REGS_RSP:
  2364. vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
  2365. break;
  2366. case VCPU_REGS_RIP:
  2367. vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
  2368. break;
  2369. case VCPU_EXREG_PDPTR:
  2370. if (enable_ept)
  2371. ept_save_pdptrs(vcpu);
  2372. break;
  2373. default:
  2374. break;
  2375. }
  2376. }
  2377. static __init int cpu_has_kvm_support(void)
  2378. {
  2379. return cpu_has_vmx();
  2380. }
  2381. static __init int vmx_disabled_by_bios(void)
  2382. {
  2383. u64 msr;
  2384. rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
  2385. if (msr & FEATURE_CONTROL_LOCKED) {
  2386. /* launched w/ TXT and VMX disabled */
  2387. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
  2388. && tboot_enabled())
  2389. return 1;
  2390. /* launched w/o TXT and VMX only enabled w/ TXT */
  2391. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
  2392. && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
  2393. && !tboot_enabled()) {
  2394. printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
  2395. "activate TXT before enabling KVM\n");
  2396. return 1;
  2397. }
  2398. /* launched w/o TXT and VMX disabled */
  2399. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
  2400. && !tboot_enabled())
  2401. return 1;
  2402. }
  2403. return 0;
  2404. }
  2405. static void kvm_cpu_vmxon(u64 addr)
  2406. {
  2407. asm volatile (ASM_VMX_VMXON_RAX
  2408. : : "a"(&addr), "m"(addr)
  2409. : "memory", "cc");
  2410. }
  2411. static int hardware_enable(void)
  2412. {
  2413. int cpu = raw_smp_processor_id();
  2414. u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
  2415. u64 old, test_bits;
  2416. if (read_cr4() & X86_CR4_VMXE)
  2417. return -EBUSY;
  2418. INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
  2419. /*
  2420. * Now we can enable the vmclear operation in kdump
  2421. * since the loaded_vmcss_on_cpu list on this cpu
  2422. * has been initialized.
  2423. *
  2424. * Though the cpu is not in VMX operation now, there
  2425. * is no problem to enable the vmclear operation
  2426. * for the loaded_vmcss_on_cpu list is empty!
  2427. */
  2428. crash_enable_local_vmclear(cpu);
  2429. rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
  2430. test_bits = FEATURE_CONTROL_LOCKED;
  2431. test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  2432. if (tboot_enabled())
  2433. test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
  2434. if ((old & test_bits) != test_bits) {
  2435. /* enable and lock */
  2436. wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
  2437. }
  2438. write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
  2439. if (vmm_exclusive) {
  2440. kvm_cpu_vmxon(phys_addr);
  2441. ept_sync_global();
  2442. }
  2443. native_store_gdt(this_cpu_ptr(&host_gdt));
  2444. return 0;
  2445. }
  2446. static void vmclear_local_loaded_vmcss(void)
  2447. {
  2448. int cpu = raw_smp_processor_id();
  2449. struct loaded_vmcs *v, *n;
  2450. list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
  2451. loaded_vmcss_on_cpu_link)
  2452. __loaded_vmcs_clear(v);
  2453. }
  2454. /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
  2455. * tricks.
  2456. */
  2457. static void kvm_cpu_vmxoff(void)
  2458. {
  2459. asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
  2460. }
  2461. static void hardware_disable(void)
  2462. {
  2463. if (vmm_exclusive) {
  2464. vmclear_local_loaded_vmcss();
  2465. kvm_cpu_vmxoff();
  2466. }
  2467. write_cr4(read_cr4() & ~X86_CR4_VMXE);
  2468. }
  2469. static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
  2470. u32 msr, u32 *result)
  2471. {
  2472. u32 vmx_msr_low, vmx_msr_high;
  2473. u32 ctl = ctl_min | ctl_opt;
  2474. rdmsr(msr, vmx_msr_low, vmx_msr_high);
  2475. ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
  2476. ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
  2477. /* Ensure minimum (required) set of control bits are supported. */
  2478. if (ctl_min & ~ctl)
  2479. return -EIO;
  2480. *result = ctl;
  2481. return 0;
  2482. }
  2483. static __init bool allow_1_setting(u32 msr, u32 ctl)
  2484. {
  2485. u32 vmx_msr_low, vmx_msr_high;
  2486. rdmsr(msr, vmx_msr_low, vmx_msr_high);
  2487. return vmx_msr_high & ctl;
  2488. }
  2489. static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
  2490. {
  2491. u32 vmx_msr_low, vmx_msr_high;
  2492. u32 min, opt, min2, opt2;
  2493. u32 _pin_based_exec_control = 0;
  2494. u32 _cpu_based_exec_control = 0;
  2495. u32 _cpu_based_2nd_exec_control = 0;
  2496. u32 _vmexit_control = 0;
  2497. u32 _vmentry_control = 0;
  2498. min = CPU_BASED_HLT_EXITING |
  2499. #ifdef CONFIG_X86_64
  2500. CPU_BASED_CR8_LOAD_EXITING |
  2501. CPU_BASED_CR8_STORE_EXITING |
  2502. #endif
  2503. CPU_BASED_CR3_LOAD_EXITING |
  2504. CPU_BASED_CR3_STORE_EXITING |
  2505. CPU_BASED_USE_IO_BITMAPS |
  2506. CPU_BASED_MOV_DR_EXITING |
  2507. CPU_BASED_USE_TSC_OFFSETING |
  2508. CPU_BASED_MWAIT_EXITING |
  2509. CPU_BASED_MONITOR_EXITING |
  2510. CPU_BASED_INVLPG_EXITING |
  2511. CPU_BASED_RDPMC_EXITING;
  2512. opt = CPU_BASED_TPR_SHADOW |
  2513. CPU_BASED_USE_MSR_BITMAPS |
  2514. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  2515. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
  2516. &_cpu_based_exec_control) < 0)
  2517. return -EIO;
  2518. #ifdef CONFIG_X86_64
  2519. if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
  2520. _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
  2521. ~CPU_BASED_CR8_STORE_EXITING;
  2522. #endif
  2523. if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
  2524. min2 = 0;
  2525. opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  2526. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  2527. SECONDARY_EXEC_WBINVD_EXITING |
  2528. SECONDARY_EXEC_ENABLE_VPID |
  2529. SECONDARY_EXEC_ENABLE_EPT |
  2530. SECONDARY_EXEC_UNRESTRICTED_GUEST |
  2531. SECONDARY_EXEC_PAUSE_LOOP_EXITING |
  2532. SECONDARY_EXEC_RDTSCP |
  2533. SECONDARY_EXEC_ENABLE_INVPCID |
  2534. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  2535. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
  2536. SECONDARY_EXEC_SHADOW_VMCS;
  2537. if (adjust_vmx_controls(min2, opt2,
  2538. MSR_IA32_VMX_PROCBASED_CTLS2,
  2539. &_cpu_based_2nd_exec_control) < 0)
  2540. return -EIO;
  2541. }
  2542. #ifndef CONFIG_X86_64
  2543. if (!(_cpu_based_2nd_exec_control &
  2544. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
  2545. _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
  2546. #endif
  2547. if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
  2548. _cpu_based_2nd_exec_control &= ~(
  2549. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  2550. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  2551. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  2552. if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
  2553. /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
  2554. enabled */
  2555. _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
  2556. CPU_BASED_CR3_STORE_EXITING |
  2557. CPU_BASED_INVLPG_EXITING);
  2558. rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
  2559. vmx_capability.ept, vmx_capability.vpid);
  2560. }
  2561. min = VM_EXIT_SAVE_DEBUG_CONTROLS;
  2562. #ifdef CONFIG_X86_64
  2563. min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
  2564. #endif
  2565. opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
  2566. VM_EXIT_ACK_INTR_ON_EXIT | VM_EXIT_CLEAR_BNDCFGS;
  2567. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
  2568. &_vmexit_control) < 0)
  2569. return -EIO;
  2570. min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
  2571. opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR;
  2572. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
  2573. &_pin_based_exec_control) < 0)
  2574. return -EIO;
  2575. if (!(_cpu_based_2nd_exec_control &
  2576. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) ||
  2577. !(_vmexit_control & VM_EXIT_ACK_INTR_ON_EXIT))
  2578. _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
  2579. min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
  2580. opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
  2581. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
  2582. &_vmentry_control) < 0)
  2583. return -EIO;
  2584. rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
  2585. /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
  2586. if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
  2587. return -EIO;
  2588. #ifdef CONFIG_X86_64
  2589. /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
  2590. if (vmx_msr_high & (1u<<16))
  2591. return -EIO;
  2592. #endif
  2593. /* Require Write-Back (WB) memory type for VMCS accesses. */
  2594. if (((vmx_msr_high >> 18) & 15) != 6)
  2595. return -EIO;
  2596. vmcs_conf->size = vmx_msr_high & 0x1fff;
  2597. vmcs_conf->order = get_order(vmcs_config.size);
  2598. vmcs_conf->revision_id = vmx_msr_low;
  2599. vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
  2600. vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
  2601. vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
  2602. vmcs_conf->vmexit_ctrl = _vmexit_control;
  2603. vmcs_conf->vmentry_ctrl = _vmentry_control;
  2604. cpu_has_load_ia32_efer =
  2605. allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
  2606. VM_ENTRY_LOAD_IA32_EFER)
  2607. && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
  2608. VM_EXIT_LOAD_IA32_EFER);
  2609. cpu_has_load_perf_global_ctrl =
  2610. allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
  2611. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
  2612. && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
  2613. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
  2614. /*
  2615. * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
  2616. * but due to arrata below it can't be used. Workaround is to use
  2617. * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
  2618. *
  2619. * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
  2620. *
  2621. * AAK155 (model 26)
  2622. * AAP115 (model 30)
  2623. * AAT100 (model 37)
  2624. * BC86,AAY89,BD102 (model 44)
  2625. * BA97 (model 46)
  2626. *
  2627. */
  2628. if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
  2629. switch (boot_cpu_data.x86_model) {
  2630. case 26:
  2631. case 30:
  2632. case 37:
  2633. case 44:
  2634. case 46:
  2635. cpu_has_load_perf_global_ctrl = false;
  2636. printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
  2637. "does not work properly. Using workaround\n");
  2638. break;
  2639. default:
  2640. break;
  2641. }
  2642. }
  2643. return 0;
  2644. }
  2645. static struct vmcs *alloc_vmcs_cpu(int cpu)
  2646. {
  2647. int node = cpu_to_node(cpu);
  2648. struct page *pages;
  2649. struct vmcs *vmcs;
  2650. pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order);
  2651. if (!pages)
  2652. return NULL;
  2653. vmcs = page_address(pages);
  2654. memset(vmcs, 0, vmcs_config.size);
  2655. vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
  2656. return vmcs;
  2657. }
  2658. static struct vmcs *alloc_vmcs(void)
  2659. {
  2660. return alloc_vmcs_cpu(raw_smp_processor_id());
  2661. }
  2662. static void free_vmcs(struct vmcs *vmcs)
  2663. {
  2664. free_pages((unsigned long)vmcs, vmcs_config.order);
  2665. }
  2666. /*
  2667. * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
  2668. */
  2669. static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
  2670. {
  2671. if (!loaded_vmcs->vmcs)
  2672. return;
  2673. loaded_vmcs_clear(loaded_vmcs);
  2674. free_vmcs(loaded_vmcs->vmcs);
  2675. loaded_vmcs->vmcs = NULL;
  2676. }
  2677. static void free_kvm_area(void)
  2678. {
  2679. int cpu;
  2680. for_each_possible_cpu(cpu) {
  2681. free_vmcs(per_cpu(vmxarea, cpu));
  2682. per_cpu(vmxarea, cpu) = NULL;
  2683. }
  2684. }
  2685. static void init_vmcs_shadow_fields(void)
  2686. {
  2687. int i, j;
  2688. /* No checks for read only fields yet */
  2689. for (i = j = 0; i < max_shadow_read_write_fields; i++) {
  2690. switch (shadow_read_write_fields[i]) {
  2691. case GUEST_BNDCFGS:
  2692. if (!vmx_mpx_supported())
  2693. continue;
  2694. break;
  2695. default:
  2696. break;
  2697. }
  2698. if (j < i)
  2699. shadow_read_write_fields[j] =
  2700. shadow_read_write_fields[i];
  2701. j++;
  2702. }
  2703. max_shadow_read_write_fields = j;
  2704. /* shadowed fields guest access without vmexit */
  2705. for (i = 0; i < max_shadow_read_write_fields; i++) {
  2706. clear_bit(shadow_read_write_fields[i],
  2707. vmx_vmwrite_bitmap);
  2708. clear_bit(shadow_read_write_fields[i],
  2709. vmx_vmread_bitmap);
  2710. }
  2711. for (i = 0; i < max_shadow_read_only_fields; i++)
  2712. clear_bit(shadow_read_only_fields[i],
  2713. vmx_vmread_bitmap);
  2714. }
  2715. static __init int alloc_kvm_area(void)
  2716. {
  2717. int cpu;
  2718. for_each_possible_cpu(cpu) {
  2719. struct vmcs *vmcs;
  2720. vmcs = alloc_vmcs_cpu(cpu);
  2721. if (!vmcs) {
  2722. free_kvm_area();
  2723. return -ENOMEM;
  2724. }
  2725. per_cpu(vmxarea, cpu) = vmcs;
  2726. }
  2727. return 0;
  2728. }
  2729. static __init int hardware_setup(void)
  2730. {
  2731. if (setup_vmcs_config(&vmcs_config) < 0)
  2732. return -EIO;
  2733. if (boot_cpu_has(X86_FEATURE_NX))
  2734. kvm_enable_efer_bits(EFER_NX);
  2735. if (!cpu_has_vmx_vpid())
  2736. enable_vpid = 0;
  2737. if (!cpu_has_vmx_shadow_vmcs())
  2738. enable_shadow_vmcs = 0;
  2739. if (enable_shadow_vmcs)
  2740. init_vmcs_shadow_fields();
  2741. if (!cpu_has_vmx_ept() ||
  2742. !cpu_has_vmx_ept_4levels()) {
  2743. enable_ept = 0;
  2744. enable_unrestricted_guest = 0;
  2745. enable_ept_ad_bits = 0;
  2746. }
  2747. if (!cpu_has_vmx_ept_ad_bits())
  2748. enable_ept_ad_bits = 0;
  2749. if (!cpu_has_vmx_unrestricted_guest())
  2750. enable_unrestricted_guest = 0;
  2751. if (!cpu_has_vmx_flexpriority()) {
  2752. flexpriority_enabled = 0;
  2753. /*
  2754. * set_apic_access_page_addr() is used to reload apic access
  2755. * page upon invalidation. No need to do anything if the
  2756. * processor does not have the APIC_ACCESS_ADDR VMCS field.
  2757. */
  2758. kvm_x86_ops->set_apic_access_page_addr = NULL;
  2759. }
  2760. if (!cpu_has_vmx_tpr_shadow())
  2761. kvm_x86_ops->update_cr8_intercept = NULL;
  2762. if (enable_ept && !cpu_has_vmx_ept_2m_page())
  2763. kvm_disable_largepages();
  2764. if (!cpu_has_vmx_ple())
  2765. ple_gap = 0;
  2766. if (!cpu_has_vmx_apicv())
  2767. enable_apicv = 0;
  2768. if (enable_apicv)
  2769. kvm_x86_ops->update_cr8_intercept = NULL;
  2770. else {
  2771. kvm_x86_ops->hwapic_irr_update = NULL;
  2772. kvm_x86_ops->deliver_posted_interrupt = NULL;
  2773. kvm_x86_ops->sync_pir_to_irr = vmx_sync_pir_to_irr_dummy;
  2774. }
  2775. if (nested)
  2776. nested_vmx_setup_ctls_msrs();
  2777. return alloc_kvm_area();
  2778. }
  2779. static __exit void hardware_unsetup(void)
  2780. {
  2781. free_kvm_area();
  2782. }
  2783. static bool emulation_required(struct kvm_vcpu *vcpu)
  2784. {
  2785. return emulate_invalid_guest_state && !guest_state_valid(vcpu);
  2786. }
  2787. static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
  2788. struct kvm_segment *save)
  2789. {
  2790. if (!emulate_invalid_guest_state) {
  2791. /*
  2792. * CS and SS RPL should be equal during guest entry according
  2793. * to VMX spec, but in reality it is not always so. Since vcpu
  2794. * is in the middle of the transition from real mode to
  2795. * protected mode it is safe to assume that RPL 0 is a good
  2796. * default value.
  2797. */
  2798. if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
  2799. save->selector &= ~SELECTOR_RPL_MASK;
  2800. save->dpl = save->selector & SELECTOR_RPL_MASK;
  2801. save->s = 1;
  2802. }
  2803. vmx_set_segment(vcpu, save, seg);
  2804. }
  2805. static void enter_pmode(struct kvm_vcpu *vcpu)
  2806. {
  2807. unsigned long flags;
  2808. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2809. /*
  2810. * Update real mode segment cache. It may be not up-to-date if sement
  2811. * register was written while vcpu was in a guest mode.
  2812. */
  2813. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
  2814. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
  2815. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
  2816. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
  2817. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
  2818. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
  2819. vmx->rmode.vm86_active = 0;
  2820. vmx_segment_cache_clear(vmx);
  2821. vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
  2822. flags = vmcs_readl(GUEST_RFLAGS);
  2823. flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
  2824. flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
  2825. vmcs_writel(GUEST_RFLAGS, flags);
  2826. vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
  2827. (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
  2828. update_exception_bitmap(vcpu);
  2829. fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
  2830. fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
  2831. fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
  2832. fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
  2833. fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
  2834. fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
  2835. }
  2836. static void fix_rmode_seg(int seg, struct kvm_segment *save)
  2837. {
  2838. const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  2839. struct kvm_segment var = *save;
  2840. var.dpl = 0x3;
  2841. if (seg == VCPU_SREG_CS)
  2842. var.type = 0x3;
  2843. if (!emulate_invalid_guest_state) {
  2844. var.selector = var.base >> 4;
  2845. var.base = var.base & 0xffff0;
  2846. var.limit = 0xffff;
  2847. var.g = 0;
  2848. var.db = 0;
  2849. var.present = 1;
  2850. var.s = 1;
  2851. var.l = 0;
  2852. var.unusable = 0;
  2853. var.type = 0x3;
  2854. var.avl = 0;
  2855. if (save->base & 0xf)
  2856. printk_once(KERN_WARNING "kvm: segment base is not "
  2857. "paragraph aligned when entering "
  2858. "protected mode (seg=%d)", seg);
  2859. }
  2860. vmcs_write16(sf->selector, var.selector);
  2861. vmcs_write32(sf->base, var.base);
  2862. vmcs_write32(sf->limit, var.limit);
  2863. vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
  2864. }
  2865. static void enter_rmode(struct kvm_vcpu *vcpu)
  2866. {
  2867. unsigned long flags;
  2868. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2869. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
  2870. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
  2871. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
  2872. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
  2873. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
  2874. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
  2875. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
  2876. vmx->rmode.vm86_active = 1;
  2877. /*
  2878. * Very old userspace does not call KVM_SET_TSS_ADDR before entering
  2879. * vcpu. Warn the user that an update is overdue.
  2880. */
  2881. if (!vcpu->kvm->arch.tss_addr)
  2882. printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
  2883. "called before entering vcpu\n");
  2884. vmx_segment_cache_clear(vmx);
  2885. vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
  2886. vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
  2887. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  2888. flags = vmcs_readl(GUEST_RFLAGS);
  2889. vmx->rmode.save_rflags = flags;
  2890. flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
  2891. vmcs_writel(GUEST_RFLAGS, flags);
  2892. vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
  2893. update_exception_bitmap(vcpu);
  2894. fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
  2895. fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
  2896. fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
  2897. fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
  2898. fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
  2899. fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
  2900. kvm_mmu_reset_context(vcpu);
  2901. }
  2902. static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
  2903. {
  2904. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2905. struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
  2906. if (!msr)
  2907. return;
  2908. /*
  2909. * Force kernel_gs_base reloading before EFER changes, as control
  2910. * of this msr depends on is_long_mode().
  2911. */
  2912. vmx_load_host_state(to_vmx(vcpu));
  2913. vcpu->arch.efer = efer;
  2914. if (efer & EFER_LMA) {
  2915. vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
  2916. msr->data = efer;
  2917. } else {
  2918. vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
  2919. msr->data = efer & ~EFER_LME;
  2920. }
  2921. setup_msrs(vmx);
  2922. }
  2923. #ifdef CONFIG_X86_64
  2924. static void enter_lmode(struct kvm_vcpu *vcpu)
  2925. {
  2926. u32 guest_tr_ar;
  2927. vmx_segment_cache_clear(to_vmx(vcpu));
  2928. guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
  2929. if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
  2930. pr_debug_ratelimited("%s: tss fixup for long mode. \n",
  2931. __func__);
  2932. vmcs_write32(GUEST_TR_AR_BYTES,
  2933. (guest_tr_ar & ~AR_TYPE_MASK)
  2934. | AR_TYPE_BUSY_64_TSS);
  2935. }
  2936. vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
  2937. }
  2938. static void exit_lmode(struct kvm_vcpu *vcpu)
  2939. {
  2940. vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
  2941. vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
  2942. }
  2943. #endif
  2944. static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
  2945. {
  2946. vpid_sync_context(to_vmx(vcpu));
  2947. if (enable_ept) {
  2948. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  2949. return;
  2950. ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
  2951. }
  2952. }
  2953. static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
  2954. {
  2955. ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
  2956. vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
  2957. vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
  2958. }
  2959. static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
  2960. {
  2961. if (enable_ept && is_paging(vcpu))
  2962. vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
  2963. __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
  2964. }
  2965. static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
  2966. {
  2967. ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
  2968. vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
  2969. vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
  2970. }
  2971. static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
  2972. {
  2973. struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
  2974. if (!test_bit(VCPU_EXREG_PDPTR,
  2975. (unsigned long *)&vcpu->arch.regs_dirty))
  2976. return;
  2977. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  2978. vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
  2979. vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
  2980. vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
  2981. vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
  2982. }
  2983. }
  2984. static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
  2985. {
  2986. struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
  2987. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  2988. mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
  2989. mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
  2990. mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
  2991. mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
  2992. }
  2993. __set_bit(VCPU_EXREG_PDPTR,
  2994. (unsigned long *)&vcpu->arch.regs_avail);
  2995. __set_bit(VCPU_EXREG_PDPTR,
  2996. (unsigned long *)&vcpu->arch.regs_dirty);
  2997. }
  2998. static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
  2999. static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
  3000. unsigned long cr0,
  3001. struct kvm_vcpu *vcpu)
  3002. {
  3003. if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
  3004. vmx_decache_cr3(vcpu);
  3005. if (!(cr0 & X86_CR0_PG)) {
  3006. /* From paging/starting to nonpaging */
  3007. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  3008. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
  3009. (CPU_BASED_CR3_LOAD_EXITING |
  3010. CPU_BASED_CR3_STORE_EXITING));
  3011. vcpu->arch.cr0 = cr0;
  3012. vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
  3013. } else if (!is_paging(vcpu)) {
  3014. /* From nonpaging to paging */
  3015. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  3016. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
  3017. ~(CPU_BASED_CR3_LOAD_EXITING |
  3018. CPU_BASED_CR3_STORE_EXITING));
  3019. vcpu->arch.cr0 = cr0;
  3020. vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
  3021. }
  3022. if (!(cr0 & X86_CR0_WP))
  3023. *hw_cr0 &= ~X86_CR0_WP;
  3024. }
  3025. static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  3026. {
  3027. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3028. unsigned long hw_cr0;
  3029. hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
  3030. if (enable_unrestricted_guest)
  3031. hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
  3032. else {
  3033. hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
  3034. if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
  3035. enter_pmode(vcpu);
  3036. if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
  3037. enter_rmode(vcpu);
  3038. }
  3039. #ifdef CONFIG_X86_64
  3040. if (vcpu->arch.efer & EFER_LME) {
  3041. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
  3042. enter_lmode(vcpu);
  3043. if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
  3044. exit_lmode(vcpu);
  3045. }
  3046. #endif
  3047. if (enable_ept)
  3048. ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
  3049. if (!vcpu->fpu_active)
  3050. hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
  3051. vmcs_writel(CR0_READ_SHADOW, cr0);
  3052. vmcs_writel(GUEST_CR0, hw_cr0);
  3053. vcpu->arch.cr0 = cr0;
  3054. /* depends on vcpu->arch.cr0 to be set to a new value */
  3055. vmx->emulation_required = emulation_required(vcpu);
  3056. }
  3057. static u64 construct_eptp(unsigned long root_hpa)
  3058. {
  3059. u64 eptp;
  3060. /* TODO write the value reading from MSR */
  3061. eptp = VMX_EPT_DEFAULT_MT |
  3062. VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
  3063. if (enable_ept_ad_bits)
  3064. eptp |= VMX_EPT_AD_ENABLE_BIT;
  3065. eptp |= (root_hpa & PAGE_MASK);
  3066. return eptp;
  3067. }
  3068. static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
  3069. {
  3070. unsigned long guest_cr3;
  3071. u64 eptp;
  3072. guest_cr3 = cr3;
  3073. if (enable_ept) {
  3074. eptp = construct_eptp(cr3);
  3075. vmcs_write64(EPT_POINTER, eptp);
  3076. if (is_paging(vcpu) || is_guest_mode(vcpu))
  3077. guest_cr3 = kvm_read_cr3(vcpu);
  3078. else
  3079. guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
  3080. ept_load_pdptrs(vcpu);
  3081. }
  3082. vmx_flush_tlb(vcpu);
  3083. vmcs_writel(GUEST_CR3, guest_cr3);
  3084. }
  3085. static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  3086. {
  3087. unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ?
  3088. KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
  3089. if (cr4 & X86_CR4_VMXE) {
  3090. /*
  3091. * To use VMXON (and later other VMX instructions), a guest
  3092. * must first be able to turn on cr4.VMXE (see handle_vmon()).
  3093. * So basically the check on whether to allow nested VMX
  3094. * is here.
  3095. */
  3096. if (!nested_vmx_allowed(vcpu))
  3097. return 1;
  3098. }
  3099. if (to_vmx(vcpu)->nested.vmxon &&
  3100. ((cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON))
  3101. return 1;
  3102. vcpu->arch.cr4 = cr4;
  3103. if (enable_ept) {
  3104. if (!is_paging(vcpu)) {
  3105. hw_cr4 &= ~X86_CR4_PAE;
  3106. hw_cr4 |= X86_CR4_PSE;
  3107. /*
  3108. * SMEP/SMAP is disabled if CPU is in non-paging mode
  3109. * in hardware. However KVM always uses paging mode to
  3110. * emulate guest non-paging mode with TDP.
  3111. * To emulate this behavior, SMEP/SMAP needs to be
  3112. * manually disabled when guest switches to non-paging
  3113. * mode.
  3114. */
  3115. hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP);
  3116. } else if (!(cr4 & X86_CR4_PAE)) {
  3117. hw_cr4 &= ~X86_CR4_PAE;
  3118. }
  3119. }
  3120. vmcs_writel(CR4_READ_SHADOW, cr4);
  3121. vmcs_writel(GUEST_CR4, hw_cr4);
  3122. return 0;
  3123. }
  3124. static void vmx_get_segment(struct kvm_vcpu *vcpu,
  3125. struct kvm_segment *var, int seg)
  3126. {
  3127. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3128. u32 ar;
  3129. if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
  3130. *var = vmx->rmode.segs[seg];
  3131. if (seg == VCPU_SREG_TR
  3132. || var->selector == vmx_read_guest_seg_selector(vmx, seg))
  3133. return;
  3134. var->base = vmx_read_guest_seg_base(vmx, seg);
  3135. var->selector = vmx_read_guest_seg_selector(vmx, seg);
  3136. return;
  3137. }
  3138. var->base = vmx_read_guest_seg_base(vmx, seg);
  3139. var->limit = vmx_read_guest_seg_limit(vmx, seg);
  3140. var->selector = vmx_read_guest_seg_selector(vmx, seg);
  3141. ar = vmx_read_guest_seg_ar(vmx, seg);
  3142. var->unusable = (ar >> 16) & 1;
  3143. var->type = ar & 15;
  3144. var->s = (ar >> 4) & 1;
  3145. var->dpl = (ar >> 5) & 3;
  3146. /*
  3147. * Some userspaces do not preserve unusable property. Since usable
  3148. * segment has to be present according to VMX spec we can use present
  3149. * property to amend userspace bug by making unusable segment always
  3150. * nonpresent. vmx_segment_access_rights() already marks nonpresent
  3151. * segment as unusable.
  3152. */
  3153. var->present = !var->unusable;
  3154. var->avl = (ar >> 12) & 1;
  3155. var->l = (ar >> 13) & 1;
  3156. var->db = (ar >> 14) & 1;
  3157. var->g = (ar >> 15) & 1;
  3158. }
  3159. static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
  3160. {
  3161. struct kvm_segment s;
  3162. if (to_vmx(vcpu)->rmode.vm86_active) {
  3163. vmx_get_segment(vcpu, &s, seg);
  3164. return s.base;
  3165. }
  3166. return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
  3167. }
  3168. static int vmx_get_cpl(struct kvm_vcpu *vcpu)
  3169. {
  3170. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3171. if (unlikely(vmx->rmode.vm86_active))
  3172. return 0;
  3173. else {
  3174. int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
  3175. return AR_DPL(ar);
  3176. }
  3177. }
  3178. static u32 vmx_segment_access_rights(struct kvm_segment *var)
  3179. {
  3180. u32 ar;
  3181. if (var->unusable || !var->present)
  3182. ar = 1 << 16;
  3183. else {
  3184. ar = var->type & 15;
  3185. ar |= (var->s & 1) << 4;
  3186. ar |= (var->dpl & 3) << 5;
  3187. ar |= (var->present & 1) << 7;
  3188. ar |= (var->avl & 1) << 12;
  3189. ar |= (var->l & 1) << 13;
  3190. ar |= (var->db & 1) << 14;
  3191. ar |= (var->g & 1) << 15;
  3192. }
  3193. return ar;
  3194. }
  3195. static void vmx_set_segment(struct kvm_vcpu *vcpu,
  3196. struct kvm_segment *var, int seg)
  3197. {
  3198. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3199. const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  3200. vmx_segment_cache_clear(vmx);
  3201. if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
  3202. vmx->rmode.segs[seg] = *var;
  3203. if (seg == VCPU_SREG_TR)
  3204. vmcs_write16(sf->selector, var->selector);
  3205. else if (var->s)
  3206. fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
  3207. goto out;
  3208. }
  3209. vmcs_writel(sf->base, var->base);
  3210. vmcs_write32(sf->limit, var->limit);
  3211. vmcs_write16(sf->selector, var->selector);
  3212. /*
  3213. * Fix the "Accessed" bit in AR field of segment registers for older
  3214. * qemu binaries.
  3215. * IA32 arch specifies that at the time of processor reset the
  3216. * "Accessed" bit in the AR field of segment registers is 1. And qemu
  3217. * is setting it to 0 in the userland code. This causes invalid guest
  3218. * state vmexit when "unrestricted guest" mode is turned on.
  3219. * Fix for this setup issue in cpu_reset is being pushed in the qemu
  3220. * tree. Newer qemu binaries with that qemu fix would not need this
  3221. * kvm hack.
  3222. */
  3223. if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
  3224. var->type |= 0x1; /* Accessed */
  3225. vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
  3226. out:
  3227. vmx->emulation_required = emulation_required(vcpu);
  3228. }
  3229. static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
  3230. {
  3231. u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
  3232. *db = (ar >> 14) & 1;
  3233. *l = (ar >> 13) & 1;
  3234. }
  3235. static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  3236. {
  3237. dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
  3238. dt->address = vmcs_readl(GUEST_IDTR_BASE);
  3239. }
  3240. static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  3241. {
  3242. vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
  3243. vmcs_writel(GUEST_IDTR_BASE, dt->address);
  3244. }
  3245. static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  3246. {
  3247. dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
  3248. dt->address = vmcs_readl(GUEST_GDTR_BASE);
  3249. }
  3250. static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  3251. {
  3252. vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
  3253. vmcs_writel(GUEST_GDTR_BASE, dt->address);
  3254. }
  3255. static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
  3256. {
  3257. struct kvm_segment var;
  3258. u32 ar;
  3259. vmx_get_segment(vcpu, &var, seg);
  3260. var.dpl = 0x3;
  3261. if (seg == VCPU_SREG_CS)
  3262. var.type = 0x3;
  3263. ar = vmx_segment_access_rights(&var);
  3264. if (var.base != (var.selector << 4))
  3265. return false;
  3266. if (var.limit != 0xffff)
  3267. return false;
  3268. if (ar != 0xf3)
  3269. return false;
  3270. return true;
  3271. }
  3272. static bool code_segment_valid(struct kvm_vcpu *vcpu)
  3273. {
  3274. struct kvm_segment cs;
  3275. unsigned int cs_rpl;
  3276. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  3277. cs_rpl = cs.selector & SELECTOR_RPL_MASK;
  3278. if (cs.unusable)
  3279. return false;
  3280. if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
  3281. return false;
  3282. if (!cs.s)
  3283. return false;
  3284. if (cs.type & AR_TYPE_WRITEABLE_MASK) {
  3285. if (cs.dpl > cs_rpl)
  3286. return false;
  3287. } else {
  3288. if (cs.dpl != cs_rpl)
  3289. return false;
  3290. }
  3291. if (!cs.present)
  3292. return false;
  3293. /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
  3294. return true;
  3295. }
  3296. static bool stack_segment_valid(struct kvm_vcpu *vcpu)
  3297. {
  3298. struct kvm_segment ss;
  3299. unsigned int ss_rpl;
  3300. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  3301. ss_rpl = ss.selector & SELECTOR_RPL_MASK;
  3302. if (ss.unusable)
  3303. return true;
  3304. if (ss.type != 3 && ss.type != 7)
  3305. return false;
  3306. if (!ss.s)
  3307. return false;
  3308. if (ss.dpl != ss_rpl) /* DPL != RPL */
  3309. return false;
  3310. if (!ss.present)
  3311. return false;
  3312. return true;
  3313. }
  3314. static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
  3315. {
  3316. struct kvm_segment var;
  3317. unsigned int rpl;
  3318. vmx_get_segment(vcpu, &var, seg);
  3319. rpl = var.selector & SELECTOR_RPL_MASK;
  3320. if (var.unusable)
  3321. return true;
  3322. if (!var.s)
  3323. return false;
  3324. if (!var.present)
  3325. return false;
  3326. if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
  3327. if (var.dpl < rpl) /* DPL < RPL */
  3328. return false;
  3329. }
  3330. /* TODO: Add other members to kvm_segment_field to allow checking for other access
  3331. * rights flags
  3332. */
  3333. return true;
  3334. }
  3335. static bool tr_valid(struct kvm_vcpu *vcpu)
  3336. {
  3337. struct kvm_segment tr;
  3338. vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
  3339. if (tr.unusable)
  3340. return false;
  3341. if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
  3342. return false;
  3343. if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
  3344. return false;
  3345. if (!tr.present)
  3346. return false;
  3347. return true;
  3348. }
  3349. static bool ldtr_valid(struct kvm_vcpu *vcpu)
  3350. {
  3351. struct kvm_segment ldtr;
  3352. vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
  3353. if (ldtr.unusable)
  3354. return true;
  3355. if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
  3356. return false;
  3357. if (ldtr.type != 2)
  3358. return false;
  3359. if (!ldtr.present)
  3360. return false;
  3361. return true;
  3362. }
  3363. static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
  3364. {
  3365. struct kvm_segment cs, ss;
  3366. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  3367. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  3368. return ((cs.selector & SELECTOR_RPL_MASK) ==
  3369. (ss.selector & SELECTOR_RPL_MASK));
  3370. }
  3371. /*
  3372. * Check if guest state is valid. Returns true if valid, false if
  3373. * not.
  3374. * We assume that registers are always usable
  3375. */
  3376. static bool guest_state_valid(struct kvm_vcpu *vcpu)
  3377. {
  3378. if (enable_unrestricted_guest)
  3379. return true;
  3380. /* real mode guest state checks */
  3381. if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
  3382. if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
  3383. return false;
  3384. if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
  3385. return false;
  3386. if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
  3387. return false;
  3388. if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
  3389. return false;
  3390. if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
  3391. return false;
  3392. if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
  3393. return false;
  3394. } else {
  3395. /* protected mode guest state checks */
  3396. if (!cs_ss_rpl_check(vcpu))
  3397. return false;
  3398. if (!code_segment_valid(vcpu))
  3399. return false;
  3400. if (!stack_segment_valid(vcpu))
  3401. return false;
  3402. if (!data_segment_valid(vcpu, VCPU_SREG_DS))
  3403. return false;
  3404. if (!data_segment_valid(vcpu, VCPU_SREG_ES))
  3405. return false;
  3406. if (!data_segment_valid(vcpu, VCPU_SREG_FS))
  3407. return false;
  3408. if (!data_segment_valid(vcpu, VCPU_SREG_GS))
  3409. return false;
  3410. if (!tr_valid(vcpu))
  3411. return false;
  3412. if (!ldtr_valid(vcpu))
  3413. return false;
  3414. }
  3415. /* TODO:
  3416. * - Add checks on RIP
  3417. * - Add checks on RFLAGS
  3418. */
  3419. return true;
  3420. }
  3421. static int init_rmode_tss(struct kvm *kvm)
  3422. {
  3423. gfn_t fn;
  3424. u16 data = 0;
  3425. int idx, r;
  3426. idx = srcu_read_lock(&kvm->srcu);
  3427. fn = kvm->arch.tss_addr >> PAGE_SHIFT;
  3428. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  3429. if (r < 0)
  3430. goto out;
  3431. data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
  3432. r = kvm_write_guest_page(kvm, fn++, &data,
  3433. TSS_IOPB_BASE_OFFSET, sizeof(u16));
  3434. if (r < 0)
  3435. goto out;
  3436. r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
  3437. if (r < 0)
  3438. goto out;
  3439. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  3440. if (r < 0)
  3441. goto out;
  3442. data = ~0;
  3443. r = kvm_write_guest_page(kvm, fn, &data,
  3444. RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
  3445. sizeof(u8));
  3446. out:
  3447. srcu_read_unlock(&kvm->srcu, idx);
  3448. return r;
  3449. }
  3450. static int init_rmode_identity_map(struct kvm *kvm)
  3451. {
  3452. int i, idx, r = 0;
  3453. pfn_t identity_map_pfn;
  3454. u32 tmp;
  3455. if (!enable_ept)
  3456. return 0;
  3457. /* Protect kvm->arch.ept_identity_pagetable_done. */
  3458. mutex_lock(&kvm->slots_lock);
  3459. if (likely(kvm->arch.ept_identity_pagetable_done))
  3460. goto out2;
  3461. identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
  3462. r = alloc_identity_pagetable(kvm);
  3463. if (r < 0)
  3464. goto out2;
  3465. idx = srcu_read_lock(&kvm->srcu);
  3466. r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
  3467. if (r < 0)
  3468. goto out;
  3469. /* Set up identity-mapping pagetable for EPT in real mode */
  3470. for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
  3471. tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
  3472. _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
  3473. r = kvm_write_guest_page(kvm, identity_map_pfn,
  3474. &tmp, i * sizeof(tmp), sizeof(tmp));
  3475. if (r < 0)
  3476. goto out;
  3477. }
  3478. kvm->arch.ept_identity_pagetable_done = true;
  3479. out:
  3480. srcu_read_unlock(&kvm->srcu, idx);
  3481. out2:
  3482. mutex_unlock(&kvm->slots_lock);
  3483. return r;
  3484. }
  3485. static void seg_setup(int seg)
  3486. {
  3487. const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  3488. unsigned int ar;
  3489. vmcs_write16(sf->selector, 0);
  3490. vmcs_writel(sf->base, 0);
  3491. vmcs_write32(sf->limit, 0xffff);
  3492. ar = 0x93;
  3493. if (seg == VCPU_SREG_CS)
  3494. ar |= 0x08; /* code segment */
  3495. vmcs_write32(sf->ar_bytes, ar);
  3496. }
  3497. static int alloc_apic_access_page(struct kvm *kvm)
  3498. {
  3499. struct page *page;
  3500. struct kvm_userspace_memory_region kvm_userspace_mem;
  3501. int r = 0;
  3502. mutex_lock(&kvm->slots_lock);
  3503. if (kvm->arch.apic_access_page_done)
  3504. goto out;
  3505. kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
  3506. kvm_userspace_mem.flags = 0;
  3507. kvm_userspace_mem.guest_phys_addr = APIC_DEFAULT_PHYS_BASE;
  3508. kvm_userspace_mem.memory_size = PAGE_SIZE;
  3509. r = __kvm_set_memory_region(kvm, &kvm_userspace_mem);
  3510. if (r)
  3511. goto out;
  3512. page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
  3513. if (is_error_page(page)) {
  3514. r = -EFAULT;
  3515. goto out;
  3516. }
  3517. /*
  3518. * Do not pin the page in memory, so that memory hot-unplug
  3519. * is able to migrate it.
  3520. */
  3521. put_page(page);
  3522. kvm->arch.apic_access_page_done = true;
  3523. out:
  3524. mutex_unlock(&kvm->slots_lock);
  3525. return r;
  3526. }
  3527. static int alloc_identity_pagetable(struct kvm *kvm)
  3528. {
  3529. /* Called with kvm->slots_lock held. */
  3530. struct kvm_userspace_memory_region kvm_userspace_mem;
  3531. int r = 0;
  3532. BUG_ON(kvm->arch.ept_identity_pagetable_done);
  3533. kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
  3534. kvm_userspace_mem.flags = 0;
  3535. kvm_userspace_mem.guest_phys_addr =
  3536. kvm->arch.ept_identity_map_addr;
  3537. kvm_userspace_mem.memory_size = PAGE_SIZE;
  3538. r = __kvm_set_memory_region(kvm, &kvm_userspace_mem);
  3539. return r;
  3540. }
  3541. static void allocate_vpid(struct vcpu_vmx *vmx)
  3542. {
  3543. int vpid;
  3544. vmx->vpid = 0;
  3545. if (!enable_vpid)
  3546. return;
  3547. spin_lock(&vmx_vpid_lock);
  3548. vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
  3549. if (vpid < VMX_NR_VPIDS) {
  3550. vmx->vpid = vpid;
  3551. __set_bit(vpid, vmx_vpid_bitmap);
  3552. }
  3553. spin_unlock(&vmx_vpid_lock);
  3554. }
  3555. static void free_vpid(struct vcpu_vmx *vmx)
  3556. {
  3557. if (!enable_vpid)
  3558. return;
  3559. spin_lock(&vmx_vpid_lock);
  3560. if (vmx->vpid != 0)
  3561. __clear_bit(vmx->vpid, vmx_vpid_bitmap);
  3562. spin_unlock(&vmx_vpid_lock);
  3563. }
  3564. #define MSR_TYPE_R 1
  3565. #define MSR_TYPE_W 2
  3566. static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
  3567. u32 msr, int type)
  3568. {
  3569. int f = sizeof(unsigned long);
  3570. if (!cpu_has_vmx_msr_bitmap())
  3571. return;
  3572. /*
  3573. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  3574. * have the write-low and read-high bitmap offsets the wrong way round.
  3575. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  3576. */
  3577. if (msr <= 0x1fff) {
  3578. if (type & MSR_TYPE_R)
  3579. /* read-low */
  3580. __clear_bit(msr, msr_bitmap + 0x000 / f);
  3581. if (type & MSR_TYPE_W)
  3582. /* write-low */
  3583. __clear_bit(msr, msr_bitmap + 0x800 / f);
  3584. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  3585. msr &= 0x1fff;
  3586. if (type & MSR_TYPE_R)
  3587. /* read-high */
  3588. __clear_bit(msr, msr_bitmap + 0x400 / f);
  3589. if (type & MSR_TYPE_W)
  3590. /* write-high */
  3591. __clear_bit(msr, msr_bitmap + 0xc00 / f);
  3592. }
  3593. }
  3594. static void __vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
  3595. u32 msr, int type)
  3596. {
  3597. int f = sizeof(unsigned long);
  3598. if (!cpu_has_vmx_msr_bitmap())
  3599. return;
  3600. /*
  3601. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  3602. * have the write-low and read-high bitmap offsets the wrong way round.
  3603. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  3604. */
  3605. if (msr <= 0x1fff) {
  3606. if (type & MSR_TYPE_R)
  3607. /* read-low */
  3608. __set_bit(msr, msr_bitmap + 0x000 / f);
  3609. if (type & MSR_TYPE_W)
  3610. /* write-low */
  3611. __set_bit(msr, msr_bitmap + 0x800 / f);
  3612. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  3613. msr &= 0x1fff;
  3614. if (type & MSR_TYPE_R)
  3615. /* read-high */
  3616. __set_bit(msr, msr_bitmap + 0x400 / f);
  3617. if (type & MSR_TYPE_W)
  3618. /* write-high */
  3619. __set_bit(msr, msr_bitmap + 0xc00 / f);
  3620. }
  3621. }
  3622. static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
  3623. {
  3624. if (!longmode_only)
  3625. __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy,
  3626. msr, MSR_TYPE_R | MSR_TYPE_W);
  3627. __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode,
  3628. msr, MSR_TYPE_R | MSR_TYPE_W);
  3629. }
  3630. static void vmx_enable_intercept_msr_read_x2apic(u32 msr)
  3631. {
  3632. __vmx_enable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
  3633. msr, MSR_TYPE_R);
  3634. __vmx_enable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
  3635. msr, MSR_TYPE_R);
  3636. }
  3637. static void vmx_disable_intercept_msr_read_x2apic(u32 msr)
  3638. {
  3639. __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
  3640. msr, MSR_TYPE_R);
  3641. __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
  3642. msr, MSR_TYPE_R);
  3643. }
  3644. static void vmx_disable_intercept_msr_write_x2apic(u32 msr)
  3645. {
  3646. __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
  3647. msr, MSR_TYPE_W);
  3648. __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
  3649. msr, MSR_TYPE_W);
  3650. }
  3651. static int vmx_vm_has_apicv(struct kvm *kvm)
  3652. {
  3653. return enable_apicv && irqchip_in_kernel(kvm);
  3654. }
  3655. /*
  3656. * Send interrupt to vcpu via posted interrupt way.
  3657. * 1. If target vcpu is running(non-root mode), send posted interrupt
  3658. * notification to vcpu and hardware will sync PIR to vIRR atomically.
  3659. * 2. If target vcpu isn't running(root mode), kick it to pick up the
  3660. * interrupt from PIR in next vmentry.
  3661. */
  3662. static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
  3663. {
  3664. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3665. int r;
  3666. if (pi_test_and_set_pir(vector, &vmx->pi_desc))
  3667. return;
  3668. r = pi_test_and_set_on(&vmx->pi_desc);
  3669. kvm_make_request(KVM_REQ_EVENT, vcpu);
  3670. #ifdef CONFIG_SMP
  3671. if (!r && (vcpu->mode == IN_GUEST_MODE))
  3672. apic->send_IPI_mask(get_cpu_mask(vcpu->cpu),
  3673. POSTED_INTR_VECTOR);
  3674. else
  3675. #endif
  3676. kvm_vcpu_kick(vcpu);
  3677. }
  3678. static void vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
  3679. {
  3680. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3681. if (!pi_test_and_clear_on(&vmx->pi_desc))
  3682. return;
  3683. kvm_apic_update_irr(vcpu, vmx->pi_desc.pir);
  3684. }
  3685. static void vmx_sync_pir_to_irr_dummy(struct kvm_vcpu *vcpu)
  3686. {
  3687. return;
  3688. }
  3689. /*
  3690. * Set up the vmcs's constant host-state fields, i.e., host-state fields that
  3691. * will not change in the lifetime of the guest.
  3692. * Note that host-state that does change is set elsewhere. E.g., host-state
  3693. * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
  3694. */
  3695. static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
  3696. {
  3697. u32 low32, high32;
  3698. unsigned long tmpl;
  3699. struct desc_ptr dt;
  3700. unsigned long cr4;
  3701. vmcs_writel(HOST_CR0, read_cr0() & ~X86_CR0_TS); /* 22.2.3 */
  3702. vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
  3703. /* Save the most likely value for this task's CR4 in the VMCS. */
  3704. cr4 = read_cr4();
  3705. vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
  3706. vmx->host_state.vmcs_host_cr4 = cr4;
  3707. vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
  3708. #ifdef CONFIG_X86_64
  3709. /*
  3710. * Load null selectors, so we can avoid reloading them in
  3711. * __vmx_load_host_state(), in case userspace uses the null selectors
  3712. * too (the expected case).
  3713. */
  3714. vmcs_write16(HOST_DS_SELECTOR, 0);
  3715. vmcs_write16(HOST_ES_SELECTOR, 0);
  3716. #else
  3717. vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  3718. vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  3719. #endif
  3720. vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  3721. vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
  3722. native_store_idt(&dt);
  3723. vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
  3724. vmx->host_idt_base = dt.address;
  3725. vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
  3726. rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
  3727. vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
  3728. rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
  3729. vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
  3730. if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
  3731. rdmsr(MSR_IA32_CR_PAT, low32, high32);
  3732. vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
  3733. }
  3734. }
  3735. static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
  3736. {
  3737. vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
  3738. if (enable_ept)
  3739. vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
  3740. if (is_guest_mode(&vmx->vcpu))
  3741. vmx->vcpu.arch.cr4_guest_owned_bits &=
  3742. ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
  3743. vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
  3744. }
  3745. static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
  3746. {
  3747. u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
  3748. if (!vmx_vm_has_apicv(vmx->vcpu.kvm))
  3749. pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
  3750. return pin_based_exec_ctrl;
  3751. }
  3752. static u32 vmx_exec_control(struct vcpu_vmx *vmx)
  3753. {
  3754. u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
  3755. if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
  3756. exec_control &= ~CPU_BASED_MOV_DR_EXITING;
  3757. if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
  3758. exec_control &= ~CPU_BASED_TPR_SHADOW;
  3759. #ifdef CONFIG_X86_64
  3760. exec_control |= CPU_BASED_CR8_STORE_EXITING |
  3761. CPU_BASED_CR8_LOAD_EXITING;
  3762. #endif
  3763. }
  3764. if (!enable_ept)
  3765. exec_control |= CPU_BASED_CR3_STORE_EXITING |
  3766. CPU_BASED_CR3_LOAD_EXITING |
  3767. CPU_BASED_INVLPG_EXITING;
  3768. return exec_control;
  3769. }
  3770. static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
  3771. {
  3772. u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
  3773. if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
  3774. exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  3775. if (vmx->vpid == 0)
  3776. exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
  3777. if (!enable_ept) {
  3778. exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
  3779. enable_unrestricted_guest = 0;
  3780. /* Enable INVPCID for non-ept guests may cause performance regression. */
  3781. exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
  3782. }
  3783. if (!enable_unrestricted_guest)
  3784. exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
  3785. if (!ple_gap)
  3786. exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  3787. if (!vmx_vm_has_apicv(vmx->vcpu.kvm))
  3788. exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
  3789. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  3790. exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  3791. /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
  3792. (handle_vmptrld).
  3793. We can NOT enable shadow_vmcs here because we don't have yet
  3794. a current VMCS12
  3795. */
  3796. exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
  3797. return exec_control;
  3798. }
  3799. static void ept_set_mmio_spte_mask(void)
  3800. {
  3801. /*
  3802. * EPT Misconfigurations can be generated if the value of bits 2:0
  3803. * of an EPT paging-structure entry is 110b (write/execute).
  3804. * Also, magic bits (0x3ull << 62) is set to quickly identify mmio
  3805. * spte.
  3806. */
  3807. kvm_mmu_set_mmio_spte_mask((0x3ull << 62) | 0x6ull);
  3808. }
  3809. /*
  3810. * Sets up the vmcs for emulated real mode.
  3811. */
  3812. static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
  3813. {
  3814. #ifdef CONFIG_X86_64
  3815. unsigned long a;
  3816. #endif
  3817. int i;
  3818. /* I/O */
  3819. vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
  3820. vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
  3821. if (enable_shadow_vmcs) {
  3822. vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
  3823. vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
  3824. }
  3825. if (cpu_has_vmx_msr_bitmap())
  3826. vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
  3827. vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
  3828. /* Control */
  3829. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
  3830. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
  3831. if (cpu_has_secondary_exec_ctrls()) {
  3832. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  3833. vmx_secondary_exec_control(vmx));
  3834. }
  3835. if (vmx_vm_has_apicv(vmx->vcpu.kvm)) {
  3836. vmcs_write64(EOI_EXIT_BITMAP0, 0);
  3837. vmcs_write64(EOI_EXIT_BITMAP1, 0);
  3838. vmcs_write64(EOI_EXIT_BITMAP2, 0);
  3839. vmcs_write64(EOI_EXIT_BITMAP3, 0);
  3840. vmcs_write16(GUEST_INTR_STATUS, 0);
  3841. vmcs_write64(POSTED_INTR_NV, POSTED_INTR_VECTOR);
  3842. vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
  3843. }
  3844. if (ple_gap) {
  3845. vmcs_write32(PLE_GAP, ple_gap);
  3846. vmx->ple_window = ple_window;
  3847. vmx->ple_window_dirty = true;
  3848. }
  3849. vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
  3850. vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
  3851. vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
  3852. vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
  3853. vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
  3854. vmx_set_constant_host_state(vmx);
  3855. #ifdef CONFIG_X86_64
  3856. rdmsrl(MSR_FS_BASE, a);
  3857. vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
  3858. rdmsrl(MSR_GS_BASE, a);
  3859. vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
  3860. #else
  3861. vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
  3862. vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
  3863. #endif
  3864. vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
  3865. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
  3866. vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
  3867. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
  3868. vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
  3869. if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
  3870. u32 msr_low, msr_high;
  3871. u64 host_pat;
  3872. rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
  3873. host_pat = msr_low | ((u64) msr_high << 32);
  3874. /* Write the default value follow host pat */
  3875. vmcs_write64(GUEST_IA32_PAT, host_pat);
  3876. /* Keep arch.pat sync with GUEST_IA32_PAT */
  3877. vmx->vcpu.arch.pat = host_pat;
  3878. }
  3879. for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
  3880. u32 index = vmx_msr_index[i];
  3881. u32 data_low, data_high;
  3882. int j = vmx->nmsrs;
  3883. if (rdmsr_safe(index, &data_low, &data_high) < 0)
  3884. continue;
  3885. if (wrmsr_safe(index, data_low, data_high) < 0)
  3886. continue;
  3887. vmx->guest_msrs[j].index = i;
  3888. vmx->guest_msrs[j].data = 0;
  3889. vmx->guest_msrs[j].mask = -1ull;
  3890. ++vmx->nmsrs;
  3891. }
  3892. vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
  3893. /* 22.2.1, 20.8.1 */
  3894. vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
  3895. vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
  3896. set_cr4_guest_host_mask(vmx);
  3897. return 0;
  3898. }
  3899. static void vmx_vcpu_reset(struct kvm_vcpu *vcpu)
  3900. {
  3901. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3902. struct msr_data apic_base_msr;
  3903. vmx->rmode.vm86_active = 0;
  3904. vmx->soft_vnmi_blocked = 0;
  3905. vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
  3906. kvm_set_cr8(&vmx->vcpu, 0);
  3907. apic_base_msr.data = APIC_DEFAULT_PHYS_BASE | MSR_IA32_APICBASE_ENABLE;
  3908. if (kvm_vcpu_is_bsp(&vmx->vcpu))
  3909. apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
  3910. apic_base_msr.host_initiated = true;
  3911. kvm_set_apic_base(&vmx->vcpu, &apic_base_msr);
  3912. vmx_segment_cache_clear(vmx);
  3913. seg_setup(VCPU_SREG_CS);
  3914. vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
  3915. vmcs_write32(GUEST_CS_BASE, 0xffff0000);
  3916. seg_setup(VCPU_SREG_DS);
  3917. seg_setup(VCPU_SREG_ES);
  3918. seg_setup(VCPU_SREG_FS);
  3919. seg_setup(VCPU_SREG_GS);
  3920. seg_setup(VCPU_SREG_SS);
  3921. vmcs_write16(GUEST_TR_SELECTOR, 0);
  3922. vmcs_writel(GUEST_TR_BASE, 0);
  3923. vmcs_write32(GUEST_TR_LIMIT, 0xffff);
  3924. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  3925. vmcs_write16(GUEST_LDTR_SELECTOR, 0);
  3926. vmcs_writel(GUEST_LDTR_BASE, 0);
  3927. vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
  3928. vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
  3929. vmcs_write32(GUEST_SYSENTER_CS, 0);
  3930. vmcs_writel(GUEST_SYSENTER_ESP, 0);
  3931. vmcs_writel(GUEST_SYSENTER_EIP, 0);
  3932. vmcs_writel(GUEST_RFLAGS, 0x02);
  3933. kvm_rip_write(vcpu, 0xfff0);
  3934. vmcs_writel(GUEST_GDTR_BASE, 0);
  3935. vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
  3936. vmcs_writel(GUEST_IDTR_BASE, 0);
  3937. vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
  3938. vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
  3939. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
  3940. vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
  3941. /* Special registers */
  3942. vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
  3943. setup_msrs(vmx);
  3944. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
  3945. if (cpu_has_vmx_tpr_shadow()) {
  3946. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
  3947. if (vm_need_tpr_shadow(vmx->vcpu.kvm))
  3948. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
  3949. __pa(vmx->vcpu.arch.apic->regs));
  3950. vmcs_write32(TPR_THRESHOLD, 0);
  3951. }
  3952. kvm_vcpu_reload_apic_access_page(vcpu);
  3953. if (vmx_vm_has_apicv(vcpu->kvm))
  3954. memset(&vmx->pi_desc, 0, sizeof(struct pi_desc));
  3955. if (vmx->vpid != 0)
  3956. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
  3957. vmx->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
  3958. vmx_set_cr0(&vmx->vcpu, kvm_read_cr0(vcpu)); /* enter rmode */
  3959. vmx_set_cr4(&vmx->vcpu, 0);
  3960. vmx_set_efer(&vmx->vcpu, 0);
  3961. vmx_fpu_activate(&vmx->vcpu);
  3962. update_exception_bitmap(&vmx->vcpu);
  3963. vpid_sync_context(vmx);
  3964. }
  3965. /*
  3966. * In nested virtualization, check if L1 asked to exit on external interrupts.
  3967. * For most existing hypervisors, this will always return true.
  3968. */
  3969. static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
  3970. {
  3971. return get_vmcs12(vcpu)->pin_based_vm_exec_control &
  3972. PIN_BASED_EXT_INTR_MASK;
  3973. }
  3974. /*
  3975. * In nested virtualization, check if L1 has set
  3976. * VM_EXIT_ACK_INTR_ON_EXIT
  3977. */
  3978. static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
  3979. {
  3980. return get_vmcs12(vcpu)->vm_exit_controls &
  3981. VM_EXIT_ACK_INTR_ON_EXIT;
  3982. }
  3983. static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
  3984. {
  3985. return get_vmcs12(vcpu)->pin_based_vm_exec_control &
  3986. PIN_BASED_NMI_EXITING;
  3987. }
  3988. static void enable_irq_window(struct kvm_vcpu *vcpu)
  3989. {
  3990. u32 cpu_based_vm_exec_control;
  3991. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  3992. cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
  3993. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  3994. }
  3995. static void enable_nmi_window(struct kvm_vcpu *vcpu)
  3996. {
  3997. u32 cpu_based_vm_exec_control;
  3998. if (!cpu_has_virtual_nmis() ||
  3999. vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
  4000. enable_irq_window(vcpu);
  4001. return;
  4002. }
  4003. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  4004. cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
  4005. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  4006. }
  4007. static void vmx_inject_irq(struct kvm_vcpu *vcpu)
  4008. {
  4009. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4010. uint32_t intr;
  4011. int irq = vcpu->arch.interrupt.nr;
  4012. trace_kvm_inj_virq(irq);
  4013. ++vcpu->stat.irq_injections;
  4014. if (vmx->rmode.vm86_active) {
  4015. int inc_eip = 0;
  4016. if (vcpu->arch.interrupt.soft)
  4017. inc_eip = vcpu->arch.event_exit_inst_len;
  4018. if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
  4019. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  4020. return;
  4021. }
  4022. intr = irq | INTR_INFO_VALID_MASK;
  4023. if (vcpu->arch.interrupt.soft) {
  4024. intr |= INTR_TYPE_SOFT_INTR;
  4025. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  4026. vmx->vcpu.arch.event_exit_inst_len);
  4027. } else
  4028. intr |= INTR_TYPE_EXT_INTR;
  4029. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
  4030. }
  4031. static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
  4032. {
  4033. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4034. if (is_guest_mode(vcpu))
  4035. return;
  4036. if (!cpu_has_virtual_nmis()) {
  4037. /*
  4038. * Tracking the NMI-blocked state in software is built upon
  4039. * finding the next open IRQ window. This, in turn, depends on
  4040. * well-behaving guests: They have to keep IRQs disabled at
  4041. * least as long as the NMI handler runs. Otherwise we may
  4042. * cause NMI nesting, maybe breaking the guest. But as this is
  4043. * highly unlikely, we can live with the residual risk.
  4044. */
  4045. vmx->soft_vnmi_blocked = 1;
  4046. vmx->vnmi_blocked_time = 0;
  4047. }
  4048. ++vcpu->stat.nmi_injections;
  4049. vmx->nmi_known_unmasked = false;
  4050. if (vmx->rmode.vm86_active) {
  4051. if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
  4052. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  4053. return;
  4054. }
  4055. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  4056. INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
  4057. }
  4058. static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
  4059. {
  4060. if (!cpu_has_virtual_nmis())
  4061. return to_vmx(vcpu)->soft_vnmi_blocked;
  4062. if (to_vmx(vcpu)->nmi_known_unmasked)
  4063. return false;
  4064. return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
  4065. }
  4066. static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
  4067. {
  4068. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4069. if (!cpu_has_virtual_nmis()) {
  4070. if (vmx->soft_vnmi_blocked != masked) {
  4071. vmx->soft_vnmi_blocked = masked;
  4072. vmx->vnmi_blocked_time = 0;
  4073. }
  4074. } else {
  4075. vmx->nmi_known_unmasked = !masked;
  4076. if (masked)
  4077. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  4078. GUEST_INTR_STATE_NMI);
  4079. else
  4080. vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
  4081. GUEST_INTR_STATE_NMI);
  4082. }
  4083. }
  4084. static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
  4085. {
  4086. if (to_vmx(vcpu)->nested.nested_run_pending)
  4087. return 0;
  4088. if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
  4089. return 0;
  4090. return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
  4091. (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
  4092. | GUEST_INTR_STATE_NMI));
  4093. }
  4094. static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
  4095. {
  4096. return (!to_vmx(vcpu)->nested.nested_run_pending &&
  4097. vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
  4098. !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
  4099. (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
  4100. }
  4101. static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
  4102. {
  4103. int ret;
  4104. struct kvm_userspace_memory_region tss_mem = {
  4105. .slot = TSS_PRIVATE_MEMSLOT,
  4106. .guest_phys_addr = addr,
  4107. .memory_size = PAGE_SIZE * 3,
  4108. .flags = 0,
  4109. };
  4110. ret = kvm_set_memory_region(kvm, &tss_mem);
  4111. if (ret)
  4112. return ret;
  4113. kvm->arch.tss_addr = addr;
  4114. return init_rmode_tss(kvm);
  4115. }
  4116. static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
  4117. {
  4118. switch (vec) {
  4119. case BP_VECTOR:
  4120. /*
  4121. * Update instruction length as we may reinject the exception
  4122. * from user space while in guest debugging mode.
  4123. */
  4124. to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
  4125. vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  4126. if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  4127. return false;
  4128. /* fall through */
  4129. case DB_VECTOR:
  4130. if (vcpu->guest_debug &
  4131. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
  4132. return false;
  4133. /* fall through */
  4134. case DE_VECTOR:
  4135. case OF_VECTOR:
  4136. case BR_VECTOR:
  4137. case UD_VECTOR:
  4138. case DF_VECTOR:
  4139. case SS_VECTOR:
  4140. case GP_VECTOR:
  4141. case MF_VECTOR:
  4142. return true;
  4143. break;
  4144. }
  4145. return false;
  4146. }
  4147. static int handle_rmode_exception(struct kvm_vcpu *vcpu,
  4148. int vec, u32 err_code)
  4149. {
  4150. /*
  4151. * Instruction with address size override prefix opcode 0x67
  4152. * Cause the #SS fault with 0 error code in VM86 mode.
  4153. */
  4154. if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
  4155. if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
  4156. if (vcpu->arch.halt_request) {
  4157. vcpu->arch.halt_request = 0;
  4158. return kvm_emulate_halt(vcpu);
  4159. }
  4160. return 1;
  4161. }
  4162. return 0;
  4163. }
  4164. /*
  4165. * Forward all other exceptions that are valid in real mode.
  4166. * FIXME: Breaks guest debugging in real mode, needs to be fixed with
  4167. * the required debugging infrastructure rework.
  4168. */
  4169. kvm_queue_exception(vcpu, vec);
  4170. return 1;
  4171. }
  4172. /*
  4173. * Trigger machine check on the host. We assume all the MSRs are already set up
  4174. * by the CPU and that we still run on the same CPU as the MCE occurred on.
  4175. * We pass a fake environment to the machine check handler because we want
  4176. * the guest to be always treated like user space, no matter what context
  4177. * it used internally.
  4178. */
  4179. static void kvm_machine_check(void)
  4180. {
  4181. #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
  4182. struct pt_regs regs = {
  4183. .cs = 3, /* Fake ring 3 no matter what the guest ran on */
  4184. .flags = X86_EFLAGS_IF,
  4185. };
  4186. do_machine_check(&regs, 0);
  4187. #endif
  4188. }
  4189. static int handle_machine_check(struct kvm_vcpu *vcpu)
  4190. {
  4191. /* already handled by vcpu_run */
  4192. return 1;
  4193. }
  4194. static int handle_exception(struct kvm_vcpu *vcpu)
  4195. {
  4196. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4197. struct kvm_run *kvm_run = vcpu->run;
  4198. u32 intr_info, ex_no, error_code;
  4199. unsigned long cr2, rip, dr6;
  4200. u32 vect_info;
  4201. enum emulation_result er;
  4202. vect_info = vmx->idt_vectoring_info;
  4203. intr_info = vmx->exit_intr_info;
  4204. if (is_machine_check(intr_info))
  4205. return handle_machine_check(vcpu);
  4206. if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
  4207. return 1; /* already handled by vmx_vcpu_run() */
  4208. if (is_no_device(intr_info)) {
  4209. vmx_fpu_activate(vcpu);
  4210. return 1;
  4211. }
  4212. if (is_invalid_opcode(intr_info)) {
  4213. er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
  4214. if (er != EMULATE_DONE)
  4215. kvm_queue_exception(vcpu, UD_VECTOR);
  4216. return 1;
  4217. }
  4218. error_code = 0;
  4219. if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
  4220. error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
  4221. /*
  4222. * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
  4223. * MMIO, it is better to report an internal error.
  4224. * See the comments in vmx_handle_exit.
  4225. */
  4226. if ((vect_info & VECTORING_INFO_VALID_MASK) &&
  4227. !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
  4228. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  4229. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
  4230. vcpu->run->internal.ndata = 2;
  4231. vcpu->run->internal.data[0] = vect_info;
  4232. vcpu->run->internal.data[1] = intr_info;
  4233. return 0;
  4234. }
  4235. if (is_page_fault(intr_info)) {
  4236. /* EPT won't cause page fault directly */
  4237. BUG_ON(enable_ept);
  4238. cr2 = vmcs_readl(EXIT_QUALIFICATION);
  4239. trace_kvm_page_fault(cr2, error_code);
  4240. if (kvm_event_needs_reinjection(vcpu))
  4241. kvm_mmu_unprotect_page_virt(vcpu, cr2);
  4242. return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
  4243. }
  4244. ex_no = intr_info & INTR_INFO_VECTOR_MASK;
  4245. if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
  4246. return handle_rmode_exception(vcpu, ex_no, error_code);
  4247. switch (ex_no) {
  4248. case DB_VECTOR:
  4249. dr6 = vmcs_readl(EXIT_QUALIFICATION);
  4250. if (!(vcpu->guest_debug &
  4251. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
  4252. vcpu->arch.dr6 &= ~15;
  4253. vcpu->arch.dr6 |= dr6 | DR6_RTM;
  4254. if (!(dr6 & ~DR6_RESERVED)) /* icebp */
  4255. skip_emulated_instruction(vcpu);
  4256. kvm_queue_exception(vcpu, DB_VECTOR);
  4257. return 1;
  4258. }
  4259. kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
  4260. kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
  4261. /* fall through */
  4262. case BP_VECTOR:
  4263. /*
  4264. * Update instruction length as we may reinject #BP from
  4265. * user space while in guest debugging mode. Reading it for
  4266. * #DB as well causes no harm, it is not used in that case.
  4267. */
  4268. vmx->vcpu.arch.event_exit_inst_len =
  4269. vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  4270. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  4271. rip = kvm_rip_read(vcpu);
  4272. kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
  4273. kvm_run->debug.arch.exception = ex_no;
  4274. break;
  4275. default:
  4276. kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
  4277. kvm_run->ex.exception = ex_no;
  4278. kvm_run->ex.error_code = error_code;
  4279. break;
  4280. }
  4281. return 0;
  4282. }
  4283. static int handle_external_interrupt(struct kvm_vcpu *vcpu)
  4284. {
  4285. ++vcpu->stat.irq_exits;
  4286. return 1;
  4287. }
  4288. static int handle_triple_fault(struct kvm_vcpu *vcpu)
  4289. {
  4290. vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
  4291. return 0;
  4292. }
  4293. static int handle_io(struct kvm_vcpu *vcpu)
  4294. {
  4295. unsigned long exit_qualification;
  4296. int size, in, string;
  4297. unsigned port;
  4298. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4299. string = (exit_qualification & 16) != 0;
  4300. in = (exit_qualification & 8) != 0;
  4301. ++vcpu->stat.io_exits;
  4302. if (string || in)
  4303. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  4304. port = exit_qualification >> 16;
  4305. size = (exit_qualification & 7) + 1;
  4306. skip_emulated_instruction(vcpu);
  4307. return kvm_fast_pio_out(vcpu, size, port);
  4308. }
  4309. static void
  4310. vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
  4311. {
  4312. /*
  4313. * Patch in the VMCALL instruction:
  4314. */
  4315. hypercall[0] = 0x0f;
  4316. hypercall[1] = 0x01;
  4317. hypercall[2] = 0xc1;
  4318. }
  4319. static bool nested_cr0_valid(struct vmcs12 *vmcs12, unsigned long val)
  4320. {
  4321. unsigned long always_on = VMXON_CR0_ALWAYSON;
  4322. if (nested_vmx_secondary_ctls_high &
  4323. SECONDARY_EXEC_UNRESTRICTED_GUEST &&
  4324. nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
  4325. always_on &= ~(X86_CR0_PE | X86_CR0_PG);
  4326. return (val & always_on) == always_on;
  4327. }
  4328. /* called to set cr0 as appropriate for a mov-to-cr0 exit. */
  4329. static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
  4330. {
  4331. if (is_guest_mode(vcpu)) {
  4332. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  4333. unsigned long orig_val = val;
  4334. /*
  4335. * We get here when L2 changed cr0 in a way that did not change
  4336. * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
  4337. * but did change L0 shadowed bits. So we first calculate the
  4338. * effective cr0 value that L1 would like to write into the
  4339. * hardware. It consists of the L2-owned bits from the new
  4340. * value combined with the L1-owned bits from L1's guest_cr0.
  4341. */
  4342. val = (val & ~vmcs12->cr0_guest_host_mask) |
  4343. (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
  4344. if (!nested_cr0_valid(vmcs12, val))
  4345. return 1;
  4346. if (kvm_set_cr0(vcpu, val))
  4347. return 1;
  4348. vmcs_writel(CR0_READ_SHADOW, orig_val);
  4349. return 0;
  4350. } else {
  4351. if (to_vmx(vcpu)->nested.vmxon &&
  4352. ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON))
  4353. return 1;
  4354. return kvm_set_cr0(vcpu, val);
  4355. }
  4356. }
  4357. static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
  4358. {
  4359. if (is_guest_mode(vcpu)) {
  4360. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  4361. unsigned long orig_val = val;
  4362. /* analogously to handle_set_cr0 */
  4363. val = (val & ~vmcs12->cr4_guest_host_mask) |
  4364. (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
  4365. if (kvm_set_cr4(vcpu, val))
  4366. return 1;
  4367. vmcs_writel(CR4_READ_SHADOW, orig_val);
  4368. return 0;
  4369. } else
  4370. return kvm_set_cr4(vcpu, val);
  4371. }
  4372. /* called to set cr0 as approriate for clts instruction exit. */
  4373. static void handle_clts(struct kvm_vcpu *vcpu)
  4374. {
  4375. if (is_guest_mode(vcpu)) {
  4376. /*
  4377. * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS
  4378. * but we did (!fpu_active). We need to keep GUEST_CR0.TS on,
  4379. * just pretend it's off (also in arch.cr0 for fpu_activate).
  4380. */
  4381. vmcs_writel(CR0_READ_SHADOW,
  4382. vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS);
  4383. vcpu->arch.cr0 &= ~X86_CR0_TS;
  4384. } else
  4385. vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
  4386. }
  4387. static int handle_cr(struct kvm_vcpu *vcpu)
  4388. {
  4389. unsigned long exit_qualification, val;
  4390. int cr;
  4391. int reg;
  4392. int err;
  4393. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4394. cr = exit_qualification & 15;
  4395. reg = (exit_qualification >> 8) & 15;
  4396. switch ((exit_qualification >> 4) & 3) {
  4397. case 0: /* mov to cr */
  4398. val = kvm_register_readl(vcpu, reg);
  4399. trace_kvm_cr_write(cr, val);
  4400. switch (cr) {
  4401. case 0:
  4402. err = handle_set_cr0(vcpu, val);
  4403. kvm_complete_insn_gp(vcpu, err);
  4404. return 1;
  4405. case 3:
  4406. err = kvm_set_cr3(vcpu, val);
  4407. kvm_complete_insn_gp(vcpu, err);
  4408. return 1;
  4409. case 4:
  4410. err = handle_set_cr4(vcpu, val);
  4411. kvm_complete_insn_gp(vcpu, err);
  4412. return 1;
  4413. case 8: {
  4414. u8 cr8_prev = kvm_get_cr8(vcpu);
  4415. u8 cr8 = (u8)val;
  4416. err = kvm_set_cr8(vcpu, cr8);
  4417. kvm_complete_insn_gp(vcpu, err);
  4418. if (irqchip_in_kernel(vcpu->kvm))
  4419. return 1;
  4420. if (cr8_prev <= cr8)
  4421. return 1;
  4422. vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
  4423. return 0;
  4424. }
  4425. }
  4426. break;
  4427. case 2: /* clts */
  4428. handle_clts(vcpu);
  4429. trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
  4430. skip_emulated_instruction(vcpu);
  4431. vmx_fpu_activate(vcpu);
  4432. return 1;
  4433. case 1: /*mov from cr*/
  4434. switch (cr) {
  4435. case 3:
  4436. val = kvm_read_cr3(vcpu);
  4437. kvm_register_write(vcpu, reg, val);
  4438. trace_kvm_cr_read(cr, val);
  4439. skip_emulated_instruction(vcpu);
  4440. return 1;
  4441. case 8:
  4442. val = kvm_get_cr8(vcpu);
  4443. kvm_register_write(vcpu, reg, val);
  4444. trace_kvm_cr_read(cr, val);
  4445. skip_emulated_instruction(vcpu);
  4446. return 1;
  4447. }
  4448. break;
  4449. case 3: /* lmsw */
  4450. val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
  4451. trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
  4452. kvm_lmsw(vcpu, val);
  4453. skip_emulated_instruction(vcpu);
  4454. return 1;
  4455. default:
  4456. break;
  4457. }
  4458. vcpu->run->exit_reason = 0;
  4459. vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
  4460. (int)(exit_qualification >> 4) & 3, cr);
  4461. return 0;
  4462. }
  4463. static int handle_dr(struct kvm_vcpu *vcpu)
  4464. {
  4465. unsigned long exit_qualification;
  4466. int dr, reg;
  4467. /* Do not handle if the CPL > 0, will trigger GP on re-entry */
  4468. if (!kvm_require_cpl(vcpu, 0))
  4469. return 1;
  4470. dr = vmcs_readl(GUEST_DR7);
  4471. if (dr & DR7_GD) {
  4472. /*
  4473. * As the vm-exit takes precedence over the debug trap, we
  4474. * need to emulate the latter, either for the host or the
  4475. * guest debugging itself.
  4476. */
  4477. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
  4478. vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
  4479. vcpu->run->debug.arch.dr7 = dr;
  4480. vcpu->run->debug.arch.pc =
  4481. vmcs_readl(GUEST_CS_BASE) +
  4482. vmcs_readl(GUEST_RIP);
  4483. vcpu->run->debug.arch.exception = DB_VECTOR;
  4484. vcpu->run->exit_reason = KVM_EXIT_DEBUG;
  4485. return 0;
  4486. } else {
  4487. vcpu->arch.dr7 &= ~DR7_GD;
  4488. vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
  4489. vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
  4490. kvm_queue_exception(vcpu, DB_VECTOR);
  4491. return 1;
  4492. }
  4493. }
  4494. if (vcpu->guest_debug == 0) {
  4495. u32 cpu_based_vm_exec_control;
  4496. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  4497. cpu_based_vm_exec_control &= ~CPU_BASED_MOV_DR_EXITING;
  4498. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  4499. /*
  4500. * No more DR vmexits; force a reload of the debug registers
  4501. * and reenter on this instruction. The next vmexit will
  4502. * retrieve the full state of the debug registers.
  4503. */
  4504. vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
  4505. return 1;
  4506. }
  4507. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4508. dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
  4509. reg = DEBUG_REG_ACCESS_REG(exit_qualification);
  4510. if (exit_qualification & TYPE_MOV_FROM_DR) {
  4511. unsigned long val;
  4512. if (kvm_get_dr(vcpu, dr, &val))
  4513. return 1;
  4514. kvm_register_write(vcpu, reg, val);
  4515. } else
  4516. if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
  4517. return 1;
  4518. skip_emulated_instruction(vcpu);
  4519. return 1;
  4520. }
  4521. static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
  4522. {
  4523. return vcpu->arch.dr6;
  4524. }
  4525. static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
  4526. {
  4527. }
  4528. static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
  4529. {
  4530. u32 cpu_based_vm_exec_control;
  4531. get_debugreg(vcpu->arch.db[0], 0);
  4532. get_debugreg(vcpu->arch.db[1], 1);
  4533. get_debugreg(vcpu->arch.db[2], 2);
  4534. get_debugreg(vcpu->arch.db[3], 3);
  4535. get_debugreg(vcpu->arch.dr6, 6);
  4536. vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
  4537. vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
  4538. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  4539. cpu_based_vm_exec_control |= CPU_BASED_MOV_DR_EXITING;
  4540. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  4541. }
  4542. static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
  4543. {
  4544. vmcs_writel(GUEST_DR7, val);
  4545. }
  4546. static int handle_cpuid(struct kvm_vcpu *vcpu)
  4547. {
  4548. kvm_emulate_cpuid(vcpu);
  4549. return 1;
  4550. }
  4551. static int handle_rdmsr(struct kvm_vcpu *vcpu)
  4552. {
  4553. u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
  4554. u64 data;
  4555. if (vmx_get_msr(vcpu, ecx, &data)) {
  4556. trace_kvm_msr_read_ex(ecx);
  4557. kvm_inject_gp(vcpu, 0);
  4558. return 1;
  4559. }
  4560. trace_kvm_msr_read(ecx, data);
  4561. /* FIXME: handling of bits 32:63 of rax, rdx */
  4562. vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
  4563. vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
  4564. skip_emulated_instruction(vcpu);
  4565. return 1;
  4566. }
  4567. static int handle_wrmsr(struct kvm_vcpu *vcpu)
  4568. {
  4569. struct msr_data msr;
  4570. u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
  4571. u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
  4572. | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
  4573. msr.data = data;
  4574. msr.index = ecx;
  4575. msr.host_initiated = false;
  4576. if (vmx_set_msr(vcpu, &msr) != 0) {
  4577. trace_kvm_msr_write_ex(ecx, data);
  4578. kvm_inject_gp(vcpu, 0);
  4579. return 1;
  4580. }
  4581. trace_kvm_msr_write(ecx, data);
  4582. skip_emulated_instruction(vcpu);
  4583. return 1;
  4584. }
  4585. static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
  4586. {
  4587. kvm_make_request(KVM_REQ_EVENT, vcpu);
  4588. return 1;
  4589. }
  4590. static int handle_interrupt_window(struct kvm_vcpu *vcpu)
  4591. {
  4592. u32 cpu_based_vm_exec_control;
  4593. /* clear pending irq */
  4594. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  4595. cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
  4596. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  4597. kvm_make_request(KVM_REQ_EVENT, vcpu);
  4598. ++vcpu->stat.irq_window_exits;
  4599. /*
  4600. * If the user space waits to inject interrupts, exit as soon as
  4601. * possible
  4602. */
  4603. if (!irqchip_in_kernel(vcpu->kvm) &&
  4604. vcpu->run->request_interrupt_window &&
  4605. !kvm_cpu_has_interrupt(vcpu)) {
  4606. vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
  4607. return 0;
  4608. }
  4609. return 1;
  4610. }
  4611. static int handle_halt(struct kvm_vcpu *vcpu)
  4612. {
  4613. skip_emulated_instruction(vcpu);
  4614. return kvm_emulate_halt(vcpu);
  4615. }
  4616. static int handle_vmcall(struct kvm_vcpu *vcpu)
  4617. {
  4618. skip_emulated_instruction(vcpu);
  4619. kvm_emulate_hypercall(vcpu);
  4620. return 1;
  4621. }
  4622. static int handle_invd(struct kvm_vcpu *vcpu)
  4623. {
  4624. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  4625. }
  4626. static int handle_invlpg(struct kvm_vcpu *vcpu)
  4627. {
  4628. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4629. kvm_mmu_invlpg(vcpu, exit_qualification);
  4630. skip_emulated_instruction(vcpu);
  4631. return 1;
  4632. }
  4633. static int handle_rdpmc(struct kvm_vcpu *vcpu)
  4634. {
  4635. int err;
  4636. err = kvm_rdpmc(vcpu);
  4637. kvm_complete_insn_gp(vcpu, err);
  4638. return 1;
  4639. }
  4640. static int handle_wbinvd(struct kvm_vcpu *vcpu)
  4641. {
  4642. skip_emulated_instruction(vcpu);
  4643. kvm_emulate_wbinvd(vcpu);
  4644. return 1;
  4645. }
  4646. static int handle_xsetbv(struct kvm_vcpu *vcpu)
  4647. {
  4648. u64 new_bv = kvm_read_edx_eax(vcpu);
  4649. u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
  4650. if (kvm_set_xcr(vcpu, index, new_bv) == 0)
  4651. skip_emulated_instruction(vcpu);
  4652. return 1;
  4653. }
  4654. static int handle_apic_access(struct kvm_vcpu *vcpu)
  4655. {
  4656. if (likely(fasteoi)) {
  4657. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4658. int access_type, offset;
  4659. access_type = exit_qualification & APIC_ACCESS_TYPE;
  4660. offset = exit_qualification & APIC_ACCESS_OFFSET;
  4661. /*
  4662. * Sane guest uses MOV to write EOI, with written value
  4663. * not cared. So make a short-circuit here by avoiding
  4664. * heavy instruction emulation.
  4665. */
  4666. if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
  4667. (offset == APIC_EOI)) {
  4668. kvm_lapic_set_eoi(vcpu);
  4669. skip_emulated_instruction(vcpu);
  4670. return 1;
  4671. }
  4672. }
  4673. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  4674. }
  4675. static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
  4676. {
  4677. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4678. int vector = exit_qualification & 0xff;
  4679. /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
  4680. kvm_apic_set_eoi_accelerated(vcpu, vector);
  4681. return 1;
  4682. }
  4683. static int handle_apic_write(struct kvm_vcpu *vcpu)
  4684. {
  4685. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4686. u32 offset = exit_qualification & 0xfff;
  4687. /* APIC-write VM exit is trap-like and thus no need to adjust IP */
  4688. kvm_apic_write_nodecode(vcpu, offset);
  4689. return 1;
  4690. }
  4691. static int handle_task_switch(struct kvm_vcpu *vcpu)
  4692. {
  4693. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4694. unsigned long exit_qualification;
  4695. bool has_error_code = false;
  4696. u32 error_code = 0;
  4697. u16 tss_selector;
  4698. int reason, type, idt_v, idt_index;
  4699. idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
  4700. idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
  4701. type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
  4702. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4703. reason = (u32)exit_qualification >> 30;
  4704. if (reason == TASK_SWITCH_GATE && idt_v) {
  4705. switch (type) {
  4706. case INTR_TYPE_NMI_INTR:
  4707. vcpu->arch.nmi_injected = false;
  4708. vmx_set_nmi_mask(vcpu, true);
  4709. break;
  4710. case INTR_TYPE_EXT_INTR:
  4711. case INTR_TYPE_SOFT_INTR:
  4712. kvm_clear_interrupt_queue(vcpu);
  4713. break;
  4714. case INTR_TYPE_HARD_EXCEPTION:
  4715. if (vmx->idt_vectoring_info &
  4716. VECTORING_INFO_DELIVER_CODE_MASK) {
  4717. has_error_code = true;
  4718. error_code =
  4719. vmcs_read32(IDT_VECTORING_ERROR_CODE);
  4720. }
  4721. /* fall through */
  4722. case INTR_TYPE_SOFT_EXCEPTION:
  4723. kvm_clear_exception_queue(vcpu);
  4724. break;
  4725. default:
  4726. break;
  4727. }
  4728. }
  4729. tss_selector = exit_qualification;
  4730. if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
  4731. type != INTR_TYPE_EXT_INTR &&
  4732. type != INTR_TYPE_NMI_INTR))
  4733. skip_emulated_instruction(vcpu);
  4734. if (kvm_task_switch(vcpu, tss_selector,
  4735. type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
  4736. has_error_code, error_code) == EMULATE_FAIL) {
  4737. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  4738. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  4739. vcpu->run->internal.ndata = 0;
  4740. return 0;
  4741. }
  4742. /* clear all local breakpoint enable flags */
  4743. vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~0x55);
  4744. /*
  4745. * TODO: What about debug traps on tss switch?
  4746. * Are we supposed to inject them and update dr6?
  4747. */
  4748. return 1;
  4749. }
  4750. static int handle_ept_violation(struct kvm_vcpu *vcpu)
  4751. {
  4752. unsigned long exit_qualification;
  4753. gpa_t gpa;
  4754. u32 error_code;
  4755. int gla_validity;
  4756. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4757. gla_validity = (exit_qualification >> 7) & 0x3;
  4758. if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
  4759. printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
  4760. printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
  4761. (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
  4762. vmcs_readl(GUEST_LINEAR_ADDRESS));
  4763. printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
  4764. (long unsigned int)exit_qualification);
  4765. vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
  4766. vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
  4767. return 0;
  4768. }
  4769. /*
  4770. * EPT violation happened while executing iret from NMI,
  4771. * "blocked by NMI" bit has to be set before next VM entry.
  4772. * There are errata that may cause this bit to not be set:
  4773. * AAK134, BY25.
  4774. */
  4775. if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
  4776. cpu_has_virtual_nmis() &&
  4777. (exit_qualification & INTR_INFO_UNBLOCK_NMI))
  4778. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
  4779. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  4780. trace_kvm_page_fault(gpa, exit_qualification);
  4781. /* It is a write fault? */
  4782. error_code = exit_qualification & (1U << 1);
  4783. /* It is a fetch fault? */
  4784. error_code |= (exit_qualification & (1U << 2)) << 2;
  4785. /* ept page table is present? */
  4786. error_code |= (exit_qualification >> 3) & 0x1;
  4787. vcpu->arch.exit_qualification = exit_qualification;
  4788. return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
  4789. }
  4790. static u64 ept_rsvd_mask(u64 spte, int level)
  4791. {
  4792. int i;
  4793. u64 mask = 0;
  4794. for (i = 51; i > boot_cpu_data.x86_phys_bits; i--)
  4795. mask |= (1ULL << i);
  4796. if (level == 4)
  4797. /* bits 7:3 reserved */
  4798. mask |= 0xf8;
  4799. else if (spte & (1ULL << 7))
  4800. /*
  4801. * 1GB/2MB page, bits 29:12 or 20:12 reserved respectively,
  4802. * level == 1 if the hypervisor is using the ignored bit 7.
  4803. */
  4804. mask |= (PAGE_SIZE << ((level - 1) * 9)) - PAGE_SIZE;
  4805. else if (level > 1)
  4806. /* bits 6:3 reserved */
  4807. mask |= 0x78;
  4808. return mask;
  4809. }
  4810. static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte,
  4811. int level)
  4812. {
  4813. printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level);
  4814. /* 010b (write-only) */
  4815. WARN_ON((spte & 0x7) == 0x2);
  4816. /* 110b (write/execute) */
  4817. WARN_ON((spte & 0x7) == 0x6);
  4818. /* 100b (execute-only) and value not supported by logical processor */
  4819. if (!cpu_has_vmx_ept_execute_only())
  4820. WARN_ON((spte & 0x7) == 0x4);
  4821. /* not 000b */
  4822. if ((spte & 0x7)) {
  4823. u64 rsvd_bits = spte & ept_rsvd_mask(spte, level);
  4824. if (rsvd_bits != 0) {
  4825. printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n",
  4826. __func__, rsvd_bits);
  4827. WARN_ON(1);
  4828. }
  4829. /* bits 5:3 are _not_ reserved for large page or leaf page */
  4830. if ((rsvd_bits & 0x38) == 0) {
  4831. u64 ept_mem_type = (spte & 0x38) >> 3;
  4832. if (ept_mem_type == 2 || ept_mem_type == 3 ||
  4833. ept_mem_type == 7) {
  4834. printk(KERN_ERR "%s: ept_mem_type=0x%llx\n",
  4835. __func__, ept_mem_type);
  4836. WARN_ON(1);
  4837. }
  4838. }
  4839. }
  4840. }
  4841. static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
  4842. {
  4843. u64 sptes[4];
  4844. int nr_sptes, i, ret;
  4845. gpa_t gpa;
  4846. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  4847. if (!kvm_io_bus_write(vcpu->kvm, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
  4848. skip_emulated_instruction(vcpu);
  4849. return 1;
  4850. }
  4851. ret = handle_mmio_page_fault_common(vcpu, gpa, true);
  4852. if (likely(ret == RET_MMIO_PF_EMULATE))
  4853. return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) ==
  4854. EMULATE_DONE;
  4855. if (unlikely(ret == RET_MMIO_PF_INVALID))
  4856. return kvm_mmu_page_fault(vcpu, gpa, 0, NULL, 0);
  4857. if (unlikely(ret == RET_MMIO_PF_RETRY))
  4858. return 1;
  4859. /* It is the real ept misconfig */
  4860. printk(KERN_ERR "EPT: Misconfiguration.\n");
  4861. printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa);
  4862. nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes);
  4863. for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i)
  4864. ept_misconfig_inspect_spte(vcpu, sptes[i-1], i);
  4865. vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
  4866. vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
  4867. return 0;
  4868. }
  4869. static int handle_nmi_window(struct kvm_vcpu *vcpu)
  4870. {
  4871. u32 cpu_based_vm_exec_control;
  4872. /* clear pending NMI */
  4873. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  4874. cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
  4875. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  4876. ++vcpu->stat.nmi_window_exits;
  4877. kvm_make_request(KVM_REQ_EVENT, vcpu);
  4878. return 1;
  4879. }
  4880. static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
  4881. {
  4882. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4883. enum emulation_result err = EMULATE_DONE;
  4884. int ret = 1;
  4885. u32 cpu_exec_ctrl;
  4886. bool intr_window_requested;
  4887. unsigned count = 130;
  4888. cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  4889. intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
  4890. while (vmx->emulation_required && count-- != 0) {
  4891. if (intr_window_requested && vmx_interrupt_allowed(vcpu))
  4892. return handle_interrupt_window(&vmx->vcpu);
  4893. if (test_bit(KVM_REQ_EVENT, &vcpu->requests))
  4894. return 1;
  4895. err = emulate_instruction(vcpu, EMULTYPE_NO_REEXECUTE);
  4896. if (err == EMULATE_USER_EXIT) {
  4897. ++vcpu->stat.mmio_exits;
  4898. ret = 0;
  4899. goto out;
  4900. }
  4901. if (err != EMULATE_DONE) {
  4902. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  4903. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  4904. vcpu->run->internal.ndata = 0;
  4905. return 0;
  4906. }
  4907. if (vcpu->arch.halt_request) {
  4908. vcpu->arch.halt_request = 0;
  4909. ret = kvm_emulate_halt(vcpu);
  4910. goto out;
  4911. }
  4912. if (signal_pending(current))
  4913. goto out;
  4914. if (need_resched())
  4915. schedule();
  4916. }
  4917. out:
  4918. return ret;
  4919. }
  4920. static int __grow_ple_window(int val)
  4921. {
  4922. if (ple_window_grow < 1)
  4923. return ple_window;
  4924. val = min(val, ple_window_actual_max);
  4925. if (ple_window_grow < ple_window)
  4926. val *= ple_window_grow;
  4927. else
  4928. val += ple_window_grow;
  4929. return val;
  4930. }
  4931. static int __shrink_ple_window(int val, int modifier, int minimum)
  4932. {
  4933. if (modifier < 1)
  4934. return ple_window;
  4935. if (modifier < ple_window)
  4936. val /= modifier;
  4937. else
  4938. val -= modifier;
  4939. return max(val, minimum);
  4940. }
  4941. static void grow_ple_window(struct kvm_vcpu *vcpu)
  4942. {
  4943. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4944. int old = vmx->ple_window;
  4945. vmx->ple_window = __grow_ple_window(old);
  4946. if (vmx->ple_window != old)
  4947. vmx->ple_window_dirty = true;
  4948. trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
  4949. }
  4950. static void shrink_ple_window(struct kvm_vcpu *vcpu)
  4951. {
  4952. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4953. int old = vmx->ple_window;
  4954. vmx->ple_window = __shrink_ple_window(old,
  4955. ple_window_shrink, ple_window);
  4956. if (vmx->ple_window != old)
  4957. vmx->ple_window_dirty = true;
  4958. trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
  4959. }
  4960. /*
  4961. * ple_window_actual_max is computed to be one grow_ple_window() below
  4962. * ple_window_max. (See __grow_ple_window for the reason.)
  4963. * This prevents overflows, because ple_window_max is int.
  4964. * ple_window_max effectively rounded down to a multiple of ple_window_grow in
  4965. * this process.
  4966. * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
  4967. */
  4968. static void update_ple_window_actual_max(void)
  4969. {
  4970. ple_window_actual_max =
  4971. __shrink_ple_window(max(ple_window_max, ple_window),
  4972. ple_window_grow, INT_MIN);
  4973. }
  4974. /*
  4975. * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
  4976. * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
  4977. */
  4978. static int handle_pause(struct kvm_vcpu *vcpu)
  4979. {
  4980. if (ple_gap)
  4981. grow_ple_window(vcpu);
  4982. skip_emulated_instruction(vcpu);
  4983. kvm_vcpu_on_spin(vcpu);
  4984. return 1;
  4985. }
  4986. static int handle_nop(struct kvm_vcpu *vcpu)
  4987. {
  4988. skip_emulated_instruction(vcpu);
  4989. return 1;
  4990. }
  4991. static int handle_mwait(struct kvm_vcpu *vcpu)
  4992. {
  4993. printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
  4994. return handle_nop(vcpu);
  4995. }
  4996. static int handle_monitor(struct kvm_vcpu *vcpu)
  4997. {
  4998. printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
  4999. return handle_nop(vcpu);
  5000. }
  5001. /*
  5002. * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12.
  5003. * We could reuse a single VMCS for all the L2 guests, but we also want the
  5004. * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this
  5005. * allows keeping them loaded on the processor, and in the future will allow
  5006. * optimizations where prepare_vmcs02 doesn't need to set all the fields on
  5007. * every entry if they never change.
  5008. * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE
  5009. * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first.
  5010. *
  5011. * The following functions allocate and free a vmcs02 in this pool.
  5012. */
  5013. /* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */
  5014. static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx)
  5015. {
  5016. struct vmcs02_list *item;
  5017. list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
  5018. if (item->vmptr == vmx->nested.current_vmptr) {
  5019. list_move(&item->list, &vmx->nested.vmcs02_pool);
  5020. return &item->vmcs02;
  5021. }
  5022. if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) {
  5023. /* Recycle the least recently used VMCS. */
  5024. item = list_entry(vmx->nested.vmcs02_pool.prev,
  5025. struct vmcs02_list, list);
  5026. item->vmptr = vmx->nested.current_vmptr;
  5027. list_move(&item->list, &vmx->nested.vmcs02_pool);
  5028. return &item->vmcs02;
  5029. }
  5030. /* Create a new VMCS */
  5031. item = kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL);
  5032. if (!item)
  5033. return NULL;
  5034. item->vmcs02.vmcs = alloc_vmcs();
  5035. if (!item->vmcs02.vmcs) {
  5036. kfree(item);
  5037. return NULL;
  5038. }
  5039. loaded_vmcs_init(&item->vmcs02);
  5040. item->vmptr = vmx->nested.current_vmptr;
  5041. list_add(&(item->list), &(vmx->nested.vmcs02_pool));
  5042. vmx->nested.vmcs02_num++;
  5043. return &item->vmcs02;
  5044. }
  5045. /* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */
  5046. static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr)
  5047. {
  5048. struct vmcs02_list *item;
  5049. list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
  5050. if (item->vmptr == vmptr) {
  5051. free_loaded_vmcs(&item->vmcs02);
  5052. list_del(&item->list);
  5053. kfree(item);
  5054. vmx->nested.vmcs02_num--;
  5055. return;
  5056. }
  5057. }
  5058. /*
  5059. * Free all VMCSs saved for this vcpu, except the one pointed by
  5060. * vmx->loaded_vmcs. We must be running L1, so vmx->loaded_vmcs
  5061. * must be &vmx->vmcs01.
  5062. */
  5063. static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx)
  5064. {
  5065. struct vmcs02_list *item, *n;
  5066. WARN_ON(vmx->loaded_vmcs != &vmx->vmcs01);
  5067. list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) {
  5068. /*
  5069. * Something will leak if the above WARN triggers. Better than
  5070. * a use-after-free.
  5071. */
  5072. if (vmx->loaded_vmcs == &item->vmcs02)
  5073. continue;
  5074. free_loaded_vmcs(&item->vmcs02);
  5075. list_del(&item->list);
  5076. kfree(item);
  5077. vmx->nested.vmcs02_num--;
  5078. }
  5079. }
  5080. /*
  5081. * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
  5082. * set the success or error code of an emulated VMX instruction, as specified
  5083. * by Vol 2B, VMX Instruction Reference, "Conventions".
  5084. */
  5085. static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
  5086. {
  5087. vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
  5088. & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
  5089. X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
  5090. }
  5091. static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
  5092. {
  5093. vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
  5094. & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
  5095. X86_EFLAGS_SF | X86_EFLAGS_OF))
  5096. | X86_EFLAGS_CF);
  5097. }
  5098. static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
  5099. u32 vm_instruction_error)
  5100. {
  5101. if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
  5102. /*
  5103. * failValid writes the error number to the current VMCS, which
  5104. * can't be done there isn't a current VMCS.
  5105. */
  5106. nested_vmx_failInvalid(vcpu);
  5107. return;
  5108. }
  5109. vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
  5110. & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
  5111. X86_EFLAGS_SF | X86_EFLAGS_OF))
  5112. | X86_EFLAGS_ZF);
  5113. get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
  5114. /*
  5115. * We don't need to force a shadow sync because
  5116. * VM_INSTRUCTION_ERROR is not shadowed
  5117. */
  5118. }
  5119. static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
  5120. {
  5121. struct vcpu_vmx *vmx =
  5122. container_of(timer, struct vcpu_vmx, nested.preemption_timer);
  5123. vmx->nested.preemption_timer_expired = true;
  5124. kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
  5125. kvm_vcpu_kick(&vmx->vcpu);
  5126. return HRTIMER_NORESTART;
  5127. }
  5128. /*
  5129. * Decode the memory-address operand of a vmx instruction, as recorded on an
  5130. * exit caused by such an instruction (run by a guest hypervisor).
  5131. * On success, returns 0. When the operand is invalid, returns 1 and throws
  5132. * #UD or #GP.
  5133. */
  5134. static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
  5135. unsigned long exit_qualification,
  5136. u32 vmx_instruction_info, gva_t *ret)
  5137. {
  5138. /*
  5139. * According to Vol. 3B, "Information for VM Exits Due to Instruction
  5140. * Execution", on an exit, vmx_instruction_info holds most of the
  5141. * addressing components of the operand. Only the displacement part
  5142. * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
  5143. * For how an actual address is calculated from all these components,
  5144. * refer to Vol. 1, "Operand Addressing".
  5145. */
  5146. int scaling = vmx_instruction_info & 3;
  5147. int addr_size = (vmx_instruction_info >> 7) & 7;
  5148. bool is_reg = vmx_instruction_info & (1u << 10);
  5149. int seg_reg = (vmx_instruction_info >> 15) & 7;
  5150. int index_reg = (vmx_instruction_info >> 18) & 0xf;
  5151. bool index_is_valid = !(vmx_instruction_info & (1u << 22));
  5152. int base_reg = (vmx_instruction_info >> 23) & 0xf;
  5153. bool base_is_valid = !(vmx_instruction_info & (1u << 27));
  5154. if (is_reg) {
  5155. kvm_queue_exception(vcpu, UD_VECTOR);
  5156. return 1;
  5157. }
  5158. /* Addr = segment_base + offset */
  5159. /* offset = base + [index * scale] + displacement */
  5160. *ret = vmx_get_segment_base(vcpu, seg_reg);
  5161. if (base_is_valid)
  5162. *ret += kvm_register_read(vcpu, base_reg);
  5163. if (index_is_valid)
  5164. *ret += kvm_register_read(vcpu, index_reg)<<scaling;
  5165. *ret += exit_qualification; /* holds the displacement */
  5166. if (addr_size == 1) /* 32 bit */
  5167. *ret &= 0xffffffff;
  5168. /*
  5169. * TODO: throw #GP (and return 1) in various cases that the VM*
  5170. * instructions require it - e.g., offset beyond segment limit,
  5171. * unusable or unreadable/unwritable segment, non-canonical 64-bit
  5172. * address, and so on. Currently these are not checked.
  5173. */
  5174. return 0;
  5175. }
  5176. /*
  5177. * This function performs the various checks including
  5178. * - if it's 4KB aligned
  5179. * - No bits beyond the physical address width are set
  5180. * - Returns 0 on success or else 1
  5181. * (Intel SDM Section 30.3)
  5182. */
  5183. static int nested_vmx_check_vmptr(struct kvm_vcpu *vcpu, int exit_reason,
  5184. gpa_t *vmpointer)
  5185. {
  5186. gva_t gva;
  5187. gpa_t vmptr;
  5188. struct x86_exception e;
  5189. struct page *page;
  5190. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5191. int maxphyaddr = cpuid_maxphyaddr(vcpu);
  5192. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  5193. vmcs_read32(VMX_INSTRUCTION_INFO), &gva))
  5194. return 1;
  5195. if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr,
  5196. sizeof(vmptr), &e)) {
  5197. kvm_inject_page_fault(vcpu, &e);
  5198. return 1;
  5199. }
  5200. switch (exit_reason) {
  5201. case EXIT_REASON_VMON:
  5202. /*
  5203. * SDM 3: 24.11.5
  5204. * The first 4 bytes of VMXON region contain the supported
  5205. * VMCS revision identifier
  5206. *
  5207. * Note - IA32_VMX_BASIC[48] will never be 1
  5208. * for the nested case;
  5209. * which replaces physical address width with 32
  5210. *
  5211. */
  5212. if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
  5213. nested_vmx_failInvalid(vcpu);
  5214. skip_emulated_instruction(vcpu);
  5215. return 1;
  5216. }
  5217. page = nested_get_page(vcpu, vmptr);
  5218. if (page == NULL ||
  5219. *(u32 *)kmap(page) != VMCS12_REVISION) {
  5220. nested_vmx_failInvalid(vcpu);
  5221. kunmap(page);
  5222. skip_emulated_instruction(vcpu);
  5223. return 1;
  5224. }
  5225. kunmap(page);
  5226. vmx->nested.vmxon_ptr = vmptr;
  5227. break;
  5228. case EXIT_REASON_VMCLEAR:
  5229. if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
  5230. nested_vmx_failValid(vcpu,
  5231. VMXERR_VMCLEAR_INVALID_ADDRESS);
  5232. skip_emulated_instruction(vcpu);
  5233. return 1;
  5234. }
  5235. if (vmptr == vmx->nested.vmxon_ptr) {
  5236. nested_vmx_failValid(vcpu,
  5237. VMXERR_VMCLEAR_VMXON_POINTER);
  5238. skip_emulated_instruction(vcpu);
  5239. return 1;
  5240. }
  5241. break;
  5242. case EXIT_REASON_VMPTRLD:
  5243. if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
  5244. nested_vmx_failValid(vcpu,
  5245. VMXERR_VMPTRLD_INVALID_ADDRESS);
  5246. skip_emulated_instruction(vcpu);
  5247. return 1;
  5248. }
  5249. if (vmptr == vmx->nested.vmxon_ptr) {
  5250. nested_vmx_failValid(vcpu,
  5251. VMXERR_VMCLEAR_VMXON_POINTER);
  5252. skip_emulated_instruction(vcpu);
  5253. return 1;
  5254. }
  5255. break;
  5256. default:
  5257. return 1; /* shouldn't happen */
  5258. }
  5259. if (vmpointer)
  5260. *vmpointer = vmptr;
  5261. return 0;
  5262. }
  5263. /*
  5264. * Emulate the VMXON instruction.
  5265. * Currently, we just remember that VMX is active, and do not save or even
  5266. * inspect the argument to VMXON (the so-called "VMXON pointer") because we
  5267. * do not currently need to store anything in that guest-allocated memory
  5268. * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
  5269. * argument is different from the VMXON pointer (which the spec says they do).
  5270. */
  5271. static int handle_vmon(struct kvm_vcpu *vcpu)
  5272. {
  5273. struct kvm_segment cs;
  5274. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5275. struct vmcs *shadow_vmcs;
  5276. const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
  5277. | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  5278. /* The Intel VMX Instruction Reference lists a bunch of bits that
  5279. * are prerequisite to running VMXON, most notably cr4.VMXE must be
  5280. * set to 1 (see vmx_set_cr4() for when we allow the guest to set this).
  5281. * Otherwise, we should fail with #UD. We test these now:
  5282. */
  5283. if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) ||
  5284. !kvm_read_cr0_bits(vcpu, X86_CR0_PE) ||
  5285. (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
  5286. kvm_queue_exception(vcpu, UD_VECTOR);
  5287. return 1;
  5288. }
  5289. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  5290. if (is_long_mode(vcpu) && !cs.l) {
  5291. kvm_queue_exception(vcpu, UD_VECTOR);
  5292. return 1;
  5293. }
  5294. if (vmx_get_cpl(vcpu)) {
  5295. kvm_inject_gp(vcpu, 0);
  5296. return 1;
  5297. }
  5298. if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMON, NULL))
  5299. return 1;
  5300. if (vmx->nested.vmxon) {
  5301. nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
  5302. skip_emulated_instruction(vcpu);
  5303. return 1;
  5304. }
  5305. if ((vmx->nested.msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
  5306. != VMXON_NEEDED_FEATURES) {
  5307. kvm_inject_gp(vcpu, 0);
  5308. return 1;
  5309. }
  5310. if (enable_shadow_vmcs) {
  5311. shadow_vmcs = alloc_vmcs();
  5312. if (!shadow_vmcs)
  5313. return -ENOMEM;
  5314. /* mark vmcs as shadow */
  5315. shadow_vmcs->revision_id |= (1u << 31);
  5316. /* init shadow vmcs */
  5317. vmcs_clear(shadow_vmcs);
  5318. vmx->nested.current_shadow_vmcs = shadow_vmcs;
  5319. }
  5320. INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool));
  5321. vmx->nested.vmcs02_num = 0;
  5322. hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
  5323. HRTIMER_MODE_REL);
  5324. vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
  5325. vmx->nested.vmxon = true;
  5326. skip_emulated_instruction(vcpu);
  5327. nested_vmx_succeed(vcpu);
  5328. return 1;
  5329. }
  5330. /*
  5331. * Intel's VMX Instruction Reference specifies a common set of prerequisites
  5332. * for running VMX instructions (except VMXON, whose prerequisites are
  5333. * slightly different). It also specifies what exception to inject otherwise.
  5334. */
  5335. static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
  5336. {
  5337. struct kvm_segment cs;
  5338. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5339. if (!vmx->nested.vmxon) {
  5340. kvm_queue_exception(vcpu, UD_VECTOR);
  5341. return 0;
  5342. }
  5343. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  5344. if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) ||
  5345. (is_long_mode(vcpu) && !cs.l)) {
  5346. kvm_queue_exception(vcpu, UD_VECTOR);
  5347. return 0;
  5348. }
  5349. if (vmx_get_cpl(vcpu)) {
  5350. kvm_inject_gp(vcpu, 0);
  5351. return 0;
  5352. }
  5353. return 1;
  5354. }
  5355. static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
  5356. {
  5357. u32 exec_control;
  5358. if (vmx->nested.current_vmptr == -1ull)
  5359. return;
  5360. /* current_vmptr and current_vmcs12 are always set/reset together */
  5361. if (WARN_ON(vmx->nested.current_vmcs12 == NULL))
  5362. return;
  5363. if (enable_shadow_vmcs) {
  5364. /* copy to memory all shadowed fields in case
  5365. they were modified */
  5366. copy_shadow_to_vmcs12(vmx);
  5367. vmx->nested.sync_shadow_vmcs = false;
  5368. exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  5369. exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
  5370. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
  5371. vmcs_write64(VMCS_LINK_POINTER, -1ull);
  5372. }
  5373. kunmap(vmx->nested.current_vmcs12_page);
  5374. nested_release_page(vmx->nested.current_vmcs12_page);
  5375. vmx->nested.current_vmptr = -1ull;
  5376. vmx->nested.current_vmcs12 = NULL;
  5377. }
  5378. /*
  5379. * Free whatever needs to be freed from vmx->nested when L1 goes down, or
  5380. * just stops using VMX.
  5381. */
  5382. static void free_nested(struct vcpu_vmx *vmx)
  5383. {
  5384. if (!vmx->nested.vmxon)
  5385. return;
  5386. vmx->nested.vmxon = false;
  5387. nested_release_vmcs12(vmx);
  5388. if (enable_shadow_vmcs)
  5389. free_vmcs(vmx->nested.current_shadow_vmcs);
  5390. /* Unpin physical memory we referred to in current vmcs02 */
  5391. if (vmx->nested.apic_access_page) {
  5392. nested_release_page(vmx->nested.apic_access_page);
  5393. vmx->nested.apic_access_page = NULL;
  5394. }
  5395. if (vmx->nested.virtual_apic_page) {
  5396. nested_release_page(vmx->nested.virtual_apic_page);
  5397. vmx->nested.virtual_apic_page = NULL;
  5398. }
  5399. nested_free_all_saved_vmcss(vmx);
  5400. }
  5401. /* Emulate the VMXOFF instruction */
  5402. static int handle_vmoff(struct kvm_vcpu *vcpu)
  5403. {
  5404. if (!nested_vmx_check_permission(vcpu))
  5405. return 1;
  5406. free_nested(to_vmx(vcpu));
  5407. skip_emulated_instruction(vcpu);
  5408. nested_vmx_succeed(vcpu);
  5409. return 1;
  5410. }
  5411. /* Emulate the VMCLEAR instruction */
  5412. static int handle_vmclear(struct kvm_vcpu *vcpu)
  5413. {
  5414. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5415. gpa_t vmptr;
  5416. struct vmcs12 *vmcs12;
  5417. struct page *page;
  5418. if (!nested_vmx_check_permission(vcpu))
  5419. return 1;
  5420. if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMCLEAR, &vmptr))
  5421. return 1;
  5422. if (vmptr == vmx->nested.current_vmptr)
  5423. nested_release_vmcs12(vmx);
  5424. page = nested_get_page(vcpu, vmptr);
  5425. if (page == NULL) {
  5426. /*
  5427. * For accurate processor emulation, VMCLEAR beyond available
  5428. * physical memory should do nothing at all. However, it is
  5429. * possible that a nested vmx bug, not a guest hypervisor bug,
  5430. * resulted in this case, so let's shut down before doing any
  5431. * more damage:
  5432. */
  5433. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  5434. return 1;
  5435. }
  5436. vmcs12 = kmap(page);
  5437. vmcs12->launch_state = 0;
  5438. kunmap(page);
  5439. nested_release_page(page);
  5440. nested_free_vmcs02(vmx, vmptr);
  5441. skip_emulated_instruction(vcpu);
  5442. nested_vmx_succeed(vcpu);
  5443. return 1;
  5444. }
  5445. static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
  5446. /* Emulate the VMLAUNCH instruction */
  5447. static int handle_vmlaunch(struct kvm_vcpu *vcpu)
  5448. {
  5449. return nested_vmx_run(vcpu, true);
  5450. }
  5451. /* Emulate the VMRESUME instruction */
  5452. static int handle_vmresume(struct kvm_vcpu *vcpu)
  5453. {
  5454. return nested_vmx_run(vcpu, false);
  5455. }
  5456. enum vmcs_field_type {
  5457. VMCS_FIELD_TYPE_U16 = 0,
  5458. VMCS_FIELD_TYPE_U64 = 1,
  5459. VMCS_FIELD_TYPE_U32 = 2,
  5460. VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
  5461. };
  5462. static inline int vmcs_field_type(unsigned long field)
  5463. {
  5464. if (0x1 & field) /* the *_HIGH fields are all 32 bit */
  5465. return VMCS_FIELD_TYPE_U32;
  5466. return (field >> 13) & 0x3 ;
  5467. }
  5468. static inline int vmcs_field_readonly(unsigned long field)
  5469. {
  5470. return (((field >> 10) & 0x3) == 1);
  5471. }
  5472. /*
  5473. * Read a vmcs12 field. Since these can have varying lengths and we return
  5474. * one type, we chose the biggest type (u64) and zero-extend the return value
  5475. * to that size. Note that the caller, handle_vmread, might need to use only
  5476. * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
  5477. * 64-bit fields are to be returned).
  5478. */
  5479. static inline bool vmcs12_read_any(struct kvm_vcpu *vcpu,
  5480. unsigned long field, u64 *ret)
  5481. {
  5482. short offset = vmcs_field_to_offset(field);
  5483. char *p;
  5484. if (offset < 0)
  5485. return 0;
  5486. p = ((char *)(get_vmcs12(vcpu))) + offset;
  5487. switch (vmcs_field_type(field)) {
  5488. case VMCS_FIELD_TYPE_NATURAL_WIDTH:
  5489. *ret = *((natural_width *)p);
  5490. return 1;
  5491. case VMCS_FIELD_TYPE_U16:
  5492. *ret = *((u16 *)p);
  5493. return 1;
  5494. case VMCS_FIELD_TYPE_U32:
  5495. *ret = *((u32 *)p);
  5496. return 1;
  5497. case VMCS_FIELD_TYPE_U64:
  5498. *ret = *((u64 *)p);
  5499. return 1;
  5500. default:
  5501. return 0; /* can never happen. */
  5502. }
  5503. }
  5504. static inline bool vmcs12_write_any(struct kvm_vcpu *vcpu,
  5505. unsigned long field, u64 field_value){
  5506. short offset = vmcs_field_to_offset(field);
  5507. char *p = ((char *) get_vmcs12(vcpu)) + offset;
  5508. if (offset < 0)
  5509. return false;
  5510. switch (vmcs_field_type(field)) {
  5511. case VMCS_FIELD_TYPE_U16:
  5512. *(u16 *)p = field_value;
  5513. return true;
  5514. case VMCS_FIELD_TYPE_U32:
  5515. *(u32 *)p = field_value;
  5516. return true;
  5517. case VMCS_FIELD_TYPE_U64:
  5518. *(u64 *)p = field_value;
  5519. return true;
  5520. case VMCS_FIELD_TYPE_NATURAL_WIDTH:
  5521. *(natural_width *)p = field_value;
  5522. return true;
  5523. default:
  5524. return false; /* can never happen. */
  5525. }
  5526. }
  5527. static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
  5528. {
  5529. int i;
  5530. unsigned long field;
  5531. u64 field_value;
  5532. struct vmcs *shadow_vmcs = vmx->nested.current_shadow_vmcs;
  5533. const unsigned long *fields = shadow_read_write_fields;
  5534. const int num_fields = max_shadow_read_write_fields;
  5535. vmcs_load(shadow_vmcs);
  5536. for (i = 0; i < num_fields; i++) {
  5537. field = fields[i];
  5538. switch (vmcs_field_type(field)) {
  5539. case VMCS_FIELD_TYPE_U16:
  5540. field_value = vmcs_read16(field);
  5541. break;
  5542. case VMCS_FIELD_TYPE_U32:
  5543. field_value = vmcs_read32(field);
  5544. break;
  5545. case VMCS_FIELD_TYPE_U64:
  5546. field_value = vmcs_read64(field);
  5547. break;
  5548. case VMCS_FIELD_TYPE_NATURAL_WIDTH:
  5549. field_value = vmcs_readl(field);
  5550. break;
  5551. }
  5552. vmcs12_write_any(&vmx->vcpu, field, field_value);
  5553. }
  5554. vmcs_clear(shadow_vmcs);
  5555. vmcs_load(vmx->loaded_vmcs->vmcs);
  5556. }
  5557. static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
  5558. {
  5559. const unsigned long *fields[] = {
  5560. shadow_read_write_fields,
  5561. shadow_read_only_fields
  5562. };
  5563. const int max_fields[] = {
  5564. max_shadow_read_write_fields,
  5565. max_shadow_read_only_fields
  5566. };
  5567. int i, q;
  5568. unsigned long field;
  5569. u64 field_value = 0;
  5570. struct vmcs *shadow_vmcs = vmx->nested.current_shadow_vmcs;
  5571. vmcs_load(shadow_vmcs);
  5572. for (q = 0; q < ARRAY_SIZE(fields); q++) {
  5573. for (i = 0; i < max_fields[q]; i++) {
  5574. field = fields[q][i];
  5575. vmcs12_read_any(&vmx->vcpu, field, &field_value);
  5576. switch (vmcs_field_type(field)) {
  5577. case VMCS_FIELD_TYPE_U16:
  5578. vmcs_write16(field, (u16)field_value);
  5579. break;
  5580. case VMCS_FIELD_TYPE_U32:
  5581. vmcs_write32(field, (u32)field_value);
  5582. break;
  5583. case VMCS_FIELD_TYPE_U64:
  5584. vmcs_write64(field, (u64)field_value);
  5585. break;
  5586. case VMCS_FIELD_TYPE_NATURAL_WIDTH:
  5587. vmcs_writel(field, (long)field_value);
  5588. break;
  5589. }
  5590. }
  5591. }
  5592. vmcs_clear(shadow_vmcs);
  5593. vmcs_load(vmx->loaded_vmcs->vmcs);
  5594. }
  5595. /*
  5596. * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
  5597. * used before) all generate the same failure when it is missing.
  5598. */
  5599. static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
  5600. {
  5601. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5602. if (vmx->nested.current_vmptr == -1ull) {
  5603. nested_vmx_failInvalid(vcpu);
  5604. skip_emulated_instruction(vcpu);
  5605. return 0;
  5606. }
  5607. return 1;
  5608. }
  5609. static int handle_vmread(struct kvm_vcpu *vcpu)
  5610. {
  5611. unsigned long field;
  5612. u64 field_value;
  5613. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  5614. u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  5615. gva_t gva = 0;
  5616. if (!nested_vmx_check_permission(vcpu) ||
  5617. !nested_vmx_check_vmcs12(vcpu))
  5618. return 1;
  5619. /* Decode instruction info and find the field to read */
  5620. field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
  5621. /* Read the field, zero-extended to a u64 field_value */
  5622. if (!vmcs12_read_any(vcpu, field, &field_value)) {
  5623. nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
  5624. skip_emulated_instruction(vcpu);
  5625. return 1;
  5626. }
  5627. /*
  5628. * Now copy part of this value to register or memory, as requested.
  5629. * Note that the number of bits actually copied is 32 or 64 depending
  5630. * on the guest's mode (32 or 64 bit), not on the given field's length.
  5631. */
  5632. if (vmx_instruction_info & (1u << 10)) {
  5633. kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
  5634. field_value);
  5635. } else {
  5636. if (get_vmx_mem_address(vcpu, exit_qualification,
  5637. vmx_instruction_info, &gva))
  5638. return 1;
  5639. /* _system ok, as nested_vmx_check_permission verified cpl=0 */
  5640. kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
  5641. &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
  5642. }
  5643. nested_vmx_succeed(vcpu);
  5644. skip_emulated_instruction(vcpu);
  5645. return 1;
  5646. }
  5647. static int handle_vmwrite(struct kvm_vcpu *vcpu)
  5648. {
  5649. unsigned long field;
  5650. gva_t gva;
  5651. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  5652. u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  5653. /* The value to write might be 32 or 64 bits, depending on L1's long
  5654. * mode, and eventually we need to write that into a field of several
  5655. * possible lengths. The code below first zero-extends the value to 64
  5656. * bit (field_value), and then copies only the approriate number of
  5657. * bits into the vmcs12 field.
  5658. */
  5659. u64 field_value = 0;
  5660. struct x86_exception e;
  5661. if (!nested_vmx_check_permission(vcpu) ||
  5662. !nested_vmx_check_vmcs12(vcpu))
  5663. return 1;
  5664. if (vmx_instruction_info & (1u << 10))
  5665. field_value = kvm_register_readl(vcpu,
  5666. (((vmx_instruction_info) >> 3) & 0xf));
  5667. else {
  5668. if (get_vmx_mem_address(vcpu, exit_qualification,
  5669. vmx_instruction_info, &gva))
  5670. return 1;
  5671. if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
  5672. &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
  5673. kvm_inject_page_fault(vcpu, &e);
  5674. return 1;
  5675. }
  5676. }
  5677. field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
  5678. if (vmcs_field_readonly(field)) {
  5679. nested_vmx_failValid(vcpu,
  5680. VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
  5681. skip_emulated_instruction(vcpu);
  5682. return 1;
  5683. }
  5684. if (!vmcs12_write_any(vcpu, field, field_value)) {
  5685. nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
  5686. skip_emulated_instruction(vcpu);
  5687. return 1;
  5688. }
  5689. nested_vmx_succeed(vcpu);
  5690. skip_emulated_instruction(vcpu);
  5691. return 1;
  5692. }
  5693. /* Emulate the VMPTRLD instruction */
  5694. static int handle_vmptrld(struct kvm_vcpu *vcpu)
  5695. {
  5696. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5697. gpa_t vmptr;
  5698. u32 exec_control;
  5699. if (!nested_vmx_check_permission(vcpu))
  5700. return 1;
  5701. if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMPTRLD, &vmptr))
  5702. return 1;
  5703. if (vmx->nested.current_vmptr != vmptr) {
  5704. struct vmcs12 *new_vmcs12;
  5705. struct page *page;
  5706. page = nested_get_page(vcpu, vmptr);
  5707. if (page == NULL) {
  5708. nested_vmx_failInvalid(vcpu);
  5709. skip_emulated_instruction(vcpu);
  5710. return 1;
  5711. }
  5712. new_vmcs12 = kmap(page);
  5713. if (new_vmcs12->revision_id != VMCS12_REVISION) {
  5714. kunmap(page);
  5715. nested_release_page_clean(page);
  5716. nested_vmx_failValid(vcpu,
  5717. VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
  5718. skip_emulated_instruction(vcpu);
  5719. return 1;
  5720. }
  5721. nested_release_vmcs12(vmx);
  5722. vmx->nested.current_vmptr = vmptr;
  5723. vmx->nested.current_vmcs12 = new_vmcs12;
  5724. vmx->nested.current_vmcs12_page = page;
  5725. if (enable_shadow_vmcs) {
  5726. exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  5727. exec_control |= SECONDARY_EXEC_SHADOW_VMCS;
  5728. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
  5729. vmcs_write64(VMCS_LINK_POINTER,
  5730. __pa(vmx->nested.current_shadow_vmcs));
  5731. vmx->nested.sync_shadow_vmcs = true;
  5732. }
  5733. }
  5734. nested_vmx_succeed(vcpu);
  5735. skip_emulated_instruction(vcpu);
  5736. return 1;
  5737. }
  5738. /* Emulate the VMPTRST instruction */
  5739. static int handle_vmptrst(struct kvm_vcpu *vcpu)
  5740. {
  5741. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  5742. u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  5743. gva_t vmcs_gva;
  5744. struct x86_exception e;
  5745. if (!nested_vmx_check_permission(vcpu))
  5746. return 1;
  5747. if (get_vmx_mem_address(vcpu, exit_qualification,
  5748. vmx_instruction_info, &vmcs_gva))
  5749. return 1;
  5750. /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */
  5751. if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
  5752. (void *)&to_vmx(vcpu)->nested.current_vmptr,
  5753. sizeof(u64), &e)) {
  5754. kvm_inject_page_fault(vcpu, &e);
  5755. return 1;
  5756. }
  5757. nested_vmx_succeed(vcpu);
  5758. skip_emulated_instruction(vcpu);
  5759. return 1;
  5760. }
  5761. /* Emulate the INVEPT instruction */
  5762. static int handle_invept(struct kvm_vcpu *vcpu)
  5763. {
  5764. u32 vmx_instruction_info, types;
  5765. unsigned long type;
  5766. gva_t gva;
  5767. struct x86_exception e;
  5768. struct {
  5769. u64 eptp, gpa;
  5770. } operand;
  5771. if (!(nested_vmx_secondary_ctls_high & SECONDARY_EXEC_ENABLE_EPT) ||
  5772. !(nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
  5773. kvm_queue_exception(vcpu, UD_VECTOR);
  5774. return 1;
  5775. }
  5776. if (!nested_vmx_check_permission(vcpu))
  5777. return 1;
  5778. if (!kvm_read_cr0_bits(vcpu, X86_CR0_PE)) {
  5779. kvm_queue_exception(vcpu, UD_VECTOR);
  5780. return 1;
  5781. }
  5782. vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  5783. type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
  5784. types = (nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
  5785. if (!(types & (1UL << type))) {
  5786. nested_vmx_failValid(vcpu,
  5787. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  5788. return 1;
  5789. }
  5790. /* According to the Intel VMX instruction reference, the memory
  5791. * operand is read even if it isn't needed (e.g., for type==global)
  5792. */
  5793. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  5794. vmx_instruction_info, &gva))
  5795. return 1;
  5796. if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
  5797. sizeof(operand), &e)) {
  5798. kvm_inject_page_fault(vcpu, &e);
  5799. return 1;
  5800. }
  5801. switch (type) {
  5802. case VMX_EPT_EXTENT_GLOBAL:
  5803. kvm_mmu_sync_roots(vcpu);
  5804. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  5805. nested_vmx_succeed(vcpu);
  5806. break;
  5807. default:
  5808. /* Trap single context invalidation invept calls */
  5809. BUG_ON(1);
  5810. break;
  5811. }
  5812. skip_emulated_instruction(vcpu);
  5813. return 1;
  5814. }
  5815. /*
  5816. * The exit handlers return 1 if the exit was handled fully and guest execution
  5817. * may resume. Otherwise they set the kvm_run parameter to indicate what needs
  5818. * to be done to userspace and return 0.
  5819. */
  5820. static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
  5821. [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
  5822. [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
  5823. [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
  5824. [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
  5825. [EXIT_REASON_IO_INSTRUCTION] = handle_io,
  5826. [EXIT_REASON_CR_ACCESS] = handle_cr,
  5827. [EXIT_REASON_DR_ACCESS] = handle_dr,
  5828. [EXIT_REASON_CPUID] = handle_cpuid,
  5829. [EXIT_REASON_MSR_READ] = handle_rdmsr,
  5830. [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
  5831. [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
  5832. [EXIT_REASON_HLT] = handle_halt,
  5833. [EXIT_REASON_INVD] = handle_invd,
  5834. [EXIT_REASON_INVLPG] = handle_invlpg,
  5835. [EXIT_REASON_RDPMC] = handle_rdpmc,
  5836. [EXIT_REASON_VMCALL] = handle_vmcall,
  5837. [EXIT_REASON_VMCLEAR] = handle_vmclear,
  5838. [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
  5839. [EXIT_REASON_VMPTRLD] = handle_vmptrld,
  5840. [EXIT_REASON_VMPTRST] = handle_vmptrst,
  5841. [EXIT_REASON_VMREAD] = handle_vmread,
  5842. [EXIT_REASON_VMRESUME] = handle_vmresume,
  5843. [EXIT_REASON_VMWRITE] = handle_vmwrite,
  5844. [EXIT_REASON_VMOFF] = handle_vmoff,
  5845. [EXIT_REASON_VMON] = handle_vmon,
  5846. [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
  5847. [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
  5848. [EXIT_REASON_APIC_WRITE] = handle_apic_write,
  5849. [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
  5850. [EXIT_REASON_WBINVD] = handle_wbinvd,
  5851. [EXIT_REASON_XSETBV] = handle_xsetbv,
  5852. [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
  5853. [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
  5854. [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
  5855. [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
  5856. [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
  5857. [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
  5858. [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
  5859. [EXIT_REASON_INVEPT] = handle_invept,
  5860. };
  5861. static const int kvm_vmx_max_exit_handlers =
  5862. ARRAY_SIZE(kvm_vmx_exit_handlers);
  5863. static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
  5864. struct vmcs12 *vmcs12)
  5865. {
  5866. unsigned long exit_qualification;
  5867. gpa_t bitmap, last_bitmap;
  5868. unsigned int port;
  5869. int size;
  5870. u8 b;
  5871. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
  5872. return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
  5873. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  5874. port = exit_qualification >> 16;
  5875. size = (exit_qualification & 7) + 1;
  5876. last_bitmap = (gpa_t)-1;
  5877. b = -1;
  5878. while (size > 0) {
  5879. if (port < 0x8000)
  5880. bitmap = vmcs12->io_bitmap_a;
  5881. else if (port < 0x10000)
  5882. bitmap = vmcs12->io_bitmap_b;
  5883. else
  5884. return 1;
  5885. bitmap += (port & 0x7fff) / 8;
  5886. if (last_bitmap != bitmap)
  5887. if (kvm_read_guest(vcpu->kvm, bitmap, &b, 1))
  5888. return 1;
  5889. if (b & (1 << (port & 7)))
  5890. return 1;
  5891. port++;
  5892. size--;
  5893. last_bitmap = bitmap;
  5894. }
  5895. return 0;
  5896. }
  5897. /*
  5898. * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
  5899. * rather than handle it ourselves in L0. I.e., check whether L1 expressed
  5900. * disinterest in the current event (read or write a specific MSR) by using an
  5901. * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
  5902. */
  5903. static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
  5904. struct vmcs12 *vmcs12, u32 exit_reason)
  5905. {
  5906. u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
  5907. gpa_t bitmap;
  5908. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
  5909. return 1;
  5910. /*
  5911. * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
  5912. * for the four combinations of read/write and low/high MSR numbers.
  5913. * First we need to figure out which of the four to use:
  5914. */
  5915. bitmap = vmcs12->msr_bitmap;
  5916. if (exit_reason == EXIT_REASON_MSR_WRITE)
  5917. bitmap += 2048;
  5918. if (msr_index >= 0xc0000000) {
  5919. msr_index -= 0xc0000000;
  5920. bitmap += 1024;
  5921. }
  5922. /* Then read the msr_index'th bit from this bitmap: */
  5923. if (msr_index < 1024*8) {
  5924. unsigned char b;
  5925. if (kvm_read_guest(vcpu->kvm, bitmap + msr_index/8, &b, 1))
  5926. return 1;
  5927. return 1 & (b >> (msr_index & 7));
  5928. } else
  5929. return 1; /* let L1 handle the wrong parameter */
  5930. }
  5931. /*
  5932. * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
  5933. * rather than handle it ourselves in L0. I.e., check if L1 wanted to
  5934. * intercept (via guest_host_mask etc.) the current event.
  5935. */
  5936. static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
  5937. struct vmcs12 *vmcs12)
  5938. {
  5939. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  5940. int cr = exit_qualification & 15;
  5941. int reg = (exit_qualification >> 8) & 15;
  5942. unsigned long val = kvm_register_readl(vcpu, reg);
  5943. switch ((exit_qualification >> 4) & 3) {
  5944. case 0: /* mov to cr */
  5945. switch (cr) {
  5946. case 0:
  5947. if (vmcs12->cr0_guest_host_mask &
  5948. (val ^ vmcs12->cr0_read_shadow))
  5949. return 1;
  5950. break;
  5951. case 3:
  5952. if ((vmcs12->cr3_target_count >= 1 &&
  5953. vmcs12->cr3_target_value0 == val) ||
  5954. (vmcs12->cr3_target_count >= 2 &&
  5955. vmcs12->cr3_target_value1 == val) ||
  5956. (vmcs12->cr3_target_count >= 3 &&
  5957. vmcs12->cr3_target_value2 == val) ||
  5958. (vmcs12->cr3_target_count >= 4 &&
  5959. vmcs12->cr3_target_value3 == val))
  5960. return 0;
  5961. if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
  5962. return 1;
  5963. break;
  5964. case 4:
  5965. if (vmcs12->cr4_guest_host_mask &
  5966. (vmcs12->cr4_read_shadow ^ val))
  5967. return 1;
  5968. break;
  5969. case 8:
  5970. if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
  5971. return 1;
  5972. break;
  5973. }
  5974. break;
  5975. case 2: /* clts */
  5976. if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
  5977. (vmcs12->cr0_read_shadow & X86_CR0_TS))
  5978. return 1;
  5979. break;
  5980. case 1: /* mov from cr */
  5981. switch (cr) {
  5982. case 3:
  5983. if (vmcs12->cpu_based_vm_exec_control &
  5984. CPU_BASED_CR3_STORE_EXITING)
  5985. return 1;
  5986. break;
  5987. case 8:
  5988. if (vmcs12->cpu_based_vm_exec_control &
  5989. CPU_BASED_CR8_STORE_EXITING)
  5990. return 1;
  5991. break;
  5992. }
  5993. break;
  5994. case 3: /* lmsw */
  5995. /*
  5996. * lmsw can change bits 1..3 of cr0, and only set bit 0 of
  5997. * cr0. Other attempted changes are ignored, with no exit.
  5998. */
  5999. if (vmcs12->cr0_guest_host_mask & 0xe &
  6000. (val ^ vmcs12->cr0_read_shadow))
  6001. return 1;
  6002. if ((vmcs12->cr0_guest_host_mask & 0x1) &&
  6003. !(vmcs12->cr0_read_shadow & 0x1) &&
  6004. (val & 0x1))
  6005. return 1;
  6006. break;
  6007. }
  6008. return 0;
  6009. }
  6010. /*
  6011. * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
  6012. * should handle it ourselves in L0 (and then continue L2). Only call this
  6013. * when in is_guest_mode (L2).
  6014. */
  6015. static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu)
  6016. {
  6017. u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  6018. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6019. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  6020. u32 exit_reason = vmx->exit_reason;
  6021. trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
  6022. vmcs_readl(EXIT_QUALIFICATION),
  6023. vmx->idt_vectoring_info,
  6024. intr_info,
  6025. vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
  6026. KVM_ISA_VMX);
  6027. if (vmx->nested.nested_run_pending)
  6028. return 0;
  6029. if (unlikely(vmx->fail)) {
  6030. pr_info_ratelimited("%s failed vm entry %x\n", __func__,
  6031. vmcs_read32(VM_INSTRUCTION_ERROR));
  6032. return 1;
  6033. }
  6034. switch (exit_reason) {
  6035. case EXIT_REASON_EXCEPTION_NMI:
  6036. if (!is_exception(intr_info))
  6037. return 0;
  6038. else if (is_page_fault(intr_info))
  6039. return enable_ept;
  6040. else if (is_no_device(intr_info) &&
  6041. !(vmcs12->guest_cr0 & X86_CR0_TS))
  6042. return 0;
  6043. return vmcs12->exception_bitmap &
  6044. (1u << (intr_info & INTR_INFO_VECTOR_MASK));
  6045. case EXIT_REASON_EXTERNAL_INTERRUPT:
  6046. return 0;
  6047. case EXIT_REASON_TRIPLE_FAULT:
  6048. return 1;
  6049. case EXIT_REASON_PENDING_INTERRUPT:
  6050. return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
  6051. case EXIT_REASON_NMI_WINDOW:
  6052. return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
  6053. case EXIT_REASON_TASK_SWITCH:
  6054. return 1;
  6055. case EXIT_REASON_CPUID:
  6056. if (kvm_register_read(vcpu, VCPU_REGS_RAX) == 0xa)
  6057. return 0;
  6058. return 1;
  6059. case EXIT_REASON_HLT:
  6060. return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
  6061. case EXIT_REASON_INVD:
  6062. return 1;
  6063. case EXIT_REASON_INVLPG:
  6064. return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
  6065. case EXIT_REASON_RDPMC:
  6066. return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
  6067. case EXIT_REASON_RDTSC:
  6068. return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
  6069. case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
  6070. case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
  6071. case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
  6072. case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
  6073. case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
  6074. case EXIT_REASON_INVEPT:
  6075. /*
  6076. * VMX instructions trap unconditionally. This allows L1 to
  6077. * emulate them for its L2 guest, i.e., allows 3-level nesting!
  6078. */
  6079. return 1;
  6080. case EXIT_REASON_CR_ACCESS:
  6081. return nested_vmx_exit_handled_cr(vcpu, vmcs12);
  6082. case EXIT_REASON_DR_ACCESS:
  6083. return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
  6084. case EXIT_REASON_IO_INSTRUCTION:
  6085. return nested_vmx_exit_handled_io(vcpu, vmcs12);
  6086. case EXIT_REASON_MSR_READ:
  6087. case EXIT_REASON_MSR_WRITE:
  6088. return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
  6089. case EXIT_REASON_INVALID_STATE:
  6090. return 1;
  6091. case EXIT_REASON_MWAIT_INSTRUCTION:
  6092. return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
  6093. case EXIT_REASON_MONITOR_INSTRUCTION:
  6094. return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
  6095. case EXIT_REASON_PAUSE_INSTRUCTION:
  6096. return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
  6097. nested_cpu_has2(vmcs12,
  6098. SECONDARY_EXEC_PAUSE_LOOP_EXITING);
  6099. case EXIT_REASON_MCE_DURING_VMENTRY:
  6100. return 0;
  6101. case EXIT_REASON_TPR_BELOW_THRESHOLD:
  6102. return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
  6103. case EXIT_REASON_APIC_ACCESS:
  6104. return nested_cpu_has2(vmcs12,
  6105. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
  6106. case EXIT_REASON_EPT_VIOLATION:
  6107. /*
  6108. * L0 always deals with the EPT violation. If nested EPT is
  6109. * used, and the nested mmu code discovers that the address is
  6110. * missing in the guest EPT table (EPT12), the EPT violation
  6111. * will be injected with nested_ept_inject_page_fault()
  6112. */
  6113. return 0;
  6114. case EXIT_REASON_EPT_MISCONFIG:
  6115. /*
  6116. * L2 never uses directly L1's EPT, but rather L0's own EPT
  6117. * table (shadow on EPT) or a merged EPT table that L0 built
  6118. * (EPT on EPT). So any problems with the structure of the
  6119. * table is L0's fault.
  6120. */
  6121. return 0;
  6122. case EXIT_REASON_WBINVD:
  6123. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
  6124. case EXIT_REASON_XSETBV:
  6125. return 1;
  6126. default:
  6127. return 1;
  6128. }
  6129. }
  6130. static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
  6131. {
  6132. *info1 = vmcs_readl(EXIT_QUALIFICATION);
  6133. *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
  6134. }
  6135. /*
  6136. * The guest has exited. See if we can fix it or if we need userspace
  6137. * assistance.
  6138. */
  6139. static int vmx_handle_exit(struct kvm_vcpu *vcpu)
  6140. {
  6141. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6142. u32 exit_reason = vmx->exit_reason;
  6143. u32 vectoring_info = vmx->idt_vectoring_info;
  6144. /* If guest state is invalid, start emulating */
  6145. if (vmx->emulation_required)
  6146. return handle_invalid_guest_state(vcpu);
  6147. if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) {
  6148. nested_vmx_vmexit(vcpu, exit_reason,
  6149. vmcs_read32(VM_EXIT_INTR_INFO),
  6150. vmcs_readl(EXIT_QUALIFICATION));
  6151. return 1;
  6152. }
  6153. if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
  6154. vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  6155. vcpu->run->fail_entry.hardware_entry_failure_reason
  6156. = exit_reason;
  6157. return 0;
  6158. }
  6159. if (unlikely(vmx->fail)) {
  6160. vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  6161. vcpu->run->fail_entry.hardware_entry_failure_reason
  6162. = vmcs_read32(VM_INSTRUCTION_ERROR);
  6163. return 0;
  6164. }
  6165. /*
  6166. * Note:
  6167. * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
  6168. * delivery event since it indicates guest is accessing MMIO.
  6169. * The vm-exit can be triggered again after return to guest that
  6170. * will cause infinite loop.
  6171. */
  6172. if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
  6173. (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
  6174. exit_reason != EXIT_REASON_EPT_VIOLATION &&
  6175. exit_reason != EXIT_REASON_TASK_SWITCH)) {
  6176. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  6177. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
  6178. vcpu->run->internal.ndata = 2;
  6179. vcpu->run->internal.data[0] = vectoring_info;
  6180. vcpu->run->internal.data[1] = exit_reason;
  6181. return 0;
  6182. }
  6183. if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked &&
  6184. !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis(
  6185. get_vmcs12(vcpu))))) {
  6186. if (vmx_interrupt_allowed(vcpu)) {
  6187. vmx->soft_vnmi_blocked = 0;
  6188. } else if (vmx->vnmi_blocked_time > 1000000000LL &&
  6189. vcpu->arch.nmi_pending) {
  6190. /*
  6191. * This CPU don't support us in finding the end of an
  6192. * NMI-blocked window if the guest runs with IRQs
  6193. * disabled. So we pull the trigger after 1 s of
  6194. * futile waiting, but inform the user about this.
  6195. */
  6196. printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
  6197. "state on VCPU %d after 1 s timeout\n",
  6198. __func__, vcpu->vcpu_id);
  6199. vmx->soft_vnmi_blocked = 0;
  6200. }
  6201. }
  6202. if (exit_reason < kvm_vmx_max_exit_handlers
  6203. && kvm_vmx_exit_handlers[exit_reason])
  6204. return kvm_vmx_exit_handlers[exit_reason](vcpu);
  6205. else {
  6206. vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
  6207. vcpu->run->hw.hardware_exit_reason = exit_reason;
  6208. }
  6209. return 0;
  6210. }
  6211. static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
  6212. {
  6213. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  6214. if (is_guest_mode(vcpu) &&
  6215. nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
  6216. return;
  6217. if (irr == -1 || tpr < irr) {
  6218. vmcs_write32(TPR_THRESHOLD, 0);
  6219. return;
  6220. }
  6221. vmcs_write32(TPR_THRESHOLD, irr);
  6222. }
  6223. static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
  6224. {
  6225. u32 sec_exec_control;
  6226. /*
  6227. * There is not point to enable virtualize x2apic without enable
  6228. * apicv
  6229. */
  6230. if (!cpu_has_vmx_virtualize_x2apic_mode() ||
  6231. !vmx_vm_has_apicv(vcpu->kvm))
  6232. return;
  6233. if (!vm_need_tpr_shadow(vcpu->kvm))
  6234. return;
  6235. sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  6236. if (set) {
  6237. sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  6238. sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  6239. } else {
  6240. sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  6241. sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  6242. }
  6243. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
  6244. vmx_set_msr_bitmap(vcpu);
  6245. }
  6246. static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
  6247. {
  6248. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6249. /*
  6250. * Currently we do not handle the nested case where L2 has an
  6251. * APIC access page of its own; that page is still pinned.
  6252. * Hence, we skip the case where the VCPU is in guest mode _and_
  6253. * L1 prepared an APIC access page for L2.
  6254. *
  6255. * For the case where L1 and L2 share the same APIC access page
  6256. * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
  6257. * in the vmcs12), this function will only update either the vmcs01
  6258. * or the vmcs02. If the former, the vmcs02 will be updated by
  6259. * prepare_vmcs02. If the latter, the vmcs01 will be updated in
  6260. * the next L2->L1 exit.
  6261. */
  6262. if (!is_guest_mode(vcpu) ||
  6263. !nested_cpu_has2(vmx->nested.current_vmcs12,
  6264. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
  6265. vmcs_write64(APIC_ACCESS_ADDR, hpa);
  6266. }
  6267. static void vmx_hwapic_isr_update(struct kvm *kvm, int isr)
  6268. {
  6269. u16 status;
  6270. u8 old;
  6271. if (!vmx_vm_has_apicv(kvm))
  6272. return;
  6273. if (isr == -1)
  6274. isr = 0;
  6275. status = vmcs_read16(GUEST_INTR_STATUS);
  6276. old = status >> 8;
  6277. if (isr != old) {
  6278. status &= 0xff;
  6279. status |= isr << 8;
  6280. vmcs_write16(GUEST_INTR_STATUS, status);
  6281. }
  6282. }
  6283. static void vmx_set_rvi(int vector)
  6284. {
  6285. u16 status;
  6286. u8 old;
  6287. status = vmcs_read16(GUEST_INTR_STATUS);
  6288. old = (u8)status & 0xff;
  6289. if ((u8)vector != old) {
  6290. status &= ~0xff;
  6291. status |= (u8)vector;
  6292. vmcs_write16(GUEST_INTR_STATUS, status);
  6293. }
  6294. }
  6295. static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
  6296. {
  6297. if (max_irr == -1)
  6298. return;
  6299. /*
  6300. * If a vmexit is needed, vmx_check_nested_events handles it.
  6301. */
  6302. if (is_guest_mode(vcpu) && nested_exit_on_intr(vcpu))
  6303. return;
  6304. if (!is_guest_mode(vcpu)) {
  6305. vmx_set_rvi(max_irr);
  6306. return;
  6307. }
  6308. /*
  6309. * Fall back to pre-APICv interrupt injection since L2
  6310. * is run without virtual interrupt delivery.
  6311. */
  6312. if (!kvm_event_needs_reinjection(vcpu) &&
  6313. vmx_interrupt_allowed(vcpu)) {
  6314. kvm_queue_interrupt(vcpu, max_irr, false);
  6315. vmx_inject_irq(vcpu);
  6316. }
  6317. }
  6318. static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
  6319. {
  6320. if (!vmx_vm_has_apicv(vcpu->kvm))
  6321. return;
  6322. vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
  6323. vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
  6324. vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
  6325. vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
  6326. }
  6327. static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
  6328. {
  6329. u32 exit_intr_info;
  6330. if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
  6331. || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
  6332. return;
  6333. vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  6334. exit_intr_info = vmx->exit_intr_info;
  6335. /* Handle machine checks before interrupts are enabled */
  6336. if (is_machine_check(exit_intr_info))
  6337. kvm_machine_check();
  6338. /* We need to handle NMIs before interrupts are enabled */
  6339. if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
  6340. (exit_intr_info & INTR_INFO_VALID_MASK)) {
  6341. kvm_before_handle_nmi(&vmx->vcpu);
  6342. asm("int $2");
  6343. kvm_after_handle_nmi(&vmx->vcpu);
  6344. }
  6345. }
  6346. static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
  6347. {
  6348. u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  6349. /*
  6350. * If external interrupt exists, IF bit is set in rflags/eflags on the
  6351. * interrupt stack frame, and interrupt will be enabled on a return
  6352. * from interrupt handler.
  6353. */
  6354. if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
  6355. == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
  6356. unsigned int vector;
  6357. unsigned long entry;
  6358. gate_desc *desc;
  6359. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6360. #ifdef CONFIG_X86_64
  6361. unsigned long tmp;
  6362. #endif
  6363. vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
  6364. desc = (gate_desc *)vmx->host_idt_base + vector;
  6365. entry = gate_offset(*desc);
  6366. asm volatile(
  6367. #ifdef CONFIG_X86_64
  6368. "mov %%" _ASM_SP ", %[sp]\n\t"
  6369. "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
  6370. "push $%c[ss]\n\t"
  6371. "push %[sp]\n\t"
  6372. #endif
  6373. "pushf\n\t"
  6374. "orl $0x200, (%%" _ASM_SP ")\n\t"
  6375. __ASM_SIZE(push) " $%c[cs]\n\t"
  6376. "call *%[entry]\n\t"
  6377. :
  6378. #ifdef CONFIG_X86_64
  6379. [sp]"=&r"(tmp)
  6380. #endif
  6381. :
  6382. [entry]"r"(entry),
  6383. [ss]"i"(__KERNEL_DS),
  6384. [cs]"i"(__KERNEL_CS)
  6385. );
  6386. } else
  6387. local_irq_enable();
  6388. }
  6389. static bool vmx_mpx_supported(void)
  6390. {
  6391. return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
  6392. (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
  6393. }
  6394. static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
  6395. {
  6396. u32 exit_intr_info;
  6397. bool unblock_nmi;
  6398. u8 vector;
  6399. bool idtv_info_valid;
  6400. idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
  6401. if (cpu_has_virtual_nmis()) {
  6402. if (vmx->nmi_known_unmasked)
  6403. return;
  6404. /*
  6405. * Can't use vmx->exit_intr_info since we're not sure what
  6406. * the exit reason is.
  6407. */
  6408. exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  6409. unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
  6410. vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
  6411. /*
  6412. * SDM 3: 27.7.1.2 (September 2008)
  6413. * Re-set bit "block by NMI" before VM entry if vmexit caused by
  6414. * a guest IRET fault.
  6415. * SDM 3: 23.2.2 (September 2008)
  6416. * Bit 12 is undefined in any of the following cases:
  6417. * If the VM exit sets the valid bit in the IDT-vectoring
  6418. * information field.
  6419. * If the VM exit is due to a double fault.
  6420. */
  6421. if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
  6422. vector != DF_VECTOR && !idtv_info_valid)
  6423. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  6424. GUEST_INTR_STATE_NMI);
  6425. else
  6426. vmx->nmi_known_unmasked =
  6427. !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
  6428. & GUEST_INTR_STATE_NMI);
  6429. } else if (unlikely(vmx->soft_vnmi_blocked))
  6430. vmx->vnmi_blocked_time +=
  6431. ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
  6432. }
  6433. static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
  6434. u32 idt_vectoring_info,
  6435. int instr_len_field,
  6436. int error_code_field)
  6437. {
  6438. u8 vector;
  6439. int type;
  6440. bool idtv_info_valid;
  6441. idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
  6442. vcpu->arch.nmi_injected = false;
  6443. kvm_clear_exception_queue(vcpu);
  6444. kvm_clear_interrupt_queue(vcpu);
  6445. if (!idtv_info_valid)
  6446. return;
  6447. kvm_make_request(KVM_REQ_EVENT, vcpu);
  6448. vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
  6449. type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
  6450. switch (type) {
  6451. case INTR_TYPE_NMI_INTR:
  6452. vcpu->arch.nmi_injected = true;
  6453. /*
  6454. * SDM 3: 27.7.1.2 (September 2008)
  6455. * Clear bit "block by NMI" before VM entry if a NMI
  6456. * delivery faulted.
  6457. */
  6458. vmx_set_nmi_mask(vcpu, false);
  6459. break;
  6460. case INTR_TYPE_SOFT_EXCEPTION:
  6461. vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
  6462. /* fall through */
  6463. case INTR_TYPE_HARD_EXCEPTION:
  6464. if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
  6465. u32 err = vmcs_read32(error_code_field);
  6466. kvm_requeue_exception_e(vcpu, vector, err);
  6467. } else
  6468. kvm_requeue_exception(vcpu, vector);
  6469. break;
  6470. case INTR_TYPE_SOFT_INTR:
  6471. vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
  6472. /* fall through */
  6473. case INTR_TYPE_EXT_INTR:
  6474. kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
  6475. break;
  6476. default:
  6477. break;
  6478. }
  6479. }
  6480. static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
  6481. {
  6482. __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
  6483. VM_EXIT_INSTRUCTION_LEN,
  6484. IDT_VECTORING_ERROR_CODE);
  6485. }
  6486. static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
  6487. {
  6488. __vmx_complete_interrupts(vcpu,
  6489. vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
  6490. VM_ENTRY_INSTRUCTION_LEN,
  6491. VM_ENTRY_EXCEPTION_ERROR_CODE);
  6492. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
  6493. }
  6494. static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
  6495. {
  6496. int i, nr_msrs;
  6497. struct perf_guest_switch_msr *msrs;
  6498. msrs = perf_guest_get_msrs(&nr_msrs);
  6499. if (!msrs)
  6500. return;
  6501. for (i = 0; i < nr_msrs; i++)
  6502. if (msrs[i].host == msrs[i].guest)
  6503. clear_atomic_switch_msr(vmx, msrs[i].msr);
  6504. else
  6505. add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
  6506. msrs[i].host);
  6507. }
  6508. static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
  6509. {
  6510. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6511. unsigned long debugctlmsr, cr4;
  6512. /* Record the guest's net vcpu time for enforced NMI injections. */
  6513. if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
  6514. vmx->entry_time = ktime_get();
  6515. /* Don't enter VMX if guest state is invalid, let the exit handler
  6516. start emulation until we arrive back to a valid state */
  6517. if (vmx->emulation_required)
  6518. return;
  6519. if (vmx->ple_window_dirty) {
  6520. vmx->ple_window_dirty = false;
  6521. vmcs_write32(PLE_WINDOW, vmx->ple_window);
  6522. }
  6523. if (vmx->nested.sync_shadow_vmcs) {
  6524. copy_vmcs12_to_shadow(vmx);
  6525. vmx->nested.sync_shadow_vmcs = false;
  6526. }
  6527. if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
  6528. vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
  6529. if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
  6530. vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
  6531. cr4 = read_cr4();
  6532. if (unlikely(cr4 != vmx->host_state.vmcs_host_cr4)) {
  6533. vmcs_writel(HOST_CR4, cr4);
  6534. vmx->host_state.vmcs_host_cr4 = cr4;
  6535. }
  6536. /* When single-stepping over STI and MOV SS, we must clear the
  6537. * corresponding interruptibility bits in the guest state. Otherwise
  6538. * vmentry fails as it then expects bit 14 (BS) in pending debug
  6539. * exceptions being set, but that's not correct for the guest debugging
  6540. * case. */
  6541. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
  6542. vmx_set_interrupt_shadow(vcpu, 0);
  6543. atomic_switch_perf_msrs(vmx);
  6544. debugctlmsr = get_debugctlmsr();
  6545. vmx->__launched = vmx->loaded_vmcs->launched;
  6546. asm(
  6547. /* Store host registers */
  6548. "push %%" _ASM_DX "; push %%" _ASM_BP ";"
  6549. "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
  6550. "push %%" _ASM_CX " \n\t"
  6551. "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
  6552. "je 1f \n\t"
  6553. "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
  6554. __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
  6555. "1: \n\t"
  6556. /* Reload cr2 if changed */
  6557. "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
  6558. "mov %%cr2, %%" _ASM_DX " \n\t"
  6559. "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
  6560. "je 2f \n\t"
  6561. "mov %%" _ASM_AX", %%cr2 \n\t"
  6562. "2: \n\t"
  6563. /* Check if vmlaunch of vmresume is needed */
  6564. "cmpl $0, %c[launched](%0) \n\t"
  6565. /* Load guest registers. Don't clobber flags. */
  6566. "mov %c[rax](%0), %%" _ASM_AX " \n\t"
  6567. "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
  6568. "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
  6569. "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
  6570. "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
  6571. "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
  6572. #ifdef CONFIG_X86_64
  6573. "mov %c[r8](%0), %%r8 \n\t"
  6574. "mov %c[r9](%0), %%r9 \n\t"
  6575. "mov %c[r10](%0), %%r10 \n\t"
  6576. "mov %c[r11](%0), %%r11 \n\t"
  6577. "mov %c[r12](%0), %%r12 \n\t"
  6578. "mov %c[r13](%0), %%r13 \n\t"
  6579. "mov %c[r14](%0), %%r14 \n\t"
  6580. "mov %c[r15](%0), %%r15 \n\t"
  6581. #endif
  6582. "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
  6583. /* Enter guest mode */
  6584. "jne 1f \n\t"
  6585. __ex(ASM_VMX_VMLAUNCH) "\n\t"
  6586. "jmp 2f \n\t"
  6587. "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
  6588. "2: "
  6589. /* Save guest registers, load host registers, keep flags */
  6590. "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
  6591. "pop %0 \n\t"
  6592. "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
  6593. "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
  6594. __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
  6595. "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
  6596. "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
  6597. "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
  6598. "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
  6599. #ifdef CONFIG_X86_64
  6600. "mov %%r8, %c[r8](%0) \n\t"
  6601. "mov %%r9, %c[r9](%0) \n\t"
  6602. "mov %%r10, %c[r10](%0) \n\t"
  6603. "mov %%r11, %c[r11](%0) \n\t"
  6604. "mov %%r12, %c[r12](%0) \n\t"
  6605. "mov %%r13, %c[r13](%0) \n\t"
  6606. "mov %%r14, %c[r14](%0) \n\t"
  6607. "mov %%r15, %c[r15](%0) \n\t"
  6608. #endif
  6609. "mov %%cr2, %%" _ASM_AX " \n\t"
  6610. "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
  6611. "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
  6612. "setbe %c[fail](%0) \n\t"
  6613. ".pushsection .rodata \n\t"
  6614. ".global vmx_return \n\t"
  6615. "vmx_return: " _ASM_PTR " 2b \n\t"
  6616. ".popsection"
  6617. : : "c"(vmx), "d"((unsigned long)HOST_RSP),
  6618. [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
  6619. [fail]"i"(offsetof(struct vcpu_vmx, fail)),
  6620. [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
  6621. [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
  6622. [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
  6623. [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
  6624. [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
  6625. [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
  6626. [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
  6627. [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
  6628. #ifdef CONFIG_X86_64
  6629. [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
  6630. [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
  6631. [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
  6632. [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
  6633. [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
  6634. [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
  6635. [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
  6636. [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
  6637. #endif
  6638. [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
  6639. [wordsize]"i"(sizeof(ulong))
  6640. : "cc", "memory"
  6641. #ifdef CONFIG_X86_64
  6642. , "rax", "rbx", "rdi", "rsi"
  6643. , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
  6644. #else
  6645. , "eax", "ebx", "edi", "esi"
  6646. #endif
  6647. );
  6648. /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
  6649. if (debugctlmsr)
  6650. update_debugctlmsr(debugctlmsr);
  6651. #ifndef CONFIG_X86_64
  6652. /*
  6653. * The sysexit path does not restore ds/es, so we must set them to
  6654. * a reasonable value ourselves.
  6655. *
  6656. * We can't defer this to vmx_load_host_state() since that function
  6657. * may be executed in interrupt context, which saves and restore segments
  6658. * around it, nullifying its effect.
  6659. */
  6660. loadsegment(ds, __USER_DS);
  6661. loadsegment(es, __USER_DS);
  6662. #endif
  6663. vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
  6664. | (1 << VCPU_EXREG_RFLAGS)
  6665. | (1 << VCPU_EXREG_PDPTR)
  6666. | (1 << VCPU_EXREG_SEGMENTS)
  6667. | (1 << VCPU_EXREG_CR3));
  6668. vcpu->arch.regs_dirty = 0;
  6669. vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
  6670. vmx->loaded_vmcs->launched = 1;
  6671. vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
  6672. trace_kvm_exit(vmx->exit_reason, vcpu, KVM_ISA_VMX);
  6673. /*
  6674. * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
  6675. * we did not inject a still-pending event to L1 now because of
  6676. * nested_run_pending, we need to re-enable this bit.
  6677. */
  6678. if (vmx->nested.nested_run_pending)
  6679. kvm_make_request(KVM_REQ_EVENT, vcpu);
  6680. vmx->nested.nested_run_pending = 0;
  6681. vmx_complete_atomic_exit(vmx);
  6682. vmx_recover_nmi_blocking(vmx);
  6683. vmx_complete_interrupts(vmx);
  6684. }
  6685. static void vmx_load_vmcs01(struct kvm_vcpu *vcpu)
  6686. {
  6687. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6688. int cpu;
  6689. if (vmx->loaded_vmcs == &vmx->vmcs01)
  6690. return;
  6691. cpu = get_cpu();
  6692. vmx->loaded_vmcs = &vmx->vmcs01;
  6693. vmx_vcpu_put(vcpu);
  6694. vmx_vcpu_load(vcpu, cpu);
  6695. vcpu->cpu = cpu;
  6696. put_cpu();
  6697. }
  6698. static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
  6699. {
  6700. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6701. free_vpid(vmx);
  6702. leave_guest_mode(vcpu);
  6703. vmx_load_vmcs01(vcpu);
  6704. free_nested(vmx);
  6705. free_loaded_vmcs(vmx->loaded_vmcs);
  6706. kfree(vmx->guest_msrs);
  6707. kvm_vcpu_uninit(vcpu);
  6708. kmem_cache_free(kvm_vcpu_cache, vmx);
  6709. }
  6710. static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
  6711. {
  6712. int err;
  6713. struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  6714. int cpu;
  6715. if (!vmx)
  6716. return ERR_PTR(-ENOMEM);
  6717. allocate_vpid(vmx);
  6718. err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
  6719. if (err)
  6720. goto free_vcpu;
  6721. vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
  6722. BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
  6723. > PAGE_SIZE);
  6724. err = -ENOMEM;
  6725. if (!vmx->guest_msrs) {
  6726. goto uninit_vcpu;
  6727. }
  6728. vmx->loaded_vmcs = &vmx->vmcs01;
  6729. vmx->loaded_vmcs->vmcs = alloc_vmcs();
  6730. if (!vmx->loaded_vmcs->vmcs)
  6731. goto free_msrs;
  6732. if (!vmm_exclusive)
  6733. kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id())));
  6734. loaded_vmcs_init(vmx->loaded_vmcs);
  6735. if (!vmm_exclusive)
  6736. kvm_cpu_vmxoff();
  6737. cpu = get_cpu();
  6738. vmx_vcpu_load(&vmx->vcpu, cpu);
  6739. vmx->vcpu.cpu = cpu;
  6740. err = vmx_vcpu_setup(vmx);
  6741. vmx_vcpu_put(&vmx->vcpu);
  6742. put_cpu();
  6743. if (err)
  6744. goto free_vmcs;
  6745. if (vm_need_virtualize_apic_accesses(kvm)) {
  6746. err = alloc_apic_access_page(kvm);
  6747. if (err)
  6748. goto free_vmcs;
  6749. }
  6750. if (enable_ept) {
  6751. if (!kvm->arch.ept_identity_map_addr)
  6752. kvm->arch.ept_identity_map_addr =
  6753. VMX_EPT_IDENTITY_PAGETABLE_ADDR;
  6754. err = init_rmode_identity_map(kvm);
  6755. if (err)
  6756. goto free_vmcs;
  6757. }
  6758. vmx->nested.current_vmptr = -1ull;
  6759. vmx->nested.current_vmcs12 = NULL;
  6760. return &vmx->vcpu;
  6761. free_vmcs:
  6762. free_loaded_vmcs(vmx->loaded_vmcs);
  6763. free_msrs:
  6764. kfree(vmx->guest_msrs);
  6765. uninit_vcpu:
  6766. kvm_vcpu_uninit(&vmx->vcpu);
  6767. free_vcpu:
  6768. free_vpid(vmx);
  6769. kmem_cache_free(kvm_vcpu_cache, vmx);
  6770. return ERR_PTR(err);
  6771. }
  6772. static void __init vmx_check_processor_compat(void *rtn)
  6773. {
  6774. struct vmcs_config vmcs_conf;
  6775. *(int *)rtn = 0;
  6776. if (setup_vmcs_config(&vmcs_conf) < 0)
  6777. *(int *)rtn = -EIO;
  6778. if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
  6779. printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
  6780. smp_processor_id());
  6781. *(int *)rtn = -EIO;
  6782. }
  6783. }
  6784. static int get_ept_level(void)
  6785. {
  6786. return VMX_EPT_DEFAULT_GAW + 1;
  6787. }
  6788. static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
  6789. {
  6790. u64 ret;
  6791. /* For VT-d and EPT combination
  6792. * 1. MMIO: always map as UC
  6793. * 2. EPT with VT-d:
  6794. * a. VT-d without snooping control feature: can't guarantee the
  6795. * result, try to trust guest.
  6796. * b. VT-d with snooping control feature: snooping control feature of
  6797. * VT-d engine can guarantee the cache correctness. Just set it
  6798. * to WB to keep consistent with host. So the same as item 3.
  6799. * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
  6800. * consistent with host MTRR
  6801. */
  6802. if (is_mmio)
  6803. ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
  6804. else if (kvm_arch_has_noncoherent_dma(vcpu->kvm))
  6805. ret = kvm_get_guest_memory_type(vcpu, gfn) <<
  6806. VMX_EPT_MT_EPTE_SHIFT;
  6807. else
  6808. ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
  6809. | VMX_EPT_IPAT_BIT;
  6810. return ret;
  6811. }
  6812. static int vmx_get_lpage_level(void)
  6813. {
  6814. if (enable_ept && !cpu_has_vmx_ept_1g_page())
  6815. return PT_DIRECTORY_LEVEL;
  6816. else
  6817. /* For shadow and EPT supported 1GB page */
  6818. return PT_PDPE_LEVEL;
  6819. }
  6820. static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
  6821. {
  6822. struct kvm_cpuid_entry2 *best;
  6823. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6824. u32 exec_control;
  6825. vmx->rdtscp_enabled = false;
  6826. if (vmx_rdtscp_supported()) {
  6827. exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  6828. if (exec_control & SECONDARY_EXEC_RDTSCP) {
  6829. best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
  6830. if (best && (best->edx & bit(X86_FEATURE_RDTSCP)))
  6831. vmx->rdtscp_enabled = true;
  6832. else {
  6833. exec_control &= ~SECONDARY_EXEC_RDTSCP;
  6834. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  6835. exec_control);
  6836. }
  6837. }
  6838. }
  6839. /* Exposing INVPCID only when PCID is exposed */
  6840. best = kvm_find_cpuid_entry(vcpu, 0x7, 0);
  6841. if (vmx_invpcid_supported() &&
  6842. best && (best->ebx & bit(X86_FEATURE_INVPCID)) &&
  6843. guest_cpuid_has_pcid(vcpu)) {
  6844. exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  6845. exec_control |= SECONDARY_EXEC_ENABLE_INVPCID;
  6846. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  6847. exec_control);
  6848. } else {
  6849. if (cpu_has_secondary_exec_ctrls()) {
  6850. exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  6851. exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
  6852. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  6853. exec_control);
  6854. }
  6855. if (best)
  6856. best->ebx &= ~bit(X86_FEATURE_INVPCID);
  6857. }
  6858. }
  6859. static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
  6860. {
  6861. if (func == 1 && nested)
  6862. entry->ecx |= bit(X86_FEATURE_VMX);
  6863. }
  6864. static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
  6865. struct x86_exception *fault)
  6866. {
  6867. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  6868. u32 exit_reason;
  6869. if (fault->error_code & PFERR_RSVD_MASK)
  6870. exit_reason = EXIT_REASON_EPT_MISCONFIG;
  6871. else
  6872. exit_reason = EXIT_REASON_EPT_VIOLATION;
  6873. nested_vmx_vmexit(vcpu, exit_reason, 0, vcpu->arch.exit_qualification);
  6874. vmcs12->guest_physical_address = fault->address;
  6875. }
  6876. /* Callbacks for nested_ept_init_mmu_context: */
  6877. static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
  6878. {
  6879. /* return the page table to be shadowed - in our case, EPT12 */
  6880. return get_vmcs12(vcpu)->ept_pointer;
  6881. }
  6882. static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
  6883. {
  6884. kvm_init_shadow_ept_mmu(vcpu, &vcpu->arch.mmu,
  6885. nested_vmx_ept_caps & VMX_EPT_EXECUTE_ONLY_BIT);
  6886. vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
  6887. vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
  6888. vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
  6889. vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
  6890. }
  6891. static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
  6892. {
  6893. vcpu->arch.walk_mmu = &vcpu->arch.mmu;
  6894. }
  6895. static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
  6896. struct x86_exception *fault)
  6897. {
  6898. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  6899. WARN_ON(!is_guest_mode(vcpu));
  6900. /* TODO: also check PFEC_MATCH/MASK, not just EB.PF. */
  6901. if (vmcs12->exception_bitmap & (1u << PF_VECTOR))
  6902. nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
  6903. vmcs_read32(VM_EXIT_INTR_INFO),
  6904. vmcs_readl(EXIT_QUALIFICATION));
  6905. else
  6906. kvm_inject_page_fault(vcpu, fault);
  6907. }
  6908. static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
  6909. struct vmcs12 *vmcs12)
  6910. {
  6911. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6912. if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
  6913. /* TODO: Also verify bits beyond physical address width are 0 */
  6914. if (!PAGE_ALIGNED(vmcs12->apic_access_addr))
  6915. return false;
  6916. /*
  6917. * Translate L1 physical address to host physical
  6918. * address for vmcs02. Keep the page pinned, so this
  6919. * physical address remains valid. We keep a reference
  6920. * to it so we can release it later.
  6921. */
  6922. if (vmx->nested.apic_access_page) /* shouldn't happen */
  6923. nested_release_page(vmx->nested.apic_access_page);
  6924. vmx->nested.apic_access_page =
  6925. nested_get_page(vcpu, vmcs12->apic_access_addr);
  6926. }
  6927. if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
  6928. /* TODO: Also verify bits beyond physical address width are 0 */
  6929. if (!PAGE_ALIGNED(vmcs12->virtual_apic_page_addr))
  6930. return false;
  6931. if (vmx->nested.virtual_apic_page) /* shouldn't happen */
  6932. nested_release_page(vmx->nested.virtual_apic_page);
  6933. vmx->nested.virtual_apic_page =
  6934. nested_get_page(vcpu, vmcs12->virtual_apic_page_addr);
  6935. /*
  6936. * Failing the vm entry is _not_ what the processor does
  6937. * but it's basically the only possibility we have.
  6938. * We could still enter the guest if CR8 load exits are
  6939. * enabled, CR8 store exits are enabled, and virtualize APIC
  6940. * access is disabled; in this case the processor would never
  6941. * use the TPR shadow and we could simply clear the bit from
  6942. * the execution control. But such a configuration is useless,
  6943. * so let's keep the code simple.
  6944. */
  6945. if (!vmx->nested.virtual_apic_page)
  6946. return false;
  6947. }
  6948. return true;
  6949. }
  6950. static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
  6951. {
  6952. u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
  6953. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6954. if (vcpu->arch.virtual_tsc_khz == 0)
  6955. return;
  6956. /* Make sure short timeouts reliably trigger an immediate vmexit.
  6957. * hrtimer_start does not guarantee this. */
  6958. if (preemption_timeout <= 1) {
  6959. vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
  6960. return;
  6961. }
  6962. preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
  6963. preemption_timeout *= 1000000;
  6964. do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
  6965. hrtimer_start(&vmx->nested.preemption_timer,
  6966. ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
  6967. }
  6968. /*
  6969. * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
  6970. * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
  6971. * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
  6972. * guest in a way that will both be appropriate to L1's requests, and our
  6973. * needs. In addition to modifying the active vmcs (which is vmcs02), this
  6974. * function also has additional necessary side-effects, like setting various
  6975. * vcpu->arch fields.
  6976. */
  6977. static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  6978. {
  6979. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6980. u32 exec_control;
  6981. vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
  6982. vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
  6983. vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
  6984. vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
  6985. vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
  6986. vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
  6987. vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
  6988. vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
  6989. vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
  6990. vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
  6991. vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
  6992. vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
  6993. vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
  6994. vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
  6995. vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
  6996. vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
  6997. vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
  6998. vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
  6999. vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
  7000. vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
  7001. vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
  7002. vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
  7003. vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
  7004. vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
  7005. vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
  7006. vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
  7007. vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
  7008. vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
  7009. vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
  7010. vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
  7011. vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
  7012. vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
  7013. vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
  7014. vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
  7015. vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
  7016. vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
  7017. if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
  7018. kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
  7019. vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
  7020. } else {
  7021. kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
  7022. vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
  7023. }
  7024. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  7025. vmcs12->vm_entry_intr_info_field);
  7026. vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
  7027. vmcs12->vm_entry_exception_error_code);
  7028. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  7029. vmcs12->vm_entry_instruction_len);
  7030. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
  7031. vmcs12->guest_interruptibility_info);
  7032. vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
  7033. vmx_set_rflags(vcpu, vmcs12->guest_rflags);
  7034. vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
  7035. vmcs12->guest_pending_dbg_exceptions);
  7036. vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
  7037. vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
  7038. vmcs_write64(VMCS_LINK_POINTER, -1ull);
  7039. exec_control = vmcs12->pin_based_vm_exec_control;
  7040. exec_control |= vmcs_config.pin_based_exec_ctrl;
  7041. exec_control &= ~(PIN_BASED_VMX_PREEMPTION_TIMER |
  7042. PIN_BASED_POSTED_INTR);
  7043. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
  7044. vmx->nested.preemption_timer_expired = false;
  7045. if (nested_cpu_has_preemption_timer(vmcs12))
  7046. vmx_start_preemption_timer(vcpu);
  7047. /*
  7048. * Whether page-faults are trapped is determined by a combination of
  7049. * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
  7050. * If enable_ept, L0 doesn't care about page faults and we should
  7051. * set all of these to L1's desires. However, if !enable_ept, L0 does
  7052. * care about (at least some) page faults, and because it is not easy
  7053. * (if at all possible?) to merge L0 and L1's desires, we simply ask
  7054. * to exit on each and every L2 page fault. This is done by setting
  7055. * MASK=MATCH=0 and (see below) EB.PF=1.
  7056. * Note that below we don't need special code to set EB.PF beyond the
  7057. * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
  7058. * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
  7059. * !enable_ept, EB.PF is 1, so the "or" will always be 1.
  7060. *
  7061. * A problem with this approach (when !enable_ept) is that L1 may be
  7062. * injected with more page faults than it asked for. This could have
  7063. * caused problems, but in practice existing hypervisors don't care.
  7064. * To fix this, we will need to emulate the PFEC checking (on the L1
  7065. * page tables), using walk_addr(), when injecting PFs to L1.
  7066. */
  7067. vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
  7068. enable_ept ? vmcs12->page_fault_error_code_mask : 0);
  7069. vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
  7070. enable_ept ? vmcs12->page_fault_error_code_match : 0);
  7071. if (cpu_has_secondary_exec_ctrls()) {
  7072. exec_control = vmx_secondary_exec_control(vmx);
  7073. if (!vmx->rdtscp_enabled)
  7074. exec_control &= ~SECONDARY_EXEC_RDTSCP;
  7075. /* Take the following fields only from vmcs12 */
  7076. exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  7077. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
  7078. SECONDARY_EXEC_APIC_REGISTER_VIRT);
  7079. if (nested_cpu_has(vmcs12,
  7080. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
  7081. exec_control |= vmcs12->secondary_vm_exec_control;
  7082. if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) {
  7083. /*
  7084. * If translation failed, no matter: This feature asks
  7085. * to exit when accessing the given address, and if it
  7086. * can never be accessed, this feature won't do
  7087. * anything anyway.
  7088. */
  7089. if (!vmx->nested.apic_access_page)
  7090. exec_control &=
  7091. ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  7092. else
  7093. vmcs_write64(APIC_ACCESS_ADDR,
  7094. page_to_phys(vmx->nested.apic_access_page));
  7095. } else if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm)) {
  7096. exec_control |=
  7097. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  7098. kvm_vcpu_reload_apic_access_page(vcpu);
  7099. }
  7100. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
  7101. }
  7102. /*
  7103. * Set host-state according to L0's settings (vmcs12 is irrelevant here)
  7104. * Some constant fields are set here by vmx_set_constant_host_state().
  7105. * Other fields are different per CPU, and will be set later when
  7106. * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
  7107. */
  7108. vmx_set_constant_host_state(vmx);
  7109. /*
  7110. * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
  7111. * entry, but only if the current (host) sp changed from the value
  7112. * we wrote last (vmx->host_rsp). This cache is no longer relevant
  7113. * if we switch vmcs, and rather than hold a separate cache per vmcs,
  7114. * here we just force the write to happen on entry.
  7115. */
  7116. vmx->host_rsp = 0;
  7117. exec_control = vmx_exec_control(vmx); /* L0's desires */
  7118. exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
  7119. exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
  7120. exec_control &= ~CPU_BASED_TPR_SHADOW;
  7121. exec_control |= vmcs12->cpu_based_vm_exec_control;
  7122. if (exec_control & CPU_BASED_TPR_SHADOW) {
  7123. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
  7124. page_to_phys(vmx->nested.virtual_apic_page));
  7125. vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
  7126. }
  7127. /*
  7128. * Merging of IO and MSR bitmaps not currently supported.
  7129. * Rather, exit every time.
  7130. */
  7131. exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
  7132. exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
  7133. exec_control |= CPU_BASED_UNCOND_IO_EXITING;
  7134. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
  7135. /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
  7136. * bitwise-or of what L1 wants to trap for L2, and what we want to
  7137. * trap. Note that CR0.TS also needs updating - we do this later.
  7138. */
  7139. update_exception_bitmap(vcpu);
  7140. vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
  7141. vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
  7142. /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
  7143. * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
  7144. * bits are further modified by vmx_set_efer() below.
  7145. */
  7146. vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
  7147. /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
  7148. * emulated by vmx_set_efer(), below.
  7149. */
  7150. vm_entry_controls_init(vmx,
  7151. (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
  7152. ~VM_ENTRY_IA32E_MODE) |
  7153. (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
  7154. if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) {
  7155. vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
  7156. vcpu->arch.pat = vmcs12->guest_ia32_pat;
  7157. } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
  7158. vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
  7159. set_cr4_guest_host_mask(vmx);
  7160. if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)
  7161. vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
  7162. if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
  7163. vmcs_write64(TSC_OFFSET,
  7164. vmx->nested.vmcs01_tsc_offset + vmcs12->tsc_offset);
  7165. else
  7166. vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
  7167. if (enable_vpid) {
  7168. /*
  7169. * Trivially support vpid by letting L2s share their parent
  7170. * L1's vpid. TODO: move to a more elaborate solution, giving
  7171. * each L2 its own vpid and exposing the vpid feature to L1.
  7172. */
  7173. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
  7174. vmx_flush_tlb(vcpu);
  7175. }
  7176. if (nested_cpu_has_ept(vmcs12)) {
  7177. kvm_mmu_unload(vcpu);
  7178. nested_ept_init_mmu_context(vcpu);
  7179. }
  7180. if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)
  7181. vcpu->arch.efer = vmcs12->guest_ia32_efer;
  7182. else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
  7183. vcpu->arch.efer |= (EFER_LMA | EFER_LME);
  7184. else
  7185. vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
  7186. /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
  7187. vmx_set_efer(vcpu, vcpu->arch.efer);
  7188. /*
  7189. * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified
  7190. * TS bit (for lazy fpu) and bits which we consider mandatory enabled.
  7191. * The CR0_READ_SHADOW is what L2 should have expected to read given
  7192. * the specifications by L1; It's not enough to take
  7193. * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
  7194. * have more bits than L1 expected.
  7195. */
  7196. vmx_set_cr0(vcpu, vmcs12->guest_cr0);
  7197. vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
  7198. vmx_set_cr4(vcpu, vmcs12->guest_cr4);
  7199. vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
  7200. /* shadow page tables on either EPT or shadow page tables */
  7201. kvm_set_cr3(vcpu, vmcs12->guest_cr3);
  7202. kvm_mmu_reset_context(vcpu);
  7203. if (!enable_ept)
  7204. vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
  7205. /*
  7206. * L1 may access the L2's PDPTR, so save them to construct vmcs12
  7207. */
  7208. if (enable_ept) {
  7209. vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
  7210. vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
  7211. vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
  7212. vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
  7213. }
  7214. kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
  7215. kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
  7216. }
  7217. /*
  7218. * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
  7219. * for running an L2 nested guest.
  7220. */
  7221. static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
  7222. {
  7223. struct vmcs12 *vmcs12;
  7224. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7225. int cpu;
  7226. struct loaded_vmcs *vmcs02;
  7227. bool ia32e;
  7228. if (!nested_vmx_check_permission(vcpu) ||
  7229. !nested_vmx_check_vmcs12(vcpu))
  7230. return 1;
  7231. skip_emulated_instruction(vcpu);
  7232. vmcs12 = get_vmcs12(vcpu);
  7233. if (enable_shadow_vmcs)
  7234. copy_shadow_to_vmcs12(vmx);
  7235. /*
  7236. * The nested entry process starts with enforcing various prerequisites
  7237. * on vmcs12 as required by the Intel SDM, and act appropriately when
  7238. * they fail: As the SDM explains, some conditions should cause the
  7239. * instruction to fail, while others will cause the instruction to seem
  7240. * to succeed, but return an EXIT_REASON_INVALID_STATE.
  7241. * To speed up the normal (success) code path, we should avoid checking
  7242. * for misconfigurations which will anyway be caught by the processor
  7243. * when using the merged vmcs02.
  7244. */
  7245. if (vmcs12->launch_state == launch) {
  7246. nested_vmx_failValid(vcpu,
  7247. launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
  7248. : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
  7249. return 1;
  7250. }
  7251. if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
  7252. vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) {
  7253. nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  7254. return 1;
  7255. }
  7256. if ((vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_MSR_BITMAPS) &&
  7257. !PAGE_ALIGNED(vmcs12->msr_bitmap)) {
  7258. /*TODO: Also verify bits beyond physical address width are 0*/
  7259. nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  7260. return 1;
  7261. }
  7262. if (!nested_get_vmcs12_pages(vcpu, vmcs12)) {
  7263. /*TODO: Also verify bits beyond physical address width are 0*/
  7264. nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  7265. return 1;
  7266. }
  7267. if (vmcs12->vm_entry_msr_load_count > 0 ||
  7268. vmcs12->vm_exit_msr_load_count > 0 ||
  7269. vmcs12->vm_exit_msr_store_count > 0) {
  7270. pr_warn_ratelimited("%s: VMCS MSR_{LOAD,STORE} unsupported\n",
  7271. __func__);
  7272. nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  7273. return 1;
  7274. }
  7275. if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
  7276. nested_vmx_true_procbased_ctls_low,
  7277. nested_vmx_procbased_ctls_high) ||
  7278. !vmx_control_verify(vmcs12->secondary_vm_exec_control,
  7279. nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high) ||
  7280. !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
  7281. nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high) ||
  7282. !vmx_control_verify(vmcs12->vm_exit_controls,
  7283. nested_vmx_true_exit_ctls_low,
  7284. nested_vmx_exit_ctls_high) ||
  7285. !vmx_control_verify(vmcs12->vm_entry_controls,
  7286. nested_vmx_true_entry_ctls_low,
  7287. nested_vmx_entry_ctls_high))
  7288. {
  7289. nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  7290. return 1;
  7291. }
  7292. if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
  7293. ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
  7294. nested_vmx_failValid(vcpu,
  7295. VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
  7296. return 1;
  7297. }
  7298. if (!nested_cr0_valid(vmcs12, vmcs12->guest_cr0) ||
  7299. ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
  7300. nested_vmx_entry_failure(vcpu, vmcs12,
  7301. EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
  7302. return 1;
  7303. }
  7304. if (vmcs12->vmcs_link_pointer != -1ull) {
  7305. nested_vmx_entry_failure(vcpu, vmcs12,
  7306. EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR);
  7307. return 1;
  7308. }
  7309. /*
  7310. * If the load IA32_EFER VM-entry control is 1, the following checks
  7311. * are performed on the field for the IA32_EFER MSR:
  7312. * - Bits reserved in the IA32_EFER MSR must be 0.
  7313. * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
  7314. * the IA-32e mode guest VM-exit control. It must also be identical
  7315. * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
  7316. * CR0.PG) is 1.
  7317. */
  7318. if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER) {
  7319. ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
  7320. if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
  7321. ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
  7322. ((vmcs12->guest_cr0 & X86_CR0_PG) &&
  7323. ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) {
  7324. nested_vmx_entry_failure(vcpu, vmcs12,
  7325. EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
  7326. return 1;
  7327. }
  7328. }
  7329. /*
  7330. * If the load IA32_EFER VM-exit control is 1, bits reserved in the
  7331. * IA32_EFER MSR must be 0 in the field for that register. In addition,
  7332. * the values of the LMA and LME bits in the field must each be that of
  7333. * the host address-space size VM-exit control.
  7334. */
  7335. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
  7336. ia32e = (vmcs12->vm_exit_controls &
  7337. VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
  7338. if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
  7339. ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
  7340. ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) {
  7341. nested_vmx_entry_failure(vcpu, vmcs12,
  7342. EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
  7343. return 1;
  7344. }
  7345. }
  7346. /*
  7347. * We're finally done with prerequisite checking, and can start with
  7348. * the nested entry.
  7349. */
  7350. vmcs02 = nested_get_current_vmcs02(vmx);
  7351. if (!vmcs02)
  7352. return -ENOMEM;
  7353. enter_guest_mode(vcpu);
  7354. vmx->nested.vmcs01_tsc_offset = vmcs_read64(TSC_OFFSET);
  7355. if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
  7356. vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
  7357. cpu = get_cpu();
  7358. vmx->loaded_vmcs = vmcs02;
  7359. vmx_vcpu_put(vcpu);
  7360. vmx_vcpu_load(vcpu, cpu);
  7361. vcpu->cpu = cpu;
  7362. put_cpu();
  7363. vmx_segment_cache_clear(vmx);
  7364. vmcs12->launch_state = 1;
  7365. prepare_vmcs02(vcpu, vmcs12);
  7366. if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT)
  7367. return kvm_emulate_halt(vcpu);
  7368. vmx->nested.nested_run_pending = 1;
  7369. /*
  7370. * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
  7371. * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
  7372. * returned as far as L1 is concerned. It will only return (and set
  7373. * the success flag) when L2 exits (see nested_vmx_vmexit()).
  7374. */
  7375. return 1;
  7376. }
  7377. /*
  7378. * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
  7379. * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
  7380. * This function returns the new value we should put in vmcs12.guest_cr0.
  7381. * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
  7382. * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
  7383. * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
  7384. * didn't trap the bit, because if L1 did, so would L0).
  7385. * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
  7386. * been modified by L2, and L1 knows it. So just leave the old value of
  7387. * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
  7388. * isn't relevant, because if L0 traps this bit it can set it to anything.
  7389. * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
  7390. * changed these bits, and therefore they need to be updated, but L0
  7391. * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
  7392. * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
  7393. */
  7394. static inline unsigned long
  7395. vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  7396. {
  7397. return
  7398. /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
  7399. /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
  7400. /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
  7401. vcpu->arch.cr0_guest_owned_bits));
  7402. }
  7403. static inline unsigned long
  7404. vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  7405. {
  7406. return
  7407. /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
  7408. /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
  7409. /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
  7410. vcpu->arch.cr4_guest_owned_bits));
  7411. }
  7412. static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
  7413. struct vmcs12 *vmcs12)
  7414. {
  7415. u32 idt_vectoring;
  7416. unsigned int nr;
  7417. if (vcpu->arch.exception.pending && vcpu->arch.exception.reinject) {
  7418. nr = vcpu->arch.exception.nr;
  7419. idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
  7420. if (kvm_exception_is_soft(nr)) {
  7421. vmcs12->vm_exit_instruction_len =
  7422. vcpu->arch.event_exit_inst_len;
  7423. idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
  7424. } else
  7425. idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
  7426. if (vcpu->arch.exception.has_error_code) {
  7427. idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
  7428. vmcs12->idt_vectoring_error_code =
  7429. vcpu->arch.exception.error_code;
  7430. }
  7431. vmcs12->idt_vectoring_info_field = idt_vectoring;
  7432. } else if (vcpu->arch.nmi_injected) {
  7433. vmcs12->idt_vectoring_info_field =
  7434. INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
  7435. } else if (vcpu->arch.interrupt.pending) {
  7436. nr = vcpu->arch.interrupt.nr;
  7437. idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
  7438. if (vcpu->arch.interrupt.soft) {
  7439. idt_vectoring |= INTR_TYPE_SOFT_INTR;
  7440. vmcs12->vm_entry_instruction_len =
  7441. vcpu->arch.event_exit_inst_len;
  7442. } else
  7443. idt_vectoring |= INTR_TYPE_EXT_INTR;
  7444. vmcs12->idt_vectoring_info_field = idt_vectoring;
  7445. }
  7446. }
  7447. static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
  7448. {
  7449. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7450. if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
  7451. vmx->nested.preemption_timer_expired) {
  7452. if (vmx->nested.nested_run_pending)
  7453. return -EBUSY;
  7454. nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
  7455. return 0;
  7456. }
  7457. if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
  7458. if (vmx->nested.nested_run_pending ||
  7459. vcpu->arch.interrupt.pending)
  7460. return -EBUSY;
  7461. nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
  7462. NMI_VECTOR | INTR_TYPE_NMI_INTR |
  7463. INTR_INFO_VALID_MASK, 0);
  7464. /*
  7465. * The NMI-triggered VM exit counts as injection:
  7466. * clear this one and block further NMIs.
  7467. */
  7468. vcpu->arch.nmi_pending = 0;
  7469. vmx_set_nmi_mask(vcpu, true);
  7470. return 0;
  7471. }
  7472. if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
  7473. nested_exit_on_intr(vcpu)) {
  7474. if (vmx->nested.nested_run_pending)
  7475. return -EBUSY;
  7476. nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
  7477. }
  7478. return 0;
  7479. }
  7480. static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
  7481. {
  7482. ktime_t remaining =
  7483. hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
  7484. u64 value;
  7485. if (ktime_to_ns(remaining) <= 0)
  7486. return 0;
  7487. value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
  7488. do_div(value, 1000000);
  7489. return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
  7490. }
  7491. /*
  7492. * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
  7493. * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
  7494. * and this function updates it to reflect the changes to the guest state while
  7495. * L2 was running (and perhaps made some exits which were handled directly by L0
  7496. * without going back to L1), and to reflect the exit reason.
  7497. * Note that we do not have to copy here all VMCS fields, just those that
  7498. * could have changed by the L2 guest or the exit - i.e., the guest-state and
  7499. * exit-information fields only. Other fields are modified by L1 with VMWRITE,
  7500. * which already writes to vmcs12 directly.
  7501. */
  7502. static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
  7503. u32 exit_reason, u32 exit_intr_info,
  7504. unsigned long exit_qualification)
  7505. {
  7506. /* update guest state fields: */
  7507. vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
  7508. vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
  7509. vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
  7510. vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
  7511. vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
  7512. vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
  7513. vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
  7514. vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
  7515. vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
  7516. vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
  7517. vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
  7518. vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
  7519. vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
  7520. vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
  7521. vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
  7522. vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
  7523. vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
  7524. vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
  7525. vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
  7526. vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
  7527. vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
  7528. vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
  7529. vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
  7530. vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
  7531. vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
  7532. vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
  7533. vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
  7534. vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
  7535. vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
  7536. vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
  7537. vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
  7538. vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
  7539. vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
  7540. vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
  7541. vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
  7542. vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
  7543. vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
  7544. vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
  7545. vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
  7546. vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
  7547. vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
  7548. vmcs12->guest_interruptibility_info =
  7549. vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  7550. vmcs12->guest_pending_dbg_exceptions =
  7551. vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
  7552. if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
  7553. vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
  7554. else
  7555. vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
  7556. if (nested_cpu_has_preemption_timer(vmcs12)) {
  7557. if (vmcs12->vm_exit_controls &
  7558. VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
  7559. vmcs12->vmx_preemption_timer_value =
  7560. vmx_get_preemption_timer_value(vcpu);
  7561. hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
  7562. }
  7563. /*
  7564. * In some cases (usually, nested EPT), L2 is allowed to change its
  7565. * own CR3 without exiting. If it has changed it, we must keep it.
  7566. * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
  7567. * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
  7568. *
  7569. * Additionally, restore L2's PDPTR to vmcs12.
  7570. */
  7571. if (enable_ept) {
  7572. vmcs12->guest_cr3 = vmcs_read64(GUEST_CR3);
  7573. vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
  7574. vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
  7575. vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
  7576. vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
  7577. }
  7578. vmcs12->vm_entry_controls =
  7579. (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
  7580. (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
  7581. if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
  7582. kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
  7583. vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
  7584. }
  7585. /* TODO: These cannot have changed unless we have MSR bitmaps and
  7586. * the relevant bit asks not to trap the change */
  7587. if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
  7588. vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
  7589. if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
  7590. vmcs12->guest_ia32_efer = vcpu->arch.efer;
  7591. vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
  7592. vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
  7593. vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
  7594. if (vmx_mpx_supported())
  7595. vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
  7596. /* update exit information fields: */
  7597. vmcs12->vm_exit_reason = exit_reason;
  7598. vmcs12->exit_qualification = exit_qualification;
  7599. vmcs12->vm_exit_intr_info = exit_intr_info;
  7600. if ((vmcs12->vm_exit_intr_info &
  7601. (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
  7602. (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK))
  7603. vmcs12->vm_exit_intr_error_code =
  7604. vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
  7605. vmcs12->idt_vectoring_info_field = 0;
  7606. vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  7607. vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  7608. if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
  7609. /* vm_entry_intr_info_field is cleared on exit. Emulate this
  7610. * instead of reading the real value. */
  7611. vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
  7612. /*
  7613. * Transfer the event that L0 or L1 may wanted to inject into
  7614. * L2 to IDT_VECTORING_INFO_FIELD.
  7615. */
  7616. vmcs12_save_pending_event(vcpu, vmcs12);
  7617. }
  7618. /*
  7619. * Drop what we picked up for L2 via vmx_complete_interrupts. It is
  7620. * preserved above and would only end up incorrectly in L1.
  7621. */
  7622. vcpu->arch.nmi_injected = false;
  7623. kvm_clear_exception_queue(vcpu);
  7624. kvm_clear_interrupt_queue(vcpu);
  7625. }
  7626. /*
  7627. * A part of what we need to when the nested L2 guest exits and we want to
  7628. * run its L1 parent, is to reset L1's guest state to the host state specified
  7629. * in vmcs12.
  7630. * This function is to be called not only on normal nested exit, but also on
  7631. * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
  7632. * Failures During or After Loading Guest State").
  7633. * This function should be called when the active VMCS is L1's (vmcs01).
  7634. */
  7635. static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
  7636. struct vmcs12 *vmcs12)
  7637. {
  7638. struct kvm_segment seg;
  7639. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
  7640. vcpu->arch.efer = vmcs12->host_ia32_efer;
  7641. else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
  7642. vcpu->arch.efer |= (EFER_LMA | EFER_LME);
  7643. else
  7644. vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
  7645. vmx_set_efer(vcpu, vcpu->arch.efer);
  7646. kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
  7647. kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
  7648. vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
  7649. /*
  7650. * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
  7651. * actually changed, because it depends on the current state of
  7652. * fpu_active (which may have changed).
  7653. * Note that vmx_set_cr0 refers to efer set above.
  7654. */
  7655. vmx_set_cr0(vcpu, vmcs12->host_cr0);
  7656. /*
  7657. * If we did fpu_activate()/fpu_deactivate() during L2's run, we need
  7658. * to apply the same changes to L1's vmcs. We just set cr0 correctly,
  7659. * but we also need to update cr0_guest_host_mask and exception_bitmap.
  7660. */
  7661. update_exception_bitmap(vcpu);
  7662. vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0);
  7663. vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
  7664. /*
  7665. * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01
  7666. * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask();
  7667. */
  7668. vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
  7669. kvm_set_cr4(vcpu, vmcs12->host_cr4);
  7670. nested_ept_uninit_mmu_context(vcpu);
  7671. kvm_set_cr3(vcpu, vmcs12->host_cr3);
  7672. kvm_mmu_reset_context(vcpu);
  7673. if (!enable_ept)
  7674. vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
  7675. if (enable_vpid) {
  7676. /*
  7677. * Trivially support vpid by letting L2s share their parent
  7678. * L1's vpid. TODO: move to a more elaborate solution, giving
  7679. * each L2 its own vpid and exposing the vpid feature to L1.
  7680. */
  7681. vmx_flush_tlb(vcpu);
  7682. }
  7683. vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
  7684. vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
  7685. vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
  7686. vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
  7687. vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
  7688. /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
  7689. if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
  7690. vmcs_write64(GUEST_BNDCFGS, 0);
  7691. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
  7692. vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
  7693. vcpu->arch.pat = vmcs12->host_ia32_pat;
  7694. }
  7695. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
  7696. vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
  7697. vmcs12->host_ia32_perf_global_ctrl);
  7698. /* Set L1 segment info according to Intel SDM
  7699. 27.5.2 Loading Host Segment and Descriptor-Table Registers */
  7700. seg = (struct kvm_segment) {
  7701. .base = 0,
  7702. .limit = 0xFFFFFFFF,
  7703. .selector = vmcs12->host_cs_selector,
  7704. .type = 11,
  7705. .present = 1,
  7706. .s = 1,
  7707. .g = 1
  7708. };
  7709. if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
  7710. seg.l = 1;
  7711. else
  7712. seg.db = 1;
  7713. vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
  7714. seg = (struct kvm_segment) {
  7715. .base = 0,
  7716. .limit = 0xFFFFFFFF,
  7717. .type = 3,
  7718. .present = 1,
  7719. .s = 1,
  7720. .db = 1,
  7721. .g = 1
  7722. };
  7723. seg.selector = vmcs12->host_ds_selector;
  7724. vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
  7725. seg.selector = vmcs12->host_es_selector;
  7726. vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
  7727. seg.selector = vmcs12->host_ss_selector;
  7728. vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
  7729. seg.selector = vmcs12->host_fs_selector;
  7730. seg.base = vmcs12->host_fs_base;
  7731. vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
  7732. seg.selector = vmcs12->host_gs_selector;
  7733. seg.base = vmcs12->host_gs_base;
  7734. vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
  7735. seg = (struct kvm_segment) {
  7736. .base = vmcs12->host_tr_base,
  7737. .limit = 0x67,
  7738. .selector = vmcs12->host_tr_selector,
  7739. .type = 11,
  7740. .present = 1
  7741. };
  7742. vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
  7743. kvm_set_dr(vcpu, 7, 0x400);
  7744. vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
  7745. }
  7746. /*
  7747. * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
  7748. * and modify vmcs12 to make it see what it would expect to see there if
  7749. * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
  7750. */
  7751. static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
  7752. u32 exit_intr_info,
  7753. unsigned long exit_qualification)
  7754. {
  7755. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7756. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  7757. /* trying to cancel vmlaunch/vmresume is a bug */
  7758. WARN_ON_ONCE(vmx->nested.nested_run_pending);
  7759. leave_guest_mode(vcpu);
  7760. prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
  7761. exit_qualification);
  7762. vmx_load_vmcs01(vcpu);
  7763. if ((exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
  7764. && nested_exit_intr_ack_set(vcpu)) {
  7765. int irq = kvm_cpu_get_interrupt(vcpu);
  7766. WARN_ON(irq < 0);
  7767. vmcs12->vm_exit_intr_info = irq |
  7768. INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
  7769. }
  7770. trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
  7771. vmcs12->exit_qualification,
  7772. vmcs12->idt_vectoring_info_field,
  7773. vmcs12->vm_exit_intr_info,
  7774. vmcs12->vm_exit_intr_error_code,
  7775. KVM_ISA_VMX);
  7776. vm_entry_controls_init(vmx, vmcs_read32(VM_ENTRY_CONTROLS));
  7777. vm_exit_controls_init(vmx, vmcs_read32(VM_EXIT_CONTROLS));
  7778. vmx_segment_cache_clear(vmx);
  7779. /* if no vmcs02 cache requested, remove the one we used */
  7780. if (VMCS02_POOL_SIZE == 0)
  7781. nested_free_vmcs02(vmx, vmx->nested.current_vmptr);
  7782. load_vmcs12_host_state(vcpu, vmcs12);
  7783. /* Update TSC_OFFSET if TSC was changed while L2 ran */
  7784. vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
  7785. /* This is needed for same reason as it was needed in prepare_vmcs02 */
  7786. vmx->host_rsp = 0;
  7787. /* Unpin physical memory we referred to in vmcs02 */
  7788. if (vmx->nested.apic_access_page) {
  7789. nested_release_page(vmx->nested.apic_access_page);
  7790. vmx->nested.apic_access_page = NULL;
  7791. }
  7792. if (vmx->nested.virtual_apic_page) {
  7793. nested_release_page(vmx->nested.virtual_apic_page);
  7794. vmx->nested.virtual_apic_page = NULL;
  7795. }
  7796. /*
  7797. * We are now running in L2, mmu_notifier will force to reload the
  7798. * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
  7799. */
  7800. kvm_vcpu_reload_apic_access_page(vcpu);
  7801. /*
  7802. * Exiting from L2 to L1, we're now back to L1 which thinks it just
  7803. * finished a VMLAUNCH or VMRESUME instruction, so we need to set the
  7804. * success or failure flag accordingly.
  7805. */
  7806. if (unlikely(vmx->fail)) {
  7807. vmx->fail = 0;
  7808. nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR));
  7809. } else
  7810. nested_vmx_succeed(vcpu);
  7811. if (enable_shadow_vmcs)
  7812. vmx->nested.sync_shadow_vmcs = true;
  7813. /* in case we halted in L2 */
  7814. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  7815. }
  7816. /*
  7817. * Forcibly leave nested mode in order to be able to reset the VCPU later on.
  7818. */
  7819. static void vmx_leave_nested(struct kvm_vcpu *vcpu)
  7820. {
  7821. if (is_guest_mode(vcpu))
  7822. nested_vmx_vmexit(vcpu, -1, 0, 0);
  7823. free_nested(to_vmx(vcpu));
  7824. }
  7825. /*
  7826. * L1's failure to enter L2 is a subset of a normal exit, as explained in
  7827. * 23.7 "VM-entry failures during or after loading guest state" (this also
  7828. * lists the acceptable exit-reason and exit-qualification parameters).
  7829. * It should only be called before L2 actually succeeded to run, and when
  7830. * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
  7831. */
  7832. static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
  7833. struct vmcs12 *vmcs12,
  7834. u32 reason, unsigned long qualification)
  7835. {
  7836. load_vmcs12_host_state(vcpu, vmcs12);
  7837. vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
  7838. vmcs12->exit_qualification = qualification;
  7839. nested_vmx_succeed(vcpu);
  7840. if (enable_shadow_vmcs)
  7841. to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
  7842. }
  7843. static int vmx_check_intercept(struct kvm_vcpu *vcpu,
  7844. struct x86_instruction_info *info,
  7845. enum x86_intercept_stage stage)
  7846. {
  7847. return X86EMUL_CONTINUE;
  7848. }
  7849. static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
  7850. {
  7851. if (ple_gap)
  7852. shrink_ple_window(vcpu);
  7853. }
  7854. static struct kvm_x86_ops vmx_x86_ops = {
  7855. .cpu_has_kvm_support = cpu_has_kvm_support,
  7856. .disabled_by_bios = vmx_disabled_by_bios,
  7857. .hardware_setup = hardware_setup,
  7858. .hardware_unsetup = hardware_unsetup,
  7859. .check_processor_compatibility = vmx_check_processor_compat,
  7860. .hardware_enable = hardware_enable,
  7861. .hardware_disable = hardware_disable,
  7862. .cpu_has_accelerated_tpr = report_flexpriority,
  7863. .vcpu_create = vmx_create_vcpu,
  7864. .vcpu_free = vmx_free_vcpu,
  7865. .vcpu_reset = vmx_vcpu_reset,
  7866. .prepare_guest_switch = vmx_save_host_state,
  7867. .vcpu_load = vmx_vcpu_load,
  7868. .vcpu_put = vmx_vcpu_put,
  7869. .update_db_bp_intercept = update_exception_bitmap,
  7870. .get_msr = vmx_get_msr,
  7871. .set_msr = vmx_set_msr,
  7872. .get_segment_base = vmx_get_segment_base,
  7873. .get_segment = vmx_get_segment,
  7874. .set_segment = vmx_set_segment,
  7875. .get_cpl = vmx_get_cpl,
  7876. .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
  7877. .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
  7878. .decache_cr3 = vmx_decache_cr3,
  7879. .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
  7880. .set_cr0 = vmx_set_cr0,
  7881. .set_cr3 = vmx_set_cr3,
  7882. .set_cr4 = vmx_set_cr4,
  7883. .set_efer = vmx_set_efer,
  7884. .get_idt = vmx_get_idt,
  7885. .set_idt = vmx_set_idt,
  7886. .get_gdt = vmx_get_gdt,
  7887. .set_gdt = vmx_set_gdt,
  7888. .get_dr6 = vmx_get_dr6,
  7889. .set_dr6 = vmx_set_dr6,
  7890. .set_dr7 = vmx_set_dr7,
  7891. .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
  7892. .cache_reg = vmx_cache_reg,
  7893. .get_rflags = vmx_get_rflags,
  7894. .set_rflags = vmx_set_rflags,
  7895. .fpu_deactivate = vmx_fpu_deactivate,
  7896. .tlb_flush = vmx_flush_tlb,
  7897. .run = vmx_vcpu_run,
  7898. .handle_exit = vmx_handle_exit,
  7899. .skip_emulated_instruction = skip_emulated_instruction,
  7900. .set_interrupt_shadow = vmx_set_interrupt_shadow,
  7901. .get_interrupt_shadow = vmx_get_interrupt_shadow,
  7902. .patch_hypercall = vmx_patch_hypercall,
  7903. .set_irq = vmx_inject_irq,
  7904. .set_nmi = vmx_inject_nmi,
  7905. .queue_exception = vmx_queue_exception,
  7906. .cancel_injection = vmx_cancel_injection,
  7907. .interrupt_allowed = vmx_interrupt_allowed,
  7908. .nmi_allowed = vmx_nmi_allowed,
  7909. .get_nmi_mask = vmx_get_nmi_mask,
  7910. .set_nmi_mask = vmx_set_nmi_mask,
  7911. .enable_nmi_window = enable_nmi_window,
  7912. .enable_irq_window = enable_irq_window,
  7913. .update_cr8_intercept = update_cr8_intercept,
  7914. .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
  7915. .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
  7916. .vm_has_apicv = vmx_vm_has_apicv,
  7917. .load_eoi_exitmap = vmx_load_eoi_exitmap,
  7918. .hwapic_irr_update = vmx_hwapic_irr_update,
  7919. .hwapic_isr_update = vmx_hwapic_isr_update,
  7920. .sync_pir_to_irr = vmx_sync_pir_to_irr,
  7921. .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
  7922. .set_tss_addr = vmx_set_tss_addr,
  7923. .get_tdp_level = get_ept_level,
  7924. .get_mt_mask = vmx_get_mt_mask,
  7925. .get_exit_info = vmx_get_exit_info,
  7926. .get_lpage_level = vmx_get_lpage_level,
  7927. .cpuid_update = vmx_cpuid_update,
  7928. .rdtscp_supported = vmx_rdtscp_supported,
  7929. .invpcid_supported = vmx_invpcid_supported,
  7930. .set_supported_cpuid = vmx_set_supported_cpuid,
  7931. .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
  7932. .set_tsc_khz = vmx_set_tsc_khz,
  7933. .read_tsc_offset = vmx_read_tsc_offset,
  7934. .write_tsc_offset = vmx_write_tsc_offset,
  7935. .adjust_tsc_offset = vmx_adjust_tsc_offset,
  7936. .compute_tsc_offset = vmx_compute_tsc_offset,
  7937. .read_l1_tsc = vmx_read_l1_tsc,
  7938. .set_tdp_cr3 = vmx_set_cr3,
  7939. .check_intercept = vmx_check_intercept,
  7940. .handle_external_intr = vmx_handle_external_intr,
  7941. .mpx_supported = vmx_mpx_supported,
  7942. .check_nested_events = vmx_check_nested_events,
  7943. .sched_in = vmx_sched_in,
  7944. };
  7945. static int __init vmx_init(void)
  7946. {
  7947. int r, i, msr;
  7948. rdmsrl_safe(MSR_EFER, &host_efer);
  7949. for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
  7950. kvm_define_shared_msr(i, vmx_msr_index[i]);
  7951. vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
  7952. if (!vmx_io_bitmap_a)
  7953. return -ENOMEM;
  7954. r = -ENOMEM;
  7955. vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
  7956. if (!vmx_io_bitmap_b)
  7957. goto out;
  7958. vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
  7959. if (!vmx_msr_bitmap_legacy)
  7960. goto out1;
  7961. vmx_msr_bitmap_legacy_x2apic =
  7962. (unsigned long *)__get_free_page(GFP_KERNEL);
  7963. if (!vmx_msr_bitmap_legacy_x2apic)
  7964. goto out2;
  7965. vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
  7966. if (!vmx_msr_bitmap_longmode)
  7967. goto out3;
  7968. vmx_msr_bitmap_longmode_x2apic =
  7969. (unsigned long *)__get_free_page(GFP_KERNEL);
  7970. if (!vmx_msr_bitmap_longmode_x2apic)
  7971. goto out4;
  7972. vmx_vmread_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
  7973. if (!vmx_vmread_bitmap)
  7974. goto out5;
  7975. vmx_vmwrite_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
  7976. if (!vmx_vmwrite_bitmap)
  7977. goto out6;
  7978. memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
  7979. memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
  7980. /*
  7981. * Allow direct access to the PC debug port (it is often used for I/O
  7982. * delays, but the vmexits simply slow things down).
  7983. */
  7984. memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
  7985. clear_bit(0x80, vmx_io_bitmap_a);
  7986. memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
  7987. memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
  7988. memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
  7989. set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
  7990. r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
  7991. __alignof__(struct vcpu_vmx), THIS_MODULE);
  7992. if (r)
  7993. goto out7;
  7994. #ifdef CONFIG_KEXEC
  7995. rcu_assign_pointer(crash_vmclear_loaded_vmcss,
  7996. crash_vmclear_local_loaded_vmcss);
  7997. #endif
  7998. vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
  7999. vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
  8000. vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
  8001. vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
  8002. vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
  8003. vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
  8004. vmx_disable_intercept_for_msr(MSR_IA32_BNDCFGS, true);
  8005. memcpy(vmx_msr_bitmap_legacy_x2apic,
  8006. vmx_msr_bitmap_legacy, PAGE_SIZE);
  8007. memcpy(vmx_msr_bitmap_longmode_x2apic,
  8008. vmx_msr_bitmap_longmode, PAGE_SIZE);
  8009. if (enable_apicv) {
  8010. for (msr = 0x800; msr <= 0x8ff; msr++)
  8011. vmx_disable_intercept_msr_read_x2apic(msr);
  8012. /* According SDM, in x2apic mode, the whole id reg is used.
  8013. * But in KVM, it only use the highest eight bits. Need to
  8014. * intercept it */
  8015. vmx_enable_intercept_msr_read_x2apic(0x802);
  8016. /* TMCCT */
  8017. vmx_enable_intercept_msr_read_x2apic(0x839);
  8018. /* TPR */
  8019. vmx_disable_intercept_msr_write_x2apic(0x808);
  8020. /* EOI */
  8021. vmx_disable_intercept_msr_write_x2apic(0x80b);
  8022. /* SELF-IPI */
  8023. vmx_disable_intercept_msr_write_x2apic(0x83f);
  8024. }
  8025. if (enable_ept) {
  8026. kvm_mmu_set_mask_ptes(0ull,
  8027. (enable_ept_ad_bits) ? VMX_EPT_ACCESS_BIT : 0ull,
  8028. (enable_ept_ad_bits) ? VMX_EPT_DIRTY_BIT : 0ull,
  8029. 0ull, VMX_EPT_EXECUTABLE_MASK);
  8030. ept_set_mmio_spte_mask();
  8031. kvm_enable_tdp();
  8032. } else
  8033. kvm_disable_tdp();
  8034. update_ple_window_actual_max();
  8035. return 0;
  8036. out7:
  8037. free_page((unsigned long)vmx_vmwrite_bitmap);
  8038. out6:
  8039. free_page((unsigned long)vmx_vmread_bitmap);
  8040. out5:
  8041. free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic);
  8042. out4:
  8043. free_page((unsigned long)vmx_msr_bitmap_longmode);
  8044. out3:
  8045. free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic);
  8046. out2:
  8047. free_page((unsigned long)vmx_msr_bitmap_legacy);
  8048. out1:
  8049. free_page((unsigned long)vmx_io_bitmap_b);
  8050. out:
  8051. free_page((unsigned long)vmx_io_bitmap_a);
  8052. return r;
  8053. }
  8054. static void __exit vmx_exit(void)
  8055. {
  8056. free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic);
  8057. free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic);
  8058. free_page((unsigned long)vmx_msr_bitmap_legacy);
  8059. free_page((unsigned long)vmx_msr_bitmap_longmode);
  8060. free_page((unsigned long)vmx_io_bitmap_b);
  8061. free_page((unsigned long)vmx_io_bitmap_a);
  8062. free_page((unsigned long)vmx_vmwrite_bitmap);
  8063. free_page((unsigned long)vmx_vmread_bitmap);
  8064. #ifdef CONFIG_KEXEC
  8065. RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
  8066. synchronize_rcu();
  8067. #endif
  8068. kvm_exit();
  8069. }
  8070. module_init(vmx_init)
  8071. module_exit(vmx_exit)