vmx.c 436 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250
  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 "lapic.h"
  22. #include "hyperv.h"
  23. #include <linux/kvm_host.h>
  24. #include <linux/module.h>
  25. #include <linux/kernel.h>
  26. #include <linux/mm.h>
  27. #include <linux/highmem.h>
  28. #include <linux/sched.h>
  29. #include <linux/moduleparam.h>
  30. #include <linux/mod_devicetable.h>
  31. #include <linux/trace_events.h>
  32. #include <linux/slab.h>
  33. #include <linux/tboot.h>
  34. #include <linux/hrtimer.h>
  35. #include <linux/frame.h>
  36. #include <linux/nospec.h>
  37. #include "kvm_cache_regs.h"
  38. #include "x86.h"
  39. #include <asm/asm.h>
  40. #include <asm/cpu.h>
  41. #include <asm/io.h>
  42. #include <asm/desc.h>
  43. #include <asm/vmx.h>
  44. #include <asm/virtext.h>
  45. #include <asm/mce.h>
  46. #include <asm/fpu/internal.h>
  47. #include <asm/perf_event.h>
  48. #include <asm/debugreg.h>
  49. #include <asm/kexec.h>
  50. #include <asm/apic.h>
  51. #include <asm/irq_remapping.h>
  52. #include <asm/mmu_context.h>
  53. #include <asm/spec-ctrl.h>
  54. #include <asm/mshyperv.h>
  55. #include "trace.h"
  56. #include "pmu.h"
  57. #include "vmx_evmcs.h"
  58. #define __ex(x) __kvm_handle_fault_on_reboot(x)
  59. #define __ex_clear(x, reg) \
  60. ____kvm_handle_fault_on_reboot(x, "xor " reg ", " reg)
  61. MODULE_AUTHOR("Qumranet");
  62. MODULE_LICENSE("GPL");
  63. static const struct x86_cpu_id vmx_cpu_id[] = {
  64. X86_FEATURE_MATCH(X86_FEATURE_VMX),
  65. {}
  66. };
  67. MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
  68. static bool __read_mostly enable_vpid = 1;
  69. module_param_named(vpid, enable_vpid, bool, 0444);
  70. static bool __read_mostly enable_vnmi = 1;
  71. module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
  72. static bool __read_mostly flexpriority_enabled = 1;
  73. module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
  74. static bool __read_mostly enable_ept = 1;
  75. module_param_named(ept, enable_ept, bool, S_IRUGO);
  76. static bool __read_mostly enable_unrestricted_guest = 1;
  77. module_param_named(unrestricted_guest,
  78. enable_unrestricted_guest, bool, S_IRUGO);
  79. static bool __read_mostly enable_ept_ad_bits = 1;
  80. module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
  81. static bool __read_mostly emulate_invalid_guest_state = true;
  82. module_param(emulate_invalid_guest_state, bool, S_IRUGO);
  83. static bool __read_mostly fasteoi = 1;
  84. module_param(fasteoi, bool, S_IRUGO);
  85. static bool __read_mostly enable_apicv = 1;
  86. module_param(enable_apicv, bool, S_IRUGO);
  87. static bool __read_mostly enable_shadow_vmcs = 1;
  88. module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
  89. /*
  90. * If nested=1, nested virtualization is supported, i.e., guests may use
  91. * VMX and be a hypervisor for its own guests. If nested=0, guests may not
  92. * use VMX instructions.
  93. */
  94. static bool __read_mostly nested = 1;
  95. module_param(nested, bool, S_IRUGO);
  96. static bool __read_mostly nested_early_check = 0;
  97. module_param(nested_early_check, bool, S_IRUGO);
  98. static u64 __read_mostly host_xss;
  99. static bool __read_mostly enable_pml = 1;
  100. module_param_named(pml, enable_pml, bool, S_IRUGO);
  101. #define MSR_TYPE_R 1
  102. #define MSR_TYPE_W 2
  103. #define MSR_TYPE_RW 3
  104. #define MSR_BITMAP_MODE_X2APIC 1
  105. #define MSR_BITMAP_MODE_X2APIC_APICV 2
  106. #define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
  107. /* Guest_tsc -> host_tsc conversion requires 64-bit division. */
  108. static int __read_mostly cpu_preemption_timer_multi;
  109. static bool __read_mostly enable_preemption_timer = 1;
  110. #ifdef CONFIG_X86_64
  111. module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
  112. #endif
  113. #define KVM_VM_CR0_ALWAYS_OFF (X86_CR0_NW | X86_CR0_CD)
  114. #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
  115. #define KVM_VM_CR0_ALWAYS_ON \
  116. (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
  117. X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
  118. #define KVM_CR4_GUEST_OWNED_BITS \
  119. (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
  120. | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
  121. #define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
  122. #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
  123. #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
  124. #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
  125. #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
  126. /*
  127. * Hyper-V requires all of these, so mark them as supported even though
  128. * they are just treated the same as all-context.
  129. */
  130. #define VMX_VPID_EXTENT_SUPPORTED_MASK \
  131. (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
  132. VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
  133. VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
  134. VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
  135. /*
  136. * These 2 parameters are used to config the controls for Pause-Loop Exiting:
  137. * ple_gap: upper bound on the amount of time between two successive
  138. * executions of PAUSE in a loop. Also indicate if ple enabled.
  139. * According to test, this time is usually smaller than 128 cycles.
  140. * ple_window: upper bound on the amount of time a guest is allowed to execute
  141. * in a PAUSE loop. Tests indicate that most spinlocks are held for
  142. * less than 2^12 cycles
  143. * Time is measured based on a counter that runs at the same rate as the TSC,
  144. * refer SDM volume 3b section 21.6.13 & 22.1.3.
  145. */
  146. static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
  147. module_param(ple_gap, uint, 0444);
  148. static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
  149. module_param(ple_window, uint, 0444);
  150. /* Default doubles per-vcpu window every exit. */
  151. static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
  152. module_param(ple_window_grow, uint, 0444);
  153. /* Default resets per-vcpu window every exit to ple_window. */
  154. static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
  155. module_param(ple_window_shrink, uint, 0444);
  156. /* Default is to compute the maximum so we can never overflow. */
  157. static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
  158. module_param(ple_window_max, uint, 0444);
  159. extern const ulong vmx_return;
  160. extern const ulong vmx_early_consistency_check_return;
  161. static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
  162. static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
  163. static DEFINE_MUTEX(vmx_l1d_flush_mutex);
  164. /* Storage for pre module init parameter parsing */
  165. static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
  166. static const struct {
  167. const char *option;
  168. bool for_parse;
  169. } vmentry_l1d_param[] = {
  170. [VMENTER_L1D_FLUSH_AUTO] = {"auto", true},
  171. [VMENTER_L1D_FLUSH_NEVER] = {"never", true},
  172. [VMENTER_L1D_FLUSH_COND] = {"cond", true},
  173. [VMENTER_L1D_FLUSH_ALWAYS] = {"always", true},
  174. [VMENTER_L1D_FLUSH_EPT_DISABLED] = {"EPT disabled", false},
  175. [VMENTER_L1D_FLUSH_NOT_REQUIRED] = {"not required", false},
  176. };
  177. #define L1D_CACHE_ORDER 4
  178. static void *vmx_l1d_flush_pages;
  179. static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
  180. {
  181. struct page *page;
  182. unsigned int i;
  183. if (!enable_ept) {
  184. l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
  185. return 0;
  186. }
  187. if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) {
  188. u64 msr;
  189. rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr);
  190. if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) {
  191. l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
  192. return 0;
  193. }
  194. }
  195. /* If set to auto use the default l1tf mitigation method */
  196. if (l1tf == VMENTER_L1D_FLUSH_AUTO) {
  197. switch (l1tf_mitigation) {
  198. case L1TF_MITIGATION_OFF:
  199. l1tf = VMENTER_L1D_FLUSH_NEVER;
  200. break;
  201. case L1TF_MITIGATION_FLUSH_NOWARN:
  202. case L1TF_MITIGATION_FLUSH:
  203. case L1TF_MITIGATION_FLUSH_NOSMT:
  204. l1tf = VMENTER_L1D_FLUSH_COND;
  205. break;
  206. case L1TF_MITIGATION_FULL:
  207. case L1TF_MITIGATION_FULL_FORCE:
  208. l1tf = VMENTER_L1D_FLUSH_ALWAYS;
  209. break;
  210. }
  211. } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) {
  212. l1tf = VMENTER_L1D_FLUSH_ALWAYS;
  213. }
  214. if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
  215. !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
  216. page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
  217. if (!page)
  218. return -ENOMEM;
  219. vmx_l1d_flush_pages = page_address(page);
  220. /*
  221. * Initialize each page with a different pattern in
  222. * order to protect against KSM in the nested
  223. * virtualization case.
  224. */
  225. for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) {
  226. memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1,
  227. PAGE_SIZE);
  228. }
  229. }
  230. l1tf_vmx_mitigation = l1tf;
  231. if (l1tf != VMENTER_L1D_FLUSH_NEVER)
  232. static_branch_enable(&vmx_l1d_should_flush);
  233. else
  234. static_branch_disable(&vmx_l1d_should_flush);
  235. if (l1tf == VMENTER_L1D_FLUSH_COND)
  236. static_branch_enable(&vmx_l1d_flush_cond);
  237. else
  238. static_branch_disable(&vmx_l1d_flush_cond);
  239. return 0;
  240. }
  241. static int vmentry_l1d_flush_parse(const char *s)
  242. {
  243. unsigned int i;
  244. if (s) {
  245. for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
  246. if (vmentry_l1d_param[i].for_parse &&
  247. sysfs_streq(s, vmentry_l1d_param[i].option))
  248. return i;
  249. }
  250. }
  251. return -EINVAL;
  252. }
  253. static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
  254. {
  255. int l1tf, ret;
  256. l1tf = vmentry_l1d_flush_parse(s);
  257. if (l1tf < 0)
  258. return l1tf;
  259. if (!boot_cpu_has(X86_BUG_L1TF))
  260. return 0;
  261. /*
  262. * Has vmx_init() run already? If not then this is the pre init
  263. * parameter parsing. In that case just store the value and let
  264. * vmx_init() do the proper setup after enable_ept has been
  265. * established.
  266. */
  267. if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
  268. vmentry_l1d_flush_param = l1tf;
  269. return 0;
  270. }
  271. mutex_lock(&vmx_l1d_flush_mutex);
  272. ret = vmx_setup_l1d_flush(l1tf);
  273. mutex_unlock(&vmx_l1d_flush_mutex);
  274. return ret;
  275. }
  276. static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
  277. {
  278. if (WARN_ON_ONCE(l1tf_vmx_mitigation >= ARRAY_SIZE(vmentry_l1d_param)))
  279. return sprintf(s, "???\n");
  280. return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
  281. }
  282. static const struct kernel_param_ops vmentry_l1d_flush_ops = {
  283. .set = vmentry_l1d_flush_set,
  284. .get = vmentry_l1d_flush_get,
  285. };
  286. module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644);
  287. enum ept_pointers_status {
  288. EPT_POINTERS_CHECK = 0,
  289. EPT_POINTERS_MATCH = 1,
  290. EPT_POINTERS_MISMATCH = 2
  291. };
  292. struct kvm_vmx {
  293. struct kvm kvm;
  294. unsigned int tss_addr;
  295. bool ept_identity_pagetable_done;
  296. gpa_t ept_identity_map_addr;
  297. enum ept_pointers_status ept_pointers_match;
  298. spinlock_t ept_pointer_lock;
  299. };
  300. #define NR_AUTOLOAD_MSRS 8
  301. struct vmcs_hdr {
  302. u32 revision_id:31;
  303. u32 shadow_vmcs:1;
  304. };
  305. struct vmcs {
  306. struct vmcs_hdr hdr;
  307. u32 abort;
  308. char data[0];
  309. };
  310. /*
  311. * vmcs_host_state tracks registers that are loaded from the VMCS on VMEXIT
  312. * and whose values change infrequently, but are not constant. I.e. this is
  313. * used as a write-through cache of the corresponding VMCS fields.
  314. */
  315. struct vmcs_host_state {
  316. unsigned long cr3; /* May not match real cr3 */
  317. unsigned long cr4; /* May not match real cr4 */
  318. unsigned long gs_base;
  319. unsigned long fs_base;
  320. u16 fs_sel, gs_sel, ldt_sel;
  321. #ifdef CONFIG_X86_64
  322. u16 ds_sel, es_sel;
  323. #endif
  324. };
  325. /*
  326. * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
  327. * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
  328. * loaded on this CPU (so we can clear them if the CPU goes down).
  329. */
  330. struct loaded_vmcs {
  331. struct vmcs *vmcs;
  332. struct vmcs *shadow_vmcs;
  333. int cpu;
  334. bool launched;
  335. bool nmi_known_unmasked;
  336. bool hv_timer_armed;
  337. /* Support for vnmi-less CPUs */
  338. int soft_vnmi_blocked;
  339. ktime_t entry_time;
  340. s64 vnmi_blocked_time;
  341. unsigned long *msr_bitmap;
  342. struct list_head loaded_vmcss_on_cpu_link;
  343. struct vmcs_host_state host_state;
  344. };
  345. struct shared_msr_entry {
  346. unsigned index;
  347. u64 data;
  348. u64 mask;
  349. };
  350. /*
  351. * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
  352. * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
  353. * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
  354. * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
  355. * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
  356. * More than one of these structures may exist, if L1 runs multiple L2 guests.
  357. * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
  358. * underlying hardware which will be used to run L2.
  359. * This structure is packed to ensure that its layout is identical across
  360. * machines (necessary for live migration).
  361. *
  362. * IMPORTANT: Changing the layout of existing fields in this structure
  363. * will break save/restore compatibility with older kvm releases. When
  364. * adding new fields, either use space in the reserved padding* arrays
  365. * or add the new fields to the end of the structure.
  366. */
  367. typedef u64 natural_width;
  368. struct __packed vmcs12 {
  369. /* According to the Intel spec, a VMCS region must start with the
  370. * following two fields. Then follow implementation-specific data.
  371. */
  372. struct vmcs_hdr hdr;
  373. u32 abort;
  374. u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
  375. u32 padding[7]; /* room for future expansion */
  376. u64 io_bitmap_a;
  377. u64 io_bitmap_b;
  378. u64 msr_bitmap;
  379. u64 vm_exit_msr_store_addr;
  380. u64 vm_exit_msr_load_addr;
  381. u64 vm_entry_msr_load_addr;
  382. u64 tsc_offset;
  383. u64 virtual_apic_page_addr;
  384. u64 apic_access_addr;
  385. u64 posted_intr_desc_addr;
  386. u64 ept_pointer;
  387. u64 eoi_exit_bitmap0;
  388. u64 eoi_exit_bitmap1;
  389. u64 eoi_exit_bitmap2;
  390. u64 eoi_exit_bitmap3;
  391. u64 xss_exit_bitmap;
  392. u64 guest_physical_address;
  393. u64 vmcs_link_pointer;
  394. u64 guest_ia32_debugctl;
  395. u64 guest_ia32_pat;
  396. u64 guest_ia32_efer;
  397. u64 guest_ia32_perf_global_ctrl;
  398. u64 guest_pdptr0;
  399. u64 guest_pdptr1;
  400. u64 guest_pdptr2;
  401. u64 guest_pdptr3;
  402. u64 guest_bndcfgs;
  403. u64 host_ia32_pat;
  404. u64 host_ia32_efer;
  405. u64 host_ia32_perf_global_ctrl;
  406. u64 vmread_bitmap;
  407. u64 vmwrite_bitmap;
  408. u64 vm_function_control;
  409. u64 eptp_list_address;
  410. u64 pml_address;
  411. u64 padding64[3]; /* room for future expansion */
  412. /*
  413. * To allow migration of L1 (complete with its L2 guests) between
  414. * machines of different natural widths (32 or 64 bit), we cannot have
  415. * unsigned long fields with no explict size. We use u64 (aliased
  416. * natural_width) instead. Luckily, x86 is little-endian.
  417. */
  418. natural_width cr0_guest_host_mask;
  419. natural_width cr4_guest_host_mask;
  420. natural_width cr0_read_shadow;
  421. natural_width cr4_read_shadow;
  422. natural_width cr3_target_value0;
  423. natural_width cr3_target_value1;
  424. natural_width cr3_target_value2;
  425. natural_width cr3_target_value3;
  426. natural_width exit_qualification;
  427. natural_width guest_linear_address;
  428. natural_width guest_cr0;
  429. natural_width guest_cr3;
  430. natural_width guest_cr4;
  431. natural_width guest_es_base;
  432. natural_width guest_cs_base;
  433. natural_width guest_ss_base;
  434. natural_width guest_ds_base;
  435. natural_width guest_fs_base;
  436. natural_width guest_gs_base;
  437. natural_width guest_ldtr_base;
  438. natural_width guest_tr_base;
  439. natural_width guest_gdtr_base;
  440. natural_width guest_idtr_base;
  441. natural_width guest_dr7;
  442. natural_width guest_rsp;
  443. natural_width guest_rip;
  444. natural_width guest_rflags;
  445. natural_width guest_pending_dbg_exceptions;
  446. natural_width guest_sysenter_esp;
  447. natural_width guest_sysenter_eip;
  448. natural_width host_cr0;
  449. natural_width host_cr3;
  450. natural_width host_cr4;
  451. natural_width host_fs_base;
  452. natural_width host_gs_base;
  453. natural_width host_tr_base;
  454. natural_width host_gdtr_base;
  455. natural_width host_idtr_base;
  456. natural_width host_ia32_sysenter_esp;
  457. natural_width host_ia32_sysenter_eip;
  458. natural_width host_rsp;
  459. natural_width host_rip;
  460. natural_width paddingl[8]; /* room for future expansion */
  461. u32 pin_based_vm_exec_control;
  462. u32 cpu_based_vm_exec_control;
  463. u32 exception_bitmap;
  464. u32 page_fault_error_code_mask;
  465. u32 page_fault_error_code_match;
  466. u32 cr3_target_count;
  467. u32 vm_exit_controls;
  468. u32 vm_exit_msr_store_count;
  469. u32 vm_exit_msr_load_count;
  470. u32 vm_entry_controls;
  471. u32 vm_entry_msr_load_count;
  472. u32 vm_entry_intr_info_field;
  473. u32 vm_entry_exception_error_code;
  474. u32 vm_entry_instruction_len;
  475. u32 tpr_threshold;
  476. u32 secondary_vm_exec_control;
  477. u32 vm_instruction_error;
  478. u32 vm_exit_reason;
  479. u32 vm_exit_intr_info;
  480. u32 vm_exit_intr_error_code;
  481. u32 idt_vectoring_info_field;
  482. u32 idt_vectoring_error_code;
  483. u32 vm_exit_instruction_len;
  484. u32 vmx_instruction_info;
  485. u32 guest_es_limit;
  486. u32 guest_cs_limit;
  487. u32 guest_ss_limit;
  488. u32 guest_ds_limit;
  489. u32 guest_fs_limit;
  490. u32 guest_gs_limit;
  491. u32 guest_ldtr_limit;
  492. u32 guest_tr_limit;
  493. u32 guest_gdtr_limit;
  494. u32 guest_idtr_limit;
  495. u32 guest_es_ar_bytes;
  496. u32 guest_cs_ar_bytes;
  497. u32 guest_ss_ar_bytes;
  498. u32 guest_ds_ar_bytes;
  499. u32 guest_fs_ar_bytes;
  500. u32 guest_gs_ar_bytes;
  501. u32 guest_ldtr_ar_bytes;
  502. u32 guest_tr_ar_bytes;
  503. u32 guest_interruptibility_info;
  504. u32 guest_activity_state;
  505. u32 guest_sysenter_cs;
  506. u32 host_ia32_sysenter_cs;
  507. u32 vmx_preemption_timer_value;
  508. u32 padding32[7]; /* room for future expansion */
  509. u16 virtual_processor_id;
  510. u16 posted_intr_nv;
  511. u16 guest_es_selector;
  512. u16 guest_cs_selector;
  513. u16 guest_ss_selector;
  514. u16 guest_ds_selector;
  515. u16 guest_fs_selector;
  516. u16 guest_gs_selector;
  517. u16 guest_ldtr_selector;
  518. u16 guest_tr_selector;
  519. u16 guest_intr_status;
  520. u16 host_es_selector;
  521. u16 host_cs_selector;
  522. u16 host_ss_selector;
  523. u16 host_ds_selector;
  524. u16 host_fs_selector;
  525. u16 host_gs_selector;
  526. u16 host_tr_selector;
  527. u16 guest_pml_index;
  528. };
  529. /*
  530. * For save/restore compatibility, the vmcs12 field offsets must not change.
  531. */
  532. #define CHECK_OFFSET(field, loc) \
  533. BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \
  534. "Offset of " #field " in struct vmcs12 has changed.")
  535. static inline void vmx_check_vmcs12_offsets(void) {
  536. CHECK_OFFSET(hdr, 0);
  537. CHECK_OFFSET(abort, 4);
  538. CHECK_OFFSET(launch_state, 8);
  539. CHECK_OFFSET(io_bitmap_a, 40);
  540. CHECK_OFFSET(io_bitmap_b, 48);
  541. CHECK_OFFSET(msr_bitmap, 56);
  542. CHECK_OFFSET(vm_exit_msr_store_addr, 64);
  543. CHECK_OFFSET(vm_exit_msr_load_addr, 72);
  544. CHECK_OFFSET(vm_entry_msr_load_addr, 80);
  545. CHECK_OFFSET(tsc_offset, 88);
  546. CHECK_OFFSET(virtual_apic_page_addr, 96);
  547. CHECK_OFFSET(apic_access_addr, 104);
  548. CHECK_OFFSET(posted_intr_desc_addr, 112);
  549. CHECK_OFFSET(ept_pointer, 120);
  550. CHECK_OFFSET(eoi_exit_bitmap0, 128);
  551. CHECK_OFFSET(eoi_exit_bitmap1, 136);
  552. CHECK_OFFSET(eoi_exit_bitmap2, 144);
  553. CHECK_OFFSET(eoi_exit_bitmap3, 152);
  554. CHECK_OFFSET(xss_exit_bitmap, 160);
  555. CHECK_OFFSET(guest_physical_address, 168);
  556. CHECK_OFFSET(vmcs_link_pointer, 176);
  557. CHECK_OFFSET(guest_ia32_debugctl, 184);
  558. CHECK_OFFSET(guest_ia32_pat, 192);
  559. CHECK_OFFSET(guest_ia32_efer, 200);
  560. CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208);
  561. CHECK_OFFSET(guest_pdptr0, 216);
  562. CHECK_OFFSET(guest_pdptr1, 224);
  563. CHECK_OFFSET(guest_pdptr2, 232);
  564. CHECK_OFFSET(guest_pdptr3, 240);
  565. CHECK_OFFSET(guest_bndcfgs, 248);
  566. CHECK_OFFSET(host_ia32_pat, 256);
  567. CHECK_OFFSET(host_ia32_efer, 264);
  568. CHECK_OFFSET(host_ia32_perf_global_ctrl, 272);
  569. CHECK_OFFSET(vmread_bitmap, 280);
  570. CHECK_OFFSET(vmwrite_bitmap, 288);
  571. CHECK_OFFSET(vm_function_control, 296);
  572. CHECK_OFFSET(eptp_list_address, 304);
  573. CHECK_OFFSET(pml_address, 312);
  574. CHECK_OFFSET(cr0_guest_host_mask, 344);
  575. CHECK_OFFSET(cr4_guest_host_mask, 352);
  576. CHECK_OFFSET(cr0_read_shadow, 360);
  577. CHECK_OFFSET(cr4_read_shadow, 368);
  578. CHECK_OFFSET(cr3_target_value0, 376);
  579. CHECK_OFFSET(cr3_target_value1, 384);
  580. CHECK_OFFSET(cr3_target_value2, 392);
  581. CHECK_OFFSET(cr3_target_value3, 400);
  582. CHECK_OFFSET(exit_qualification, 408);
  583. CHECK_OFFSET(guest_linear_address, 416);
  584. CHECK_OFFSET(guest_cr0, 424);
  585. CHECK_OFFSET(guest_cr3, 432);
  586. CHECK_OFFSET(guest_cr4, 440);
  587. CHECK_OFFSET(guest_es_base, 448);
  588. CHECK_OFFSET(guest_cs_base, 456);
  589. CHECK_OFFSET(guest_ss_base, 464);
  590. CHECK_OFFSET(guest_ds_base, 472);
  591. CHECK_OFFSET(guest_fs_base, 480);
  592. CHECK_OFFSET(guest_gs_base, 488);
  593. CHECK_OFFSET(guest_ldtr_base, 496);
  594. CHECK_OFFSET(guest_tr_base, 504);
  595. CHECK_OFFSET(guest_gdtr_base, 512);
  596. CHECK_OFFSET(guest_idtr_base, 520);
  597. CHECK_OFFSET(guest_dr7, 528);
  598. CHECK_OFFSET(guest_rsp, 536);
  599. CHECK_OFFSET(guest_rip, 544);
  600. CHECK_OFFSET(guest_rflags, 552);
  601. CHECK_OFFSET(guest_pending_dbg_exceptions, 560);
  602. CHECK_OFFSET(guest_sysenter_esp, 568);
  603. CHECK_OFFSET(guest_sysenter_eip, 576);
  604. CHECK_OFFSET(host_cr0, 584);
  605. CHECK_OFFSET(host_cr3, 592);
  606. CHECK_OFFSET(host_cr4, 600);
  607. CHECK_OFFSET(host_fs_base, 608);
  608. CHECK_OFFSET(host_gs_base, 616);
  609. CHECK_OFFSET(host_tr_base, 624);
  610. CHECK_OFFSET(host_gdtr_base, 632);
  611. CHECK_OFFSET(host_idtr_base, 640);
  612. CHECK_OFFSET(host_ia32_sysenter_esp, 648);
  613. CHECK_OFFSET(host_ia32_sysenter_eip, 656);
  614. CHECK_OFFSET(host_rsp, 664);
  615. CHECK_OFFSET(host_rip, 672);
  616. CHECK_OFFSET(pin_based_vm_exec_control, 744);
  617. CHECK_OFFSET(cpu_based_vm_exec_control, 748);
  618. CHECK_OFFSET(exception_bitmap, 752);
  619. CHECK_OFFSET(page_fault_error_code_mask, 756);
  620. CHECK_OFFSET(page_fault_error_code_match, 760);
  621. CHECK_OFFSET(cr3_target_count, 764);
  622. CHECK_OFFSET(vm_exit_controls, 768);
  623. CHECK_OFFSET(vm_exit_msr_store_count, 772);
  624. CHECK_OFFSET(vm_exit_msr_load_count, 776);
  625. CHECK_OFFSET(vm_entry_controls, 780);
  626. CHECK_OFFSET(vm_entry_msr_load_count, 784);
  627. CHECK_OFFSET(vm_entry_intr_info_field, 788);
  628. CHECK_OFFSET(vm_entry_exception_error_code, 792);
  629. CHECK_OFFSET(vm_entry_instruction_len, 796);
  630. CHECK_OFFSET(tpr_threshold, 800);
  631. CHECK_OFFSET(secondary_vm_exec_control, 804);
  632. CHECK_OFFSET(vm_instruction_error, 808);
  633. CHECK_OFFSET(vm_exit_reason, 812);
  634. CHECK_OFFSET(vm_exit_intr_info, 816);
  635. CHECK_OFFSET(vm_exit_intr_error_code, 820);
  636. CHECK_OFFSET(idt_vectoring_info_field, 824);
  637. CHECK_OFFSET(idt_vectoring_error_code, 828);
  638. CHECK_OFFSET(vm_exit_instruction_len, 832);
  639. CHECK_OFFSET(vmx_instruction_info, 836);
  640. CHECK_OFFSET(guest_es_limit, 840);
  641. CHECK_OFFSET(guest_cs_limit, 844);
  642. CHECK_OFFSET(guest_ss_limit, 848);
  643. CHECK_OFFSET(guest_ds_limit, 852);
  644. CHECK_OFFSET(guest_fs_limit, 856);
  645. CHECK_OFFSET(guest_gs_limit, 860);
  646. CHECK_OFFSET(guest_ldtr_limit, 864);
  647. CHECK_OFFSET(guest_tr_limit, 868);
  648. CHECK_OFFSET(guest_gdtr_limit, 872);
  649. CHECK_OFFSET(guest_idtr_limit, 876);
  650. CHECK_OFFSET(guest_es_ar_bytes, 880);
  651. CHECK_OFFSET(guest_cs_ar_bytes, 884);
  652. CHECK_OFFSET(guest_ss_ar_bytes, 888);
  653. CHECK_OFFSET(guest_ds_ar_bytes, 892);
  654. CHECK_OFFSET(guest_fs_ar_bytes, 896);
  655. CHECK_OFFSET(guest_gs_ar_bytes, 900);
  656. CHECK_OFFSET(guest_ldtr_ar_bytes, 904);
  657. CHECK_OFFSET(guest_tr_ar_bytes, 908);
  658. CHECK_OFFSET(guest_interruptibility_info, 912);
  659. CHECK_OFFSET(guest_activity_state, 916);
  660. CHECK_OFFSET(guest_sysenter_cs, 920);
  661. CHECK_OFFSET(host_ia32_sysenter_cs, 924);
  662. CHECK_OFFSET(vmx_preemption_timer_value, 928);
  663. CHECK_OFFSET(virtual_processor_id, 960);
  664. CHECK_OFFSET(posted_intr_nv, 962);
  665. CHECK_OFFSET(guest_es_selector, 964);
  666. CHECK_OFFSET(guest_cs_selector, 966);
  667. CHECK_OFFSET(guest_ss_selector, 968);
  668. CHECK_OFFSET(guest_ds_selector, 970);
  669. CHECK_OFFSET(guest_fs_selector, 972);
  670. CHECK_OFFSET(guest_gs_selector, 974);
  671. CHECK_OFFSET(guest_ldtr_selector, 976);
  672. CHECK_OFFSET(guest_tr_selector, 978);
  673. CHECK_OFFSET(guest_intr_status, 980);
  674. CHECK_OFFSET(host_es_selector, 982);
  675. CHECK_OFFSET(host_cs_selector, 984);
  676. CHECK_OFFSET(host_ss_selector, 986);
  677. CHECK_OFFSET(host_ds_selector, 988);
  678. CHECK_OFFSET(host_fs_selector, 990);
  679. CHECK_OFFSET(host_gs_selector, 992);
  680. CHECK_OFFSET(host_tr_selector, 994);
  681. CHECK_OFFSET(guest_pml_index, 996);
  682. }
  683. /*
  684. * VMCS12_REVISION is an arbitrary id that should be changed if the content or
  685. * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
  686. * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
  687. *
  688. * IMPORTANT: Changing this value will break save/restore compatibility with
  689. * older kvm releases.
  690. */
  691. #define VMCS12_REVISION 0x11e57ed0
  692. /*
  693. * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
  694. * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
  695. * current implementation, 4K are reserved to avoid future complications.
  696. */
  697. #define VMCS12_SIZE 0x1000
  698. /*
  699. * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
  700. * supported VMCS12 field encoding.
  701. */
  702. #define VMCS12_MAX_FIELD_INDEX 0x17
  703. struct nested_vmx_msrs {
  704. /*
  705. * We only store the "true" versions of the VMX capability MSRs. We
  706. * generate the "non-true" versions by setting the must-be-1 bits
  707. * according to the SDM.
  708. */
  709. u32 procbased_ctls_low;
  710. u32 procbased_ctls_high;
  711. u32 secondary_ctls_low;
  712. u32 secondary_ctls_high;
  713. u32 pinbased_ctls_low;
  714. u32 pinbased_ctls_high;
  715. u32 exit_ctls_low;
  716. u32 exit_ctls_high;
  717. u32 entry_ctls_low;
  718. u32 entry_ctls_high;
  719. u32 misc_low;
  720. u32 misc_high;
  721. u32 ept_caps;
  722. u32 vpid_caps;
  723. u64 basic;
  724. u64 cr0_fixed0;
  725. u64 cr0_fixed1;
  726. u64 cr4_fixed0;
  727. u64 cr4_fixed1;
  728. u64 vmcs_enum;
  729. u64 vmfunc_controls;
  730. };
  731. /*
  732. * The nested_vmx structure is part of vcpu_vmx, and holds information we need
  733. * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
  734. */
  735. struct nested_vmx {
  736. /* Has the level1 guest done vmxon? */
  737. bool vmxon;
  738. gpa_t vmxon_ptr;
  739. bool pml_full;
  740. /* The guest-physical address of the current VMCS L1 keeps for L2 */
  741. gpa_t current_vmptr;
  742. /*
  743. * Cache of the guest's VMCS, existing outside of guest memory.
  744. * Loaded from guest memory during VMPTRLD. Flushed to guest
  745. * memory during VMCLEAR and VMPTRLD.
  746. */
  747. struct vmcs12 *cached_vmcs12;
  748. /*
  749. * Cache of the guest's shadow VMCS, existing outside of guest
  750. * memory. Loaded from guest memory during VM entry. Flushed
  751. * to guest memory during VM exit.
  752. */
  753. struct vmcs12 *cached_shadow_vmcs12;
  754. /*
  755. * Indicates if the shadow vmcs or enlightened vmcs must be updated
  756. * with the data held by struct vmcs12.
  757. */
  758. bool need_vmcs12_sync;
  759. bool dirty_vmcs12;
  760. /*
  761. * vmcs02 has been initialized, i.e. state that is constant for
  762. * vmcs02 has been written to the backing VMCS. Initialization
  763. * is delayed until L1 actually attempts to run a nested VM.
  764. */
  765. bool vmcs02_initialized;
  766. bool change_vmcs01_virtual_apic_mode;
  767. /*
  768. * Enlightened VMCS has been enabled. It does not mean that L1 has to
  769. * use it. However, VMX features available to L1 will be limited based
  770. * on what the enlightened VMCS supports.
  771. */
  772. bool enlightened_vmcs_enabled;
  773. /* L2 must run next, and mustn't decide to exit to L1. */
  774. bool nested_run_pending;
  775. struct loaded_vmcs vmcs02;
  776. /*
  777. * Guest pages referred to in the vmcs02 with host-physical
  778. * pointers, so we must keep them pinned while L2 runs.
  779. */
  780. struct page *apic_access_page;
  781. struct page *virtual_apic_page;
  782. struct page *pi_desc_page;
  783. struct pi_desc *pi_desc;
  784. bool pi_pending;
  785. u16 posted_intr_nv;
  786. struct hrtimer preemption_timer;
  787. bool preemption_timer_expired;
  788. /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
  789. u64 vmcs01_debugctl;
  790. u64 vmcs01_guest_bndcfgs;
  791. u16 vpid02;
  792. u16 last_vpid;
  793. struct nested_vmx_msrs msrs;
  794. /* SMM related state */
  795. struct {
  796. /* in VMX operation on SMM entry? */
  797. bool vmxon;
  798. /* in guest mode on SMM entry? */
  799. bool guest_mode;
  800. } smm;
  801. gpa_t hv_evmcs_vmptr;
  802. struct page *hv_evmcs_page;
  803. struct hv_enlightened_vmcs *hv_evmcs;
  804. };
  805. #define POSTED_INTR_ON 0
  806. #define POSTED_INTR_SN 1
  807. /* Posted-Interrupt Descriptor */
  808. struct pi_desc {
  809. u32 pir[8]; /* Posted interrupt requested */
  810. union {
  811. struct {
  812. /* bit 256 - Outstanding Notification */
  813. u16 on : 1,
  814. /* bit 257 - Suppress Notification */
  815. sn : 1,
  816. /* bit 271:258 - Reserved */
  817. rsvd_1 : 14;
  818. /* bit 279:272 - Notification Vector */
  819. u8 nv;
  820. /* bit 287:280 - Reserved */
  821. u8 rsvd_2;
  822. /* bit 319:288 - Notification Destination */
  823. u32 ndst;
  824. };
  825. u64 control;
  826. };
  827. u32 rsvd[6];
  828. } __aligned(64);
  829. static bool pi_test_and_set_on(struct pi_desc *pi_desc)
  830. {
  831. return test_and_set_bit(POSTED_INTR_ON,
  832. (unsigned long *)&pi_desc->control);
  833. }
  834. static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
  835. {
  836. return test_and_clear_bit(POSTED_INTR_ON,
  837. (unsigned long *)&pi_desc->control);
  838. }
  839. static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
  840. {
  841. return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
  842. }
  843. static inline void pi_clear_sn(struct pi_desc *pi_desc)
  844. {
  845. return clear_bit(POSTED_INTR_SN,
  846. (unsigned long *)&pi_desc->control);
  847. }
  848. static inline void pi_set_sn(struct pi_desc *pi_desc)
  849. {
  850. return set_bit(POSTED_INTR_SN,
  851. (unsigned long *)&pi_desc->control);
  852. }
  853. static inline void pi_clear_on(struct pi_desc *pi_desc)
  854. {
  855. clear_bit(POSTED_INTR_ON,
  856. (unsigned long *)&pi_desc->control);
  857. }
  858. static inline int pi_test_on(struct pi_desc *pi_desc)
  859. {
  860. return test_bit(POSTED_INTR_ON,
  861. (unsigned long *)&pi_desc->control);
  862. }
  863. static inline int pi_test_sn(struct pi_desc *pi_desc)
  864. {
  865. return test_bit(POSTED_INTR_SN,
  866. (unsigned long *)&pi_desc->control);
  867. }
  868. struct vmx_msrs {
  869. unsigned int nr;
  870. struct vmx_msr_entry val[NR_AUTOLOAD_MSRS];
  871. };
  872. struct vcpu_vmx {
  873. struct kvm_vcpu vcpu;
  874. unsigned long host_rsp;
  875. u8 fail;
  876. u8 msr_bitmap_mode;
  877. u32 exit_intr_info;
  878. u32 idt_vectoring_info;
  879. ulong rflags;
  880. struct shared_msr_entry *guest_msrs;
  881. int nmsrs;
  882. int save_nmsrs;
  883. bool guest_msrs_dirty;
  884. unsigned long host_idt_base;
  885. #ifdef CONFIG_X86_64
  886. u64 msr_host_kernel_gs_base;
  887. u64 msr_guest_kernel_gs_base;
  888. #endif
  889. u64 arch_capabilities;
  890. u64 spec_ctrl;
  891. u32 vm_entry_controls_shadow;
  892. u32 vm_exit_controls_shadow;
  893. u32 secondary_exec_control;
  894. /*
  895. * loaded_vmcs points to the VMCS currently used in this vcpu. For a
  896. * non-nested (L1) guest, it always points to vmcs01. For a nested
  897. * guest (L2), it points to a different VMCS. loaded_cpu_state points
  898. * to the VMCS whose state is loaded into the CPU registers that only
  899. * need to be switched when transitioning to/from the kernel; a NULL
  900. * value indicates that host state is loaded.
  901. */
  902. struct loaded_vmcs vmcs01;
  903. struct loaded_vmcs *loaded_vmcs;
  904. struct loaded_vmcs *loaded_cpu_state;
  905. bool __launched; /* temporary, used in vmx_vcpu_run */
  906. struct msr_autoload {
  907. struct vmx_msrs guest;
  908. struct vmx_msrs host;
  909. } msr_autoload;
  910. struct {
  911. int vm86_active;
  912. ulong save_rflags;
  913. struct kvm_segment segs[8];
  914. } rmode;
  915. struct {
  916. u32 bitmask; /* 4 bits per segment (1 bit per field) */
  917. struct kvm_save_segment {
  918. u16 selector;
  919. unsigned long base;
  920. u32 limit;
  921. u32 ar;
  922. } seg[8];
  923. } segment_cache;
  924. int vpid;
  925. bool emulation_required;
  926. u32 exit_reason;
  927. /* Posted interrupt descriptor */
  928. struct pi_desc pi_desc;
  929. /* Support for a guest hypervisor (nested VMX) */
  930. struct nested_vmx nested;
  931. /* Dynamic PLE window. */
  932. int ple_window;
  933. bool ple_window_dirty;
  934. bool req_immediate_exit;
  935. /* Support for PML */
  936. #define PML_ENTITY_NUM 512
  937. struct page *pml_pg;
  938. /* apic deadline value in host tsc */
  939. u64 hv_deadline_tsc;
  940. u64 current_tsc_ratio;
  941. u32 host_pkru;
  942. unsigned long host_debugctlmsr;
  943. /*
  944. * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
  945. * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
  946. * in msr_ia32_feature_control_valid_bits.
  947. */
  948. u64 msr_ia32_feature_control;
  949. u64 msr_ia32_feature_control_valid_bits;
  950. u64 ept_pointer;
  951. };
  952. enum segment_cache_field {
  953. SEG_FIELD_SEL = 0,
  954. SEG_FIELD_BASE = 1,
  955. SEG_FIELD_LIMIT = 2,
  956. SEG_FIELD_AR = 3,
  957. SEG_FIELD_NR = 4
  958. };
  959. static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
  960. {
  961. return container_of(kvm, struct kvm_vmx, kvm);
  962. }
  963. static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
  964. {
  965. return container_of(vcpu, struct vcpu_vmx, vcpu);
  966. }
  967. static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
  968. {
  969. return &(to_vmx(vcpu)->pi_desc);
  970. }
  971. #define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
  972. #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
  973. #define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
  974. #define FIELD64(number, name) \
  975. FIELD(number, name), \
  976. [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
  977. static u16 shadow_read_only_fields[] = {
  978. #define SHADOW_FIELD_RO(x) x,
  979. #include "vmx_shadow_fields.h"
  980. };
  981. static int max_shadow_read_only_fields =
  982. ARRAY_SIZE(shadow_read_only_fields);
  983. static u16 shadow_read_write_fields[] = {
  984. #define SHADOW_FIELD_RW(x) x,
  985. #include "vmx_shadow_fields.h"
  986. };
  987. static int max_shadow_read_write_fields =
  988. ARRAY_SIZE(shadow_read_write_fields);
  989. static const unsigned short vmcs_field_to_offset_table[] = {
  990. FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
  991. FIELD(POSTED_INTR_NV, posted_intr_nv),
  992. FIELD(GUEST_ES_SELECTOR, guest_es_selector),
  993. FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
  994. FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
  995. FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
  996. FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
  997. FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
  998. FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
  999. FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
  1000. FIELD(GUEST_INTR_STATUS, guest_intr_status),
  1001. FIELD(GUEST_PML_INDEX, guest_pml_index),
  1002. FIELD(HOST_ES_SELECTOR, host_es_selector),
  1003. FIELD(HOST_CS_SELECTOR, host_cs_selector),
  1004. FIELD(HOST_SS_SELECTOR, host_ss_selector),
  1005. FIELD(HOST_DS_SELECTOR, host_ds_selector),
  1006. FIELD(HOST_FS_SELECTOR, host_fs_selector),
  1007. FIELD(HOST_GS_SELECTOR, host_gs_selector),
  1008. FIELD(HOST_TR_SELECTOR, host_tr_selector),
  1009. FIELD64(IO_BITMAP_A, io_bitmap_a),
  1010. FIELD64(IO_BITMAP_B, io_bitmap_b),
  1011. FIELD64(MSR_BITMAP, msr_bitmap),
  1012. FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
  1013. FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
  1014. FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
  1015. FIELD64(PML_ADDRESS, pml_address),
  1016. FIELD64(TSC_OFFSET, tsc_offset),
  1017. FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
  1018. FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
  1019. FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
  1020. FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
  1021. FIELD64(EPT_POINTER, ept_pointer),
  1022. FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
  1023. FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
  1024. FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
  1025. FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
  1026. FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
  1027. FIELD64(VMREAD_BITMAP, vmread_bitmap),
  1028. FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
  1029. FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
  1030. FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
  1031. FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
  1032. FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
  1033. FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
  1034. FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
  1035. FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
  1036. FIELD64(GUEST_PDPTR0, guest_pdptr0),
  1037. FIELD64(GUEST_PDPTR1, guest_pdptr1),
  1038. FIELD64(GUEST_PDPTR2, guest_pdptr2),
  1039. FIELD64(GUEST_PDPTR3, guest_pdptr3),
  1040. FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
  1041. FIELD64(HOST_IA32_PAT, host_ia32_pat),
  1042. FIELD64(HOST_IA32_EFER, host_ia32_efer),
  1043. FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
  1044. FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
  1045. FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
  1046. FIELD(EXCEPTION_BITMAP, exception_bitmap),
  1047. FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
  1048. FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
  1049. FIELD(CR3_TARGET_COUNT, cr3_target_count),
  1050. FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
  1051. FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
  1052. FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
  1053. FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
  1054. FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
  1055. FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
  1056. FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
  1057. FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
  1058. FIELD(TPR_THRESHOLD, tpr_threshold),
  1059. FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
  1060. FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
  1061. FIELD(VM_EXIT_REASON, vm_exit_reason),
  1062. FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
  1063. FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
  1064. FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
  1065. FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
  1066. FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
  1067. FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
  1068. FIELD(GUEST_ES_LIMIT, guest_es_limit),
  1069. FIELD(GUEST_CS_LIMIT, guest_cs_limit),
  1070. FIELD(GUEST_SS_LIMIT, guest_ss_limit),
  1071. FIELD(GUEST_DS_LIMIT, guest_ds_limit),
  1072. FIELD(GUEST_FS_LIMIT, guest_fs_limit),
  1073. FIELD(GUEST_GS_LIMIT, guest_gs_limit),
  1074. FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
  1075. FIELD(GUEST_TR_LIMIT, guest_tr_limit),
  1076. FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
  1077. FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
  1078. FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
  1079. FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
  1080. FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
  1081. FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
  1082. FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
  1083. FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
  1084. FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
  1085. FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
  1086. FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
  1087. FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
  1088. FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
  1089. FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
  1090. FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
  1091. FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
  1092. FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
  1093. FIELD(CR0_READ_SHADOW, cr0_read_shadow),
  1094. FIELD(CR4_READ_SHADOW, cr4_read_shadow),
  1095. FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
  1096. FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
  1097. FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
  1098. FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
  1099. FIELD(EXIT_QUALIFICATION, exit_qualification),
  1100. FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
  1101. FIELD(GUEST_CR0, guest_cr0),
  1102. FIELD(GUEST_CR3, guest_cr3),
  1103. FIELD(GUEST_CR4, guest_cr4),
  1104. FIELD(GUEST_ES_BASE, guest_es_base),
  1105. FIELD(GUEST_CS_BASE, guest_cs_base),
  1106. FIELD(GUEST_SS_BASE, guest_ss_base),
  1107. FIELD(GUEST_DS_BASE, guest_ds_base),
  1108. FIELD(GUEST_FS_BASE, guest_fs_base),
  1109. FIELD(GUEST_GS_BASE, guest_gs_base),
  1110. FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
  1111. FIELD(GUEST_TR_BASE, guest_tr_base),
  1112. FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
  1113. FIELD(GUEST_IDTR_BASE, guest_idtr_base),
  1114. FIELD(GUEST_DR7, guest_dr7),
  1115. FIELD(GUEST_RSP, guest_rsp),
  1116. FIELD(GUEST_RIP, guest_rip),
  1117. FIELD(GUEST_RFLAGS, guest_rflags),
  1118. FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
  1119. FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
  1120. FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
  1121. FIELD(HOST_CR0, host_cr0),
  1122. FIELD(HOST_CR3, host_cr3),
  1123. FIELD(HOST_CR4, host_cr4),
  1124. FIELD(HOST_FS_BASE, host_fs_base),
  1125. FIELD(HOST_GS_BASE, host_gs_base),
  1126. FIELD(HOST_TR_BASE, host_tr_base),
  1127. FIELD(HOST_GDTR_BASE, host_gdtr_base),
  1128. FIELD(HOST_IDTR_BASE, host_idtr_base),
  1129. FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
  1130. FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
  1131. FIELD(HOST_RSP, host_rsp),
  1132. FIELD(HOST_RIP, host_rip),
  1133. };
  1134. static inline short vmcs_field_to_offset(unsigned long field)
  1135. {
  1136. const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
  1137. unsigned short offset;
  1138. unsigned index;
  1139. if (field >> 15)
  1140. return -ENOENT;
  1141. index = ROL16(field, 6);
  1142. if (index >= size)
  1143. return -ENOENT;
  1144. index = array_index_nospec(index, size);
  1145. offset = vmcs_field_to_offset_table[index];
  1146. if (offset == 0)
  1147. return -ENOENT;
  1148. return offset;
  1149. }
  1150. static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
  1151. {
  1152. return to_vmx(vcpu)->nested.cached_vmcs12;
  1153. }
  1154. static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu)
  1155. {
  1156. return to_vmx(vcpu)->nested.cached_shadow_vmcs12;
  1157. }
  1158. static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
  1159. static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
  1160. static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
  1161. static bool vmx_xsaves_supported(void);
  1162. static void vmx_set_segment(struct kvm_vcpu *vcpu,
  1163. struct kvm_segment *var, int seg);
  1164. static void vmx_get_segment(struct kvm_vcpu *vcpu,
  1165. struct kvm_segment *var, int seg);
  1166. static bool guest_state_valid(struct kvm_vcpu *vcpu);
  1167. static u32 vmx_segment_access_rights(struct kvm_segment *var);
  1168. static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
  1169. static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
  1170. static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
  1171. static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
  1172. u16 error_code);
  1173. static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
  1174. static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
  1175. u32 msr, int type);
  1176. static DEFINE_PER_CPU(struct vmcs *, vmxarea);
  1177. static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
  1178. /*
  1179. * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
  1180. * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
  1181. */
  1182. static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
  1183. /*
  1184. * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
  1185. * can find which vCPU should be waken up.
  1186. */
  1187. static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
  1188. static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
  1189. enum {
  1190. VMX_VMREAD_BITMAP,
  1191. VMX_VMWRITE_BITMAP,
  1192. VMX_BITMAP_NR
  1193. };
  1194. static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
  1195. #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
  1196. #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
  1197. static bool cpu_has_load_ia32_efer;
  1198. static bool cpu_has_load_perf_global_ctrl;
  1199. static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
  1200. static DEFINE_SPINLOCK(vmx_vpid_lock);
  1201. static struct vmcs_config {
  1202. int size;
  1203. int order;
  1204. u32 basic_cap;
  1205. u32 revision_id;
  1206. u32 pin_based_exec_ctrl;
  1207. u32 cpu_based_exec_ctrl;
  1208. u32 cpu_based_2nd_exec_ctrl;
  1209. u32 vmexit_ctrl;
  1210. u32 vmentry_ctrl;
  1211. struct nested_vmx_msrs nested;
  1212. } vmcs_config;
  1213. static struct vmx_capability {
  1214. u32 ept;
  1215. u32 vpid;
  1216. } vmx_capability;
  1217. #define VMX_SEGMENT_FIELD(seg) \
  1218. [VCPU_SREG_##seg] = { \
  1219. .selector = GUEST_##seg##_SELECTOR, \
  1220. .base = GUEST_##seg##_BASE, \
  1221. .limit = GUEST_##seg##_LIMIT, \
  1222. .ar_bytes = GUEST_##seg##_AR_BYTES, \
  1223. }
  1224. static const struct kvm_vmx_segment_field {
  1225. unsigned selector;
  1226. unsigned base;
  1227. unsigned limit;
  1228. unsigned ar_bytes;
  1229. } kvm_vmx_segment_fields[] = {
  1230. VMX_SEGMENT_FIELD(CS),
  1231. VMX_SEGMENT_FIELD(DS),
  1232. VMX_SEGMENT_FIELD(ES),
  1233. VMX_SEGMENT_FIELD(FS),
  1234. VMX_SEGMENT_FIELD(GS),
  1235. VMX_SEGMENT_FIELD(SS),
  1236. VMX_SEGMENT_FIELD(TR),
  1237. VMX_SEGMENT_FIELD(LDTR),
  1238. };
  1239. static u64 host_efer;
  1240. static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
  1241. /*
  1242. * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
  1243. * away by decrementing the array size.
  1244. */
  1245. static const u32 vmx_msr_index[] = {
  1246. #ifdef CONFIG_X86_64
  1247. MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
  1248. #endif
  1249. MSR_EFER, MSR_TSC_AUX, MSR_STAR,
  1250. };
  1251. DEFINE_STATIC_KEY_FALSE(enable_evmcs);
  1252. #define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
  1253. #define KVM_EVMCS_VERSION 1
  1254. /*
  1255. * Enlightened VMCSv1 doesn't support these:
  1256. *
  1257. * POSTED_INTR_NV = 0x00000002,
  1258. * GUEST_INTR_STATUS = 0x00000810,
  1259. * APIC_ACCESS_ADDR = 0x00002014,
  1260. * POSTED_INTR_DESC_ADDR = 0x00002016,
  1261. * EOI_EXIT_BITMAP0 = 0x0000201c,
  1262. * EOI_EXIT_BITMAP1 = 0x0000201e,
  1263. * EOI_EXIT_BITMAP2 = 0x00002020,
  1264. * EOI_EXIT_BITMAP3 = 0x00002022,
  1265. * GUEST_PML_INDEX = 0x00000812,
  1266. * PML_ADDRESS = 0x0000200e,
  1267. * VM_FUNCTION_CONTROL = 0x00002018,
  1268. * EPTP_LIST_ADDRESS = 0x00002024,
  1269. * VMREAD_BITMAP = 0x00002026,
  1270. * VMWRITE_BITMAP = 0x00002028,
  1271. *
  1272. * TSC_MULTIPLIER = 0x00002032,
  1273. * PLE_GAP = 0x00004020,
  1274. * PLE_WINDOW = 0x00004022,
  1275. * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
  1276. * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
  1277. * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
  1278. *
  1279. * Currently unsupported in KVM:
  1280. * GUEST_IA32_RTIT_CTL = 0x00002814,
  1281. */
  1282. #define EVMCS1_UNSUPPORTED_PINCTRL (PIN_BASED_POSTED_INTR | \
  1283. PIN_BASED_VMX_PREEMPTION_TIMER)
  1284. #define EVMCS1_UNSUPPORTED_2NDEXEC \
  1285. (SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | \
  1286. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | \
  1287. SECONDARY_EXEC_APIC_REGISTER_VIRT | \
  1288. SECONDARY_EXEC_ENABLE_PML | \
  1289. SECONDARY_EXEC_ENABLE_VMFUNC | \
  1290. SECONDARY_EXEC_SHADOW_VMCS | \
  1291. SECONDARY_EXEC_TSC_SCALING | \
  1292. SECONDARY_EXEC_PAUSE_LOOP_EXITING)
  1293. #define EVMCS1_UNSUPPORTED_VMEXIT_CTRL (VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
  1294. #define EVMCS1_UNSUPPORTED_VMENTRY_CTRL (VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
  1295. #define EVMCS1_UNSUPPORTED_VMFUNC (VMX_VMFUNC_EPTP_SWITCHING)
  1296. #if IS_ENABLED(CONFIG_HYPERV)
  1297. static bool __read_mostly enlightened_vmcs = true;
  1298. module_param(enlightened_vmcs, bool, 0444);
  1299. static inline void evmcs_write64(unsigned long field, u64 value)
  1300. {
  1301. u16 clean_field;
  1302. int offset = get_evmcs_offset(field, &clean_field);
  1303. if (offset < 0)
  1304. return;
  1305. *(u64 *)((char *)current_evmcs + offset) = value;
  1306. current_evmcs->hv_clean_fields &= ~clean_field;
  1307. }
  1308. static inline void evmcs_write32(unsigned long field, u32 value)
  1309. {
  1310. u16 clean_field;
  1311. int offset = get_evmcs_offset(field, &clean_field);
  1312. if (offset < 0)
  1313. return;
  1314. *(u32 *)((char *)current_evmcs + offset) = value;
  1315. current_evmcs->hv_clean_fields &= ~clean_field;
  1316. }
  1317. static inline void evmcs_write16(unsigned long field, u16 value)
  1318. {
  1319. u16 clean_field;
  1320. int offset = get_evmcs_offset(field, &clean_field);
  1321. if (offset < 0)
  1322. return;
  1323. *(u16 *)((char *)current_evmcs + offset) = value;
  1324. current_evmcs->hv_clean_fields &= ~clean_field;
  1325. }
  1326. static inline u64 evmcs_read64(unsigned long field)
  1327. {
  1328. int offset = get_evmcs_offset(field, NULL);
  1329. if (offset < 0)
  1330. return 0;
  1331. return *(u64 *)((char *)current_evmcs + offset);
  1332. }
  1333. static inline u32 evmcs_read32(unsigned long field)
  1334. {
  1335. int offset = get_evmcs_offset(field, NULL);
  1336. if (offset < 0)
  1337. return 0;
  1338. return *(u32 *)((char *)current_evmcs + offset);
  1339. }
  1340. static inline u16 evmcs_read16(unsigned long field)
  1341. {
  1342. int offset = get_evmcs_offset(field, NULL);
  1343. if (offset < 0)
  1344. return 0;
  1345. return *(u16 *)((char *)current_evmcs + offset);
  1346. }
  1347. static inline void evmcs_touch_msr_bitmap(void)
  1348. {
  1349. if (unlikely(!current_evmcs))
  1350. return;
  1351. if (current_evmcs->hv_enlightenments_control.msr_bitmap)
  1352. current_evmcs->hv_clean_fields &=
  1353. ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
  1354. }
  1355. static void evmcs_load(u64 phys_addr)
  1356. {
  1357. struct hv_vp_assist_page *vp_ap =
  1358. hv_get_vp_assist_page(smp_processor_id());
  1359. vp_ap->current_nested_vmcs = phys_addr;
  1360. vp_ap->enlighten_vmentry = 1;
  1361. }
  1362. static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
  1363. {
  1364. vmcs_conf->pin_based_exec_ctrl &= ~EVMCS1_UNSUPPORTED_PINCTRL;
  1365. vmcs_conf->cpu_based_2nd_exec_ctrl &= ~EVMCS1_UNSUPPORTED_2NDEXEC;
  1366. vmcs_conf->vmexit_ctrl &= ~EVMCS1_UNSUPPORTED_VMEXIT_CTRL;
  1367. vmcs_conf->vmentry_ctrl &= ~EVMCS1_UNSUPPORTED_VMENTRY_CTRL;
  1368. }
  1369. /* check_ept_pointer() should be under protection of ept_pointer_lock. */
  1370. static void check_ept_pointer_match(struct kvm *kvm)
  1371. {
  1372. struct kvm_vcpu *vcpu;
  1373. u64 tmp_eptp = INVALID_PAGE;
  1374. int i;
  1375. kvm_for_each_vcpu(i, vcpu, kvm) {
  1376. if (!VALID_PAGE(tmp_eptp)) {
  1377. tmp_eptp = to_vmx(vcpu)->ept_pointer;
  1378. } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) {
  1379. to_kvm_vmx(kvm)->ept_pointers_match
  1380. = EPT_POINTERS_MISMATCH;
  1381. return;
  1382. }
  1383. }
  1384. to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH;
  1385. }
  1386. static int vmx_hv_remote_flush_tlb(struct kvm *kvm)
  1387. {
  1388. struct kvm_vcpu *vcpu;
  1389. int ret = -ENOTSUPP, i;
  1390. spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
  1391. if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK)
  1392. check_ept_pointer_match(kvm);
  1393. /*
  1394. * FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE hypercall needs the address of the
  1395. * base of EPT PML4 table, strip off EPT configuration information.
  1396. */
  1397. if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
  1398. kvm_for_each_vcpu(i, vcpu, kvm)
  1399. ret |= hyperv_flush_guest_mapping(
  1400. to_vmx(kvm_get_vcpu(kvm, i))->ept_pointer & PAGE_MASK);
  1401. } else {
  1402. ret = hyperv_flush_guest_mapping(
  1403. to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer & PAGE_MASK);
  1404. }
  1405. spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
  1406. return ret;
  1407. }
  1408. #else /* !IS_ENABLED(CONFIG_HYPERV) */
  1409. static inline void evmcs_write64(unsigned long field, u64 value) {}
  1410. static inline void evmcs_write32(unsigned long field, u32 value) {}
  1411. static inline void evmcs_write16(unsigned long field, u16 value) {}
  1412. static inline u64 evmcs_read64(unsigned long field) { return 0; }
  1413. static inline u32 evmcs_read32(unsigned long field) { return 0; }
  1414. static inline u16 evmcs_read16(unsigned long field) { return 0; }
  1415. static inline void evmcs_load(u64 phys_addr) {}
  1416. static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
  1417. static inline void evmcs_touch_msr_bitmap(void) {}
  1418. #endif /* IS_ENABLED(CONFIG_HYPERV) */
  1419. static int nested_enable_evmcs(struct kvm_vcpu *vcpu,
  1420. uint16_t *vmcs_version)
  1421. {
  1422. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1423. /*
  1424. * vmcs_version represents the range of supported Enlightened VMCS
  1425. * versions: lower 8 bits is the minimal version, higher 8 bits is the
  1426. * maximum supported version. KVM supports versions from 1 to
  1427. * KVM_EVMCS_VERSION.
  1428. */
  1429. if (vmcs_version)
  1430. *vmcs_version = (KVM_EVMCS_VERSION << 8) | 1;
  1431. /* We don't support disabling the feature for simplicity. */
  1432. if (vmx->nested.enlightened_vmcs_enabled)
  1433. return 0;
  1434. vmx->nested.enlightened_vmcs_enabled = true;
  1435. vmx->nested.msrs.pinbased_ctls_high &= ~EVMCS1_UNSUPPORTED_PINCTRL;
  1436. vmx->nested.msrs.entry_ctls_high &= ~EVMCS1_UNSUPPORTED_VMENTRY_CTRL;
  1437. vmx->nested.msrs.exit_ctls_high &= ~EVMCS1_UNSUPPORTED_VMEXIT_CTRL;
  1438. vmx->nested.msrs.secondary_ctls_high &= ~EVMCS1_UNSUPPORTED_2NDEXEC;
  1439. vmx->nested.msrs.vmfunc_controls &= ~EVMCS1_UNSUPPORTED_VMFUNC;
  1440. return 0;
  1441. }
  1442. static inline bool is_exception_n(u32 intr_info, u8 vector)
  1443. {
  1444. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  1445. INTR_INFO_VALID_MASK)) ==
  1446. (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
  1447. }
  1448. static inline bool is_debug(u32 intr_info)
  1449. {
  1450. return is_exception_n(intr_info, DB_VECTOR);
  1451. }
  1452. static inline bool is_breakpoint(u32 intr_info)
  1453. {
  1454. return is_exception_n(intr_info, BP_VECTOR);
  1455. }
  1456. static inline bool is_page_fault(u32 intr_info)
  1457. {
  1458. return is_exception_n(intr_info, PF_VECTOR);
  1459. }
  1460. static inline bool is_invalid_opcode(u32 intr_info)
  1461. {
  1462. return is_exception_n(intr_info, UD_VECTOR);
  1463. }
  1464. static inline bool is_gp_fault(u32 intr_info)
  1465. {
  1466. return is_exception_n(intr_info, GP_VECTOR);
  1467. }
  1468. static inline bool is_machine_check(u32 intr_info)
  1469. {
  1470. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  1471. INTR_INFO_VALID_MASK)) ==
  1472. (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
  1473. }
  1474. /* Undocumented: icebp/int1 */
  1475. static inline bool is_icebp(u32 intr_info)
  1476. {
  1477. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
  1478. == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
  1479. }
  1480. static inline bool cpu_has_vmx_msr_bitmap(void)
  1481. {
  1482. return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
  1483. }
  1484. static inline bool cpu_has_vmx_tpr_shadow(void)
  1485. {
  1486. return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
  1487. }
  1488. static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
  1489. {
  1490. return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
  1491. }
  1492. static inline bool cpu_has_secondary_exec_ctrls(void)
  1493. {
  1494. return vmcs_config.cpu_based_exec_ctrl &
  1495. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  1496. }
  1497. static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
  1498. {
  1499. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1500. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  1501. }
  1502. static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
  1503. {
  1504. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1505. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  1506. }
  1507. static inline bool cpu_has_vmx_apic_register_virt(void)
  1508. {
  1509. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1510. SECONDARY_EXEC_APIC_REGISTER_VIRT;
  1511. }
  1512. static inline bool cpu_has_vmx_virtual_intr_delivery(void)
  1513. {
  1514. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1515. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
  1516. }
  1517. static inline bool cpu_has_vmx_encls_vmexit(void)
  1518. {
  1519. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1520. SECONDARY_EXEC_ENCLS_EXITING;
  1521. }
  1522. /*
  1523. * Comment's format: document - errata name - stepping - processor name.
  1524. * Refer from
  1525. * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
  1526. */
  1527. static u32 vmx_preemption_cpu_tfms[] = {
  1528. /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
  1529. 0x000206E6,
  1530. /* 323056.pdf - AAX65 - C2 - Xeon L3406 */
  1531. /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
  1532. /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
  1533. 0x00020652,
  1534. /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
  1535. 0x00020655,
  1536. /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
  1537. /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
  1538. /*
  1539. * 320767.pdf - AAP86 - B1 -
  1540. * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
  1541. */
  1542. 0x000106E5,
  1543. /* 321333.pdf - AAM126 - C0 - Xeon 3500 */
  1544. 0x000106A0,
  1545. /* 321333.pdf - AAM126 - C1 - Xeon 3500 */
  1546. 0x000106A1,
  1547. /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
  1548. 0x000106A4,
  1549. /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
  1550. /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
  1551. /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
  1552. 0x000106A5,
  1553. };
  1554. static inline bool cpu_has_broken_vmx_preemption_timer(void)
  1555. {
  1556. u32 eax = cpuid_eax(0x00000001), i;
  1557. /* Clear the reserved bits */
  1558. eax &= ~(0x3U << 14 | 0xfU << 28);
  1559. for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
  1560. if (eax == vmx_preemption_cpu_tfms[i])
  1561. return true;
  1562. return false;
  1563. }
  1564. static inline bool cpu_has_vmx_preemption_timer(void)
  1565. {
  1566. return vmcs_config.pin_based_exec_ctrl &
  1567. PIN_BASED_VMX_PREEMPTION_TIMER;
  1568. }
  1569. static inline bool cpu_has_vmx_posted_intr(void)
  1570. {
  1571. return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
  1572. vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
  1573. }
  1574. static inline bool cpu_has_vmx_apicv(void)
  1575. {
  1576. return cpu_has_vmx_apic_register_virt() &&
  1577. cpu_has_vmx_virtual_intr_delivery() &&
  1578. cpu_has_vmx_posted_intr();
  1579. }
  1580. static inline bool cpu_has_vmx_flexpriority(void)
  1581. {
  1582. return cpu_has_vmx_tpr_shadow() &&
  1583. cpu_has_vmx_virtualize_apic_accesses();
  1584. }
  1585. static inline bool cpu_has_vmx_ept_execute_only(void)
  1586. {
  1587. return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
  1588. }
  1589. static inline bool cpu_has_vmx_ept_2m_page(void)
  1590. {
  1591. return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
  1592. }
  1593. static inline bool cpu_has_vmx_ept_1g_page(void)
  1594. {
  1595. return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
  1596. }
  1597. static inline bool cpu_has_vmx_ept_4levels(void)
  1598. {
  1599. return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
  1600. }
  1601. static inline bool cpu_has_vmx_ept_mt_wb(void)
  1602. {
  1603. return vmx_capability.ept & VMX_EPTP_WB_BIT;
  1604. }
  1605. static inline bool cpu_has_vmx_ept_5levels(void)
  1606. {
  1607. return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
  1608. }
  1609. static inline bool cpu_has_vmx_ept_ad_bits(void)
  1610. {
  1611. return vmx_capability.ept & VMX_EPT_AD_BIT;
  1612. }
  1613. static inline bool cpu_has_vmx_invept_context(void)
  1614. {
  1615. return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
  1616. }
  1617. static inline bool cpu_has_vmx_invept_global(void)
  1618. {
  1619. return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
  1620. }
  1621. static inline bool cpu_has_vmx_invvpid_individual_addr(void)
  1622. {
  1623. return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
  1624. }
  1625. static inline bool cpu_has_vmx_invvpid_single(void)
  1626. {
  1627. return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
  1628. }
  1629. static inline bool cpu_has_vmx_invvpid_global(void)
  1630. {
  1631. return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
  1632. }
  1633. static inline bool cpu_has_vmx_invvpid(void)
  1634. {
  1635. return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
  1636. }
  1637. static inline bool cpu_has_vmx_ept(void)
  1638. {
  1639. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1640. SECONDARY_EXEC_ENABLE_EPT;
  1641. }
  1642. static inline bool cpu_has_vmx_unrestricted_guest(void)
  1643. {
  1644. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1645. SECONDARY_EXEC_UNRESTRICTED_GUEST;
  1646. }
  1647. static inline bool cpu_has_vmx_ple(void)
  1648. {
  1649. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1650. SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  1651. }
  1652. static inline bool cpu_has_vmx_basic_inout(void)
  1653. {
  1654. return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
  1655. }
  1656. static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
  1657. {
  1658. return flexpriority_enabled && lapic_in_kernel(vcpu);
  1659. }
  1660. static inline bool cpu_has_vmx_vpid(void)
  1661. {
  1662. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1663. SECONDARY_EXEC_ENABLE_VPID;
  1664. }
  1665. static inline bool cpu_has_vmx_rdtscp(void)
  1666. {
  1667. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1668. SECONDARY_EXEC_RDTSCP;
  1669. }
  1670. static inline bool cpu_has_vmx_invpcid(void)
  1671. {
  1672. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1673. SECONDARY_EXEC_ENABLE_INVPCID;
  1674. }
  1675. static inline bool cpu_has_virtual_nmis(void)
  1676. {
  1677. return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
  1678. }
  1679. static inline bool cpu_has_vmx_wbinvd_exit(void)
  1680. {
  1681. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1682. SECONDARY_EXEC_WBINVD_EXITING;
  1683. }
  1684. static inline bool cpu_has_vmx_shadow_vmcs(void)
  1685. {
  1686. u64 vmx_msr;
  1687. rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
  1688. /* check if the cpu supports writing r/o exit information fields */
  1689. if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
  1690. return false;
  1691. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1692. SECONDARY_EXEC_SHADOW_VMCS;
  1693. }
  1694. static inline bool cpu_has_vmx_pml(void)
  1695. {
  1696. return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
  1697. }
  1698. static inline bool cpu_has_vmx_tsc_scaling(void)
  1699. {
  1700. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1701. SECONDARY_EXEC_TSC_SCALING;
  1702. }
  1703. static inline bool cpu_has_vmx_vmfunc(void)
  1704. {
  1705. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1706. SECONDARY_EXEC_ENABLE_VMFUNC;
  1707. }
  1708. static bool vmx_umip_emulated(void)
  1709. {
  1710. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1711. SECONDARY_EXEC_DESC;
  1712. }
  1713. static inline bool report_flexpriority(void)
  1714. {
  1715. return flexpriority_enabled;
  1716. }
  1717. static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
  1718. {
  1719. return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
  1720. }
  1721. /*
  1722. * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
  1723. * to modify any valid field of the VMCS, or are the VM-exit
  1724. * information fields read-only?
  1725. */
  1726. static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
  1727. {
  1728. return to_vmx(vcpu)->nested.msrs.misc_low &
  1729. MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
  1730. }
  1731. static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
  1732. {
  1733. return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
  1734. }
  1735. static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
  1736. {
  1737. return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
  1738. CPU_BASED_MONITOR_TRAP_FLAG;
  1739. }
  1740. static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu)
  1741. {
  1742. return to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
  1743. SECONDARY_EXEC_SHADOW_VMCS;
  1744. }
  1745. static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
  1746. {
  1747. return vmcs12->cpu_based_vm_exec_control & bit;
  1748. }
  1749. static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
  1750. {
  1751. return (vmcs12->cpu_based_vm_exec_control &
  1752. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
  1753. (vmcs12->secondary_vm_exec_control & bit);
  1754. }
  1755. static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
  1756. {
  1757. return vmcs12->pin_based_vm_exec_control &
  1758. PIN_BASED_VMX_PREEMPTION_TIMER;
  1759. }
  1760. static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
  1761. {
  1762. return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
  1763. }
  1764. static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
  1765. {
  1766. return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
  1767. }
  1768. static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
  1769. {
  1770. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
  1771. }
  1772. static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
  1773. {
  1774. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
  1775. }
  1776. static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
  1777. {
  1778. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
  1779. }
  1780. static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
  1781. {
  1782. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
  1783. }
  1784. static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
  1785. {
  1786. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
  1787. }
  1788. static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
  1789. {
  1790. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
  1791. }
  1792. static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
  1793. {
  1794. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  1795. }
  1796. static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
  1797. {
  1798. return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
  1799. }
  1800. static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
  1801. {
  1802. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
  1803. }
  1804. static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
  1805. {
  1806. return nested_cpu_has_vmfunc(vmcs12) &&
  1807. (vmcs12->vm_function_control &
  1808. VMX_VMFUNC_EPTP_SWITCHING);
  1809. }
  1810. static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12)
  1811. {
  1812. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS);
  1813. }
  1814. static inline bool is_nmi(u32 intr_info)
  1815. {
  1816. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
  1817. == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
  1818. }
  1819. static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
  1820. u32 exit_intr_info,
  1821. unsigned long exit_qualification);
  1822. static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
  1823. {
  1824. int i;
  1825. for (i = 0; i < vmx->nmsrs; ++i)
  1826. if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
  1827. return i;
  1828. return -1;
  1829. }
  1830. static inline void __invvpid(unsigned long ext, u16 vpid, gva_t gva)
  1831. {
  1832. struct {
  1833. u64 vpid : 16;
  1834. u64 rsvd : 48;
  1835. u64 gva;
  1836. } operand = { vpid, 0, gva };
  1837. bool error;
  1838. asm volatile (__ex("invvpid %2, %1") CC_SET(na)
  1839. : CC_OUT(na) (error) : "r"(ext), "m"(operand));
  1840. BUG_ON(error);
  1841. }
  1842. static inline void __invept(unsigned long ext, u64 eptp, gpa_t gpa)
  1843. {
  1844. struct {
  1845. u64 eptp, gpa;
  1846. } operand = {eptp, gpa};
  1847. bool error;
  1848. asm volatile (__ex("invept %2, %1") CC_SET(na)
  1849. : CC_OUT(na) (error) : "r"(ext), "m"(operand));
  1850. BUG_ON(error);
  1851. }
  1852. static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
  1853. {
  1854. int i;
  1855. i = __find_msr_index(vmx, msr);
  1856. if (i >= 0)
  1857. return &vmx->guest_msrs[i];
  1858. return NULL;
  1859. }
  1860. static void vmcs_clear(struct vmcs *vmcs)
  1861. {
  1862. u64 phys_addr = __pa(vmcs);
  1863. bool error;
  1864. asm volatile (__ex("vmclear %1") CC_SET(na)
  1865. : CC_OUT(na) (error) : "m"(phys_addr));
  1866. if (unlikely(error))
  1867. printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
  1868. vmcs, phys_addr);
  1869. }
  1870. static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
  1871. {
  1872. vmcs_clear(loaded_vmcs->vmcs);
  1873. if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
  1874. vmcs_clear(loaded_vmcs->shadow_vmcs);
  1875. loaded_vmcs->cpu = -1;
  1876. loaded_vmcs->launched = 0;
  1877. }
  1878. static void vmcs_load(struct vmcs *vmcs)
  1879. {
  1880. u64 phys_addr = __pa(vmcs);
  1881. bool error;
  1882. if (static_branch_unlikely(&enable_evmcs))
  1883. return evmcs_load(phys_addr);
  1884. asm volatile (__ex("vmptrld %1") CC_SET(na)
  1885. : CC_OUT(na) (error) : "m"(phys_addr));
  1886. if (unlikely(error))
  1887. printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
  1888. vmcs, phys_addr);
  1889. }
  1890. #ifdef CONFIG_KEXEC_CORE
  1891. /*
  1892. * This bitmap is used to indicate whether the vmclear
  1893. * operation is enabled on all cpus. All disabled by
  1894. * default.
  1895. */
  1896. static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
  1897. static inline void crash_enable_local_vmclear(int cpu)
  1898. {
  1899. cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
  1900. }
  1901. static inline void crash_disable_local_vmclear(int cpu)
  1902. {
  1903. cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
  1904. }
  1905. static inline int crash_local_vmclear_enabled(int cpu)
  1906. {
  1907. return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
  1908. }
  1909. static void crash_vmclear_local_loaded_vmcss(void)
  1910. {
  1911. int cpu = raw_smp_processor_id();
  1912. struct loaded_vmcs *v;
  1913. if (!crash_local_vmclear_enabled(cpu))
  1914. return;
  1915. list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
  1916. loaded_vmcss_on_cpu_link)
  1917. vmcs_clear(v->vmcs);
  1918. }
  1919. #else
  1920. static inline void crash_enable_local_vmclear(int cpu) { }
  1921. static inline void crash_disable_local_vmclear(int cpu) { }
  1922. #endif /* CONFIG_KEXEC_CORE */
  1923. static void __loaded_vmcs_clear(void *arg)
  1924. {
  1925. struct loaded_vmcs *loaded_vmcs = arg;
  1926. int cpu = raw_smp_processor_id();
  1927. if (loaded_vmcs->cpu != cpu)
  1928. return; /* vcpu migration can race with cpu offline */
  1929. if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
  1930. per_cpu(current_vmcs, cpu) = NULL;
  1931. crash_disable_local_vmclear(cpu);
  1932. list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
  1933. /*
  1934. * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
  1935. * is before setting loaded_vmcs->vcpu to -1 which is done in
  1936. * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
  1937. * then adds the vmcs into percpu list before it is deleted.
  1938. */
  1939. smp_wmb();
  1940. loaded_vmcs_init(loaded_vmcs);
  1941. crash_enable_local_vmclear(cpu);
  1942. }
  1943. static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
  1944. {
  1945. int cpu = loaded_vmcs->cpu;
  1946. if (cpu != -1)
  1947. smp_call_function_single(cpu,
  1948. __loaded_vmcs_clear, loaded_vmcs, 1);
  1949. }
  1950. static inline bool vpid_sync_vcpu_addr(int vpid, gva_t addr)
  1951. {
  1952. if (vpid == 0)
  1953. return true;
  1954. if (cpu_has_vmx_invvpid_individual_addr()) {
  1955. __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, vpid, addr);
  1956. return true;
  1957. }
  1958. return false;
  1959. }
  1960. static inline void vpid_sync_vcpu_single(int vpid)
  1961. {
  1962. if (vpid == 0)
  1963. return;
  1964. if (cpu_has_vmx_invvpid_single())
  1965. __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
  1966. }
  1967. static inline void vpid_sync_vcpu_global(void)
  1968. {
  1969. if (cpu_has_vmx_invvpid_global())
  1970. __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
  1971. }
  1972. static inline void vpid_sync_context(int vpid)
  1973. {
  1974. if (cpu_has_vmx_invvpid_single())
  1975. vpid_sync_vcpu_single(vpid);
  1976. else
  1977. vpid_sync_vcpu_global();
  1978. }
  1979. static inline void ept_sync_global(void)
  1980. {
  1981. __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
  1982. }
  1983. static inline void ept_sync_context(u64 eptp)
  1984. {
  1985. if (cpu_has_vmx_invept_context())
  1986. __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
  1987. else
  1988. ept_sync_global();
  1989. }
  1990. static __always_inline void vmcs_check16(unsigned long field)
  1991. {
  1992. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
  1993. "16-bit accessor invalid for 64-bit field");
  1994. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
  1995. "16-bit accessor invalid for 64-bit high field");
  1996. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
  1997. "16-bit accessor invalid for 32-bit high field");
  1998. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
  1999. "16-bit accessor invalid for natural width field");
  2000. }
  2001. static __always_inline void vmcs_check32(unsigned long field)
  2002. {
  2003. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
  2004. "32-bit accessor invalid for 16-bit field");
  2005. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
  2006. "32-bit accessor invalid for natural width field");
  2007. }
  2008. static __always_inline void vmcs_check64(unsigned long field)
  2009. {
  2010. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
  2011. "64-bit accessor invalid for 16-bit field");
  2012. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
  2013. "64-bit accessor invalid for 64-bit high field");
  2014. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
  2015. "64-bit accessor invalid for 32-bit field");
  2016. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
  2017. "64-bit accessor invalid for natural width field");
  2018. }
  2019. static __always_inline void vmcs_checkl(unsigned long field)
  2020. {
  2021. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
  2022. "Natural width accessor invalid for 16-bit field");
  2023. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
  2024. "Natural width accessor invalid for 64-bit field");
  2025. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
  2026. "Natural width accessor invalid for 64-bit high field");
  2027. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
  2028. "Natural width accessor invalid for 32-bit field");
  2029. }
  2030. static __always_inline unsigned long __vmcs_readl(unsigned long field)
  2031. {
  2032. unsigned long value;
  2033. asm volatile (__ex_clear("vmread %1, %0", "%k0")
  2034. : "=r"(value) : "r"(field));
  2035. return value;
  2036. }
  2037. static __always_inline u16 vmcs_read16(unsigned long field)
  2038. {
  2039. vmcs_check16(field);
  2040. if (static_branch_unlikely(&enable_evmcs))
  2041. return evmcs_read16(field);
  2042. return __vmcs_readl(field);
  2043. }
  2044. static __always_inline u32 vmcs_read32(unsigned long field)
  2045. {
  2046. vmcs_check32(field);
  2047. if (static_branch_unlikely(&enable_evmcs))
  2048. return evmcs_read32(field);
  2049. return __vmcs_readl(field);
  2050. }
  2051. static __always_inline u64 vmcs_read64(unsigned long field)
  2052. {
  2053. vmcs_check64(field);
  2054. if (static_branch_unlikely(&enable_evmcs))
  2055. return evmcs_read64(field);
  2056. #ifdef CONFIG_X86_64
  2057. return __vmcs_readl(field);
  2058. #else
  2059. return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
  2060. #endif
  2061. }
  2062. static __always_inline unsigned long vmcs_readl(unsigned long field)
  2063. {
  2064. vmcs_checkl(field);
  2065. if (static_branch_unlikely(&enable_evmcs))
  2066. return evmcs_read64(field);
  2067. return __vmcs_readl(field);
  2068. }
  2069. static noinline void vmwrite_error(unsigned long field, unsigned long value)
  2070. {
  2071. printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
  2072. field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
  2073. dump_stack();
  2074. }
  2075. static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
  2076. {
  2077. bool error;
  2078. asm volatile (__ex("vmwrite %2, %1") CC_SET(na)
  2079. : CC_OUT(na) (error) : "r"(field), "rm"(value));
  2080. if (unlikely(error))
  2081. vmwrite_error(field, value);
  2082. }
  2083. static __always_inline void vmcs_write16(unsigned long field, u16 value)
  2084. {
  2085. vmcs_check16(field);
  2086. if (static_branch_unlikely(&enable_evmcs))
  2087. return evmcs_write16(field, value);
  2088. __vmcs_writel(field, value);
  2089. }
  2090. static __always_inline void vmcs_write32(unsigned long field, u32 value)
  2091. {
  2092. vmcs_check32(field);
  2093. if (static_branch_unlikely(&enable_evmcs))
  2094. return evmcs_write32(field, value);
  2095. __vmcs_writel(field, value);
  2096. }
  2097. static __always_inline void vmcs_write64(unsigned long field, u64 value)
  2098. {
  2099. vmcs_check64(field);
  2100. if (static_branch_unlikely(&enable_evmcs))
  2101. return evmcs_write64(field, value);
  2102. __vmcs_writel(field, value);
  2103. #ifndef CONFIG_X86_64
  2104. asm volatile ("");
  2105. __vmcs_writel(field+1, value >> 32);
  2106. #endif
  2107. }
  2108. static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
  2109. {
  2110. vmcs_checkl(field);
  2111. if (static_branch_unlikely(&enable_evmcs))
  2112. return evmcs_write64(field, value);
  2113. __vmcs_writel(field, value);
  2114. }
  2115. static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
  2116. {
  2117. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
  2118. "vmcs_clear_bits does not support 64-bit fields");
  2119. if (static_branch_unlikely(&enable_evmcs))
  2120. return evmcs_write32(field, evmcs_read32(field) & ~mask);
  2121. __vmcs_writel(field, __vmcs_readl(field) & ~mask);
  2122. }
  2123. static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
  2124. {
  2125. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
  2126. "vmcs_set_bits does not support 64-bit fields");
  2127. if (static_branch_unlikely(&enable_evmcs))
  2128. return evmcs_write32(field, evmcs_read32(field) | mask);
  2129. __vmcs_writel(field, __vmcs_readl(field) | mask);
  2130. }
  2131. static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
  2132. {
  2133. vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
  2134. }
  2135. static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
  2136. {
  2137. vmcs_write32(VM_ENTRY_CONTROLS, val);
  2138. vmx->vm_entry_controls_shadow = val;
  2139. }
  2140. static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
  2141. {
  2142. if (vmx->vm_entry_controls_shadow != val)
  2143. vm_entry_controls_init(vmx, val);
  2144. }
  2145. static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
  2146. {
  2147. return vmx->vm_entry_controls_shadow;
  2148. }
  2149. static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
  2150. {
  2151. vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
  2152. }
  2153. static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
  2154. {
  2155. vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
  2156. }
  2157. static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
  2158. {
  2159. vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
  2160. }
  2161. static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
  2162. {
  2163. vmcs_write32(VM_EXIT_CONTROLS, val);
  2164. vmx->vm_exit_controls_shadow = val;
  2165. }
  2166. static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
  2167. {
  2168. if (vmx->vm_exit_controls_shadow != val)
  2169. vm_exit_controls_init(vmx, val);
  2170. }
  2171. static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
  2172. {
  2173. return vmx->vm_exit_controls_shadow;
  2174. }
  2175. static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
  2176. {
  2177. vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
  2178. }
  2179. static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
  2180. {
  2181. vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
  2182. }
  2183. static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
  2184. {
  2185. vmx->segment_cache.bitmask = 0;
  2186. }
  2187. static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
  2188. unsigned field)
  2189. {
  2190. bool ret;
  2191. u32 mask = 1 << (seg * SEG_FIELD_NR + field);
  2192. if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
  2193. vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
  2194. vmx->segment_cache.bitmask = 0;
  2195. }
  2196. ret = vmx->segment_cache.bitmask & mask;
  2197. vmx->segment_cache.bitmask |= mask;
  2198. return ret;
  2199. }
  2200. static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
  2201. {
  2202. u16 *p = &vmx->segment_cache.seg[seg].selector;
  2203. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
  2204. *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
  2205. return *p;
  2206. }
  2207. static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
  2208. {
  2209. ulong *p = &vmx->segment_cache.seg[seg].base;
  2210. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
  2211. *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
  2212. return *p;
  2213. }
  2214. static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
  2215. {
  2216. u32 *p = &vmx->segment_cache.seg[seg].limit;
  2217. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
  2218. *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
  2219. return *p;
  2220. }
  2221. static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
  2222. {
  2223. u32 *p = &vmx->segment_cache.seg[seg].ar;
  2224. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
  2225. *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
  2226. return *p;
  2227. }
  2228. static void update_exception_bitmap(struct kvm_vcpu *vcpu)
  2229. {
  2230. u32 eb;
  2231. eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
  2232. (1u << DB_VECTOR) | (1u << AC_VECTOR);
  2233. /*
  2234. * Guest access to VMware backdoor ports could legitimately
  2235. * trigger #GP because of TSS I/O permission bitmap.
  2236. * We intercept those #GP and allow access to them anyway
  2237. * as VMware does.
  2238. */
  2239. if (enable_vmware_backdoor)
  2240. eb |= (1u << GP_VECTOR);
  2241. if ((vcpu->guest_debug &
  2242. (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
  2243. (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
  2244. eb |= 1u << BP_VECTOR;
  2245. if (to_vmx(vcpu)->rmode.vm86_active)
  2246. eb = ~0;
  2247. if (enable_ept)
  2248. eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
  2249. /* When we are running a nested L2 guest and L1 specified for it a
  2250. * certain exception bitmap, we must trap the same exceptions and pass
  2251. * them to L1. When running L2, we will only handle the exceptions
  2252. * specified above if L1 did not want them.
  2253. */
  2254. if (is_guest_mode(vcpu))
  2255. eb |= get_vmcs12(vcpu)->exception_bitmap;
  2256. vmcs_write32(EXCEPTION_BITMAP, eb);
  2257. }
  2258. /*
  2259. * Check if MSR is intercepted for currently loaded MSR bitmap.
  2260. */
  2261. static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
  2262. {
  2263. unsigned long *msr_bitmap;
  2264. int f = sizeof(unsigned long);
  2265. if (!cpu_has_vmx_msr_bitmap())
  2266. return true;
  2267. msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
  2268. if (msr <= 0x1fff) {
  2269. return !!test_bit(msr, msr_bitmap + 0x800 / f);
  2270. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  2271. msr &= 0x1fff;
  2272. return !!test_bit(msr, msr_bitmap + 0xc00 / f);
  2273. }
  2274. return true;
  2275. }
  2276. /*
  2277. * Check if MSR is intercepted for L01 MSR bitmap.
  2278. */
  2279. static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
  2280. {
  2281. unsigned long *msr_bitmap;
  2282. int f = sizeof(unsigned long);
  2283. if (!cpu_has_vmx_msr_bitmap())
  2284. return true;
  2285. msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
  2286. if (msr <= 0x1fff) {
  2287. return !!test_bit(msr, msr_bitmap + 0x800 / f);
  2288. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  2289. msr &= 0x1fff;
  2290. return !!test_bit(msr, msr_bitmap + 0xc00 / f);
  2291. }
  2292. return true;
  2293. }
  2294. static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
  2295. unsigned long entry, unsigned long exit)
  2296. {
  2297. vm_entry_controls_clearbit(vmx, entry);
  2298. vm_exit_controls_clearbit(vmx, exit);
  2299. }
  2300. static int find_msr(struct vmx_msrs *m, unsigned int msr)
  2301. {
  2302. unsigned int i;
  2303. for (i = 0; i < m->nr; ++i) {
  2304. if (m->val[i].index == msr)
  2305. return i;
  2306. }
  2307. return -ENOENT;
  2308. }
  2309. static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
  2310. {
  2311. int i;
  2312. struct msr_autoload *m = &vmx->msr_autoload;
  2313. switch (msr) {
  2314. case MSR_EFER:
  2315. if (cpu_has_load_ia32_efer) {
  2316. clear_atomic_switch_msr_special(vmx,
  2317. VM_ENTRY_LOAD_IA32_EFER,
  2318. VM_EXIT_LOAD_IA32_EFER);
  2319. return;
  2320. }
  2321. break;
  2322. case MSR_CORE_PERF_GLOBAL_CTRL:
  2323. if (cpu_has_load_perf_global_ctrl) {
  2324. clear_atomic_switch_msr_special(vmx,
  2325. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
  2326. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
  2327. return;
  2328. }
  2329. break;
  2330. }
  2331. i = find_msr(&m->guest, msr);
  2332. if (i < 0)
  2333. goto skip_guest;
  2334. --m->guest.nr;
  2335. m->guest.val[i] = m->guest.val[m->guest.nr];
  2336. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
  2337. skip_guest:
  2338. i = find_msr(&m->host, msr);
  2339. if (i < 0)
  2340. return;
  2341. --m->host.nr;
  2342. m->host.val[i] = m->host.val[m->host.nr];
  2343. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
  2344. }
  2345. static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
  2346. unsigned long entry, unsigned long exit,
  2347. unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
  2348. u64 guest_val, u64 host_val)
  2349. {
  2350. vmcs_write64(guest_val_vmcs, guest_val);
  2351. if (host_val_vmcs != HOST_IA32_EFER)
  2352. vmcs_write64(host_val_vmcs, host_val);
  2353. vm_entry_controls_setbit(vmx, entry);
  2354. vm_exit_controls_setbit(vmx, exit);
  2355. }
  2356. static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
  2357. u64 guest_val, u64 host_val, bool entry_only)
  2358. {
  2359. int i, j = 0;
  2360. struct msr_autoload *m = &vmx->msr_autoload;
  2361. switch (msr) {
  2362. case MSR_EFER:
  2363. if (cpu_has_load_ia32_efer) {
  2364. add_atomic_switch_msr_special(vmx,
  2365. VM_ENTRY_LOAD_IA32_EFER,
  2366. VM_EXIT_LOAD_IA32_EFER,
  2367. GUEST_IA32_EFER,
  2368. HOST_IA32_EFER,
  2369. guest_val, host_val);
  2370. return;
  2371. }
  2372. break;
  2373. case MSR_CORE_PERF_GLOBAL_CTRL:
  2374. if (cpu_has_load_perf_global_ctrl) {
  2375. add_atomic_switch_msr_special(vmx,
  2376. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
  2377. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
  2378. GUEST_IA32_PERF_GLOBAL_CTRL,
  2379. HOST_IA32_PERF_GLOBAL_CTRL,
  2380. guest_val, host_val);
  2381. return;
  2382. }
  2383. break;
  2384. case MSR_IA32_PEBS_ENABLE:
  2385. /* PEBS needs a quiescent period after being disabled (to write
  2386. * a record). Disabling PEBS through VMX MSR swapping doesn't
  2387. * provide that period, so a CPU could write host's record into
  2388. * guest's memory.
  2389. */
  2390. wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
  2391. }
  2392. i = find_msr(&m->guest, msr);
  2393. if (!entry_only)
  2394. j = find_msr(&m->host, msr);
  2395. if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) {
  2396. printk_once(KERN_WARNING "Not enough msr switch entries. "
  2397. "Can't add msr %x\n", msr);
  2398. return;
  2399. }
  2400. if (i < 0) {
  2401. i = m->guest.nr++;
  2402. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
  2403. }
  2404. m->guest.val[i].index = msr;
  2405. m->guest.val[i].value = guest_val;
  2406. if (entry_only)
  2407. return;
  2408. if (j < 0) {
  2409. j = m->host.nr++;
  2410. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
  2411. }
  2412. m->host.val[j].index = msr;
  2413. m->host.val[j].value = host_val;
  2414. }
  2415. static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
  2416. {
  2417. u64 guest_efer = vmx->vcpu.arch.efer;
  2418. u64 ignore_bits = 0;
  2419. if (!enable_ept) {
  2420. /*
  2421. * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
  2422. * host CPUID is more efficient than testing guest CPUID
  2423. * or CR4. Host SMEP is anyway a requirement for guest SMEP.
  2424. */
  2425. if (boot_cpu_has(X86_FEATURE_SMEP))
  2426. guest_efer |= EFER_NX;
  2427. else if (!(guest_efer & EFER_NX))
  2428. ignore_bits |= EFER_NX;
  2429. }
  2430. /*
  2431. * LMA and LME handled by hardware; SCE meaningless outside long mode.
  2432. */
  2433. ignore_bits |= EFER_SCE;
  2434. #ifdef CONFIG_X86_64
  2435. ignore_bits |= EFER_LMA | EFER_LME;
  2436. /* SCE is meaningful only in long mode on Intel */
  2437. if (guest_efer & EFER_LMA)
  2438. ignore_bits &= ~(u64)EFER_SCE;
  2439. #endif
  2440. /*
  2441. * On EPT, we can't emulate NX, so we must switch EFER atomically.
  2442. * On CPUs that support "load IA32_EFER", always switch EFER
  2443. * atomically, since it's faster than switching it manually.
  2444. */
  2445. if (cpu_has_load_ia32_efer ||
  2446. (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
  2447. if (!(guest_efer & EFER_LMA))
  2448. guest_efer &= ~EFER_LME;
  2449. if (guest_efer != host_efer)
  2450. add_atomic_switch_msr(vmx, MSR_EFER,
  2451. guest_efer, host_efer, false);
  2452. else
  2453. clear_atomic_switch_msr(vmx, MSR_EFER);
  2454. return false;
  2455. } else {
  2456. clear_atomic_switch_msr(vmx, MSR_EFER);
  2457. guest_efer &= ~ignore_bits;
  2458. guest_efer |= host_efer & ignore_bits;
  2459. vmx->guest_msrs[efer_offset].data = guest_efer;
  2460. vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
  2461. return true;
  2462. }
  2463. }
  2464. #ifdef CONFIG_X86_32
  2465. /*
  2466. * On 32-bit kernels, VM exits still load the FS and GS bases from the
  2467. * VMCS rather than the segment table. KVM uses this helper to figure
  2468. * out the current bases to poke them into the VMCS before entry.
  2469. */
  2470. static unsigned long segment_base(u16 selector)
  2471. {
  2472. struct desc_struct *table;
  2473. unsigned long v;
  2474. if (!(selector & ~SEGMENT_RPL_MASK))
  2475. return 0;
  2476. table = get_current_gdt_ro();
  2477. if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
  2478. u16 ldt_selector = kvm_read_ldt();
  2479. if (!(ldt_selector & ~SEGMENT_RPL_MASK))
  2480. return 0;
  2481. table = (struct desc_struct *)segment_base(ldt_selector);
  2482. }
  2483. v = get_desc_base(&table[selector >> 3]);
  2484. return v;
  2485. }
  2486. #endif
  2487. static void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
  2488. {
  2489. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2490. struct vmcs_host_state *host_state;
  2491. #ifdef CONFIG_X86_64
  2492. int cpu = raw_smp_processor_id();
  2493. #endif
  2494. unsigned long fs_base, gs_base;
  2495. u16 fs_sel, gs_sel;
  2496. int i;
  2497. vmx->req_immediate_exit = false;
  2498. /*
  2499. * Note that guest MSRs to be saved/restored can also be changed
  2500. * when guest state is loaded. This happens when guest transitions
  2501. * to/from long-mode by setting MSR_EFER.LMA.
  2502. */
  2503. if (!vmx->loaded_cpu_state || vmx->guest_msrs_dirty) {
  2504. vmx->guest_msrs_dirty = false;
  2505. for (i = 0; i < vmx->save_nmsrs; ++i)
  2506. kvm_set_shared_msr(vmx->guest_msrs[i].index,
  2507. vmx->guest_msrs[i].data,
  2508. vmx->guest_msrs[i].mask);
  2509. }
  2510. if (vmx->loaded_cpu_state)
  2511. return;
  2512. vmx->loaded_cpu_state = vmx->loaded_vmcs;
  2513. host_state = &vmx->loaded_cpu_state->host_state;
  2514. /*
  2515. * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
  2516. * allow segment selectors with cpl > 0 or ti == 1.
  2517. */
  2518. host_state->ldt_sel = kvm_read_ldt();
  2519. #ifdef CONFIG_X86_64
  2520. savesegment(ds, host_state->ds_sel);
  2521. savesegment(es, host_state->es_sel);
  2522. gs_base = cpu_kernelmode_gs_base(cpu);
  2523. if (likely(is_64bit_mm(current->mm))) {
  2524. save_fsgs_for_kvm();
  2525. fs_sel = current->thread.fsindex;
  2526. gs_sel = current->thread.gsindex;
  2527. fs_base = current->thread.fsbase;
  2528. vmx->msr_host_kernel_gs_base = current->thread.gsbase;
  2529. } else {
  2530. savesegment(fs, fs_sel);
  2531. savesegment(gs, gs_sel);
  2532. fs_base = read_msr(MSR_FS_BASE);
  2533. vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
  2534. }
  2535. wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
  2536. #else
  2537. savesegment(fs, fs_sel);
  2538. savesegment(gs, gs_sel);
  2539. fs_base = segment_base(fs_sel);
  2540. gs_base = segment_base(gs_sel);
  2541. #endif
  2542. if (unlikely(fs_sel != host_state->fs_sel)) {
  2543. if (!(fs_sel & 7))
  2544. vmcs_write16(HOST_FS_SELECTOR, fs_sel);
  2545. else
  2546. vmcs_write16(HOST_FS_SELECTOR, 0);
  2547. host_state->fs_sel = fs_sel;
  2548. }
  2549. if (unlikely(gs_sel != host_state->gs_sel)) {
  2550. if (!(gs_sel & 7))
  2551. vmcs_write16(HOST_GS_SELECTOR, gs_sel);
  2552. else
  2553. vmcs_write16(HOST_GS_SELECTOR, 0);
  2554. host_state->gs_sel = gs_sel;
  2555. }
  2556. if (unlikely(fs_base != host_state->fs_base)) {
  2557. vmcs_writel(HOST_FS_BASE, fs_base);
  2558. host_state->fs_base = fs_base;
  2559. }
  2560. if (unlikely(gs_base != host_state->gs_base)) {
  2561. vmcs_writel(HOST_GS_BASE, gs_base);
  2562. host_state->gs_base = gs_base;
  2563. }
  2564. }
  2565. static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
  2566. {
  2567. struct vmcs_host_state *host_state;
  2568. if (!vmx->loaded_cpu_state)
  2569. return;
  2570. WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs);
  2571. host_state = &vmx->loaded_cpu_state->host_state;
  2572. ++vmx->vcpu.stat.host_state_reload;
  2573. vmx->loaded_cpu_state = NULL;
  2574. #ifdef CONFIG_X86_64
  2575. rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
  2576. #endif
  2577. if (host_state->ldt_sel || (host_state->gs_sel & 7)) {
  2578. kvm_load_ldt(host_state->ldt_sel);
  2579. #ifdef CONFIG_X86_64
  2580. load_gs_index(host_state->gs_sel);
  2581. #else
  2582. loadsegment(gs, host_state->gs_sel);
  2583. #endif
  2584. }
  2585. if (host_state->fs_sel & 7)
  2586. loadsegment(fs, host_state->fs_sel);
  2587. #ifdef CONFIG_X86_64
  2588. if (unlikely(host_state->ds_sel | host_state->es_sel)) {
  2589. loadsegment(ds, host_state->ds_sel);
  2590. loadsegment(es, host_state->es_sel);
  2591. }
  2592. #endif
  2593. invalidate_tss_limit();
  2594. #ifdef CONFIG_X86_64
  2595. wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
  2596. #endif
  2597. load_fixmap_gdt(raw_smp_processor_id());
  2598. }
  2599. #ifdef CONFIG_X86_64
  2600. static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
  2601. {
  2602. preempt_disable();
  2603. if (vmx->loaded_cpu_state)
  2604. rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
  2605. preempt_enable();
  2606. return vmx->msr_guest_kernel_gs_base;
  2607. }
  2608. static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
  2609. {
  2610. preempt_disable();
  2611. if (vmx->loaded_cpu_state)
  2612. wrmsrl(MSR_KERNEL_GS_BASE, data);
  2613. preempt_enable();
  2614. vmx->msr_guest_kernel_gs_base = data;
  2615. }
  2616. #endif
  2617. static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
  2618. {
  2619. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  2620. struct pi_desc old, new;
  2621. unsigned int dest;
  2622. /*
  2623. * In case of hot-plug or hot-unplug, we may have to undo
  2624. * vmx_vcpu_pi_put even if there is no assigned device. And we
  2625. * always keep PI.NDST up to date for simplicity: it makes the
  2626. * code easier, and CPU migration is not a fast path.
  2627. */
  2628. if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
  2629. return;
  2630. /*
  2631. * First handle the simple case where no cmpxchg is necessary; just
  2632. * allow posting non-urgent interrupts.
  2633. *
  2634. * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
  2635. * PI.NDST: pi_post_block will do it for us and the wakeup_handler
  2636. * expects the VCPU to be on the blocked_vcpu_list that matches
  2637. * PI.NDST.
  2638. */
  2639. if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
  2640. vcpu->cpu == cpu) {
  2641. pi_clear_sn(pi_desc);
  2642. return;
  2643. }
  2644. /* The full case. */
  2645. do {
  2646. old.control = new.control = pi_desc->control;
  2647. dest = cpu_physical_id(cpu);
  2648. if (x2apic_enabled())
  2649. new.ndst = dest;
  2650. else
  2651. new.ndst = (dest << 8) & 0xFF00;
  2652. new.sn = 0;
  2653. } while (cmpxchg64(&pi_desc->control, old.control,
  2654. new.control) != old.control);
  2655. }
  2656. static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
  2657. {
  2658. vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
  2659. vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
  2660. }
  2661. /*
  2662. * Switches to specified vcpu, until a matching vcpu_put(), but assumes
  2663. * vcpu mutex is already taken.
  2664. */
  2665. static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  2666. {
  2667. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2668. bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
  2669. if (!already_loaded) {
  2670. loaded_vmcs_clear(vmx->loaded_vmcs);
  2671. local_irq_disable();
  2672. crash_disable_local_vmclear(cpu);
  2673. /*
  2674. * Read loaded_vmcs->cpu should be before fetching
  2675. * loaded_vmcs->loaded_vmcss_on_cpu_link.
  2676. * See the comments in __loaded_vmcs_clear().
  2677. */
  2678. smp_rmb();
  2679. list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
  2680. &per_cpu(loaded_vmcss_on_cpu, cpu));
  2681. crash_enable_local_vmclear(cpu);
  2682. local_irq_enable();
  2683. }
  2684. if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
  2685. per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
  2686. vmcs_load(vmx->loaded_vmcs->vmcs);
  2687. indirect_branch_prediction_barrier();
  2688. }
  2689. if (!already_loaded) {
  2690. void *gdt = get_current_gdt_ro();
  2691. unsigned long sysenter_esp;
  2692. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  2693. /*
  2694. * Linux uses per-cpu TSS and GDT, so set these when switching
  2695. * processors. See 22.2.4.
  2696. */
  2697. vmcs_writel(HOST_TR_BASE,
  2698. (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
  2699. vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
  2700. /*
  2701. * VM exits change the host TR limit to 0x67 after a VM
  2702. * exit. This is okay, since 0x67 covers everything except
  2703. * the IO bitmap and have have code to handle the IO bitmap
  2704. * being lost after a VM exit.
  2705. */
  2706. BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
  2707. rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
  2708. vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
  2709. vmx->loaded_vmcs->cpu = cpu;
  2710. }
  2711. /* Setup TSC multiplier */
  2712. if (kvm_has_tsc_control &&
  2713. vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
  2714. decache_tsc_multiplier(vmx);
  2715. vmx_vcpu_pi_load(vcpu, cpu);
  2716. vmx->host_pkru = read_pkru();
  2717. vmx->host_debugctlmsr = get_debugctlmsr();
  2718. }
  2719. static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
  2720. {
  2721. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  2722. if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
  2723. !irq_remapping_cap(IRQ_POSTING_CAP) ||
  2724. !kvm_vcpu_apicv_active(vcpu))
  2725. return;
  2726. /* Set SN when the vCPU is preempted */
  2727. if (vcpu->preempted)
  2728. pi_set_sn(pi_desc);
  2729. }
  2730. static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
  2731. {
  2732. vmx_vcpu_pi_put(vcpu);
  2733. vmx_prepare_switch_to_host(to_vmx(vcpu));
  2734. }
  2735. static bool emulation_required(struct kvm_vcpu *vcpu)
  2736. {
  2737. return emulate_invalid_guest_state && !guest_state_valid(vcpu);
  2738. }
  2739. static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
  2740. /*
  2741. * Return the cr0 value that a nested guest would read. This is a combination
  2742. * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
  2743. * its hypervisor (cr0_read_shadow).
  2744. */
  2745. static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
  2746. {
  2747. return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
  2748. (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
  2749. }
  2750. static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
  2751. {
  2752. return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
  2753. (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
  2754. }
  2755. static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
  2756. {
  2757. unsigned long rflags, save_rflags;
  2758. if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
  2759. __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
  2760. rflags = vmcs_readl(GUEST_RFLAGS);
  2761. if (to_vmx(vcpu)->rmode.vm86_active) {
  2762. rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
  2763. save_rflags = to_vmx(vcpu)->rmode.save_rflags;
  2764. rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
  2765. }
  2766. to_vmx(vcpu)->rflags = rflags;
  2767. }
  2768. return to_vmx(vcpu)->rflags;
  2769. }
  2770. static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  2771. {
  2772. unsigned long old_rflags = vmx_get_rflags(vcpu);
  2773. __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
  2774. to_vmx(vcpu)->rflags = rflags;
  2775. if (to_vmx(vcpu)->rmode.vm86_active) {
  2776. to_vmx(vcpu)->rmode.save_rflags = rflags;
  2777. rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
  2778. }
  2779. vmcs_writel(GUEST_RFLAGS, rflags);
  2780. if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
  2781. to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
  2782. }
  2783. static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
  2784. {
  2785. u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  2786. int ret = 0;
  2787. if (interruptibility & GUEST_INTR_STATE_STI)
  2788. ret |= KVM_X86_SHADOW_INT_STI;
  2789. if (interruptibility & GUEST_INTR_STATE_MOV_SS)
  2790. ret |= KVM_X86_SHADOW_INT_MOV_SS;
  2791. return ret;
  2792. }
  2793. static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
  2794. {
  2795. u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  2796. u32 interruptibility = interruptibility_old;
  2797. interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
  2798. if (mask & KVM_X86_SHADOW_INT_MOV_SS)
  2799. interruptibility |= GUEST_INTR_STATE_MOV_SS;
  2800. else if (mask & KVM_X86_SHADOW_INT_STI)
  2801. interruptibility |= GUEST_INTR_STATE_STI;
  2802. if ((interruptibility != interruptibility_old))
  2803. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
  2804. }
  2805. static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
  2806. {
  2807. unsigned long rip;
  2808. rip = kvm_rip_read(vcpu);
  2809. rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  2810. kvm_rip_write(vcpu, rip);
  2811. /* skipping an emulated instruction also counts */
  2812. vmx_set_interrupt_shadow(vcpu, 0);
  2813. }
  2814. static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
  2815. unsigned long exit_qual)
  2816. {
  2817. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  2818. unsigned int nr = vcpu->arch.exception.nr;
  2819. u32 intr_info = nr | INTR_INFO_VALID_MASK;
  2820. if (vcpu->arch.exception.has_error_code) {
  2821. vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
  2822. intr_info |= INTR_INFO_DELIVER_CODE_MASK;
  2823. }
  2824. if (kvm_exception_is_soft(nr))
  2825. intr_info |= INTR_TYPE_SOFT_EXCEPTION;
  2826. else
  2827. intr_info |= INTR_TYPE_HARD_EXCEPTION;
  2828. if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
  2829. vmx_get_nmi_mask(vcpu))
  2830. intr_info |= INTR_INFO_UNBLOCK_NMI;
  2831. nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
  2832. }
  2833. /*
  2834. * KVM wants to inject page-faults which it got to the guest. This function
  2835. * checks whether in a nested guest, we need to inject them to L1 or L2.
  2836. */
  2837. static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
  2838. {
  2839. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  2840. unsigned int nr = vcpu->arch.exception.nr;
  2841. bool has_payload = vcpu->arch.exception.has_payload;
  2842. unsigned long payload = vcpu->arch.exception.payload;
  2843. if (nr == PF_VECTOR) {
  2844. if (vcpu->arch.exception.nested_apf) {
  2845. *exit_qual = vcpu->arch.apf.nested_apf_token;
  2846. return 1;
  2847. }
  2848. if (nested_vmx_is_page_fault_vmexit(vmcs12,
  2849. vcpu->arch.exception.error_code)) {
  2850. *exit_qual = has_payload ? payload : vcpu->arch.cr2;
  2851. return 1;
  2852. }
  2853. } else if (vmcs12->exception_bitmap & (1u << nr)) {
  2854. if (nr == DB_VECTOR) {
  2855. if (!has_payload) {
  2856. payload = vcpu->arch.dr6;
  2857. payload &= ~(DR6_FIXED_1 | DR6_BT);
  2858. payload ^= DR6_RTM;
  2859. }
  2860. *exit_qual = payload;
  2861. } else
  2862. *exit_qual = 0;
  2863. return 1;
  2864. }
  2865. return 0;
  2866. }
  2867. static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
  2868. {
  2869. /*
  2870. * Ensure that we clear the HLT state in the VMCS. We don't need to
  2871. * explicitly skip the instruction because if the HLT state is set,
  2872. * then the instruction is already executing and RIP has already been
  2873. * advanced.
  2874. */
  2875. if (kvm_hlt_in_guest(vcpu->kvm) &&
  2876. vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
  2877. vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
  2878. }
  2879. static void vmx_queue_exception(struct kvm_vcpu *vcpu)
  2880. {
  2881. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2882. unsigned nr = vcpu->arch.exception.nr;
  2883. bool has_error_code = vcpu->arch.exception.has_error_code;
  2884. u32 error_code = vcpu->arch.exception.error_code;
  2885. u32 intr_info = nr | INTR_INFO_VALID_MASK;
  2886. kvm_deliver_exception_payload(vcpu);
  2887. if (has_error_code) {
  2888. vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
  2889. intr_info |= INTR_INFO_DELIVER_CODE_MASK;
  2890. }
  2891. if (vmx->rmode.vm86_active) {
  2892. int inc_eip = 0;
  2893. if (kvm_exception_is_soft(nr))
  2894. inc_eip = vcpu->arch.event_exit_inst_len;
  2895. if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
  2896. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  2897. return;
  2898. }
  2899. WARN_ON_ONCE(vmx->emulation_required);
  2900. if (kvm_exception_is_soft(nr)) {
  2901. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  2902. vmx->vcpu.arch.event_exit_inst_len);
  2903. intr_info |= INTR_TYPE_SOFT_EXCEPTION;
  2904. } else
  2905. intr_info |= INTR_TYPE_HARD_EXCEPTION;
  2906. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
  2907. vmx_clear_hlt(vcpu);
  2908. }
  2909. static bool vmx_rdtscp_supported(void)
  2910. {
  2911. return cpu_has_vmx_rdtscp();
  2912. }
  2913. static bool vmx_invpcid_supported(void)
  2914. {
  2915. return cpu_has_vmx_invpcid();
  2916. }
  2917. /*
  2918. * Swap MSR entry in host/guest MSR entry array.
  2919. */
  2920. static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
  2921. {
  2922. struct shared_msr_entry tmp;
  2923. tmp = vmx->guest_msrs[to];
  2924. vmx->guest_msrs[to] = vmx->guest_msrs[from];
  2925. vmx->guest_msrs[from] = tmp;
  2926. }
  2927. /*
  2928. * Set up the vmcs to automatically save and restore system
  2929. * msrs. Don't touch the 64-bit msrs if the guest is in legacy
  2930. * mode, as fiddling with msrs is very expensive.
  2931. */
  2932. static void setup_msrs(struct vcpu_vmx *vmx)
  2933. {
  2934. int save_nmsrs, index;
  2935. save_nmsrs = 0;
  2936. #ifdef CONFIG_X86_64
  2937. if (is_long_mode(&vmx->vcpu)) {
  2938. index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
  2939. if (index >= 0)
  2940. move_msr_up(vmx, index, save_nmsrs++);
  2941. index = __find_msr_index(vmx, MSR_LSTAR);
  2942. if (index >= 0)
  2943. move_msr_up(vmx, index, save_nmsrs++);
  2944. index = __find_msr_index(vmx, MSR_CSTAR);
  2945. if (index >= 0)
  2946. move_msr_up(vmx, index, save_nmsrs++);
  2947. index = __find_msr_index(vmx, MSR_TSC_AUX);
  2948. if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
  2949. move_msr_up(vmx, index, save_nmsrs++);
  2950. /*
  2951. * MSR_STAR is only needed on long mode guests, and only
  2952. * if efer.sce is enabled.
  2953. */
  2954. index = __find_msr_index(vmx, MSR_STAR);
  2955. if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
  2956. move_msr_up(vmx, index, save_nmsrs++);
  2957. }
  2958. #endif
  2959. index = __find_msr_index(vmx, MSR_EFER);
  2960. if (index >= 0 && update_transition_efer(vmx, index))
  2961. move_msr_up(vmx, index, save_nmsrs++);
  2962. vmx->save_nmsrs = save_nmsrs;
  2963. vmx->guest_msrs_dirty = true;
  2964. if (cpu_has_vmx_msr_bitmap())
  2965. vmx_update_msr_bitmap(&vmx->vcpu);
  2966. }
  2967. static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
  2968. {
  2969. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  2970. if (is_guest_mode(vcpu) &&
  2971. (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
  2972. return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
  2973. return vcpu->arch.tsc_offset;
  2974. }
  2975. static u64 vmx_write_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
  2976. {
  2977. u64 active_offset = offset;
  2978. if (is_guest_mode(vcpu)) {
  2979. /*
  2980. * We're here if L1 chose not to trap WRMSR to TSC. According
  2981. * to the spec, this should set L1's TSC; The offset that L1
  2982. * set for L2 remains unchanged, and still needs to be added
  2983. * to the newly set TSC to get L2's TSC.
  2984. */
  2985. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  2986. if (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING))
  2987. active_offset += vmcs12->tsc_offset;
  2988. } else {
  2989. trace_kvm_write_tsc_offset(vcpu->vcpu_id,
  2990. vmcs_read64(TSC_OFFSET), offset);
  2991. }
  2992. vmcs_write64(TSC_OFFSET, active_offset);
  2993. return active_offset;
  2994. }
  2995. /*
  2996. * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
  2997. * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
  2998. * all guests if the "nested" module option is off, and can also be disabled
  2999. * for a single guest by disabling its VMX cpuid bit.
  3000. */
  3001. static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
  3002. {
  3003. return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
  3004. }
  3005. /*
  3006. * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
  3007. * returned for the various VMX controls MSRs when nested VMX is enabled.
  3008. * The same values should also be used to verify that vmcs12 control fields are
  3009. * valid during nested entry from L1 to L2.
  3010. * Each of these control msrs has a low and high 32-bit half: A low bit is on
  3011. * if the corresponding bit in the (32-bit) control field *must* be on, and a
  3012. * bit in the high half is on if the corresponding bit in the control field
  3013. * may be on. See also vmx_control_verify().
  3014. */
  3015. static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
  3016. {
  3017. if (!nested) {
  3018. memset(msrs, 0, sizeof(*msrs));
  3019. return;
  3020. }
  3021. /*
  3022. * Note that as a general rule, the high half of the MSRs (bits in
  3023. * the control fields which may be 1) should be initialized by the
  3024. * intersection of the underlying hardware's MSR (i.e., features which
  3025. * can be supported) and the list of features we want to expose -
  3026. * because they are known to be properly supported in our code.
  3027. * Also, usually, the low half of the MSRs (bits which must be 1) can
  3028. * be set to 0, meaning that L1 may turn off any of these bits. The
  3029. * reason is that if one of these bits is necessary, it will appear
  3030. * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
  3031. * fields of vmcs01 and vmcs02, will turn these bits off - and
  3032. * nested_vmx_exit_reflected() will not pass related exits to L1.
  3033. * These rules have exceptions below.
  3034. */
  3035. /* pin-based controls */
  3036. rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
  3037. msrs->pinbased_ctls_low,
  3038. msrs->pinbased_ctls_high);
  3039. msrs->pinbased_ctls_low |=
  3040. PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  3041. msrs->pinbased_ctls_high &=
  3042. PIN_BASED_EXT_INTR_MASK |
  3043. PIN_BASED_NMI_EXITING |
  3044. PIN_BASED_VIRTUAL_NMIS |
  3045. (apicv ? PIN_BASED_POSTED_INTR : 0);
  3046. msrs->pinbased_ctls_high |=
  3047. PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
  3048. PIN_BASED_VMX_PREEMPTION_TIMER;
  3049. /* exit controls */
  3050. rdmsr(MSR_IA32_VMX_EXIT_CTLS,
  3051. msrs->exit_ctls_low,
  3052. msrs->exit_ctls_high);
  3053. msrs->exit_ctls_low =
  3054. VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
  3055. msrs->exit_ctls_high &=
  3056. #ifdef CONFIG_X86_64
  3057. VM_EXIT_HOST_ADDR_SPACE_SIZE |
  3058. #endif
  3059. VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
  3060. msrs->exit_ctls_high |=
  3061. VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
  3062. VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
  3063. VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
  3064. /* We support free control of debug control saving. */
  3065. msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
  3066. /* entry controls */
  3067. rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
  3068. msrs->entry_ctls_low,
  3069. msrs->entry_ctls_high);
  3070. msrs->entry_ctls_low =
  3071. VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
  3072. msrs->entry_ctls_high &=
  3073. #ifdef CONFIG_X86_64
  3074. VM_ENTRY_IA32E_MODE |
  3075. #endif
  3076. VM_ENTRY_LOAD_IA32_PAT;
  3077. msrs->entry_ctls_high |=
  3078. (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
  3079. /* We support free control of debug control loading. */
  3080. msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
  3081. /* cpu-based controls */
  3082. rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
  3083. msrs->procbased_ctls_low,
  3084. msrs->procbased_ctls_high);
  3085. msrs->procbased_ctls_low =
  3086. CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  3087. msrs->procbased_ctls_high &=
  3088. CPU_BASED_VIRTUAL_INTR_PENDING |
  3089. CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
  3090. CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
  3091. CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
  3092. CPU_BASED_CR3_STORE_EXITING |
  3093. #ifdef CONFIG_X86_64
  3094. CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
  3095. #endif
  3096. CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
  3097. CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
  3098. CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
  3099. CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
  3100. CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  3101. /*
  3102. * We can allow some features even when not supported by the
  3103. * hardware. For example, L1 can specify an MSR bitmap - and we
  3104. * can use it to avoid exits to L1 - even when L0 runs L2
  3105. * without MSR bitmaps.
  3106. */
  3107. msrs->procbased_ctls_high |=
  3108. CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
  3109. CPU_BASED_USE_MSR_BITMAPS;
  3110. /* We support free control of CR3 access interception. */
  3111. msrs->procbased_ctls_low &=
  3112. ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
  3113. /*
  3114. * secondary cpu-based controls. Do not include those that
  3115. * depend on CPUID bits, they are added later by vmx_cpuid_update.
  3116. */
  3117. rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
  3118. msrs->secondary_ctls_low,
  3119. msrs->secondary_ctls_high);
  3120. msrs->secondary_ctls_low = 0;
  3121. msrs->secondary_ctls_high &=
  3122. SECONDARY_EXEC_DESC |
  3123. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  3124. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  3125. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
  3126. SECONDARY_EXEC_WBINVD_EXITING;
  3127. /*
  3128. * We can emulate "VMCS shadowing," even if the hardware
  3129. * doesn't support it.
  3130. */
  3131. msrs->secondary_ctls_high |=
  3132. SECONDARY_EXEC_SHADOW_VMCS;
  3133. if (enable_ept) {
  3134. /* nested EPT: emulate EPT also to L1 */
  3135. msrs->secondary_ctls_high |=
  3136. SECONDARY_EXEC_ENABLE_EPT;
  3137. msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
  3138. VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
  3139. if (cpu_has_vmx_ept_execute_only())
  3140. msrs->ept_caps |=
  3141. VMX_EPT_EXECUTE_ONLY_BIT;
  3142. msrs->ept_caps &= vmx_capability.ept;
  3143. msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
  3144. VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
  3145. VMX_EPT_1GB_PAGE_BIT;
  3146. if (enable_ept_ad_bits) {
  3147. msrs->secondary_ctls_high |=
  3148. SECONDARY_EXEC_ENABLE_PML;
  3149. msrs->ept_caps |= VMX_EPT_AD_BIT;
  3150. }
  3151. }
  3152. if (cpu_has_vmx_vmfunc()) {
  3153. msrs->secondary_ctls_high |=
  3154. SECONDARY_EXEC_ENABLE_VMFUNC;
  3155. /*
  3156. * Advertise EPTP switching unconditionally
  3157. * since we emulate it
  3158. */
  3159. if (enable_ept)
  3160. msrs->vmfunc_controls =
  3161. VMX_VMFUNC_EPTP_SWITCHING;
  3162. }
  3163. /*
  3164. * Old versions of KVM use the single-context version without
  3165. * checking for support, so declare that it is supported even
  3166. * though it is treated as global context. The alternative is
  3167. * not failing the single-context invvpid, and it is worse.
  3168. */
  3169. if (enable_vpid) {
  3170. msrs->secondary_ctls_high |=
  3171. SECONDARY_EXEC_ENABLE_VPID;
  3172. msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
  3173. VMX_VPID_EXTENT_SUPPORTED_MASK;
  3174. }
  3175. if (enable_unrestricted_guest)
  3176. msrs->secondary_ctls_high |=
  3177. SECONDARY_EXEC_UNRESTRICTED_GUEST;
  3178. if (flexpriority_enabled)
  3179. msrs->secondary_ctls_high |=
  3180. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  3181. /* miscellaneous data */
  3182. rdmsr(MSR_IA32_VMX_MISC,
  3183. msrs->misc_low,
  3184. msrs->misc_high);
  3185. msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
  3186. msrs->misc_low |=
  3187. MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
  3188. VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
  3189. VMX_MISC_ACTIVITY_HLT;
  3190. msrs->misc_high = 0;
  3191. /*
  3192. * This MSR reports some information about VMX support. We
  3193. * should return information about the VMX we emulate for the
  3194. * guest, and the VMCS structure we give it - not about the
  3195. * VMX support of the underlying hardware.
  3196. */
  3197. msrs->basic =
  3198. VMCS12_REVISION |
  3199. VMX_BASIC_TRUE_CTLS |
  3200. ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
  3201. (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
  3202. if (cpu_has_vmx_basic_inout())
  3203. msrs->basic |= VMX_BASIC_INOUT;
  3204. /*
  3205. * These MSRs specify bits which the guest must keep fixed on
  3206. * while L1 is in VMXON mode (in L1's root mode, or running an L2).
  3207. * We picked the standard core2 setting.
  3208. */
  3209. #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
  3210. #define VMXON_CR4_ALWAYSON X86_CR4_VMXE
  3211. msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
  3212. msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
  3213. /* These MSRs specify bits which the guest must keep fixed off. */
  3214. rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
  3215. rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
  3216. /* highest index: VMX_PREEMPTION_TIMER_VALUE */
  3217. msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
  3218. }
  3219. /*
  3220. * if fixed0[i] == 1: val[i] must be 1
  3221. * if fixed1[i] == 0: val[i] must be 0
  3222. */
  3223. static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
  3224. {
  3225. return ((val & fixed1) | fixed0) == val;
  3226. }
  3227. static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
  3228. {
  3229. return fixed_bits_valid(control, low, high);
  3230. }
  3231. static inline u64 vmx_control_msr(u32 low, u32 high)
  3232. {
  3233. return low | ((u64)high << 32);
  3234. }
  3235. static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
  3236. {
  3237. superset &= mask;
  3238. subset &= mask;
  3239. return (superset | subset) == superset;
  3240. }
  3241. static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
  3242. {
  3243. const u64 feature_and_reserved =
  3244. /* feature (except bit 48; see below) */
  3245. BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
  3246. /* reserved */
  3247. BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
  3248. u64 vmx_basic = vmx->nested.msrs.basic;
  3249. if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
  3250. return -EINVAL;
  3251. /*
  3252. * KVM does not emulate a version of VMX that constrains physical
  3253. * addresses of VMX structures (e.g. VMCS) to 32-bits.
  3254. */
  3255. if (data & BIT_ULL(48))
  3256. return -EINVAL;
  3257. if (vmx_basic_vmcs_revision_id(vmx_basic) !=
  3258. vmx_basic_vmcs_revision_id(data))
  3259. return -EINVAL;
  3260. if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
  3261. return -EINVAL;
  3262. vmx->nested.msrs.basic = data;
  3263. return 0;
  3264. }
  3265. static int
  3266. vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
  3267. {
  3268. u64 supported;
  3269. u32 *lowp, *highp;
  3270. switch (msr_index) {
  3271. case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
  3272. lowp = &vmx->nested.msrs.pinbased_ctls_low;
  3273. highp = &vmx->nested.msrs.pinbased_ctls_high;
  3274. break;
  3275. case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
  3276. lowp = &vmx->nested.msrs.procbased_ctls_low;
  3277. highp = &vmx->nested.msrs.procbased_ctls_high;
  3278. break;
  3279. case MSR_IA32_VMX_TRUE_EXIT_CTLS:
  3280. lowp = &vmx->nested.msrs.exit_ctls_low;
  3281. highp = &vmx->nested.msrs.exit_ctls_high;
  3282. break;
  3283. case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
  3284. lowp = &vmx->nested.msrs.entry_ctls_low;
  3285. highp = &vmx->nested.msrs.entry_ctls_high;
  3286. break;
  3287. case MSR_IA32_VMX_PROCBASED_CTLS2:
  3288. lowp = &vmx->nested.msrs.secondary_ctls_low;
  3289. highp = &vmx->nested.msrs.secondary_ctls_high;
  3290. break;
  3291. default:
  3292. BUG();
  3293. }
  3294. supported = vmx_control_msr(*lowp, *highp);
  3295. /* Check must-be-1 bits are still 1. */
  3296. if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
  3297. return -EINVAL;
  3298. /* Check must-be-0 bits are still 0. */
  3299. if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
  3300. return -EINVAL;
  3301. *lowp = data;
  3302. *highp = data >> 32;
  3303. return 0;
  3304. }
  3305. static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
  3306. {
  3307. const u64 feature_and_reserved_bits =
  3308. /* feature */
  3309. BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
  3310. BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
  3311. /* reserved */
  3312. GENMASK_ULL(13, 9) | BIT_ULL(31);
  3313. u64 vmx_misc;
  3314. vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
  3315. vmx->nested.msrs.misc_high);
  3316. if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
  3317. return -EINVAL;
  3318. if ((vmx->nested.msrs.pinbased_ctls_high &
  3319. PIN_BASED_VMX_PREEMPTION_TIMER) &&
  3320. vmx_misc_preemption_timer_rate(data) !=
  3321. vmx_misc_preemption_timer_rate(vmx_misc))
  3322. return -EINVAL;
  3323. if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
  3324. return -EINVAL;
  3325. if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
  3326. return -EINVAL;
  3327. if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
  3328. return -EINVAL;
  3329. vmx->nested.msrs.misc_low = data;
  3330. vmx->nested.msrs.misc_high = data >> 32;
  3331. /*
  3332. * If L1 has read-only VM-exit information fields, use the
  3333. * less permissive vmx_vmwrite_bitmap to specify write
  3334. * permissions for the shadow VMCS.
  3335. */
  3336. if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
  3337. vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
  3338. return 0;
  3339. }
  3340. static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
  3341. {
  3342. u64 vmx_ept_vpid_cap;
  3343. vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
  3344. vmx->nested.msrs.vpid_caps);
  3345. /* Every bit is either reserved or a feature bit. */
  3346. if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
  3347. return -EINVAL;
  3348. vmx->nested.msrs.ept_caps = data;
  3349. vmx->nested.msrs.vpid_caps = data >> 32;
  3350. return 0;
  3351. }
  3352. static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
  3353. {
  3354. u64 *msr;
  3355. switch (msr_index) {
  3356. case MSR_IA32_VMX_CR0_FIXED0:
  3357. msr = &vmx->nested.msrs.cr0_fixed0;
  3358. break;
  3359. case MSR_IA32_VMX_CR4_FIXED0:
  3360. msr = &vmx->nested.msrs.cr4_fixed0;
  3361. break;
  3362. default:
  3363. BUG();
  3364. }
  3365. /*
  3366. * 1 bits (which indicates bits which "must-be-1" during VMX operation)
  3367. * must be 1 in the restored value.
  3368. */
  3369. if (!is_bitwise_subset(data, *msr, -1ULL))
  3370. return -EINVAL;
  3371. *msr = data;
  3372. return 0;
  3373. }
  3374. /*
  3375. * Called when userspace is restoring VMX MSRs.
  3376. *
  3377. * Returns 0 on success, non-0 otherwise.
  3378. */
  3379. static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
  3380. {
  3381. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3382. /*
  3383. * Don't allow changes to the VMX capability MSRs while the vCPU
  3384. * is in VMX operation.
  3385. */
  3386. if (vmx->nested.vmxon)
  3387. return -EBUSY;
  3388. switch (msr_index) {
  3389. case MSR_IA32_VMX_BASIC:
  3390. return vmx_restore_vmx_basic(vmx, data);
  3391. case MSR_IA32_VMX_PINBASED_CTLS:
  3392. case MSR_IA32_VMX_PROCBASED_CTLS:
  3393. case MSR_IA32_VMX_EXIT_CTLS:
  3394. case MSR_IA32_VMX_ENTRY_CTLS:
  3395. /*
  3396. * The "non-true" VMX capability MSRs are generated from the
  3397. * "true" MSRs, so we do not support restoring them directly.
  3398. *
  3399. * If userspace wants to emulate VMX_BASIC[55]=0, userspace
  3400. * should restore the "true" MSRs with the must-be-1 bits
  3401. * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
  3402. * DEFAULT SETTINGS".
  3403. */
  3404. return -EINVAL;
  3405. case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
  3406. case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
  3407. case MSR_IA32_VMX_TRUE_EXIT_CTLS:
  3408. case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
  3409. case MSR_IA32_VMX_PROCBASED_CTLS2:
  3410. return vmx_restore_control_msr(vmx, msr_index, data);
  3411. case MSR_IA32_VMX_MISC:
  3412. return vmx_restore_vmx_misc(vmx, data);
  3413. case MSR_IA32_VMX_CR0_FIXED0:
  3414. case MSR_IA32_VMX_CR4_FIXED0:
  3415. return vmx_restore_fixed0_msr(vmx, msr_index, data);
  3416. case MSR_IA32_VMX_CR0_FIXED1:
  3417. case MSR_IA32_VMX_CR4_FIXED1:
  3418. /*
  3419. * These MSRs are generated based on the vCPU's CPUID, so we
  3420. * do not support restoring them directly.
  3421. */
  3422. return -EINVAL;
  3423. case MSR_IA32_VMX_EPT_VPID_CAP:
  3424. return vmx_restore_vmx_ept_vpid_cap(vmx, data);
  3425. case MSR_IA32_VMX_VMCS_ENUM:
  3426. vmx->nested.msrs.vmcs_enum = data;
  3427. return 0;
  3428. default:
  3429. /*
  3430. * The rest of the VMX capability MSRs do not support restore.
  3431. */
  3432. return -EINVAL;
  3433. }
  3434. }
  3435. /* Returns 0 on success, non-0 otherwise. */
  3436. static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
  3437. {
  3438. switch (msr_index) {
  3439. case MSR_IA32_VMX_BASIC:
  3440. *pdata = msrs->basic;
  3441. break;
  3442. case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
  3443. case MSR_IA32_VMX_PINBASED_CTLS:
  3444. *pdata = vmx_control_msr(
  3445. msrs->pinbased_ctls_low,
  3446. msrs->pinbased_ctls_high);
  3447. if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
  3448. *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  3449. break;
  3450. case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
  3451. case MSR_IA32_VMX_PROCBASED_CTLS:
  3452. *pdata = vmx_control_msr(
  3453. msrs->procbased_ctls_low,
  3454. msrs->procbased_ctls_high);
  3455. if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
  3456. *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  3457. break;
  3458. case MSR_IA32_VMX_TRUE_EXIT_CTLS:
  3459. case MSR_IA32_VMX_EXIT_CTLS:
  3460. *pdata = vmx_control_msr(
  3461. msrs->exit_ctls_low,
  3462. msrs->exit_ctls_high);
  3463. if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
  3464. *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
  3465. break;
  3466. case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
  3467. case MSR_IA32_VMX_ENTRY_CTLS:
  3468. *pdata = vmx_control_msr(
  3469. msrs->entry_ctls_low,
  3470. msrs->entry_ctls_high);
  3471. if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
  3472. *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
  3473. break;
  3474. case MSR_IA32_VMX_MISC:
  3475. *pdata = vmx_control_msr(
  3476. msrs->misc_low,
  3477. msrs->misc_high);
  3478. break;
  3479. case MSR_IA32_VMX_CR0_FIXED0:
  3480. *pdata = msrs->cr0_fixed0;
  3481. break;
  3482. case MSR_IA32_VMX_CR0_FIXED1:
  3483. *pdata = msrs->cr0_fixed1;
  3484. break;
  3485. case MSR_IA32_VMX_CR4_FIXED0:
  3486. *pdata = msrs->cr4_fixed0;
  3487. break;
  3488. case MSR_IA32_VMX_CR4_FIXED1:
  3489. *pdata = msrs->cr4_fixed1;
  3490. break;
  3491. case MSR_IA32_VMX_VMCS_ENUM:
  3492. *pdata = msrs->vmcs_enum;
  3493. break;
  3494. case MSR_IA32_VMX_PROCBASED_CTLS2:
  3495. *pdata = vmx_control_msr(
  3496. msrs->secondary_ctls_low,
  3497. msrs->secondary_ctls_high);
  3498. break;
  3499. case MSR_IA32_VMX_EPT_VPID_CAP:
  3500. *pdata = msrs->ept_caps |
  3501. ((u64)msrs->vpid_caps << 32);
  3502. break;
  3503. case MSR_IA32_VMX_VMFUNC:
  3504. *pdata = msrs->vmfunc_controls;
  3505. break;
  3506. default:
  3507. return 1;
  3508. }
  3509. return 0;
  3510. }
  3511. static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
  3512. uint64_t val)
  3513. {
  3514. uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
  3515. return !(val & ~valid_bits);
  3516. }
  3517. static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
  3518. {
  3519. switch (msr->index) {
  3520. case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
  3521. if (!nested)
  3522. return 1;
  3523. return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
  3524. default:
  3525. return 1;
  3526. }
  3527. return 0;
  3528. }
  3529. /*
  3530. * Reads an msr value (of 'msr_index') into 'pdata'.
  3531. * Returns 0 on success, non-0 otherwise.
  3532. * Assumes vcpu_load() was already called.
  3533. */
  3534. static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  3535. {
  3536. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3537. struct shared_msr_entry *msr;
  3538. switch (msr_info->index) {
  3539. #ifdef CONFIG_X86_64
  3540. case MSR_FS_BASE:
  3541. msr_info->data = vmcs_readl(GUEST_FS_BASE);
  3542. break;
  3543. case MSR_GS_BASE:
  3544. msr_info->data = vmcs_readl(GUEST_GS_BASE);
  3545. break;
  3546. case MSR_KERNEL_GS_BASE:
  3547. msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
  3548. break;
  3549. #endif
  3550. case MSR_EFER:
  3551. return kvm_get_msr_common(vcpu, msr_info);
  3552. case MSR_IA32_SPEC_CTRL:
  3553. if (!msr_info->host_initiated &&
  3554. !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
  3555. return 1;
  3556. msr_info->data = to_vmx(vcpu)->spec_ctrl;
  3557. break;
  3558. case MSR_IA32_ARCH_CAPABILITIES:
  3559. if (!msr_info->host_initiated &&
  3560. !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
  3561. return 1;
  3562. msr_info->data = to_vmx(vcpu)->arch_capabilities;
  3563. break;
  3564. case MSR_IA32_SYSENTER_CS:
  3565. msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
  3566. break;
  3567. case MSR_IA32_SYSENTER_EIP:
  3568. msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
  3569. break;
  3570. case MSR_IA32_SYSENTER_ESP:
  3571. msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
  3572. break;
  3573. case MSR_IA32_BNDCFGS:
  3574. if (!kvm_mpx_supported() ||
  3575. (!msr_info->host_initiated &&
  3576. !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
  3577. return 1;
  3578. msr_info->data = vmcs_read64(GUEST_BNDCFGS);
  3579. break;
  3580. case MSR_IA32_MCG_EXT_CTL:
  3581. if (!msr_info->host_initiated &&
  3582. !(vmx->msr_ia32_feature_control &
  3583. FEATURE_CONTROL_LMCE))
  3584. return 1;
  3585. msr_info->data = vcpu->arch.mcg_ext_ctl;
  3586. break;
  3587. case MSR_IA32_FEATURE_CONTROL:
  3588. msr_info->data = vmx->msr_ia32_feature_control;
  3589. break;
  3590. case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
  3591. if (!nested_vmx_allowed(vcpu))
  3592. return 1;
  3593. return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
  3594. &msr_info->data);
  3595. case MSR_IA32_XSS:
  3596. if (!vmx_xsaves_supported())
  3597. return 1;
  3598. msr_info->data = vcpu->arch.ia32_xss;
  3599. break;
  3600. case MSR_TSC_AUX:
  3601. if (!msr_info->host_initiated &&
  3602. !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
  3603. return 1;
  3604. /* Otherwise falls through */
  3605. default:
  3606. msr = find_msr_entry(vmx, msr_info->index);
  3607. if (msr) {
  3608. msr_info->data = msr->data;
  3609. break;
  3610. }
  3611. return kvm_get_msr_common(vcpu, msr_info);
  3612. }
  3613. return 0;
  3614. }
  3615. static void vmx_leave_nested(struct kvm_vcpu *vcpu);
  3616. /*
  3617. * Writes msr value into into the appropriate "register".
  3618. * Returns 0 on success, non-0 otherwise.
  3619. * Assumes vcpu_load() was already called.
  3620. */
  3621. static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  3622. {
  3623. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3624. struct shared_msr_entry *msr;
  3625. int ret = 0;
  3626. u32 msr_index = msr_info->index;
  3627. u64 data = msr_info->data;
  3628. switch (msr_index) {
  3629. case MSR_EFER:
  3630. ret = kvm_set_msr_common(vcpu, msr_info);
  3631. break;
  3632. #ifdef CONFIG_X86_64
  3633. case MSR_FS_BASE:
  3634. vmx_segment_cache_clear(vmx);
  3635. vmcs_writel(GUEST_FS_BASE, data);
  3636. break;
  3637. case MSR_GS_BASE:
  3638. vmx_segment_cache_clear(vmx);
  3639. vmcs_writel(GUEST_GS_BASE, data);
  3640. break;
  3641. case MSR_KERNEL_GS_BASE:
  3642. vmx_write_guest_kernel_gs_base(vmx, data);
  3643. break;
  3644. #endif
  3645. case MSR_IA32_SYSENTER_CS:
  3646. vmcs_write32(GUEST_SYSENTER_CS, data);
  3647. break;
  3648. case MSR_IA32_SYSENTER_EIP:
  3649. vmcs_writel(GUEST_SYSENTER_EIP, data);
  3650. break;
  3651. case MSR_IA32_SYSENTER_ESP:
  3652. vmcs_writel(GUEST_SYSENTER_ESP, data);
  3653. break;
  3654. case MSR_IA32_BNDCFGS:
  3655. if (!kvm_mpx_supported() ||
  3656. (!msr_info->host_initiated &&
  3657. !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
  3658. return 1;
  3659. if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
  3660. (data & MSR_IA32_BNDCFGS_RSVD))
  3661. return 1;
  3662. vmcs_write64(GUEST_BNDCFGS, data);
  3663. break;
  3664. case MSR_IA32_SPEC_CTRL:
  3665. if (!msr_info->host_initiated &&
  3666. !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
  3667. return 1;
  3668. /* The STIBP bit doesn't fault even if it's not advertised */
  3669. if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
  3670. return 1;
  3671. vmx->spec_ctrl = data;
  3672. if (!data)
  3673. break;
  3674. /*
  3675. * For non-nested:
  3676. * When it's written (to non-zero) for the first time, pass
  3677. * it through.
  3678. *
  3679. * For nested:
  3680. * The handling of the MSR bitmap for L2 guests is done in
  3681. * nested_vmx_merge_msr_bitmap. We should not touch the
  3682. * vmcs02.msr_bitmap here since it gets completely overwritten
  3683. * in the merging. We update the vmcs01 here for L1 as well
  3684. * since it will end up touching the MSR anyway now.
  3685. */
  3686. vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
  3687. MSR_IA32_SPEC_CTRL,
  3688. MSR_TYPE_RW);
  3689. break;
  3690. case MSR_IA32_PRED_CMD:
  3691. if (!msr_info->host_initiated &&
  3692. !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
  3693. return 1;
  3694. if (data & ~PRED_CMD_IBPB)
  3695. return 1;
  3696. if (!data)
  3697. break;
  3698. wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
  3699. /*
  3700. * For non-nested:
  3701. * When it's written (to non-zero) for the first time, pass
  3702. * it through.
  3703. *
  3704. * For nested:
  3705. * The handling of the MSR bitmap for L2 guests is done in
  3706. * nested_vmx_merge_msr_bitmap. We should not touch the
  3707. * vmcs02.msr_bitmap here since it gets completely overwritten
  3708. * in the merging.
  3709. */
  3710. vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
  3711. MSR_TYPE_W);
  3712. break;
  3713. case MSR_IA32_ARCH_CAPABILITIES:
  3714. if (!msr_info->host_initiated)
  3715. return 1;
  3716. vmx->arch_capabilities = data;
  3717. break;
  3718. case MSR_IA32_CR_PAT:
  3719. if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
  3720. if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
  3721. return 1;
  3722. vmcs_write64(GUEST_IA32_PAT, data);
  3723. vcpu->arch.pat = data;
  3724. break;
  3725. }
  3726. ret = kvm_set_msr_common(vcpu, msr_info);
  3727. break;
  3728. case MSR_IA32_TSC_ADJUST:
  3729. ret = kvm_set_msr_common(vcpu, msr_info);
  3730. break;
  3731. case MSR_IA32_MCG_EXT_CTL:
  3732. if ((!msr_info->host_initiated &&
  3733. !(to_vmx(vcpu)->msr_ia32_feature_control &
  3734. FEATURE_CONTROL_LMCE)) ||
  3735. (data & ~MCG_EXT_CTL_LMCE_EN))
  3736. return 1;
  3737. vcpu->arch.mcg_ext_ctl = data;
  3738. break;
  3739. case MSR_IA32_FEATURE_CONTROL:
  3740. if (!vmx_feature_control_msr_valid(vcpu, data) ||
  3741. (to_vmx(vcpu)->msr_ia32_feature_control &
  3742. FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
  3743. return 1;
  3744. vmx->msr_ia32_feature_control = data;
  3745. if (msr_info->host_initiated && data == 0)
  3746. vmx_leave_nested(vcpu);
  3747. break;
  3748. case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
  3749. if (!msr_info->host_initiated)
  3750. return 1; /* they are read-only */
  3751. if (!nested_vmx_allowed(vcpu))
  3752. return 1;
  3753. return vmx_set_vmx_msr(vcpu, msr_index, data);
  3754. case MSR_IA32_XSS:
  3755. if (!vmx_xsaves_supported())
  3756. return 1;
  3757. /*
  3758. * The only supported bit as of Skylake is bit 8, but
  3759. * it is not supported on KVM.
  3760. */
  3761. if (data != 0)
  3762. return 1;
  3763. vcpu->arch.ia32_xss = data;
  3764. if (vcpu->arch.ia32_xss != host_xss)
  3765. add_atomic_switch_msr(vmx, MSR_IA32_XSS,
  3766. vcpu->arch.ia32_xss, host_xss, false);
  3767. else
  3768. clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
  3769. break;
  3770. case MSR_TSC_AUX:
  3771. if (!msr_info->host_initiated &&
  3772. !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
  3773. return 1;
  3774. /* Check reserved bit, higher 32 bits should be zero */
  3775. if ((data >> 32) != 0)
  3776. return 1;
  3777. /* Otherwise falls through */
  3778. default:
  3779. msr = find_msr_entry(vmx, msr_index);
  3780. if (msr) {
  3781. u64 old_msr_data = msr->data;
  3782. msr->data = data;
  3783. if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
  3784. preempt_disable();
  3785. ret = kvm_set_shared_msr(msr->index, msr->data,
  3786. msr->mask);
  3787. preempt_enable();
  3788. if (ret)
  3789. msr->data = old_msr_data;
  3790. }
  3791. break;
  3792. }
  3793. ret = kvm_set_msr_common(vcpu, msr_info);
  3794. }
  3795. return ret;
  3796. }
  3797. static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
  3798. {
  3799. __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
  3800. switch (reg) {
  3801. case VCPU_REGS_RSP:
  3802. vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
  3803. break;
  3804. case VCPU_REGS_RIP:
  3805. vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
  3806. break;
  3807. case VCPU_EXREG_PDPTR:
  3808. if (enable_ept)
  3809. ept_save_pdptrs(vcpu);
  3810. break;
  3811. default:
  3812. break;
  3813. }
  3814. }
  3815. static __init int cpu_has_kvm_support(void)
  3816. {
  3817. return cpu_has_vmx();
  3818. }
  3819. static __init int vmx_disabled_by_bios(void)
  3820. {
  3821. u64 msr;
  3822. rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
  3823. if (msr & FEATURE_CONTROL_LOCKED) {
  3824. /* launched w/ TXT and VMX disabled */
  3825. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
  3826. && tboot_enabled())
  3827. return 1;
  3828. /* launched w/o TXT and VMX only enabled w/ TXT */
  3829. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
  3830. && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
  3831. && !tboot_enabled()) {
  3832. printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
  3833. "activate TXT before enabling KVM\n");
  3834. return 1;
  3835. }
  3836. /* launched w/o TXT and VMX disabled */
  3837. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
  3838. && !tboot_enabled())
  3839. return 1;
  3840. }
  3841. return 0;
  3842. }
  3843. static void kvm_cpu_vmxon(u64 addr)
  3844. {
  3845. cr4_set_bits(X86_CR4_VMXE);
  3846. intel_pt_handle_vmx(1);
  3847. asm volatile ("vmxon %0" : : "m"(addr));
  3848. }
  3849. static int hardware_enable(void)
  3850. {
  3851. int cpu = raw_smp_processor_id();
  3852. u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
  3853. u64 old, test_bits;
  3854. if (cr4_read_shadow() & X86_CR4_VMXE)
  3855. return -EBUSY;
  3856. /*
  3857. * This can happen if we hot-added a CPU but failed to allocate
  3858. * VP assist page for it.
  3859. */
  3860. if (static_branch_unlikely(&enable_evmcs) &&
  3861. !hv_get_vp_assist_page(cpu))
  3862. return -EFAULT;
  3863. INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
  3864. INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
  3865. spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
  3866. /*
  3867. * Now we can enable the vmclear operation in kdump
  3868. * since the loaded_vmcss_on_cpu list on this cpu
  3869. * has been initialized.
  3870. *
  3871. * Though the cpu is not in VMX operation now, there
  3872. * is no problem to enable the vmclear operation
  3873. * for the loaded_vmcss_on_cpu list is empty!
  3874. */
  3875. crash_enable_local_vmclear(cpu);
  3876. rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
  3877. test_bits = FEATURE_CONTROL_LOCKED;
  3878. test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  3879. if (tboot_enabled())
  3880. test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
  3881. if ((old & test_bits) != test_bits) {
  3882. /* enable and lock */
  3883. wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
  3884. }
  3885. kvm_cpu_vmxon(phys_addr);
  3886. if (enable_ept)
  3887. ept_sync_global();
  3888. return 0;
  3889. }
  3890. static void vmclear_local_loaded_vmcss(void)
  3891. {
  3892. int cpu = raw_smp_processor_id();
  3893. struct loaded_vmcs *v, *n;
  3894. list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
  3895. loaded_vmcss_on_cpu_link)
  3896. __loaded_vmcs_clear(v);
  3897. }
  3898. /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
  3899. * tricks.
  3900. */
  3901. static void kvm_cpu_vmxoff(void)
  3902. {
  3903. asm volatile (__ex("vmxoff"));
  3904. intel_pt_handle_vmx(0);
  3905. cr4_clear_bits(X86_CR4_VMXE);
  3906. }
  3907. static void hardware_disable(void)
  3908. {
  3909. vmclear_local_loaded_vmcss();
  3910. kvm_cpu_vmxoff();
  3911. }
  3912. static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
  3913. u32 msr, u32 *result)
  3914. {
  3915. u32 vmx_msr_low, vmx_msr_high;
  3916. u32 ctl = ctl_min | ctl_opt;
  3917. rdmsr(msr, vmx_msr_low, vmx_msr_high);
  3918. ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
  3919. ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
  3920. /* Ensure minimum (required) set of control bits are supported. */
  3921. if (ctl_min & ~ctl)
  3922. return -EIO;
  3923. *result = ctl;
  3924. return 0;
  3925. }
  3926. static __init bool allow_1_setting(u32 msr, u32 ctl)
  3927. {
  3928. u32 vmx_msr_low, vmx_msr_high;
  3929. rdmsr(msr, vmx_msr_low, vmx_msr_high);
  3930. return vmx_msr_high & ctl;
  3931. }
  3932. static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
  3933. {
  3934. u32 vmx_msr_low, vmx_msr_high;
  3935. u32 min, opt, min2, opt2;
  3936. u32 _pin_based_exec_control = 0;
  3937. u32 _cpu_based_exec_control = 0;
  3938. u32 _cpu_based_2nd_exec_control = 0;
  3939. u32 _vmexit_control = 0;
  3940. u32 _vmentry_control = 0;
  3941. memset(vmcs_conf, 0, sizeof(*vmcs_conf));
  3942. min = CPU_BASED_HLT_EXITING |
  3943. #ifdef CONFIG_X86_64
  3944. CPU_BASED_CR8_LOAD_EXITING |
  3945. CPU_BASED_CR8_STORE_EXITING |
  3946. #endif
  3947. CPU_BASED_CR3_LOAD_EXITING |
  3948. CPU_BASED_CR3_STORE_EXITING |
  3949. CPU_BASED_UNCOND_IO_EXITING |
  3950. CPU_BASED_MOV_DR_EXITING |
  3951. CPU_BASED_USE_TSC_OFFSETING |
  3952. CPU_BASED_MWAIT_EXITING |
  3953. CPU_BASED_MONITOR_EXITING |
  3954. CPU_BASED_INVLPG_EXITING |
  3955. CPU_BASED_RDPMC_EXITING;
  3956. opt = CPU_BASED_TPR_SHADOW |
  3957. CPU_BASED_USE_MSR_BITMAPS |
  3958. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  3959. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
  3960. &_cpu_based_exec_control) < 0)
  3961. return -EIO;
  3962. #ifdef CONFIG_X86_64
  3963. if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
  3964. _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
  3965. ~CPU_BASED_CR8_STORE_EXITING;
  3966. #endif
  3967. if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
  3968. min2 = 0;
  3969. opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  3970. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  3971. SECONDARY_EXEC_WBINVD_EXITING |
  3972. SECONDARY_EXEC_ENABLE_VPID |
  3973. SECONDARY_EXEC_ENABLE_EPT |
  3974. SECONDARY_EXEC_UNRESTRICTED_GUEST |
  3975. SECONDARY_EXEC_PAUSE_LOOP_EXITING |
  3976. SECONDARY_EXEC_DESC |
  3977. SECONDARY_EXEC_RDTSCP |
  3978. SECONDARY_EXEC_ENABLE_INVPCID |
  3979. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  3980. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
  3981. SECONDARY_EXEC_SHADOW_VMCS |
  3982. SECONDARY_EXEC_XSAVES |
  3983. SECONDARY_EXEC_RDSEED_EXITING |
  3984. SECONDARY_EXEC_RDRAND_EXITING |
  3985. SECONDARY_EXEC_ENABLE_PML |
  3986. SECONDARY_EXEC_TSC_SCALING |
  3987. SECONDARY_EXEC_ENABLE_VMFUNC |
  3988. SECONDARY_EXEC_ENCLS_EXITING;
  3989. if (adjust_vmx_controls(min2, opt2,
  3990. MSR_IA32_VMX_PROCBASED_CTLS2,
  3991. &_cpu_based_2nd_exec_control) < 0)
  3992. return -EIO;
  3993. }
  3994. #ifndef CONFIG_X86_64
  3995. if (!(_cpu_based_2nd_exec_control &
  3996. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
  3997. _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
  3998. #endif
  3999. if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
  4000. _cpu_based_2nd_exec_control &= ~(
  4001. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  4002. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  4003. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  4004. rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
  4005. &vmx_capability.ept, &vmx_capability.vpid);
  4006. if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
  4007. /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
  4008. enabled */
  4009. _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
  4010. CPU_BASED_CR3_STORE_EXITING |
  4011. CPU_BASED_INVLPG_EXITING);
  4012. } else if (vmx_capability.ept) {
  4013. vmx_capability.ept = 0;
  4014. pr_warn_once("EPT CAP should not exist if not support "
  4015. "1-setting enable EPT VM-execution control\n");
  4016. }
  4017. if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
  4018. vmx_capability.vpid) {
  4019. vmx_capability.vpid = 0;
  4020. pr_warn_once("VPID CAP should not exist if not support "
  4021. "1-setting enable VPID VM-execution control\n");
  4022. }
  4023. min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
  4024. #ifdef CONFIG_X86_64
  4025. min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
  4026. #endif
  4027. opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
  4028. VM_EXIT_CLEAR_BNDCFGS;
  4029. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
  4030. &_vmexit_control) < 0)
  4031. return -EIO;
  4032. min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
  4033. opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
  4034. PIN_BASED_VMX_PREEMPTION_TIMER;
  4035. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
  4036. &_pin_based_exec_control) < 0)
  4037. return -EIO;
  4038. if (cpu_has_broken_vmx_preemption_timer())
  4039. _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  4040. if (!(_cpu_based_2nd_exec_control &
  4041. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
  4042. _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
  4043. min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
  4044. opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
  4045. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
  4046. &_vmentry_control) < 0)
  4047. return -EIO;
  4048. rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
  4049. /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
  4050. if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
  4051. return -EIO;
  4052. #ifdef CONFIG_X86_64
  4053. /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
  4054. if (vmx_msr_high & (1u<<16))
  4055. return -EIO;
  4056. #endif
  4057. /* Require Write-Back (WB) memory type for VMCS accesses. */
  4058. if (((vmx_msr_high >> 18) & 15) != 6)
  4059. return -EIO;
  4060. vmcs_conf->size = vmx_msr_high & 0x1fff;
  4061. vmcs_conf->order = get_order(vmcs_conf->size);
  4062. vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
  4063. vmcs_conf->revision_id = vmx_msr_low;
  4064. vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
  4065. vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
  4066. vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
  4067. vmcs_conf->vmexit_ctrl = _vmexit_control;
  4068. vmcs_conf->vmentry_ctrl = _vmentry_control;
  4069. if (static_branch_unlikely(&enable_evmcs))
  4070. evmcs_sanitize_exec_ctrls(vmcs_conf);
  4071. cpu_has_load_ia32_efer =
  4072. allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
  4073. VM_ENTRY_LOAD_IA32_EFER)
  4074. && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
  4075. VM_EXIT_LOAD_IA32_EFER);
  4076. cpu_has_load_perf_global_ctrl =
  4077. allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
  4078. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
  4079. && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
  4080. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
  4081. /*
  4082. * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
  4083. * but due to errata below it can't be used. Workaround is to use
  4084. * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
  4085. *
  4086. * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
  4087. *
  4088. * AAK155 (model 26)
  4089. * AAP115 (model 30)
  4090. * AAT100 (model 37)
  4091. * BC86,AAY89,BD102 (model 44)
  4092. * BA97 (model 46)
  4093. *
  4094. */
  4095. if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
  4096. switch (boot_cpu_data.x86_model) {
  4097. case 26:
  4098. case 30:
  4099. case 37:
  4100. case 44:
  4101. case 46:
  4102. cpu_has_load_perf_global_ctrl = false;
  4103. printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
  4104. "does not work properly. Using workaround\n");
  4105. break;
  4106. default:
  4107. break;
  4108. }
  4109. }
  4110. if (boot_cpu_has(X86_FEATURE_XSAVES))
  4111. rdmsrl(MSR_IA32_XSS, host_xss);
  4112. return 0;
  4113. }
  4114. static struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu)
  4115. {
  4116. int node = cpu_to_node(cpu);
  4117. struct page *pages;
  4118. struct vmcs *vmcs;
  4119. pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
  4120. if (!pages)
  4121. return NULL;
  4122. vmcs = page_address(pages);
  4123. memset(vmcs, 0, vmcs_config.size);
  4124. /* KVM supports Enlightened VMCS v1 only */
  4125. if (static_branch_unlikely(&enable_evmcs))
  4126. vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
  4127. else
  4128. vmcs->hdr.revision_id = vmcs_config.revision_id;
  4129. if (shadow)
  4130. vmcs->hdr.shadow_vmcs = 1;
  4131. return vmcs;
  4132. }
  4133. static void free_vmcs(struct vmcs *vmcs)
  4134. {
  4135. free_pages((unsigned long)vmcs, vmcs_config.order);
  4136. }
  4137. /*
  4138. * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
  4139. */
  4140. static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
  4141. {
  4142. if (!loaded_vmcs->vmcs)
  4143. return;
  4144. loaded_vmcs_clear(loaded_vmcs);
  4145. free_vmcs(loaded_vmcs->vmcs);
  4146. loaded_vmcs->vmcs = NULL;
  4147. if (loaded_vmcs->msr_bitmap)
  4148. free_page((unsigned long)loaded_vmcs->msr_bitmap);
  4149. WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
  4150. }
  4151. static struct vmcs *alloc_vmcs(bool shadow)
  4152. {
  4153. return alloc_vmcs_cpu(shadow, raw_smp_processor_id());
  4154. }
  4155. static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
  4156. {
  4157. loaded_vmcs->vmcs = alloc_vmcs(false);
  4158. if (!loaded_vmcs->vmcs)
  4159. return -ENOMEM;
  4160. loaded_vmcs->shadow_vmcs = NULL;
  4161. loaded_vmcs_init(loaded_vmcs);
  4162. if (cpu_has_vmx_msr_bitmap()) {
  4163. loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
  4164. if (!loaded_vmcs->msr_bitmap)
  4165. goto out_vmcs;
  4166. memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
  4167. if (IS_ENABLED(CONFIG_HYPERV) &&
  4168. static_branch_unlikely(&enable_evmcs) &&
  4169. (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
  4170. struct hv_enlightened_vmcs *evmcs =
  4171. (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
  4172. evmcs->hv_enlightenments_control.msr_bitmap = 1;
  4173. }
  4174. }
  4175. memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state));
  4176. return 0;
  4177. out_vmcs:
  4178. free_loaded_vmcs(loaded_vmcs);
  4179. return -ENOMEM;
  4180. }
  4181. static void free_kvm_area(void)
  4182. {
  4183. int cpu;
  4184. for_each_possible_cpu(cpu) {
  4185. free_vmcs(per_cpu(vmxarea, cpu));
  4186. per_cpu(vmxarea, cpu) = NULL;
  4187. }
  4188. }
  4189. enum vmcs_field_width {
  4190. VMCS_FIELD_WIDTH_U16 = 0,
  4191. VMCS_FIELD_WIDTH_U64 = 1,
  4192. VMCS_FIELD_WIDTH_U32 = 2,
  4193. VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
  4194. };
  4195. static inline int vmcs_field_width(unsigned long field)
  4196. {
  4197. if (0x1 & field) /* the *_HIGH fields are all 32 bit */
  4198. return VMCS_FIELD_WIDTH_U32;
  4199. return (field >> 13) & 0x3 ;
  4200. }
  4201. static inline int vmcs_field_readonly(unsigned long field)
  4202. {
  4203. return (((field >> 10) & 0x3) == 1);
  4204. }
  4205. static void init_vmcs_shadow_fields(void)
  4206. {
  4207. int i, j;
  4208. for (i = j = 0; i < max_shadow_read_only_fields; i++) {
  4209. u16 field = shadow_read_only_fields[i];
  4210. if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
  4211. (i + 1 == max_shadow_read_only_fields ||
  4212. shadow_read_only_fields[i + 1] != field + 1))
  4213. pr_err("Missing field from shadow_read_only_field %x\n",
  4214. field + 1);
  4215. clear_bit(field, vmx_vmread_bitmap);
  4216. #ifdef CONFIG_X86_64
  4217. if (field & 1)
  4218. continue;
  4219. #endif
  4220. if (j < i)
  4221. shadow_read_only_fields[j] = field;
  4222. j++;
  4223. }
  4224. max_shadow_read_only_fields = j;
  4225. for (i = j = 0; i < max_shadow_read_write_fields; i++) {
  4226. u16 field = shadow_read_write_fields[i];
  4227. if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
  4228. (i + 1 == max_shadow_read_write_fields ||
  4229. shadow_read_write_fields[i + 1] != field + 1))
  4230. pr_err("Missing field from shadow_read_write_field %x\n",
  4231. field + 1);
  4232. /*
  4233. * PML and the preemption timer can be emulated, but the
  4234. * processor cannot vmwrite to fields that don't exist
  4235. * on bare metal.
  4236. */
  4237. switch (field) {
  4238. case GUEST_PML_INDEX:
  4239. if (!cpu_has_vmx_pml())
  4240. continue;
  4241. break;
  4242. case VMX_PREEMPTION_TIMER_VALUE:
  4243. if (!cpu_has_vmx_preemption_timer())
  4244. continue;
  4245. break;
  4246. case GUEST_INTR_STATUS:
  4247. if (!cpu_has_vmx_apicv())
  4248. continue;
  4249. break;
  4250. default:
  4251. break;
  4252. }
  4253. clear_bit(field, vmx_vmwrite_bitmap);
  4254. clear_bit(field, vmx_vmread_bitmap);
  4255. #ifdef CONFIG_X86_64
  4256. if (field & 1)
  4257. continue;
  4258. #endif
  4259. if (j < i)
  4260. shadow_read_write_fields[j] = field;
  4261. j++;
  4262. }
  4263. max_shadow_read_write_fields = j;
  4264. }
  4265. static __init int alloc_kvm_area(void)
  4266. {
  4267. int cpu;
  4268. for_each_possible_cpu(cpu) {
  4269. struct vmcs *vmcs;
  4270. vmcs = alloc_vmcs_cpu(false, cpu);
  4271. if (!vmcs) {
  4272. free_kvm_area();
  4273. return -ENOMEM;
  4274. }
  4275. /*
  4276. * When eVMCS is enabled, alloc_vmcs_cpu() sets
  4277. * vmcs->revision_id to KVM_EVMCS_VERSION instead of
  4278. * revision_id reported by MSR_IA32_VMX_BASIC.
  4279. *
  4280. * However, even though not explictly documented by
  4281. * TLFS, VMXArea passed as VMXON argument should
  4282. * still be marked with revision_id reported by
  4283. * physical CPU.
  4284. */
  4285. if (static_branch_unlikely(&enable_evmcs))
  4286. vmcs->hdr.revision_id = vmcs_config.revision_id;
  4287. per_cpu(vmxarea, cpu) = vmcs;
  4288. }
  4289. return 0;
  4290. }
  4291. static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
  4292. struct kvm_segment *save)
  4293. {
  4294. if (!emulate_invalid_guest_state) {
  4295. /*
  4296. * CS and SS RPL should be equal during guest entry according
  4297. * to VMX spec, but in reality it is not always so. Since vcpu
  4298. * is in the middle of the transition from real mode to
  4299. * protected mode it is safe to assume that RPL 0 is a good
  4300. * default value.
  4301. */
  4302. if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
  4303. save->selector &= ~SEGMENT_RPL_MASK;
  4304. save->dpl = save->selector & SEGMENT_RPL_MASK;
  4305. save->s = 1;
  4306. }
  4307. vmx_set_segment(vcpu, save, seg);
  4308. }
  4309. static void enter_pmode(struct kvm_vcpu *vcpu)
  4310. {
  4311. unsigned long flags;
  4312. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4313. /*
  4314. * Update real mode segment cache. It may be not up-to-date if sement
  4315. * register was written while vcpu was in a guest mode.
  4316. */
  4317. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
  4318. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
  4319. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
  4320. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
  4321. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
  4322. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
  4323. vmx->rmode.vm86_active = 0;
  4324. vmx_segment_cache_clear(vmx);
  4325. vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
  4326. flags = vmcs_readl(GUEST_RFLAGS);
  4327. flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
  4328. flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
  4329. vmcs_writel(GUEST_RFLAGS, flags);
  4330. vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
  4331. (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
  4332. update_exception_bitmap(vcpu);
  4333. fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
  4334. fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
  4335. fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
  4336. fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
  4337. fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
  4338. fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
  4339. }
  4340. static void fix_rmode_seg(int seg, struct kvm_segment *save)
  4341. {
  4342. const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  4343. struct kvm_segment var = *save;
  4344. var.dpl = 0x3;
  4345. if (seg == VCPU_SREG_CS)
  4346. var.type = 0x3;
  4347. if (!emulate_invalid_guest_state) {
  4348. var.selector = var.base >> 4;
  4349. var.base = var.base & 0xffff0;
  4350. var.limit = 0xffff;
  4351. var.g = 0;
  4352. var.db = 0;
  4353. var.present = 1;
  4354. var.s = 1;
  4355. var.l = 0;
  4356. var.unusable = 0;
  4357. var.type = 0x3;
  4358. var.avl = 0;
  4359. if (save->base & 0xf)
  4360. printk_once(KERN_WARNING "kvm: segment base is not "
  4361. "paragraph aligned when entering "
  4362. "protected mode (seg=%d)", seg);
  4363. }
  4364. vmcs_write16(sf->selector, var.selector);
  4365. vmcs_writel(sf->base, var.base);
  4366. vmcs_write32(sf->limit, var.limit);
  4367. vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
  4368. }
  4369. static void enter_rmode(struct kvm_vcpu *vcpu)
  4370. {
  4371. unsigned long flags;
  4372. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4373. struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
  4374. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
  4375. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
  4376. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
  4377. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
  4378. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
  4379. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
  4380. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
  4381. vmx->rmode.vm86_active = 1;
  4382. /*
  4383. * Very old userspace does not call KVM_SET_TSS_ADDR before entering
  4384. * vcpu. Warn the user that an update is overdue.
  4385. */
  4386. if (!kvm_vmx->tss_addr)
  4387. printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
  4388. "called before entering vcpu\n");
  4389. vmx_segment_cache_clear(vmx);
  4390. vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
  4391. vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
  4392. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  4393. flags = vmcs_readl(GUEST_RFLAGS);
  4394. vmx->rmode.save_rflags = flags;
  4395. flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
  4396. vmcs_writel(GUEST_RFLAGS, flags);
  4397. vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
  4398. update_exception_bitmap(vcpu);
  4399. fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
  4400. fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
  4401. fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
  4402. fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
  4403. fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
  4404. fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
  4405. kvm_mmu_reset_context(vcpu);
  4406. }
  4407. static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
  4408. {
  4409. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4410. struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
  4411. if (!msr)
  4412. return;
  4413. vcpu->arch.efer = efer;
  4414. if (efer & EFER_LMA) {
  4415. vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
  4416. msr->data = efer;
  4417. } else {
  4418. vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
  4419. msr->data = efer & ~EFER_LME;
  4420. }
  4421. setup_msrs(vmx);
  4422. }
  4423. #ifdef CONFIG_X86_64
  4424. static void enter_lmode(struct kvm_vcpu *vcpu)
  4425. {
  4426. u32 guest_tr_ar;
  4427. vmx_segment_cache_clear(to_vmx(vcpu));
  4428. guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
  4429. if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
  4430. pr_debug_ratelimited("%s: tss fixup for long mode. \n",
  4431. __func__);
  4432. vmcs_write32(GUEST_TR_AR_BYTES,
  4433. (guest_tr_ar & ~VMX_AR_TYPE_MASK)
  4434. | VMX_AR_TYPE_BUSY_64_TSS);
  4435. }
  4436. vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
  4437. }
  4438. static void exit_lmode(struct kvm_vcpu *vcpu)
  4439. {
  4440. vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
  4441. vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
  4442. }
  4443. #endif
  4444. static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
  4445. bool invalidate_gpa)
  4446. {
  4447. if (enable_ept && (invalidate_gpa || !enable_vpid)) {
  4448. if (!VALID_PAGE(vcpu->arch.mmu->root_hpa))
  4449. return;
  4450. ept_sync_context(construct_eptp(vcpu,
  4451. vcpu->arch.mmu->root_hpa));
  4452. } else {
  4453. vpid_sync_context(vpid);
  4454. }
  4455. }
  4456. static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
  4457. {
  4458. __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
  4459. }
  4460. static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
  4461. {
  4462. int vpid = to_vmx(vcpu)->vpid;
  4463. if (!vpid_sync_vcpu_addr(vpid, addr))
  4464. vpid_sync_context(vpid);
  4465. /*
  4466. * If VPIDs are not supported or enabled, then the above is a no-op.
  4467. * But we don't really need a TLB flush in that case anyway, because
  4468. * each VM entry/exit includes an implicit flush when VPID is 0.
  4469. */
  4470. }
  4471. static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
  4472. {
  4473. ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
  4474. vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
  4475. vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
  4476. }
  4477. static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
  4478. {
  4479. if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
  4480. vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
  4481. __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
  4482. }
  4483. static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
  4484. {
  4485. ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
  4486. vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
  4487. vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
  4488. }
  4489. static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
  4490. {
  4491. struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
  4492. if (!test_bit(VCPU_EXREG_PDPTR,
  4493. (unsigned long *)&vcpu->arch.regs_dirty))
  4494. return;
  4495. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  4496. vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
  4497. vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
  4498. vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
  4499. vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
  4500. }
  4501. }
  4502. static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
  4503. {
  4504. struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
  4505. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  4506. mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
  4507. mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
  4508. mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
  4509. mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
  4510. }
  4511. __set_bit(VCPU_EXREG_PDPTR,
  4512. (unsigned long *)&vcpu->arch.regs_avail);
  4513. __set_bit(VCPU_EXREG_PDPTR,
  4514. (unsigned long *)&vcpu->arch.regs_dirty);
  4515. }
  4516. static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
  4517. {
  4518. u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
  4519. u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
  4520. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  4521. if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
  4522. SECONDARY_EXEC_UNRESTRICTED_GUEST &&
  4523. nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
  4524. fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
  4525. return fixed_bits_valid(val, fixed0, fixed1);
  4526. }
  4527. static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
  4528. {
  4529. u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
  4530. u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
  4531. return fixed_bits_valid(val, fixed0, fixed1);
  4532. }
  4533. static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
  4534. {
  4535. u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
  4536. u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
  4537. return fixed_bits_valid(val, fixed0, fixed1);
  4538. }
  4539. /* No difference in the restrictions on guest and host CR4 in VMX operation. */
  4540. #define nested_guest_cr4_valid nested_cr4_valid
  4541. #define nested_host_cr4_valid nested_cr4_valid
  4542. static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
  4543. static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
  4544. unsigned long cr0,
  4545. struct kvm_vcpu *vcpu)
  4546. {
  4547. if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
  4548. vmx_decache_cr3(vcpu);
  4549. if (!(cr0 & X86_CR0_PG)) {
  4550. /* From paging/starting to nonpaging */
  4551. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  4552. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
  4553. (CPU_BASED_CR3_LOAD_EXITING |
  4554. CPU_BASED_CR3_STORE_EXITING));
  4555. vcpu->arch.cr0 = cr0;
  4556. vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
  4557. } else if (!is_paging(vcpu)) {
  4558. /* From nonpaging to paging */
  4559. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  4560. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
  4561. ~(CPU_BASED_CR3_LOAD_EXITING |
  4562. CPU_BASED_CR3_STORE_EXITING));
  4563. vcpu->arch.cr0 = cr0;
  4564. vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
  4565. }
  4566. if (!(cr0 & X86_CR0_WP))
  4567. *hw_cr0 &= ~X86_CR0_WP;
  4568. }
  4569. static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  4570. {
  4571. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4572. unsigned long hw_cr0;
  4573. hw_cr0 = (cr0 & ~KVM_VM_CR0_ALWAYS_OFF);
  4574. if (enable_unrestricted_guest)
  4575. hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
  4576. else {
  4577. hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
  4578. if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
  4579. enter_pmode(vcpu);
  4580. if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
  4581. enter_rmode(vcpu);
  4582. }
  4583. #ifdef CONFIG_X86_64
  4584. if (vcpu->arch.efer & EFER_LME) {
  4585. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
  4586. enter_lmode(vcpu);
  4587. if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
  4588. exit_lmode(vcpu);
  4589. }
  4590. #endif
  4591. if (enable_ept && !enable_unrestricted_guest)
  4592. ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
  4593. vmcs_writel(CR0_READ_SHADOW, cr0);
  4594. vmcs_writel(GUEST_CR0, hw_cr0);
  4595. vcpu->arch.cr0 = cr0;
  4596. /* depends on vcpu->arch.cr0 to be set to a new value */
  4597. vmx->emulation_required = emulation_required(vcpu);
  4598. }
  4599. static int get_ept_level(struct kvm_vcpu *vcpu)
  4600. {
  4601. if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
  4602. return 5;
  4603. return 4;
  4604. }
  4605. static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
  4606. {
  4607. u64 eptp = VMX_EPTP_MT_WB;
  4608. eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
  4609. if (enable_ept_ad_bits &&
  4610. (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
  4611. eptp |= VMX_EPTP_AD_ENABLE_BIT;
  4612. eptp |= (root_hpa & PAGE_MASK);
  4613. return eptp;
  4614. }
  4615. static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
  4616. {
  4617. struct kvm *kvm = vcpu->kvm;
  4618. unsigned long guest_cr3;
  4619. u64 eptp;
  4620. guest_cr3 = cr3;
  4621. if (enable_ept) {
  4622. eptp = construct_eptp(vcpu, cr3);
  4623. vmcs_write64(EPT_POINTER, eptp);
  4624. if (kvm_x86_ops->tlb_remote_flush) {
  4625. spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
  4626. to_vmx(vcpu)->ept_pointer = eptp;
  4627. to_kvm_vmx(kvm)->ept_pointers_match
  4628. = EPT_POINTERS_CHECK;
  4629. spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
  4630. }
  4631. if (enable_unrestricted_guest || is_paging(vcpu) ||
  4632. is_guest_mode(vcpu))
  4633. guest_cr3 = kvm_read_cr3(vcpu);
  4634. else
  4635. guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
  4636. ept_load_pdptrs(vcpu);
  4637. }
  4638. vmcs_writel(GUEST_CR3, guest_cr3);
  4639. }
  4640. static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  4641. {
  4642. /*
  4643. * Pass through host's Machine Check Enable value to hw_cr4, which
  4644. * is in force while we are in guest mode. Do not let guests control
  4645. * this bit, even if host CR4.MCE == 0.
  4646. */
  4647. unsigned long hw_cr4;
  4648. hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
  4649. if (enable_unrestricted_guest)
  4650. hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
  4651. else if (to_vmx(vcpu)->rmode.vm86_active)
  4652. hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
  4653. else
  4654. hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
  4655. if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
  4656. if (cr4 & X86_CR4_UMIP) {
  4657. vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
  4658. SECONDARY_EXEC_DESC);
  4659. hw_cr4 &= ~X86_CR4_UMIP;
  4660. } else if (!is_guest_mode(vcpu) ||
  4661. !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
  4662. vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
  4663. SECONDARY_EXEC_DESC);
  4664. }
  4665. if (cr4 & X86_CR4_VMXE) {
  4666. /*
  4667. * To use VMXON (and later other VMX instructions), a guest
  4668. * must first be able to turn on cr4.VMXE (see handle_vmon()).
  4669. * So basically the check on whether to allow nested VMX
  4670. * is here. We operate under the default treatment of SMM,
  4671. * so VMX cannot be enabled under SMM.
  4672. */
  4673. if (!nested_vmx_allowed(vcpu) || is_smm(vcpu))
  4674. return 1;
  4675. }
  4676. if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
  4677. return 1;
  4678. vcpu->arch.cr4 = cr4;
  4679. if (!enable_unrestricted_guest) {
  4680. if (enable_ept) {
  4681. if (!is_paging(vcpu)) {
  4682. hw_cr4 &= ~X86_CR4_PAE;
  4683. hw_cr4 |= X86_CR4_PSE;
  4684. } else if (!(cr4 & X86_CR4_PAE)) {
  4685. hw_cr4 &= ~X86_CR4_PAE;
  4686. }
  4687. }
  4688. /*
  4689. * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
  4690. * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
  4691. * to be manually disabled when guest switches to non-paging
  4692. * mode.
  4693. *
  4694. * If !enable_unrestricted_guest, the CPU is always running
  4695. * with CR0.PG=1 and CR4 needs to be modified.
  4696. * If enable_unrestricted_guest, the CPU automatically
  4697. * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
  4698. */
  4699. if (!is_paging(vcpu))
  4700. hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
  4701. }
  4702. vmcs_writel(CR4_READ_SHADOW, cr4);
  4703. vmcs_writel(GUEST_CR4, hw_cr4);
  4704. return 0;
  4705. }
  4706. static void vmx_get_segment(struct kvm_vcpu *vcpu,
  4707. struct kvm_segment *var, int seg)
  4708. {
  4709. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4710. u32 ar;
  4711. if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
  4712. *var = vmx->rmode.segs[seg];
  4713. if (seg == VCPU_SREG_TR
  4714. || var->selector == vmx_read_guest_seg_selector(vmx, seg))
  4715. return;
  4716. var->base = vmx_read_guest_seg_base(vmx, seg);
  4717. var->selector = vmx_read_guest_seg_selector(vmx, seg);
  4718. return;
  4719. }
  4720. var->base = vmx_read_guest_seg_base(vmx, seg);
  4721. var->limit = vmx_read_guest_seg_limit(vmx, seg);
  4722. var->selector = vmx_read_guest_seg_selector(vmx, seg);
  4723. ar = vmx_read_guest_seg_ar(vmx, seg);
  4724. var->unusable = (ar >> 16) & 1;
  4725. var->type = ar & 15;
  4726. var->s = (ar >> 4) & 1;
  4727. var->dpl = (ar >> 5) & 3;
  4728. /*
  4729. * Some userspaces do not preserve unusable property. Since usable
  4730. * segment has to be present according to VMX spec we can use present
  4731. * property to amend userspace bug by making unusable segment always
  4732. * nonpresent. vmx_segment_access_rights() already marks nonpresent
  4733. * segment as unusable.
  4734. */
  4735. var->present = !var->unusable;
  4736. var->avl = (ar >> 12) & 1;
  4737. var->l = (ar >> 13) & 1;
  4738. var->db = (ar >> 14) & 1;
  4739. var->g = (ar >> 15) & 1;
  4740. }
  4741. static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
  4742. {
  4743. struct kvm_segment s;
  4744. if (to_vmx(vcpu)->rmode.vm86_active) {
  4745. vmx_get_segment(vcpu, &s, seg);
  4746. return s.base;
  4747. }
  4748. return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
  4749. }
  4750. static int vmx_get_cpl(struct kvm_vcpu *vcpu)
  4751. {
  4752. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4753. if (unlikely(vmx->rmode.vm86_active))
  4754. return 0;
  4755. else {
  4756. int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
  4757. return VMX_AR_DPL(ar);
  4758. }
  4759. }
  4760. static u32 vmx_segment_access_rights(struct kvm_segment *var)
  4761. {
  4762. u32 ar;
  4763. if (var->unusable || !var->present)
  4764. ar = 1 << 16;
  4765. else {
  4766. ar = var->type & 15;
  4767. ar |= (var->s & 1) << 4;
  4768. ar |= (var->dpl & 3) << 5;
  4769. ar |= (var->present & 1) << 7;
  4770. ar |= (var->avl & 1) << 12;
  4771. ar |= (var->l & 1) << 13;
  4772. ar |= (var->db & 1) << 14;
  4773. ar |= (var->g & 1) << 15;
  4774. }
  4775. return ar;
  4776. }
  4777. static void vmx_set_segment(struct kvm_vcpu *vcpu,
  4778. struct kvm_segment *var, int seg)
  4779. {
  4780. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4781. const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  4782. vmx_segment_cache_clear(vmx);
  4783. if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
  4784. vmx->rmode.segs[seg] = *var;
  4785. if (seg == VCPU_SREG_TR)
  4786. vmcs_write16(sf->selector, var->selector);
  4787. else if (var->s)
  4788. fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
  4789. goto out;
  4790. }
  4791. vmcs_writel(sf->base, var->base);
  4792. vmcs_write32(sf->limit, var->limit);
  4793. vmcs_write16(sf->selector, var->selector);
  4794. /*
  4795. * Fix the "Accessed" bit in AR field of segment registers for older
  4796. * qemu binaries.
  4797. * IA32 arch specifies that at the time of processor reset the
  4798. * "Accessed" bit in the AR field of segment registers is 1. And qemu
  4799. * is setting it to 0 in the userland code. This causes invalid guest
  4800. * state vmexit when "unrestricted guest" mode is turned on.
  4801. * Fix for this setup issue in cpu_reset is being pushed in the qemu
  4802. * tree. Newer qemu binaries with that qemu fix would not need this
  4803. * kvm hack.
  4804. */
  4805. if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
  4806. var->type |= 0x1; /* Accessed */
  4807. vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
  4808. out:
  4809. vmx->emulation_required = emulation_required(vcpu);
  4810. }
  4811. static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
  4812. {
  4813. u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
  4814. *db = (ar >> 14) & 1;
  4815. *l = (ar >> 13) & 1;
  4816. }
  4817. static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  4818. {
  4819. dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
  4820. dt->address = vmcs_readl(GUEST_IDTR_BASE);
  4821. }
  4822. static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  4823. {
  4824. vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
  4825. vmcs_writel(GUEST_IDTR_BASE, dt->address);
  4826. }
  4827. static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  4828. {
  4829. dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
  4830. dt->address = vmcs_readl(GUEST_GDTR_BASE);
  4831. }
  4832. static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  4833. {
  4834. vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
  4835. vmcs_writel(GUEST_GDTR_BASE, dt->address);
  4836. }
  4837. static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
  4838. {
  4839. struct kvm_segment var;
  4840. u32 ar;
  4841. vmx_get_segment(vcpu, &var, seg);
  4842. var.dpl = 0x3;
  4843. if (seg == VCPU_SREG_CS)
  4844. var.type = 0x3;
  4845. ar = vmx_segment_access_rights(&var);
  4846. if (var.base != (var.selector << 4))
  4847. return false;
  4848. if (var.limit != 0xffff)
  4849. return false;
  4850. if (ar != 0xf3)
  4851. return false;
  4852. return true;
  4853. }
  4854. static bool code_segment_valid(struct kvm_vcpu *vcpu)
  4855. {
  4856. struct kvm_segment cs;
  4857. unsigned int cs_rpl;
  4858. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  4859. cs_rpl = cs.selector & SEGMENT_RPL_MASK;
  4860. if (cs.unusable)
  4861. return false;
  4862. if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
  4863. return false;
  4864. if (!cs.s)
  4865. return false;
  4866. if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
  4867. if (cs.dpl > cs_rpl)
  4868. return false;
  4869. } else {
  4870. if (cs.dpl != cs_rpl)
  4871. return false;
  4872. }
  4873. if (!cs.present)
  4874. return false;
  4875. /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
  4876. return true;
  4877. }
  4878. static bool stack_segment_valid(struct kvm_vcpu *vcpu)
  4879. {
  4880. struct kvm_segment ss;
  4881. unsigned int ss_rpl;
  4882. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  4883. ss_rpl = ss.selector & SEGMENT_RPL_MASK;
  4884. if (ss.unusable)
  4885. return true;
  4886. if (ss.type != 3 && ss.type != 7)
  4887. return false;
  4888. if (!ss.s)
  4889. return false;
  4890. if (ss.dpl != ss_rpl) /* DPL != RPL */
  4891. return false;
  4892. if (!ss.present)
  4893. return false;
  4894. return true;
  4895. }
  4896. static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
  4897. {
  4898. struct kvm_segment var;
  4899. unsigned int rpl;
  4900. vmx_get_segment(vcpu, &var, seg);
  4901. rpl = var.selector & SEGMENT_RPL_MASK;
  4902. if (var.unusable)
  4903. return true;
  4904. if (!var.s)
  4905. return false;
  4906. if (!var.present)
  4907. return false;
  4908. if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
  4909. if (var.dpl < rpl) /* DPL < RPL */
  4910. return false;
  4911. }
  4912. /* TODO: Add other members to kvm_segment_field to allow checking for other access
  4913. * rights flags
  4914. */
  4915. return true;
  4916. }
  4917. static bool tr_valid(struct kvm_vcpu *vcpu)
  4918. {
  4919. struct kvm_segment tr;
  4920. vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
  4921. if (tr.unusable)
  4922. return false;
  4923. if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
  4924. return false;
  4925. if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
  4926. return false;
  4927. if (!tr.present)
  4928. return false;
  4929. return true;
  4930. }
  4931. static bool ldtr_valid(struct kvm_vcpu *vcpu)
  4932. {
  4933. struct kvm_segment ldtr;
  4934. vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
  4935. if (ldtr.unusable)
  4936. return true;
  4937. if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
  4938. return false;
  4939. if (ldtr.type != 2)
  4940. return false;
  4941. if (!ldtr.present)
  4942. return false;
  4943. return true;
  4944. }
  4945. static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
  4946. {
  4947. struct kvm_segment cs, ss;
  4948. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  4949. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  4950. return ((cs.selector & SEGMENT_RPL_MASK) ==
  4951. (ss.selector & SEGMENT_RPL_MASK));
  4952. }
  4953. /*
  4954. * Check if guest state is valid. Returns true if valid, false if
  4955. * not.
  4956. * We assume that registers are always usable
  4957. */
  4958. static bool guest_state_valid(struct kvm_vcpu *vcpu)
  4959. {
  4960. if (enable_unrestricted_guest)
  4961. return true;
  4962. /* real mode guest state checks */
  4963. if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
  4964. if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
  4965. return false;
  4966. if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
  4967. return false;
  4968. if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
  4969. return false;
  4970. if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
  4971. return false;
  4972. if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
  4973. return false;
  4974. if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
  4975. return false;
  4976. } else {
  4977. /* protected mode guest state checks */
  4978. if (!cs_ss_rpl_check(vcpu))
  4979. return false;
  4980. if (!code_segment_valid(vcpu))
  4981. return false;
  4982. if (!stack_segment_valid(vcpu))
  4983. return false;
  4984. if (!data_segment_valid(vcpu, VCPU_SREG_DS))
  4985. return false;
  4986. if (!data_segment_valid(vcpu, VCPU_SREG_ES))
  4987. return false;
  4988. if (!data_segment_valid(vcpu, VCPU_SREG_FS))
  4989. return false;
  4990. if (!data_segment_valid(vcpu, VCPU_SREG_GS))
  4991. return false;
  4992. if (!tr_valid(vcpu))
  4993. return false;
  4994. if (!ldtr_valid(vcpu))
  4995. return false;
  4996. }
  4997. /* TODO:
  4998. * - Add checks on RIP
  4999. * - Add checks on RFLAGS
  5000. */
  5001. return true;
  5002. }
  5003. static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
  5004. {
  5005. return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
  5006. }
  5007. static int init_rmode_tss(struct kvm *kvm)
  5008. {
  5009. gfn_t fn;
  5010. u16 data = 0;
  5011. int idx, r;
  5012. idx = srcu_read_lock(&kvm->srcu);
  5013. fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
  5014. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  5015. if (r < 0)
  5016. goto out;
  5017. data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
  5018. r = kvm_write_guest_page(kvm, fn++, &data,
  5019. TSS_IOPB_BASE_OFFSET, sizeof(u16));
  5020. if (r < 0)
  5021. goto out;
  5022. r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
  5023. if (r < 0)
  5024. goto out;
  5025. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  5026. if (r < 0)
  5027. goto out;
  5028. data = ~0;
  5029. r = kvm_write_guest_page(kvm, fn, &data,
  5030. RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
  5031. sizeof(u8));
  5032. out:
  5033. srcu_read_unlock(&kvm->srcu, idx);
  5034. return r;
  5035. }
  5036. static int init_rmode_identity_map(struct kvm *kvm)
  5037. {
  5038. struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
  5039. int i, idx, r = 0;
  5040. kvm_pfn_t identity_map_pfn;
  5041. u32 tmp;
  5042. /* Protect kvm_vmx->ept_identity_pagetable_done. */
  5043. mutex_lock(&kvm->slots_lock);
  5044. if (likely(kvm_vmx->ept_identity_pagetable_done))
  5045. goto out2;
  5046. if (!kvm_vmx->ept_identity_map_addr)
  5047. kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
  5048. identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
  5049. r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
  5050. kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
  5051. if (r < 0)
  5052. goto out2;
  5053. idx = srcu_read_lock(&kvm->srcu);
  5054. r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
  5055. if (r < 0)
  5056. goto out;
  5057. /* Set up identity-mapping pagetable for EPT in real mode */
  5058. for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
  5059. tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
  5060. _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
  5061. r = kvm_write_guest_page(kvm, identity_map_pfn,
  5062. &tmp, i * sizeof(tmp), sizeof(tmp));
  5063. if (r < 0)
  5064. goto out;
  5065. }
  5066. kvm_vmx->ept_identity_pagetable_done = true;
  5067. out:
  5068. srcu_read_unlock(&kvm->srcu, idx);
  5069. out2:
  5070. mutex_unlock(&kvm->slots_lock);
  5071. return r;
  5072. }
  5073. static void seg_setup(int seg)
  5074. {
  5075. const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  5076. unsigned int ar;
  5077. vmcs_write16(sf->selector, 0);
  5078. vmcs_writel(sf->base, 0);
  5079. vmcs_write32(sf->limit, 0xffff);
  5080. ar = 0x93;
  5081. if (seg == VCPU_SREG_CS)
  5082. ar |= 0x08; /* code segment */
  5083. vmcs_write32(sf->ar_bytes, ar);
  5084. }
  5085. static int alloc_apic_access_page(struct kvm *kvm)
  5086. {
  5087. struct page *page;
  5088. int r = 0;
  5089. mutex_lock(&kvm->slots_lock);
  5090. if (kvm->arch.apic_access_page_done)
  5091. goto out;
  5092. r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
  5093. APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
  5094. if (r)
  5095. goto out;
  5096. page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
  5097. if (is_error_page(page)) {
  5098. r = -EFAULT;
  5099. goto out;
  5100. }
  5101. /*
  5102. * Do not pin the page in memory, so that memory hot-unplug
  5103. * is able to migrate it.
  5104. */
  5105. put_page(page);
  5106. kvm->arch.apic_access_page_done = true;
  5107. out:
  5108. mutex_unlock(&kvm->slots_lock);
  5109. return r;
  5110. }
  5111. static int allocate_vpid(void)
  5112. {
  5113. int vpid;
  5114. if (!enable_vpid)
  5115. return 0;
  5116. spin_lock(&vmx_vpid_lock);
  5117. vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
  5118. if (vpid < VMX_NR_VPIDS)
  5119. __set_bit(vpid, vmx_vpid_bitmap);
  5120. else
  5121. vpid = 0;
  5122. spin_unlock(&vmx_vpid_lock);
  5123. return vpid;
  5124. }
  5125. static void free_vpid(int vpid)
  5126. {
  5127. if (!enable_vpid || vpid == 0)
  5128. return;
  5129. spin_lock(&vmx_vpid_lock);
  5130. __clear_bit(vpid, vmx_vpid_bitmap);
  5131. spin_unlock(&vmx_vpid_lock);
  5132. }
  5133. static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
  5134. u32 msr, int type)
  5135. {
  5136. int f = sizeof(unsigned long);
  5137. if (!cpu_has_vmx_msr_bitmap())
  5138. return;
  5139. if (static_branch_unlikely(&enable_evmcs))
  5140. evmcs_touch_msr_bitmap();
  5141. /*
  5142. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  5143. * have the write-low and read-high bitmap offsets the wrong way round.
  5144. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  5145. */
  5146. if (msr <= 0x1fff) {
  5147. if (type & MSR_TYPE_R)
  5148. /* read-low */
  5149. __clear_bit(msr, msr_bitmap + 0x000 / f);
  5150. if (type & MSR_TYPE_W)
  5151. /* write-low */
  5152. __clear_bit(msr, msr_bitmap + 0x800 / f);
  5153. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  5154. msr &= 0x1fff;
  5155. if (type & MSR_TYPE_R)
  5156. /* read-high */
  5157. __clear_bit(msr, msr_bitmap + 0x400 / f);
  5158. if (type & MSR_TYPE_W)
  5159. /* write-high */
  5160. __clear_bit(msr, msr_bitmap + 0xc00 / f);
  5161. }
  5162. }
  5163. static __always_inline void vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
  5164. u32 msr, int type)
  5165. {
  5166. int f = sizeof(unsigned long);
  5167. if (!cpu_has_vmx_msr_bitmap())
  5168. return;
  5169. if (static_branch_unlikely(&enable_evmcs))
  5170. evmcs_touch_msr_bitmap();
  5171. /*
  5172. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  5173. * have the write-low and read-high bitmap offsets the wrong way round.
  5174. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  5175. */
  5176. if (msr <= 0x1fff) {
  5177. if (type & MSR_TYPE_R)
  5178. /* read-low */
  5179. __set_bit(msr, msr_bitmap + 0x000 / f);
  5180. if (type & MSR_TYPE_W)
  5181. /* write-low */
  5182. __set_bit(msr, msr_bitmap + 0x800 / f);
  5183. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  5184. msr &= 0x1fff;
  5185. if (type & MSR_TYPE_R)
  5186. /* read-high */
  5187. __set_bit(msr, msr_bitmap + 0x400 / f);
  5188. if (type & MSR_TYPE_W)
  5189. /* write-high */
  5190. __set_bit(msr, msr_bitmap + 0xc00 / f);
  5191. }
  5192. }
  5193. static __always_inline void vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
  5194. u32 msr, int type, bool value)
  5195. {
  5196. if (value)
  5197. vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
  5198. else
  5199. vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
  5200. }
  5201. /*
  5202. * If a msr is allowed by L0, we should check whether it is allowed by L1.
  5203. * The corresponding bit will be cleared unless both of L0 and L1 allow it.
  5204. */
  5205. static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
  5206. unsigned long *msr_bitmap_nested,
  5207. u32 msr, int type)
  5208. {
  5209. int f = sizeof(unsigned long);
  5210. /*
  5211. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  5212. * have the write-low and read-high bitmap offsets the wrong way round.
  5213. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  5214. */
  5215. if (msr <= 0x1fff) {
  5216. if (type & MSR_TYPE_R &&
  5217. !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
  5218. /* read-low */
  5219. __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
  5220. if (type & MSR_TYPE_W &&
  5221. !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
  5222. /* write-low */
  5223. __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
  5224. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  5225. msr &= 0x1fff;
  5226. if (type & MSR_TYPE_R &&
  5227. !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
  5228. /* read-high */
  5229. __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
  5230. if (type & MSR_TYPE_W &&
  5231. !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
  5232. /* write-high */
  5233. __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
  5234. }
  5235. }
  5236. static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
  5237. {
  5238. u8 mode = 0;
  5239. if (cpu_has_secondary_exec_ctrls() &&
  5240. (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
  5241. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
  5242. mode |= MSR_BITMAP_MODE_X2APIC;
  5243. if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
  5244. mode |= MSR_BITMAP_MODE_X2APIC_APICV;
  5245. }
  5246. return mode;
  5247. }
  5248. #define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
  5249. static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
  5250. u8 mode)
  5251. {
  5252. int msr;
  5253. for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
  5254. unsigned word = msr / BITS_PER_LONG;
  5255. msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
  5256. msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
  5257. }
  5258. if (mode & MSR_BITMAP_MODE_X2APIC) {
  5259. /*
  5260. * TPR reads and writes can be virtualized even if virtual interrupt
  5261. * delivery is not in use.
  5262. */
  5263. vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
  5264. if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
  5265. vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
  5266. vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
  5267. vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
  5268. }
  5269. }
  5270. }
  5271. static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
  5272. {
  5273. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5274. unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
  5275. u8 mode = vmx_msr_bitmap_mode(vcpu);
  5276. u8 changed = mode ^ vmx->msr_bitmap_mode;
  5277. if (!changed)
  5278. return;
  5279. if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
  5280. vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
  5281. vmx->msr_bitmap_mode = mode;
  5282. }
  5283. static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
  5284. {
  5285. return enable_apicv;
  5286. }
  5287. static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
  5288. {
  5289. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  5290. gfn_t gfn;
  5291. /*
  5292. * Don't need to mark the APIC access page dirty; it is never
  5293. * written to by the CPU during APIC virtualization.
  5294. */
  5295. if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
  5296. gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
  5297. kvm_vcpu_mark_page_dirty(vcpu, gfn);
  5298. }
  5299. if (nested_cpu_has_posted_intr(vmcs12)) {
  5300. gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
  5301. kvm_vcpu_mark_page_dirty(vcpu, gfn);
  5302. }
  5303. }
  5304. static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
  5305. {
  5306. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5307. int max_irr;
  5308. void *vapic_page;
  5309. u16 status;
  5310. if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
  5311. return;
  5312. vmx->nested.pi_pending = false;
  5313. if (!pi_test_and_clear_on(vmx->nested.pi_desc))
  5314. return;
  5315. max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
  5316. if (max_irr != 256) {
  5317. vapic_page = kmap(vmx->nested.virtual_apic_page);
  5318. __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
  5319. vapic_page, &max_irr);
  5320. kunmap(vmx->nested.virtual_apic_page);
  5321. status = vmcs_read16(GUEST_INTR_STATUS);
  5322. if ((u8)max_irr > ((u8)status & 0xff)) {
  5323. status &= ~0xff;
  5324. status |= (u8)max_irr;
  5325. vmcs_write16(GUEST_INTR_STATUS, status);
  5326. }
  5327. }
  5328. nested_mark_vmcs12_pages_dirty(vcpu);
  5329. }
  5330. static u8 vmx_get_rvi(void)
  5331. {
  5332. return vmcs_read16(GUEST_INTR_STATUS) & 0xff;
  5333. }
  5334. static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
  5335. {
  5336. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5337. void *vapic_page;
  5338. u32 vppr;
  5339. int rvi;
  5340. if (WARN_ON_ONCE(!is_guest_mode(vcpu)) ||
  5341. !nested_cpu_has_vid(get_vmcs12(vcpu)) ||
  5342. WARN_ON_ONCE(!vmx->nested.virtual_apic_page))
  5343. return false;
  5344. rvi = vmx_get_rvi();
  5345. vapic_page = kmap(vmx->nested.virtual_apic_page);
  5346. vppr = *((u32 *)(vapic_page + APIC_PROCPRI));
  5347. kunmap(vmx->nested.virtual_apic_page);
  5348. return ((rvi & 0xf0) > (vppr & 0xf0));
  5349. }
  5350. static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
  5351. bool nested)
  5352. {
  5353. #ifdef CONFIG_SMP
  5354. int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
  5355. if (vcpu->mode == IN_GUEST_MODE) {
  5356. /*
  5357. * The vector of interrupt to be delivered to vcpu had
  5358. * been set in PIR before this function.
  5359. *
  5360. * Following cases will be reached in this block, and
  5361. * we always send a notification event in all cases as
  5362. * explained below.
  5363. *
  5364. * Case 1: vcpu keeps in non-root mode. Sending a
  5365. * notification event posts the interrupt to vcpu.
  5366. *
  5367. * Case 2: vcpu exits to root mode and is still
  5368. * runnable. PIR will be synced to vIRR before the
  5369. * next vcpu entry. Sending a notification event in
  5370. * this case has no effect, as vcpu is not in root
  5371. * mode.
  5372. *
  5373. * Case 3: vcpu exits to root mode and is blocked.
  5374. * vcpu_block() has already synced PIR to vIRR and
  5375. * never blocks vcpu if vIRR is not cleared. Therefore,
  5376. * a blocked vcpu here does not wait for any requested
  5377. * interrupts in PIR, and sending a notification event
  5378. * which has no effect is safe here.
  5379. */
  5380. apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
  5381. return true;
  5382. }
  5383. #endif
  5384. return false;
  5385. }
  5386. static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
  5387. int vector)
  5388. {
  5389. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5390. if (is_guest_mode(vcpu) &&
  5391. vector == vmx->nested.posted_intr_nv) {
  5392. /*
  5393. * If a posted intr is not recognized by hardware,
  5394. * we will accomplish it in the next vmentry.
  5395. */
  5396. vmx->nested.pi_pending = true;
  5397. kvm_make_request(KVM_REQ_EVENT, vcpu);
  5398. /* the PIR and ON have been set by L1. */
  5399. if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
  5400. kvm_vcpu_kick(vcpu);
  5401. return 0;
  5402. }
  5403. return -1;
  5404. }
  5405. /*
  5406. * Send interrupt to vcpu via posted interrupt way.
  5407. * 1. If target vcpu is running(non-root mode), send posted interrupt
  5408. * notification to vcpu and hardware will sync PIR to vIRR atomically.
  5409. * 2. If target vcpu isn't running(root mode), kick it to pick up the
  5410. * interrupt from PIR in next vmentry.
  5411. */
  5412. static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
  5413. {
  5414. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5415. int r;
  5416. r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
  5417. if (!r)
  5418. return;
  5419. if (pi_test_and_set_pir(vector, &vmx->pi_desc))
  5420. return;
  5421. /* If a previous notification has sent the IPI, nothing to do. */
  5422. if (pi_test_and_set_on(&vmx->pi_desc))
  5423. return;
  5424. if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
  5425. kvm_vcpu_kick(vcpu);
  5426. }
  5427. /*
  5428. * Set up the vmcs's constant host-state fields, i.e., host-state fields that
  5429. * will not change in the lifetime of the guest.
  5430. * Note that host-state that does change is set elsewhere. E.g., host-state
  5431. * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
  5432. */
  5433. static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
  5434. {
  5435. u32 low32, high32;
  5436. unsigned long tmpl;
  5437. struct desc_ptr dt;
  5438. unsigned long cr0, cr3, cr4;
  5439. cr0 = read_cr0();
  5440. WARN_ON(cr0 & X86_CR0_TS);
  5441. vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
  5442. /*
  5443. * Save the most likely value for this task's CR3 in the VMCS.
  5444. * We can't use __get_current_cr3_fast() because we're not atomic.
  5445. */
  5446. cr3 = __read_cr3();
  5447. vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
  5448. vmx->loaded_vmcs->host_state.cr3 = cr3;
  5449. /* Save the most likely value for this task's CR4 in the VMCS. */
  5450. cr4 = cr4_read_shadow();
  5451. vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
  5452. vmx->loaded_vmcs->host_state.cr4 = cr4;
  5453. vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
  5454. #ifdef CONFIG_X86_64
  5455. /*
  5456. * Load null selectors, so we can avoid reloading them in
  5457. * vmx_prepare_switch_to_host(), in case userspace uses
  5458. * the null selectors too (the expected case).
  5459. */
  5460. vmcs_write16(HOST_DS_SELECTOR, 0);
  5461. vmcs_write16(HOST_ES_SELECTOR, 0);
  5462. #else
  5463. vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  5464. vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  5465. #endif
  5466. vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  5467. vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
  5468. store_idt(&dt);
  5469. vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
  5470. vmx->host_idt_base = dt.address;
  5471. vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
  5472. rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
  5473. vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
  5474. rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
  5475. vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
  5476. if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
  5477. rdmsr(MSR_IA32_CR_PAT, low32, high32);
  5478. vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
  5479. }
  5480. if (cpu_has_load_ia32_efer)
  5481. vmcs_write64(HOST_IA32_EFER, host_efer);
  5482. }
  5483. static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
  5484. {
  5485. vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
  5486. if (enable_ept)
  5487. vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
  5488. if (is_guest_mode(&vmx->vcpu))
  5489. vmx->vcpu.arch.cr4_guest_owned_bits &=
  5490. ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
  5491. vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
  5492. }
  5493. static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
  5494. {
  5495. u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
  5496. if (!kvm_vcpu_apicv_active(&vmx->vcpu))
  5497. pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
  5498. if (!enable_vnmi)
  5499. pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
  5500. /* Enable the preemption timer dynamically */
  5501. pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  5502. return pin_based_exec_ctrl;
  5503. }
  5504. static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
  5505. {
  5506. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5507. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
  5508. if (cpu_has_secondary_exec_ctrls()) {
  5509. if (kvm_vcpu_apicv_active(vcpu))
  5510. vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
  5511. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  5512. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  5513. else
  5514. vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
  5515. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  5516. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  5517. }
  5518. if (cpu_has_vmx_msr_bitmap())
  5519. vmx_update_msr_bitmap(vcpu);
  5520. }
  5521. static u32 vmx_exec_control(struct vcpu_vmx *vmx)
  5522. {
  5523. u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
  5524. if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
  5525. exec_control &= ~CPU_BASED_MOV_DR_EXITING;
  5526. if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
  5527. exec_control &= ~CPU_BASED_TPR_SHADOW;
  5528. #ifdef CONFIG_X86_64
  5529. exec_control |= CPU_BASED_CR8_STORE_EXITING |
  5530. CPU_BASED_CR8_LOAD_EXITING;
  5531. #endif
  5532. }
  5533. if (!enable_ept)
  5534. exec_control |= CPU_BASED_CR3_STORE_EXITING |
  5535. CPU_BASED_CR3_LOAD_EXITING |
  5536. CPU_BASED_INVLPG_EXITING;
  5537. if (kvm_mwait_in_guest(vmx->vcpu.kvm))
  5538. exec_control &= ~(CPU_BASED_MWAIT_EXITING |
  5539. CPU_BASED_MONITOR_EXITING);
  5540. if (kvm_hlt_in_guest(vmx->vcpu.kvm))
  5541. exec_control &= ~CPU_BASED_HLT_EXITING;
  5542. return exec_control;
  5543. }
  5544. static bool vmx_rdrand_supported(void)
  5545. {
  5546. return vmcs_config.cpu_based_2nd_exec_ctrl &
  5547. SECONDARY_EXEC_RDRAND_EXITING;
  5548. }
  5549. static bool vmx_rdseed_supported(void)
  5550. {
  5551. return vmcs_config.cpu_based_2nd_exec_ctrl &
  5552. SECONDARY_EXEC_RDSEED_EXITING;
  5553. }
  5554. static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
  5555. {
  5556. struct kvm_vcpu *vcpu = &vmx->vcpu;
  5557. u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
  5558. if (!cpu_need_virtualize_apic_accesses(vcpu))
  5559. exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  5560. if (vmx->vpid == 0)
  5561. exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
  5562. if (!enable_ept) {
  5563. exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
  5564. enable_unrestricted_guest = 0;
  5565. }
  5566. if (!enable_unrestricted_guest)
  5567. exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
  5568. if (kvm_pause_in_guest(vmx->vcpu.kvm))
  5569. exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  5570. if (!kvm_vcpu_apicv_active(vcpu))
  5571. exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
  5572. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  5573. exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  5574. /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
  5575. * in vmx_set_cr4. */
  5576. exec_control &= ~SECONDARY_EXEC_DESC;
  5577. /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
  5578. (handle_vmptrld).
  5579. We can NOT enable shadow_vmcs here because we don't have yet
  5580. a current VMCS12
  5581. */
  5582. exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
  5583. if (!enable_pml)
  5584. exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
  5585. if (vmx_xsaves_supported()) {
  5586. /* Exposing XSAVES only when XSAVE is exposed */
  5587. bool xsaves_enabled =
  5588. guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
  5589. guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
  5590. if (!xsaves_enabled)
  5591. exec_control &= ~SECONDARY_EXEC_XSAVES;
  5592. if (nested) {
  5593. if (xsaves_enabled)
  5594. vmx->nested.msrs.secondary_ctls_high |=
  5595. SECONDARY_EXEC_XSAVES;
  5596. else
  5597. vmx->nested.msrs.secondary_ctls_high &=
  5598. ~SECONDARY_EXEC_XSAVES;
  5599. }
  5600. }
  5601. if (vmx_rdtscp_supported()) {
  5602. bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
  5603. if (!rdtscp_enabled)
  5604. exec_control &= ~SECONDARY_EXEC_RDTSCP;
  5605. if (nested) {
  5606. if (rdtscp_enabled)
  5607. vmx->nested.msrs.secondary_ctls_high |=
  5608. SECONDARY_EXEC_RDTSCP;
  5609. else
  5610. vmx->nested.msrs.secondary_ctls_high &=
  5611. ~SECONDARY_EXEC_RDTSCP;
  5612. }
  5613. }
  5614. if (vmx_invpcid_supported()) {
  5615. /* Exposing INVPCID only when PCID is exposed */
  5616. bool invpcid_enabled =
  5617. guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
  5618. guest_cpuid_has(vcpu, X86_FEATURE_PCID);
  5619. if (!invpcid_enabled) {
  5620. exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
  5621. guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
  5622. }
  5623. if (nested) {
  5624. if (invpcid_enabled)
  5625. vmx->nested.msrs.secondary_ctls_high |=
  5626. SECONDARY_EXEC_ENABLE_INVPCID;
  5627. else
  5628. vmx->nested.msrs.secondary_ctls_high &=
  5629. ~SECONDARY_EXEC_ENABLE_INVPCID;
  5630. }
  5631. }
  5632. if (vmx_rdrand_supported()) {
  5633. bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
  5634. if (rdrand_enabled)
  5635. exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
  5636. if (nested) {
  5637. if (rdrand_enabled)
  5638. vmx->nested.msrs.secondary_ctls_high |=
  5639. SECONDARY_EXEC_RDRAND_EXITING;
  5640. else
  5641. vmx->nested.msrs.secondary_ctls_high &=
  5642. ~SECONDARY_EXEC_RDRAND_EXITING;
  5643. }
  5644. }
  5645. if (vmx_rdseed_supported()) {
  5646. bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
  5647. if (rdseed_enabled)
  5648. exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
  5649. if (nested) {
  5650. if (rdseed_enabled)
  5651. vmx->nested.msrs.secondary_ctls_high |=
  5652. SECONDARY_EXEC_RDSEED_EXITING;
  5653. else
  5654. vmx->nested.msrs.secondary_ctls_high &=
  5655. ~SECONDARY_EXEC_RDSEED_EXITING;
  5656. }
  5657. }
  5658. vmx->secondary_exec_control = exec_control;
  5659. }
  5660. static void ept_set_mmio_spte_mask(void)
  5661. {
  5662. /*
  5663. * EPT Misconfigurations can be generated if the value of bits 2:0
  5664. * of an EPT paging-structure entry is 110b (write/execute).
  5665. */
  5666. kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
  5667. VMX_EPT_MISCONFIG_WX_VALUE);
  5668. }
  5669. #define VMX_XSS_EXIT_BITMAP 0
  5670. /*
  5671. * Sets up the vmcs for emulated real mode.
  5672. */
  5673. static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
  5674. {
  5675. int i;
  5676. if (enable_shadow_vmcs) {
  5677. /*
  5678. * At vCPU creation, "VMWRITE to any supported field
  5679. * in the VMCS" is supported, so use the more
  5680. * permissive vmx_vmread_bitmap to specify both read
  5681. * and write permissions for the shadow VMCS.
  5682. */
  5683. vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
  5684. vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
  5685. }
  5686. if (cpu_has_vmx_msr_bitmap())
  5687. vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
  5688. vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
  5689. /* Control */
  5690. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
  5691. vmx->hv_deadline_tsc = -1;
  5692. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
  5693. if (cpu_has_secondary_exec_ctrls()) {
  5694. vmx_compute_secondary_exec_control(vmx);
  5695. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  5696. vmx->secondary_exec_control);
  5697. }
  5698. if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
  5699. vmcs_write64(EOI_EXIT_BITMAP0, 0);
  5700. vmcs_write64(EOI_EXIT_BITMAP1, 0);
  5701. vmcs_write64(EOI_EXIT_BITMAP2, 0);
  5702. vmcs_write64(EOI_EXIT_BITMAP3, 0);
  5703. vmcs_write16(GUEST_INTR_STATUS, 0);
  5704. vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
  5705. vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
  5706. }
  5707. if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
  5708. vmcs_write32(PLE_GAP, ple_gap);
  5709. vmx->ple_window = ple_window;
  5710. vmx->ple_window_dirty = true;
  5711. }
  5712. vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
  5713. vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
  5714. vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
  5715. vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
  5716. vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
  5717. vmx_set_constant_host_state(vmx);
  5718. vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
  5719. vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
  5720. if (cpu_has_vmx_vmfunc())
  5721. vmcs_write64(VM_FUNCTION_CONTROL, 0);
  5722. vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
  5723. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
  5724. vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
  5725. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
  5726. vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
  5727. if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
  5728. vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
  5729. for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
  5730. u32 index = vmx_msr_index[i];
  5731. u32 data_low, data_high;
  5732. int j = vmx->nmsrs;
  5733. if (rdmsr_safe(index, &data_low, &data_high) < 0)
  5734. continue;
  5735. if (wrmsr_safe(index, data_low, data_high) < 0)
  5736. continue;
  5737. vmx->guest_msrs[j].index = i;
  5738. vmx->guest_msrs[j].data = 0;
  5739. vmx->guest_msrs[j].mask = -1ull;
  5740. ++vmx->nmsrs;
  5741. }
  5742. vmx->arch_capabilities = kvm_get_arch_capabilities();
  5743. vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
  5744. /* 22.2.1, 20.8.1 */
  5745. vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
  5746. vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
  5747. vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
  5748. set_cr4_guest_host_mask(vmx);
  5749. if (vmx_xsaves_supported())
  5750. vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
  5751. if (enable_pml) {
  5752. vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
  5753. vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
  5754. }
  5755. if (cpu_has_vmx_encls_vmexit())
  5756. vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
  5757. }
  5758. static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
  5759. {
  5760. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5761. struct msr_data apic_base_msr;
  5762. u64 cr0;
  5763. vmx->rmode.vm86_active = 0;
  5764. vmx->spec_ctrl = 0;
  5765. vcpu->arch.microcode_version = 0x100000000ULL;
  5766. vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
  5767. kvm_set_cr8(vcpu, 0);
  5768. if (!init_event) {
  5769. apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
  5770. MSR_IA32_APICBASE_ENABLE;
  5771. if (kvm_vcpu_is_reset_bsp(vcpu))
  5772. apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
  5773. apic_base_msr.host_initiated = true;
  5774. kvm_set_apic_base(vcpu, &apic_base_msr);
  5775. }
  5776. vmx_segment_cache_clear(vmx);
  5777. seg_setup(VCPU_SREG_CS);
  5778. vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
  5779. vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
  5780. seg_setup(VCPU_SREG_DS);
  5781. seg_setup(VCPU_SREG_ES);
  5782. seg_setup(VCPU_SREG_FS);
  5783. seg_setup(VCPU_SREG_GS);
  5784. seg_setup(VCPU_SREG_SS);
  5785. vmcs_write16(GUEST_TR_SELECTOR, 0);
  5786. vmcs_writel(GUEST_TR_BASE, 0);
  5787. vmcs_write32(GUEST_TR_LIMIT, 0xffff);
  5788. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  5789. vmcs_write16(GUEST_LDTR_SELECTOR, 0);
  5790. vmcs_writel(GUEST_LDTR_BASE, 0);
  5791. vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
  5792. vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
  5793. if (!init_event) {
  5794. vmcs_write32(GUEST_SYSENTER_CS, 0);
  5795. vmcs_writel(GUEST_SYSENTER_ESP, 0);
  5796. vmcs_writel(GUEST_SYSENTER_EIP, 0);
  5797. vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
  5798. }
  5799. kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
  5800. kvm_rip_write(vcpu, 0xfff0);
  5801. vmcs_writel(GUEST_GDTR_BASE, 0);
  5802. vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
  5803. vmcs_writel(GUEST_IDTR_BASE, 0);
  5804. vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
  5805. vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
  5806. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
  5807. vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
  5808. if (kvm_mpx_supported())
  5809. vmcs_write64(GUEST_BNDCFGS, 0);
  5810. setup_msrs(vmx);
  5811. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
  5812. if (cpu_has_vmx_tpr_shadow() && !init_event) {
  5813. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
  5814. if (cpu_need_tpr_shadow(vcpu))
  5815. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
  5816. __pa(vcpu->arch.apic->regs));
  5817. vmcs_write32(TPR_THRESHOLD, 0);
  5818. }
  5819. kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
  5820. if (vmx->vpid != 0)
  5821. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
  5822. cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
  5823. vmx->vcpu.arch.cr0 = cr0;
  5824. vmx_set_cr0(vcpu, cr0); /* enter rmode */
  5825. vmx_set_cr4(vcpu, 0);
  5826. vmx_set_efer(vcpu, 0);
  5827. update_exception_bitmap(vcpu);
  5828. vpid_sync_context(vmx->vpid);
  5829. if (init_event)
  5830. vmx_clear_hlt(vcpu);
  5831. }
  5832. /*
  5833. * In nested virtualization, check if L1 asked to exit on external interrupts.
  5834. * For most existing hypervisors, this will always return true.
  5835. */
  5836. static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
  5837. {
  5838. return get_vmcs12(vcpu)->pin_based_vm_exec_control &
  5839. PIN_BASED_EXT_INTR_MASK;
  5840. }
  5841. /*
  5842. * In nested virtualization, check if L1 has set
  5843. * VM_EXIT_ACK_INTR_ON_EXIT
  5844. */
  5845. static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
  5846. {
  5847. return get_vmcs12(vcpu)->vm_exit_controls &
  5848. VM_EXIT_ACK_INTR_ON_EXIT;
  5849. }
  5850. static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
  5851. {
  5852. return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
  5853. }
  5854. static void enable_irq_window(struct kvm_vcpu *vcpu)
  5855. {
  5856. vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
  5857. CPU_BASED_VIRTUAL_INTR_PENDING);
  5858. }
  5859. static void enable_nmi_window(struct kvm_vcpu *vcpu)
  5860. {
  5861. if (!enable_vnmi ||
  5862. vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
  5863. enable_irq_window(vcpu);
  5864. return;
  5865. }
  5866. vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
  5867. CPU_BASED_VIRTUAL_NMI_PENDING);
  5868. }
  5869. static void vmx_inject_irq(struct kvm_vcpu *vcpu)
  5870. {
  5871. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5872. uint32_t intr;
  5873. int irq = vcpu->arch.interrupt.nr;
  5874. trace_kvm_inj_virq(irq);
  5875. ++vcpu->stat.irq_injections;
  5876. if (vmx->rmode.vm86_active) {
  5877. int inc_eip = 0;
  5878. if (vcpu->arch.interrupt.soft)
  5879. inc_eip = vcpu->arch.event_exit_inst_len;
  5880. if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
  5881. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  5882. return;
  5883. }
  5884. intr = irq | INTR_INFO_VALID_MASK;
  5885. if (vcpu->arch.interrupt.soft) {
  5886. intr |= INTR_TYPE_SOFT_INTR;
  5887. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  5888. vmx->vcpu.arch.event_exit_inst_len);
  5889. } else
  5890. intr |= INTR_TYPE_EXT_INTR;
  5891. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
  5892. vmx_clear_hlt(vcpu);
  5893. }
  5894. static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
  5895. {
  5896. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5897. if (!enable_vnmi) {
  5898. /*
  5899. * Tracking the NMI-blocked state in software is built upon
  5900. * finding the next open IRQ window. This, in turn, depends on
  5901. * well-behaving guests: They have to keep IRQs disabled at
  5902. * least as long as the NMI handler runs. Otherwise we may
  5903. * cause NMI nesting, maybe breaking the guest. But as this is
  5904. * highly unlikely, we can live with the residual risk.
  5905. */
  5906. vmx->loaded_vmcs->soft_vnmi_blocked = 1;
  5907. vmx->loaded_vmcs->vnmi_blocked_time = 0;
  5908. }
  5909. ++vcpu->stat.nmi_injections;
  5910. vmx->loaded_vmcs->nmi_known_unmasked = false;
  5911. if (vmx->rmode.vm86_active) {
  5912. if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
  5913. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  5914. return;
  5915. }
  5916. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  5917. INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
  5918. vmx_clear_hlt(vcpu);
  5919. }
  5920. static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
  5921. {
  5922. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5923. bool masked;
  5924. if (!enable_vnmi)
  5925. return vmx->loaded_vmcs->soft_vnmi_blocked;
  5926. if (vmx->loaded_vmcs->nmi_known_unmasked)
  5927. return false;
  5928. masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
  5929. vmx->loaded_vmcs->nmi_known_unmasked = !masked;
  5930. return masked;
  5931. }
  5932. static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
  5933. {
  5934. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5935. if (!enable_vnmi) {
  5936. if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
  5937. vmx->loaded_vmcs->soft_vnmi_blocked = masked;
  5938. vmx->loaded_vmcs->vnmi_blocked_time = 0;
  5939. }
  5940. } else {
  5941. vmx->loaded_vmcs->nmi_known_unmasked = !masked;
  5942. if (masked)
  5943. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  5944. GUEST_INTR_STATE_NMI);
  5945. else
  5946. vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
  5947. GUEST_INTR_STATE_NMI);
  5948. }
  5949. }
  5950. static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
  5951. {
  5952. if (to_vmx(vcpu)->nested.nested_run_pending)
  5953. return 0;
  5954. if (!enable_vnmi &&
  5955. to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
  5956. return 0;
  5957. return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
  5958. (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
  5959. | GUEST_INTR_STATE_NMI));
  5960. }
  5961. static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
  5962. {
  5963. return (!to_vmx(vcpu)->nested.nested_run_pending &&
  5964. vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
  5965. !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
  5966. (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
  5967. }
  5968. static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
  5969. {
  5970. int ret;
  5971. if (enable_unrestricted_guest)
  5972. return 0;
  5973. ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
  5974. PAGE_SIZE * 3);
  5975. if (ret)
  5976. return ret;
  5977. to_kvm_vmx(kvm)->tss_addr = addr;
  5978. return init_rmode_tss(kvm);
  5979. }
  5980. static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
  5981. {
  5982. to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
  5983. return 0;
  5984. }
  5985. static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
  5986. {
  5987. switch (vec) {
  5988. case BP_VECTOR:
  5989. /*
  5990. * Update instruction length as we may reinject the exception
  5991. * from user space while in guest debugging mode.
  5992. */
  5993. to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
  5994. vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  5995. if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  5996. return false;
  5997. /* fall through */
  5998. case DB_VECTOR:
  5999. if (vcpu->guest_debug &
  6000. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
  6001. return false;
  6002. /* fall through */
  6003. case DE_VECTOR:
  6004. case OF_VECTOR:
  6005. case BR_VECTOR:
  6006. case UD_VECTOR:
  6007. case DF_VECTOR:
  6008. case SS_VECTOR:
  6009. case GP_VECTOR:
  6010. case MF_VECTOR:
  6011. return true;
  6012. break;
  6013. }
  6014. return false;
  6015. }
  6016. static int handle_rmode_exception(struct kvm_vcpu *vcpu,
  6017. int vec, u32 err_code)
  6018. {
  6019. /*
  6020. * Instruction with address size override prefix opcode 0x67
  6021. * Cause the #SS fault with 0 error code in VM86 mode.
  6022. */
  6023. if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
  6024. if (kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE) {
  6025. if (vcpu->arch.halt_request) {
  6026. vcpu->arch.halt_request = 0;
  6027. return kvm_vcpu_halt(vcpu);
  6028. }
  6029. return 1;
  6030. }
  6031. return 0;
  6032. }
  6033. /*
  6034. * Forward all other exceptions that are valid in real mode.
  6035. * FIXME: Breaks guest debugging in real mode, needs to be fixed with
  6036. * the required debugging infrastructure rework.
  6037. */
  6038. kvm_queue_exception(vcpu, vec);
  6039. return 1;
  6040. }
  6041. /*
  6042. * Trigger machine check on the host. We assume all the MSRs are already set up
  6043. * by the CPU and that we still run on the same CPU as the MCE occurred on.
  6044. * We pass a fake environment to the machine check handler because we want
  6045. * the guest to be always treated like user space, no matter what context
  6046. * it used internally.
  6047. */
  6048. static void kvm_machine_check(void)
  6049. {
  6050. #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
  6051. struct pt_regs regs = {
  6052. .cs = 3, /* Fake ring 3 no matter what the guest ran on */
  6053. .flags = X86_EFLAGS_IF,
  6054. };
  6055. do_machine_check(&regs, 0);
  6056. #endif
  6057. }
  6058. static int handle_machine_check(struct kvm_vcpu *vcpu)
  6059. {
  6060. /* already handled by vcpu_run */
  6061. return 1;
  6062. }
  6063. static int handle_exception(struct kvm_vcpu *vcpu)
  6064. {
  6065. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6066. struct kvm_run *kvm_run = vcpu->run;
  6067. u32 intr_info, ex_no, error_code;
  6068. unsigned long cr2, rip, dr6;
  6069. u32 vect_info;
  6070. enum emulation_result er;
  6071. vect_info = vmx->idt_vectoring_info;
  6072. intr_info = vmx->exit_intr_info;
  6073. if (is_machine_check(intr_info))
  6074. return handle_machine_check(vcpu);
  6075. if (is_nmi(intr_info))
  6076. return 1; /* already handled by vmx_vcpu_run() */
  6077. if (is_invalid_opcode(intr_info))
  6078. return handle_ud(vcpu);
  6079. error_code = 0;
  6080. if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
  6081. error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
  6082. if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
  6083. WARN_ON_ONCE(!enable_vmware_backdoor);
  6084. er = kvm_emulate_instruction(vcpu,
  6085. EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
  6086. if (er == EMULATE_USER_EXIT)
  6087. return 0;
  6088. else if (er != EMULATE_DONE)
  6089. kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
  6090. return 1;
  6091. }
  6092. /*
  6093. * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
  6094. * MMIO, it is better to report an internal error.
  6095. * See the comments in vmx_handle_exit.
  6096. */
  6097. if ((vect_info & VECTORING_INFO_VALID_MASK) &&
  6098. !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
  6099. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  6100. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
  6101. vcpu->run->internal.ndata = 3;
  6102. vcpu->run->internal.data[0] = vect_info;
  6103. vcpu->run->internal.data[1] = intr_info;
  6104. vcpu->run->internal.data[2] = error_code;
  6105. return 0;
  6106. }
  6107. if (is_page_fault(intr_info)) {
  6108. cr2 = vmcs_readl(EXIT_QUALIFICATION);
  6109. /* EPT won't cause page fault directly */
  6110. WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
  6111. return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
  6112. }
  6113. ex_no = intr_info & INTR_INFO_VECTOR_MASK;
  6114. if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
  6115. return handle_rmode_exception(vcpu, ex_no, error_code);
  6116. switch (ex_no) {
  6117. case AC_VECTOR:
  6118. kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
  6119. return 1;
  6120. case DB_VECTOR:
  6121. dr6 = vmcs_readl(EXIT_QUALIFICATION);
  6122. if (!(vcpu->guest_debug &
  6123. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
  6124. vcpu->arch.dr6 &= ~15;
  6125. vcpu->arch.dr6 |= dr6 | DR6_RTM;
  6126. if (is_icebp(intr_info))
  6127. skip_emulated_instruction(vcpu);
  6128. kvm_queue_exception(vcpu, DB_VECTOR);
  6129. return 1;
  6130. }
  6131. kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
  6132. kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
  6133. /* fall through */
  6134. case BP_VECTOR:
  6135. /*
  6136. * Update instruction length as we may reinject #BP from
  6137. * user space while in guest debugging mode. Reading it for
  6138. * #DB as well causes no harm, it is not used in that case.
  6139. */
  6140. vmx->vcpu.arch.event_exit_inst_len =
  6141. vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  6142. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  6143. rip = kvm_rip_read(vcpu);
  6144. kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
  6145. kvm_run->debug.arch.exception = ex_no;
  6146. break;
  6147. default:
  6148. kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
  6149. kvm_run->ex.exception = ex_no;
  6150. kvm_run->ex.error_code = error_code;
  6151. break;
  6152. }
  6153. return 0;
  6154. }
  6155. static int handle_external_interrupt(struct kvm_vcpu *vcpu)
  6156. {
  6157. ++vcpu->stat.irq_exits;
  6158. return 1;
  6159. }
  6160. static int handle_triple_fault(struct kvm_vcpu *vcpu)
  6161. {
  6162. vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
  6163. vcpu->mmio_needed = 0;
  6164. return 0;
  6165. }
  6166. static int handle_io(struct kvm_vcpu *vcpu)
  6167. {
  6168. unsigned long exit_qualification;
  6169. int size, in, string;
  6170. unsigned port;
  6171. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6172. string = (exit_qualification & 16) != 0;
  6173. ++vcpu->stat.io_exits;
  6174. if (string)
  6175. return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
  6176. port = exit_qualification >> 16;
  6177. size = (exit_qualification & 7) + 1;
  6178. in = (exit_qualification & 8) != 0;
  6179. return kvm_fast_pio(vcpu, size, port, in);
  6180. }
  6181. static void
  6182. vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
  6183. {
  6184. /*
  6185. * Patch in the VMCALL instruction:
  6186. */
  6187. hypercall[0] = 0x0f;
  6188. hypercall[1] = 0x01;
  6189. hypercall[2] = 0xc1;
  6190. }
  6191. /* called to set cr0 as appropriate for a mov-to-cr0 exit. */
  6192. static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
  6193. {
  6194. if (is_guest_mode(vcpu)) {
  6195. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  6196. unsigned long orig_val = val;
  6197. /*
  6198. * We get here when L2 changed cr0 in a way that did not change
  6199. * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
  6200. * but did change L0 shadowed bits. So we first calculate the
  6201. * effective cr0 value that L1 would like to write into the
  6202. * hardware. It consists of the L2-owned bits from the new
  6203. * value combined with the L1-owned bits from L1's guest_cr0.
  6204. */
  6205. val = (val & ~vmcs12->cr0_guest_host_mask) |
  6206. (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
  6207. if (!nested_guest_cr0_valid(vcpu, val))
  6208. return 1;
  6209. if (kvm_set_cr0(vcpu, val))
  6210. return 1;
  6211. vmcs_writel(CR0_READ_SHADOW, orig_val);
  6212. return 0;
  6213. } else {
  6214. if (to_vmx(vcpu)->nested.vmxon &&
  6215. !nested_host_cr0_valid(vcpu, val))
  6216. return 1;
  6217. return kvm_set_cr0(vcpu, val);
  6218. }
  6219. }
  6220. static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
  6221. {
  6222. if (is_guest_mode(vcpu)) {
  6223. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  6224. unsigned long orig_val = val;
  6225. /* analogously to handle_set_cr0 */
  6226. val = (val & ~vmcs12->cr4_guest_host_mask) |
  6227. (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
  6228. if (kvm_set_cr4(vcpu, val))
  6229. return 1;
  6230. vmcs_writel(CR4_READ_SHADOW, orig_val);
  6231. return 0;
  6232. } else
  6233. return kvm_set_cr4(vcpu, val);
  6234. }
  6235. static int handle_desc(struct kvm_vcpu *vcpu)
  6236. {
  6237. WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
  6238. return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
  6239. }
  6240. static int handle_cr(struct kvm_vcpu *vcpu)
  6241. {
  6242. unsigned long exit_qualification, val;
  6243. int cr;
  6244. int reg;
  6245. int err;
  6246. int ret;
  6247. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6248. cr = exit_qualification & 15;
  6249. reg = (exit_qualification >> 8) & 15;
  6250. switch ((exit_qualification >> 4) & 3) {
  6251. case 0: /* mov to cr */
  6252. val = kvm_register_readl(vcpu, reg);
  6253. trace_kvm_cr_write(cr, val);
  6254. switch (cr) {
  6255. case 0:
  6256. err = handle_set_cr0(vcpu, val);
  6257. return kvm_complete_insn_gp(vcpu, err);
  6258. case 3:
  6259. WARN_ON_ONCE(enable_unrestricted_guest);
  6260. err = kvm_set_cr3(vcpu, val);
  6261. return kvm_complete_insn_gp(vcpu, err);
  6262. case 4:
  6263. err = handle_set_cr4(vcpu, val);
  6264. return kvm_complete_insn_gp(vcpu, err);
  6265. case 8: {
  6266. u8 cr8_prev = kvm_get_cr8(vcpu);
  6267. u8 cr8 = (u8)val;
  6268. err = kvm_set_cr8(vcpu, cr8);
  6269. ret = kvm_complete_insn_gp(vcpu, err);
  6270. if (lapic_in_kernel(vcpu))
  6271. return ret;
  6272. if (cr8_prev <= cr8)
  6273. return ret;
  6274. /*
  6275. * TODO: we might be squashing a
  6276. * KVM_GUESTDBG_SINGLESTEP-triggered
  6277. * KVM_EXIT_DEBUG here.
  6278. */
  6279. vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
  6280. return 0;
  6281. }
  6282. }
  6283. break;
  6284. case 2: /* clts */
  6285. WARN_ONCE(1, "Guest should always own CR0.TS");
  6286. vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
  6287. trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
  6288. return kvm_skip_emulated_instruction(vcpu);
  6289. case 1: /*mov from cr*/
  6290. switch (cr) {
  6291. case 3:
  6292. WARN_ON_ONCE(enable_unrestricted_guest);
  6293. val = kvm_read_cr3(vcpu);
  6294. kvm_register_write(vcpu, reg, val);
  6295. trace_kvm_cr_read(cr, val);
  6296. return kvm_skip_emulated_instruction(vcpu);
  6297. case 8:
  6298. val = kvm_get_cr8(vcpu);
  6299. kvm_register_write(vcpu, reg, val);
  6300. trace_kvm_cr_read(cr, val);
  6301. return kvm_skip_emulated_instruction(vcpu);
  6302. }
  6303. break;
  6304. case 3: /* lmsw */
  6305. val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
  6306. trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
  6307. kvm_lmsw(vcpu, val);
  6308. return kvm_skip_emulated_instruction(vcpu);
  6309. default:
  6310. break;
  6311. }
  6312. vcpu->run->exit_reason = 0;
  6313. vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
  6314. (int)(exit_qualification >> 4) & 3, cr);
  6315. return 0;
  6316. }
  6317. static int handle_dr(struct kvm_vcpu *vcpu)
  6318. {
  6319. unsigned long exit_qualification;
  6320. int dr, dr7, reg;
  6321. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6322. dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
  6323. /* First, if DR does not exist, trigger UD */
  6324. if (!kvm_require_dr(vcpu, dr))
  6325. return 1;
  6326. /* Do not handle if the CPL > 0, will trigger GP on re-entry */
  6327. if (!kvm_require_cpl(vcpu, 0))
  6328. return 1;
  6329. dr7 = vmcs_readl(GUEST_DR7);
  6330. if (dr7 & DR7_GD) {
  6331. /*
  6332. * As the vm-exit takes precedence over the debug trap, we
  6333. * need to emulate the latter, either for the host or the
  6334. * guest debugging itself.
  6335. */
  6336. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
  6337. vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
  6338. vcpu->run->debug.arch.dr7 = dr7;
  6339. vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
  6340. vcpu->run->debug.arch.exception = DB_VECTOR;
  6341. vcpu->run->exit_reason = KVM_EXIT_DEBUG;
  6342. return 0;
  6343. } else {
  6344. vcpu->arch.dr6 &= ~15;
  6345. vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
  6346. kvm_queue_exception(vcpu, DB_VECTOR);
  6347. return 1;
  6348. }
  6349. }
  6350. if (vcpu->guest_debug == 0) {
  6351. vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
  6352. CPU_BASED_MOV_DR_EXITING);
  6353. /*
  6354. * No more DR vmexits; force a reload of the debug registers
  6355. * and reenter on this instruction. The next vmexit will
  6356. * retrieve the full state of the debug registers.
  6357. */
  6358. vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
  6359. return 1;
  6360. }
  6361. reg = DEBUG_REG_ACCESS_REG(exit_qualification);
  6362. if (exit_qualification & TYPE_MOV_FROM_DR) {
  6363. unsigned long val;
  6364. if (kvm_get_dr(vcpu, dr, &val))
  6365. return 1;
  6366. kvm_register_write(vcpu, reg, val);
  6367. } else
  6368. if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
  6369. return 1;
  6370. return kvm_skip_emulated_instruction(vcpu);
  6371. }
  6372. static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
  6373. {
  6374. return vcpu->arch.dr6;
  6375. }
  6376. static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
  6377. {
  6378. }
  6379. static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
  6380. {
  6381. get_debugreg(vcpu->arch.db[0], 0);
  6382. get_debugreg(vcpu->arch.db[1], 1);
  6383. get_debugreg(vcpu->arch.db[2], 2);
  6384. get_debugreg(vcpu->arch.db[3], 3);
  6385. get_debugreg(vcpu->arch.dr6, 6);
  6386. vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
  6387. vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
  6388. vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
  6389. }
  6390. static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
  6391. {
  6392. vmcs_writel(GUEST_DR7, val);
  6393. }
  6394. static int handle_cpuid(struct kvm_vcpu *vcpu)
  6395. {
  6396. return kvm_emulate_cpuid(vcpu);
  6397. }
  6398. static int handle_rdmsr(struct kvm_vcpu *vcpu)
  6399. {
  6400. u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
  6401. struct msr_data msr_info;
  6402. msr_info.index = ecx;
  6403. msr_info.host_initiated = false;
  6404. if (vmx_get_msr(vcpu, &msr_info)) {
  6405. trace_kvm_msr_read_ex(ecx);
  6406. kvm_inject_gp(vcpu, 0);
  6407. return 1;
  6408. }
  6409. trace_kvm_msr_read(ecx, msr_info.data);
  6410. /* FIXME: handling of bits 32:63 of rax, rdx */
  6411. vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
  6412. vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
  6413. return kvm_skip_emulated_instruction(vcpu);
  6414. }
  6415. static int handle_wrmsr(struct kvm_vcpu *vcpu)
  6416. {
  6417. struct msr_data msr;
  6418. u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
  6419. u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
  6420. | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
  6421. msr.data = data;
  6422. msr.index = ecx;
  6423. msr.host_initiated = false;
  6424. if (kvm_set_msr(vcpu, &msr) != 0) {
  6425. trace_kvm_msr_write_ex(ecx, data);
  6426. kvm_inject_gp(vcpu, 0);
  6427. return 1;
  6428. }
  6429. trace_kvm_msr_write(ecx, data);
  6430. return kvm_skip_emulated_instruction(vcpu);
  6431. }
  6432. static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
  6433. {
  6434. kvm_apic_update_ppr(vcpu);
  6435. return 1;
  6436. }
  6437. static int handle_interrupt_window(struct kvm_vcpu *vcpu)
  6438. {
  6439. vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
  6440. CPU_BASED_VIRTUAL_INTR_PENDING);
  6441. kvm_make_request(KVM_REQ_EVENT, vcpu);
  6442. ++vcpu->stat.irq_window_exits;
  6443. return 1;
  6444. }
  6445. static int handle_halt(struct kvm_vcpu *vcpu)
  6446. {
  6447. return kvm_emulate_halt(vcpu);
  6448. }
  6449. static int handle_vmcall(struct kvm_vcpu *vcpu)
  6450. {
  6451. return kvm_emulate_hypercall(vcpu);
  6452. }
  6453. static int handle_invd(struct kvm_vcpu *vcpu)
  6454. {
  6455. return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
  6456. }
  6457. static int handle_invlpg(struct kvm_vcpu *vcpu)
  6458. {
  6459. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6460. kvm_mmu_invlpg(vcpu, exit_qualification);
  6461. return kvm_skip_emulated_instruction(vcpu);
  6462. }
  6463. static int handle_rdpmc(struct kvm_vcpu *vcpu)
  6464. {
  6465. int err;
  6466. err = kvm_rdpmc(vcpu);
  6467. return kvm_complete_insn_gp(vcpu, err);
  6468. }
  6469. static int handle_wbinvd(struct kvm_vcpu *vcpu)
  6470. {
  6471. return kvm_emulate_wbinvd(vcpu);
  6472. }
  6473. static int handle_xsetbv(struct kvm_vcpu *vcpu)
  6474. {
  6475. u64 new_bv = kvm_read_edx_eax(vcpu);
  6476. u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
  6477. if (kvm_set_xcr(vcpu, index, new_bv) == 0)
  6478. return kvm_skip_emulated_instruction(vcpu);
  6479. return 1;
  6480. }
  6481. static int handle_xsaves(struct kvm_vcpu *vcpu)
  6482. {
  6483. kvm_skip_emulated_instruction(vcpu);
  6484. WARN(1, "this should never happen\n");
  6485. return 1;
  6486. }
  6487. static int handle_xrstors(struct kvm_vcpu *vcpu)
  6488. {
  6489. kvm_skip_emulated_instruction(vcpu);
  6490. WARN(1, "this should never happen\n");
  6491. return 1;
  6492. }
  6493. static int handle_apic_access(struct kvm_vcpu *vcpu)
  6494. {
  6495. if (likely(fasteoi)) {
  6496. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6497. int access_type, offset;
  6498. access_type = exit_qualification & APIC_ACCESS_TYPE;
  6499. offset = exit_qualification & APIC_ACCESS_OFFSET;
  6500. /*
  6501. * Sane guest uses MOV to write EOI, with written value
  6502. * not cared. So make a short-circuit here by avoiding
  6503. * heavy instruction emulation.
  6504. */
  6505. if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
  6506. (offset == APIC_EOI)) {
  6507. kvm_lapic_set_eoi(vcpu);
  6508. return kvm_skip_emulated_instruction(vcpu);
  6509. }
  6510. }
  6511. return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
  6512. }
  6513. static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
  6514. {
  6515. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6516. int vector = exit_qualification & 0xff;
  6517. /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
  6518. kvm_apic_set_eoi_accelerated(vcpu, vector);
  6519. return 1;
  6520. }
  6521. static int handle_apic_write(struct kvm_vcpu *vcpu)
  6522. {
  6523. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6524. u32 offset = exit_qualification & 0xfff;
  6525. /* APIC-write VM exit is trap-like and thus no need to adjust IP */
  6526. kvm_apic_write_nodecode(vcpu, offset);
  6527. return 1;
  6528. }
  6529. static int handle_task_switch(struct kvm_vcpu *vcpu)
  6530. {
  6531. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6532. unsigned long exit_qualification;
  6533. bool has_error_code = false;
  6534. u32 error_code = 0;
  6535. u16 tss_selector;
  6536. int reason, type, idt_v, idt_index;
  6537. idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
  6538. idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
  6539. type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
  6540. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6541. reason = (u32)exit_qualification >> 30;
  6542. if (reason == TASK_SWITCH_GATE && idt_v) {
  6543. switch (type) {
  6544. case INTR_TYPE_NMI_INTR:
  6545. vcpu->arch.nmi_injected = false;
  6546. vmx_set_nmi_mask(vcpu, true);
  6547. break;
  6548. case INTR_TYPE_EXT_INTR:
  6549. case INTR_TYPE_SOFT_INTR:
  6550. kvm_clear_interrupt_queue(vcpu);
  6551. break;
  6552. case INTR_TYPE_HARD_EXCEPTION:
  6553. if (vmx->idt_vectoring_info &
  6554. VECTORING_INFO_DELIVER_CODE_MASK) {
  6555. has_error_code = true;
  6556. error_code =
  6557. vmcs_read32(IDT_VECTORING_ERROR_CODE);
  6558. }
  6559. /* fall through */
  6560. case INTR_TYPE_SOFT_EXCEPTION:
  6561. kvm_clear_exception_queue(vcpu);
  6562. break;
  6563. default:
  6564. break;
  6565. }
  6566. }
  6567. tss_selector = exit_qualification;
  6568. if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
  6569. type != INTR_TYPE_EXT_INTR &&
  6570. type != INTR_TYPE_NMI_INTR))
  6571. skip_emulated_instruction(vcpu);
  6572. if (kvm_task_switch(vcpu, tss_selector,
  6573. type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
  6574. has_error_code, error_code) == EMULATE_FAIL) {
  6575. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  6576. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  6577. vcpu->run->internal.ndata = 0;
  6578. return 0;
  6579. }
  6580. /*
  6581. * TODO: What about debug traps on tss switch?
  6582. * Are we supposed to inject them and update dr6?
  6583. */
  6584. return 1;
  6585. }
  6586. static int handle_ept_violation(struct kvm_vcpu *vcpu)
  6587. {
  6588. unsigned long exit_qualification;
  6589. gpa_t gpa;
  6590. u64 error_code;
  6591. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6592. /*
  6593. * EPT violation happened while executing iret from NMI,
  6594. * "blocked by NMI" bit has to be set before next VM entry.
  6595. * There are errata that may cause this bit to not be set:
  6596. * AAK134, BY25.
  6597. */
  6598. if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
  6599. enable_vnmi &&
  6600. (exit_qualification & INTR_INFO_UNBLOCK_NMI))
  6601. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
  6602. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  6603. trace_kvm_page_fault(gpa, exit_qualification);
  6604. /* Is it a read fault? */
  6605. error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
  6606. ? PFERR_USER_MASK : 0;
  6607. /* Is it a write fault? */
  6608. error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
  6609. ? PFERR_WRITE_MASK : 0;
  6610. /* Is it a fetch fault? */
  6611. error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
  6612. ? PFERR_FETCH_MASK : 0;
  6613. /* ept page table entry is present? */
  6614. error_code |= (exit_qualification &
  6615. (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
  6616. EPT_VIOLATION_EXECUTABLE))
  6617. ? PFERR_PRESENT_MASK : 0;
  6618. error_code |= (exit_qualification & 0x100) != 0 ?
  6619. PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
  6620. vcpu->arch.exit_qualification = exit_qualification;
  6621. return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
  6622. }
  6623. static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
  6624. {
  6625. gpa_t gpa;
  6626. /*
  6627. * A nested guest cannot optimize MMIO vmexits, because we have an
  6628. * nGPA here instead of the required GPA.
  6629. */
  6630. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  6631. if (!is_guest_mode(vcpu) &&
  6632. !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
  6633. trace_kvm_fast_mmio(gpa);
  6634. /*
  6635. * Doing kvm_skip_emulated_instruction() depends on undefined
  6636. * behavior: Intel's manual doesn't mandate
  6637. * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
  6638. * occurs and while on real hardware it was observed to be set,
  6639. * other hypervisors (namely Hyper-V) don't set it, we end up
  6640. * advancing IP with some random value. Disable fast mmio when
  6641. * running nested and keep it for real hardware in hope that
  6642. * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
  6643. */
  6644. if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
  6645. return kvm_skip_emulated_instruction(vcpu);
  6646. else
  6647. return kvm_emulate_instruction(vcpu, EMULTYPE_SKIP) ==
  6648. EMULATE_DONE;
  6649. }
  6650. return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
  6651. }
  6652. static int handle_nmi_window(struct kvm_vcpu *vcpu)
  6653. {
  6654. WARN_ON_ONCE(!enable_vnmi);
  6655. vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
  6656. CPU_BASED_VIRTUAL_NMI_PENDING);
  6657. ++vcpu->stat.nmi_window_exits;
  6658. kvm_make_request(KVM_REQ_EVENT, vcpu);
  6659. return 1;
  6660. }
  6661. static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
  6662. {
  6663. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6664. enum emulation_result err = EMULATE_DONE;
  6665. int ret = 1;
  6666. u32 cpu_exec_ctrl;
  6667. bool intr_window_requested;
  6668. unsigned count = 130;
  6669. /*
  6670. * We should never reach the point where we are emulating L2
  6671. * due to invalid guest state as that means we incorrectly
  6672. * allowed a nested VMEntry with an invalid vmcs12.
  6673. */
  6674. WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
  6675. cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  6676. intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
  6677. while (vmx->emulation_required && count-- != 0) {
  6678. if (intr_window_requested && vmx_interrupt_allowed(vcpu))
  6679. return handle_interrupt_window(&vmx->vcpu);
  6680. if (kvm_test_request(KVM_REQ_EVENT, vcpu))
  6681. return 1;
  6682. err = kvm_emulate_instruction(vcpu, 0);
  6683. if (err == EMULATE_USER_EXIT) {
  6684. ++vcpu->stat.mmio_exits;
  6685. ret = 0;
  6686. goto out;
  6687. }
  6688. if (err != EMULATE_DONE)
  6689. goto emulation_error;
  6690. if (vmx->emulation_required && !vmx->rmode.vm86_active &&
  6691. vcpu->arch.exception.pending)
  6692. goto emulation_error;
  6693. if (vcpu->arch.halt_request) {
  6694. vcpu->arch.halt_request = 0;
  6695. ret = kvm_vcpu_halt(vcpu);
  6696. goto out;
  6697. }
  6698. if (signal_pending(current))
  6699. goto out;
  6700. if (need_resched())
  6701. schedule();
  6702. }
  6703. out:
  6704. return ret;
  6705. emulation_error:
  6706. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  6707. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  6708. vcpu->run->internal.ndata = 0;
  6709. return 0;
  6710. }
  6711. static void grow_ple_window(struct kvm_vcpu *vcpu)
  6712. {
  6713. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6714. int old = vmx->ple_window;
  6715. vmx->ple_window = __grow_ple_window(old, ple_window,
  6716. ple_window_grow,
  6717. ple_window_max);
  6718. if (vmx->ple_window != old)
  6719. vmx->ple_window_dirty = true;
  6720. trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
  6721. }
  6722. static void shrink_ple_window(struct kvm_vcpu *vcpu)
  6723. {
  6724. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6725. int old = vmx->ple_window;
  6726. vmx->ple_window = __shrink_ple_window(old, ple_window,
  6727. ple_window_shrink,
  6728. ple_window);
  6729. if (vmx->ple_window != old)
  6730. vmx->ple_window_dirty = true;
  6731. trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
  6732. }
  6733. /*
  6734. * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
  6735. */
  6736. static void wakeup_handler(void)
  6737. {
  6738. struct kvm_vcpu *vcpu;
  6739. int cpu = smp_processor_id();
  6740. spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
  6741. list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
  6742. blocked_vcpu_list) {
  6743. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  6744. if (pi_test_on(pi_desc) == 1)
  6745. kvm_vcpu_kick(vcpu);
  6746. }
  6747. spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
  6748. }
  6749. static void vmx_enable_tdp(void)
  6750. {
  6751. kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
  6752. enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
  6753. enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
  6754. 0ull, VMX_EPT_EXECUTABLE_MASK,
  6755. cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
  6756. VMX_EPT_RWX_MASK, 0ull);
  6757. ept_set_mmio_spte_mask();
  6758. kvm_enable_tdp();
  6759. }
  6760. static __init int hardware_setup(void)
  6761. {
  6762. unsigned long host_bndcfgs;
  6763. int r = -ENOMEM, i;
  6764. rdmsrl_safe(MSR_EFER, &host_efer);
  6765. for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
  6766. kvm_define_shared_msr(i, vmx_msr_index[i]);
  6767. for (i = 0; i < VMX_BITMAP_NR; i++) {
  6768. vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
  6769. if (!vmx_bitmap[i])
  6770. goto out;
  6771. }
  6772. memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
  6773. memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
  6774. if (setup_vmcs_config(&vmcs_config) < 0) {
  6775. r = -EIO;
  6776. goto out;
  6777. }
  6778. if (boot_cpu_has(X86_FEATURE_NX))
  6779. kvm_enable_efer_bits(EFER_NX);
  6780. if (boot_cpu_has(X86_FEATURE_MPX)) {
  6781. rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
  6782. WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
  6783. }
  6784. if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
  6785. !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
  6786. enable_vpid = 0;
  6787. if (!cpu_has_vmx_ept() ||
  6788. !cpu_has_vmx_ept_4levels() ||
  6789. !cpu_has_vmx_ept_mt_wb() ||
  6790. !cpu_has_vmx_invept_global())
  6791. enable_ept = 0;
  6792. if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
  6793. enable_ept_ad_bits = 0;
  6794. if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
  6795. enable_unrestricted_guest = 0;
  6796. if (!cpu_has_vmx_flexpriority())
  6797. flexpriority_enabled = 0;
  6798. if (!cpu_has_virtual_nmis())
  6799. enable_vnmi = 0;
  6800. /*
  6801. * set_apic_access_page_addr() is used to reload apic access
  6802. * page upon invalidation. No need to do anything if not
  6803. * using the APIC_ACCESS_ADDR VMCS field.
  6804. */
  6805. if (!flexpriority_enabled)
  6806. kvm_x86_ops->set_apic_access_page_addr = NULL;
  6807. if (!cpu_has_vmx_tpr_shadow())
  6808. kvm_x86_ops->update_cr8_intercept = NULL;
  6809. if (enable_ept && !cpu_has_vmx_ept_2m_page())
  6810. kvm_disable_largepages();
  6811. #if IS_ENABLED(CONFIG_HYPERV)
  6812. if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
  6813. && enable_ept)
  6814. kvm_x86_ops->tlb_remote_flush = vmx_hv_remote_flush_tlb;
  6815. #endif
  6816. if (!cpu_has_vmx_ple()) {
  6817. ple_gap = 0;
  6818. ple_window = 0;
  6819. ple_window_grow = 0;
  6820. ple_window_max = 0;
  6821. ple_window_shrink = 0;
  6822. }
  6823. if (!cpu_has_vmx_apicv()) {
  6824. enable_apicv = 0;
  6825. kvm_x86_ops->sync_pir_to_irr = NULL;
  6826. }
  6827. if (cpu_has_vmx_tsc_scaling()) {
  6828. kvm_has_tsc_control = true;
  6829. kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
  6830. kvm_tsc_scaling_ratio_frac_bits = 48;
  6831. }
  6832. set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
  6833. if (enable_ept)
  6834. vmx_enable_tdp();
  6835. else
  6836. kvm_disable_tdp();
  6837. if (!nested) {
  6838. kvm_x86_ops->get_nested_state = NULL;
  6839. kvm_x86_ops->set_nested_state = NULL;
  6840. }
  6841. /*
  6842. * Only enable PML when hardware supports PML feature, and both EPT
  6843. * and EPT A/D bit features are enabled -- PML depends on them to work.
  6844. */
  6845. if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
  6846. enable_pml = 0;
  6847. if (!enable_pml) {
  6848. kvm_x86_ops->slot_enable_log_dirty = NULL;
  6849. kvm_x86_ops->slot_disable_log_dirty = NULL;
  6850. kvm_x86_ops->flush_log_dirty = NULL;
  6851. kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
  6852. }
  6853. if (!cpu_has_vmx_preemption_timer())
  6854. kvm_x86_ops->request_immediate_exit = __kvm_request_immediate_exit;
  6855. if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
  6856. u64 vmx_msr;
  6857. rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
  6858. cpu_preemption_timer_multi =
  6859. vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
  6860. } else {
  6861. kvm_x86_ops->set_hv_timer = NULL;
  6862. kvm_x86_ops->cancel_hv_timer = NULL;
  6863. }
  6864. if (!cpu_has_vmx_shadow_vmcs())
  6865. enable_shadow_vmcs = 0;
  6866. if (enable_shadow_vmcs)
  6867. init_vmcs_shadow_fields();
  6868. kvm_set_posted_intr_wakeup_handler(wakeup_handler);
  6869. nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
  6870. kvm_mce_cap_supported |= MCG_LMCE_P;
  6871. return alloc_kvm_area();
  6872. out:
  6873. for (i = 0; i < VMX_BITMAP_NR; i++)
  6874. free_page((unsigned long)vmx_bitmap[i]);
  6875. return r;
  6876. }
  6877. static __exit void hardware_unsetup(void)
  6878. {
  6879. int i;
  6880. for (i = 0; i < VMX_BITMAP_NR; i++)
  6881. free_page((unsigned long)vmx_bitmap[i]);
  6882. free_kvm_area();
  6883. }
  6884. /*
  6885. * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
  6886. * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
  6887. */
  6888. static int handle_pause(struct kvm_vcpu *vcpu)
  6889. {
  6890. if (!kvm_pause_in_guest(vcpu->kvm))
  6891. grow_ple_window(vcpu);
  6892. /*
  6893. * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
  6894. * VM-execution control is ignored if CPL > 0. OTOH, KVM
  6895. * never set PAUSE_EXITING and just set PLE if supported,
  6896. * so the vcpu must be CPL=0 if it gets a PAUSE exit.
  6897. */
  6898. kvm_vcpu_on_spin(vcpu, true);
  6899. return kvm_skip_emulated_instruction(vcpu);
  6900. }
  6901. static int handle_nop(struct kvm_vcpu *vcpu)
  6902. {
  6903. return kvm_skip_emulated_instruction(vcpu);
  6904. }
  6905. static int handle_mwait(struct kvm_vcpu *vcpu)
  6906. {
  6907. printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
  6908. return handle_nop(vcpu);
  6909. }
  6910. static int handle_invalid_op(struct kvm_vcpu *vcpu)
  6911. {
  6912. kvm_queue_exception(vcpu, UD_VECTOR);
  6913. return 1;
  6914. }
  6915. static int handle_monitor_trap(struct kvm_vcpu *vcpu)
  6916. {
  6917. return 1;
  6918. }
  6919. static int handle_monitor(struct kvm_vcpu *vcpu)
  6920. {
  6921. printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
  6922. return handle_nop(vcpu);
  6923. }
  6924. /*
  6925. * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
  6926. * set the success or error code of an emulated VMX instruction (as specified
  6927. * by Vol 2B, VMX Instruction Reference, "Conventions"), and skip the emulated
  6928. * instruction.
  6929. */
  6930. static int nested_vmx_succeed(struct kvm_vcpu *vcpu)
  6931. {
  6932. vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
  6933. & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
  6934. X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
  6935. return kvm_skip_emulated_instruction(vcpu);
  6936. }
  6937. static int nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
  6938. {
  6939. vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
  6940. & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
  6941. X86_EFLAGS_SF | X86_EFLAGS_OF))
  6942. | X86_EFLAGS_CF);
  6943. return kvm_skip_emulated_instruction(vcpu);
  6944. }
  6945. static int nested_vmx_failValid(struct kvm_vcpu *vcpu,
  6946. u32 vm_instruction_error)
  6947. {
  6948. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6949. /*
  6950. * failValid writes the error number to the current VMCS, which
  6951. * can't be done if there isn't a current VMCS.
  6952. */
  6953. if (vmx->nested.current_vmptr == -1ull && !vmx->nested.hv_evmcs)
  6954. return nested_vmx_failInvalid(vcpu);
  6955. vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
  6956. & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
  6957. X86_EFLAGS_SF | X86_EFLAGS_OF))
  6958. | X86_EFLAGS_ZF);
  6959. get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
  6960. /*
  6961. * We don't need to force a shadow sync because
  6962. * VM_INSTRUCTION_ERROR is not shadowed
  6963. */
  6964. return kvm_skip_emulated_instruction(vcpu);
  6965. }
  6966. static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
  6967. {
  6968. /* TODO: not to reset guest simply here. */
  6969. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  6970. pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
  6971. }
  6972. static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
  6973. {
  6974. struct vcpu_vmx *vmx =
  6975. container_of(timer, struct vcpu_vmx, nested.preemption_timer);
  6976. vmx->nested.preemption_timer_expired = true;
  6977. kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
  6978. kvm_vcpu_kick(&vmx->vcpu);
  6979. return HRTIMER_NORESTART;
  6980. }
  6981. /*
  6982. * Decode the memory-address operand of a vmx instruction, as recorded on an
  6983. * exit caused by such an instruction (run by a guest hypervisor).
  6984. * On success, returns 0. When the operand is invalid, returns 1 and throws
  6985. * #UD or #GP.
  6986. */
  6987. static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
  6988. unsigned long exit_qualification,
  6989. u32 vmx_instruction_info, bool wr, gva_t *ret)
  6990. {
  6991. gva_t off;
  6992. bool exn;
  6993. struct kvm_segment s;
  6994. /*
  6995. * According to Vol. 3B, "Information for VM Exits Due to Instruction
  6996. * Execution", on an exit, vmx_instruction_info holds most of the
  6997. * addressing components of the operand. Only the displacement part
  6998. * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
  6999. * For how an actual address is calculated from all these components,
  7000. * refer to Vol. 1, "Operand Addressing".
  7001. */
  7002. int scaling = vmx_instruction_info & 3;
  7003. int addr_size = (vmx_instruction_info >> 7) & 7;
  7004. bool is_reg = vmx_instruction_info & (1u << 10);
  7005. int seg_reg = (vmx_instruction_info >> 15) & 7;
  7006. int index_reg = (vmx_instruction_info >> 18) & 0xf;
  7007. bool index_is_valid = !(vmx_instruction_info & (1u << 22));
  7008. int base_reg = (vmx_instruction_info >> 23) & 0xf;
  7009. bool base_is_valid = !(vmx_instruction_info & (1u << 27));
  7010. if (is_reg) {
  7011. kvm_queue_exception(vcpu, UD_VECTOR);
  7012. return 1;
  7013. }
  7014. /* Addr = segment_base + offset */
  7015. /* offset = base + [index * scale] + displacement */
  7016. off = exit_qualification; /* holds the displacement */
  7017. if (base_is_valid)
  7018. off += kvm_register_read(vcpu, base_reg);
  7019. if (index_is_valid)
  7020. off += kvm_register_read(vcpu, index_reg)<<scaling;
  7021. vmx_get_segment(vcpu, &s, seg_reg);
  7022. *ret = s.base + off;
  7023. if (addr_size == 1) /* 32 bit */
  7024. *ret &= 0xffffffff;
  7025. /* Checks for #GP/#SS exceptions. */
  7026. exn = false;
  7027. if (is_long_mode(vcpu)) {
  7028. /* Long mode: #GP(0)/#SS(0) if the memory address is in a
  7029. * non-canonical form. This is the only check on the memory
  7030. * destination for long mode!
  7031. */
  7032. exn = is_noncanonical_address(*ret, vcpu);
  7033. } else if (is_protmode(vcpu)) {
  7034. /* Protected mode: apply checks for segment validity in the
  7035. * following order:
  7036. * - segment type check (#GP(0) may be thrown)
  7037. * - usability check (#GP(0)/#SS(0))
  7038. * - limit check (#GP(0)/#SS(0))
  7039. */
  7040. if (wr)
  7041. /* #GP(0) if the destination operand is located in a
  7042. * read-only data segment or any code segment.
  7043. */
  7044. exn = ((s.type & 0xa) == 0 || (s.type & 8));
  7045. else
  7046. /* #GP(0) if the source operand is located in an
  7047. * execute-only code segment
  7048. */
  7049. exn = ((s.type & 0xa) == 8);
  7050. if (exn) {
  7051. kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
  7052. return 1;
  7053. }
  7054. /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
  7055. */
  7056. exn = (s.unusable != 0);
  7057. /* Protected mode: #GP(0)/#SS(0) if the memory
  7058. * operand is outside the segment limit.
  7059. */
  7060. exn = exn || (off + sizeof(u64) > s.limit);
  7061. }
  7062. if (exn) {
  7063. kvm_queue_exception_e(vcpu,
  7064. seg_reg == VCPU_SREG_SS ?
  7065. SS_VECTOR : GP_VECTOR,
  7066. 0);
  7067. return 1;
  7068. }
  7069. return 0;
  7070. }
  7071. static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
  7072. {
  7073. gva_t gva;
  7074. struct x86_exception e;
  7075. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  7076. vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
  7077. return 1;
  7078. if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
  7079. kvm_inject_page_fault(vcpu, &e);
  7080. return 1;
  7081. }
  7082. return 0;
  7083. }
  7084. /*
  7085. * Allocate a shadow VMCS and associate it with the currently loaded
  7086. * VMCS, unless such a shadow VMCS already exists. The newly allocated
  7087. * VMCS is also VMCLEARed, so that it is ready for use.
  7088. */
  7089. static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
  7090. {
  7091. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7092. struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
  7093. /*
  7094. * We should allocate a shadow vmcs for vmcs01 only when L1
  7095. * executes VMXON and free it when L1 executes VMXOFF.
  7096. * As it is invalid to execute VMXON twice, we shouldn't reach
  7097. * here when vmcs01 already have an allocated shadow vmcs.
  7098. */
  7099. WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
  7100. if (!loaded_vmcs->shadow_vmcs) {
  7101. loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
  7102. if (loaded_vmcs->shadow_vmcs)
  7103. vmcs_clear(loaded_vmcs->shadow_vmcs);
  7104. }
  7105. return loaded_vmcs->shadow_vmcs;
  7106. }
  7107. static int enter_vmx_operation(struct kvm_vcpu *vcpu)
  7108. {
  7109. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7110. int r;
  7111. r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
  7112. if (r < 0)
  7113. goto out_vmcs02;
  7114. vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
  7115. if (!vmx->nested.cached_vmcs12)
  7116. goto out_cached_vmcs12;
  7117. vmx->nested.cached_shadow_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
  7118. if (!vmx->nested.cached_shadow_vmcs12)
  7119. goto out_cached_shadow_vmcs12;
  7120. if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
  7121. goto out_shadow_vmcs;
  7122. hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
  7123. HRTIMER_MODE_REL_PINNED);
  7124. vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
  7125. vmx->nested.vpid02 = allocate_vpid();
  7126. vmx->nested.vmcs02_initialized = false;
  7127. vmx->nested.vmxon = true;
  7128. return 0;
  7129. out_shadow_vmcs:
  7130. kfree(vmx->nested.cached_shadow_vmcs12);
  7131. out_cached_shadow_vmcs12:
  7132. kfree(vmx->nested.cached_vmcs12);
  7133. out_cached_vmcs12:
  7134. free_loaded_vmcs(&vmx->nested.vmcs02);
  7135. out_vmcs02:
  7136. return -ENOMEM;
  7137. }
  7138. /*
  7139. * Emulate the VMXON instruction.
  7140. * Currently, we just remember that VMX is active, and do not save or even
  7141. * inspect the argument to VMXON (the so-called "VMXON pointer") because we
  7142. * do not currently need to store anything in that guest-allocated memory
  7143. * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
  7144. * argument is different from the VMXON pointer (which the spec says they do).
  7145. */
  7146. static int handle_vmon(struct kvm_vcpu *vcpu)
  7147. {
  7148. int ret;
  7149. gpa_t vmptr;
  7150. struct page *page;
  7151. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7152. const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
  7153. | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  7154. /*
  7155. * The Intel VMX Instruction Reference lists a bunch of bits that are
  7156. * prerequisite to running VMXON, most notably cr4.VMXE must be set to
  7157. * 1 (see vmx_set_cr4() for when we allow the guest to set this).
  7158. * Otherwise, we should fail with #UD. But most faulting conditions
  7159. * have already been checked by hardware, prior to the VM-exit for
  7160. * VMXON. We do test guest cr4.VMXE because processor CR4 always has
  7161. * that bit set to 1 in non-root mode.
  7162. */
  7163. if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
  7164. kvm_queue_exception(vcpu, UD_VECTOR);
  7165. return 1;
  7166. }
  7167. /* CPL=0 must be checked manually. */
  7168. if (vmx_get_cpl(vcpu)) {
  7169. kvm_inject_gp(vcpu, 0);
  7170. return 1;
  7171. }
  7172. if (vmx->nested.vmxon)
  7173. return nested_vmx_failValid(vcpu,
  7174. VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
  7175. if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
  7176. != VMXON_NEEDED_FEATURES) {
  7177. kvm_inject_gp(vcpu, 0);
  7178. return 1;
  7179. }
  7180. if (nested_vmx_get_vmptr(vcpu, &vmptr))
  7181. return 1;
  7182. /*
  7183. * SDM 3: 24.11.5
  7184. * The first 4 bytes of VMXON region contain the supported
  7185. * VMCS revision identifier
  7186. *
  7187. * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
  7188. * which replaces physical address width with 32
  7189. */
  7190. if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu)))
  7191. return nested_vmx_failInvalid(vcpu);
  7192. page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
  7193. if (is_error_page(page))
  7194. return nested_vmx_failInvalid(vcpu);
  7195. if (*(u32 *)kmap(page) != VMCS12_REVISION) {
  7196. kunmap(page);
  7197. kvm_release_page_clean(page);
  7198. return nested_vmx_failInvalid(vcpu);
  7199. }
  7200. kunmap(page);
  7201. kvm_release_page_clean(page);
  7202. vmx->nested.vmxon_ptr = vmptr;
  7203. ret = enter_vmx_operation(vcpu);
  7204. if (ret)
  7205. return ret;
  7206. return nested_vmx_succeed(vcpu);
  7207. }
  7208. /*
  7209. * Intel's VMX Instruction Reference specifies a common set of prerequisites
  7210. * for running VMX instructions (except VMXON, whose prerequisites are
  7211. * slightly different). It also specifies what exception to inject otherwise.
  7212. * Note that many of these exceptions have priority over VM exits, so they
  7213. * don't have to be checked again here.
  7214. */
  7215. static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
  7216. {
  7217. if (!to_vmx(vcpu)->nested.vmxon) {
  7218. kvm_queue_exception(vcpu, UD_VECTOR);
  7219. return 0;
  7220. }
  7221. if (vmx_get_cpl(vcpu)) {
  7222. kvm_inject_gp(vcpu, 0);
  7223. return 0;
  7224. }
  7225. return 1;
  7226. }
  7227. static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
  7228. {
  7229. vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
  7230. vmcs_write64(VMCS_LINK_POINTER, -1ull);
  7231. }
  7232. static inline void nested_release_evmcs(struct kvm_vcpu *vcpu)
  7233. {
  7234. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7235. if (!vmx->nested.hv_evmcs)
  7236. return;
  7237. kunmap(vmx->nested.hv_evmcs_page);
  7238. kvm_release_page_dirty(vmx->nested.hv_evmcs_page);
  7239. vmx->nested.hv_evmcs_vmptr = -1ull;
  7240. vmx->nested.hv_evmcs_page = NULL;
  7241. vmx->nested.hv_evmcs = NULL;
  7242. }
  7243. static inline void nested_release_vmcs12(struct kvm_vcpu *vcpu)
  7244. {
  7245. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7246. if (vmx->nested.current_vmptr == -1ull)
  7247. return;
  7248. if (enable_shadow_vmcs) {
  7249. /* copy to memory all shadowed fields in case
  7250. they were modified */
  7251. copy_shadow_to_vmcs12(vmx);
  7252. vmx->nested.need_vmcs12_sync = false;
  7253. vmx_disable_shadow_vmcs(vmx);
  7254. }
  7255. vmx->nested.posted_intr_nv = -1;
  7256. /* Flush VMCS12 to guest memory */
  7257. kvm_vcpu_write_guest_page(vcpu,
  7258. vmx->nested.current_vmptr >> PAGE_SHIFT,
  7259. vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
  7260. kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
  7261. vmx->nested.current_vmptr = -1ull;
  7262. }
  7263. /*
  7264. * Free whatever needs to be freed from vmx->nested when L1 goes down, or
  7265. * just stops using VMX.
  7266. */
  7267. static void free_nested(struct kvm_vcpu *vcpu)
  7268. {
  7269. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7270. if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
  7271. return;
  7272. vmx->nested.vmxon = false;
  7273. vmx->nested.smm.vmxon = false;
  7274. free_vpid(vmx->nested.vpid02);
  7275. vmx->nested.posted_intr_nv = -1;
  7276. vmx->nested.current_vmptr = -1ull;
  7277. if (enable_shadow_vmcs) {
  7278. vmx_disable_shadow_vmcs(vmx);
  7279. vmcs_clear(vmx->vmcs01.shadow_vmcs);
  7280. free_vmcs(vmx->vmcs01.shadow_vmcs);
  7281. vmx->vmcs01.shadow_vmcs = NULL;
  7282. }
  7283. kfree(vmx->nested.cached_vmcs12);
  7284. kfree(vmx->nested.cached_shadow_vmcs12);
  7285. /* Unpin physical memory we referred to in the vmcs02 */
  7286. if (vmx->nested.apic_access_page) {
  7287. kvm_release_page_dirty(vmx->nested.apic_access_page);
  7288. vmx->nested.apic_access_page = NULL;
  7289. }
  7290. if (vmx->nested.virtual_apic_page) {
  7291. kvm_release_page_dirty(vmx->nested.virtual_apic_page);
  7292. vmx->nested.virtual_apic_page = NULL;
  7293. }
  7294. if (vmx->nested.pi_desc_page) {
  7295. kunmap(vmx->nested.pi_desc_page);
  7296. kvm_release_page_dirty(vmx->nested.pi_desc_page);
  7297. vmx->nested.pi_desc_page = NULL;
  7298. vmx->nested.pi_desc = NULL;
  7299. }
  7300. kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
  7301. nested_release_evmcs(vcpu);
  7302. free_loaded_vmcs(&vmx->nested.vmcs02);
  7303. }
  7304. /* Emulate the VMXOFF instruction */
  7305. static int handle_vmoff(struct kvm_vcpu *vcpu)
  7306. {
  7307. if (!nested_vmx_check_permission(vcpu))
  7308. return 1;
  7309. free_nested(vcpu);
  7310. return nested_vmx_succeed(vcpu);
  7311. }
  7312. /* Emulate the VMCLEAR instruction */
  7313. static int handle_vmclear(struct kvm_vcpu *vcpu)
  7314. {
  7315. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7316. u32 zero = 0;
  7317. gpa_t vmptr;
  7318. if (!nested_vmx_check_permission(vcpu))
  7319. return 1;
  7320. if (nested_vmx_get_vmptr(vcpu, &vmptr))
  7321. return 1;
  7322. if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu)))
  7323. return nested_vmx_failValid(vcpu,
  7324. VMXERR_VMCLEAR_INVALID_ADDRESS);
  7325. if (vmptr == vmx->nested.vmxon_ptr)
  7326. return nested_vmx_failValid(vcpu,
  7327. VMXERR_VMCLEAR_VMXON_POINTER);
  7328. if (vmx->nested.hv_evmcs_page) {
  7329. if (vmptr == vmx->nested.hv_evmcs_vmptr)
  7330. nested_release_evmcs(vcpu);
  7331. } else {
  7332. if (vmptr == vmx->nested.current_vmptr)
  7333. nested_release_vmcs12(vcpu);
  7334. kvm_vcpu_write_guest(vcpu,
  7335. vmptr + offsetof(struct vmcs12,
  7336. launch_state),
  7337. &zero, sizeof(zero));
  7338. }
  7339. return nested_vmx_succeed(vcpu);
  7340. }
  7341. static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
  7342. /* Emulate the VMLAUNCH instruction */
  7343. static int handle_vmlaunch(struct kvm_vcpu *vcpu)
  7344. {
  7345. return nested_vmx_run(vcpu, true);
  7346. }
  7347. /* Emulate the VMRESUME instruction */
  7348. static int handle_vmresume(struct kvm_vcpu *vcpu)
  7349. {
  7350. return nested_vmx_run(vcpu, false);
  7351. }
  7352. /*
  7353. * Read a vmcs12 field. Since these can have varying lengths and we return
  7354. * one type, we chose the biggest type (u64) and zero-extend the return value
  7355. * to that size. Note that the caller, handle_vmread, might need to use only
  7356. * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
  7357. * 64-bit fields are to be returned).
  7358. */
  7359. static inline int vmcs12_read_any(struct vmcs12 *vmcs12,
  7360. unsigned long field, u64 *ret)
  7361. {
  7362. short offset = vmcs_field_to_offset(field);
  7363. char *p;
  7364. if (offset < 0)
  7365. return offset;
  7366. p = (char *)vmcs12 + offset;
  7367. switch (vmcs_field_width(field)) {
  7368. case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
  7369. *ret = *((natural_width *)p);
  7370. return 0;
  7371. case VMCS_FIELD_WIDTH_U16:
  7372. *ret = *((u16 *)p);
  7373. return 0;
  7374. case VMCS_FIELD_WIDTH_U32:
  7375. *ret = *((u32 *)p);
  7376. return 0;
  7377. case VMCS_FIELD_WIDTH_U64:
  7378. *ret = *((u64 *)p);
  7379. return 0;
  7380. default:
  7381. WARN_ON(1);
  7382. return -ENOENT;
  7383. }
  7384. }
  7385. static inline int vmcs12_write_any(struct vmcs12 *vmcs12,
  7386. unsigned long field, u64 field_value){
  7387. short offset = vmcs_field_to_offset(field);
  7388. char *p = (char *)vmcs12 + offset;
  7389. if (offset < 0)
  7390. return offset;
  7391. switch (vmcs_field_width(field)) {
  7392. case VMCS_FIELD_WIDTH_U16:
  7393. *(u16 *)p = field_value;
  7394. return 0;
  7395. case VMCS_FIELD_WIDTH_U32:
  7396. *(u32 *)p = field_value;
  7397. return 0;
  7398. case VMCS_FIELD_WIDTH_U64:
  7399. *(u64 *)p = field_value;
  7400. return 0;
  7401. case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
  7402. *(natural_width *)p = field_value;
  7403. return 0;
  7404. default:
  7405. WARN_ON(1);
  7406. return -ENOENT;
  7407. }
  7408. }
  7409. static int copy_enlightened_to_vmcs12(struct vcpu_vmx *vmx)
  7410. {
  7411. struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
  7412. struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
  7413. /* HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE */
  7414. vmcs12->tpr_threshold = evmcs->tpr_threshold;
  7415. vmcs12->guest_rip = evmcs->guest_rip;
  7416. if (unlikely(!(evmcs->hv_clean_fields &
  7417. HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC))) {
  7418. vmcs12->guest_rsp = evmcs->guest_rsp;
  7419. vmcs12->guest_rflags = evmcs->guest_rflags;
  7420. vmcs12->guest_interruptibility_info =
  7421. evmcs->guest_interruptibility_info;
  7422. }
  7423. if (unlikely(!(evmcs->hv_clean_fields &
  7424. HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC))) {
  7425. vmcs12->cpu_based_vm_exec_control =
  7426. evmcs->cpu_based_vm_exec_control;
  7427. }
  7428. if (unlikely(!(evmcs->hv_clean_fields &
  7429. HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC))) {
  7430. vmcs12->exception_bitmap = evmcs->exception_bitmap;
  7431. }
  7432. if (unlikely(!(evmcs->hv_clean_fields &
  7433. HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_ENTRY))) {
  7434. vmcs12->vm_entry_controls = evmcs->vm_entry_controls;
  7435. }
  7436. if (unlikely(!(evmcs->hv_clean_fields &
  7437. HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT))) {
  7438. vmcs12->vm_entry_intr_info_field =
  7439. evmcs->vm_entry_intr_info_field;
  7440. vmcs12->vm_entry_exception_error_code =
  7441. evmcs->vm_entry_exception_error_code;
  7442. vmcs12->vm_entry_instruction_len =
  7443. evmcs->vm_entry_instruction_len;
  7444. }
  7445. if (unlikely(!(evmcs->hv_clean_fields &
  7446. HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1))) {
  7447. vmcs12->host_ia32_pat = evmcs->host_ia32_pat;
  7448. vmcs12->host_ia32_efer = evmcs->host_ia32_efer;
  7449. vmcs12->host_cr0 = evmcs->host_cr0;
  7450. vmcs12->host_cr3 = evmcs->host_cr3;
  7451. vmcs12->host_cr4 = evmcs->host_cr4;
  7452. vmcs12->host_ia32_sysenter_esp = evmcs->host_ia32_sysenter_esp;
  7453. vmcs12->host_ia32_sysenter_eip = evmcs->host_ia32_sysenter_eip;
  7454. vmcs12->host_rip = evmcs->host_rip;
  7455. vmcs12->host_ia32_sysenter_cs = evmcs->host_ia32_sysenter_cs;
  7456. vmcs12->host_es_selector = evmcs->host_es_selector;
  7457. vmcs12->host_cs_selector = evmcs->host_cs_selector;
  7458. vmcs12->host_ss_selector = evmcs->host_ss_selector;
  7459. vmcs12->host_ds_selector = evmcs->host_ds_selector;
  7460. vmcs12->host_fs_selector = evmcs->host_fs_selector;
  7461. vmcs12->host_gs_selector = evmcs->host_gs_selector;
  7462. vmcs12->host_tr_selector = evmcs->host_tr_selector;
  7463. }
  7464. if (unlikely(!(evmcs->hv_clean_fields &
  7465. HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1))) {
  7466. vmcs12->pin_based_vm_exec_control =
  7467. evmcs->pin_based_vm_exec_control;
  7468. vmcs12->vm_exit_controls = evmcs->vm_exit_controls;
  7469. vmcs12->secondary_vm_exec_control =
  7470. evmcs->secondary_vm_exec_control;
  7471. }
  7472. if (unlikely(!(evmcs->hv_clean_fields &
  7473. HV_VMX_ENLIGHTENED_CLEAN_FIELD_IO_BITMAP))) {
  7474. vmcs12->io_bitmap_a = evmcs->io_bitmap_a;
  7475. vmcs12->io_bitmap_b = evmcs->io_bitmap_b;
  7476. }
  7477. if (unlikely(!(evmcs->hv_clean_fields &
  7478. HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP))) {
  7479. vmcs12->msr_bitmap = evmcs->msr_bitmap;
  7480. }
  7481. if (unlikely(!(evmcs->hv_clean_fields &
  7482. HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2))) {
  7483. vmcs12->guest_es_base = evmcs->guest_es_base;
  7484. vmcs12->guest_cs_base = evmcs->guest_cs_base;
  7485. vmcs12->guest_ss_base = evmcs->guest_ss_base;
  7486. vmcs12->guest_ds_base = evmcs->guest_ds_base;
  7487. vmcs12->guest_fs_base = evmcs->guest_fs_base;
  7488. vmcs12->guest_gs_base = evmcs->guest_gs_base;
  7489. vmcs12->guest_ldtr_base = evmcs->guest_ldtr_base;
  7490. vmcs12->guest_tr_base = evmcs->guest_tr_base;
  7491. vmcs12->guest_gdtr_base = evmcs->guest_gdtr_base;
  7492. vmcs12->guest_idtr_base = evmcs->guest_idtr_base;
  7493. vmcs12->guest_es_limit = evmcs->guest_es_limit;
  7494. vmcs12->guest_cs_limit = evmcs->guest_cs_limit;
  7495. vmcs12->guest_ss_limit = evmcs->guest_ss_limit;
  7496. vmcs12->guest_ds_limit = evmcs->guest_ds_limit;
  7497. vmcs12->guest_fs_limit = evmcs->guest_fs_limit;
  7498. vmcs12->guest_gs_limit = evmcs->guest_gs_limit;
  7499. vmcs12->guest_ldtr_limit = evmcs->guest_ldtr_limit;
  7500. vmcs12->guest_tr_limit = evmcs->guest_tr_limit;
  7501. vmcs12->guest_gdtr_limit = evmcs->guest_gdtr_limit;
  7502. vmcs12->guest_idtr_limit = evmcs->guest_idtr_limit;
  7503. vmcs12->guest_es_ar_bytes = evmcs->guest_es_ar_bytes;
  7504. vmcs12->guest_cs_ar_bytes = evmcs->guest_cs_ar_bytes;
  7505. vmcs12->guest_ss_ar_bytes = evmcs->guest_ss_ar_bytes;
  7506. vmcs12->guest_ds_ar_bytes = evmcs->guest_ds_ar_bytes;
  7507. vmcs12->guest_fs_ar_bytes = evmcs->guest_fs_ar_bytes;
  7508. vmcs12->guest_gs_ar_bytes = evmcs->guest_gs_ar_bytes;
  7509. vmcs12->guest_ldtr_ar_bytes = evmcs->guest_ldtr_ar_bytes;
  7510. vmcs12->guest_tr_ar_bytes = evmcs->guest_tr_ar_bytes;
  7511. vmcs12->guest_es_selector = evmcs->guest_es_selector;
  7512. vmcs12->guest_cs_selector = evmcs->guest_cs_selector;
  7513. vmcs12->guest_ss_selector = evmcs->guest_ss_selector;
  7514. vmcs12->guest_ds_selector = evmcs->guest_ds_selector;
  7515. vmcs12->guest_fs_selector = evmcs->guest_fs_selector;
  7516. vmcs12->guest_gs_selector = evmcs->guest_gs_selector;
  7517. vmcs12->guest_ldtr_selector = evmcs->guest_ldtr_selector;
  7518. vmcs12->guest_tr_selector = evmcs->guest_tr_selector;
  7519. }
  7520. if (unlikely(!(evmcs->hv_clean_fields &
  7521. HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2))) {
  7522. vmcs12->tsc_offset = evmcs->tsc_offset;
  7523. vmcs12->virtual_apic_page_addr = evmcs->virtual_apic_page_addr;
  7524. vmcs12->xss_exit_bitmap = evmcs->xss_exit_bitmap;
  7525. }
  7526. if (unlikely(!(evmcs->hv_clean_fields &
  7527. HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR))) {
  7528. vmcs12->cr0_guest_host_mask = evmcs->cr0_guest_host_mask;
  7529. vmcs12->cr4_guest_host_mask = evmcs->cr4_guest_host_mask;
  7530. vmcs12->cr0_read_shadow = evmcs->cr0_read_shadow;
  7531. vmcs12->cr4_read_shadow = evmcs->cr4_read_shadow;
  7532. vmcs12->guest_cr0 = evmcs->guest_cr0;
  7533. vmcs12->guest_cr3 = evmcs->guest_cr3;
  7534. vmcs12->guest_cr4 = evmcs->guest_cr4;
  7535. vmcs12->guest_dr7 = evmcs->guest_dr7;
  7536. }
  7537. if (unlikely(!(evmcs->hv_clean_fields &
  7538. HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER))) {
  7539. vmcs12->host_fs_base = evmcs->host_fs_base;
  7540. vmcs12->host_gs_base = evmcs->host_gs_base;
  7541. vmcs12->host_tr_base = evmcs->host_tr_base;
  7542. vmcs12->host_gdtr_base = evmcs->host_gdtr_base;
  7543. vmcs12->host_idtr_base = evmcs->host_idtr_base;
  7544. vmcs12->host_rsp = evmcs->host_rsp;
  7545. }
  7546. if (unlikely(!(evmcs->hv_clean_fields &
  7547. HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_XLAT))) {
  7548. vmcs12->ept_pointer = evmcs->ept_pointer;
  7549. vmcs12->virtual_processor_id = evmcs->virtual_processor_id;
  7550. }
  7551. if (unlikely(!(evmcs->hv_clean_fields &
  7552. HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1))) {
  7553. vmcs12->vmcs_link_pointer = evmcs->vmcs_link_pointer;
  7554. vmcs12->guest_ia32_debugctl = evmcs->guest_ia32_debugctl;
  7555. vmcs12->guest_ia32_pat = evmcs->guest_ia32_pat;
  7556. vmcs12->guest_ia32_efer = evmcs->guest_ia32_efer;
  7557. vmcs12->guest_pdptr0 = evmcs->guest_pdptr0;
  7558. vmcs12->guest_pdptr1 = evmcs->guest_pdptr1;
  7559. vmcs12->guest_pdptr2 = evmcs->guest_pdptr2;
  7560. vmcs12->guest_pdptr3 = evmcs->guest_pdptr3;
  7561. vmcs12->guest_pending_dbg_exceptions =
  7562. evmcs->guest_pending_dbg_exceptions;
  7563. vmcs12->guest_sysenter_esp = evmcs->guest_sysenter_esp;
  7564. vmcs12->guest_sysenter_eip = evmcs->guest_sysenter_eip;
  7565. vmcs12->guest_bndcfgs = evmcs->guest_bndcfgs;
  7566. vmcs12->guest_activity_state = evmcs->guest_activity_state;
  7567. vmcs12->guest_sysenter_cs = evmcs->guest_sysenter_cs;
  7568. }
  7569. /*
  7570. * Not used?
  7571. * vmcs12->vm_exit_msr_store_addr = evmcs->vm_exit_msr_store_addr;
  7572. * vmcs12->vm_exit_msr_load_addr = evmcs->vm_exit_msr_load_addr;
  7573. * vmcs12->vm_entry_msr_load_addr = evmcs->vm_entry_msr_load_addr;
  7574. * vmcs12->cr3_target_value0 = evmcs->cr3_target_value0;
  7575. * vmcs12->cr3_target_value1 = evmcs->cr3_target_value1;
  7576. * vmcs12->cr3_target_value2 = evmcs->cr3_target_value2;
  7577. * vmcs12->cr3_target_value3 = evmcs->cr3_target_value3;
  7578. * vmcs12->page_fault_error_code_mask =
  7579. * evmcs->page_fault_error_code_mask;
  7580. * vmcs12->page_fault_error_code_match =
  7581. * evmcs->page_fault_error_code_match;
  7582. * vmcs12->cr3_target_count = evmcs->cr3_target_count;
  7583. * vmcs12->vm_exit_msr_store_count = evmcs->vm_exit_msr_store_count;
  7584. * vmcs12->vm_exit_msr_load_count = evmcs->vm_exit_msr_load_count;
  7585. * vmcs12->vm_entry_msr_load_count = evmcs->vm_entry_msr_load_count;
  7586. */
  7587. /*
  7588. * Read only fields:
  7589. * vmcs12->guest_physical_address = evmcs->guest_physical_address;
  7590. * vmcs12->vm_instruction_error = evmcs->vm_instruction_error;
  7591. * vmcs12->vm_exit_reason = evmcs->vm_exit_reason;
  7592. * vmcs12->vm_exit_intr_info = evmcs->vm_exit_intr_info;
  7593. * vmcs12->vm_exit_intr_error_code = evmcs->vm_exit_intr_error_code;
  7594. * vmcs12->idt_vectoring_info_field = evmcs->idt_vectoring_info_field;
  7595. * vmcs12->idt_vectoring_error_code = evmcs->idt_vectoring_error_code;
  7596. * vmcs12->vm_exit_instruction_len = evmcs->vm_exit_instruction_len;
  7597. * vmcs12->vmx_instruction_info = evmcs->vmx_instruction_info;
  7598. * vmcs12->exit_qualification = evmcs->exit_qualification;
  7599. * vmcs12->guest_linear_address = evmcs->guest_linear_address;
  7600. *
  7601. * Not present in struct vmcs12:
  7602. * vmcs12->exit_io_instruction_ecx = evmcs->exit_io_instruction_ecx;
  7603. * vmcs12->exit_io_instruction_esi = evmcs->exit_io_instruction_esi;
  7604. * vmcs12->exit_io_instruction_edi = evmcs->exit_io_instruction_edi;
  7605. * vmcs12->exit_io_instruction_eip = evmcs->exit_io_instruction_eip;
  7606. */
  7607. return 0;
  7608. }
  7609. static int copy_vmcs12_to_enlightened(struct vcpu_vmx *vmx)
  7610. {
  7611. struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
  7612. struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
  7613. /*
  7614. * Should not be changed by KVM:
  7615. *
  7616. * evmcs->host_es_selector = vmcs12->host_es_selector;
  7617. * evmcs->host_cs_selector = vmcs12->host_cs_selector;
  7618. * evmcs->host_ss_selector = vmcs12->host_ss_selector;
  7619. * evmcs->host_ds_selector = vmcs12->host_ds_selector;
  7620. * evmcs->host_fs_selector = vmcs12->host_fs_selector;
  7621. * evmcs->host_gs_selector = vmcs12->host_gs_selector;
  7622. * evmcs->host_tr_selector = vmcs12->host_tr_selector;
  7623. * evmcs->host_ia32_pat = vmcs12->host_ia32_pat;
  7624. * evmcs->host_ia32_efer = vmcs12->host_ia32_efer;
  7625. * evmcs->host_cr0 = vmcs12->host_cr0;
  7626. * evmcs->host_cr3 = vmcs12->host_cr3;
  7627. * evmcs->host_cr4 = vmcs12->host_cr4;
  7628. * evmcs->host_ia32_sysenter_esp = vmcs12->host_ia32_sysenter_esp;
  7629. * evmcs->host_ia32_sysenter_eip = vmcs12->host_ia32_sysenter_eip;
  7630. * evmcs->host_rip = vmcs12->host_rip;
  7631. * evmcs->host_ia32_sysenter_cs = vmcs12->host_ia32_sysenter_cs;
  7632. * evmcs->host_fs_base = vmcs12->host_fs_base;
  7633. * evmcs->host_gs_base = vmcs12->host_gs_base;
  7634. * evmcs->host_tr_base = vmcs12->host_tr_base;
  7635. * evmcs->host_gdtr_base = vmcs12->host_gdtr_base;
  7636. * evmcs->host_idtr_base = vmcs12->host_idtr_base;
  7637. * evmcs->host_rsp = vmcs12->host_rsp;
  7638. * sync_vmcs12() doesn't read these:
  7639. * evmcs->io_bitmap_a = vmcs12->io_bitmap_a;
  7640. * evmcs->io_bitmap_b = vmcs12->io_bitmap_b;
  7641. * evmcs->msr_bitmap = vmcs12->msr_bitmap;
  7642. * evmcs->ept_pointer = vmcs12->ept_pointer;
  7643. * evmcs->xss_exit_bitmap = vmcs12->xss_exit_bitmap;
  7644. * evmcs->vm_exit_msr_store_addr = vmcs12->vm_exit_msr_store_addr;
  7645. * evmcs->vm_exit_msr_load_addr = vmcs12->vm_exit_msr_load_addr;
  7646. * evmcs->vm_entry_msr_load_addr = vmcs12->vm_entry_msr_load_addr;
  7647. * evmcs->cr3_target_value0 = vmcs12->cr3_target_value0;
  7648. * evmcs->cr3_target_value1 = vmcs12->cr3_target_value1;
  7649. * evmcs->cr3_target_value2 = vmcs12->cr3_target_value2;
  7650. * evmcs->cr3_target_value3 = vmcs12->cr3_target_value3;
  7651. * evmcs->tpr_threshold = vmcs12->tpr_threshold;
  7652. * evmcs->virtual_processor_id = vmcs12->virtual_processor_id;
  7653. * evmcs->exception_bitmap = vmcs12->exception_bitmap;
  7654. * evmcs->vmcs_link_pointer = vmcs12->vmcs_link_pointer;
  7655. * evmcs->pin_based_vm_exec_control = vmcs12->pin_based_vm_exec_control;
  7656. * evmcs->vm_exit_controls = vmcs12->vm_exit_controls;
  7657. * evmcs->secondary_vm_exec_control = vmcs12->secondary_vm_exec_control;
  7658. * evmcs->page_fault_error_code_mask =
  7659. * vmcs12->page_fault_error_code_mask;
  7660. * evmcs->page_fault_error_code_match =
  7661. * vmcs12->page_fault_error_code_match;
  7662. * evmcs->cr3_target_count = vmcs12->cr3_target_count;
  7663. * evmcs->virtual_apic_page_addr = vmcs12->virtual_apic_page_addr;
  7664. * evmcs->tsc_offset = vmcs12->tsc_offset;
  7665. * evmcs->guest_ia32_debugctl = vmcs12->guest_ia32_debugctl;
  7666. * evmcs->cr0_guest_host_mask = vmcs12->cr0_guest_host_mask;
  7667. * evmcs->cr4_guest_host_mask = vmcs12->cr4_guest_host_mask;
  7668. * evmcs->cr0_read_shadow = vmcs12->cr0_read_shadow;
  7669. * evmcs->cr4_read_shadow = vmcs12->cr4_read_shadow;
  7670. * evmcs->vm_exit_msr_store_count = vmcs12->vm_exit_msr_store_count;
  7671. * evmcs->vm_exit_msr_load_count = vmcs12->vm_exit_msr_load_count;
  7672. * evmcs->vm_entry_msr_load_count = vmcs12->vm_entry_msr_load_count;
  7673. *
  7674. * Not present in struct vmcs12:
  7675. * evmcs->exit_io_instruction_ecx = vmcs12->exit_io_instruction_ecx;
  7676. * evmcs->exit_io_instruction_esi = vmcs12->exit_io_instruction_esi;
  7677. * evmcs->exit_io_instruction_edi = vmcs12->exit_io_instruction_edi;
  7678. * evmcs->exit_io_instruction_eip = vmcs12->exit_io_instruction_eip;
  7679. */
  7680. evmcs->guest_es_selector = vmcs12->guest_es_selector;
  7681. evmcs->guest_cs_selector = vmcs12->guest_cs_selector;
  7682. evmcs->guest_ss_selector = vmcs12->guest_ss_selector;
  7683. evmcs->guest_ds_selector = vmcs12->guest_ds_selector;
  7684. evmcs->guest_fs_selector = vmcs12->guest_fs_selector;
  7685. evmcs->guest_gs_selector = vmcs12->guest_gs_selector;
  7686. evmcs->guest_ldtr_selector = vmcs12->guest_ldtr_selector;
  7687. evmcs->guest_tr_selector = vmcs12->guest_tr_selector;
  7688. evmcs->guest_es_limit = vmcs12->guest_es_limit;
  7689. evmcs->guest_cs_limit = vmcs12->guest_cs_limit;
  7690. evmcs->guest_ss_limit = vmcs12->guest_ss_limit;
  7691. evmcs->guest_ds_limit = vmcs12->guest_ds_limit;
  7692. evmcs->guest_fs_limit = vmcs12->guest_fs_limit;
  7693. evmcs->guest_gs_limit = vmcs12->guest_gs_limit;
  7694. evmcs->guest_ldtr_limit = vmcs12->guest_ldtr_limit;
  7695. evmcs->guest_tr_limit = vmcs12->guest_tr_limit;
  7696. evmcs->guest_gdtr_limit = vmcs12->guest_gdtr_limit;
  7697. evmcs->guest_idtr_limit = vmcs12->guest_idtr_limit;
  7698. evmcs->guest_es_ar_bytes = vmcs12->guest_es_ar_bytes;
  7699. evmcs->guest_cs_ar_bytes = vmcs12->guest_cs_ar_bytes;
  7700. evmcs->guest_ss_ar_bytes = vmcs12->guest_ss_ar_bytes;
  7701. evmcs->guest_ds_ar_bytes = vmcs12->guest_ds_ar_bytes;
  7702. evmcs->guest_fs_ar_bytes = vmcs12->guest_fs_ar_bytes;
  7703. evmcs->guest_gs_ar_bytes = vmcs12->guest_gs_ar_bytes;
  7704. evmcs->guest_ldtr_ar_bytes = vmcs12->guest_ldtr_ar_bytes;
  7705. evmcs->guest_tr_ar_bytes = vmcs12->guest_tr_ar_bytes;
  7706. evmcs->guest_es_base = vmcs12->guest_es_base;
  7707. evmcs->guest_cs_base = vmcs12->guest_cs_base;
  7708. evmcs->guest_ss_base = vmcs12->guest_ss_base;
  7709. evmcs->guest_ds_base = vmcs12->guest_ds_base;
  7710. evmcs->guest_fs_base = vmcs12->guest_fs_base;
  7711. evmcs->guest_gs_base = vmcs12->guest_gs_base;
  7712. evmcs->guest_ldtr_base = vmcs12->guest_ldtr_base;
  7713. evmcs->guest_tr_base = vmcs12->guest_tr_base;
  7714. evmcs->guest_gdtr_base = vmcs12->guest_gdtr_base;
  7715. evmcs->guest_idtr_base = vmcs12->guest_idtr_base;
  7716. evmcs->guest_ia32_pat = vmcs12->guest_ia32_pat;
  7717. evmcs->guest_ia32_efer = vmcs12->guest_ia32_efer;
  7718. evmcs->guest_pdptr0 = vmcs12->guest_pdptr0;
  7719. evmcs->guest_pdptr1 = vmcs12->guest_pdptr1;
  7720. evmcs->guest_pdptr2 = vmcs12->guest_pdptr2;
  7721. evmcs->guest_pdptr3 = vmcs12->guest_pdptr3;
  7722. evmcs->guest_pending_dbg_exceptions =
  7723. vmcs12->guest_pending_dbg_exceptions;
  7724. evmcs->guest_sysenter_esp = vmcs12->guest_sysenter_esp;
  7725. evmcs->guest_sysenter_eip = vmcs12->guest_sysenter_eip;
  7726. evmcs->guest_activity_state = vmcs12->guest_activity_state;
  7727. evmcs->guest_sysenter_cs = vmcs12->guest_sysenter_cs;
  7728. evmcs->guest_cr0 = vmcs12->guest_cr0;
  7729. evmcs->guest_cr3 = vmcs12->guest_cr3;
  7730. evmcs->guest_cr4 = vmcs12->guest_cr4;
  7731. evmcs->guest_dr7 = vmcs12->guest_dr7;
  7732. evmcs->guest_physical_address = vmcs12->guest_physical_address;
  7733. evmcs->vm_instruction_error = vmcs12->vm_instruction_error;
  7734. evmcs->vm_exit_reason = vmcs12->vm_exit_reason;
  7735. evmcs->vm_exit_intr_info = vmcs12->vm_exit_intr_info;
  7736. evmcs->vm_exit_intr_error_code = vmcs12->vm_exit_intr_error_code;
  7737. evmcs->idt_vectoring_info_field = vmcs12->idt_vectoring_info_field;
  7738. evmcs->idt_vectoring_error_code = vmcs12->idt_vectoring_error_code;
  7739. evmcs->vm_exit_instruction_len = vmcs12->vm_exit_instruction_len;
  7740. evmcs->vmx_instruction_info = vmcs12->vmx_instruction_info;
  7741. evmcs->exit_qualification = vmcs12->exit_qualification;
  7742. evmcs->guest_linear_address = vmcs12->guest_linear_address;
  7743. evmcs->guest_rsp = vmcs12->guest_rsp;
  7744. evmcs->guest_rflags = vmcs12->guest_rflags;
  7745. evmcs->guest_interruptibility_info =
  7746. vmcs12->guest_interruptibility_info;
  7747. evmcs->cpu_based_vm_exec_control = vmcs12->cpu_based_vm_exec_control;
  7748. evmcs->vm_entry_controls = vmcs12->vm_entry_controls;
  7749. evmcs->vm_entry_intr_info_field = vmcs12->vm_entry_intr_info_field;
  7750. evmcs->vm_entry_exception_error_code =
  7751. vmcs12->vm_entry_exception_error_code;
  7752. evmcs->vm_entry_instruction_len = vmcs12->vm_entry_instruction_len;
  7753. evmcs->guest_rip = vmcs12->guest_rip;
  7754. evmcs->guest_bndcfgs = vmcs12->guest_bndcfgs;
  7755. return 0;
  7756. }
  7757. /*
  7758. * Copy the writable VMCS shadow fields back to the VMCS12, in case
  7759. * they have been modified by the L1 guest. Note that the "read-only"
  7760. * VM-exit information fields are actually writable if the vCPU is
  7761. * configured to support "VMWRITE to any supported field in the VMCS."
  7762. */
  7763. static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
  7764. {
  7765. const u16 *fields[] = {
  7766. shadow_read_write_fields,
  7767. shadow_read_only_fields
  7768. };
  7769. const int max_fields[] = {
  7770. max_shadow_read_write_fields,
  7771. max_shadow_read_only_fields
  7772. };
  7773. int i, q;
  7774. unsigned long field;
  7775. u64 field_value;
  7776. struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
  7777. preempt_disable();
  7778. vmcs_load(shadow_vmcs);
  7779. for (q = 0; q < ARRAY_SIZE(fields); q++) {
  7780. for (i = 0; i < max_fields[q]; i++) {
  7781. field = fields[q][i];
  7782. field_value = __vmcs_readl(field);
  7783. vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
  7784. }
  7785. /*
  7786. * Skip the VM-exit information fields if they are read-only.
  7787. */
  7788. if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
  7789. break;
  7790. }
  7791. vmcs_clear(shadow_vmcs);
  7792. vmcs_load(vmx->loaded_vmcs->vmcs);
  7793. preempt_enable();
  7794. }
  7795. static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
  7796. {
  7797. const u16 *fields[] = {
  7798. shadow_read_write_fields,
  7799. shadow_read_only_fields
  7800. };
  7801. const int max_fields[] = {
  7802. max_shadow_read_write_fields,
  7803. max_shadow_read_only_fields
  7804. };
  7805. int i, q;
  7806. unsigned long field;
  7807. u64 field_value = 0;
  7808. struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
  7809. vmcs_load(shadow_vmcs);
  7810. for (q = 0; q < ARRAY_SIZE(fields); q++) {
  7811. for (i = 0; i < max_fields[q]; i++) {
  7812. field = fields[q][i];
  7813. vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
  7814. __vmcs_writel(field, field_value);
  7815. }
  7816. }
  7817. vmcs_clear(shadow_vmcs);
  7818. vmcs_load(vmx->loaded_vmcs->vmcs);
  7819. }
  7820. static int handle_vmread(struct kvm_vcpu *vcpu)
  7821. {
  7822. unsigned long field;
  7823. u64 field_value;
  7824. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  7825. u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  7826. gva_t gva = 0;
  7827. struct vmcs12 *vmcs12;
  7828. if (!nested_vmx_check_permission(vcpu))
  7829. return 1;
  7830. if (to_vmx(vcpu)->nested.current_vmptr == -1ull)
  7831. return nested_vmx_failInvalid(vcpu);
  7832. if (!is_guest_mode(vcpu))
  7833. vmcs12 = get_vmcs12(vcpu);
  7834. else {
  7835. /*
  7836. * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
  7837. * to shadowed-field sets the ALU flags for VMfailInvalid.
  7838. */
  7839. if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull)
  7840. return nested_vmx_failInvalid(vcpu);
  7841. vmcs12 = get_shadow_vmcs12(vcpu);
  7842. }
  7843. /* Decode instruction info and find the field to read */
  7844. field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
  7845. /* Read the field, zero-extended to a u64 field_value */
  7846. if (vmcs12_read_any(vmcs12, field, &field_value) < 0)
  7847. return nested_vmx_failValid(vcpu,
  7848. VMXERR_UNSUPPORTED_VMCS_COMPONENT);
  7849. /*
  7850. * Now copy part of this value to register or memory, as requested.
  7851. * Note that the number of bits actually copied is 32 or 64 depending
  7852. * on the guest's mode (32 or 64 bit), not on the given field's length.
  7853. */
  7854. if (vmx_instruction_info & (1u << 10)) {
  7855. kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
  7856. field_value);
  7857. } else {
  7858. if (get_vmx_mem_address(vcpu, exit_qualification,
  7859. vmx_instruction_info, true, &gva))
  7860. return 1;
  7861. /* _system ok, nested_vmx_check_permission has verified cpl=0 */
  7862. kvm_write_guest_virt_system(vcpu, gva, &field_value,
  7863. (is_long_mode(vcpu) ? 8 : 4), NULL);
  7864. }
  7865. return nested_vmx_succeed(vcpu);
  7866. }
  7867. static int handle_vmwrite(struct kvm_vcpu *vcpu)
  7868. {
  7869. unsigned long field;
  7870. gva_t gva;
  7871. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7872. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  7873. u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  7874. /* The value to write might be 32 or 64 bits, depending on L1's long
  7875. * mode, and eventually we need to write that into a field of several
  7876. * possible lengths. The code below first zero-extends the value to 64
  7877. * bit (field_value), and then copies only the appropriate number of
  7878. * bits into the vmcs12 field.
  7879. */
  7880. u64 field_value = 0;
  7881. struct x86_exception e;
  7882. struct vmcs12 *vmcs12;
  7883. if (!nested_vmx_check_permission(vcpu))
  7884. return 1;
  7885. if (vmx->nested.current_vmptr == -1ull)
  7886. return nested_vmx_failInvalid(vcpu);
  7887. if (vmx_instruction_info & (1u << 10))
  7888. field_value = kvm_register_readl(vcpu,
  7889. (((vmx_instruction_info) >> 3) & 0xf));
  7890. else {
  7891. if (get_vmx_mem_address(vcpu, exit_qualification,
  7892. vmx_instruction_info, false, &gva))
  7893. return 1;
  7894. if (kvm_read_guest_virt(vcpu, gva, &field_value,
  7895. (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
  7896. kvm_inject_page_fault(vcpu, &e);
  7897. return 1;
  7898. }
  7899. }
  7900. field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
  7901. /*
  7902. * If the vCPU supports "VMWRITE to any supported field in the
  7903. * VMCS," then the "read-only" fields are actually read/write.
  7904. */
  7905. if (vmcs_field_readonly(field) &&
  7906. !nested_cpu_has_vmwrite_any_field(vcpu))
  7907. return nested_vmx_failValid(vcpu,
  7908. VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
  7909. if (!is_guest_mode(vcpu))
  7910. vmcs12 = get_vmcs12(vcpu);
  7911. else {
  7912. /*
  7913. * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
  7914. * to shadowed-field sets the ALU flags for VMfailInvalid.
  7915. */
  7916. if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull)
  7917. return nested_vmx_failInvalid(vcpu);
  7918. vmcs12 = get_shadow_vmcs12(vcpu);
  7919. }
  7920. if (vmcs12_write_any(vmcs12, field, field_value) < 0)
  7921. return nested_vmx_failValid(vcpu,
  7922. VMXERR_UNSUPPORTED_VMCS_COMPONENT);
  7923. /*
  7924. * Do not track vmcs12 dirty-state if in guest-mode
  7925. * as we actually dirty shadow vmcs12 instead of vmcs12.
  7926. */
  7927. if (!is_guest_mode(vcpu)) {
  7928. switch (field) {
  7929. #define SHADOW_FIELD_RW(x) case x:
  7930. #include "vmx_shadow_fields.h"
  7931. /*
  7932. * The fields that can be updated by L1 without a vmexit are
  7933. * always updated in the vmcs02, the others go down the slow
  7934. * path of prepare_vmcs02.
  7935. */
  7936. break;
  7937. default:
  7938. vmx->nested.dirty_vmcs12 = true;
  7939. break;
  7940. }
  7941. }
  7942. return nested_vmx_succeed(vcpu);
  7943. }
  7944. static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
  7945. {
  7946. vmx->nested.current_vmptr = vmptr;
  7947. if (enable_shadow_vmcs) {
  7948. vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
  7949. SECONDARY_EXEC_SHADOW_VMCS);
  7950. vmcs_write64(VMCS_LINK_POINTER,
  7951. __pa(vmx->vmcs01.shadow_vmcs));
  7952. vmx->nested.need_vmcs12_sync = true;
  7953. }
  7954. vmx->nested.dirty_vmcs12 = true;
  7955. }
  7956. /* Emulate the VMPTRLD instruction */
  7957. static int handle_vmptrld(struct kvm_vcpu *vcpu)
  7958. {
  7959. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7960. gpa_t vmptr;
  7961. if (!nested_vmx_check_permission(vcpu))
  7962. return 1;
  7963. if (nested_vmx_get_vmptr(vcpu, &vmptr))
  7964. return 1;
  7965. if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu)))
  7966. return nested_vmx_failValid(vcpu,
  7967. VMXERR_VMPTRLD_INVALID_ADDRESS);
  7968. if (vmptr == vmx->nested.vmxon_ptr)
  7969. return nested_vmx_failValid(vcpu,
  7970. VMXERR_VMPTRLD_VMXON_POINTER);
  7971. /* Forbid normal VMPTRLD if Enlightened version was used */
  7972. if (vmx->nested.hv_evmcs)
  7973. return 1;
  7974. if (vmx->nested.current_vmptr != vmptr) {
  7975. struct vmcs12 *new_vmcs12;
  7976. struct page *page;
  7977. page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
  7978. if (is_error_page(page))
  7979. return nested_vmx_failInvalid(vcpu);
  7980. new_vmcs12 = kmap(page);
  7981. if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
  7982. (new_vmcs12->hdr.shadow_vmcs &&
  7983. !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
  7984. kunmap(page);
  7985. kvm_release_page_clean(page);
  7986. return nested_vmx_failValid(vcpu,
  7987. VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
  7988. }
  7989. nested_release_vmcs12(vcpu);
  7990. /*
  7991. * Load VMCS12 from guest memory since it is not already
  7992. * cached.
  7993. */
  7994. memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
  7995. kunmap(page);
  7996. kvm_release_page_clean(page);
  7997. set_current_vmptr(vmx, vmptr);
  7998. }
  7999. return nested_vmx_succeed(vcpu);
  8000. }
  8001. /*
  8002. * This is an equivalent of the nested hypervisor executing the vmptrld
  8003. * instruction.
  8004. */
  8005. static int nested_vmx_handle_enlightened_vmptrld(struct kvm_vcpu *vcpu,
  8006. bool from_launch)
  8007. {
  8008. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8009. struct hv_vp_assist_page assist_page;
  8010. if (likely(!vmx->nested.enlightened_vmcs_enabled))
  8011. return 1;
  8012. if (unlikely(!kvm_hv_get_assist_page(vcpu, &assist_page)))
  8013. return 1;
  8014. if (unlikely(!assist_page.enlighten_vmentry))
  8015. return 1;
  8016. if (unlikely(assist_page.current_nested_vmcs !=
  8017. vmx->nested.hv_evmcs_vmptr)) {
  8018. if (!vmx->nested.hv_evmcs)
  8019. vmx->nested.current_vmptr = -1ull;
  8020. nested_release_evmcs(vcpu);
  8021. vmx->nested.hv_evmcs_page = kvm_vcpu_gpa_to_page(
  8022. vcpu, assist_page.current_nested_vmcs);
  8023. if (unlikely(is_error_page(vmx->nested.hv_evmcs_page)))
  8024. return 0;
  8025. vmx->nested.hv_evmcs = kmap(vmx->nested.hv_evmcs_page);
  8026. /*
  8027. * Currently, KVM only supports eVMCS version 1
  8028. * (== KVM_EVMCS_VERSION) and thus we expect guest to set this
  8029. * value to first u32 field of eVMCS which should specify eVMCS
  8030. * VersionNumber.
  8031. *
  8032. * Guest should be aware of supported eVMCS versions by host by
  8033. * examining CPUID.0x4000000A.EAX[0:15]. Host userspace VMM is
  8034. * expected to set this CPUID leaf according to the value
  8035. * returned in vmcs_version from nested_enable_evmcs().
  8036. *
  8037. * However, it turns out that Microsoft Hyper-V fails to comply
  8038. * to their own invented interface: When Hyper-V use eVMCS, it
  8039. * just sets first u32 field of eVMCS to revision_id specified
  8040. * in MSR_IA32_VMX_BASIC. Instead of used eVMCS version number
  8041. * which is one of the supported versions specified in
  8042. * CPUID.0x4000000A.EAX[0:15].
  8043. *
  8044. * To overcome Hyper-V bug, we accept here either a supported
  8045. * eVMCS version or VMCS12 revision_id as valid values for first
  8046. * u32 field of eVMCS.
  8047. */
  8048. if ((vmx->nested.hv_evmcs->revision_id != KVM_EVMCS_VERSION) &&
  8049. (vmx->nested.hv_evmcs->revision_id != VMCS12_REVISION)) {
  8050. nested_release_evmcs(vcpu);
  8051. return 0;
  8052. }
  8053. vmx->nested.dirty_vmcs12 = true;
  8054. /*
  8055. * As we keep L2 state for one guest only 'hv_clean_fields' mask
  8056. * can't be used when we switch between them. Reset it here for
  8057. * simplicity.
  8058. */
  8059. vmx->nested.hv_evmcs->hv_clean_fields &=
  8060. ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
  8061. vmx->nested.hv_evmcs_vmptr = assist_page.current_nested_vmcs;
  8062. /*
  8063. * Unlike normal vmcs12, enlightened vmcs12 is not fully
  8064. * reloaded from guest's memory (read only fields, fields not
  8065. * present in struct hv_enlightened_vmcs, ...). Make sure there
  8066. * are no leftovers.
  8067. */
  8068. if (from_launch) {
  8069. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  8070. memset(vmcs12, 0, sizeof(*vmcs12));
  8071. vmcs12->hdr.revision_id = VMCS12_REVISION;
  8072. }
  8073. }
  8074. return 1;
  8075. }
  8076. /* Emulate the VMPTRST instruction */
  8077. static int handle_vmptrst(struct kvm_vcpu *vcpu)
  8078. {
  8079. unsigned long exit_qual = vmcs_readl(EXIT_QUALIFICATION);
  8080. u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  8081. gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
  8082. struct x86_exception e;
  8083. gva_t gva;
  8084. if (!nested_vmx_check_permission(vcpu))
  8085. return 1;
  8086. if (unlikely(to_vmx(vcpu)->nested.hv_evmcs))
  8087. return 1;
  8088. if (get_vmx_mem_address(vcpu, exit_qual, instr_info, true, &gva))
  8089. return 1;
  8090. /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
  8091. if (kvm_write_guest_virt_system(vcpu, gva, (void *)&current_vmptr,
  8092. sizeof(gpa_t), &e)) {
  8093. kvm_inject_page_fault(vcpu, &e);
  8094. return 1;
  8095. }
  8096. return nested_vmx_succeed(vcpu);
  8097. }
  8098. /* Emulate the INVEPT instruction */
  8099. static int handle_invept(struct kvm_vcpu *vcpu)
  8100. {
  8101. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8102. u32 vmx_instruction_info, types;
  8103. unsigned long type;
  8104. gva_t gva;
  8105. struct x86_exception e;
  8106. struct {
  8107. u64 eptp, gpa;
  8108. } operand;
  8109. if (!(vmx->nested.msrs.secondary_ctls_high &
  8110. SECONDARY_EXEC_ENABLE_EPT) ||
  8111. !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
  8112. kvm_queue_exception(vcpu, UD_VECTOR);
  8113. return 1;
  8114. }
  8115. if (!nested_vmx_check_permission(vcpu))
  8116. return 1;
  8117. vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  8118. type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
  8119. types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
  8120. if (type >= 32 || !(types & (1 << type)))
  8121. return nested_vmx_failValid(vcpu,
  8122. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  8123. /* According to the Intel VMX instruction reference, the memory
  8124. * operand is read even if it isn't needed (e.g., for type==global)
  8125. */
  8126. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  8127. vmx_instruction_info, false, &gva))
  8128. return 1;
  8129. if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
  8130. kvm_inject_page_fault(vcpu, &e);
  8131. return 1;
  8132. }
  8133. switch (type) {
  8134. case VMX_EPT_EXTENT_GLOBAL:
  8135. /*
  8136. * TODO: track mappings and invalidate
  8137. * single context requests appropriately
  8138. */
  8139. case VMX_EPT_EXTENT_CONTEXT:
  8140. kvm_mmu_sync_roots(vcpu);
  8141. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  8142. break;
  8143. default:
  8144. BUG_ON(1);
  8145. break;
  8146. }
  8147. return nested_vmx_succeed(vcpu);
  8148. }
  8149. static u16 nested_get_vpid02(struct kvm_vcpu *vcpu)
  8150. {
  8151. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8152. return vmx->nested.vpid02 ? vmx->nested.vpid02 : vmx->vpid;
  8153. }
  8154. static int handle_invvpid(struct kvm_vcpu *vcpu)
  8155. {
  8156. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8157. u32 vmx_instruction_info;
  8158. unsigned long type, types;
  8159. gva_t gva;
  8160. struct x86_exception e;
  8161. struct {
  8162. u64 vpid;
  8163. u64 gla;
  8164. } operand;
  8165. u16 vpid02;
  8166. if (!(vmx->nested.msrs.secondary_ctls_high &
  8167. SECONDARY_EXEC_ENABLE_VPID) ||
  8168. !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
  8169. kvm_queue_exception(vcpu, UD_VECTOR);
  8170. return 1;
  8171. }
  8172. if (!nested_vmx_check_permission(vcpu))
  8173. return 1;
  8174. vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  8175. type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
  8176. types = (vmx->nested.msrs.vpid_caps &
  8177. VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
  8178. if (type >= 32 || !(types & (1 << type)))
  8179. return nested_vmx_failValid(vcpu,
  8180. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  8181. /* according to the intel vmx instruction reference, the memory
  8182. * operand is read even if it isn't needed (e.g., for type==global)
  8183. */
  8184. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  8185. vmx_instruction_info, false, &gva))
  8186. return 1;
  8187. if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
  8188. kvm_inject_page_fault(vcpu, &e);
  8189. return 1;
  8190. }
  8191. if (operand.vpid >> 16)
  8192. return nested_vmx_failValid(vcpu,
  8193. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  8194. vpid02 = nested_get_vpid02(vcpu);
  8195. switch (type) {
  8196. case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
  8197. if (!operand.vpid ||
  8198. is_noncanonical_address(operand.gla, vcpu))
  8199. return nested_vmx_failValid(vcpu,
  8200. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  8201. if (cpu_has_vmx_invvpid_individual_addr()) {
  8202. __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
  8203. vpid02, operand.gla);
  8204. } else
  8205. __vmx_flush_tlb(vcpu, vpid02, false);
  8206. break;
  8207. case VMX_VPID_EXTENT_SINGLE_CONTEXT:
  8208. case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
  8209. if (!operand.vpid)
  8210. return nested_vmx_failValid(vcpu,
  8211. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  8212. __vmx_flush_tlb(vcpu, vpid02, false);
  8213. break;
  8214. case VMX_VPID_EXTENT_ALL_CONTEXT:
  8215. __vmx_flush_tlb(vcpu, vpid02, false);
  8216. break;
  8217. default:
  8218. WARN_ON_ONCE(1);
  8219. return kvm_skip_emulated_instruction(vcpu);
  8220. }
  8221. return nested_vmx_succeed(vcpu);
  8222. }
  8223. static int handle_invpcid(struct kvm_vcpu *vcpu)
  8224. {
  8225. u32 vmx_instruction_info;
  8226. unsigned long type;
  8227. bool pcid_enabled;
  8228. gva_t gva;
  8229. struct x86_exception e;
  8230. unsigned i;
  8231. unsigned long roots_to_free = 0;
  8232. struct {
  8233. u64 pcid;
  8234. u64 gla;
  8235. } operand;
  8236. if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
  8237. kvm_queue_exception(vcpu, UD_VECTOR);
  8238. return 1;
  8239. }
  8240. vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  8241. type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
  8242. if (type > 3) {
  8243. kvm_inject_gp(vcpu, 0);
  8244. return 1;
  8245. }
  8246. /* According to the Intel instruction reference, the memory operand
  8247. * is read even if it isn't needed (e.g., for type==all)
  8248. */
  8249. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  8250. vmx_instruction_info, false, &gva))
  8251. return 1;
  8252. if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
  8253. kvm_inject_page_fault(vcpu, &e);
  8254. return 1;
  8255. }
  8256. if (operand.pcid >> 12 != 0) {
  8257. kvm_inject_gp(vcpu, 0);
  8258. return 1;
  8259. }
  8260. pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
  8261. switch (type) {
  8262. case INVPCID_TYPE_INDIV_ADDR:
  8263. if ((!pcid_enabled && (operand.pcid != 0)) ||
  8264. is_noncanonical_address(operand.gla, vcpu)) {
  8265. kvm_inject_gp(vcpu, 0);
  8266. return 1;
  8267. }
  8268. kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
  8269. return kvm_skip_emulated_instruction(vcpu);
  8270. case INVPCID_TYPE_SINGLE_CTXT:
  8271. if (!pcid_enabled && (operand.pcid != 0)) {
  8272. kvm_inject_gp(vcpu, 0);
  8273. return 1;
  8274. }
  8275. if (kvm_get_active_pcid(vcpu) == operand.pcid) {
  8276. kvm_mmu_sync_roots(vcpu);
  8277. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  8278. }
  8279. for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
  8280. if (kvm_get_pcid(vcpu, vcpu->arch.mmu->prev_roots[i].cr3)
  8281. == operand.pcid)
  8282. roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
  8283. kvm_mmu_free_roots(vcpu, vcpu->arch.mmu, roots_to_free);
  8284. /*
  8285. * If neither the current cr3 nor any of the prev_roots use the
  8286. * given PCID, then nothing needs to be done here because a
  8287. * resync will happen anyway before switching to any other CR3.
  8288. */
  8289. return kvm_skip_emulated_instruction(vcpu);
  8290. case INVPCID_TYPE_ALL_NON_GLOBAL:
  8291. /*
  8292. * Currently, KVM doesn't mark global entries in the shadow
  8293. * page tables, so a non-global flush just degenerates to a
  8294. * global flush. If needed, we could optimize this later by
  8295. * keeping track of global entries in shadow page tables.
  8296. */
  8297. /* fall-through */
  8298. case INVPCID_TYPE_ALL_INCL_GLOBAL:
  8299. kvm_mmu_unload(vcpu);
  8300. return kvm_skip_emulated_instruction(vcpu);
  8301. default:
  8302. BUG(); /* We have already checked above that type <= 3 */
  8303. }
  8304. }
  8305. static int handle_pml_full(struct kvm_vcpu *vcpu)
  8306. {
  8307. unsigned long exit_qualification;
  8308. trace_kvm_pml_full(vcpu->vcpu_id);
  8309. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  8310. /*
  8311. * PML buffer FULL happened while executing iret from NMI,
  8312. * "blocked by NMI" bit has to be set before next VM entry.
  8313. */
  8314. if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
  8315. enable_vnmi &&
  8316. (exit_qualification & INTR_INFO_UNBLOCK_NMI))
  8317. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  8318. GUEST_INTR_STATE_NMI);
  8319. /*
  8320. * PML buffer already flushed at beginning of VMEXIT. Nothing to do
  8321. * here.., and there's no userspace involvement needed for PML.
  8322. */
  8323. return 1;
  8324. }
  8325. static int handle_preemption_timer(struct kvm_vcpu *vcpu)
  8326. {
  8327. if (!to_vmx(vcpu)->req_immediate_exit)
  8328. kvm_lapic_expired_hv_timer(vcpu);
  8329. return 1;
  8330. }
  8331. static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
  8332. {
  8333. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8334. int maxphyaddr = cpuid_maxphyaddr(vcpu);
  8335. /* Check for memory type validity */
  8336. switch (address & VMX_EPTP_MT_MASK) {
  8337. case VMX_EPTP_MT_UC:
  8338. if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
  8339. return false;
  8340. break;
  8341. case VMX_EPTP_MT_WB:
  8342. if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
  8343. return false;
  8344. break;
  8345. default:
  8346. return false;
  8347. }
  8348. /* only 4 levels page-walk length are valid */
  8349. if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
  8350. return false;
  8351. /* Reserved bits should not be set */
  8352. if (address >> maxphyaddr || ((address >> 7) & 0x1f))
  8353. return false;
  8354. /* AD, if set, should be supported */
  8355. if (address & VMX_EPTP_AD_ENABLE_BIT) {
  8356. if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
  8357. return false;
  8358. }
  8359. return true;
  8360. }
  8361. static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
  8362. struct vmcs12 *vmcs12)
  8363. {
  8364. u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
  8365. u64 address;
  8366. bool accessed_dirty;
  8367. struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
  8368. if (!nested_cpu_has_eptp_switching(vmcs12) ||
  8369. !nested_cpu_has_ept(vmcs12))
  8370. return 1;
  8371. if (index >= VMFUNC_EPTP_ENTRIES)
  8372. return 1;
  8373. if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
  8374. &address, index * 8, 8))
  8375. return 1;
  8376. accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
  8377. /*
  8378. * If the (L2) guest does a vmfunc to the currently
  8379. * active ept pointer, we don't have to do anything else
  8380. */
  8381. if (vmcs12->ept_pointer != address) {
  8382. if (!valid_ept_address(vcpu, address))
  8383. return 1;
  8384. kvm_mmu_unload(vcpu);
  8385. mmu->ept_ad = accessed_dirty;
  8386. mmu->mmu_role.base.ad_disabled = !accessed_dirty;
  8387. vmcs12->ept_pointer = address;
  8388. /*
  8389. * TODO: Check what's the correct approach in case
  8390. * mmu reload fails. Currently, we just let the next
  8391. * reload potentially fail
  8392. */
  8393. kvm_mmu_reload(vcpu);
  8394. }
  8395. return 0;
  8396. }
  8397. static int handle_vmfunc(struct kvm_vcpu *vcpu)
  8398. {
  8399. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8400. struct vmcs12 *vmcs12;
  8401. u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
  8402. /*
  8403. * VMFUNC is only supported for nested guests, but we always enable the
  8404. * secondary control for simplicity; for non-nested mode, fake that we
  8405. * didn't by injecting #UD.
  8406. */
  8407. if (!is_guest_mode(vcpu)) {
  8408. kvm_queue_exception(vcpu, UD_VECTOR);
  8409. return 1;
  8410. }
  8411. vmcs12 = get_vmcs12(vcpu);
  8412. if ((vmcs12->vm_function_control & (1 << function)) == 0)
  8413. goto fail;
  8414. switch (function) {
  8415. case 0:
  8416. if (nested_vmx_eptp_switching(vcpu, vmcs12))
  8417. goto fail;
  8418. break;
  8419. default:
  8420. goto fail;
  8421. }
  8422. return kvm_skip_emulated_instruction(vcpu);
  8423. fail:
  8424. nested_vmx_vmexit(vcpu, vmx->exit_reason,
  8425. vmcs_read32(VM_EXIT_INTR_INFO),
  8426. vmcs_readl(EXIT_QUALIFICATION));
  8427. return 1;
  8428. }
  8429. static int handle_encls(struct kvm_vcpu *vcpu)
  8430. {
  8431. /*
  8432. * SGX virtualization is not yet supported. There is no software
  8433. * enable bit for SGX, so we have to trap ENCLS and inject a #UD
  8434. * to prevent the guest from executing ENCLS.
  8435. */
  8436. kvm_queue_exception(vcpu, UD_VECTOR);
  8437. return 1;
  8438. }
  8439. /*
  8440. * The exit handlers return 1 if the exit was handled fully and guest execution
  8441. * may resume. Otherwise they set the kvm_run parameter to indicate what needs
  8442. * to be done to userspace and return 0.
  8443. */
  8444. static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
  8445. [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
  8446. [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
  8447. [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
  8448. [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
  8449. [EXIT_REASON_IO_INSTRUCTION] = handle_io,
  8450. [EXIT_REASON_CR_ACCESS] = handle_cr,
  8451. [EXIT_REASON_DR_ACCESS] = handle_dr,
  8452. [EXIT_REASON_CPUID] = handle_cpuid,
  8453. [EXIT_REASON_MSR_READ] = handle_rdmsr,
  8454. [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
  8455. [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
  8456. [EXIT_REASON_HLT] = handle_halt,
  8457. [EXIT_REASON_INVD] = handle_invd,
  8458. [EXIT_REASON_INVLPG] = handle_invlpg,
  8459. [EXIT_REASON_RDPMC] = handle_rdpmc,
  8460. [EXIT_REASON_VMCALL] = handle_vmcall,
  8461. [EXIT_REASON_VMCLEAR] = handle_vmclear,
  8462. [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
  8463. [EXIT_REASON_VMPTRLD] = handle_vmptrld,
  8464. [EXIT_REASON_VMPTRST] = handle_vmptrst,
  8465. [EXIT_REASON_VMREAD] = handle_vmread,
  8466. [EXIT_REASON_VMRESUME] = handle_vmresume,
  8467. [EXIT_REASON_VMWRITE] = handle_vmwrite,
  8468. [EXIT_REASON_VMOFF] = handle_vmoff,
  8469. [EXIT_REASON_VMON] = handle_vmon,
  8470. [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
  8471. [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
  8472. [EXIT_REASON_APIC_WRITE] = handle_apic_write,
  8473. [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
  8474. [EXIT_REASON_WBINVD] = handle_wbinvd,
  8475. [EXIT_REASON_XSETBV] = handle_xsetbv,
  8476. [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
  8477. [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
  8478. [EXIT_REASON_GDTR_IDTR] = handle_desc,
  8479. [EXIT_REASON_LDTR_TR] = handle_desc,
  8480. [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
  8481. [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
  8482. [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
  8483. [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
  8484. [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
  8485. [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
  8486. [EXIT_REASON_INVEPT] = handle_invept,
  8487. [EXIT_REASON_INVVPID] = handle_invvpid,
  8488. [EXIT_REASON_RDRAND] = handle_invalid_op,
  8489. [EXIT_REASON_RDSEED] = handle_invalid_op,
  8490. [EXIT_REASON_XSAVES] = handle_xsaves,
  8491. [EXIT_REASON_XRSTORS] = handle_xrstors,
  8492. [EXIT_REASON_PML_FULL] = handle_pml_full,
  8493. [EXIT_REASON_INVPCID] = handle_invpcid,
  8494. [EXIT_REASON_VMFUNC] = handle_vmfunc,
  8495. [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
  8496. [EXIT_REASON_ENCLS] = handle_encls,
  8497. };
  8498. static const int kvm_vmx_max_exit_handlers =
  8499. ARRAY_SIZE(kvm_vmx_exit_handlers);
  8500. static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
  8501. struct vmcs12 *vmcs12)
  8502. {
  8503. unsigned long exit_qualification;
  8504. gpa_t bitmap, last_bitmap;
  8505. unsigned int port;
  8506. int size;
  8507. u8 b;
  8508. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
  8509. return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
  8510. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  8511. port = exit_qualification >> 16;
  8512. size = (exit_qualification & 7) + 1;
  8513. last_bitmap = (gpa_t)-1;
  8514. b = -1;
  8515. while (size > 0) {
  8516. if (port < 0x8000)
  8517. bitmap = vmcs12->io_bitmap_a;
  8518. else if (port < 0x10000)
  8519. bitmap = vmcs12->io_bitmap_b;
  8520. else
  8521. return true;
  8522. bitmap += (port & 0x7fff) / 8;
  8523. if (last_bitmap != bitmap)
  8524. if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
  8525. return true;
  8526. if (b & (1 << (port & 7)))
  8527. return true;
  8528. port++;
  8529. size--;
  8530. last_bitmap = bitmap;
  8531. }
  8532. return false;
  8533. }
  8534. /*
  8535. * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
  8536. * rather than handle it ourselves in L0. I.e., check whether L1 expressed
  8537. * disinterest in the current event (read or write a specific MSR) by using an
  8538. * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
  8539. */
  8540. static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
  8541. struct vmcs12 *vmcs12, u32 exit_reason)
  8542. {
  8543. u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
  8544. gpa_t bitmap;
  8545. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
  8546. return true;
  8547. /*
  8548. * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
  8549. * for the four combinations of read/write and low/high MSR numbers.
  8550. * First we need to figure out which of the four to use:
  8551. */
  8552. bitmap = vmcs12->msr_bitmap;
  8553. if (exit_reason == EXIT_REASON_MSR_WRITE)
  8554. bitmap += 2048;
  8555. if (msr_index >= 0xc0000000) {
  8556. msr_index -= 0xc0000000;
  8557. bitmap += 1024;
  8558. }
  8559. /* Then read the msr_index'th bit from this bitmap: */
  8560. if (msr_index < 1024*8) {
  8561. unsigned char b;
  8562. if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
  8563. return true;
  8564. return 1 & (b >> (msr_index & 7));
  8565. } else
  8566. return true; /* let L1 handle the wrong parameter */
  8567. }
  8568. /*
  8569. * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
  8570. * rather than handle it ourselves in L0. I.e., check if L1 wanted to
  8571. * intercept (via guest_host_mask etc.) the current event.
  8572. */
  8573. static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
  8574. struct vmcs12 *vmcs12)
  8575. {
  8576. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  8577. int cr = exit_qualification & 15;
  8578. int reg;
  8579. unsigned long val;
  8580. switch ((exit_qualification >> 4) & 3) {
  8581. case 0: /* mov to cr */
  8582. reg = (exit_qualification >> 8) & 15;
  8583. val = kvm_register_readl(vcpu, reg);
  8584. switch (cr) {
  8585. case 0:
  8586. if (vmcs12->cr0_guest_host_mask &
  8587. (val ^ vmcs12->cr0_read_shadow))
  8588. return true;
  8589. break;
  8590. case 3:
  8591. if ((vmcs12->cr3_target_count >= 1 &&
  8592. vmcs12->cr3_target_value0 == val) ||
  8593. (vmcs12->cr3_target_count >= 2 &&
  8594. vmcs12->cr3_target_value1 == val) ||
  8595. (vmcs12->cr3_target_count >= 3 &&
  8596. vmcs12->cr3_target_value2 == val) ||
  8597. (vmcs12->cr3_target_count >= 4 &&
  8598. vmcs12->cr3_target_value3 == val))
  8599. return false;
  8600. if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
  8601. return true;
  8602. break;
  8603. case 4:
  8604. if (vmcs12->cr4_guest_host_mask &
  8605. (vmcs12->cr4_read_shadow ^ val))
  8606. return true;
  8607. break;
  8608. case 8:
  8609. if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
  8610. return true;
  8611. break;
  8612. }
  8613. break;
  8614. case 2: /* clts */
  8615. if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
  8616. (vmcs12->cr0_read_shadow & X86_CR0_TS))
  8617. return true;
  8618. break;
  8619. case 1: /* mov from cr */
  8620. switch (cr) {
  8621. case 3:
  8622. if (vmcs12->cpu_based_vm_exec_control &
  8623. CPU_BASED_CR3_STORE_EXITING)
  8624. return true;
  8625. break;
  8626. case 8:
  8627. if (vmcs12->cpu_based_vm_exec_control &
  8628. CPU_BASED_CR8_STORE_EXITING)
  8629. return true;
  8630. break;
  8631. }
  8632. break;
  8633. case 3: /* lmsw */
  8634. /*
  8635. * lmsw can change bits 1..3 of cr0, and only set bit 0 of
  8636. * cr0. Other attempted changes are ignored, with no exit.
  8637. */
  8638. val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
  8639. if (vmcs12->cr0_guest_host_mask & 0xe &
  8640. (val ^ vmcs12->cr0_read_shadow))
  8641. return true;
  8642. if ((vmcs12->cr0_guest_host_mask & 0x1) &&
  8643. !(vmcs12->cr0_read_shadow & 0x1) &&
  8644. (val & 0x1))
  8645. return true;
  8646. break;
  8647. }
  8648. return false;
  8649. }
  8650. static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
  8651. struct vmcs12 *vmcs12, gpa_t bitmap)
  8652. {
  8653. u32 vmx_instruction_info;
  8654. unsigned long field;
  8655. u8 b;
  8656. if (!nested_cpu_has_shadow_vmcs(vmcs12))
  8657. return true;
  8658. /* Decode instruction info and find the field to access */
  8659. vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  8660. field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
  8661. /* Out-of-range fields always cause a VM exit from L2 to L1 */
  8662. if (field >> 15)
  8663. return true;
  8664. if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
  8665. return true;
  8666. return 1 & (b >> (field & 7));
  8667. }
  8668. /*
  8669. * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
  8670. * should handle it ourselves in L0 (and then continue L2). Only call this
  8671. * when in is_guest_mode (L2).
  8672. */
  8673. static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
  8674. {
  8675. u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  8676. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8677. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  8678. if (vmx->nested.nested_run_pending)
  8679. return false;
  8680. if (unlikely(vmx->fail)) {
  8681. pr_info_ratelimited("%s failed vm entry %x\n", __func__,
  8682. vmcs_read32(VM_INSTRUCTION_ERROR));
  8683. return true;
  8684. }
  8685. /*
  8686. * The host physical addresses of some pages of guest memory
  8687. * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
  8688. * Page). The CPU may write to these pages via their host
  8689. * physical address while L2 is running, bypassing any
  8690. * address-translation-based dirty tracking (e.g. EPT write
  8691. * protection).
  8692. *
  8693. * Mark them dirty on every exit from L2 to prevent them from
  8694. * getting out of sync with dirty tracking.
  8695. */
  8696. nested_mark_vmcs12_pages_dirty(vcpu);
  8697. trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
  8698. vmcs_readl(EXIT_QUALIFICATION),
  8699. vmx->idt_vectoring_info,
  8700. intr_info,
  8701. vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
  8702. KVM_ISA_VMX);
  8703. switch (exit_reason) {
  8704. case EXIT_REASON_EXCEPTION_NMI:
  8705. if (is_nmi(intr_info))
  8706. return false;
  8707. else if (is_page_fault(intr_info))
  8708. return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
  8709. else if (is_debug(intr_info) &&
  8710. vcpu->guest_debug &
  8711. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
  8712. return false;
  8713. else if (is_breakpoint(intr_info) &&
  8714. vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  8715. return false;
  8716. return vmcs12->exception_bitmap &
  8717. (1u << (intr_info & INTR_INFO_VECTOR_MASK));
  8718. case EXIT_REASON_EXTERNAL_INTERRUPT:
  8719. return false;
  8720. case EXIT_REASON_TRIPLE_FAULT:
  8721. return true;
  8722. case EXIT_REASON_PENDING_INTERRUPT:
  8723. return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
  8724. case EXIT_REASON_NMI_WINDOW:
  8725. return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
  8726. case EXIT_REASON_TASK_SWITCH:
  8727. return true;
  8728. case EXIT_REASON_CPUID:
  8729. return true;
  8730. case EXIT_REASON_HLT:
  8731. return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
  8732. case EXIT_REASON_INVD:
  8733. return true;
  8734. case EXIT_REASON_INVLPG:
  8735. return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
  8736. case EXIT_REASON_RDPMC:
  8737. return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
  8738. case EXIT_REASON_RDRAND:
  8739. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
  8740. case EXIT_REASON_RDSEED:
  8741. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
  8742. case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
  8743. return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
  8744. case EXIT_REASON_VMREAD:
  8745. return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
  8746. vmcs12->vmread_bitmap);
  8747. case EXIT_REASON_VMWRITE:
  8748. return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
  8749. vmcs12->vmwrite_bitmap);
  8750. case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
  8751. case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
  8752. case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
  8753. case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
  8754. case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
  8755. /*
  8756. * VMX instructions trap unconditionally. This allows L1 to
  8757. * emulate them for its L2 guest, i.e., allows 3-level nesting!
  8758. */
  8759. return true;
  8760. case EXIT_REASON_CR_ACCESS:
  8761. return nested_vmx_exit_handled_cr(vcpu, vmcs12);
  8762. case EXIT_REASON_DR_ACCESS:
  8763. return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
  8764. case EXIT_REASON_IO_INSTRUCTION:
  8765. return nested_vmx_exit_handled_io(vcpu, vmcs12);
  8766. case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
  8767. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
  8768. case EXIT_REASON_MSR_READ:
  8769. case EXIT_REASON_MSR_WRITE:
  8770. return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
  8771. case EXIT_REASON_INVALID_STATE:
  8772. return true;
  8773. case EXIT_REASON_MWAIT_INSTRUCTION:
  8774. return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
  8775. case EXIT_REASON_MONITOR_TRAP_FLAG:
  8776. return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
  8777. case EXIT_REASON_MONITOR_INSTRUCTION:
  8778. return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
  8779. case EXIT_REASON_PAUSE_INSTRUCTION:
  8780. return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
  8781. nested_cpu_has2(vmcs12,
  8782. SECONDARY_EXEC_PAUSE_LOOP_EXITING);
  8783. case EXIT_REASON_MCE_DURING_VMENTRY:
  8784. return false;
  8785. case EXIT_REASON_TPR_BELOW_THRESHOLD:
  8786. return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
  8787. case EXIT_REASON_APIC_ACCESS:
  8788. case EXIT_REASON_APIC_WRITE:
  8789. case EXIT_REASON_EOI_INDUCED:
  8790. /*
  8791. * The controls for "virtualize APIC accesses," "APIC-
  8792. * register virtualization," and "virtual-interrupt
  8793. * delivery" only come from vmcs12.
  8794. */
  8795. return true;
  8796. case EXIT_REASON_EPT_VIOLATION:
  8797. /*
  8798. * L0 always deals with the EPT violation. If nested EPT is
  8799. * used, and the nested mmu code discovers that the address is
  8800. * missing in the guest EPT table (EPT12), the EPT violation
  8801. * will be injected with nested_ept_inject_page_fault()
  8802. */
  8803. return false;
  8804. case EXIT_REASON_EPT_MISCONFIG:
  8805. /*
  8806. * L2 never uses directly L1's EPT, but rather L0's own EPT
  8807. * table (shadow on EPT) or a merged EPT table that L0 built
  8808. * (EPT on EPT). So any problems with the structure of the
  8809. * table is L0's fault.
  8810. */
  8811. return false;
  8812. case EXIT_REASON_INVPCID:
  8813. return
  8814. nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
  8815. nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
  8816. case EXIT_REASON_WBINVD:
  8817. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
  8818. case EXIT_REASON_XSETBV:
  8819. return true;
  8820. case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
  8821. /*
  8822. * This should never happen, since it is not possible to
  8823. * set XSS to a non-zero value---neither in L1 nor in L2.
  8824. * If if it were, XSS would have to be checked against
  8825. * the XSS exit bitmap in vmcs12.
  8826. */
  8827. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
  8828. case EXIT_REASON_PREEMPTION_TIMER:
  8829. return false;
  8830. case EXIT_REASON_PML_FULL:
  8831. /* We emulate PML support to L1. */
  8832. return false;
  8833. case EXIT_REASON_VMFUNC:
  8834. /* VM functions are emulated through L2->L0 vmexits. */
  8835. return false;
  8836. case EXIT_REASON_ENCLS:
  8837. /* SGX is never exposed to L1 */
  8838. return false;
  8839. default:
  8840. return true;
  8841. }
  8842. }
  8843. static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
  8844. {
  8845. u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  8846. /*
  8847. * At this point, the exit interruption info in exit_intr_info
  8848. * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
  8849. * we need to query the in-kernel LAPIC.
  8850. */
  8851. WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
  8852. if ((exit_intr_info &
  8853. (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
  8854. (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
  8855. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  8856. vmcs12->vm_exit_intr_error_code =
  8857. vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
  8858. }
  8859. nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
  8860. vmcs_readl(EXIT_QUALIFICATION));
  8861. return 1;
  8862. }
  8863. static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
  8864. {
  8865. *info1 = vmcs_readl(EXIT_QUALIFICATION);
  8866. *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
  8867. }
  8868. static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
  8869. {
  8870. if (vmx->pml_pg) {
  8871. __free_page(vmx->pml_pg);
  8872. vmx->pml_pg = NULL;
  8873. }
  8874. }
  8875. static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
  8876. {
  8877. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8878. u64 *pml_buf;
  8879. u16 pml_idx;
  8880. pml_idx = vmcs_read16(GUEST_PML_INDEX);
  8881. /* Do nothing if PML buffer is empty */
  8882. if (pml_idx == (PML_ENTITY_NUM - 1))
  8883. return;
  8884. /* PML index always points to next available PML buffer entity */
  8885. if (pml_idx >= PML_ENTITY_NUM)
  8886. pml_idx = 0;
  8887. else
  8888. pml_idx++;
  8889. pml_buf = page_address(vmx->pml_pg);
  8890. for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
  8891. u64 gpa;
  8892. gpa = pml_buf[pml_idx];
  8893. WARN_ON(gpa & (PAGE_SIZE - 1));
  8894. kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
  8895. }
  8896. /* reset PML index */
  8897. vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
  8898. }
  8899. /*
  8900. * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
  8901. * Called before reporting dirty_bitmap to userspace.
  8902. */
  8903. static void kvm_flush_pml_buffers(struct kvm *kvm)
  8904. {
  8905. int i;
  8906. struct kvm_vcpu *vcpu;
  8907. /*
  8908. * We only need to kick vcpu out of guest mode here, as PML buffer
  8909. * is flushed at beginning of all VMEXITs, and it's obvious that only
  8910. * vcpus running in guest are possible to have unflushed GPAs in PML
  8911. * buffer.
  8912. */
  8913. kvm_for_each_vcpu(i, vcpu, kvm)
  8914. kvm_vcpu_kick(vcpu);
  8915. }
  8916. static void vmx_dump_sel(char *name, uint32_t sel)
  8917. {
  8918. pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
  8919. name, vmcs_read16(sel),
  8920. vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
  8921. vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
  8922. vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
  8923. }
  8924. static void vmx_dump_dtsel(char *name, uint32_t limit)
  8925. {
  8926. pr_err("%s limit=0x%08x, base=0x%016lx\n",
  8927. name, vmcs_read32(limit),
  8928. vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
  8929. }
  8930. static void dump_vmcs(void)
  8931. {
  8932. u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
  8933. u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
  8934. u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  8935. u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
  8936. u32 secondary_exec_control = 0;
  8937. unsigned long cr4 = vmcs_readl(GUEST_CR4);
  8938. u64 efer = vmcs_read64(GUEST_IA32_EFER);
  8939. int i, n;
  8940. if (cpu_has_secondary_exec_ctrls())
  8941. secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  8942. pr_err("*** Guest State ***\n");
  8943. pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
  8944. vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
  8945. vmcs_readl(CR0_GUEST_HOST_MASK));
  8946. pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
  8947. cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
  8948. pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
  8949. if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
  8950. (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
  8951. {
  8952. pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
  8953. vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
  8954. pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
  8955. vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
  8956. }
  8957. pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
  8958. vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
  8959. pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
  8960. vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
  8961. pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
  8962. vmcs_readl(GUEST_SYSENTER_ESP),
  8963. vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
  8964. vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
  8965. vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
  8966. vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
  8967. vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
  8968. vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
  8969. vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
  8970. vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
  8971. vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
  8972. vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
  8973. vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
  8974. if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
  8975. (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
  8976. pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
  8977. efer, vmcs_read64(GUEST_IA32_PAT));
  8978. pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
  8979. vmcs_read64(GUEST_IA32_DEBUGCTL),
  8980. vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
  8981. if (cpu_has_load_perf_global_ctrl &&
  8982. vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
  8983. pr_err("PerfGlobCtl = 0x%016llx\n",
  8984. vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
  8985. if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
  8986. pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
  8987. pr_err("Interruptibility = %08x ActivityState = %08x\n",
  8988. vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
  8989. vmcs_read32(GUEST_ACTIVITY_STATE));
  8990. if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
  8991. pr_err("InterruptStatus = %04x\n",
  8992. vmcs_read16(GUEST_INTR_STATUS));
  8993. pr_err("*** Host State ***\n");
  8994. pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
  8995. vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
  8996. pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
  8997. vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
  8998. vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
  8999. vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
  9000. vmcs_read16(HOST_TR_SELECTOR));
  9001. pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
  9002. vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
  9003. vmcs_readl(HOST_TR_BASE));
  9004. pr_err("GDTBase=%016lx IDTBase=%016lx\n",
  9005. vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
  9006. pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
  9007. vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
  9008. vmcs_readl(HOST_CR4));
  9009. pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
  9010. vmcs_readl(HOST_IA32_SYSENTER_ESP),
  9011. vmcs_read32(HOST_IA32_SYSENTER_CS),
  9012. vmcs_readl(HOST_IA32_SYSENTER_EIP));
  9013. if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
  9014. pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
  9015. vmcs_read64(HOST_IA32_EFER),
  9016. vmcs_read64(HOST_IA32_PAT));
  9017. if (cpu_has_load_perf_global_ctrl &&
  9018. vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
  9019. pr_err("PerfGlobCtl = 0x%016llx\n",
  9020. vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
  9021. pr_err("*** Control State ***\n");
  9022. pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
  9023. pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
  9024. pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
  9025. pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
  9026. vmcs_read32(EXCEPTION_BITMAP),
  9027. vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
  9028. vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
  9029. pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
  9030. vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
  9031. vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
  9032. vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
  9033. pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
  9034. vmcs_read32(VM_EXIT_INTR_INFO),
  9035. vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
  9036. vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
  9037. pr_err(" reason=%08x qualification=%016lx\n",
  9038. vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
  9039. pr_err("IDTVectoring: info=%08x errcode=%08x\n",
  9040. vmcs_read32(IDT_VECTORING_INFO_FIELD),
  9041. vmcs_read32(IDT_VECTORING_ERROR_CODE));
  9042. pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
  9043. if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
  9044. pr_err("TSC Multiplier = 0x%016llx\n",
  9045. vmcs_read64(TSC_MULTIPLIER));
  9046. if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
  9047. pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
  9048. if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
  9049. pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
  9050. if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
  9051. pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
  9052. n = vmcs_read32(CR3_TARGET_COUNT);
  9053. for (i = 0; i + 1 < n; i += 4)
  9054. pr_err("CR3 target%u=%016lx target%u=%016lx\n",
  9055. i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
  9056. i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
  9057. if (i < n)
  9058. pr_err("CR3 target%u=%016lx\n",
  9059. i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
  9060. if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
  9061. pr_err("PLE Gap=%08x Window=%08x\n",
  9062. vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
  9063. if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
  9064. pr_err("Virtual processor ID = 0x%04x\n",
  9065. vmcs_read16(VIRTUAL_PROCESSOR_ID));
  9066. }
  9067. /*
  9068. * The guest has exited. See if we can fix it or if we need userspace
  9069. * assistance.
  9070. */
  9071. static int vmx_handle_exit(struct kvm_vcpu *vcpu)
  9072. {
  9073. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9074. u32 exit_reason = vmx->exit_reason;
  9075. u32 vectoring_info = vmx->idt_vectoring_info;
  9076. trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
  9077. /*
  9078. * Flush logged GPAs PML buffer, this will make dirty_bitmap more
  9079. * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
  9080. * querying dirty_bitmap, we only need to kick all vcpus out of guest
  9081. * mode as if vcpus is in root mode, the PML buffer must has been
  9082. * flushed already.
  9083. */
  9084. if (enable_pml)
  9085. vmx_flush_pml_buffer(vcpu);
  9086. /* If guest state is invalid, start emulating */
  9087. if (vmx->emulation_required)
  9088. return handle_invalid_guest_state(vcpu);
  9089. if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
  9090. return nested_vmx_reflect_vmexit(vcpu, exit_reason);
  9091. if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
  9092. dump_vmcs();
  9093. vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  9094. vcpu->run->fail_entry.hardware_entry_failure_reason
  9095. = exit_reason;
  9096. return 0;
  9097. }
  9098. if (unlikely(vmx->fail)) {
  9099. vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  9100. vcpu->run->fail_entry.hardware_entry_failure_reason
  9101. = vmcs_read32(VM_INSTRUCTION_ERROR);
  9102. return 0;
  9103. }
  9104. /*
  9105. * Note:
  9106. * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
  9107. * delivery event since it indicates guest is accessing MMIO.
  9108. * The vm-exit can be triggered again after return to guest that
  9109. * will cause infinite loop.
  9110. */
  9111. if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
  9112. (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
  9113. exit_reason != EXIT_REASON_EPT_VIOLATION &&
  9114. exit_reason != EXIT_REASON_PML_FULL &&
  9115. exit_reason != EXIT_REASON_TASK_SWITCH)) {
  9116. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  9117. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
  9118. vcpu->run->internal.ndata = 3;
  9119. vcpu->run->internal.data[0] = vectoring_info;
  9120. vcpu->run->internal.data[1] = exit_reason;
  9121. vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
  9122. if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
  9123. vcpu->run->internal.ndata++;
  9124. vcpu->run->internal.data[3] =
  9125. vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  9126. }
  9127. return 0;
  9128. }
  9129. if (unlikely(!enable_vnmi &&
  9130. vmx->loaded_vmcs->soft_vnmi_blocked)) {
  9131. if (vmx_interrupt_allowed(vcpu)) {
  9132. vmx->loaded_vmcs->soft_vnmi_blocked = 0;
  9133. } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
  9134. vcpu->arch.nmi_pending) {
  9135. /*
  9136. * This CPU don't support us in finding the end of an
  9137. * NMI-blocked window if the guest runs with IRQs
  9138. * disabled. So we pull the trigger after 1 s of
  9139. * futile waiting, but inform the user about this.
  9140. */
  9141. printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
  9142. "state on VCPU %d after 1 s timeout\n",
  9143. __func__, vcpu->vcpu_id);
  9144. vmx->loaded_vmcs->soft_vnmi_blocked = 0;
  9145. }
  9146. }
  9147. if (exit_reason < kvm_vmx_max_exit_handlers
  9148. && kvm_vmx_exit_handlers[exit_reason])
  9149. return kvm_vmx_exit_handlers[exit_reason](vcpu);
  9150. else {
  9151. vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
  9152. exit_reason);
  9153. kvm_queue_exception(vcpu, UD_VECTOR);
  9154. return 1;
  9155. }
  9156. }
  9157. /*
  9158. * Software based L1D cache flush which is used when microcode providing
  9159. * the cache control MSR is not loaded.
  9160. *
  9161. * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
  9162. * flush it is required to read in 64 KiB because the replacement algorithm
  9163. * is not exactly LRU. This could be sized at runtime via topology
  9164. * information but as all relevant affected CPUs have 32KiB L1D cache size
  9165. * there is no point in doing so.
  9166. */
  9167. static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
  9168. {
  9169. int size = PAGE_SIZE << L1D_CACHE_ORDER;
  9170. /*
  9171. * This code is only executed when the the flush mode is 'cond' or
  9172. * 'always'
  9173. */
  9174. if (static_branch_likely(&vmx_l1d_flush_cond)) {
  9175. bool flush_l1d;
  9176. /*
  9177. * Clear the per-vcpu flush bit, it gets set again
  9178. * either from vcpu_run() or from one of the unsafe
  9179. * VMEXIT handlers.
  9180. */
  9181. flush_l1d = vcpu->arch.l1tf_flush_l1d;
  9182. vcpu->arch.l1tf_flush_l1d = false;
  9183. /*
  9184. * Clear the per-cpu flush bit, it gets set again from
  9185. * the interrupt handlers.
  9186. */
  9187. flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
  9188. kvm_clear_cpu_l1tf_flush_l1d();
  9189. if (!flush_l1d)
  9190. return;
  9191. }
  9192. vcpu->stat.l1d_flush++;
  9193. if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
  9194. wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
  9195. return;
  9196. }
  9197. asm volatile(
  9198. /* First ensure the pages are in the TLB */
  9199. "xorl %%eax, %%eax\n"
  9200. ".Lpopulate_tlb:\n\t"
  9201. "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
  9202. "addl $4096, %%eax\n\t"
  9203. "cmpl %%eax, %[size]\n\t"
  9204. "jne .Lpopulate_tlb\n\t"
  9205. "xorl %%eax, %%eax\n\t"
  9206. "cpuid\n\t"
  9207. /* Now fill the cache */
  9208. "xorl %%eax, %%eax\n"
  9209. ".Lfill_cache:\n"
  9210. "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
  9211. "addl $64, %%eax\n\t"
  9212. "cmpl %%eax, %[size]\n\t"
  9213. "jne .Lfill_cache\n\t"
  9214. "lfence\n"
  9215. :: [flush_pages] "r" (vmx_l1d_flush_pages),
  9216. [size] "r" (size)
  9217. : "eax", "ebx", "ecx", "edx");
  9218. }
  9219. static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
  9220. {
  9221. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  9222. if (is_guest_mode(vcpu) &&
  9223. nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
  9224. return;
  9225. if (irr == -1 || tpr < irr) {
  9226. vmcs_write32(TPR_THRESHOLD, 0);
  9227. return;
  9228. }
  9229. vmcs_write32(TPR_THRESHOLD, irr);
  9230. }
  9231. static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
  9232. {
  9233. u32 sec_exec_control;
  9234. if (!lapic_in_kernel(vcpu))
  9235. return;
  9236. if (!flexpriority_enabled &&
  9237. !cpu_has_vmx_virtualize_x2apic_mode())
  9238. return;
  9239. /* Postpone execution until vmcs01 is the current VMCS. */
  9240. if (is_guest_mode(vcpu)) {
  9241. to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
  9242. return;
  9243. }
  9244. sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  9245. sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  9246. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
  9247. switch (kvm_get_apic_mode(vcpu)) {
  9248. case LAPIC_MODE_INVALID:
  9249. WARN_ONCE(true, "Invalid local APIC state");
  9250. case LAPIC_MODE_DISABLED:
  9251. break;
  9252. case LAPIC_MODE_XAPIC:
  9253. if (flexpriority_enabled) {
  9254. sec_exec_control |=
  9255. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  9256. vmx_flush_tlb(vcpu, true);
  9257. }
  9258. break;
  9259. case LAPIC_MODE_X2APIC:
  9260. if (cpu_has_vmx_virtualize_x2apic_mode())
  9261. sec_exec_control |=
  9262. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  9263. break;
  9264. }
  9265. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
  9266. vmx_update_msr_bitmap(vcpu);
  9267. }
  9268. static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
  9269. {
  9270. if (!is_guest_mode(vcpu)) {
  9271. vmcs_write64(APIC_ACCESS_ADDR, hpa);
  9272. vmx_flush_tlb(vcpu, true);
  9273. }
  9274. }
  9275. static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
  9276. {
  9277. u16 status;
  9278. u8 old;
  9279. if (max_isr == -1)
  9280. max_isr = 0;
  9281. status = vmcs_read16(GUEST_INTR_STATUS);
  9282. old = status >> 8;
  9283. if (max_isr != old) {
  9284. status &= 0xff;
  9285. status |= max_isr << 8;
  9286. vmcs_write16(GUEST_INTR_STATUS, status);
  9287. }
  9288. }
  9289. static void vmx_set_rvi(int vector)
  9290. {
  9291. u16 status;
  9292. u8 old;
  9293. if (vector == -1)
  9294. vector = 0;
  9295. status = vmcs_read16(GUEST_INTR_STATUS);
  9296. old = (u8)status & 0xff;
  9297. if ((u8)vector != old) {
  9298. status &= ~0xff;
  9299. status |= (u8)vector;
  9300. vmcs_write16(GUEST_INTR_STATUS, status);
  9301. }
  9302. }
  9303. static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
  9304. {
  9305. /*
  9306. * When running L2, updating RVI is only relevant when
  9307. * vmcs12 virtual-interrupt-delivery enabled.
  9308. * However, it can be enabled only when L1 also
  9309. * intercepts external-interrupts and in that case
  9310. * we should not update vmcs02 RVI but instead intercept
  9311. * interrupt. Therefore, do nothing when running L2.
  9312. */
  9313. if (!is_guest_mode(vcpu))
  9314. vmx_set_rvi(max_irr);
  9315. }
  9316. static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
  9317. {
  9318. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9319. int max_irr;
  9320. bool max_irr_updated;
  9321. WARN_ON(!vcpu->arch.apicv_active);
  9322. if (pi_test_on(&vmx->pi_desc)) {
  9323. pi_clear_on(&vmx->pi_desc);
  9324. /*
  9325. * IOMMU can write to PIR.ON, so the barrier matters even on UP.
  9326. * But on x86 this is just a compiler barrier anyway.
  9327. */
  9328. smp_mb__after_atomic();
  9329. max_irr_updated =
  9330. kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
  9331. /*
  9332. * If we are running L2 and L1 has a new pending interrupt
  9333. * which can be injected, we should re-evaluate
  9334. * what should be done with this new L1 interrupt.
  9335. * If L1 intercepts external-interrupts, we should
  9336. * exit from L2 to L1. Otherwise, interrupt should be
  9337. * delivered directly to L2.
  9338. */
  9339. if (is_guest_mode(vcpu) && max_irr_updated) {
  9340. if (nested_exit_on_intr(vcpu))
  9341. kvm_vcpu_exiting_guest_mode(vcpu);
  9342. else
  9343. kvm_make_request(KVM_REQ_EVENT, vcpu);
  9344. }
  9345. } else {
  9346. max_irr = kvm_lapic_find_highest_irr(vcpu);
  9347. }
  9348. vmx_hwapic_irr_update(vcpu, max_irr);
  9349. return max_irr;
  9350. }
  9351. static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu)
  9352. {
  9353. u8 rvi = vmx_get_rvi();
  9354. u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI);
  9355. return ((rvi & 0xf0) > (vppr & 0xf0));
  9356. }
  9357. static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
  9358. {
  9359. if (!kvm_vcpu_apicv_active(vcpu))
  9360. return;
  9361. vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
  9362. vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
  9363. vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
  9364. vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
  9365. }
  9366. static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
  9367. {
  9368. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9369. pi_clear_on(&vmx->pi_desc);
  9370. memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
  9371. }
  9372. static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
  9373. {
  9374. u32 exit_intr_info = 0;
  9375. u16 basic_exit_reason = (u16)vmx->exit_reason;
  9376. if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
  9377. || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
  9378. return;
  9379. if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
  9380. exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  9381. vmx->exit_intr_info = exit_intr_info;
  9382. /* if exit due to PF check for async PF */
  9383. if (is_page_fault(exit_intr_info))
  9384. vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
  9385. /* Handle machine checks before interrupts are enabled */
  9386. if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
  9387. is_machine_check(exit_intr_info))
  9388. kvm_machine_check();
  9389. /* We need to handle NMIs before interrupts are enabled */
  9390. if (is_nmi(exit_intr_info)) {
  9391. kvm_before_interrupt(&vmx->vcpu);
  9392. asm("int $2");
  9393. kvm_after_interrupt(&vmx->vcpu);
  9394. }
  9395. }
  9396. static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
  9397. {
  9398. u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  9399. if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
  9400. == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
  9401. unsigned int vector;
  9402. unsigned long entry;
  9403. gate_desc *desc;
  9404. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9405. #ifdef CONFIG_X86_64
  9406. unsigned long tmp;
  9407. #endif
  9408. vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
  9409. desc = (gate_desc *)vmx->host_idt_base + vector;
  9410. entry = gate_offset(desc);
  9411. asm volatile(
  9412. #ifdef CONFIG_X86_64
  9413. "mov %%" _ASM_SP ", %[sp]\n\t"
  9414. "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
  9415. "push $%c[ss]\n\t"
  9416. "push %[sp]\n\t"
  9417. #endif
  9418. "pushf\n\t"
  9419. __ASM_SIZE(push) " $%c[cs]\n\t"
  9420. CALL_NOSPEC
  9421. :
  9422. #ifdef CONFIG_X86_64
  9423. [sp]"=&r"(tmp),
  9424. #endif
  9425. ASM_CALL_CONSTRAINT
  9426. :
  9427. THUNK_TARGET(entry),
  9428. [ss]"i"(__KERNEL_DS),
  9429. [cs]"i"(__KERNEL_CS)
  9430. );
  9431. }
  9432. }
  9433. STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
  9434. static bool vmx_has_emulated_msr(int index)
  9435. {
  9436. switch (index) {
  9437. case MSR_IA32_SMBASE:
  9438. /*
  9439. * We cannot do SMM unless we can run the guest in big
  9440. * real mode.
  9441. */
  9442. return enable_unrestricted_guest || emulate_invalid_guest_state;
  9443. case MSR_AMD64_VIRT_SPEC_CTRL:
  9444. /* This is AMD only. */
  9445. return false;
  9446. default:
  9447. return true;
  9448. }
  9449. }
  9450. static bool vmx_mpx_supported(void)
  9451. {
  9452. return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
  9453. (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
  9454. }
  9455. static bool vmx_xsaves_supported(void)
  9456. {
  9457. return vmcs_config.cpu_based_2nd_exec_ctrl &
  9458. SECONDARY_EXEC_XSAVES;
  9459. }
  9460. static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
  9461. {
  9462. u32 exit_intr_info;
  9463. bool unblock_nmi;
  9464. u8 vector;
  9465. bool idtv_info_valid;
  9466. idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
  9467. if (enable_vnmi) {
  9468. if (vmx->loaded_vmcs->nmi_known_unmasked)
  9469. return;
  9470. /*
  9471. * Can't use vmx->exit_intr_info since we're not sure what
  9472. * the exit reason is.
  9473. */
  9474. exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  9475. unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
  9476. vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
  9477. /*
  9478. * SDM 3: 27.7.1.2 (September 2008)
  9479. * Re-set bit "block by NMI" before VM entry if vmexit caused by
  9480. * a guest IRET fault.
  9481. * SDM 3: 23.2.2 (September 2008)
  9482. * Bit 12 is undefined in any of the following cases:
  9483. * If the VM exit sets the valid bit in the IDT-vectoring
  9484. * information field.
  9485. * If the VM exit is due to a double fault.
  9486. */
  9487. if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
  9488. vector != DF_VECTOR && !idtv_info_valid)
  9489. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  9490. GUEST_INTR_STATE_NMI);
  9491. else
  9492. vmx->loaded_vmcs->nmi_known_unmasked =
  9493. !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
  9494. & GUEST_INTR_STATE_NMI);
  9495. } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
  9496. vmx->loaded_vmcs->vnmi_blocked_time +=
  9497. ktime_to_ns(ktime_sub(ktime_get(),
  9498. vmx->loaded_vmcs->entry_time));
  9499. }
  9500. static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
  9501. u32 idt_vectoring_info,
  9502. int instr_len_field,
  9503. int error_code_field)
  9504. {
  9505. u8 vector;
  9506. int type;
  9507. bool idtv_info_valid;
  9508. idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
  9509. vcpu->arch.nmi_injected = false;
  9510. kvm_clear_exception_queue(vcpu);
  9511. kvm_clear_interrupt_queue(vcpu);
  9512. if (!idtv_info_valid)
  9513. return;
  9514. kvm_make_request(KVM_REQ_EVENT, vcpu);
  9515. vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
  9516. type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
  9517. switch (type) {
  9518. case INTR_TYPE_NMI_INTR:
  9519. vcpu->arch.nmi_injected = true;
  9520. /*
  9521. * SDM 3: 27.7.1.2 (September 2008)
  9522. * Clear bit "block by NMI" before VM entry if a NMI
  9523. * delivery faulted.
  9524. */
  9525. vmx_set_nmi_mask(vcpu, false);
  9526. break;
  9527. case INTR_TYPE_SOFT_EXCEPTION:
  9528. vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
  9529. /* fall through */
  9530. case INTR_TYPE_HARD_EXCEPTION:
  9531. if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
  9532. u32 err = vmcs_read32(error_code_field);
  9533. kvm_requeue_exception_e(vcpu, vector, err);
  9534. } else
  9535. kvm_requeue_exception(vcpu, vector);
  9536. break;
  9537. case INTR_TYPE_SOFT_INTR:
  9538. vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
  9539. /* fall through */
  9540. case INTR_TYPE_EXT_INTR:
  9541. kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
  9542. break;
  9543. default:
  9544. break;
  9545. }
  9546. }
  9547. static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
  9548. {
  9549. __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
  9550. VM_EXIT_INSTRUCTION_LEN,
  9551. IDT_VECTORING_ERROR_CODE);
  9552. }
  9553. static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
  9554. {
  9555. __vmx_complete_interrupts(vcpu,
  9556. vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
  9557. VM_ENTRY_INSTRUCTION_LEN,
  9558. VM_ENTRY_EXCEPTION_ERROR_CODE);
  9559. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
  9560. }
  9561. static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
  9562. {
  9563. int i, nr_msrs;
  9564. struct perf_guest_switch_msr *msrs;
  9565. msrs = perf_guest_get_msrs(&nr_msrs);
  9566. if (!msrs)
  9567. return;
  9568. for (i = 0; i < nr_msrs; i++)
  9569. if (msrs[i].host == msrs[i].guest)
  9570. clear_atomic_switch_msr(vmx, msrs[i].msr);
  9571. else
  9572. add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
  9573. msrs[i].host, false);
  9574. }
  9575. static void vmx_arm_hv_timer(struct vcpu_vmx *vmx, u32 val)
  9576. {
  9577. vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, val);
  9578. if (!vmx->loaded_vmcs->hv_timer_armed)
  9579. vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
  9580. PIN_BASED_VMX_PREEMPTION_TIMER);
  9581. vmx->loaded_vmcs->hv_timer_armed = true;
  9582. }
  9583. static void vmx_update_hv_timer(struct kvm_vcpu *vcpu)
  9584. {
  9585. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9586. u64 tscl;
  9587. u32 delta_tsc;
  9588. if (vmx->req_immediate_exit) {
  9589. vmx_arm_hv_timer(vmx, 0);
  9590. return;
  9591. }
  9592. if (vmx->hv_deadline_tsc != -1) {
  9593. tscl = rdtsc();
  9594. if (vmx->hv_deadline_tsc > tscl)
  9595. /* set_hv_timer ensures the delta fits in 32-bits */
  9596. delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
  9597. cpu_preemption_timer_multi);
  9598. else
  9599. delta_tsc = 0;
  9600. vmx_arm_hv_timer(vmx, delta_tsc);
  9601. return;
  9602. }
  9603. if (vmx->loaded_vmcs->hv_timer_armed)
  9604. vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
  9605. PIN_BASED_VMX_PREEMPTION_TIMER);
  9606. vmx->loaded_vmcs->hv_timer_armed = false;
  9607. }
  9608. static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
  9609. {
  9610. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9611. unsigned long cr3, cr4, evmcs_rsp;
  9612. /* Record the guest's net vcpu time for enforced NMI injections. */
  9613. if (unlikely(!enable_vnmi &&
  9614. vmx->loaded_vmcs->soft_vnmi_blocked))
  9615. vmx->loaded_vmcs->entry_time = ktime_get();
  9616. /* Don't enter VMX if guest state is invalid, let the exit handler
  9617. start emulation until we arrive back to a valid state */
  9618. if (vmx->emulation_required)
  9619. return;
  9620. if (vmx->ple_window_dirty) {
  9621. vmx->ple_window_dirty = false;
  9622. vmcs_write32(PLE_WINDOW, vmx->ple_window);
  9623. }
  9624. if (vmx->nested.need_vmcs12_sync) {
  9625. /*
  9626. * hv_evmcs may end up being not mapped after migration (when
  9627. * L2 was running), map it here to make sure vmcs12 changes are
  9628. * properly reflected.
  9629. */
  9630. if (vmx->nested.enlightened_vmcs_enabled &&
  9631. !vmx->nested.hv_evmcs)
  9632. nested_vmx_handle_enlightened_vmptrld(vcpu, false);
  9633. if (vmx->nested.hv_evmcs) {
  9634. copy_vmcs12_to_enlightened(vmx);
  9635. /* All fields are clean */
  9636. vmx->nested.hv_evmcs->hv_clean_fields |=
  9637. HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
  9638. } else {
  9639. copy_vmcs12_to_shadow(vmx);
  9640. }
  9641. vmx->nested.need_vmcs12_sync = false;
  9642. }
  9643. if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
  9644. vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
  9645. if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
  9646. vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
  9647. cr3 = __get_current_cr3_fast();
  9648. if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
  9649. vmcs_writel(HOST_CR3, cr3);
  9650. vmx->loaded_vmcs->host_state.cr3 = cr3;
  9651. }
  9652. cr4 = cr4_read_shadow();
  9653. if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
  9654. vmcs_writel(HOST_CR4, cr4);
  9655. vmx->loaded_vmcs->host_state.cr4 = cr4;
  9656. }
  9657. /* When single-stepping over STI and MOV SS, we must clear the
  9658. * corresponding interruptibility bits in the guest state. Otherwise
  9659. * vmentry fails as it then expects bit 14 (BS) in pending debug
  9660. * exceptions being set, but that's not correct for the guest debugging
  9661. * case. */
  9662. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
  9663. vmx_set_interrupt_shadow(vcpu, 0);
  9664. if (static_cpu_has(X86_FEATURE_PKU) &&
  9665. kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
  9666. vcpu->arch.pkru != vmx->host_pkru)
  9667. __write_pkru(vcpu->arch.pkru);
  9668. atomic_switch_perf_msrs(vmx);
  9669. vmx_update_hv_timer(vcpu);
  9670. /*
  9671. * If this vCPU has touched SPEC_CTRL, restore the guest's value if
  9672. * it's non-zero. Since vmentry is serialising on affected CPUs, there
  9673. * is no need to worry about the conditional branch over the wrmsr
  9674. * being speculatively taken.
  9675. */
  9676. x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
  9677. vmx->__launched = vmx->loaded_vmcs->launched;
  9678. evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
  9679. (unsigned long)&current_evmcs->host_rsp : 0;
  9680. if (static_branch_unlikely(&vmx_l1d_should_flush))
  9681. vmx_l1d_flush(vcpu);
  9682. asm(
  9683. /* Store host registers */
  9684. "push %%" _ASM_DX "; push %%" _ASM_BP ";"
  9685. "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
  9686. "push %%" _ASM_CX " \n\t"
  9687. "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
  9688. "je 1f \n\t"
  9689. "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
  9690. /* Avoid VMWRITE when Enlightened VMCS is in use */
  9691. "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
  9692. "jz 2f \n\t"
  9693. "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
  9694. "jmp 1f \n\t"
  9695. "2: \n\t"
  9696. __ex("vmwrite %%" _ASM_SP ", %%" _ASM_DX) "\n\t"
  9697. "1: \n\t"
  9698. /* Reload cr2 if changed */
  9699. "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
  9700. "mov %%cr2, %%" _ASM_DX " \n\t"
  9701. "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
  9702. "je 3f \n\t"
  9703. "mov %%" _ASM_AX", %%cr2 \n\t"
  9704. "3: \n\t"
  9705. /* Check if vmlaunch of vmresume is needed */
  9706. "cmpl $0, %c[launched](%0) \n\t"
  9707. /* Load guest registers. Don't clobber flags. */
  9708. "mov %c[rax](%0), %%" _ASM_AX " \n\t"
  9709. "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
  9710. "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
  9711. "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
  9712. "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
  9713. "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
  9714. #ifdef CONFIG_X86_64
  9715. "mov %c[r8](%0), %%r8 \n\t"
  9716. "mov %c[r9](%0), %%r9 \n\t"
  9717. "mov %c[r10](%0), %%r10 \n\t"
  9718. "mov %c[r11](%0), %%r11 \n\t"
  9719. "mov %c[r12](%0), %%r12 \n\t"
  9720. "mov %c[r13](%0), %%r13 \n\t"
  9721. "mov %c[r14](%0), %%r14 \n\t"
  9722. "mov %c[r15](%0), %%r15 \n\t"
  9723. #endif
  9724. "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
  9725. /* Enter guest mode */
  9726. "jne 1f \n\t"
  9727. __ex("vmlaunch") "\n\t"
  9728. "jmp 2f \n\t"
  9729. "1: " __ex("vmresume") "\n\t"
  9730. "2: "
  9731. /* Save guest registers, load host registers, keep flags */
  9732. "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
  9733. "pop %0 \n\t"
  9734. "setbe %c[fail](%0)\n\t"
  9735. "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
  9736. "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
  9737. __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
  9738. "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
  9739. "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
  9740. "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
  9741. "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
  9742. #ifdef CONFIG_X86_64
  9743. "mov %%r8, %c[r8](%0) \n\t"
  9744. "mov %%r9, %c[r9](%0) \n\t"
  9745. "mov %%r10, %c[r10](%0) \n\t"
  9746. "mov %%r11, %c[r11](%0) \n\t"
  9747. "mov %%r12, %c[r12](%0) \n\t"
  9748. "mov %%r13, %c[r13](%0) \n\t"
  9749. "mov %%r14, %c[r14](%0) \n\t"
  9750. "mov %%r15, %c[r15](%0) \n\t"
  9751. /*
  9752. * Clear host registers marked as clobbered to prevent
  9753. * speculative use.
  9754. */
  9755. "xor %%r8d, %%r8d \n\t"
  9756. "xor %%r9d, %%r9d \n\t"
  9757. "xor %%r10d, %%r10d \n\t"
  9758. "xor %%r11d, %%r11d \n\t"
  9759. "xor %%r12d, %%r12d \n\t"
  9760. "xor %%r13d, %%r13d \n\t"
  9761. "xor %%r14d, %%r14d \n\t"
  9762. "xor %%r15d, %%r15d \n\t"
  9763. #endif
  9764. "mov %%cr2, %%" _ASM_AX " \n\t"
  9765. "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
  9766. "xor %%eax, %%eax \n\t"
  9767. "xor %%ebx, %%ebx \n\t"
  9768. "xor %%esi, %%esi \n\t"
  9769. "xor %%edi, %%edi \n\t"
  9770. "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
  9771. ".pushsection .rodata \n\t"
  9772. ".global vmx_return \n\t"
  9773. "vmx_return: " _ASM_PTR " 2b \n\t"
  9774. ".popsection"
  9775. : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
  9776. [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
  9777. [fail]"i"(offsetof(struct vcpu_vmx, fail)),
  9778. [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
  9779. [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
  9780. [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
  9781. [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
  9782. [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
  9783. [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
  9784. [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
  9785. [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
  9786. #ifdef CONFIG_X86_64
  9787. [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
  9788. [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
  9789. [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
  9790. [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
  9791. [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
  9792. [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
  9793. [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
  9794. [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
  9795. #endif
  9796. [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
  9797. [wordsize]"i"(sizeof(ulong))
  9798. : "cc", "memory"
  9799. #ifdef CONFIG_X86_64
  9800. , "rax", "rbx", "rdi"
  9801. , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
  9802. #else
  9803. , "eax", "ebx", "edi"
  9804. #endif
  9805. );
  9806. /*
  9807. * We do not use IBRS in the kernel. If this vCPU has used the
  9808. * SPEC_CTRL MSR it may have left it on; save the value and
  9809. * turn it off. This is much more efficient than blindly adding
  9810. * it to the atomic save/restore list. Especially as the former
  9811. * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
  9812. *
  9813. * For non-nested case:
  9814. * If the L01 MSR bitmap does not intercept the MSR, then we need to
  9815. * save it.
  9816. *
  9817. * For nested case:
  9818. * If the L02 MSR bitmap does not intercept the MSR, then we need to
  9819. * save it.
  9820. */
  9821. if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
  9822. vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
  9823. x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
  9824. /* Eliminate branch target predictions from guest mode */
  9825. vmexit_fill_RSB();
  9826. /* All fields are clean at this point */
  9827. if (static_branch_unlikely(&enable_evmcs))
  9828. current_evmcs->hv_clean_fields |=
  9829. HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
  9830. /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
  9831. if (vmx->host_debugctlmsr)
  9832. update_debugctlmsr(vmx->host_debugctlmsr);
  9833. #ifndef CONFIG_X86_64
  9834. /*
  9835. * The sysexit path does not restore ds/es, so we must set them to
  9836. * a reasonable value ourselves.
  9837. *
  9838. * We can't defer this to vmx_prepare_switch_to_host() since that
  9839. * function may be executed in interrupt context, which saves and
  9840. * restore segments around it, nullifying its effect.
  9841. */
  9842. loadsegment(ds, __USER_DS);
  9843. loadsegment(es, __USER_DS);
  9844. #endif
  9845. vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
  9846. | (1 << VCPU_EXREG_RFLAGS)
  9847. | (1 << VCPU_EXREG_PDPTR)
  9848. | (1 << VCPU_EXREG_SEGMENTS)
  9849. | (1 << VCPU_EXREG_CR3));
  9850. vcpu->arch.regs_dirty = 0;
  9851. /*
  9852. * eager fpu is enabled if PKEY is supported and CR4 is switched
  9853. * back on host, so it is safe to read guest PKRU from current
  9854. * XSAVE.
  9855. */
  9856. if (static_cpu_has(X86_FEATURE_PKU) &&
  9857. kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
  9858. vcpu->arch.pkru = __read_pkru();
  9859. if (vcpu->arch.pkru != vmx->host_pkru)
  9860. __write_pkru(vmx->host_pkru);
  9861. }
  9862. vmx->nested.nested_run_pending = 0;
  9863. vmx->idt_vectoring_info = 0;
  9864. vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
  9865. if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
  9866. return;
  9867. vmx->loaded_vmcs->launched = 1;
  9868. vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
  9869. vmx_complete_atomic_exit(vmx);
  9870. vmx_recover_nmi_blocking(vmx);
  9871. vmx_complete_interrupts(vmx);
  9872. }
  9873. STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
  9874. static struct kvm *vmx_vm_alloc(void)
  9875. {
  9876. struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
  9877. return &kvm_vmx->kvm;
  9878. }
  9879. static void vmx_vm_free(struct kvm *kvm)
  9880. {
  9881. vfree(to_kvm_vmx(kvm));
  9882. }
  9883. static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
  9884. {
  9885. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9886. int cpu;
  9887. if (vmx->loaded_vmcs == vmcs)
  9888. return;
  9889. cpu = get_cpu();
  9890. vmx_vcpu_put(vcpu);
  9891. vmx->loaded_vmcs = vmcs;
  9892. vmx_vcpu_load(vcpu, cpu);
  9893. put_cpu();
  9894. vm_entry_controls_reset_shadow(vmx);
  9895. vm_exit_controls_reset_shadow(vmx);
  9896. vmx_segment_cache_clear(vmx);
  9897. }
  9898. /*
  9899. * Ensure that the current vmcs of the logical processor is the
  9900. * vmcs01 of the vcpu before calling free_nested().
  9901. */
  9902. static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
  9903. {
  9904. vcpu_load(vcpu);
  9905. vmx_switch_vmcs(vcpu, &to_vmx(vcpu)->vmcs01);
  9906. free_nested(vcpu);
  9907. vcpu_put(vcpu);
  9908. }
  9909. static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
  9910. {
  9911. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9912. if (enable_pml)
  9913. vmx_destroy_pml_buffer(vmx);
  9914. free_vpid(vmx->vpid);
  9915. leave_guest_mode(vcpu);
  9916. vmx_free_vcpu_nested(vcpu);
  9917. free_loaded_vmcs(vmx->loaded_vmcs);
  9918. kfree(vmx->guest_msrs);
  9919. kvm_vcpu_uninit(vcpu);
  9920. kmem_cache_free(kvm_vcpu_cache, vmx);
  9921. }
  9922. static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
  9923. {
  9924. int err;
  9925. struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  9926. unsigned long *msr_bitmap;
  9927. int cpu;
  9928. if (!vmx)
  9929. return ERR_PTR(-ENOMEM);
  9930. vmx->vpid = allocate_vpid();
  9931. err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
  9932. if (err)
  9933. goto free_vcpu;
  9934. err = -ENOMEM;
  9935. /*
  9936. * If PML is turned on, failure on enabling PML just results in failure
  9937. * of creating the vcpu, therefore we can simplify PML logic (by
  9938. * avoiding dealing with cases, such as enabling PML partially on vcpus
  9939. * for the guest, etc.
  9940. */
  9941. if (enable_pml) {
  9942. vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
  9943. if (!vmx->pml_pg)
  9944. goto uninit_vcpu;
  9945. }
  9946. vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
  9947. BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
  9948. > PAGE_SIZE);
  9949. if (!vmx->guest_msrs)
  9950. goto free_pml;
  9951. err = alloc_loaded_vmcs(&vmx->vmcs01);
  9952. if (err < 0)
  9953. goto free_msrs;
  9954. msr_bitmap = vmx->vmcs01.msr_bitmap;
  9955. vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
  9956. vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
  9957. vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
  9958. vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
  9959. vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
  9960. vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
  9961. vmx->msr_bitmap_mode = 0;
  9962. vmx->loaded_vmcs = &vmx->vmcs01;
  9963. cpu = get_cpu();
  9964. vmx_vcpu_load(&vmx->vcpu, cpu);
  9965. vmx->vcpu.cpu = cpu;
  9966. vmx_vcpu_setup(vmx);
  9967. vmx_vcpu_put(&vmx->vcpu);
  9968. put_cpu();
  9969. if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
  9970. err = alloc_apic_access_page(kvm);
  9971. if (err)
  9972. goto free_vmcs;
  9973. }
  9974. if (enable_ept && !enable_unrestricted_guest) {
  9975. err = init_rmode_identity_map(kvm);
  9976. if (err)
  9977. goto free_vmcs;
  9978. }
  9979. if (nested)
  9980. nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
  9981. kvm_vcpu_apicv_active(&vmx->vcpu));
  9982. vmx->nested.posted_intr_nv = -1;
  9983. vmx->nested.current_vmptr = -1ull;
  9984. vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
  9985. /*
  9986. * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
  9987. * or POSTED_INTR_WAKEUP_VECTOR.
  9988. */
  9989. vmx->pi_desc.nv = POSTED_INTR_VECTOR;
  9990. vmx->pi_desc.sn = 1;
  9991. return &vmx->vcpu;
  9992. free_vmcs:
  9993. free_loaded_vmcs(vmx->loaded_vmcs);
  9994. free_msrs:
  9995. kfree(vmx->guest_msrs);
  9996. free_pml:
  9997. vmx_destroy_pml_buffer(vmx);
  9998. uninit_vcpu:
  9999. kvm_vcpu_uninit(&vmx->vcpu);
  10000. free_vcpu:
  10001. free_vpid(vmx->vpid);
  10002. kmem_cache_free(kvm_vcpu_cache, vmx);
  10003. return ERR_PTR(err);
  10004. }
  10005. #define L1TF_MSG_SMT "L1TF CPU bug present and SMT on, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/l1tf.html for details.\n"
  10006. #define L1TF_MSG_L1D "L1TF CPU bug present and virtualization mitigation disabled, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/l1tf.html for details.\n"
  10007. static int vmx_vm_init(struct kvm *kvm)
  10008. {
  10009. spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
  10010. if (!ple_gap)
  10011. kvm->arch.pause_in_guest = true;
  10012. if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
  10013. switch (l1tf_mitigation) {
  10014. case L1TF_MITIGATION_OFF:
  10015. case L1TF_MITIGATION_FLUSH_NOWARN:
  10016. /* 'I explicitly don't care' is set */
  10017. break;
  10018. case L1TF_MITIGATION_FLUSH:
  10019. case L1TF_MITIGATION_FLUSH_NOSMT:
  10020. case L1TF_MITIGATION_FULL:
  10021. /*
  10022. * Warn upon starting the first VM in a potentially
  10023. * insecure environment.
  10024. */
  10025. if (cpu_smt_control == CPU_SMT_ENABLED)
  10026. pr_warn_once(L1TF_MSG_SMT);
  10027. if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
  10028. pr_warn_once(L1TF_MSG_L1D);
  10029. break;
  10030. case L1TF_MITIGATION_FULL_FORCE:
  10031. /* Flush is enforced */
  10032. break;
  10033. }
  10034. }
  10035. return 0;
  10036. }
  10037. static void __init vmx_check_processor_compat(void *rtn)
  10038. {
  10039. struct vmcs_config vmcs_conf;
  10040. *(int *)rtn = 0;
  10041. if (setup_vmcs_config(&vmcs_conf) < 0)
  10042. *(int *)rtn = -EIO;
  10043. nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
  10044. if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
  10045. printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
  10046. smp_processor_id());
  10047. *(int *)rtn = -EIO;
  10048. }
  10049. }
  10050. static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
  10051. {
  10052. u8 cache;
  10053. u64 ipat = 0;
  10054. /* For VT-d and EPT combination
  10055. * 1. MMIO: always map as UC
  10056. * 2. EPT with VT-d:
  10057. * a. VT-d without snooping control feature: can't guarantee the
  10058. * result, try to trust guest.
  10059. * b. VT-d with snooping control feature: snooping control feature of
  10060. * VT-d engine can guarantee the cache correctness. Just set it
  10061. * to WB to keep consistent with host. So the same as item 3.
  10062. * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
  10063. * consistent with host MTRR
  10064. */
  10065. if (is_mmio) {
  10066. cache = MTRR_TYPE_UNCACHABLE;
  10067. goto exit;
  10068. }
  10069. if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
  10070. ipat = VMX_EPT_IPAT_BIT;
  10071. cache = MTRR_TYPE_WRBACK;
  10072. goto exit;
  10073. }
  10074. if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
  10075. ipat = VMX_EPT_IPAT_BIT;
  10076. if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
  10077. cache = MTRR_TYPE_WRBACK;
  10078. else
  10079. cache = MTRR_TYPE_UNCACHABLE;
  10080. goto exit;
  10081. }
  10082. cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
  10083. exit:
  10084. return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
  10085. }
  10086. static int vmx_get_lpage_level(void)
  10087. {
  10088. if (enable_ept && !cpu_has_vmx_ept_1g_page())
  10089. return PT_DIRECTORY_LEVEL;
  10090. else
  10091. /* For shadow and EPT supported 1GB page */
  10092. return PT_PDPE_LEVEL;
  10093. }
  10094. static void vmcs_set_secondary_exec_control(u32 new_ctl)
  10095. {
  10096. /*
  10097. * These bits in the secondary execution controls field
  10098. * are dynamic, the others are mostly based on the hypervisor
  10099. * architecture and the guest's CPUID. Do not touch the
  10100. * dynamic bits.
  10101. */
  10102. u32 mask =
  10103. SECONDARY_EXEC_SHADOW_VMCS |
  10104. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  10105. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  10106. SECONDARY_EXEC_DESC;
  10107. u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  10108. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  10109. (new_ctl & ~mask) | (cur_ctl & mask));
  10110. }
  10111. /*
  10112. * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
  10113. * (indicating "allowed-1") if they are supported in the guest's CPUID.
  10114. */
  10115. static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
  10116. {
  10117. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10118. struct kvm_cpuid_entry2 *entry;
  10119. vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
  10120. vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
  10121. #define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
  10122. if (entry && (entry->_reg & (_cpuid_mask))) \
  10123. vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
  10124. } while (0)
  10125. entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
  10126. cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
  10127. cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
  10128. cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
  10129. cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
  10130. cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
  10131. cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
  10132. cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
  10133. cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
  10134. cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
  10135. cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
  10136. cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
  10137. cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
  10138. cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
  10139. cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
  10140. entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
  10141. cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
  10142. cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
  10143. cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
  10144. cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
  10145. cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
  10146. #undef cr4_fixed1_update
  10147. }
  10148. static void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
  10149. {
  10150. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10151. if (kvm_mpx_supported()) {
  10152. bool mpx_enabled = guest_cpuid_has(vcpu, X86_FEATURE_MPX);
  10153. if (mpx_enabled) {
  10154. vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
  10155. vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
  10156. } else {
  10157. vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS;
  10158. vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS;
  10159. }
  10160. }
  10161. }
  10162. static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
  10163. {
  10164. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10165. if (cpu_has_secondary_exec_ctrls()) {
  10166. vmx_compute_secondary_exec_control(vmx);
  10167. vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
  10168. }
  10169. if (nested_vmx_allowed(vcpu))
  10170. to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
  10171. FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  10172. else
  10173. to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
  10174. ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  10175. if (nested_vmx_allowed(vcpu)) {
  10176. nested_vmx_cr_fixed1_bits_update(vcpu);
  10177. nested_vmx_entry_exit_ctls_update(vcpu);
  10178. }
  10179. }
  10180. static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
  10181. {
  10182. if (func == 1 && nested)
  10183. entry->ecx |= bit(X86_FEATURE_VMX);
  10184. }
  10185. static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
  10186. struct x86_exception *fault)
  10187. {
  10188. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  10189. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10190. u32 exit_reason;
  10191. unsigned long exit_qualification = vcpu->arch.exit_qualification;
  10192. if (vmx->nested.pml_full) {
  10193. exit_reason = EXIT_REASON_PML_FULL;
  10194. vmx->nested.pml_full = false;
  10195. exit_qualification &= INTR_INFO_UNBLOCK_NMI;
  10196. } else if (fault->error_code & PFERR_RSVD_MASK)
  10197. exit_reason = EXIT_REASON_EPT_MISCONFIG;
  10198. else
  10199. exit_reason = EXIT_REASON_EPT_VIOLATION;
  10200. nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
  10201. vmcs12->guest_physical_address = fault->address;
  10202. }
  10203. static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
  10204. {
  10205. return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
  10206. }
  10207. /* Callbacks for nested_ept_init_mmu_context: */
  10208. static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
  10209. {
  10210. /* return the page table to be shadowed - in our case, EPT12 */
  10211. return get_vmcs12(vcpu)->ept_pointer;
  10212. }
  10213. static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
  10214. {
  10215. WARN_ON(mmu_is_nested(vcpu));
  10216. vcpu->arch.mmu = &vcpu->arch.guest_mmu;
  10217. kvm_init_shadow_ept_mmu(vcpu,
  10218. to_vmx(vcpu)->nested.msrs.ept_caps &
  10219. VMX_EPT_EXECUTE_ONLY_BIT,
  10220. nested_ept_ad_enabled(vcpu),
  10221. nested_ept_get_cr3(vcpu));
  10222. vcpu->arch.mmu->set_cr3 = vmx_set_cr3;
  10223. vcpu->arch.mmu->get_cr3 = nested_ept_get_cr3;
  10224. vcpu->arch.mmu->inject_page_fault = nested_ept_inject_page_fault;
  10225. vcpu->arch.mmu->get_pdptr = kvm_pdptr_read;
  10226. vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
  10227. }
  10228. static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
  10229. {
  10230. vcpu->arch.mmu = &vcpu->arch.root_mmu;
  10231. vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
  10232. }
  10233. static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
  10234. u16 error_code)
  10235. {
  10236. bool inequality, bit;
  10237. bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
  10238. inequality =
  10239. (error_code & vmcs12->page_fault_error_code_mask) !=
  10240. vmcs12->page_fault_error_code_match;
  10241. return inequality ^ bit;
  10242. }
  10243. static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
  10244. struct x86_exception *fault)
  10245. {
  10246. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  10247. WARN_ON(!is_guest_mode(vcpu));
  10248. if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
  10249. !to_vmx(vcpu)->nested.nested_run_pending) {
  10250. vmcs12->vm_exit_intr_error_code = fault->error_code;
  10251. nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
  10252. PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
  10253. INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
  10254. fault->address);
  10255. } else {
  10256. kvm_inject_page_fault(vcpu, fault);
  10257. }
  10258. }
  10259. static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
  10260. struct vmcs12 *vmcs12);
  10261. static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
  10262. {
  10263. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  10264. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10265. struct page *page;
  10266. u64 hpa;
  10267. if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
  10268. /*
  10269. * Translate L1 physical address to host physical
  10270. * address for vmcs02. Keep the page pinned, so this
  10271. * physical address remains valid. We keep a reference
  10272. * to it so we can release it later.
  10273. */
  10274. if (vmx->nested.apic_access_page) { /* shouldn't happen */
  10275. kvm_release_page_dirty(vmx->nested.apic_access_page);
  10276. vmx->nested.apic_access_page = NULL;
  10277. }
  10278. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
  10279. /*
  10280. * If translation failed, no matter: This feature asks
  10281. * to exit when accessing the given address, and if it
  10282. * can never be accessed, this feature won't do
  10283. * anything anyway.
  10284. */
  10285. if (!is_error_page(page)) {
  10286. vmx->nested.apic_access_page = page;
  10287. hpa = page_to_phys(vmx->nested.apic_access_page);
  10288. vmcs_write64(APIC_ACCESS_ADDR, hpa);
  10289. } else {
  10290. vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
  10291. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
  10292. }
  10293. }
  10294. if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
  10295. if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
  10296. kvm_release_page_dirty(vmx->nested.virtual_apic_page);
  10297. vmx->nested.virtual_apic_page = NULL;
  10298. }
  10299. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
  10300. /*
  10301. * If translation failed, VM entry will fail because
  10302. * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
  10303. * Failing the vm entry is _not_ what the processor
  10304. * does but it's basically the only possibility we
  10305. * have. We could still enter the guest if CR8 load
  10306. * exits are enabled, CR8 store exits are enabled, and
  10307. * virtualize APIC access is disabled; in this case
  10308. * the processor would never use the TPR shadow and we
  10309. * could simply clear the bit from the execution
  10310. * control. But such a configuration is useless, so
  10311. * let's keep the code simple.
  10312. */
  10313. if (!is_error_page(page)) {
  10314. vmx->nested.virtual_apic_page = page;
  10315. hpa = page_to_phys(vmx->nested.virtual_apic_page);
  10316. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
  10317. }
  10318. }
  10319. if (nested_cpu_has_posted_intr(vmcs12)) {
  10320. if (vmx->nested.pi_desc_page) { /* shouldn't happen */
  10321. kunmap(vmx->nested.pi_desc_page);
  10322. kvm_release_page_dirty(vmx->nested.pi_desc_page);
  10323. vmx->nested.pi_desc_page = NULL;
  10324. }
  10325. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
  10326. if (is_error_page(page))
  10327. return;
  10328. vmx->nested.pi_desc_page = page;
  10329. vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
  10330. vmx->nested.pi_desc =
  10331. (struct pi_desc *)((void *)vmx->nested.pi_desc +
  10332. (unsigned long)(vmcs12->posted_intr_desc_addr &
  10333. (PAGE_SIZE - 1)));
  10334. vmcs_write64(POSTED_INTR_DESC_ADDR,
  10335. page_to_phys(vmx->nested.pi_desc_page) +
  10336. (unsigned long)(vmcs12->posted_intr_desc_addr &
  10337. (PAGE_SIZE - 1)));
  10338. }
  10339. if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
  10340. vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
  10341. CPU_BASED_USE_MSR_BITMAPS);
  10342. else
  10343. vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
  10344. CPU_BASED_USE_MSR_BITMAPS);
  10345. }
  10346. static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
  10347. {
  10348. u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
  10349. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10350. /*
  10351. * A timer value of zero is architecturally guaranteed to cause
  10352. * a VMExit prior to executing any instructions in the guest.
  10353. */
  10354. if (preemption_timeout == 0) {
  10355. vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
  10356. return;
  10357. }
  10358. if (vcpu->arch.virtual_tsc_khz == 0)
  10359. return;
  10360. preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
  10361. preemption_timeout *= 1000000;
  10362. do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
  10363. hrtimer_start(&vmx->nested.preemption_timer,
  10364. ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
  10365. }
  10366. static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
  10367. struct vmcs12 *vmcs12)
  10368. {
  10369. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
  10370. return 0;
  10371. if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
  10372. !page_address_valid(vcpu, vmcs12->io_bitmap_b))
  10373. return -EINVAL;
  10374. return 0;
  10375. }
  10376. static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
  10377. struct vmcs12 *vmcs12)
  10378. {
  10379. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
  10380. return 0;
  10381. if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
  10382. return -EINVAL;
  10383. return 0;
  10384. }
  10385. static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
  10386. struct vmcs12 *vmcs12)
  10387. {
  10388. if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
  10389. return 0;
  10390. if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
  10391. return -EINVAL;
  10392. return 0;
  10393. }
  10394. /*
  10395. * Merge L0's and L1's MSR bitmap, return false to indicate that
  10396. * we do not use the hardware.
  10397. */
  10398. static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
  10399. struct vmcs12 *vmcs12)
  10400. {
  10401. int msr;
  10402. struct page *page;
  10403. unsigned long *msr_bitmap_l1;
  10404. unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
  10405. /*
  10406. * pred_cmd & spec_ctrl are trying to verify two things:
  10407. *
  10408. * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
  10409. * ensures that we do not accidentally generate an L02 MSR bitmap
  10410. * from the L12 MSR bitmap that is too permissive.
  10411. * 2. That L1 or L2s have actually used the MSR. This avoids
  10412. * unnecessarily merging of the bitmap if the MSR is unused. This
  10413. * works properly because we only update the L01 MSR bitmap lazily.
  10414. * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
  10415. * updated to reflect this when L1 (or its L2s) actually write to
  10416. * the MSR.
  10417. */
  10418. bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
  10419. bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
  10420. /* Nothing to do if the MSR bitmap is not in use. */
  10421. if (!cpu_has_vmx_msr_bitmap() ||
  10422. !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
  10423. return false;
  10424. if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
  10425. !pred_cmd && !spec_ctrl)
  10426. return false;
  10427. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
  10428. if (is_error_page(page))
  10429. return false;
  10430. msr_bitmap_l1 = (unsigned long *)kmap(page);
  10431. if (nested_cpu_has_apic_reg_virt(vmcs12)) {
  10432. /*
  10433. * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
  10434. * just lets the processor take the value from the virtual-APIC page;
  10435. * take those 256 bits directly from the L1 bitmap.
  10436. */
  10437. for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
  10438. unsigned word = msr / BITS_PER_LONG;
  10439. msr_bitmap_l0[word] = msr_bitmap_l1[word];
  10440. msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
  10441. }
  10442. } else {
  10443. for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
  10444. unsigned word = msr / BITS_PER_LONG;
  10445. msr_bitmap_l0[word] = ~0;
  10446. msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
  10447. }
  10448. }
  10449. nested_vmx_disable_intercept_for_msr(
  10450. msr_bitmap_l1, msr_bitmap_l0,
  10451. X2APIC_MSR(APIC_TASKPRI),
  10452. MSR_TYPE_W);
  10453. if (nested_cpu_has_vid(vmcs12)) {
  10454. nested_vmx_disable_intercept_for_msr(
  10455. msr_bitmap_l1, msr_bitmap_l0,
  10456. X2APIC_MSR(APIC_EOI),
  10457. MSR_TYPE_W);
  10458. nested_vmx_disable_intercept_for_msr(
  10459. msr_bitmap_l1, msr_bitmap_l0,
  10460. X2APIC_MSR(APIC_SELF_IPI),
  10461. MSR_TYPE_W);
  10462. }
  10463. if (spec_ctrl)
  10464. nested_vmx_disable_intercept_for_msr(
  10465. msr_bitmap_l1, msr_bitmap_l0,
  10466. MSR_IA32_SPEC_CTRL,
  10467. MSR_TYPE_R | MSR_TYPE_W);
  10468. if (pred_cmd)
  10469. nested_vmx_disable_intercept_for_msr(
  10470. msr_bitmap_l1, msr_bitmap_l0,
  10471. MSR_IA32_PRED_CMD,
  10472. MSR_TYPE_W);
  10473. kunmap(page);
  10474. kvm_release_page_clean(page);
  10475. return true;
  10476. }
  10477. static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
  10478. struct vmcs12 *vmcs12)
  10479. {
  10480. struct vmcs12 *shadow;
  10481. struct page *page;
  10482. if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
  10483. vmcs12->vmcs_link_pointer == -1ull)
  10484. return;
  10485. shadow = get_shadow_vmcs12(vcpu);
  10486. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
  10487. memcpy(shadow, kmap(page), VMCS12_SIZE);
  10488. kunmap(page);
  10489. kvm_release_page_clean(page);
  10490. }
  10491. static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
  10492. struct vmcs12 *vmcs12)
  10493. {
  10494. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10495. if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
  10496. vmcs12->vmcs_link_pointer == -1ull)
  10497. return;
  10498. kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
  10499. get_shadow_vmcs12(vcpu), VMCS12_SIZE);
  10500. }
  10501. static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
  10502. struct vmcs12 *vmcs12)
  10503. {
  10504. if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
  10505. !page_address_valid(vcpu, vmcs12->apic_access_addr))
  10506. return -EINVAL;
  10507. else
  10508. return 0;
  10509. }
  10510. static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
  10511. struct vmcs12 *vmcs12)
  10512. {
  10513. if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
  10514. !nested_cpu_has_apic_reg_virt(vmcs12) &&
  10515. !nested_cpu_has_vid(vmcs12) &&
  10516. !nested_cpu_has_posted_intr(vmcs12))
  10517. return 0;
  10518. /*
  10519. * If virtualize x2apic mode is enabled,
  10520. * virtualize apic access must be disabled.
  10521. */
  10522. if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
  10523. nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
  10524. return -EINVAL;
  10525. /*
  10526. * If virtual interrupt delivery is enabled,
  10527. * we must exit on external interrupts.
  10528. */
  10529. if (nested_cpu_has_vid(vmcs12) &&
  10530. !nested_exit_on_intr(vcpu))
  10531. return -EINVAL;
  10532. /*
  10533. * bits 15:8 should be zero in posted_intr_nv,
  10534. * the descriptor address has been already checked
  10535. * in nested_get_vmcs12_pages.
  10536. *
  10537. * bits 5:0 of posted_intr_desc_addr should be zero.
  10538. */
  10539. if (nested_cpu_has_posted_intr(vmcs12) &&
  10540. (!nested_cpu_has_vid(vmcs12) ||
  10541. !nested_exit_intr_ack_set(vcpu) ||
  10542. (vmcs12->posted_intr_nv & 0xff00) ||
  10543. (vmcs12->posted_intr_desc_addr & 0x3f) ||
  10544. (vmcs12->posted_intr_desc_addr >> cpuid_maxphyaddr(vcpu))))
  10545. return -EINVAL;
  10546. /* tpr shadow is needed by all apicv features. */
  10547. if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
  10548. return -EINVAL;
  10549. return 0;
  10550. }
  10551. static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
  10552. unsigned long count_field,
  10553. unsigned long addr_field)
  10554. {
  10555. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  10556. int maxphyaddr;
  10557. u64 count, addr;
  10558. if (vmcs12_read_any(vmcs12, count_field, &count) ||
  10559. vmcs12_read_any(vmcs12, addr_field, &addr)) {
  10560. WARN_ON(1);
  10561. return -EINVAL;
  10562. }
  10563. if (count == 0)
  10564. return 0;
  10565. maxphyaddr = cpuid_maxphyaddr(vcpu);
  10566. if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
  10567. (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
  10568. pr_debug_ratelimited(
  10569. "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
  10570. addr_field, maxphyaddr, count, addr);
  10571. return -EINVAL;
  10572. }
  10573. return 0;
  10574. }
  10575. static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
  10576. struct vmcs12 *vmcs12)
  10577. {
  10578. if (vmcs12->vm_exit_msr_load_count == 0 &&
  10579. vmcs12->vm_exit_msr_store_count == 0 &&
  10580. vmcs12->vm_entry_msr_load_count == 0)
  10581. return 0; /* Fast path */
  10582. if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
  10583. VM_EXIT_MSR_LOAD_ADDR) ||
  10584. nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
  10585. VM_EXIT_MSR_STORE_ADDR) ||
  10586. nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
  10587. VM_ENTRY_MSR_LOAD_ADDR))
  10588. return -EINVAL;
  10589. return 0;
  10590. }
  10591. static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
  10592. struct vmcs12 *vmcs12)
  10593. {
  10594. if (!nested_cpu_has_pml(vmcs12))
  10595. return 0;
  10596. if (!nested_cpu_has_ept(vmcs12) ||
  10597. !page_address_valid(vcpu, vmcs12->pml_address))
  10598. return -EINVAL;
  10599. return 0;
  10600. }
  10601. static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
  10602. struct vmcs12 *vmcs12)
  10603. {
  10604. if (!nested_cpu_has_shadow_vmcs(vmcs12))
  10605. return 0;
  10606. if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
  10607. !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
  10608. return -EINVAL;
  10609. return 0;
  10610. }
  10611. static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
  10612. struct vmx_msr_entry *e)
  10613. {
  10614. /* x2APIC MSR accesses are not allowed */
  10615. if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
  10616. return -EINVAL;
  10617. if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
  10618. e->index == MSR_IA32_UCODE_REV)
  10619. return -EINVAL;
  10620. if (e->reserved != 0)
  10621. return -EINVAL;
  10622. return 0;
  10623. }
  10624. static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
  10625. struct vmx_msr_entry *e)
  10626. {
  10627. if (e->index == MSR_FS_BASE ||
  10628. e->index == MSR_GS_BASE ||
  10629. e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
  10630. nested_vmx_msr_check_common(vcpu, e))
  10631. return -EINVAL;
  10632. return 0;
  10633. }
  10634. static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
  10635. struct vmx_msr_entry *e)
  10636. {
  10637. if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
  10638. nested_vmx_msr_check_common(vcpu, e))
  10639. return -EINVAL;
  10640. return 0;
  10641. }
  10642. /*
  10643. * Load guest's/host's msr at nested entry/exit.
  10644. * return 0 for success, entry index for failure.
  10645. */
  10646. static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
  10647. {
  10648. u32 i;
  10649. struct vmx_msr_entry e;
  10650. struct msr_data msr;
  10651. msr.host_initiated = false;
  10652. for (i = 0; i < count; i++) {
  10653. if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
  10654. &e, sizeof(e))) {
  10655. pr_debug_ratelimited(
  10656. "%s cannot read MSR entry (%u, 0x%08llx)\n",
  10657. __func__, i, gpa + i * sizeof(e));
  10658. goto fail;
  10659. }
  10660. if (nested_vmx_load_msr_check(vcpu, &e)) {
  10661. pr_debug_ratelimited(
  10662. "%s check failed (%u, 0x%x, 0x%x)\n",
  10663. __func__, i, e.index, e.reserved);
  10664. goto fail;
  10665. }
  10666. msr.index = e.index;
  10667. msr.data = e.value;
  10668. if (kvm_set_msr(vcpu, &msr)) {
  10669. pr_debug_ratelimited(
  10670. "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
  10671. __func__, i, e.index, e.value);
  10672. goto fail;
  10673. }
  10674. }
  10675. return 0;
  10676. fail:
  10677. return i + 1;
  10678. }
  10679. static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
  10680. {
  10681. u32 i;
  10682. struct vmx_msr_entry e;
  10683. for (i = 0; i < count; i++) {
  10684. struct msr_data msr_info;
  10685. if (kvm_vcpu_read_guest(vcpu,
  10686. gpa + i * sizeof(e),
  10687. &e, 2 * sizeof(u32))) {
  10688. pr_debug_ratelimited(
  10689. "%s cannot read MSR entry (%u, 0x%08llx)\n",
  10690. __func__, i, gpa + i * sizeof(e));
  10691. return -EINVAL;
  10692. }
  10693. if (nested_vmx_store_msr_check(vcpu, &e)) {
  10694. pr_debug_ratelimited(
  10695. "%s check failed (%u, 0x%x, 0x%x)\n",
  10696. __func__, i, e.index, e.reserved);
  10697. return -EINVAL;
  10698. }
  10699. msr_info.host_initiated = false;
  10700. msr_info.index = e.index;
  10701. if (kvm_get_msr(vcpu, &msr_info)) {
  10702. pr_debug_ratelimited(
  10703. "%s cannot read MSR (%u, 0x%x)\n",
  10704. __func__, i, e.index);
  10705. return -EINVAL;
  10706. }
  10707. if (kvm_vcpu_write_guest(vcpu,
  10708. gpa + i * sizeof(e) +
  10709. offsetof(struct vmx_msr_entry, value),
  10710. &msr_info.data, sizeof(msr_info.data))) {
  10711. pr_debug_ratelimited(
  10712. "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
  10713. __func__, i, e.index, msr_info.data);
  10714. return -EINVAL;
  10715. }
  10716. }
  10717. return 0;
  10718. }
  10719. static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
  10720. {
  10721. unsigned long invalid_mask;
  10722. invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
  10723. return (val & invalid_mask) == 0;
  10724. }
  10725. /*
  10726. * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
  10727. * emulating VM entry into a guest with EPT enabled.
  10728. * Returns 0 on success, 1 on failure. Invalid state exit qualification code
  10729. * is assigned to entry_failure_code on failure.
  10730. */
  10731. static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
  10732. u32 *entry_failure_code)
  10733. {
  10734. if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
  10735. if (!nested_cr3_valid(vcpu, cr3)) {
  10736. *entry_failure_code = ENTRY_FAIL_DEFAULT;
  10737. return 1;
  10738. }
  10739. /*
  10740. * If PAE paging and EPT are both on, CR3 is not used by the CPU and
  10741. * must not be dereferenced.
  10742. */
  10743. if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
  10744. !nested_ept) {
  10745. if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
  10746. *entry_failure_code = ENTRY_FAIL_PDPTE;
  10747. return 1;
  10748. }
  10749. }
  10750. }
  10751. if (!nested_ept)
  10752. kvm_mmu_new_cr3(vcpu, cr3, false);
  10753. vcpu->arch.cr3 = cr3;
  10754. __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
  10755. kvm_init_mmu(vcpu, false);
  10756. return 0;
  10757. }
  10758. /*
  10759. * Returns if KVM is able to config CPU to tag TLB entries
  10760. * populated by L2 differently than TLB entries populated
  10761. * by L1.
  10762. *
  10763. * If L1 uses EPT, then TLB entries are tagged with different EPTP.
  10764. *
  10765. * If L1 uses VPID and we allocated a vpid02, TLB entries are tagged
  10766. * with different VPID (L1 entries are tagged with vmx->vpid
  10767. * while L2 entries are tagged with vmx->nested.vpid02).
  10768. */
  10769. static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu)
  10770. {
  10771. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  10772. return nested_cpu_has_ept(vmcs12) ||
  10773. (nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02);
  10774. }
  10775. static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
  10776. {
  10777. if (vmx->nested.nested_run_pending &&
  10778. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
  10779. return vmcs12->guest_ia32_efer;
  10780. else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
  10781. return vmx->vcpu.arch.efer | (EFER_LMA | EFER_LME);
  10782. else
  10783. return vmx->vcpu.arch.efer & ~(EFER_LMA | EFER_LME);
  10784. }
  10785. static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx)
  10786. {
  10787. /*
  10788. * If vmcs02 hasn't been initialized, set the constant vmcs02 state
  10789. * according to L0's settings (vmcs12 is irrelevant here). Host
  10790. * fields that come from L0 and are not constant, e.g. HOST_CR3,
  10791. * will be set as needed prior to VMLAUNCH/VMRESUME.
  10792. */
  10793. if (vmx->nested.vmcs02_initialized)
  10794. return;
  10795. vmx->nested.vmcs02_initialized = true;
  10796. /*
  10797. * We don't care what the EPTP value is we just need to guarantee
  10798. * it's valid so we don't get a false positive when doing early
  10799. * consistency checks.
  10800. */
  10801. if (enable_ept && nested_early_check)
  10802. vmcs_write64(EPT_POINTER, construct_eptp(&vmx->vcpu, 0));
  10803. /* All VMFUNCs are currently emulated through L0 vmexits. */
  10804. if (cpu_has_vmx_vmfunc())
  10805. vmcs_write64(VM_FUNCTION_CONTROL, 0);
  10806. if (cpu_has_vmx_posted_intr())
  10807. vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
  10808. if (cpu_has_vmx_msr_bitmap())
  10809. vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
  10810. if (enable_pml)
  10811. vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
  10812. /*
  10813. * Set the MSR load/store lists to match L0's settings. Only the
  10814. * addresses are constant (for vmcs02), the counts can change based
  10815. * on L2's behavior, e.g. switching to/from long mode.
  10816. */
  10817. vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
  10818. vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
  10819. vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
  10820. vmx_set_constant_host_state(vmx);
  10821. }
  10822. static void prepare_vmcs02_early_full(struct vcpu_vmx *vmx,
  10823. struct vmcs12 *vmcs12)
  10824. {
  10825. prepare_vmcs02_constant_state(vmx);
  10826. vmcs_write64(VMCS_LINK_POINTER, -1ull);
  10827. if (enable_vpid) {
  10828. if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
  10829. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
  10830. else
  10831. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
  10832. }
  10833. }
  10834. static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
  10835. {
  10836. u32 exec_control, vmcs12_exec_ctrl;
  10837. u64 guest_efer = nested_vmx_calc_efer(vmx, vmcs12);
  10838. if (vmx->nested.dirty_vmcs12 || vmx->nested.hv_evmcs)
  10839. prepare_vmcs02_early_full(vmx, vmcs12);
  10840. /*
  10841. * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
  10842. * entry, but only if the current (host) sp changed from the value
  10843. * we wrote last (vmx->host_rsp). This cache is no longer relevant
  10844. * if we switch vmcs, and rather than hold a separate cache per vmcs,
  10845. * here we just force the write to happen on entry. host_rsp will
  10846. * also be written unconditionally by nested_vmx_check_vmentry_hw()
  10847. * if we are doing early consistency checks via hardware.
  10848. */
  10849. vmx->host_rsp = 0;
  10850. /*
  10851. * PIN CONTROLS
  10852. */
  10853. exec_control = vmcs12->pin_based_vm_exec_control;
  10854. /* Preemption timer setting is computed directly in vmx_vcpu_run. */
  10855. exec_control |= vmcs_config.pin_based_exec_ctrl;
  10856. exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  10857. vmx->loaded_vmcs->hv_timer_armed = false;
  10858. /* Posted interrupts setting is only taken from vmcs12. */
  10859. if (nested_cpu_has_posted_intr(vmcs12)) {
  10860. vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
  10861. vmx->nested.pi_pending = false;
  10862. } else {
  10863. exec_control &= ~PIN_BASED_POSTED_INTR;
  10864. }
  10865. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
  10866. /*
  10867. * EXEC CONTROLS
  10868. */
  10869. exec_control = vmx_exec_control(vmx); /* L0's desires */
  10870. exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
  10871. exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
  10872. exec_control &= ~CPU_BASED_TPR_SHADOW;
  10873. exec_control |= vmcs12->cpu_based_vm_exec_control;
  10874. /*
  10875. * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
  10876. * nested_get_vmcs12_pages can't fix it up, the illegal value
  10877. * will result in a VM entry failure.
  10878. */
  10879. if (exec_control & CPU_BASED_TPR_SHADOW) {
  10880. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
  10881. vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
  10882. } else {
  10883. #ifdef CONFIG_X86_64
  10884. exec_control |= CPU_BASED_CR8_LOAD_EXITING |
  10885. CPU_BASED_CR8_STORE_EXITING;
  10886. #endif
  10887. }
  10888. /*
  10889. * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
  10890. * for I/O port accesses.
  10891. */
  10892. exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
  10893. exec_control |= CPU_BASED_UNCOND_IO_EXITING;
  10894. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
  10895. /*
  10896. * SECONDARY EXEC CONTROLS
  10897. */
  10898. if (cpu_has_secondary_exec_ctrls()) {
  10899. exec_control = vmx->secondary_exec_control;
  10900. /* Take the following fields only from vmcs12 */
  10901. exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  10902. SECONDARY_EXEC_ENABLE_INVPCID |
  10903. SECONDARY_EXEC_RDTSCP |
  10904. SECONDARY_EXEC_XSAVES |
  10905. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
  10906. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  10907. SECONDARY_EXEC_ENABLE_VMFUNC);
  10908. if (nested_cpu_has(vmcs12,
  10909. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
  10910. vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
  10911. ~SECONDARY_EXEC_ENABLE_PML;
  10912. exec_control |= vmcs12_exec_ctrl;
  10913. }
  10914. /* VMCS shadowing for L2 is emulated for now */
  10915. exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
  10916. if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
  10917. vmcs_write16(GUEST_INTR_STATUS,
  10918. vmcs12->guest_intr_status);
  10919. /*
  10920. * Write an illegal value to APIC_ACCESS_ADDR. Later,
  10921. * nested_get_vmcs12_pages will either fix it up or
  10922. * remove the VM execution control.
  10923. */
  10924. if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
  10925. vmcs_write64(APIC_ACCESS_ADDR, -1ull);
  10926. if (exec_control & SECONDARY_EXEC_ENCLS_EXITING)
  10927. vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
  10928. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
  10929. }
  10930. /*
  10931. * ENTRY CONTROLS
  10932. *
  10933. * vmcs12's VM_{ENTRY,EXIT}_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE
  10934. * are emulated by vmx_set_efer() in prepare_vmcs02(), but speculate
  10935. * on the related bits (if supported by the CPU) in the hope that
  10936. * we can avoid VMWrites during vmx_set_efer().
  10937. */
  10938. exec_control = (vmcs12->vm_entry_controls | vmcs_config.vmentry_ctrl) &
  10939. ~VM_ENTRY_IA32E_MODE & ~VM_ENTRY_LOAD_IA32_EFER;
  10940. if (cpu_has_load_ia32_efer) {
  10941. if (guest_efer & EFER_LMA)
  10942. exec_control |= VM_ENTRY_IA32E_MODE;
  10943. if (guest_efer != host_efer)
  10944. exec_control |= VM_ENTRY_LOAD_IA32_EFER;
  10945. }
  10946. vm_entry_controls_init(vmx, exec_control);
  10947. /*
  10948. * EXIT CONTROLS
  10949. *
  10950. * L2->L1 exit controls are emulated - the hardware exit is to L0 so
  10951. * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
  10952. * bits may be modified by vmx_set_efer() in prepare_vmcs02().
  10953. */
  10954. exec_control = vmcs_config.vmexit_ctrl;
  10955. if (cpu_has_load_ia32_efer && guest_efer != host_efer)
  10956. exec_control |= VM_EXIT_LOAD_IA32_EFER;
  10957. vm_exit_controls_init(vmx, exec_control);
  10958. /*
  10959. * Conceptually we want to copy the PML address and index from
  10960. * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
  10961. * since we always flush the log on each vmexit and never change
  10962. * the PML address (once set), this happens to be equivalent to
  10963. * simply resetting the index in vmcs02.
  10964. */
  10965. if (enable_pml)
  10966. vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
  10967. /*
  10968. * Interrupt/Exception Fields
  10969. */
  10970. if (vmx->nested.nested_run_pending) {
  10971. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  10972. vmcs12->vm_entry_intr_info_field);
  10973. vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
  10974. vmcs12->vm_entry_exception_error_code);
  10975. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  10976. vmcs12->vm_entry_instruction_len);
  10977. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
  10978. vmcs12->guest_interruptibility_info);
  10979. vmx->loaded_vmcs->nmi_known_unmasked =
  10980. !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
  10981. } else {
  10982. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
  10983. }
  10984. }
  10985. static void prepare_vmcs02_full(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
  10986. {
  10987. struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs;
  10988. if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
  10989. HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) {
  10990. vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
  10991. vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
  10992. vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
  10993. vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
  10994. vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
  10995. vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
  10996. vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
  10997. vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
  10998. vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
  10999. vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
  11000. vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
  11001. vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
  11002. vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
  11003. vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
  11004. vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
  11005. vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
  11006. vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
  11007. vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
  11008. vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
  11009. vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
  11010. vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
  11011. vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
  11012. vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
  11013. vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
  11014. vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
  11015. vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
  11016. vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
  11017. vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
  11018. vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
  11019. vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
  11020. vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
  11021. vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
  11022. vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
  11023. vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
  11024. }
  11025. if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
  11026. HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1)) {
  11027. vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
  11028. vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
  11029. vmcs12->guest_pending_dbg_exceptions);
  11030. vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
  11031. vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
  11032. /*
  11033. * L1 may access the L2's PDPTR, so save them to construct
  11034. * vmcs12
  11035. */
  11036. if (enable_ept) {
  11037. vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
  11038. vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
  11039. vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
  11040. vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
  11041. }
  11042. }
  11043. if (nested_cpu_has_xsaves(vmcs12))
  11044. vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
  11045. /*
  11046. * Whether page-faults are trapped is determined by a combination of
  11047. * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
  11048. * If enable_ept, L0 doesn't care about page faults and we should
  11049. * set all of these to L1's desires. However, if !enable_ept, L0 does
  11050. * care about (at least some) page faults, and because it is not easy
  11051. * (if at all possible?) to merge L0 and L1's desires, we simply ask
  11052. * to exit on each and every L2 page fault. This is done by setting
  11053. * MASK=MATCH=0 and (see below) EB.PF=1.
  11054. * Note that below we don't need special code to set EB.PF beyond the
  11055. * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
  11056. * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
  11057. * !enable_ept, EB.PF is 1, so the "or" will always be 1.
  11058. */
  11059. vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
  11060. enable_ept ? vmcs12->page_fault_error_code_mask : 0);
  11061. vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
  11062. enable_ept ? vmcs12->page_fault_error_code_match : 0);
  11063. if (cpu_has_vmx_apicv()) {
  11064. vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
  11065. vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
  11066. vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
  11067. vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
  11068. }
  11069. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
  11070. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
  11071. set_cr4_guest_host_mask(vmx);
  11072. if (kvm_mpx_supported()) {
  11073. if (vmx->nested.nested_run_pending &&
  11074. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
  11075. vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
  11076. else
  11077. vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs);
  11078. }
  11079. }
  11080. /*
  11081. * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
  11082. * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
  11083. * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
  11084. * guest in a way that will both be appropriate to L1's requests, and our
  11085. * needs. In addition to modifying the active vmcs (which is vmcs02), this
  11086. * function also has additional necessary side-effects, like setting various
  11087. * vcpu->arch fields.
  11088. * Returns 0 on success, 1 on failure. Invalid state exit qualification code
  11089. * is assigned to entry_failure_code on failure.
  11090. */
  11091. static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
  11092. u32 *entry_failure_code)
  11093. {
  11094. struct vcpu_vmx *vmx = to_vmx(vcpu);
  11095. struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs;
  11096. if (vmx->nested.dirty_vmcs12 || vmx->nested.hv_evmcs) {
  11097. prepare_vmcs02_full(vmx, vmcs12);
  11098. vmx->nested.dirty_vmcs12 = false;
  11099. }
  11100. /*
  11101. * First, the fields that are shadowed. This must be kept in sync
  11102. * with vmx_shadow_fields.h.
  11103. */
  11104. if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
  11105. HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) {
  11106. vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
  11107. vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
  11108. }
  11109. if (vmx->nested.nested_run_pending &&
  11110. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
  11111. kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
  11112. vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
  11113. } else {
  11114. kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
  11115. vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
  11116. }
  11117. vmx_set_rflags(vcpu, vmcs12->guest_rflags);
  11118. vmx->nested.preemption_timer_expired = false;
  11119. if (nested_cpu_has_preemption_timer(vmcs12))
  11120. vmx_start_preemption_timer(vcpu);
  11121. /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
  11122. * bitwise-or of what L1 wants to trap for L2, and what we want to
  11123. * trap. Note that CR0.TS also needs updating - we do this later.
  11124. */
  11125. update_exception_bitmap(vcpu);
  11126. vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
  11127. vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
  11128. if (vmx->nested.nested_run_pending &&
  11129. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
  11130. vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
  11131. vcpu->arch.pat = vmcs12->guest_ia32_pat;
  11132. } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
  11133. vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
  11134. }
  11135. vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
  11136. if (kvm_has_tsc_control)
  11137. decache_tsc_multiplier(vmx);
  11138. if (enable_vpid) {
  11139. /*
  11140. * There is no direct mapping between vpid02 and vpid12, the
  11141. * vpid02 is per-vCPU for L0 and reused while the value of
  11142. * vpid12 is changed w/ one invvpid during nested vmentry.
  11143. * The vpid12 is allocated by L1 for L2, so it will not
  11144. * influence global bitmap(for vpid01 and vpid02 allocation)
  11145. * even if spawn a lot of nested vCPUs.
  11146. */
  11147. if (nested_cpu_has_vpid(vmcs12) && nested_has_guest_tlb_tag(vcpu)) {
  11148. if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
  11149. vmx->nested.last_vpid = vmcs12->virtual_processor_id;
  11150. __vmx_flush_tlb(vcpu, nested_get_vpid02(vcpu), false);
  11151. }
  11152. } else {
  11153. /*
  11154. * If L1 use EPT, then L0 needs to execute INVEPT on
  11155. * EPTP02 instead of EPTP01. Therefore, delay TLB
  11156. * flush until vmcs02->eptp is fully updated by
  11157. * KVM_REQ_LOAD_CR3. Note that this assumes
  11158. * KVM_REQ_TLB_FLUSH is evaluated after
  11159. * KVM_REQ_LOAD_CR3 in vcpu_enter_guest().
  11160. */
  11161. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  11162. }
  11163. }
  11164. if (nested_cpu_has_ept(vmcs12))
  11165. nested_ept_init_mmu_context(vcpu);
  11166. else if (nested_cpu_has2(vmcs12,
  11167. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
  11168. vmx_flush_tlb(vcpu, true);
  11169. /*
  11170. * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
  11171. * bits which we consider mandatory enabled.
  11172. * The CR0_READ_SHADOW is what L2 should have expected to read given
  11173. * the specifications by L1; It's not enough to take
  11174. * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
  11175. * have more bits than L1 expected.
  11176. */
  11177. vmx_set_cr0(vcpu, vmcs12->guest_cr0);
  11178. vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
  11179. vmx_set_cr4(vcpu, vmcs12->guest_cr4);
  11180. vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
  11181. vcpu->arch.efer = nested_vmx_calc_efer(vmx, vmcs12);
  11182. /* Note: may modify VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
  11183. vmx_set_efer(vcpu, vcpu->arch.efer);
  11184. /*
  11185. * Guest state is invalid and unrestricted guest is disabled,
  11186. * which means L1 attempted VMEntry to L2 with invalid state.
  11187. * Fail the VMEntry.
  11188. */
  11189. if (vmx->emulation_required) {
  11190. *entry_failure_code = ENTRY_FAIL_DEFAULT;
  11191. return 1;
  11192. }
  11193. /* Shadow page tables on either EPT or shadow page tables. */
  11194. if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
  11195. entry_failure_code))
  11196. return 1;
  11197. if (!enable_ept)
  11198. vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
  11199. kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
  11200. kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
  11201. return 0;
  11202. }
  11203. static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
  11204. {
  11205. if (!nested_cpu_has_nmi_exiting(vmcs12) &&
  11206. nested_cpu_has_virtual_nmis(vmcs12))
  11207. return -EINVAL;
  11208. if (!nested_cpu_has_virtual_nmis(vmcs12) &&
  11209. nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
  11210. return -EINVAL;
  11211. return 0;
  11212. }
  11213. static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  11214. {
  11215. struct vcpu_vmx *vmx = to_vmx(vcpu);
  11216. bool ia32e;
  11217. if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
  11218. vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
  11219. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11220. if (nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id)
  11221. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11222. if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
  11223. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11224. if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
  11225. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11226. if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
  11227. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11228. if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
  11229. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11230. if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
  11231. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11232. if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
  11233. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11234. if (nested_vmx_check_pml_controls(vcpu, vmcs12))
  11235. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11236. if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12))
  11237. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11238. if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
  11239. vmx->nested.msrs.procbased_ctls_low,
  11240. vmx->nested.msrs.procbased_ctls_high) ||
  11241. (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
  11242. !vmx_control_verify(vmcs12->secondary_vm_exec_control,
  11243. vmx->nested.msrs.secondary_ctls_low,
  11244. vmx->nested.msrs.secondary_ctls_high)) ||
  11245. !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
  11246. vmx->nested.msrs.pinbased_ctls_low,
  11247. vmx->nested.msrs.pinbased_ctls_high) ||
  11248. !vmx_control_verify(vmcs12->vm_exit_controls,
  11249. vmx->nested.msrs.exit_ctls_low,
  11250. vmx->nested.msrs.exit_ctls_high) ||
  11251. !vmx_control_verify(vmcs12->vm_entry_controls,
  11252. vmx->nested.msrs.entry_ctls_low,
  11253. vmx->nested.msrs.entry_ctls_high))
  11254. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11255. if (nested_vmx_check_nmi_controls(vmcs12))
  11256. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11257. if (nested_cpu_has_vmfunc(vmcs12)) {
  11258. if (vmcs12->vm_function_control &
  11259. ~vmx->nested.msrs.vmfunc_controls)
  11260. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11261. if (nested_cpu_has_eptp_switching(vmcs12)) {
  11262. if (!nested_cpu_has_ept(vmcs12) ||
  11263. !page_address_valid(vcpu, vmcs12->eptp_list_address))
  11264. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11265. }
  11266. }
  11267. if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
  11268. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11269. if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
  11270. !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
  11271. !nested_cr3_valid(vcpu, vmcs12->host_cr3))
  11272. return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
  11273. /*
  11274. * If the load IA32_EFER VM-exit control is 1, bits reserved in the
  11275. * IA32_EFER MSR must be 0 in the field for that register. In addition,
  11276. * the values of the LMA and LME bits in the field must each be that of
  11277. * the host address-space size VM-exit control.
  11278. */
  11279. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
  11280. ia32e = (vmcs12->vm_exit_controls &
  11281. VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
  11282. if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
  11283. ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
  11284. ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
  11285. return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
  11286. }
  11287. /*
  11288. * From the Intel SDM, volume 3:
  11289. * Fields relevant to VM-entry event injection must be set properly.
  11290. * These fields are the VM-entry interruption-information field, the
  11291. * VM-entry exception error code, and the VM-entry instruction length.
  11292. */
  11293. if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
  11294. u32 intr_info = vmcs12->vm_entry_intr_info_field;
  11295. u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
  11296. u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
  11297. bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
  11298. bool should_have_error_code;
  11299. bool urg = nested_cpu_has2(vmcs12,
  11300. SECONDARY_EXEC_UNRESTRICTED_GUEST);
  11301. bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
  11302. /* VM-entry interruption-info field: interruption type */
  11303. if (intr_type == INTR_TYPE_RESERVED ||
  11304. (intr_type == INTR_TYPE_OTHER_EVENT &&
  11305. !nested_cpu_supports_monitor_trap_flag(vcpu)))
  11306. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11307. /* VM-entry interruption-info field: vector */
  11308. if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
  11309. (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
  11310. (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
  11311. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11312. /* VM-entry interruption-info field: deliver error code */
  11313. should_have_error_code =
  11314. intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
  11315. x86_exception_has_error_code(vector);
  11316. if (has_error_code != should_have_error_code)
  11317. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11318. /* VM-entry exception error code */
  11319. if (has_error_code &&
  11320. vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
  11321. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11322. /* VM-entry interruption-info field: reserved bits */
  11323. if (intr_info & INTR_INFO_RESVD_BITS_MASK)
  11324. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11325. /* VM-entry instruction length */
  11326. switch (intr_type) {
  11327. case INTR_TYPE_SOFT_EXCEPTION:
  11328. case INTR_TYPE_SOFT_INTR:
  11329. case INTR_TYPE_PRIV_SW_EXCEPTION:
  11330. if ((vmcs12->vm_entry_instruction_len > 15) ||
  11331. (vmcs12->vm_entry_instruction_len == 0 &&
  11332. !nested_cpu_has_zero_length_injection(vcpu)))
  11333. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11334. }
  11335. }
  11336. if (nested_cpu_has_ept(vmcs12) &&
  11337. !valid_ept_address(vcpu, vmcs12->ept_pointer))
  11338. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11339. return 0;
  11340. }
  11341. static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
  11342. struct vmcs12 *vmcs12)
  11343. {
  11344. int r;
  11345. struct page *page;
  11346. struct vmcs12 *shadow;
  11347. if (vmcs12->vmcs_link_pointer == -1ull)
  11348. return 0;
  11349. if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
  11350. return -EINVAL;
  11351. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
  11352. if (is_error_page(page))
  11353. return -EINVAL;
  11354. r = 0;
  11355. shadow = kmap(page);
  11356. if (shadow->hdr.revision_id != VMCS12_REVISION ||
  11357. shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
  11358. r = -EINVAL;
  11359. kunmap(page);
  11360. kvm_release_page_clean(page);
  11361. return r;
  11362. }
  11363. static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
  11364. u32 *exit_qual)
  11365. {
  11366. bool ia32e;
  11367. *exit_qual = ENTRY_FAIL_DEFAULT;
  11368. if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
  11369. !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
  11370. return 1;
  11371. if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
  11372. *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
  11373. return 1;
  11374. }
  11375. /*
  11376. * If the load IA32_EFER VM-entry control is 1, the following checks
  11377. * are performed on the field for the IA32_EFER MSR:
  11378. * - Bits reserved in the IA32_EFER MSR must be 0.
  11379. * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
  11380. * the IA-32e mode guest VM-exit control. It must also be identical
  11381. * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
  11382. * CR0.PG) is 1.
  11383. */
  11384. if (to_vmx(vcpu)->nested.nested_run_pending &&
  11385. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
  11386. ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
  11387. if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
  11388. ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
  11389. ((vmcs12->guest_cr0 & X86_CR0_PG) &&
  11390. ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
  11391. return 1;
  11392. }
  11393. if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
  11394. (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
  11395. (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
  11396. return 1;
  11397. return 0;
  11398. }
  11399. static int __noclone nested_vmx_check_vmentry_hw(struct kvm_vcpu *vcpu)
  11400. {
  11401. struct vcpu_vmx *vmx = to_vmx(vcpu);
  11402. unsigned long cr3, cr4;
  11403. if (!nested_early_check)
  11404. return 0;
  11405. if (vmx->msr_autoload.host.nr)
  11406. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
  11407. if (vmx->msr_autoload.guest.nr)
  11408. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
  11409. preempt_disable();
  11410. vmx_prepare_switch_to_guest(vcpu);
  11411. /*
  11412. * Induce a consistency check VMExit by clearing bit 1 in GUEST_RFLAGS,
  11413. * which is reserved to '1' by hardware. GUEST_RFLAGS is guaranteed to
  11414. * be written (by preparve_vmcs02()) before the "real" VMEnter, i.e.
  11415. * there is no need to preserve other bits or save/restore the field.
  11416. */
  11417. vmcs_writel(GUEST_RFLAGS, 0);
  11418. vmcs_writel(HOST_RIP, vmx_early_consistency_check_return);
  11419. cr3 = __get_current_cr3_fast();
  11420. if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
  11421. vmcs_writel(HOST_CR3, cr3);
  11422. vmx->loaded_vmcs->host_state.cr3 = cr3;
  11423. }
  11424. cr4 = cr4_read_shadow();
  11425. if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
  11426. vmcs_writel(HOST_CR4, cr4);
  11427. vmx->loaded_vmcs->host_state.cr4 = cr4;
  11428. }
  11429. vmx->__launched = vmx->loaded_vmcs->launched;
  11430. asm(
  11431. /* Set HOST_RSP */
  11432. __ex("vmwrite %%" _ASM_SP ", %%" _ASM_DX) "\n\t"
  11433. "mov %%" _ASM_SP ", %c[host_rsp](%0)\n\t"
  11434. /* Check if vmlaunch of vmresume is needed */
  11435. "cmpl $0, %c[launched](%0)\n\t"
  11436. "je 1f\n\t"
  11437. __ex("vmresume") "\n\t"
  11438. "jmp 2f\n\t"
  11439. "1: " __ex("vmlaunch") "\n\t"
  11440. "jmp 2f\n\t"
  11441. "2: "
  11442. /* Set vmx->fail accordingly */
  11443. "setbe %c[fail](%0)\n\t"
  11444. ".pushsection .rodata\n\t"
  11445. ".global vmx_early_consistency_check_return\n\t"
  11446. "vmx_early_consistency_check_return: " _ASM_PTR " 2b\n\t"
  11447. ".popsection"
  11448. :
  11449. : "c"(vmx), "d"((unsigned long)HOST_RSP),
  11450. [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
  11451. [fail]"i"(offsetof(struct vcpu_vmx, fail)),
  11452. [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp))
  11453. : "rax", "cc", "memory"
  11454. );
  11455. vmcs_writel(HOST_RIP, vmx_return);
  11456. preempt_enable();
  11457. if (vmx->msr_autoload.host.nr)
  11458. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
  11459. if (vmx->msr_autoload.guest.nr)
  11460. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
  11461. if (vmx->fail) {
  11462. WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) !=
  11463. VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  11464. vmx->fail = 0;
  11465. return 1;
  11466. }
  11467. /*
  11468. * VMExit clears RFLAGS.IF and DR7, even on a consistency check.
  11469. */
  11470. local_irq_enable();
  11471. if (hw_breakpoint_active())
  11472. set_debugreg(__this_cpu_read(cpu_dr7), 7);
  11473. /*
  11474. * A non-failing VMEntry means we somehow entered guest mode with
  11475. * an illegal RIP, and that's just the tip of the iceberg. There
  11476. * is no telling what memory has been modified or what state has
  11477. * been exposed to unknown code. Hitting this all but guarantees
  11478. * a (very critical) hardware issue.
  11479. */
  11480. WARN_ON(!(vmcs_read32(VM_EXIT_REASON) &
  11481. VMX_EXIT_REASONS_FAILED_VMENTRY));
  11482. return 0;
  11483. }
  11484. STACK_FRAME_NON_STANDARD(nested_vmx_check_vmentry_hw);
  11485. static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
  11486. struct vmcs12 *vmcs12);
  11487. /*
  11488. * If from_vmentry is false, this is being called from state restore (either RSM
  11489. * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
  11490. + *
  11491. + * Returns:
  11492. + * 0 - success, i.e. proceed with actual VMEnter
  11493. + * 1 - consistency check VMExit
  11494. + * -1 - consistency check VMFail
  11495. */
  11496. static int nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu,
  11497. bool from_vmentry)
  11498. {
  11499. struct vcpu_vmx *vmx = to_vmx(vcpu);
  11500. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  11501. bool evaluate_pending_interrupts;
  11502. u32 exit_reason = EXIT_REASON_INVALID_STATE;
  11503. u32 exit_qual;
  11504. evaluate_pending_interrupts = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
  11505. (CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_VIRTUAL_NMI_PENDING);
  11506. if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu))
  11507. evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu);
  11508. if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
  11509. vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
  11510. if (kvm_mpx_supported() &&
  11511. !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
  11512. vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
  11513. vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
  11514. prepare_vmcs02_early(vmx, vmcs12);
  11515. if (from_vmentry) {
  11516. nested_get_vmcs12_pages(vcpu);
  11517. if (nested_vmx_check_vmentry_hw(vcpu)) {
  11518. vmx_switch_vmcs(vcpu, &vmx->vmcs01);
  11519. return -1;
  11520. }
  11521. if (check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
  11522. goto vmentry_fail_vmexit;
  11523. }
  11524. enter_guest_mode(vcpu);
  11525. if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
  11526. vcpu->arch.tsc_offset += vmcs12->tsc_offset;
  11527. if (prepare_vmcs02(vcpu, vmcs12, &exit_qual))
  11528. goto vmentry_fail_vmexit_guest_mode;
  11529. if (from_vmentry) {
  11530. exit_reason = EXIT_REASON_MSR_LOAD_FAIL;
  11531. exit_qual = nested_vmx_load_msr(vcpu,
  11532. vmcs12->vm_entry_msr_load_addr,
  11533. vmcs12->vm_entry_msr_load_count);
  11534. if (exit_qual)
  11535. goto vmentry_fail_vmexit_guest_mode;
  11536. } else {
  11537. /*
  11538. * The MMU is not initialized to point at the right entities yet and
  11539. * "get pages" would need to read data from the guest (i.e. we will
  11540. * need to perform gpa to hpa translation). Request a call
  11541. * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
  11542. * have already been set at vmentry time and should not be reset.
  11543. */
  11544. kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
  11545. }
  11546. /*
  11547. * If L1 had a pending IRQ/NMI until it executed
  11548. * VMLAUNCH/VMRESUME which wasn't delivered because it was
  11549. * disallowed (e.g. interrupts disabled), L0 needs to
  11550. * evaluate if this pending event should cause an exit from L2
  11551. * to L1 or delivered directly to L2 (e.g. In case L1 don't
  11552. * intercept EXTERNAL_INTERRUPT).
  11553. *
  11554. * Usually this would be handled by the processor noticing an
  11555. * IRQ/NMI window request, or checking RVI during evaluation of
  11556. * pending virtual interrupts. However, this setting was done
  11557. * on VMCS01 and now VMCS02 is active instead. Thus, we force L0
  11558. * to perform pending event evaluation by requesting a KVM_REQ_EVENT.
  11559. */
  11560. if (unlikely(evaluate_pending_interrupts))
  11561. kvm_make_request(KVM_REQ_EVENT, vcpu);
  11562. /*
  11563. * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
  11564. * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
  11565. * returned as far as L1 is concerned. It will only return (and set
  11566. * the success flag) when L2 exits (see nested_vmx_vmexit()).
  11567. */
  11568. return 0;
  11569. /*
  11570. * A failed consistency check that leads to a VMExit during L1's
  11571. * VMEnter to L2 is a variation of a normal VMexit, as explained in
  11572. * 26.7 "VM-entry failures during or after loading guest state".
  11573. */
  11574. vmentry_fail_vmexit_guest_mode:
  11575. if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
  11576. vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
  11577. leave_guest_mode(vcpu);
  11578. vmentry_fail_vmexit:
  11579. vmx_switch_vmcs(vcpu, &vmx->vmcs01);
  11580. if (!from_vmentry)
  11581. return 1;
  11582. load_vmcs12_host_state(vcpu, vmcs12);
  11583. vmcs12->vm_exit_reason = exit_reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
  11584. vmcs12->exit_qualification = exit_qual;
  11585. if (enable_shadow_vmcs || vmx->nested.hv_evmcs)
  11586. vmx->nested.need_vmcs12_sync = true;
  11587. return 1;
  11588. }
  11589. /*
  11590. * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
  11591. * for running an L2 nested guest.
  11592. */
  11593. static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
  11594. {
  11595. struct vmcs12 *vmcs12;
  11596. struct vcpu_vmx *vmx = to_vmx(vcpu);
  11597. u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
  11598. int ret;
  11599. if (!nested_vmx_check_permission(vcpu))
  11600. return 1;
  11601. if (!nested_vmx_handle_enlightened_vmptrld(vcpu, true))
  11602. return 1;
  11603. if (!vmx->nested.hv_evmcs && vmx->nested.current_vmptr == -1ull)
  11604. return nested_vmx_failInvalid(vcpu);
  11605. vmcs12 = get_vmcs12(vcpu);
  11606. /*
  11607. * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
  11608. * that there *is* a valid VMCS pointer, RFLAGS.CF is set
  11609. * rather than RFLAGS.ZF, and no error number is stored to the
  11610. * VM-instruction error field.
  11611. */
  11612. if (vmcs12->hdr.shadow_vmcs)
  11613. return nested_vmx_failInvalid(vcpu);
  11614. if (vmx->nested.hv_evmcs) {
  11615. copy_enlightened_to_vmcs12(vmx);
  11616. /* Enlightened VMCS doesn't have launch state */
  11617. vmcs12->launch_state = !launch;
  11618. } else if (enable_shadow_vmcs) {
  11619. copy_shadow_to_vmcs12(vmx);
  11620. }
  11621. /*
  11622. * The nested entry process starts with enforcing various prerequisites
  11623. * on vmcs12 as required by the Intel SDM, and act appropriately when
  11624. * they fail: As the SDM explains, some conditions should cause the
  11625. * instruction to fail, while others will cause the instruction to seem
  11626. * to succeed, but return an EXIT_REASON_INVALID_STATE.
  11627. * To speed up the normal (success) code path, we should avoid checking
  11628. * for misconfigurations which will anyway be caught by the processor
  11629. * when using the merged vmcs02.
  11630. */
  11631. if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS)
  11632. return nested_vmx_failValid(vcpu,
  11633. VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
  11634. if (vmcs12->launch_state == launch)
  11635. return nested_vmx_failValid(vcpu,
  11636. launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
  11637. : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
  11638. ret = check_vmentry_prereqs(vcpu, vmcs12);
  11639. if (ret)
  11640. return nested_vmx_failValid(vcpu, ret);
  11641. /*
  11642. * We're finally done with prerequisite checking, and can start with
  11643. * the nested entry.
  11644. */
  11645. vmx->nested.nested_run_pending = 1;
  11646. ret = nested_vmx_enter_non_root_mode(vcpu, true);
  11647. vmx->nested.nested_run_pending = !ret;
  11648. if (ret > 0)
  11649. return 1;
  11650. else if (ret)
  11651. return nested_vmx_failValid(vcpu,
  11652. VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  11653. /* Hide L1D cache contents from the nested guest. */
  11654. vmx->vcpu.arch.l1tf_flush_l1d = true;
  11655. /*
  11656. * Must happen outside of nested_vmx_enter_non_root_mode() as it will
  11657. * also be used as part of restoring nVMX state for
  11658. * snapshot restore (migration).
  11659. *
  11660. * In this flow, it is assumed that vmcs12 cache was
  11661. * trasferred as part of captured nVMX state and should
  11662. * therefore not be read from guest memory (which may not
  11663. * exist on destination host yet).
  11664. */
  11665. nested_cache_shadow_vmcs12(vcpu, vmcs12);
  11666. /*
  11667. * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
  11668. * by event injection, halt vcpu.
  11669. */
  11670. if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
  11671. !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
  11672. vmx->nested.nested_run_pending = 0;
  11673. return kvm_vcpu_halt(vcpu);
  11674. }
  11675. return 1;
  11676. }
  11677. /*
  11678. * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
  11679. * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
  11680. * This function returns the new value we should put in vmcs12.guest_cr0.
  11681. * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
  11682. * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
  11683. * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
  11684. * didn't trap the bit, because if L1 did, so would L0).
  11685. * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
  11686. * been modified by L2, and L1 knows it. So just leave the old value of
  11687. * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
  11688. * isn't relevant, because if L0 traps this bit it can set it to anything.
  11689. * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
  11690. * changed these bits, and therefore they need to be updated, but L0
  11691. * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
  11692. * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
  11693. */
  11694. static inline unsigned long
  11695. vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  11696. {
  11697. return
  11698. /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
  11699. /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
  11700. /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
  11701. vcpu->arch.cr0_guest_owned_bits));
  11702. }
  11703. static inline unsigned long
  11704. vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  11705. {
  11706. return
  11707. /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
  11708. /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
  11709. /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
  11710. vcpu->arch.cr4_guest_owned_bits));
  11711. }
  11712. static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
  11713. struct vmcs12 *vmcs12)
  11714. {
  11715. u32 idt_vectoring;
  11716. unsigned int nr;
  11717. if (vcpu->arch.exception.injected) {
  11718. nr = vcpu->arch.exception.nr;
  11719. idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
  11720. if (kvm_exception_is_soft(nr)) {
  11721. vmcs12->vm_exit_instruction_len =
  11722. vcpu->arch.event_exit_inst_len;
  11723. idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
  11724. } else
  11725. idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
  11726. if (vcpu->arch.exception.has_error_code) {
  11727. idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
  11728. vmcs12->idt_vectoring_error_code =
  11729. vcpu->arch.exception.error_code;
  11730. }
  11731. vmcs12->idt_vectoring_info_field = idt_vectoring;
  11732. } else if (vcpu->arch.nmi_injected) {
  11733. vmcs12->idt_vectoring_info_field =
  11734. INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
  11735. } else if (vcpu->arch.interrupt.injected) {
  11736. nr = vcpu->arch.interrupt.nr;
  11737. idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
  11738. if (vcpu->arch.interrupt.soft) {
  11739. idt_vectoring |= INTR_TYPE_SOFT_INTR;
  11740. vmcs12->vm_entry_instruction_len =
  11741. vcpu->arch.event_exit_inst_len;
  11742. } else
  11743. idt_vectoring |= INTR_TYPE_EXT_INTR;
  11744. vmcs12->idt_vectoring_info_field = idt_vectoring;
  11745. }
  11746. }
  11747. static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
  11748. {
  11749. struct vcpu_vmx *vmx = to_vmx(vcpu);
  11750. unsigned long exit_qual;
  11751. bool block_nested_events =
  11752. vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
  11753. if (vcpu->arch.exception.pending &&
  11754. nested_vmx_check_exception(vcpu, &exit_qual)) {
  11755. if (block_nested_events)
  11756. return -EBUSY;
  11757. nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
  11758. return 0;
  11759. }
  11760. if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
  11761. vmx->nested.preemption_timer_expired) {
  11762. if (block_nested_events)
  11763. return -EBUSY;
  11764. nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
  11765. return 0;
  11766. }
  11767. if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
  11768. if (block_nested_events)
  11769. return -EBUSY;
  11770. nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
  11771. NMI_VECTOR | INTR_TYPE_NMI_INTR |
  11772. INTR_INFO_VALID_MASK, 0);
  11773. /*
  11774. * The NMI-triggered VM exit counts as injection:
  11775. * clear this one and block further NMIs.
  11776. */
  11777. vcpu->arch.nmi_pending = 0;
  11778. vmx_set_nmi_mask(vcpu, true);
  11779. return 0;
  11780. }
  11781. if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
  11782. nested_exit_on_intr(vcpu)) {
  11783. if (block_nested_events)
  11784. return -EBUSY;
  11785. nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
  11786. return 0;
  11787. }
  11788. vmx_complete_nested_posted_interrupt(vcpu);
  11789. return 0;
  11790. }
  11791. static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu)
  11792. {
  11793. to_vmx(vcpu)->req_immediate_exit = true;
  11794. }
  11795. static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
  11796. {
  11797. ktime_t remaining =
  11798. hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
  11799. u64 value;
  11800. if (ktime_to_ns(remaining) <= 0)
  11801. return 0;
  11802. value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
  11803. do_div(value, 1000000);
  11804. return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
  11805. }
  11806. /*
  11807. * Update the guest state fields of vmcs12 to reflect changes that
  11808. * occurred while L2 was running. (The "IA-32e mode guest" bit of the
  11809. * VM-entry controls is also updated, since this is really a guest
  11810. * state bit.)
  11811. */
  11812. static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  11813. {
  11814. vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
  11815. vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
  11816. vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
  11817. vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
  11818. vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
  11819. vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
  11820. vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
  11821. vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
  11822. vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
  11823. vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
  11824. vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
  11825. vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
  11826. vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
  11827. vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
  11828. vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
  11829. vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
  11830. vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
  11831. vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
  11832. vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
  11833. vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
  11834. vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
  11835. vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
  11836. vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
  11837. vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
  11838. vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
  11839. vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
  11840. vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
  11841. vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
  11842. vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
  11843. vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
  11844. vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
  11845. vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
  11846. vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
  11847. vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
  11848. vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
  11849. vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
  11850. vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
  11851. vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
  11852. vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
  11853. vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
  11854. vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
  11855. vmcs12->guest_interruptibility_info =
  11856. vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  11857. vmcs12->guest_pending_dbg_exceptions =
  11858. vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
  11859. if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
  11860. vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
  11861. else
  11862. vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
  11863. if (nested_cpu_has_preemption_timer(vmcs12)) {
  11864. if (vmcs12->vm_exit_controls &
  11865. VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
  11866. vmcs12->vmx_preemption_timer_value =
  11867. vmx_get_preemption_timer_value(vcpu);
  11868. hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
  11869. }
  11870. /*
  11871. * In some cases (usually, nested EPT), L2 is allowed to change its
  11872. * own CR3 without exiting. If it has changed it, we must keep it.
  11873. * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
  11874. * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
  11875. *
  11876. * Additionally, restore L2's PDPTR to vmcs12.
  11877. */
  11878. if (enable_ept) {
  11879. vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
  11880. vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
  11881. vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
  11882. vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
  11883. vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
  11884. }
  11885. vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
  11886. if (nested_cpu_has_vid(vmcs12))
  11887. vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
  11888. vmcs12->vm_entry_controls =
  11889. (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
  11890. (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
  11891. if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
  11892. kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
  11893. vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
  11894. }
  11895. /* TODO: These cannot have changed unless we have MSR bitmaps and
  11896. * the relevant bit asks not to trap the change */
  11897. if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
  11898. vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
  11899. if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
  11900. vmcs12->guest_ia32_efer = vcpu->arch.efer;
  11901. vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
  11902. vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
  11903. vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
  11904. if (kvm_mpx_supported())
  11905. vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
  11906. }
  11907. /*
  11908. * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
  11909. * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
  11910. * and this function updates it to reflect the changes to the guest state while
  11911. * L2 was running (and perhaps made some exits which were handled directly by L0
  11912. * without going back to L1), and to reflect the exit reason.
  11913. * Note that we do not have to copy here all VMCS fields, just those that
  11914. * could have changed by the L2 guest or the exit - i.e., the guest-state and
  11915. * exit-information fields only. Other fields are modified by L1 with VMWRITE,
  11916. * which already writes to vmcs12 directly.
  11917. */
  11918. static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
  11919. u32 exit_reason, u32 exit_intr_info,
  11920. unsigned long exit_qualification)
  11921. {
  11922. /* update guest state fields: */
  11923. sync_vmcs12(vcpu, vmcs12);
  11924. /* update exit information fields: */
  11925. vmcs12->vm_exit_reason = exit_reason;
  11926. vmcs12->exit_qualification = exit_qualification;
  11927. vmcs12->vm_exit_intr_info = exit_intr_info;
  11928. vmcs12->idt_vectoring_info_field = 0;
  11929. vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  11930. vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  11931. if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
  11932. vmcs12->launch_state = 1;
  11933. /* vm_entry_intr_info_field is cleared on exit. Emulate this
  11934. * instead of reading the real value. */
  11935. vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
  11936. /*
  11937. * Transfer the event that L0 or L1 may wanted to inject into
  11938. * L2 to IDT_VECTORING_INFO_FIELD.
  11939. */
  11940. vmcs12_save_pending_event(vcpu, vmcs12);
  11941. }
  11942. /*
  11943. * Drop what we picked up for L2 via vmx_complete_interrupts. It is
  11944. * preserved above and would only end up incorrectly in L1.
  11945. */
  11946. vcpu->arch.nmi_injected = false;
  11947. kvm_clear_exception_queue(vcpu);
  11948. kvm_clear_interrupt_queue(vcpu);
  11949. }
  11950. /*
  11951. * A part of what we need to when the nested L2 guest exits and we want to
  11952. * run its L1 parent, is to reset L1's guest state to the host state specified
  11953. * in vmcs12.
  11954. * This function is to be called not only on normal nested exit, but also on
  11955. * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
  11956. * Failures During or After Loading Guest State").
  11957. * This function should be called when the active VMCS is L1's (vmcs01).
  11958. */
  11959. static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
  11960. struct vmcs12 *vmcs12)
  11961. {
  11962. struct kvm_segment seg;
  11963. u32 entry_failure_code;
  11964. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
  11965. vcpu->arch.efer = vmcs12->host_ia32_efer;
  11966. else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
  11967. vcpu->arch.efer |= (EFER_LMA | EFER_LME);
  11968. else
  11969. vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
  11970. vmx_set_efer(vcpu, vcpu->arch.efer);
  11971. kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
  11972. kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
  11973. vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
  11974. vmx_set_interrupt_shadow(vcpu, 0);
  11975. /*
  11976. * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
  11977. * actually changed, because vmx_set_cr0 refers to efer set above.
  11978. *
  11979. * CR0_GUEST_HOST_MASK is already set in the original vmcs01
  11980. * (KVM doesn't change it);
  11981. */
  11982. vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
  11983. vmx_set_cr0(vcpu, vmcs12->host_cr0);
  11984. /* Same as above - no reason to call set_cr4_guest_host_mask(). */
  11985. vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
  11986. vmx_set_cr4(vcpu, vmcs12->host_cr4);
  11987. nested_ept_uninit_mmu_context(vcpu);
  11988. /*
  11989. * Only PDPTE load can fail as the value of cr3 was checked on entry and
  11990. * couldn't have changed.
  11991. */
  11992. if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
  11993. nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
  11994. if (!enable_ept)
  11995. vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
  11996. /*
  11997. * If vmcs01 doesn't use VPID, CPU flushes TLB on every
  11998. * VMEntry/VMExit. Thus, no need to flush TLB.
  11999. *
  12000. * If vmcs12 doesn't use VPID, L1 expects TLB to be
  12001. * flushed on every VMEntry/VMExit.
  12002. *
  12003. * Otherwise, we can preserve TLB entries as long as we are
  12004. * able to tag L1 TLB entries differently than L2 TLB entries.
  12005. *
  12006. * If vmcs12 uses EPT, we need to execute this flush on EPTP01
  12007. * and therefore we request the TLB flush to happen only after VMCS EPTP
  12008. * has been set by KVM_REQ_LOAD_CR3.
  12009. */
  12010. if (enable_vpid &&
  12011. (!nested_cpu_has_vpid(vmcs12) || !nested_has_guest_tlb_tag(vcpu))) {
  12012. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  12013. }
  12014. vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
  12015. vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
  12016. vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
  12017. vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
  12018. vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
  12019. vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
  12020. vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
  12021. /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
  12022. if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
  12023. vmcs_write64(GUEST_BNDCFGS, 0);
  12024. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
  12025. vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
  12026. vcpu->arch.pat = vmcs12->host_ia32_pat;
  12027. }
  12028. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
  12029. vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
  12030. vmcs12->host_ia32_perf_global_ctrl);
  12031. /* Set L1 segment info according to Intel SDM
  12032. 27.5.2 Loading Host Segment and Descriptor-Table Registers */
  12033. seg = (struct kvm_segment) {
  12034. .base = 0,
  12035. .limit = 0xFFFFFFFF,
  12036. .selector = vmcs12->host_cs_selector,
  12037. .type = 11,
  12038. .present = 1,
  12039. .s = 1,
  12040. .g = 1
  12041. };
  12042. if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
  12043. seg.l = 1;
  12044. else
  12045. seg.db = 1;
  12046. vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
  12047. seg = (struct kvm_segment) {
  12048. .base = 0,
  12049. .limit = 0xFFFFFFFF,
  12050. .type = 3,
  12051. .present = 1,
  12052. .s = 1,
  12053. .db = 1,
  12054. .g = 1
  12055. };
  12056. seg.selector = vmcs12->host_ds_selector;
  12057. vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
  12058. seg.selector = vmcs12->host_es_selector;
  12059. vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
  12060. seg.selector = vmcs12->host_ss_selector;
  12061. vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
  12062. seg.selector = vmcs12->host_fs_selector;
  12063. seg.base = vmcs12->host_fs_base;
  12064. vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
  12065. seg.selector = vmcs12->host_gs_selector;
  12066. seg.base = vmcs12->host_gs_base;
  12067. vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
  12068. seg = (struct kvm_segment) {
  12069. .base = vmcs12->host_tr_base,
  12070. .limit = 0x67,
  12071. .selector = vmcs12->host_tr_selector,
  12072. .type = 11,
  12073. .present = 1
  12074. };
  12075. vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
  12076. kvm_set_dr(vcpu, 7, 0x400);
  12077. vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
  12078. if (cpu_has_vmx_msr_bitmap())
  12079. vmx_update_msr_bitmap(vcpu);
  12080. if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
  12081. vmcs12->vm_exit_msr_load_count))
  12082. nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
  12083. }
  12084. static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx)
  12085. {
  12086. struct shared_msr_entry *efer_msr;
  12087. unsigned int i;
  12088. if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER)
  12089. return vmcs_read64(GUEST_IA32_EFER);
  12090. if (cpu_has_load_ia32_efer)
  12091. return host_efer;
  12092. for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) {
  12093. if (vmx->msr_autoload.guest.val[i].index == MSR_EFER)
  12094. return vmx->msr_autoload.guest.val[i].value;
  12095. }
  12096. efer_msr = find_msr_entry(vmx, MSR_EFER);
  12097. if (efer_msr)
  12098. return efer_msr->data;
  12099. return host_efer;
  12100. }
  12101. static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu)
  12102. {
  12103. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  12104. struct vcpu_vmx *vmx = to_vmx(vcpu);
  12105. struct vmx_msr_entry g, h;
  12106. struct msr_data msr;
  12107. gpa_t gpa;
  12108. u32 i, j;
  12109. vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT);
  12110. if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
  12111. /*
  12112. * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set
  12113. * as vmcs01.GUEST_DR7 contains a userspace defined value
  12114. * and vcpu->arch.dr7 is not squirreled away before the
  12115. * nested VMENTER (not worth adding a variable in nested_vmx).
  12116. */
  12117. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
  12118. kvm_set_dr(vcpu, 7, DR7_FIXED_1);
  12119. else
  12120. WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7)));
  12121. }
  12122. /*
  12123. * Note that calling vmx_set_{efer,cr0,cr4} is important as they
  12124. * handle a variety of side effects to KVM's software model.
  12125. */
  12126. vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx));
  12127. vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
  12128. vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW));
  12129. vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
  12130. vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW));
  12131. nested_ept_uninit_mmu_context(vcpu);
  12132. vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
  12133. __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
  12134. /*
  12135. * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs
  12136. * from vmcs01 (if necessary). The PDPTRs are not loaded on
  12137. * VMFail, like everything else we just need to ensure our
  12138. * software model is up-to-date.
  12139. */
  12140. ept_save_pdptrs(vcpu);
  12141. kvm_mmu_reset_context(vcpu);
  12142. if (cpu_has_vmx_msr_bitmap())
  12143. vmx_update_msr_bitmap(vcpu);
  12144. /*
  12145. * This nasty bit of open coding is a compromise between blindly
  12146. * loading L1's MSRs using the exit load lists (incorrect emulation
  12147. * of VMFail), leaving the nested VM's MSRs in the software model
  12148. * (incorrect behavior) and snapshotting the modified MSRs (too
  12149. * expensive since the lists are unbound by hardware). For each
  12150. * MSR that was (prematurely) loaded from the nested VMEntry load
  12151. * list, reload it from the exit load list if it exists and differs
  12152. * from the guest value. The intent is to stuff host state as
  12153. * silently as possible, not to fully process the exit load list.
  12154. */
  12155. msr.host_initiated = false;
  12156. for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) {
  12157. gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g));
  12158. if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) {
  12159. pr_debug_ratelimited(
  12160. "%s read MSR index failed (%u, 0x%08llx)\n",
  12161. __func__, i, gpa);
  12162. goto vmabort;
  12163. }
  12164. for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) {
  12165. gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h));
  12166. if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) {
  12167. pr_debug_ratelimited(
  12168. "%s read MSR failed (%u, 0x%08llx)\n",
  12169. __func__, j, gpa);
  12170. goto vmabort;
  12171. }
  12172. if (h.index != g.index)
  12173. continue;
  12174. if (h.value == g.value)
  12175. break;
  12176. if (nested_vmx_load_msr_check(vcpu, &h)) {
  12177. pr_debug_ratelimited(
  12178. "%s check failed (%u, 0x%x, 0x%x)\n",
  12179. __func__, j, h.index, h.reserved);
  12180. goto vmabort;
  12181. }
  12182. msr.index = h.index;
  12183. msr.data = h.value;
  12184. if (kvm_set_msr(vcpu, &msr)) {
  12185. pr_debug_ratelimited(
  12186. "%s WRMSR failed (%u, 0x%x, 0x%llx)\n",
  12187. __func__, j, h.index, h.value);
  12188. goto vmabort;
  12189. }
  12190. }
  12191. }
  12192. return;
  12193. vmabort:
  12194. nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
  12195. }
  12196. /*
  12197. * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
  12198. * and modify vmcs12 to make it see what it would expect to see there if
  12199. * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
  12200. */
  12201. static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
  12202. u32 exit_intr_info,
  12203. unsigned long exit_qualification)
  12204. {
  12205. struct vcpu_vmx *vmx = to_vmx(vcpu);
  12206. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  12207. /* trying to cancel vmlaunch/vmresume is a bug */
  12208. WARN_ON_ONCE(vmx->nested.nested_run_pending);
  12209. leave_guest_mode(vcpu);
  12210. if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
  12211. vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
  12212. if (likely(!vmx->fail)) {
  12213. if (exit_reason == -1)
  12214. sync_vmcs12(vcpu, vmcs12);
  12215. else
  12216. prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
  12217. exit_qualification);
  12218. /*
  12219. * Must happen outside of sync_vmcs12() as it will
  12220. * also be used to capture vmcs12 cache as part of
  12221. * capturing nVMX state for snapshot (migration).
  12222. *
  12223. * Otherwise, this flush will dirty guest memory at a
  12224. * point it is already assumed by user-space to be
  12225. * immutable.
  12226. */
  12227. nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
  12228. if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
  12229. vmcs12->vm_exit_msr_store_count))
  12230. nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
  12231. } else {
  12232. /*
  12233. * The only expected VM-instruction error is "VM entry with
  12234. * invalid control field(s)." Anything else indicates a
  12235. * problem with L0. And we should never get here with a
  12236. * VMFail of any type if early consistency checks are enabled.
  12237. */
  12238. WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) !=
  12239. VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  12240. WARN_ON_ONCE(nested_early_check);
  12241. }
  12242. vmx_switch_vmcs(vcpu, &vmx->vmcs01);
  12243. /* Update any VMCS fields that might have changed while L2 ran */
  12244. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
  12245. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
  12246. vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
  12247. if (kvm_has_tsc_control)
  12248. decache_tsc_multiplier(vmx);
  12249. if (vmx->nested.change_vmcs01_virtual_apic_mode) {
  12250. vmx->nested.change_vmcs01_virtual_apic_mode = false;
  12251. vmx_set_virtual_apic_mode(vcpu);
  12252. } else if (!nested_cpu_has_ept(vmcs12) &&
  12253. nested_cpu_has2(vmcs12,
  12254. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
  12255. vmx_flush_tlb(vcpu, true);
  12256. }
  12257. /* This is needed for same reason as it was needed in prepare_vmcs02 */
  12258. vmx->host_rsp = 0;
  12259. /* Unpin physical memory we referred to in vmcs02 */
  12260. if (vmx->nested.apic_access_page) {
  12261. kvm_release_page_dirty(vmx->nested.apic_access_page);
  12262. vmx->nested.apic_access_page = NULL;
  12263. }
  12264. if (vmx->nested.virtual_apic_page) {
  12265. kvm_release_page_dirty(vmx->nested.virtual_apic_page);
  12266. vmx->nested.virtual_apic_page = NULL;
  12267. }
  12268. if (vmx->nested.pi_desc_page) {
  12269. kunmap(vmx->nested.pi_desc_page);
  12270. kvm_release_page_dirty(vmx->nested.pi_desc_page);
  12271. vmx->nested.pi_desc_page = NULL;
  12272. vmx->nested.pi_desc = NULL;
  12273. }
  12274. /*
  12275. * We are now running in L2, mmu_notifier will force to reload the
  12276. * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
  12277. */
  12278. kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
  12279. if ((exit_reason != -1) && (enable_shadow_vmcs || vmx->nested.hv_evmcs))
  12280. vmx->nested.need_vmcs12_sync = true;
  12281. /* in case we halted in L2 */
  12282. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  12283. if (likely(!vmx->fail)) {
  12284. /*
  12285. * TODO: SDM says that with acknowledge interrupt on
  12286. * exit, bit 31 of the VM-exit interrupt information
  12287. * (valid interrupt) is always set to 1 on
  12288. * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
  12289. * need kvm_cpu_has_interrupt(). See the commit
  12290. * message for details.
  12291. */
  12292. if (nested_exit_intr_ack_set(vcpu) &&
  12293. exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
  12294. kvm_cpu_has_interrupt(vcpu)) {
  12295. int irq = kvm_cpu_get_interrupt(vcpu);
  12296. WARN_ON(irq < 0);
  12297. vmcs12->vm_exit_intr_info = irq |
  12298. INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
  12299. }
  12300. if (exit_reason != -1)
  12301. trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
  12302. vmcs12->exit_qualification,
  12303. vmcs12->idt_vectoring_info_field,
  12304. vmcs12->vm_exit_intr_info,
  12305. vmcs12->vm_exit_intr_error_code,
  12306. KVM_ISA_VMX);
  12307. load_vmcs12_host_state(vcpu, vmcs12);
  12308. return;
  12309. }
  12310. /*
  12311. * After an early L2 VM-entry failure, we're now back
  12312. * in L1 which thinks it just finished a VMLAUNCH or
  12313. * VMRESUME instruction, so we need to set the failure
  12314. * flag and the VM-instruction error field of the VMCS
  12315. * accordingly, and skip the emulated instruction.
  12316. */
  12317. (void)nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  12318. /*
  12319. * Restore L1's host state to KVM's software model. We're here
  12320. * because a consistency check was caught by hardware, which
  12321. * means some amount of guest state has been propagated to KVM's
  12322. * model and needs to be unwound to the host's state.
  12323. */
  12324. nested_vmx_restore_host_state(vcpu);
  12325. vmx->fail = 0;
  12326. }
  12327. /*
  12328. * Forcibly leave nested mode in order to be able to reset the VCPU later on.
  12329. */
  12330. static void vmx_leave_nested(struct kvm_vcpu *vcpu)
  12331. {
  12332. if (is_guest_mode(vcpu)) {
  12333. to_vmx(vcpu)->nested.nested_run_pending = 0;
  12334. nested_vmx_vmexit(vcpu, -1, 0, 0);
  12335. }
  12336. free_nested(vcpu);
  12337. }
  12338. static int vmx_check_intercept(struct kvm_vcpu *vcpu,
  12339. struct x86_instruction_info *info,
  12340. enum x86_intercept_stage stage)
  12341. {
  12342. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  12343. struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
  12344. /*
  12345. * RDPID causes #UD if disabled through secondary execution controls.
  12346. * Because it is marked as EmulateOnUD, we need to intercept it here.
  12347. */
  12348. if (info->intercept == x86_intercept_rdtscp &&
  12349. !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
  12350. ctxt->exception.vector = UD_VECTOR;
  12351. ctxt->exception.error_code_valid = false;
  12352. return X86EMUL_PROPAGATE_FAULT;
  12353. }
  12354. /* TODO: check more intercepts... */
  12355. return X86EMUL_CONTINUE;
  12356. }
  12357. #ifdef CONFIG_X86_64
  12358. /* (a << shift) / divisor, return 1 if overflow otherwise 0 */
  12359. static inline int u64_shl_div_u64(u64 a, unsigned int shift,
  12360. u64 divisor, u64 *result)
  12361. {
  12362. u64 low = a << shift, high = a >> (64 - shift);
  12363. /* To avoid the overflow on divq */
  12364. if (high >= divisor)
  12365. return 1;
  12366. /* Low hold the result, high hold rem which is discarded */
  12367. asm("divq %2\n\t" : "=a" (low), "=d" (high) :
  12368. "rm" (divisor), "0" (low), "1" (high));
  12369. *result = low;
  12370. return 0;
  12371. }
  12372. static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
  12373. {
  12374. struct vcpu_vmx *vmx;
  12375. u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
  12376. if (kvm_mwait_in_guest(vcpu->kvm))
  12377. return -EOPNOTSUPP;
  12378. vmx = to_vmx(vcpu);
  12379. tscl = rdtsc();
  12380. guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
  12381. delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
  12382. lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
  12383. if (delta_tsc > lapic_timer_advance_cycles)
  12384. delta_tsc -= lapic_timer_advance_cycles;
  12385. else
  12386. delta_tsc = 0;
  12387. /* Convert to host delta tsc if tsc scaling is enabled */
  12388. if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
  12389. u64_shl_div_u64(delta_tsc,
  12390. kvm_tsc_scaling_ratio_frac_bits,
  12391. vcpu->arch.tsc_scaling_ratio,
  12392. &delta_tsc))
  12393. return -ERANGE;
  12394. /*
  12395. * If the delta tsc can't fit in the 32 bit after the multi shift,
  12396. * we can't use the preemption timer.
  12397. * It's possible that it fits on later vmentries, but checking
  12398. * on every vmentry is costly so we just use an hrtimer.
  12399. */
  12400. if (delta_tsc >> (cpu_preemption_timer_multi + 32))
  12401. return -ERANGE;
  12402. vmx->hv_deadline_tsc = tscl + delta_tsc;
  12403. return delta_tsc == 0;
  12404. }
  12405. static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
  12406. {
  12407. to_vmx(vcpu)->hv_deadline_tsc = -1;
  12408. }
  12409. #endif
  12410. static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
  12411. {
  12412. if (!kvm_pause_in_guest(vcpu->kvm))
  12413. shrink_ple_window(vcpu);
  12414. }
  12415. static void vmx_slot_enable_log_dirty(struct kvm *kvm,
  12416. struct kvm_memory_slot *slot)
  12417. {
  12418. kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
  12419. kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
  12420. }
  12421. static void vmx_slot_disable_log_dirty(struct kvm *kvm,
  12422. struct kvm_memory_slot *slot)
  12423. {
  12424. kvm_mmu_slot_set_dirty(kvm, slot);
  12425. }
  12426. static void vmx_flush_log_dirty(struct kvm *kvm)
  12427. {
  12428. kvm_flush_pml_buffers(kvm);
  12429. }
  12430. static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
  12431. {
  12432. struct vmcs12 *vmcs12;
  12433. struct vcpu_vmx *vmx = to_vmx(vcpu);
  12434. gpa_t gpa;
  12435. struct page *page = NULL;
  12436. u64 *pml_address;
  12437. if (is_guest_mode(vcpu)) {
  12438. WARN_ON_ONCE(vmx->nested.pml_full);
  12439. /*
  12440. * Check if PML is enabled for the nested guest.
  12441. * Whether eptp bit 6 is set is already checked
  12442. * as part of A/D emulation.
  12443. */
  12444. vmcs12 = get_vmcs12(vcpu);
  12445. if (!nested_cpu_has_pml(vmcs12))
  12446. return 0;
  12447. if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
  12448. vmx->nested.pml_full = true;
  12449. return 1;
  12450. }
  12451. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
  12452. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
  12453. if (is_error_page(page))
  12454. return 0;
  12455. pml_address = kmap(page);
  12456. pml_address[vmcs12->guest_pml_index--] = gpa;
  12457. kunmap(page);
  12458. kvm_release_page_clean(page);
  12459. }
  12460. return 0;
  12461. }
  12462. static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
  12463. struct kvm_memory_slot *memslot,
  12464. gfn_t offset, unsigned long mask)
  12465. {
  12466. kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
  12467. }
  12468. static void __pi_post_block(struct kvm_vcpu *vcpu)
  12469. {
  12470. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  12471. struct pi_desc old, new;
  12472. unsigned int dest;
  12473. do {
  12474. old.control = new.control = pi_desc->control;
  12475. WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
  12476. "Wakeup handler not enabled while the VCPU is blocked\n");
  12477. dest = cpu_physical_id(vcpu->cpu);
  12478. if (x2apic_enabled())
  12479. new.ndst = dest;
  12480. else
  12481. new.ndst = (dest << 8) & 0xFF00;
  12482. /* set 'NV' to 'notification vector' */
  12483. new.nv = POSTED_INTR_VECTOR;
  12484. } while (cmpxchg64(&pi_desc->control, old.control,
  12485. new.control) != old.control);
  12486. if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
  12487. spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
  12488. list_del(&vcpu->blocked_vcpu_list);
  12489. spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
  12490. vcpu->pre_pcpu = -1;
  12491. }
  12492. }
  12493. /*
  12494. * This routine does the following things for vCPU which is going
  12495. * to be blocked if VT-d PI is enabled.
  12496. * - Store the vCPU to the wakeup list, so when interrupts happen
  12497. * we can find the right vCPU to wake up.
  12498. * - Change the Posted-interrupt descriptor as below:
  12499. * 'NDST' <-- vcpu->pre_pcpu
  12500. * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
  12501. * - If 'ON' is set during this process, which means at least one
  12502. * interrupt is posted for this vCPU, we cannot block it, in
  12503. * this case, return 1, otherwise, return 0.
  12504. *
  12505. */
  12506. static int pi_pre_block(struct kvm_vcpu *vcpu)
  12507. {
  12508. unsigned int dest;
  12509. struct pi_desc old, new;
  12510. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  12511. if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
  12512. !irq_remapping_cap(IRQ_POSTING_CAP) ||
  12513. !kvm_vcpu_apicv_active(vcpu))
  12514. return 0;
  12515. WARN_ON(irqs_disabled());
  12516. local_irq_disable();
  12517. if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
  12518. vcpu->pre_pcpu = vcpu->cpu;
  12519. spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
  12520. list_add_tail(&vcpu->blocked_vcpu_list,
  12521. &per_cpu(blocked_vcpu_on_cpu,
  12522. vcpu->pre_pcpu));
  12523. spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
  12524. }
  12525. do {
  12526. old.control = new.control = pi_desc->control;
  12527. WARN((pi_desc->sn == 1),
  12528. "Warning: SN field of posted-interrupts "
  12529. "is set before blocking\n");
  12530. /*
  12531. * Since vCPU can be preempted during this process,
  12532. * vcpu->cpu could be different with pre_pcpu, we
  12533. * need to set pre_pcpu as the destination of wakeup
  12534. * notification event, then we can find the right vCPU
  12535. * to wakeup in wakeup handler if interrupts happen
  12536. * when the vCPU is in blocked state.
  12537. */
  12538. dest = cpu_physical_id(vcpu->pre_pcpu);
  12539. if (x2apic_enabled())
  12540. new.ndst = dest;
  12541. else
  12542. new.ndst = (dest << 8) & 0xFF00;
  12543. /* set 'NV' to 'wakeup vector' */
  12544. new.nv = POSTED_INTR_WAKEUP_VECTOR;
  12545. } while (cmpxchg64(&pi_desc->control, old.control,
  12546. new.control) != old.control);
  12547. /* We should not block the vCPU if an interrupt is posted for it. */
  12548. if (pi_test_on(pi_desc) == 1)
  12549. __pi_post_block(vcpu);
  12550. local_irq_enable();
  12551. return (vcpu->pre_pcpu == -1);
  12552. }
  12553. static int vmx_pre_block(struct kvm_vcpu *vcpu)
  12554. {
  12555. if (pi_pre_block(vcpu))
  12556. return 1;
  12557. if (kvm_lapic_hv_timer_in_use(vcpu))
  12558. kvm_lapic_switch_to_sw_timer(vcpu);
  12559. return 0;
  12560. }
  12561. static void pi_post_block(struct kvm_vcpu *vcpu)
  12562. {
  12563. if (vcpu->pre_pcpu == -1)
  12564. return;
  12565. WARN_ON(irqs_disabled());
  12566. local_irq_disable();
  12567. __pi_post_block(vcpu);
  12568. local_irq_enable();
  12569. }
  12570. static void vmx_post_block(struct kvm_vcpu *vcpu)
  12571. {
  12572. if (kvm_x86_ops->set_hv_timer)
  12573. kvm_lapic_switch_to_hv_timer(vcpu);
  12574. pi_post_block(vcpu);
  12575. }
  12576. /*
  12577. * vmx_update_pi_irte - set IRTE for Posted-Interrupts
  12578. *
  12579. * @kvm: kvm
  12580. * @host_irq: host irq of the interrupt
  12581. * @guest_irq: gsi of the interrupt
  12582. * @set: set or unset PI
  12583. * returns 0 on success, < 0 on failure
  12584. */
  12585. static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
  12586. uint32_t guest_irq, bool set)
  12587. {
  12588. struct kvm_kernel_irq_routing_entry *e;
  12589. struct kvm_irq_routing_table *irq_rt;
  12590. struct kvm_lapic_irq irq;
  12591. struct kvm_vcpu *vcpu;
  12592. struct vcpu_data vcpu_info;
  12593. int idx, ret = 0;
  12594. if (!kvm_arch_has_assigned_device(kvm) ||
  12595. !irq_remapping_cap(IRQ_POSTING_CAP) ||
  12596. !kvm_vcpu_apicv_active(kvm->vcpus[0]))
  12597. return 0;
  12598. idx = srcu_read_lock(&kvm->irq_srcu);
  12599. irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
  12600. if (guest_irq >= irq_rt->nr_rt_entries ||
  12601. hlist_empty(&irq_rt->map[guest_irq])) {
  12602. pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
  12603. guest_irq, irq_rt->nr_rt_entries);
  12604. goto out;
  12605. }
  12606. hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
  12607. if (e->type != KVM_IRQ_ROUTING_MSI)
  12608. continue;
  12609. /*
  12610. * VT-d PI cannot support posting multicast/broadcast
  12611. * interrupts to a vCPU, we still use interrupt remapping
  12612. * for these kind of interrupts.
  12613. *
  12614. * For lowest-priority interrupts, we only support
  12615. * those with single CPU as the destination, e.g. user
  12616. * configures the interrupts via /proc/irq or uses
  12617. * irqbalance to make the interrupts single-CPU.
  12618. *
  12619. * We will support full lowest-priority interrupt later.
  12620. */
  12621. kvm_set_msi_irq(kvm, e, &irq);
  12622. if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
  12623. /*
  12624. * Make sure the IRTE is in remapped mode if
  12625. * we don't handle it in posted mode.
  12626. */
  12627. ret = irq_set_vcpu_affinity(host_irq, NULL);
  12628. if (ret < 0) {
  12629. printk(KERN_INFO
  12630. "failed to back to remapped mode, irq: %u\n",
  12631. host_irq);
  12632. goto out;
  12633. }
  12634. continue;
  12635. }
  12636. vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
  12637. vcpu_info.vector = irq.vector;
  12638. trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
  12639. vcpu_info.vector, vcpu_info.pi_desc_addr, set);
  12640. if (set)
  12641. ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
  12642. else
  12643. ret = irq_set_vcpu_affinity(host_irq, NULL);
  12644. if (ret < 0) {
  12645. printk(KERN_INFO "%s: failed to update PI IRTE\n",
  12646. __func__);
  12647. goto out;
  12648. }
  12649. }
  12650. ret = 0;
  12651. out:
  12652. srcu_read_unlock(&kvm->irq_srcu, idx);
  12653. return ret;
  12654. }
  12655. static void vmx_setup_mce(struct kvm_vcpu *vcpu)
  12656. {
  12657. if (vcpu->arch.mcg_cap & MCG_LMCE_P)
  12658. to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
  12659. FEATURE_CONTROL_LMCE;
  12660. else
  12661. to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
  12662. ~FEATURE_CONTROL_LMCE;
  12663. }
  12664. static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
  12665. {
  12666. /* we need a nested vmexit to enter SMM, postpone if run is pending */
  12667. if (to_vmx(vcpu)->nested.nested_run_pending)
  12668. return 0;
  12669. return 1;
  12670. }
  12671. static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
  12672. {
  12673. struct vcpu_vmx *vmx = to_vmx(vcpu);
  12674. vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
  12675. if (vmx->nested.smm.guest_mode)
  12676. nested_vmx_vmexit(vcpu, -1, 0, 0);
  12677. vmx->nested.smm.vmxon = vmx->nested.vmxon;
  12678. vmx->nested.vmxon = false;
  12679. vmx_clear_hlt(vcpu);
  12680. return 0;
  12681. }
  12682. static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
  12683. {
  12684. struct vcpu_vmx *vmx = to_vmx(vcpu);
  12685. int ret;
  12686. if (vmx->nested.smm.vmxon) {
  12687. vmx->nested.vmxon = true;
  12688. vmx->nested.smm.vmxon = false;
  12689. }
  12690. if (vmx->nested.smm.guest_mode) {
  12691. vcpu->arch.hflags &= ~HF_SMM_MASK;
  12692. ret = nested_vmx_enter_non_root_mode(vcpu, false);
  12693. vcpu->arch.hflags |= HF_SMM_MASK;
  12694. if (ret)
  12695. return ret;
  12696. vmx->nested.smm.guest_mode = false;
  12697. }
  12698. return 0;
  12699. }
  12700. static int enable_smi_window(struct kvm_vcpu *vcpu)
  12701. {
  12702. return 0;
  12703. }
  12704. static inline int vmx_has_valid_vmcs12(struct kvm_vcpu *vcpu)
  12705. {
  12706. struct vcpu_vmx *vmx = to_vmx(vcpu);
  12707. /*
  12708. * In case we do two consecutive get/set_nested_state()s while L2 was
  12709. * running hv_evmcs may end up not being mapped (we map it from
  12710. * nested_vmx_run()/vmx_vcpu_run()). Check is_guest_mode() as we always
  12711. * have vmcs12 if it is true.
  12712. */
  12713. return is_guest_mode(vcpu) || vmx->nested.current_vmptr != -1ull ||
  12714. vmx->nested.hv_evmcs;
  12715. }
  12716. static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
  12717. struct kvm_nested_state __user *user_kvm_nested_state,
  12718. u32 user_data_size)
  12719. {
  12720. struct vcpu_vmx *vmx;
  12721. struct vmcs12 *vmcs12;
  12722. struct kvm_nested_state kvm_state = {
  12723. .flags = 0,
  12724. .format = 0,
  12725. .size = sizeof(kvm_state),
  12726. .vmx.vmxon_pa = -1ull,
  12727. .vmx.vmcs_pa = -1ull,
  12728. };
  12729. if (!vcpu)
  12730. return kvm_state.size + 2 * VMCS12_SIZE;
  12731. vmx = to_vmx(vcpu);
  12732. vmcs12 = get_vmcs12(vcpu);
  12733. if (nested_vmx_allowed(vcpu) && vmx->nested.enlightened_vmcs_enabled)
  12734. kvm_state.flags |= KVM_STATE_NESTED_EVMCS;
  12735. if (nested_vmx_allowed(vcpu) &&
  12736. (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
  12737. kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
  12738. kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
  12739. if (vmx_has_valid_vmcs12(vcpu)) {
  12740. kvm_state.size += VMCS12_SIZE;
  12741. if (is_guest_mode(vcpu) &&
  12742. nested_cpu_has_shadow_vmcs(vmcs12) &&
  12743. vmcs12->vmcs_link_pointer != -1ull)
  12744. kvm_state.size += VMCS12_SIZE;
  12745. }
  12746. if (vmx->nested.smm.vmxon)
  12747. kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
  12748. if (vmx->nested.smm.guest_mode)
  12749. kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
  12750. if (is_guest_mode(vcpu)) {
  12751. kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
  12752. if (vmx->nested.nested_run_pending)
  12753. kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
  12754. }
  12755. }
  12756. if (user_data_size < kvm_state.size)
  12757. goto out;
  12758. if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
  12759. return -EFAULT;
  12760. if (!vmx_has_valid_vmcs12(vcpu))
  12761. goto out;
  12762. /*
  12763. * When running L2, the authoritative vmcs12 state is in the
  12764. * vmcs02. When running L1, the authoritative vmcs12 state is
  12765. * in the shadow or enlightened vmcs linked to vmcs01, unless
  12766. * need_vmcs12_sync is set, in which case, the authoritative
  12767. * vmcs12 state is in the vmcs12 already.
  12768. */
  12769. if (is_guest_mode(vcpu)) {
  12770. sync_vmcs12(vcpu, vmcs12);
  12771. } else if (!vmx->nested.need_vmcs12_sync) {
  12772. if (vmx->nested.hv_evmcs)
  12773. copy_enlightened_to_vmcs12(vmx);
  12774. else if (enable_shadow_vmcs)
  12775. copy_shadow_to_vmcs12(vmx);
  12776. }
  12777. if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12)))
  12778. return -EFAULT;
  12779. if (nested_cpu_has_shadow_vmcs(vmcs12) &&
  12780. vmcs12->vmcs_link_pointer != -1ull) {
  12781. if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
  12782. get_shadow_vmcs12(vcpu), sizeof(*vmcs12)))
  12783. return -EFAULT;
  12784. }
  12785. out:
  12786. return kvm_state.size;
  12787. }
  12788. static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
  12789. struct kvm_nested_state __user *user_kvm_nested_state,
  12790. struct kvm_nested_state *kvm_state)
  12791. {
  12792. struct vcpu_vmx *vmx = to_vmx(vcpu);
  12793. struct vmcs12 *vmcs12;
  12794. u32 exit_qual;
  12795. int ret;
  12796. if (kvm_state->format != 0)
  12797. return -EINVAL;
  12798. if (kvm_state->flags & KVM_STATE_NESTED_EVMCS)
  12799. nested_enable_evmcs(vcpu, NULL);
  12800. if (!nested_vmx_allowed(vcpu))
  12801. return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
  12802. if (kvm_state->vmx.vmxon_pa == -1ull) {
  12803. if (kvm_state->vmx.smm.flags)
  12804. return -EINVAL;
  12805. if (kvm_state->vmx.vmcs_pa != -1ull)
  12806. return -EINVAL;
  12807. vmx_leave_nested(vcpu);
  12808. return 0;
  12809. }
  12810. if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
  12811. return -EINVAL;
  12812. if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
  12813. (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
  12814. return -EINVAL;
  12815. if (kvm_state->vmx.smm.flags &
  12816. ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
  12817. return -EINVAL;
  12818. /*
  12819. * SMM temporarily disables VMX, so we cannot be in guest mode,
  12820. * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags
  12821. * must be zero.
  12822. */
  12823. if (is_smm(vcpu) ? kvm_state->flags : kvm_state->vmx.smm.flags)
  12824. return -EINVAL;
  12825. if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
  12826. !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
  12827. return -EINVAL;
  12828. vmx_leave_nested(vcpu);
  12829. if (kvm_state->vmx.vmxon_pa == -1ull)
  12830. return 0;
  12831. vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
  12832. ret = enter_vmx_operation(vcpu);
  12833. if (ret)
  12834. return ret;
  12835. /* Empty 'VMXON' state is permitted */
  12836. if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
  12837. return 0;
  12838. if (kvm_state->vmx.vmcs_pa != -1ull) {
  12839. if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
  12840. !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
  12841. return -EINVAL;
  12842. set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
  12843. } else if (kvm_state->flags & KVM_STATE_NESTED_EVMCS) {
  12844. /*
  12845. * Sync eVMCS upon entry as we may not have
  12846. * HV_X64_MSR_VP_ASSIST_PAGE set up yet.
  12847. */
  12848. vmx->nested.need_vmcs12_sync = true;
  12849. } else {
  12850. return -EINVAL;
  12851. }
  12852. if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
  12853. vmx->nested.smm.vmxon = true;
  12854. vmx->nested.vmxon = false;
  12855. if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
  12856. vmx->nested.smm.guest_mode = true;
  12857. }
  12858. vmcs12 = get_vmcs12(vcpu);
  12859. if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
  12860. return -EFAULT;
  12861. if (vmcs12->hdr.revision_id != VMCS12_REVISION)
  12862. return -EINVAL;
  12863. if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
  12864. return 0;
  12865. vmx->nested.nested_run_pending =
  12866. !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
  12867. if (nested_cpu_has_shadow_vmcs(vmcs12) &&
  12868. vmcs12->vmcs_link_pointer != -1ull) {
  12869. struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
  12870. if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12))
  12871. return -EINVAL;
  12872. if (copy_from_user(shadow_vmcs12,
  12873. user_kvm_nested_state->data + VMCS12_SIZE,
  12874. sizeof(*vmcs12)))
  12875. return -EFAULT;
  12876. if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
  12877. !shadow_vmcs12->hdr.shadow_vmcs)
  12878. return -EINVAL;
  12879. }
  12880. if (check_vmentry_prereqs(vcpu, vmcs12) ||
  12881. check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
  12882. return -EINVAL;
  12883. vmx->nested.dirty_vmcs12 = true;
  12884. ret = nested_vmx_enter_non_root_mode(vcpu, false);
  12885. if (ret)
  12886. return -EINVAL;
  12887. return 0;
  12888. }
  12889. static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
  12890. .cpu_has_kvm_support = cpu_has_kvm_support,
  12891. .disabled_by_bios = vmx_disabled_by_bios,
  12892. .hardware_setup = hardware_setup,
  12893. .hardware_unsetup = hardware_unsetup,
  12894. .check_processor_compatibility = vmx_check_processor_compat,
  12895. .hardware_enable = hardware_enable,
  12896. .hardware_disable = hardware_disable,
  12897. .cpu_has_accelerated_tpr = report_flexpriority,
  12898. .has_emulated_msr = vmx_has_emulated_msr,
  12899. .vm_init = vmx_vm_init,
  12900. .vm_alloc = vmx_vm_alloc,
  12901. .vm_free = vmx_vm_free,
  12902. .vcpu_create = vmx_create_vcpu,
  12903. .vcpu_free = vmx_free_vcpu,
  12904. .vcpu_reset = vmx_vcpu_reset,
  12905. .prepare_guest_switch = vmx_prepare_switch_to_guest,
  12906. .vcpu_load = vmx_vcpu_load,
  12907. .vcpu_put = vmx_vcpu_put,
  12908. .update_bp_intercept = update_exception_bitmap,
  12909. .get_msr_feature = vmx_get_msr_feature,
  12910. .get_msr = vmx_get_msr,
  12911. .set_msr = vmx_set_msr,
  12912. .get_segment_base = vmx_get_segment_base,
  12913. .get_segment = vmx_get_segment,
  12914. .set_segment = vmx_set_segment,
  12915. .get_cpl = vmx_get_cpl,
  12916. .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
  12917. .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
  12918. .decache_cr3 = vmx_decache_cr3,
  12919. .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
  12920. .set_cr0 = vmx_set_cr0,
  12921. .set_cr3 = vmx_set_cr3,
  12922. .set_cr4 = vmx_set_cr4,
  12923. .set_efer = vmx_set_efer,
  12924. .get_idt = vmx_get_idt,
  12925. .set_idt = vmx_set_idt,
  12926. .get_gdt = vmx_get_gdt,
  12927. .set_gdt = vmx_set_gdt,
  12928. .get_dr6 = vmx_get_dr6,
  12929. .set_dr6 = vmx_set_dr6,
  12930. .set_dr7 = vmx_set_dr7,
  12931. .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
  12932. .cache_reg = vmx_cache_reg,
  12933. .get_rflags = vmx_get_rflags,
  12934. .set_rflags = vmx_set_rflags,
  12935. .tlb_flush = vmx_flush_tlb,
  12936. .tlb_flush_gva = vmx_flush_tlb_gva,
  12937. .run = vmx_vcpu_run,
  12938. .handle_exit = vmx_handle_exit,
  12939. .skip_emulated_instruction = skip_emulated_instruction,
  12940. .set_interrupt_shadow = vmx_set_interrupt_shadow,
  12941. .get_interrupt_shadow = vmx_get_interrupt_shadow,
  12942. .patch_hypercall = vmx_patch_hypercall,
  12943. .set_irq = vmx_inject_irq,
  12944. .set_nmi = vmx_inject_nmi,
  12945. .queue_exception = vmx_queue_exception,
  12946. .cancel_injection = vmx_cancel_injection,
  12947. .interrupt_allowed = vmx_interrupt_allowed,
  12948. .nmi_allowed = vmx_nmi_allowed,
  12949. .get_nmi_mask = vmx_get_nmi_mask,
  12950. .set_nmi_mask = vmx_set_nmi_mask,
  12951. .enable_nmi_window = enable_nmi_window,
  12952. .enable_irq_window = enable_irq_window,
  12953. .update_cr8_intercept = update_cr8_intercept,
  12954. .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
  12955. .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
  12956. .get_enable_apicv = vmx_get_enable_apicv,
  12957. .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
  12958. .load_eoi_exitmap = vmx_load_eoi_exitmap,
  12959. .apicv_post_state_restore = vmx_apicv_post_state_restore,
  12960. .hwapic_irr_update = vmx_hwapic_irr_update,
  12961. .hwapic_isr_update = vmx_hwapic_isr_update,
  12962. .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt,
  12963. .sync_pir_to_irr = vmx_sync_pir_to_irr,
  12964. .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
  12965. .set_tss_addr = vmx_set_tss_addr,
  12966. .set_identity_map_addr = vmx_set_identity_map_addr,
  12967. .get_tdp_level = get_ept_level,
  12968. .get_mt_mask = vmx_get_mt_mask,
  12969. .get_exit_info = vmx_get_exit_info,
  12970. .get_lpage_level = vmx_get_lpage_level,
  12971. .cpuid_update = vmx_cpuid_update,
  12972. .rdtscp_supported = vmx_rdtscp_supported,
  12973. .invpcid_supported = vmx_invpcid_supported,
  12974. .set_supported_cpuid = vmx_set_supported_cpuid,
  12975. .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
  12976. .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
  12977. .write_l1_tsc_offset = vmx_write_l1_tsc_offset,
  12978. .set_tdp_cr3 = vmx_set_cr3,
  12979. .check_intercept = vmx_check_intercept,
  12980. .handle_external_intr = vmx_handle_external_intr,
  12981. .mpx_supported = vmx_mpx_supported,
  12982. .xsaves_supported = vmx_xsaves_supported,
  12983. .umip_emulated = vmx_umip_emulated,
  12984. .check_nested_events = vmx_check_nested_events,
  12985. .request_immediate_exit = vmx_request_immediate_exit,
  12986. .sched_in = vmx_sched_in,
  12987. .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
  12988. .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
  12989. .flush_log_dirty = vmx_flush_log_dirty,
  12990. .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
  12991. .write_log_dirty = vmx_write_pml_buffer,
  12992. .pre_block = vmx_pre_block,
  12993. .post_block = vmx_post_block,
  12994. .pmu_ops = &intel_pmu_ops,
  12995. .update_pi_irte = vmx_update_pi_irte,
  12996. #ifdef CONFIG_X86_64
  12997. .set_hv_timer = vmx_set_hv_timer,
  12998. .cancel_hv_timer = vmx_cancel_hv_timer,
  12999. #endif
  13000. .setup_mce = vmx_setup_mce,
  13001. .get_nested_state = vmx_get_nested_state,
  13002. .set_nested_state = vmx_set_nested_state,
  13003. .get_vmcs12_pages = nested_get_vmcs12_pages,
  13004. .smi_allowed = vmx_smi_allowed,
  13005. .pre_enter_smm = vmx_pre_enter_smm,
  13006. .pre_leave_smm = vmx_pre_leave_smm,
  13007. .enable_smi_window = enable_smi_window,
  13008. .nested_enable_evmcs = nested_enable_evmcs,
  13009. };
  13010. static void vmx_cleanup_l1d_flush(void)
  13011. {
  13012. if (vmx_l1d_flush_pages) {
  13013. free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
  13014. vmx_l1d_flush_pages = NULL;
  13015. }
  13016. /* Restore state so sysfs ignores VMX */
  13017. l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
  13018. }
  13019. static void vmx_exit(void)
  13020. {
  13021. #ifdef CONFIG_KEXEC_CORE
  13022. RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
  13023. synchronize_rcu();
  13024. #endif
  13025. kvm_exit();
  13026. #if IS_ENABLED(CONFIG_HYPERV)
  13027. if (static_branch_unlikely(&enable_evmcs)) {
  13028. int cpu;
  13029. struct hv_vp_assist_page *vp_ap;
  13030. /*
  13031. * Reset everything to support using non-enlightened VMCS
  13032. * access later (e.g. when we reload the module with
  13033. * enlightened_vmcs=0)
  13034. */
  13035. for_each_online_cpu(cpu) {
  13036. vp_ap = hv_get_vp_assist_page(cpu);
  13037. if (!vp_ap)
  13038. continue;
  13039. vp_ap->current_nested_vmcs = 0;
  13040. vp_ap->enlighten_vmentry = 0;
  13041. }
  13042. static_branch_disable(&enable_evmcs);
  13043. }
  13044. #endif
  13045. vmx_cleanup_l1d_flush();
  13046. }
  13047. module_exit(vmx_exit);
  13048. static int __init vmx_init(void)
  13049. {
  13050. int r;
  13051. #if IS_ENABLED(CONFIG_HYPERV)
  13052. /*
  13053. * Enlightened VMCS usage should be recommended and the host needs
  13054. * to support eVMCS v1 or above. We can also disable eVMCS support
  13055. * with module parameter.
  13056. */
  13057. if (enlightened_vmcs &&
  13058. ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
  13059. (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
  13060. KVM_EVMCS_VERSION) {
  13061. int cpu;
  13062. /* Check that we have assist pages on all online CPUs */
  13063. for_each_online_cpu(cpu) {
  13064. if (!hv_get_vp_assist_page(cpu)) {
  13065. enlightened_vmcs = false;
  13066. break;
  13067. }
  13068. }
  13069. if (enlightened_vmcs) {
  13070. pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
  13071. static_branch_enable(&enable_evmcs);
  13072. }
  13073. } else {
  13074. enlightened_vmcs = false;
  13075. }
  13076. #endif
  13077. r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
  13078. __alignof__(struct vcpu_vmx), THIS_MODULE);
  13079. if (r)
  13080. return r;
  13081. /*
  13082. * Must be called after kvm_init() so enable_ept is properly set
  13083. * up. Hand the parameter mitigation value in which was stored in
  13084. * the pre module init parser. If no parameter was given, it will
  13085. * contain 'auto' which will be turned into the default 'cond'
  13086. * mitigation mode.
  13087. */
  13088. if (boot_cpu_has(X86_BUG_L1TF)) {
  13089. r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
  13090. if (r) {
  13091. vmx_exit();
  13092. return r;
  13093. }
  13094. }
  13095. #ifdef CONFIG_KEXEC_CORE
  13096. rcu_assign_pointer(crash_vmclear_loaded_vmcss,
  13097. crash_vmclear_local_loaded_vmcss);
  13098. #endif
  13099. vmx_check_vmcs12_offsets();
  13100. return 0;
  13101. }
  13102. module_init(vmx_init);