vmx.c 434 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218
  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. static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
  148. module_param(ple_window, uint, 0444);
  149. /* Default doubles per-vcpu window every exit. */
  150. static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
  151. module_param(ple_window_grow, uint, 0444);
  152. /* Default resets per-vcpu window every exit to ple_window. */
  153. static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
  154. module_param(ple_window_shrink, uint, 0444);
  155. /* Default is to compute the maximum so we can never overflow. */
  156. static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
  157. module_param(ple_window_max, uint, 0444);
  158. extern const ulong vmx_return;
  159. extern const ulong vmx_early_consistency_check_return;
  160. static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
  161. static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
  162. static DEFINE_MUTEX(vmx_l1d_flush_mutex);
  163. /* Storage for pre module init parameter parsing */
  164. static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
  165. static const struct {
  166. const char *option;
  167. bool for_parse;
  168. } vmentry_l1d_param[] = {
  169. [VMENTER_L1D_FLUSH_AUTO] = {"auto", true},
  170. [VMENTER_L1D_FLUSH_NEVER] = {"never", true},
  171. [VMENTER_L1D_FLUSH_COND] = {"cond", true},
  172. [VMENTER_L1D_FLUSH_ALWAYS] = {"always", true},
  173. [VMENTER_L1D_FLUSH_EPT_DISABLED] = {"EPT disabled", false},
  174. [VMENTER_L1D_FLUSH_NOT_REQUIRED] = {"not required", false},
  175. };
  176. #define L1D_CACHE_ORDER 4
  177. static void *vmx_l1d_flush_pages;
  178. static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
  179. {
  180. struct page *page;
  181. unsigned int i;
  182. if (!enable_ept) {
  183. l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
  184. return 0;
  185. }
  186. if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) {
  187. u64 msr;
  188. rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr);
  189. if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) {
  190. l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
  191. return 0;
  192. }
  193. }
  194. /* If set to auto use the default l1tf mitigation method */
  195. if (l1tf == VMENTER_L1D_FLUSH_AUTO) {
  196. switch (l1tf_mitigation) {
  197. case L1TF_MITIGATION_OFF:
  198. l1tf = VMENTER_L1D_FLUSH_NEVER;
  199. break;
  200. case L1TF_MITIGATION_FLUSH_NOWARN:
  201. case L1TF_MITIGATION_FLUSH:
  202. case L1TF_MITIGATION_FLUSH_NOSMT:
  203. l1tf = VMENTER_L1D_FLUSH_COND;
  204. break;
  205. case L1TF_MITIGATION_FULL:
  206. case L1TF_MITIGATION_FULL_FORCE:
  207. l1tf = VMENTER_L1D_FLUSH_ALWAYS;
  208. break;
  209. }
  210. } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) {
  211. l1tf = VMENTER_L1D_FLUSH_ALWAYS;
  212. }
  213. if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
  214. !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
  215. page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
  216. if (!page)
  217. return -ENOMEM;
  218. vmx_l1d_flush_pages = page_address(page);
  219. /*
  220. * Initialize each page with a different pattern in
  221. * order to protect against KSM in the nested
  222. * virtualization case.
  223. */
  224. for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) {
  225. memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1,
  226. PAGE_SIZE);
  227. }
  228. }
  229. l1tf_vmx_mitigation = l1tf;
  230. if (l1tf != VMENTER_L1D_FLUSH_NEVER)
  231. static_branch_enable(&vmx_l1d_should_flush);
  232. else
  233. static_branch_disable(&vmx_l1d_should_flush);
  234. if (l1tf == VMENTER_L1D_FLUSH_COND)
  235. static_branch_enable(&vmx_l1d_flush_cond);
  236. else
  237. static_branch_disable(&vmx_l1d_flush_cond);
  238. return 0;
  239. }
  240. static int vmentry_l1d_flush_parse(const char *s)
  241. {
  242. unsigned int i;
  243. if (s) {
  244. for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
  245. if (vmentry_l1d_param[i].for_parse &&
  246. sysfs_streq(s, vmentry_l1d_param[i].option))
  247. return i;
  248. }
  249. }
  250. return -EINVAL;
  251. }
  252. static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
  253. {
  254. int l1tf, ret;
  255. l1tf = vmentry_l1d_flush_parse(s);
  256. if (l1tf < 0)
  257. return l1tf;
  258. if (!boot_cpu_has(X86_BUG_L1TF))
  259. return 0;
  260. /*
  261. * Has vmx_init() run already? If not then this is the pre init
  262. * parameter parsing. In that case just store the value and let
  263. * vmx_init() do the proper setup after enable_ept has been
  264. * established.
  265. */
  266. if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
  267. vmentry_l1d_flush_param = l1tf;
  268. return 0;
  269. }
  270. mutex_lock(&vmx_l1d_flush_mutex);
  271. ret = vmx_setup_l1d_flush(l1tf);
  272. mutex_unlock(&vmx_l1d_flush_mutex);
  273. return ret;
  274. }
  275. static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
  276. {
  277. if (WARN_ON_ONCE(l1tf_vmx_mitigation >= ARRAY_SIZE(vmentry_l1d_param)))
  278. return sprintf(s, "???\n");
  279. return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
  280. }
  281. static const struct kernel_param_ops vmentry_l1d_flush_ops = {
  282. .set = vmentry_l1d_flush_set,
  283. .get = vmentry_l1d_flush_get,
  284. };
  285. module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644);
  286. enum ept_pointers_status {
  287. EPT_POINTERS_CHECK = 0,
  288. EPT_POINTERS_MATCH = 1,
  289. EPT_POINTERS_MISMATCH = 2
  290. };
  291. struct kvm_vmx {
  292. struct kvm kvm;
  293. unsigned int tss_addr;
  294. bool ept_identity_pagetable_done;
  295. gpa_t ept_identity_map_addr;
  296. enum ept_pointers_status ept_pointers_match;
  297. spinlock_t ept_pointer_lock;
  298. };
  299. #define NR_AUTOLOAD_MSRS 8
  300. struct vmcs_hdr {
  301. u32 revision_id:31;
  302. u32 shadow_vmcs:1;
  303. };
  304. struct vmcs {
  305. struct vmcs_hdr hdr;
  306. u32 abort;
  307. char data[0];
  308. };
  309. /*
  310. * vmcs_host_state tracks registers that are loaded from the VMCS on VMEXIT
  311. * and whose values change infrequently, but are not constant. I.e. this is
  312. * used as a write-through cache of the corresponding VMCS fields.
  313. */
  314. struct vmcs_host_state {
  315. unsigned long cr3; /* May not match real cr3 */
  316. unsigned long cr4; /* May not match real cr4 */
  317. unsigned long gs_base;
  318. unsigned long fs_base;
  319. u16 fs_sel, gs_sel, ldt_sel;
  320. #ifdef CONFIG_X86_64
  321. u16 ds_sel, es_sel;
  322. #endif
  323. };
  324. /*
  325. * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
  326. * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
  327. * loaded on this CPU (so we can clear them if the CPU goes down).
  328. */
  329. struct loaded_vmcs {
  330. struct vmcs *vmcs;
  331. struct vmcs *shadow_vmcs;
  332. int cpu;
  333. bool launched;
  334. bool nmi_known_unmasked;
  335. bool hv_timer_armed;
  336. /* Support for vnmi-less CPUs */
  337. int soft_vnmi_blocked;
  338. ktime_t entry_time;
  339. s64 vnmi_blocked_time;
  340. unsigned long *msr_bitmap;
  341. struct list_head loaded_vmcss_on_cpu_link;
  342. struct vmcs_host_state host_state;
  343. };
  344. struct shared_msr_entry {
  345. unsigned index;
  346. u64 data;
  347. u64 mask;
  348. };
  349. /*
  350. * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
  351. * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
  352. * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
  353. * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
  354. * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
  355. * More than one of these structures may exist, if L1 runs multiple L2 guests.
  356. * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
  357. * underlying hardware which will be used to run L2.
  358. * This structure is packed to ensure that its layout is identical across
  359. * machines (necessary for live migration).
  360. *
  361. * IMPORTANT: Changing the layout of existing fields in this structure
  362. * will break save/restore compatibility with older kvm releases. When
  363. * adding new fields, either use space in the reserved padding* arrays
  364. * or add the new fields to the end of the structure.
  365. */
  366. typedef u64 natural_width;
  367. struct __packed vmcs12 {
  368. /* According to the Intel spec, a VMCS region must start with the
  369. * following two fields. Then follow implementation-specific data.
  370. */
  371. struct vmcs_hdr hdr;
  372. u32 abort;
  373. u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
  374. u32 padding[7]; /* room for future expansion */
  375. u64 io_bitmap_a;
  376. u64 io_bitmap_b;
  377. u64 msr_bitmap;
  378. u64 vm_exit_msr_store_addr;
  379. u64 vm_exit_msr_load_addr;
  380. u64 vm_entry_msr_load_addr;
  381. u64 tsc_offset;
  382. u64 virtual_apic_page_addr;
  383. u64 apic_access_addr;
  384. u64 posted_intr_desc_addr;
  385. u64 ept_pointer;
  386. u64 eoi_exit_bitmap0;
  387. u64 eoi_exit_bitmap1;
  388. u64 eoi_exit_bitmap2;
  389. u64 eoi_exit_bitmap3;
  390. u64 xss_exit_bitmap;
  391. u64 guest_physical_address;
  392. u64 vmcs_link_pointer;
  393. u64 guest_ia32_debugctl;
  394. u64 guest_ia32_pat;
  395. u64 guest_ia32_efer;
  396. u64 guest_ia32_perf_global_ctrl;
  397. u64 guest_pdptr0;
  398. u64 guest_pdptr1;
  399. u64 guest_pdptr2;
  400. u64 guest_pdptr3;
  401. u64 guest_bndcfgs;
  402. u64 host_ia32_pat;
  403. u64 host_ia32_efer;
  404. u64 host_ia32_perf_global_ctrl;
  405. u64 vmread_bitmap;
  406. u64 vmwrite_bitmap;
  407. u64 vm_function_control;
  408. u64 eptp_list_address;
  409. u64 pml_address;
  410. u64 padding64[3]; /* room for future expansion */
  411. /*
  412. * To allow migration of L1 (complete with its L2 guests) between
  413. * machines of different natural widths (32 or 64 bit), we cannot have
  414. * unsigned long fields with no explict size. We use u64 (aliased
  415. * natural_width) instead. Luckily, x86 is little-endian.
  416. */
  417. natural_width cr0_guest_host_mask;
  418. natural_width cr4_guest_host_mask;
  419. natural_width cr0_read_shadow;
  420. natural_width cr4_read_shadow;
  421. natural_width cr3_target_value0;
  422. natural_width cr3_target_value1;
  423. natural_width cr3_target_value2;
  424. natural_width cr3_target_value3;
  425. natural_width exit_qualification;
  426. natural_width guest_linear_address;
  427. natural_width guest_cr0;
  428. natural_width guest_cr3;
  429. natural_width guest_cr4;
  430. natural_width guest_es_base;
  431. natural_width guest_cs_base;
  432. natural_width guest_ss_base;
  433. natural_width guest_ds_base;
  434. natural_width guest_fs_base;
  435. natural_width guest_gs_base;
  436. natural_width guest_ldtr_base;
  437. natural_width guest_tr_base;
  438. natural_width guest_gdtr_base;
  439. natural_width guest_idtr_base;
  440. natural_width guest_dr7;
  441. natural_width guest_rsp;
  442. natural_width guest_rip;
  443. natural_width guest_rflags;
  444. natural_width guest_pending_dbg_exceptions;
  445. natural_width guest_sysenter_esp;
  446. natural_width guest_sysenter_eip;
  447. natural_width host_cr0;
  448. natural_width host_cr3;
  449. natural_width host_cr4;
  450. natural_width host_fs_base;
  451. natural_width host_gs_base;
  452. natural_width host_tr_base;
  453. natural_width host_gdtr_base;
  454. natural_width host_idtr_base;
  455. natural_width host_ia32_sysenter_esp;
  456. natural_width host_ia32_sysenter_eip;
  457. natural_width host_rsp;
  458. natural_width host_rip;
  459. natural_width paddingl[8]; /* room for future expansion */
  460. u32 pin_based_vm_exec_control;
  461. u32 cpu_based_vm_exec_control;
  462. u32 exception_bitmap;
  463. u32 page_fault_error_code_mask;
  464. u32 page_fault_error_code_match;
  465. u32 cr3_target_count;
  466. u32 vm_exit_controls;
  467. u32 vm_exit_msr_store_count;
  468. u32 vm_exit_msr_load_count;
  469. u32 vm_entry_controls;
  470. u32 vm_entry_msr_load_count;
  471. u32 vm_entry_intr_info_field;
  472. u32 vm_entry_exception_error_code;
  473. u32 vm_entry_instruction_len;
  474. u32 tpr_threshold;
  475. u32 secondary_vm_exec_control;
  476. u32 vm_instruction_error;
  477. u32 vm_exit_reason;
  478. u32 vm_exit_intr_info;
  479. u32 vm_exit_intr_error_code;
  480. u32 idt_vectoring_info_field;
  481. u32 idt_vectoring_error_code;
  482. u32 vm_exit_instruction_len;
  483. u32 vmx_instruction_info;
  484. u32 guest_es_limit;
  485. u32 guest_cs_limit;
  486. u32 guest_ss_limit;
  487. u32 guest_ds_limit;
  488. u32 guest_fs_limit;
  489. u32 guest_gs_limit;
  490. u32 guest_ldtr_limit;
  491. u32 guest_tr_limit;
  492. u32 guest_gdtr_limit;
  493. u32 guest_idtr_limit;
  494. u32 guest_es_ar_bytes;
  495. u32 guest_cs_ar_bytes;
  496. u32 guest_ss_ar_bytes;
  497. u32 guest_ds_ar_bytes;
  498. u32 guest_fs_ar_bytes;
  499. u32 guest_gs_ar_bytes;
  500. u32 guest_ldtr_ar_bytes;
  501. u32 guest_tr_ar_bytes;
  502. u32 guest_interruptibility_info;
  503. u32 guest_activity_state;
  504. u32 guest_sysenter_cs;
  505. u32 host_ia32_sysenter_cs;
  506. u32 vmx_preemption_timer_value;
  507. u32 padding32[7]; /* room for future expansion */
  508. u16 virtual_processor_id;
  509. u16 posted_intr_nv;
  510. u16 guest_es_selector;
  511. u16 guest_cs_selector;
  512. u16 guest_ss_selector;
  513. u16 guest_ds_selector;
  514. u16 guest_fs_selector;
  515. u16 guest_gs_selector;
  516. u16 guest_ldtr_selector;
  517. u16 guest_tr_selector;
  518. u16 guest_intr_status;
  519. u16 host_es_selector;
  520. u16 host_cs_selector;
  521. u16 host_ss_selector;
  522. u16 host_ds_selector;
  523. u16 host_fs_selector;
  524. u16 host_gs_selector;
  525. u16 host_tr_selector;
  526. u16 guest_pml_index;
  527. };
  528. /*
  529. * For save/restore compatibility, the vmcs12 field offsets must not change.
  530. */
  531. #define CHECK_OFFSET(field, loc) \
  532. BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \
  533. "Offset of " #field " in struct vmcs12 has changed.")
  534. static inline void vmx_check_vmcs12_offsets(void) {
  535. CHECK_OFFSET(hdr, 0);
  536. CHECK_OFFSET(abort, 4);
  537. CHECK_OFFSET(launch_state, 8);
  538. CHECK_OFFSET(io_bitmap_a, 40);
  539. CHECK_OFFSET(io_bitmap_b, 48);
  540. CHECK_OFFSET(msr_bitmap, 56);
  541. CHECK_OFFSET(vm_exit_msr_store_addr, 64);
  542. CHECK_OFFSET(vm_exit_msr_load_addr, 72);
  543. CHECK_OFFSET(vm_entry_msr_load_addr, 80);
  544. CHECK_OFFSET(tsc_offset, 88);
  545. CHECK_OFFSET(virtual_apic_page_addr, 96);
  546. CHECK_OFFSET(apic_access_addr, 104);
  547. CHECK_OFFSET(posted_intr_desc_addr, 112);
  548. CHECK_OFFSET(ept_pointer, 120);
  549. CHECK_OFFSET(eoi_exit_bitmap0, 128);
  550. CHECK_OFFSET(eoi_exit_bitmap1, 136);
  551. CHECK_OFFSET(eoi_exit_bitmap2, 144);
  552. CHECK_OFFSET(eoi_exit_bitmap3, 152);
  553. CHECK_OFFSET(xss_exit_bitmap, 160);
  554. CHECK_OFFSET(guest_physical_address, 168);
  555. CHECK_OFFSET(vmcs_link_pointer, 176);
  556. CHECK_OFFSET(guest_ia32_debugctl, 184);
  557. CHECK_OFFSET(guest_ia32_pat, 192);
  558. CHECK_OFFSET(guest_ia32_efer, 200);
  559. CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208);
  560. CHECK_OFFSET(guest_pdptr0, 216);
  561. CHECK_OFFSET(guest_pdptr1, 224);
  562. CHECK_OFFSET(guest_pdptr2, 232);
  563. CHECK_OFFSET(guest_pdptr3, 240);
  564. CHECK_OFFSET(guest_bndcfgs, 248);
  565. CHECK_OFFSET(host_ia32_pat, 256);
  566. CHECK_OFFSET(host_ia32_efer, 264);
  567. CHECK_OFFSET(host_ia32_perf_global_ctrl, 272);
  568. CHECK_OFFSET(vmread_bitmap, 280);
  569. CHECK_OFFSET(vmwrite_bitmap, 288);
  570. CHECK_OFFSET(vm_function_control, 296);
  571. CHECK_OFFSET(eptp_list_address, 304);
  572. CHECK_OFFSET(pml_address, 312);
  573. CHECK_OFFSET(cr0_guest_host_mask, 344);
  574. CHECK_OFFSET(cr4_guest_host_mask, 352);
  575. CHECK_OFFSET(cr0_read_shadow, 360);
  576. CHECK_OFFSET(cr4_read_shadow, 368);
  577. CHECK_OFFSET(cr3_target_value0, 376);
  578. CHECK_OFFSET(cr3_target_value1, 384);
  579. CHECK_OFFSET(cr3_target_value2, 392);
  580. CHECK_OFFSET(cr3_target_value3, 400);
  581. CHECK_OFFSET(exit_qualification, 408);
  582. CHECK_OFFSET(guest_linear_address, 416);
  583. CHECK_OFFSET(guest_cr0, 424);
  584. CHECK_OFFSET(guest_cr3, 432);
  585. CHECK_OFFSET(guest_cr4, 440);
  586. CHECK_OFFSET(guest_es_base, 448);
  587. CHECK_OFFSET(guest_cs_base, 456);
  588. CHECK_OFFSET(guest_ss_base, 464);
  589. CHECK_OFFSET(guest_ds_base, 472);
  590. CHECK_OFFSET(guest_fs_base, 480);
  591. CHECK_OFFSET(guest_gs_base, 488);
  592. CHECK_OFFSET(guest_ldtr_base, 496);
  593. CHECK_OFFSET(guest_tr_base, 504);
  594. CHECK_OFFSET(guest_gdtr_base, 512);
  595. CHECK_OFFSET(guest_idtr_base, 520);
  596. CHECK_OFFSET(guest_dr7, 528);
  597. CHECK_OFFSET(guest_rsp, 536);
  598. CHECK_OFFSET(guest_rip, 544);
  599. CHECK_OFFSET(guest_rflags, 552);
  600. CHECK_OFFSET(guest_pending_dbg_exceptions, 560);
  601. CHECK_OFFSET(guest_sysenter_esp, 568);
  602. CHECK_OFFSET(guest_sysenter_eip, 576);
  603. CHECK_OFFSET(host_cr0, 584);
  604. CHECK_OFFSET(host_cr3, 592);
  605. CHECK_OFFSET(host_cr4, 600);
  606. CHECK_OFFSET(host_fs_base, 608);
  607. CHECK_OFFSET(host_gs_base, 616);
  608. CHECK_OFFSET(host_tr_base, 624);
  609. CHECK_OFFSET(host_gdtr_base, 632);
  610. CHECK_OFFSET(host_idtr_base, 640);
  611. CHECK_OFFSET(host_ia32_sysenter_esp, 648);
  612. CHECK_OFFSET(host_ia32_sysenter_eip, 656);
  613. CHECK_OFFSET(host_rsp, 664);
  614. CHECK_OFFSET(host_rip, 672);
  615. CHECK_OFFSET(pin_based_vm_exec_control, 744);
  616. CHECK_OFFSET(cpu_based_vm_exec_control, 748);
  617. CHECK_OFFSET(exception_bitmap, 752);
  618. CHECK_OFFSET(page_fault_error_code_mask, 756);
  619. CHECK_OFFSET(page_fault_error_code_match, 760);
  620. CHECK_OFFSET(cr3_target_count, 764);
  621. CHECK_OFFSET(vm_exit_controls, 768);
  622. CHECK_OFFSET(vm_exit_msr_store_count, 772);
  623. CHECK_OFFSET(vm_exit_msr_load_count, 776);
  624. CHECK_OFFSET(vm_entry_controls, 780);
  625. CHECK_OFFSET(vm_entry_msr_load_count, 784);
  626. CHECK_OFFSET(vm_entry_intr_info_field, 788);
  627. CHECK_OFFSET(vm_entry_exception_error_code, 792);
  628. CHECK_OFFSET(vm_entry_instruction_len, 796);
  629. CHECK_OFFSET(tpr_threshold, 800);
  630. CHECK_OFFSET(secondary_vm_exec_control, 804);
  631. CHECK_OFFSET(vm_instruction_error, 808);
  632. CHECK_OFFSET(vm_exit_reason, 812);
  633. CHECK_OFFSET(vm_exit_intr_info, 816);
  634. CHECK_OFFSET(vm_exit_intr_error_code, 820);
  635. CHECK_OFFSET(idt_vectoring_info_field, 824);
  636. CHECK_OFFSET(idt_vectoring_error_code, 828);
  637. CHECK_OFFSET(vm_exit_instruction_len, 832);
  638. CHECK_OFFSET(vmx_instruction_info, 836);
  639. CHECK_OFFSET(guest_es_limit, 840);
  640. CHECK_OFFSET(guest_cs_limit, 844);
  641. CHECK_OFFSET(guest_ss_limit, 848);
  642. CHECK_OFFSET(guest_ds_limit, 852);
  643. CHECK_OFFSET(guest_fs_limit, 856);
  644. CHECK_OFFSET(guest_gs_limit, 860);
  645. CHECK_OFFSET(guest_ldtr_limit, 864);
  646. CHECK_OFFSET(guest_tr_limit, 868);
  647. CHECK_OFFSET(guest_gdtr_limit, 872);
  648. CHECK_OFFSET(guest_idtr_limit, 876);
  649. CHECK_OFFSET(guest_es_ar_bytes, 880);
  650. CHECK_OFFSET(guest_cs_ar_bytes, 884);
  651. CHECK_OFFSET(guest_ss_ar_bytes, 888);
  652. CHECK_OFFSET(guest_ds_ar_bytes, 892);
  653. CHECK_OFFSET(guest_fs_ar_bytes, 896);
  654. CHECK_OFFSET(guest_gs_ar_bytes, 900);
  655. CHECK_OFFSET(guest_ldtr_ar_bytes, 904);
  656. CHECK_OFFSET(guest_tr_ar_bytes, 908);
  657. CHECK_OFFSET(guest_interruptibility_info, 912);
  658. CHECK_OFFSET(guest_activity_state, 916);
  659. CHECK_OFFSET(guest_sysenter_cs, 920);
  660. CHECK_OFFSET(host_ia32_sysenter_cs, 924);
  661. CHECK_OFFSET(vmx_preemption_timer_value, 928);
  662. CHECK_OFFSET(virtual_processor_id, 960);
  663. CHECK_OFFSET(posted_intr_nv, 962);
  664. CHECK_OFFSET(guest_es_selector, 964);
  665. CHECK_OFFSET(guest_cs_selector, 966);
  666. CHECK_OFFSET(guest_ss_selector, 968);
  667. CHECK_OFFSET(guest_ds_selector, 970);
  668. CHECK_OFFSET(guest_fs_selector, 972);
  669. CHECK_OFFSET(guest_gs_selector, 974);
  670. CHECK_OFFSET(guest_ldtr_selector, 976);
  671. CHECK_OFFSET(guest_tr_selector, 978);
  672. CHECK_OFFSET(guest_intr_status, 980);
  673. CHECK_OFFSET(host_es_selector, 982);
  674. CHECK_OFFSET(host_cs_selector, 984);
  675. CHECK_OFFSET(host_ss_selector, 986);
  676. CHECK_OFFSET(host_ds_selector, 988);
  677. CHECK_OFFSET(host_fs_selector, 990);
  678. CHECK_OFFSET(host_gs_selector, 992);
  679. CHECK_OFFSET(host_tr_selector, 994);
  680. CHECK_OFFSET(guest_pml_index, 996);
  681. }
  682. /*
  683. * VMCS12_REVISION is an arbitrary id that should be changed if the content or
  684. * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
  685. * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
  686. *
  687. * IMPORTANT: Changing this value will break save/restore compatibility with
  688. * older kvm releases.
  689. */
  690. #define VMCS12_REVISION 0x11e57ed0
  691. /*
  692. * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
  693. * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
  694. * current implementation, 4K are reserved to avoid future complications.
  695. */
  696. #define VMCS12_SIZE 0x1000
  697. /*
  698. * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
  699. * supported VMCS12 field encoding.
  700. */
  701. #define VMCS12_MAX_FIELD_INDEX 0x17
  702. struct nested_vmx_msrs {
  703. /*
  704. * We only store the "true" versions of the VMX capability MSRs. We
  705. * generate the "non-true" versions by setting the must-be-1 bits
  706. * according to the SDM.
  707. */
  708. u32 procbased_ctls_low;
  709. u32 procbased_ctls_high;
  710. u32 secondary_ctls_low;
  711. u32 secondary_ctls_high;
  712. u32 pinbased_ctls_low;
  713. u32 pinbased_ctls_high;
  714. u32 exit_ctls_low;
  715. u32 exit_ctls_high;
  716. u32 entry_ctls_low;
  717. u32 entry_ctls_high;
  718. u32 misc_low;
  719. u32 misc_high;
  720. u32 ept_caps;
  721. u32 vpid_caps;
  722. u64 basic;
  723. u64 cr0_fixed0;
  724. u64 cr0_fixed1;
  725. u64 cr4_fixed0;
  726. u64 cr4_fixed1;
  727. u64 vmcs_enum;
  728. u64 vmfunc_controls;
  729. };
  730. /*
  731. * The nested_vmx structure is part of vcpu_vmx, and holds information we need
  732. * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
  733. */
  734. struct nested_vmx {
  735. /* Has the level1 guest done vmxon? */
  736. bool vmxon;
  737. gpa_t vmxon_ptr;
  738. bool pml_full;
  739. /* The guest-physical address of the current VMCS L1 keeps for L2 */
  740. gpa_t current_vmptr;
  741. /*
  742. * Cache of the guest's VMCS, existing outside of guest memory.
  743. * Loaded from guest memory during VMPTRLD. Flushed to guest
  744. * memory during VMCLEAR and VMPTRLD.
  745. */
  746. struct vmcs12 *cached_vmcs12;
  747. /*
  748. * Cache of the guest's shadow VMCS, existing outside of guest
  749. * memory. Loaded from guest memory during VM entry. Flushed
  750. * to guest memory during VM exit.
  751. */
  752. struct vmcs12 *cached_shadow_vmcs12;
  753. /*
  754. * Indicates if the shadow vmcs or enlightened vmcs must be updated
  755. * with the data held by struct vmcs12.
  756. */
  757. bool need_vmcs12_sync;
  758. bool dirty_vmcs12;
  759. /*
  760. * vmcs02 has been initialized, i.e. state that is constant for
  761. * vmcs02 has been written to the backing VMCS. Initialization
  762. * is delayed until L1 actually attempts to run a nested VM.
  763. */
  764. bool vmcs02_initialized;
  765. bool change_vmcs01_virtual_apic_mode;
  766. /*
  767. * Enlightened VMCS has been enabled. It does not mean that L1 has to
  768. * use it. However, VMX features available to L1 will be limited based
  769. * on what the enlightened VMCS supports.
  770. */
  771. bool enlightened_vmcs_enabled;
  772. /* L2 must run next, and mustn't decide to exit to L1. */
  773. bool nested_run_pending;
  774. struct loaded_vmcs vmcs02;
  775. /*
  776. * Guest pages referred to in the vmcs02 with host-physical
  777. * pointers, so we must keep them pinned while L2 runs.
  778. */
  779. struct page *apic_access_page;
  780. struct page *virtual_apic_page;
  781. struct page *pi_desc_page;
  782. struct pi_desc *pi_desc;
  783. bool pi_pending;
  784. u16 posted_intr_nv;
  785. struct hrtimer preemption_timer;
  786. bool preemption_timer_expired;
  787. /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
  788. u64 vmcs01_debugctl;
  789. u64 vmcs01_guest_bndcfgs;
  790. u16 vpid02;
  791. u16 last_vpid;
  792. struct nested_vmx_msrs msrs;
  793. /* SMM related state */
  794. struct {
  795. /* in VMX operation on SMM entry? */
  796. bool vmxon;
  797. /* in guest mode on SMM entry? */
  798. bool guest_mode;
  799. } smm;
  800. gpa_t hv_evmcs_vmptr;
  801. struct page *hv_evmcs_page;
  802. struct hv_enlightened_vmcs *hv_evmcs;
  803. };
  804. #define POSTED_INTR_ON 0
  805. #define POSTED_INTR_SN 1
  806. /* Posted-Interrupt Descriptor */
  807. struct pi_desc {
  808. u32 pir[8]; /* Posted interrupt requested */
  809. union {
  810. struct {
  811. /* bit 256 - Outstanding Notification */
  812. u16 on : 1,
  813. /* bit 257 - Suppress Notification */
  814. sn : 1,
  815. /* bit 271:258 - Reserved */
  816. rsvd_1 : 14;
  817. /* bit 279:272 - Notification Vector */
  818. u8 nv;
  819. /* bit 287:280 - Reserved */
  820. u8 rsvd_2;
  821. /* bit 319:288 - Notification Destination */
  822. u32 ndst;
  823. };
  824. u64 control;
  825. };
  826. u32 rsvd[6];
  827. } __aligned(64);
  828. static bool pi_test_and_set_on(struct pi_desc *pi_desc)
  829. {
  830. return test_and_set_bit(POSTED_INTR_ON,
  831. (unsigned long *)&pi_desc->control);
  832. }
  833. static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
  834. {
  835. return test_and_clear_bit(POSTED_INTR_ON,
  836. (unsigned long *)&pi_desc->control);
  837. }
  838. static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
  839. {
  840. return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
  841. }
  842. static inline void pi_clear_sn(struct pi_desc *pi_desc)
  843. {
  844. return clear_bit(POSTED_INTR_SN,
  845. (unsigned long *)&pi_desc->control);
  846. }
  847. static inline void pi_set_sn(struct pi_desc *pi_desc)
  848. {
  849. return set_bit(POSTED_INTR_SN,
  850. (unsigned long *)&pi_desc->control);
  851. }
  852. static inline void pi_clear_on(struct pi_desc *pi_desc)
  853. {
  854. clear_bit(POSTED_INTR_ON,
  855. (unsigned long *)&pi_desc->control);
  856. }
  857. static inline int pi_test_on(struct pi_desc *pi_desc)
  858. {
  859. return test_bit(POSTED_INTR_ON,
  860. (unsigned long *)&pi_desc->control);
  861. }
  862. static inline int pi_test_sn(struct pi_desc *pi_desc)
  863. {
  864. return test_bit(POSTED_INTR_SN,
  865. (unsigned long *)&pi_desc->control);
  866. }
  867. struct vmx_msrs {
  868. unsigned int nr;
  869. struct vmx_msr_entry val[NR_AUTOLOAD_MSRS];
  870. };
  871. struct vcpu_vmx {
  872. struct kvm_vcpu vcpu;
  873. unsigned long host_rsp;
  874. u8 fail;
  875. u8 msr_bitmap_mode;
  876. u32 exit_intr_info;
  877. u32 idt_vectoring_info;
  878. ulong rflags;
  879. struct shared_msr_entry *guest_msrs;
  880. int nmsrs;
  881. int save_nmsrs;
  882. unsigned long host_idt_base;
  883. #ifdef CONFIG_X86_64
  884. u64 msr_host_kernel_gs_base;
  885. u64 msr_guest_kernel_gs_base;
  886. #endif
  887. u64 arch_capabilities;
  888. u64 spec_ctrl;
  889. u32 vm_entry_controls_shadow;
  890. u32 vm_exit_controls_shadow;
  891. u32 secondary_exec_control;
  892. /*
  893. * loaded_vmcs points to the VMCS currently used in this vcpu. For a
  894. * non-nested (L1) guest, it always points to vmcs01. For a nested
  895. * guest (L2), it points to a different VMCS. loaded_cpu_state points
  896. * to the VMCS whose state is loaded into the CPU registers that only
  897. * need to be switched when transitioning to/from the kernel; a NULL
  898. * value indicates that host state is loaded.
  899. */
  900. struct loaded_vmcs vmcs01;
  901. struct loaded_vmcs *loaded_vmcs;
  902. struct loaded_vmcs *loaded_cpu_state;
  903. bool __launched; /* temporary, used in vmx_vcpu_run */
  904. struct msr_autoload {
  905. struct vmx_msrs guest;
  906. struct vmx_msrs host;
  907. } msr_autoload;
  908. struct {
  909. int vm86_active;
  910. ulong save_rflags;
  911. struct kvm_segment segs[8];
  912. } rmode;
  913. struct {
  914. u32 bitmask; /* 4 bits per segment (1 bit per field) */
  915. struct kvm_save_segment {
  916. u16 selector;
  917. unsigned long base;
  918. u32 limit;
  919. u32 ar;
  920. } seg[8];
  921. } segment_cache;
  922. int vpid;
  923. bool emulation_required;
  924. u32 exit_reason;
  925. /* Posted interrupt descriptor */
  926. struct pi_desc pi_desc;
  927. /* Support for a guest hypervisor (nested VMX) */
  928. struct nested_vmx nested;
  929. /* Dynamic PLE window. */
  930. int ple_window;
  931. bool ple_window_dirty;
  932. bool req_immediate_exit;
  933. /* Support for PML */
  934. #define PML_ENTITY_NUM 512
  935. struct page *pml_pg;
  936. /* apic deadline value in host tsc */
  937. u64 hv_deadline_tsc;
  938. u64 current_tsc_ratio;
  939. u32 host_pkru;
  940. unsigned long host_debugctlmsr;
  941. /*
  942. * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
  943. * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
  944. * in msr_ia32_feature_control_valid_bits.
  945. */
  946. u64 msr_ia32_feature_control;
  947. u64 msr_ia32_feature_control_valid_bits;
  948. u64 ept_pointer;
  949. };
  950. enum segment_cache_field {
  951. SEG_FIELD_SEL = 0,
  952. SEG_FIELD_BASE = 1,
  953. SEG_FIELD_LIMIT = 2,
  954. SEG_FIELD_AR = 3,
  955. SEG_FIELD_NR = 4
  956. };
  957. static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
  958. {
  959. return container_of(kvm, struct kvm_vmx, kvm);
  960. }
  961. static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
  962. {
  963. return container_of(vcpu, struct vcpu_vmx, vcpu);
  964. }
  965. static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
  966. {
  967. return &(to_vmx(vcpu)->pi_desc);
  968. }
  969. #define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
  970. #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
  971. #define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
  972. #define FIELD64(number, name) \
  973. FIELD(number, name), \
  974. [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
  975. static u16 shadow_read_only_fields[] = {
  976. #define SHADOW_FIELD_RO(x) x,
  977. #include "vmx_shadow_fields.h"
  978. };
  979. static int max_shadow_read_only_fields =
  980. ARRAY_SIZE(shadow_read_only_fields);
  981. static u16 shadow_read_write_fields[] = {
  982. #define SHADOW_FIELD_RW(x) x,
  983. #include "vmx_shadow_fields.h"
  984. };
  985. static int max_shadow_read_write_fields =
  986. ARRAY_SIZE(shadow_read_write_fields);
  987. static const unsigned short vmcs_field_to_offset_table[] = {
  988. FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
  989. FIELD(POSTED_INTR_NV, posted_intr_nv),
  990. FIELD(GUEST_ES_SELECTOR, guest_es_selector),
  991. FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
  992. FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
  993. FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
  994. FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
  995. FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
  996. FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
  997. FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
  998. FIELD(GUEST_INTR_STATUS, guest_intr_status),
  999. FIELD(GUEST_PML_INDEX, guest_pml_index),
  1000. FIELD(HOST_ES_SELECTOR, host_es_selector),
  1001. FIELD(HOST_CS_SELECTOR, host_cs_selector),
  1002. FIELD(HOST_SS_SELECTOR, host_ss_selector),
  1003. FIELD(HOST_DS_SELECTOR, host_ds_selector),
  1004. FIELD(HOST_FS_SELECTOR, host_fs_selector),
  1005. FIELD(HOST_GS_SELECTOR, host_gs_selector),
  1006. FIELD(HOST_TR_SELECTOR, host_tr_selector),
  1007. FIELD64(IO_BITMAP_A, io_bitmap_a),
  1008. FIELD64(IO_BITMAP_B, io_bitmap_b),
  1009. FIELD64(MSR_BITMAP, msr_bitmap),
  1010. FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
  1011. FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
  1012. FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
  1013. FIELD64(PML_ADDRESS, pml_address),
  1014. FIELD64(TSC_OFFSET, tsc_offset),
  1015. FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
  1016. FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
  1017. FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
  1018. FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
  1019. FIELD64(EPT_POINTER, ept_pointer),
  1020. FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
  1021. FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
  1022. FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
  1023. FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
  1024. FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
  1025. FIELD64(VMREAD_BITMAP, vmread_bitmap),
  1026. FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
  1027. FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
  1028. FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
  1029. FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
  1030. FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
  1031. FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
  1032. FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
  1033. FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
  1034. FIELD64(GUEST_PDPTR0, guest_pdptr0),
  1035. FIELD64(GUEST_PDPTR1, guest_pdptr1),
  1036. FIELD64(GUEST_PDPTR2, guest_pdptr2),
  1037. FIELD64(GUEST_PDPTR3, guest_pdptr3),
  1038. FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
  1039. FIELD64(HOST_IA32_PAT, host_ia32_pat),
  1040. FIELD64(HOST_IA32_EFER, host_ia32_efer),
  1041. FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
  1042. FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
  1043. FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
  1044. FIELD(EXCEPTION_BITMAP, exception_bitmap),
  1045. FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
  1046. FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
  1047. FIELD(CR3_TARGET_COUNT, cr3_target_count),
  1048. FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
  1049. FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
  1050. FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
  1051. FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
  1052. FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
  1053. FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
  1054. FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
  1055. FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
  1056. FIELD(TPR_THRESHOLD, tpr_threshold),
  1057. FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
  1058. FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
  1059. FIELD(VM_EXIT_REASON, vm_exit_reason),
  1060. FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
  1061. FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
  1062. FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
  1063. FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
  1064. FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
  1065. FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
  1066. FIELD(GUEST_ES_LIMIT, guest_es_limit),
  1067. FIELD(GUEST_CS_LIMIT, guest_cs_limit),
  1068. FIELD(GUEST_SS_LIMIT, guest_ss_limit),
  1069. FIELD(GUEST_DS_LIMIT, guest_ds_limit),
  1070. FIELD(GUEST_FS_LIMIT, guest_fs_limit),
  1071. FIELD(GUEST_GS_LIMIT, guest_gs_limit),
  1072. FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
  1073. FIELD(GUEST_TR_LIMIT, guest_tr_limit),
  1074. FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
  1075. FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
  1076. FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
  1077. FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
  1078. FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
  1079. FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
  1080. FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
  1081. FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
  1082. FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
  1083. FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
  1084. FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
  1085. FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
  1086. FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
  1087. FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
  1088. FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
  1089. FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
  1090. FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
  1091. FIELD(CR0_READ_SHADOW, cr0_read_shadow),
  1092. FIELD(CR4_READ_SHADOW, cr4_read_shadow),
  1093. FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
  1094. FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
  1095. FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
  1096. FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
  1097. FIELD(EXIT_QUALIFICATION, exit_qualification),
  1098. FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
  1099. FIELD(GUEST_CR0, guest_cr0),
  1100. FIELD(GUEST_CR3, guest_cr3),
  1101. FIELD(GUEST_CR4, guest_cr4),
  1102. FIELD(GUEST_ES_BASE, guest_es_base),
  1103. FIELD(GUEST_CS_BASE, guest_cs_base),
  1104. FIELD(GUEST_SS_BASE, guest_ss_base),
  1105. FIELD(GUEST_DS_BASE, guest_ds_base),
  1106. FIELD(GUEST_FS_BASE, guest_fs_base),
  1107. FIELD(GUEST_GS_BASE, guest_gs_base),
  1108. FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
  1109. FIELD(GUEST_TR_BASE, guest_tr_base),
  1110. FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
  1111. FIELD(GUEST_IDTR_BASE, guest_idtr_base),
  1112. FIELD(GUEST_DR7, guest_dr7),
  1113. FIELD(GUEST_RSP, guest_rsp),
  1114. FIELD(GUEST_RIP, guest_rip),
  1115. FIELD(GUEST_RFLAGS, guest_rflags),
  1116. FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
  1117. FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
  1118. FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
  1119. FIELD(HOST_CR0, host_cr0),
  1120. FIELD(HOST_CR3, host_cr3),
  1121. FIELD(HOST_CR4, host_cr4),
  1122. FIELD(HOST_FS_BASE, host_fs_base),
  1123. FIELD(HOST_GS_BASE, host_gs_base),
  1124. FIELD(HOST_TR_BASE, host_tr_base),
  1125. FIELD(HOST_GDTR_BASE, host_gdtr_base),
  1126. FIELD(HOST_IDTR_BASE, host_idtr_base),
  1127. FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
  1128. FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
  1129. FIELD(HOST_RSP, host_rsp),
  1130. FIELD(HOST_RIP, host_rip),
  1131. };
  1132. static inline short vmcs_field_to_offset(unsigned long field)
  1133. {
  1134. const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
  1135. unsigned short offset;
  1136. unsigned index;
  1137. if (field >> 15)
  1138. return -ENOENT;
  1139. index = ROL16(field, 6);
  1140. if (index >= size)
  1141. return -ENOENT;
  1142. index = array_index_nospec(index, size);
  1143. offset = vmcs_field_to_offset_table[index];
  1144. if (offset == 0)
  1145. return -ENOENT;
  1146. return offset;
  1147. }
  1148. static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
  1149. {
  1150. return to_vmx(vcpu)->nested.cached_vmcs12;
  1151. }
  1152. static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu)
  1153. {
  1154. return to_vmx(vcpu)->nested.cached_shadow_vmcs12;
  1155. }
  1156. static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
  1157. static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
  1158. static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
  1159. static bool vmx_xsaves_supported(void);
  1160. static void vmx_set_segment(struct kvm_vcpu *vcpu,
  1161. struct kvm_segment *var, int seg);
  1162. static void vmx_get_segment(struct kvm_vcpu *vcpu,
  1163. struct kvm_segment *var, int seg);
  1164. static bool guest_state_valid(struct kvm_vcpu *vcpu);
  1165. static u32 vmx_segment_access_rights(struct kvm_segment *var);
  1166. static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
  1167. static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
  1168. static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
  1169. static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
  1170. u16 error_code);
  1171. static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
  1172. static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
  1173. u32 msr, int type);
  1174. static DEFINE_PER_CPU(struct vmcs *, vmxarea);
  1175. static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
  1176. /*
  1177. * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
  1178. * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
  1179. */
  1180. static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
  1181. /*
  1182. * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
  1183. * can find which vCPU should be waken up.
  1184. */
  1185. static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
  1186. static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
  1187. enum {
  1188. VMX_VMREAD_BITMAP,
  1189. VMX_VMWRITE_BITMAP,
  1190. VMX_BITMAP_NR
  1191. };
  1192. static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
  1193. #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
  1194. #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
  1195. static bool cpu_has_load_ia32_efer;
  1196. static bool cpu_has_load_perf_global_ctrl;
  1197. static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
  1198. static DEFINE_SPINLOCK(vmx_vpid_lock);
  1199. static struct vmcs_config {
  1200. int size;
  1201. int order;
  1202. u32 basic_cap;
  1203. u32 revision_id;
  1204. u32 pin_based_exec_ctrl;
  1205. u32 cpu_based_exec_ctrl;
  1206. u32 cpu_based_2nd_exec_ctrl;
  1207. u32 vmexit_ctrl;
  1208. u32 vmentry_ctrl;
  1209. struct nested_vmx_msrs nested;
  1210. } vmcs_config;
  1211. static struct vmx_capability {
  1212. u32 ept;
  1213. u32 vpid;
  1214. } vmx_capability;
  1215. #define VMX_SEGMENT_FIELD(seg) \
  1216. [VCPU_SREG_##seg] = { \
  1217. .selector = GUEST_##seg##_SELECTOR, \
  1218. .base = GUEST_##seg##_BASE, \
  1219. .limit = GUEST_##seg##_LIMIT, \
  1220. .ar_bytes = GUEST_##seg##_AR_BYTES, \
  1221. }
  1222. static const struct kvm_vmx_segment_field {
  1223. unsigned selector;
  1224. unsigned base;
  1225. unsigned limit;
  1226. unsigned ar_bytes;
  1227. } kvm_vmx_segment_fields[] = {
  1228. VMX_SEGMENT_FIELD(CS),
  1229. VMX_SEGMENT_FIELD(DS),
  1230. VMX_SEGMENT_FIELD(ES),
  1231. VMX_SEGMENT_FIELD(FS),
  1232. VMX_SEGMENT_FIELD(GS),
  1233. VMX_SEGMENT_FIELD(SS),
  1234. VMX_SEGMENT_FIELD(TR),
  1235. VMX_SEGMENT_FIELD(LDTR),
  1236. };
  1237. static u64 host_efer;
  1238. static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
  1239. /*
  1240. * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
  1241. * away by decrementing the array size.
  1242. */
  1243. static const u32 vmx_msr_index[] = {
  1244. #ifdef CONFIG_X86_64
  1245. MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
  1246. #endif
  1247. MSR_EFER, MSR_TSC_AUX, MSR_STAR,
  1248. };
  1249. DEFINE_STATIC_KEY_FALSE(enable_evmcs);
  1250. #define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
  1251. #define KVM_EVMCS_VERSION 1
  1252. /*
  1253. * Enlightened VMCSv1 doesn't support these:
  1254. *
  1255. * POSTED_INTR_NV = 0x00000002,
  1256. * GUEST_INTR_STATUS = 0x00000810,
  1257. * APIC_ACCESS_ADDR = 0x00002014,
  1258. * POSTED_INTR_DESC_ADDR = 0x00002016,
  1259. * EOI_EXIT_BITMAP0 = 0x0000201c,
  1260. * EOI_EXIT_BITMAP1 = 0x0000201e,
  1261. * EOI_EXIT_BITMAP2 = 0x00002020,
  1262. * EOI_EXIT_BITMAP3 = 0x00002022,
  1263. * GUEST_PML_INDEX = 0x00000812,
  1264. * PML_ADDRESS = 0x0000200e,
  1265. * VM_FUNCTION_CONTROL = 0x00002018,
  1266. * EPTP_LIST_ADDRESS = 0x00002024,
  1267. * VMREAD_BITMAP = 0x00002026,
  1268. * VMWRITE_BITMAP = 0x00002028,
  1269. *
  1270. * TSC_MULTIPLIER = 0x00002032,
  1271. * PLE_GAP = 0x00004020,
  1272. * PLE_WINDOW = 0x00004022,
  1273. * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
  1274. * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
  1275. * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
  1276. *
  1277. * Currently unsupported in KVM:
  1278. * GUEST_IA32_RTIT_CTL = 0x00002814,
  1279. */
  1280. #define EVMCS1_UNSUPPORTED_PINCTRL (PIN_BASED_POSTED_INTR | \
  1281. PIN_BASED_VMX_PREEMPTION_TIMER)
  1282. #define EVMCS1_UNSUPPORTED_2NDEXEC \
  1283. (SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | \
  1284. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | \
  1285. SECONDARY_EXEC_APIC_REGISTER_VIRT | \
  1286. SECONDARY_EXEC_ENABLE_PML | \
  1287. SECONDARY_EXEC_ENABLE_VMFUNC | \
  1288. SECONDARY_EXEC_SHADOW_VMCS | \
  1289. SECONDARY_EXEC_TSC_SCALING | \
  1290. SECONDARY_EXEC_PAUSE_LOOP_EXITING)
  1291. #define EVMCS1_UNSUPPORTED_VMEXIT_CTRL (VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
  1292. #define EVMCS1_UNSUPPORTED_VMENTRY_CTRL (VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
  1293. #define EVMCS1_UNSUPPORTED_VMFUNC (VMX_VMFUNC_EPTP_SWITCHING)
  1294. #if IS_ENABLED(CONFIG_HYPERV)
  1295. static bool __read_mostly enlightened_vmcs = true;
  1296. module_param(enlightened_vmcs, bool, 0444);
  1297. static inline void evmcs_write64(unsigned long field, u64 value)
  1298. {
  1299. u16 clean_field;
  1300. int offset = get_evmcs_offset(field, &clean_field);
  1301. if (offset < 0)
  1302. return;
  1303. *(u64 *)((char *)current_evmcs + offset) = value;
  1304. current_evmcs->hv_clean_fields &= ~clean_field;
  1305. }
  1306. static inline void evmcs_write32(unsigned long field, u32 value)
  1307. {
  1308. u16 clean_field;
  1309. int offset = get_evmcs_offset(field, &clean_field);
  1310. if (offset < 0)
  1311. return;
  1312. *(u32 *)((char *)current_evmcs + offset) = value;
  1313. current_evmcs->hv_clean_fields &= ~clean_field;
  1314. }
  1315. static inline void evmcs_write16(unsigned long field, u16 value)
  1316. {
  1317. u16 clean_field;
  1318. int offset = get_evmcs_offset(field, &clean_field);
  1319. if (offset < 0)
  1320. return;
  1321. *(u16 *)((char *)current_evmcs + offset) = value;
  1322. current_evmcs->hv_clean_fields &= ~clean_field;
  1323. }
  1324. static inline u64 evmcs_read64(unsigned long field)
  1325. {
  1326. int offset = get_evmcs_offset(field, NULL);
  1327. if (offset < 0)
  1328. return 0;
  1329. return *(u64 *)((char *)current_evmcs + offset);
  1330. }
  1331. static inline u32 evmcs_read32(unsigned long field)
  1332. {
  1333. int offset = get_evmcs_offset(field, NULL);
  1334. if (offset < 0)
  1335. return 0;
  1336. return *(u32 *)((char *)current_evmcs + offset);
  1337. }
  1338. static inline u16 evmcs_read16(unsigned long field)
  1339. {
  1340. int offset = get_evmcs_offset(field, NULL);
  1341. if (offset < 0)
  1342. return 0;
  1343. return *(u16 *)((char *)current_evmcs + offset);
  1344. }
  1345. static inline void evmcs_touch_msr_bitmap(void)
  1346. {
  1347. if (unlikely(!current_evmcs))
  1348. return;
  1349. if (current_evmcs->hv_enlightenments_control.msr_bitmap)
  1350. current_evmcs->hv_clean_fields &=
  1351. ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
  1352. }
  1353. static void evmcs_load(u64 phys_addr)
  1354. {
  1355. struct hv_vp_assist_page *vp_ap =
  1356. hv_get_vp_assist_page(smp_processor_id());
  1357. vp_ap->current_nested_vmcs = phys_addr;
  1358. vp_ap->enlighten_vmentry = 1;
  1359. }
  1360. static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
  1361. {
  1362. vmcs_conf->pin_based_exec_ctrl &= ~EVMCS1_UNSUPPORTED_PINCTRL;
  1363. vmcs_conf->cpu_based_2nd_exec_ctrl &= ~EVMCS1_UNSUPPORTED_2NDEXEC;
  1364. vmcs_conf->vmexit_ctrl &= ~EVMCS1_UNSUPPORTED_VMEXIT_CTRL;
  1365. vmcs_conf->vmentry_ctrl &= ~EVMCS1_UNSUPPORTED_VMENTRY_CTRL;
  1366. }
  1367. /* check_ept_pointer() should be under protection of ept_pointer_lock. */
  1368. static void check_ept_pointer_match(struct kvm *kvm)
  1369. {
  1370. struct kvm_vcpu *vcpu;
  1371. u64 tmp_eptp = INVALID_PAGE;
  1372. int i;
  1373. kvm_for_each_vcpu(i, vcpu, kvm) {
  1374. if (!VALID_PAGE(tmp_eptp)) {
  1375. tmp_eptp = to_vmx(vcpu)->ept_pointer;
  1376. } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) {
  1377. to_kvm_vmx(kvm)->ept_pointers_match
  1378. = EPT_POINTERS_MISMATCH;
  1379. return;
  1380. }
  1381. }
  1382. to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH;
  1383. }
  1384. static int vmx_hv_remote_flush_tlb(struct kvm *kvm)
  1385. {
  1386. struct kvm_vcpu *vcpu;
  1387. int ret = -ENOTSUPP, i;
  1388. spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
  1389. if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK)
  1390. check_ept_pointer_match(kvm);
  1391. /*
  1392. * FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE hypercall needs the address of the
  1393. * base of EPT PML4 table, strip off EPT configuration information.
  1394. */
  1395. if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
  1396. kvm_for_each_vcpu(i, vcpu, kvm)
  1397. ret |= hyperv_flush_guest_mapping(
  1398. to_vmx(kvm_get_vcpu(kvm, i))->ept_pointer & PAGE_MASK);
  1399. } else {
  1400. ret = hyperv_flush_guest_mapping(
  1401. to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer & PAGE_MASK);
  1402. }
  1403. spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
  1404. return ret;
  1405. }
  1406. #else /* !IS_ENABLED(CONFIG_HYPERV) */
  1407. static inline void evmcs_write64(unsigned long field, u64 value) {}
  1408. static inline void evmcs_write32(unsigned long field, u32 value) {}
  1409. static inline void evmcs_write16(unsigned long field, u16 value) {}
  1410. static inline u64 evmcs_read64(unsigned long field) { return 0; }
  1411. static inline u32 evmcs_read32(unsigned long field) { return 0; }
  1412. static inline u16 evmcs_read16(unsigned long field) { return 0; }
  1413. static inline void evmcs_load(u64 phys_addr) {}
  1414. static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
  1415. static inline void evmcs_touch_msr_bitmap(void) {}
  1416. #endif /* IS_ENABLED(CONFIG_HYPERV) */
  1417. static int nested_enable_evmcs(struct kvm_vcpu *vcpu,
  1418. uint16_t *vmcs_version)
  1419. {
  1420. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1421. /* We don't support disabling the feature for simplicity. */
  1422. if (vmx->nested.enlightened_vmcs_enabled)
  1423. return 0;
  1424. vmx->nested.enlightened_vmcs_enabled = true;
  1425. /*
  1426. * vmcs_version represents the range of supported Enlightened VMCS
  1427. * versions: lower 8 bits is the minimal version, higher 8 bits is the
  1428. * maximum supported version. KVM supports versions from 1 to
  1429. * KVM_EVMCS_VERSION.
  1430. */
  1431. if (vmcs_version)
  1432. *vmcs_version = (KVM_EVMCS_VERSION << 8) | 1;
  1433. vmx->nested.msrs.pinbased_ctls_high &= ~EVMCS1_UNSUPPORTED_PINCTRL;
  1434. vmx->nested.msrs.entry_ctls_high &= ~EVMCS1_UNSUPPORTED_VMENTRY_CTRL;
  1435. vmx->nested.msrs.exit_ctls_high &= ~EVMCS1_UNSUPPORTED_VMEXIT_CTRL;
  1436. vmx->nested.msrs.secondary_ctls_high &= ~EVMCS1_UNSUPPORTED_2NDEXEC;
  1437. vmx->nested.msrs.vmfunc_controls &= ~EVMCS1_UNSUPPORTED_VMFUNC;
  1438. return 0;
  1439. }
  1440. static inline bool is_exception_n(u32 intr_info, u8 vector)
  1441. {
  1442. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  1443. INTR_INFO_VALID_MASK)) ==
  1444. (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
  1445. }
  1446. static inline bool is_debug(u32 intr_info)
  1447. {
  1448. return is_exception_n(intr_info, DB_VECTOR);
  1449. }
  1450. static inline bool is_breakpoint(u32 intr_info)
  1451. {
  1452. return is_exception_n(intr_info, BP_VECTOR);
  1453. }
  1454. static inline bool is_page_fault(u32 intr_info)
  1455. {
  1456. return is_exception_n(intr_info, PF_VECTOR);
  1457. }
  1458. static inline bool is_invalid_opcode(u32 intr_info)
  1459. {
  1460. return is_exception_n(intr_info, UD_VECTOR);
  1461. }
  1462. static inline bool is_gp_fault(u32 intr_info)
  1463. {
  1464. return is_exception_n(intr_info, GP_VECTOR);
  1465. }
  1466. static inline bool is_machine_check(u32 intr_info)
  1467. {
  1468. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  1469. INTR_INFO_VALID_MASK)) ==
  1470. (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
  1471. }
  1472. /* Undocumented: icebp/int1 */
  1473. static inline bool is_icebp(u32 intr_info)
  1474. {
  1475. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
  1476. == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
  1477. }
  1478. static inline bool cpu_has_vmx_msr_bitmap(void)
  1479. {
  1480. return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
  1481. }
  1482. static inline bool cpu_has_vmx_tpr_shadow(void)
  1483. {
  1484. return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
  1485. }
  1486. static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
  1487. {
  1488. return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
  1489. }
  1490. static inline bool cpu_has_secondary_exec_ctrls(void)
  1491. {
  1492. return vmcs_config.cpu_based_exec_ctrl &
  1493. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  1494. }
  1495. static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
  1496. {
  1497. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1498. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  1499. }
  1500. static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
  1501. {
  1502. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1503. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  1504. }
  1505. static inline bool cpu_has_vmx_apic_register_virt(void)
  1506. {
  1507. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1508. SECONDARY_EXEC_APIC_REGISTER_VIRT;
  1509. }
  1510. static inline bool cpu_has_vmx_virtual_intr_delivery(void)
  1511. {
  1512. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1513. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
  1514. }
  1515. static inline bool cpu_has_vmx_encls_vmexit(void)
  1516. {
  1517. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1518. SECONDARY_EXEC_ENCLS_EXITING;
  1519. }
  1520. /*
  1521. * Comment's format: document - errata name - stepping - processor name.
  1522. * Refer from
  1523. * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
  1524. */
  1525. static u32 vmx_preemption_cpu_tfms[] = {
  1526. /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
  1527. 0x000206E6,
  1528. /* 323056.pdf - AAX65 - C2 - Xeon L3406 */
  1529. /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
  1530. /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
  1531. 0x00020652,
  1532. /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
  1533. 0x00020655,
  1534. /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
  1535. /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
  1536. /*
  1537. * 320767.pdf - AAP86 - B1 -
  1538. * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
  1539. */
  1540. 0x000106E5,
  1541. /* 321333.pdf - AAM126 - C0 - Xeon 3500 */
  1542. 0x000106A0,
  1543. /* 321333.pdf - AAM126 - C1 - Xeon 3500 */
  1544. 0x000106A1,
  1545. /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
  1546. 0x000106A4,
  1547. /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
  1548. /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
  1549. /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
  1550. 0x000106A5,
  1551. };
  1552. static inline bool cpu_has_broken_vmx_preemption_timer(void)
  1553. {
  1554. u32 eax = cpuid_eax(0x00000001), i;
  1555. /* Clear the reserved bits */
  1556. eax &= ~(0x3U << 14 | 0xfU << 28);
  1557. for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
  1558. if (eax == vmx_preemption_cpu_tfms[i])
  1559. return true;
  1560. return false;
  1561. }
  1562. static inline bool cpu_has_vmx_preemption_timer(void)
  1563. {
  1564. return vmcs_config.pin_based_exec_ctrl &
  1565. PIN_BASED_VMX_PREEMPTION_TIMER;
  1566. }
  1567. static inline bool cpu_has_vmx_posted_intr(void)
  1568. {
  1569. return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
  1570. vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
  1571. }
  1572. static inline bool cpu_has_vmx_apicv(void)
  1573. {
  1574. return cpu_has_vmx_apic_register_virt() &&
  1575. cpu_has_vmx_virtual_intr_delivery() &&
  1576. cpu_has_vmx_posted_intr();
  1577. }
  1578. static inline bool cpu_has_vmx_flexpriority(void)
  1579. {
  1580. return cpu_has_vmx_tpr_shadow() &&
  1581. cpu_has_vmx_virtualize_apic_accesses();
  1582. }
  1583. static inline bool cpu_has_vmx_ept_execute_only(void)
  1584. {
  1585. return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
  1586. }
  1587. static inline bool cpu_has_vmx_ept_2m_page(void)
  1588. {
  1589. return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
  1590. }
  1591. static inline bool cpu_has_vmx_ept_1g_page(void)
  1592. {
  1593. return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
  1594. }
  1595. static inline bool cpu_has_vmx_ept_4levels(void)
  1596. {
  1597. return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
  1598. }
  1599. static inline bool cpu_has_vmx_ept_mt_wb(void)
  1600. {
  1601. return vmx_capability.ept & VMX_EPTP_WB_BIT;
  1602. }
  1603. static inline bool cpu_has_vmx_ept_5levels(void)
  1604. {
  1605. return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
  1606. }
  1607. static inline bool cpu_has_vmx_ept_ad_bits(void)
  1608. {
  1609. return vmx_capability.ept & VMX_EPT_AD_BIT;
  1610. }
  1611. static inline bool cpu_has_vmx_invept_context(void)
  1612. {
  1613. return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
  1614. }
  1615. static inline bool cpu_has_vmx_invept_global(void)
  1616. {
  1617. return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
  1618. }
  1619. static inline bool cpu_has_vmx_invvpid_individual_addr(void)
  1620. {
  1621. return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
  1622. }
  1623. static inline bool cpu_has_vmx_invvpid_single(void)
  1624. {
  1625. return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
  1626. }
  1627. static inline bool cpu_has_vmx_invvpid_global(void)
  1628. {
  1629. return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
  1630. }
  1631. static inline bool cpu_has_vmx_invvpid(void)
  1632. {
  1633. return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
  1634. }
  1635. static inline bool cpu_has_vmx_ept(void)
  1636. {
  1637. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1638. SECONDARY_EXEC_ENABLE_EPT;
  1639. }
  1640. static inline bool cpu_has_vmx_unrestricted_guest(void)
  1641. {
  1642. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1643. SECONDARY_EXEC_UNRESTRICTED_GUEST;
  1644. }
  1645. static inline bool cpu_has_vmx_ple(void)
  1646. {
  1647. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1648. SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  1649. }
  1650. static inline bool cpu_has_vmx_basic_inout(void)
  1651. {
  1652. return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
  1653. }
  1654. static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
  1655. {
  1656. return flexpriority_enabled && lapic_in_kernel(vcpu);
  1657. }
  1658. static inline bool cpu_has_vmx_vpid(void)
  1659. {
  1660. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1661. SECONDARY_EXEC_ENABLE_VPID;
  1662. }
  1663. static inline bool cpu_has_vmx_rdtscp(void)
  1664. {
  1665. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1666. SECONDARY_EXEC_RDTSCP;
  1667. }
  1668. static inline bool cpu_has_vmx_invpcid(void)
  1669. {
  1670. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1671. SECONDARY_EXEC_ENABLE_INVPCID;
  1672. }
  1673. static inline bool cpu_has_virtual_nmis(void)
  1674. {
  1675. return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
  1676. }
  1677. static inline bool cpu_has_vmx_wbinvd_exit(void)
  1678. {
  1679. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1680. SECONDARY_EXEC_WBINVD_EXITING;
  1681. }
  1682. static inline bool cpu_has_vmx_shadow_vmcs(void)
  1683. {
  1684. u64 vmx_msr;
  1685. rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
  1686. /* check if the cpu supports writing r/o exit information fields */
  1687. if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
  1688. return false;
  1689. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1690. SECONDARY_EXEC_SHADOW_VMCS;
  1691. }
  1692. static inline bool cpu_has_vmx_pml(void)
  1693. {
  1694. return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
  1695. }
  1696. static inline bool cpu_has_vmx_tsc_scaling(void)
  1697. {
  1698. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1699. SECONDARY_EXEC_TSC_SCALING;
  1700. }
  1701. static inline bool cpu_has_vmx_vmfunc(void)
  1702. {
  1703. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1704. SECONDARY_EXEC_ENABLE_VMFUNC;
  1705. }
  1706. static bool vmx_umip_emulated(void)
  1707. {
  1708. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1709. SECONDARY_EXEC_DESC;
  1710. }
  1711. static inline bool report_flexpriority(void)
  1712. {
  1713. return flexpriority_enabled;
  1714. }
  1715. static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
  1716. {
  1717. return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
  1718. }
  1719. /*
  1720. * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
  1721. * to modify any valid field of the VMCS, or are the VM-exit
  1722. * information fields read-only?
  1723. */
  1724. static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
  1725. {
  1726. return to_vmx(vcpu)->nested.msrs.misc_low &
  1727. MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
  1728. }
  1729. static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
  1730. {
  1731. return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
  1732. }
  1733. static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
  1734. {
  1735. return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
  1736. CPU_BASED_MONITOR_TRAP_FLAG;
  1737. }
  1738. static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu)
  1739. {
  1740. return to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
  1741. SECONDARY_EXEC_SHADOW_VMCS;
  1742. }
  1743. static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
  1744. {
  1745. return vmcs12->cpu_based_vm_exec_control & bit;
  1746. }
  1747. static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
  1748. {
  1749. return (vmcs12->cpu_based_vm_exec_control &
  1750. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
  1751. (vmcs12->secondary_vm_exec_control & bit);
  1752. }
  1753. static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
  1754. {
  1755. return vmcs12->pin_based_vm_exec_control &
  1756. PIN_BASED_VMX_PREEMPTION_TIMER;
  1757. }
  1758. static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
  1759. {
  1760. return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
  1761. }
  1762. static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
  1763. {
  1764. return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
  1765. }
  1766. static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
  1767. {
  1768. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
  1769. }
  1770. static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
  1771. {
  1772. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
  1773. }
  1774. static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
  1775. {
  1776. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
  1777. }
  1778. static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
  1779. {
  1780. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
  1781. }
  1782. static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
  1783. {
  1784. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
  1785. }
  1786. static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
  1787. {
  1788. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
  1789. }
  1790. static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
  1791. {
  1792. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  1793. }
  1794. static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
  1795. {
  1796. return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
  1797. }
  1798. static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
  1799. {
  1800. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
  1801. }
  1802. static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
  1803. {
  1804. return nested_cpu_has_vmfunc(vmcs12) &&
  1805. (vmcs12->vm_function_control &
  1806. VMX_VMFUNC_EPTP_SWITCHING);
  1807. }
  1808. static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12)
  1809. {
  1810. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS);
  1811. }
  1812. static inline bool is_nmi(u32 intr_info)
  1813. {
  1814. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
  1815. == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
  1816. }
  1817. static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
  1818. u32 exit_intr_info,
  1819. unsigned long exit_qualification);
  1820. static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
  1821. {
  1822. int i;
  1823. for (i = 0; i < vmx->nmsrs; ++i)
  1824. if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
  1825. return i;
  1826. return -1;
  1827. }
  1828. static inline void __invvpid(unsigned long ext, u16 vpid, gva_t gva)
  1829. {
  1830. struct {
  1831. u64 vpid : 16;
  1832. u64 rsvd : 48;
  1833. u64 gva;
  1834. } operand = { vpid, 0, gva };
  1835. bool error;
  1836. asm volatile (__ex("invvpid %2, %1") CC_SET(na)
  1837. : CC_OUT(na) (error) : "r"(ext), "m"(operand));
  1838. BUG_ON(error);
  1839. }
  1840. static inline void __invept(unsigned long ext, u64 eptp, gpa_t gpa)
  1841. {
  1842. struct {
  1843. u64 eptp, gpa;
  1844. } operand = {eptp, gpa};
  1845. bool error;
  1846. asm volatile (__ex("invept %2, %1") CC_SET(na)
  1847. : CC_OUT(na) (error) : "r"(ext), "m"(operand));
  1848. BUG_ON(error);
  1849. }
  1850. static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
  1851. {
  1852. int i;
  1853. i = __find_msr_index(vmx, msr);
  1854. if (i >= 0)
  1855. return &vmx->guest_msrs[i];
  1856. return NULL;
  1857. }
  1858. static void vmcs_clear(struct vmcs *vmcs)
  1859. {
  1860. u64 phys_addr = __pa(vmcs);
  1861. bool error;
  1862. asm volatile (__ex("vmclear %1") CC_SET(na)
  1863. : CC_OUT(na) (error) : "m"(phys_addr));
  1864. if (unlikely(error))
  1865. printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
  1866. vmcs, phys_addr);
  1867. }
  1868. static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
  1869. {
  1870. vmcs_clear(loaded_vmcs->vmcs);
  1871. if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
  1872. vmcs_clear(loaded_vmcs->shadow_vmcs);
  1873. loaded_vmcs->cpu = -1;
  1874. loaded_vmcs->launched = 0;
  1875. }
  1876. static void vmcs_load(struct vmcs *vmcs)
  1877. {
  1878. u64 phys_addr = __pa(vmcs);
  1879. bool error;
  1880. if (static_branch_unlikely(&enable_evmcs))
  1881. return evmcs_load(phys_addr);
  1882. asm volatile (__ex("vmptrld %1") CC_SET(na)
  1883. : CC_OUT(na) (error) : "m"(phys_addr));
  1884. if (unlikely(error))
  1885. printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
  1886. vmcs, phys_addr);
  1887. }
  1888. #ifdef CONFIG_KEXEC_CORE
  1889. /*
  1890. * This bitmap is used to indicate whether the vmclear
  1891. * operation is enabled on all cpus. All disabled by
  1892. * default.
  1893. */
  1894. static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
  1895. static inline void crash_enable_local_vmclear(int cpu)
  1896. {
  1897. cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
  1898. }
  1899. static inline void crash_disable_local_vmclear(int cpu)
  1900. {
  1901. cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
  1902. }
  1903. static inline int crash_local_vmclear_enabled(int cpu)
  1904. {
  1905. return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
  1906. }
  1907. static void crash_vmclear_local_loaded_vmcss(void)
  1908. {
  1909. int cpu = raw_smp_processor_id();
  1910. struct loaded_vmcs *v;
  1911. if (!crash_local_vmclear_enabled(cpu))
  1912. return;
  1913. list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
  1914. loaded_vmcss_on_cpu_link)
  1915. vmcs_clear(v->vmcs);
  1916. }
  1917. #else
  1918. static inline void crash_enable_local_vmclear(int cpu) { }
  1919. static inline void crash_disable_local_vmclear(int cpu) { }
  1920. #endif /* CONFIG_KEXEC_CORE */
  1921. static void __loaded_vmcs_clear(void *arg)
  1922. {
  1923. struct loaded_vmcs *loaded_vmcs = arg;
  1924. int cpu = raw_smp_processor_id();
  1925. if (loaded_vmcs->cpu != cpu)
  1926. return; /* vcpu migration can race with cpu offline */
  1927. if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
  1928. per_cpu(current_vmcs, cpu) = NULL;
  1929. crash_disable_local_vmclear(cpu);
  1930. list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
  1931. /*
  1932. * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
  1933. * is before setting loaded_vmcs->vcpu to -1 which is done in
  1934. * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
  1935. * then adds the vmcs into percpu list before it is deleted.
  1936. */
  1937. smp_wmb();
  1938. loaded_vmcs_init(loaded_vmcs);
  1939. crash_enable_local_vmclear(cpu);
  1940. }
  1941. static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
  1942. {
  1943. int cpu = loaded_vmcs->cpu;
  1944. if (cpu != -1)
  1945. smp_call_function_single(cpu,
  1946. __loaded_vmcs_clear, loaded_vmcs, 1);
  1947. }
  1948. static inline bool vpid_sync_vcpu_addr(int vpid, gva_t addr)
  1949. {
  1950. if (vpid == 0)
  1951. return true;
  1952. if (cpu_has_vmx_invvpid_individual_addr()) {
  1953. __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, vpid, addr);
  1954. return true;
  1955. }
  1956. return false;
  1957. }
  1958. static inline void vpid_sync_vcpu_single(int vpid)
  1959. {
  1960. if (vpid == 0)
  1961. return;
  1962. if (cpu_has_vmx_invvpid_single())
  1963. __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
  1964. }
  1965. static inline void vpid_sync_vcpu_global(void)
  1966. {
  1967. if (cpu_has_vmx_invvpid_global())
  1968. __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
  1969. }
  1970. static inline void vpid_sync_context(int vpid)
  1971. {
  1972. if (cpu_has_vmx_invvpid_single())
  1973. vpid_sync_vcpu_single(vpid);
  1974. else
  1975. vpid_sync_vcpu_global();
  1976. }
  1977. static inline void ept_sync_global(void)
  1978. {
  1979. __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
  1980. }
  1981. static inline void ept_sync_context(u64 eptp)
  1982. {
  1983. if (cpu_has_vmx_invept_context())
  1984. __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
  1985. else
  1986. ept_sync_global();
  1987. }
  1988. static __always_inline void vmcs_check16(unsigned long field)
  1989. {
  1990. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
  1991. "16-bit accessor invalid for 64-bit field");
  1992. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
  1993. "16-bit accessor invalid for 64-bit high field");
  1994. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
  1995. "16-bit accessor invalid for 32-bit high field");
  1996. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
  1997. "16-bit accessor invalid for natural width field");
  1998. }
  1999. static __always_inline void vmcs_check32(unsigned long field)
  2000. {
  2001. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
  2002. "32-bit accessor invalid for 16-bit field");
  2003. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
  2004. "32-bit accessor invalid for natural width field");
  2005. }
  2006. static __always_inline void vmcs_check64(unsigned long field)
  2007. {
  2008. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
  2009. "64-bit accessor invalid for 16-bit field");
  2010. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
  2011. "64-bit accessor invalid for 64-bit high field");
  2012. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
  2013. "64-bit accessor invalid for 32-bit field");
  2014. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
  2015. "64-bit accessor invalid for natural width field");
  2016. }
  2017. static __always_inline void vmcs_checkl(unsigned long field)
  2018. {
  2019. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
  2020. "Natural width accessor invalid for 16-bit field");
  2021. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
  2022. "Natural width accessor invalid for 64-bit field");
  2023. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
  2024. "Natural width accessor invalid for 64-bit high field");
  2025. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
  2026. "Natural width accessor invalid for 32-bit field");
  2027. }
  2028. static __always_inline unsigned long __vmcs_readl(unsigned long field)
  2029. {
  2030. unsigned long value;
  2031. asm volatile (__ex_clear("vmread %1, %0", "%k0")
  2032. : "=r"(value) : "r"(field));
  2033. return value;
  2034. }
  2035. static __always_inline u16 vmcs_read16(unsigned long field)
  2036. {
  2037. vmcs_check16(field);
  2038. if (static_branch_unlikely(&enable_evmcs))
  2039. return evmcs_read16(field);
  2040. return __vmcs_readl(field);
  2041. }
  2042. static __always_inline u32 vmcs_read32(unsigned long field)
  2043. {
  2044. vmcs_check32(field);
  2045. if (static_branch_unlikely(&enable_evmcs))
  2046. return evmcs_read32(field);
  2047. return __vmcs_readl(field);
  2048. }
  2049. static __always_inline u64 vmcs_read64(unsigned long field)
  2050. {
  2051. vmcs_check64(field);
  2052. if (static_branch_unlikely(&enable_evmcs))
  2053. return evmcs_read64(field);
  2054. #ifdef CONFIG_X86_64
  2055. return __vmcs_readl(field);
  2056. #else
  2057. return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
  2058. #endif
  2059. }
  2060. static __always_inline unsigned long vmcs_readl(unsigned long field)
  2061. {
  2062. vmcs_checkl(field);
  2063. if (static_branch_unlikely(&enable_evmcs))
  2064. return evmcs_read64(field);
  2065. return __vmcs_readl(field);
  2066. }
  2067. static noinline void vmwrite_error(unsigned long field, unsigned long value)
  2068. {
  2069. printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
  2070. field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
  2071. dump_stack();
  2072. }
  2073. static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
  2074. {
  2075. bool error;
  2076. asm volatile (__ex("vmwrite %2, %1") CC_SET(na)
  2077. : CC_OUT(na) (error) : "r"(field), "rm"(value));
  2078. if (unlikely(error))
  2079. vmwrite_error(field, value);
  2080. }
  2081. static __always_inline void vmcs_write16(unsigned long field, u16 value)
  2082. {
  2083. vmcs_check16(field);
  2084. if (static_branch_unlikely(&enable_evmcs))
  2085. return evmcs_write16(field, value);
  2086. __vmcs_writel(field, value);
  2087. }
  2088. static __always_inline void vmcs_write32(unsigned long field, u32 value)
  2089. {
  2090. vmcs_check32(field);
  2091. if (static_branch_unlikely(&enable_evmcs))
  2092. return evmcs_write32(field, value);
  2093. __vmcs_writel(field, value);
  2094. }
  2095. static __always_inline void vmcs_write64(unsigned long field, u64 value)
  2096. {
  2097. vmcs_check64(field);
  2098. if (static_branch_unlikely(&enable_evmcs))
  2099. return evmcs_write64(field, value);
  2100. __vmcs_writel(field, value);
  2101. #ifndef CONFIG_X86_64
  2102. asm volatile ("");
  2103. __vmcs_writel(field+1, value >> 32);
  2104. #endif
  2105. }
  2106. static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
  2107. {
  2108. vmcs_checkl(field);
  2109. if (static_branch_unlikely(&enable_evmcs))
  2110. return evmcs_write64(field, value);
  2111. __vmcs_writel(field, value);
  2112. }
  2113. static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
  2114. {
  2115. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
  2116. "vmcs_clear_bits does not support 64-bit fields");
  2117. if (static_branch_unlikely(&enable_evmcs))
  2118. return evmcs_write32(field, evmcs_read32(field) & ~mask);
  2119. __vmcs_writel(field, __vmcs_readl(field) & ~mask);
  2120. }
  2121. static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
  2122. {
  2123. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
  2124. "vmcs_set_bits does not support 64-bit fields");
  2125. if (static_branch_unlikely(&enable_evmcs))
  2126. return evmcs_write32(field, evmcs_read32(field) | mask);
  2127. __vmcs_writel(field, __vmcs_readl(field) | mask);
  2128. }
  2129. static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
  2130. {
  2131. vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
  2132. }
  2133. static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
  2134. {
  2135. vmcs_write32(VM_ENTRY_CONTROLS, val);
  2136. vmx->vm_entry_controls_shadow = val;
  2137. }
  2138. static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
  2139. {
  2140. if (vmx->vm_entry_controls_shadow != val)
  2141. vm_entry_controls_init(vmx, val);
  2142. }
  2143. static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
  2144. {
  2145. return vmx->vm_entry_controls_shadow;
  2146. }
  2147. static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
  2148. {
  2149. vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
  2150. }
  2151. static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
  2152. {
  2153. vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
  2154. }
  2155. static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
  2156. {
  2157. vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
  2158. }
  2159. static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
  2160. {
  2161. vmcs_write32(VM_EXIT_CONTROLS, val);
  2162. vmx->vm_exit_controls_shadow = val;
  2163. }
  2164. static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
  2165. {
  2166. if (vmx->vm_exit_controls_shadow != val)
  2167. vm_exit_controls_init(vmx, val);
  2168. }
  2169. static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
  2170. {
  2171. return vmx->vm_exit_controls_shadow;
  2172. }
  2173. static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
  2174. {
  2175. vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
  2176. }
  2177. static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
  2178. {
  2179. vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
  2180. }
  2181. static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
  2182. {
  2183. vmx->segment_cache.bitmask = 0;
  2184. }
  2185. static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
  2186. unsigned field)
  2187. {
  2188. bool ret;
  2189. u32 mask = 1 << (seg * SEG_FIELD_NR + field);
  2190. if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
  2191. vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
  2192. vmx->segment_cache.bitmask = 0;
  2193. }
  2194. ret = vmx->segment_cache.bitmask & mask;
  2195. vmx->segment_cache.bitmask |= mask;
  2196. return ret;
  2197. }
  2198. static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
  2199. {
  2200. u16 *p = &vmx->segment_cache.seg[seg].selector;
  2201. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
  2202. *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
  2203. return *p;
  2204. }
  2205. static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
  2206. {
  2207. ulong *p = &vmx->segment_cache.seg[seg].base;
  2208. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
  2209. *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
  2210. return *p;
  2211. }
  2212. static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
  2213. {
  2214. u32 *p = &vmx->segment_cache.seg[seg].limit;
  2215. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
  2216. *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
  2217. return *p;
  2218. }
  2219. static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
  2220. {
  2221. u32 *p = &vmx->segment_cache.seg[seg].ar;
  2222. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
  2223. *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
  2224. return *p;
  2225. }
  2226. static void update_exception_bitmap(struct kvm_vcpu *vcpu)
  2227. {
  2228. u32 eb;
  2229. eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
  2230. (1u << DB_VECTOR) | (1u << AC_VECTOR);
  2231. /*
  2232. * Guest access to VMware backdoor ports could legitimately
  2233. * trigger #GP because of TSS I/O permission bitmap.
  2234. * We intercept those #GP and allow access to them anyway
  2235. * as VMware does.
  2236. */
  2237. if (enable_vmware_backdoor)
  2238. eb |= (1u << GP_VECTOR);
  2239. if ((vcpu->guest_debug &
  2240. (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
  2241. (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
  2242. eb |= 1u << BP_VECTOR;
  2243. if (to_vmx(vcpu)->rmode.vm86_active)
  2244. eb = ~0;
  2245. if (enable_ept)
  2246. eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
  2247. /* When we are running a nested L2 guest and L1 specified for it a
  2248. * certain exception bitmap, we must trap the same exceptions and pass
  2249. * them to L1. When running L2, we will only handle the exceptions
  2250. * specified above if L1 did not want them.
  2251. */
  2252. if (is_guest_mode(vcpu))
  2253. eb |= get_vmcs12(vcpu)->exception_bitmap;
  2254. vmcs_write32(EXCEPTION_BITMAP, eb);
  2255. }
  2256. /*
  2257. * Check if MSR is intercepted for currently loaded MSR bitmap.
  2258. */
  2259. static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
  2260. {
  2261. unsigned long *msr_bitmap;
  2262. int f = sizeof(unsigned long);
  2263. if (!cpu_has_vmx_msr_bitmap())
  2264. return true;
  2265. msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
  2266. if (msr <= 0x1fff) {
  2267. return !!test_bit(msr, msr_bitmap + 0x800 / f);
  2268. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  2269. msr &= 0x1fff;
  2270. return !!test_bit(msr, msr_bitmap + 0xc00 / f);
  2271. }
  2272. return true;
  2273. }
  2274. /*
  2275. * Check if MSR is intercepted for L01 MSR bitmap.
  2276. */
  2277. static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
  2278. {
  2279. unsigned long *msr_bitmap;
  2280. int f = sizeof(unsigned long);
  2281. if (!cpu_has_vmx_msr_bitmap())
  2282. return true;
  2283. msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
  2284. if (msr <= 0x1fff) {
  2285. return !!test_bit(msr, msr_bitmap + 0x800 / f);
  2286. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  2287. msr &= 0x1fff;
  2288. return !!test_bit(msr, msr_bitmap + 0xc00 / f);
  2289. }
  2290. return true;
  2291. }
  2292. static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
  2293. unsigned long entry, unsigned long exit)
  2294. {
  2295. vm_entry_controls_clearbit(vmx, entry);
  2296. vm_exit_controls_clearbit(vmx, exit);
  2297. }
  2298. static int find_msr(struct vmx_msrs *m, unsigned int msr)
  2299. {
  2300. unsigned int i;
  2301. for (i = 0; i < m->nr; ++i) {
  2302. if (m->val[i].index == msr)
  2303. return i;
  2304. }
  2305. return -ENOENT;
  2306. }
  2307. static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
  2308. {
  2309. int i;
  2310. struct msr_autoload *m = &vmx->msr_autoload;
  2311. switch (msr) {
  2312. case MSR_EFER:
  2313. if (cpu_has_load_ia32_efer) {
  2314. clear_atomic_switch_msr_special(vmx,
  2315. VM_ENTRY_LOAD_IA32_EFER,
  2316. VM_EXIT_LOAD_IA32_EFER);
  2317. return;
  2318. }
  2319. break;
  2320. case MSR_CORE_PERF_GLOBAL_CTRL:
  2321. if (cpu_has_load_perf_global_ctrl) {
  2322. clear_atomic_switch_msr_special(vmx,
  2323. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
  2324. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
  2325. return;
  2326. }
  2327. break;
  2328. }
  2329. i = find_msr(&m->guest, msr);
  2330. if (i < 0)
  2331. goto skip_guest;
  2332. --m->guest.nr;
  2333. m->guest.val[i] = m->guest.val[m->guest.nr];
  2334. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
  2335. skip_guest:
  2336. i = find_msr(&m->host, msr);
  2337. if (i < 0)
  2338. return;
  2339. --m->host.nr;
  2340. m->host.val[i] = m->host.val[m->host.nr];
  2341. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
  2342. }
  2343. static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
  2344. unsigned long entry, unsigned long exit,
  2345. unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
  2346. u64 guest_val, u64 host_val)
  2347. {
  2348. vmcs_write64(guest_val_vmcs, guest_val);
  2349. if (host_val_vmcs != HOST_IA32_EFER)
  2350. vmcs_write64(host_val_vmcs, host_val);
  2351. vm_entry_controls_setbit(vmx, entry);
  2352. vm_exit_controls_setbit(vmx, exit);
  2353. }
  2354. static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
  2355. u64 guest_val, u64 host_val, bool entry_only)
  2356. {
  2357. int i, j = 0;
  2358. struct msr_autoload *m = &vmx->msr_autoload;
  2359. switch (msr) {
  2360. case MSR_EFER:
  2361. if (cpu_has_load_ia32_efer) {
  2362. add_atomic_switch_msr_special(vmx,
  2363. VM_ENTRY_LOAD_IA32_EFER,
  2364. VM_EXIT_LOAD_IA32_EFER,
  2365. GUEST_IA32_EFER,
  2366. HOST_IA32_EFER,
  2367. guest_val, host_val);
  2368. return;
  2369. }
  2370. break;
  2371. case MSR_CORE_PERF_GLOBAL_CTRL:
  2372. if (cpu_has_load_perf_global_ctrl) {
  2373. add_atomic_switch_msr_special(vmx,
  2374. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
  2375. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
  2376. GUEST_IA32_PERF_GLOBAL_CTRL,
  2377. HOST_IA32_PERF_GLOBAL_CTRL,
  2378. guest_val, host_val);
  2379. return;
  2380. }
  2381. break;
  2382. case MSR_IA32_PEBS_ENABLE:
  2383. /* PEBS needs a quiescent period after being disabled (to write
  2384. * a record). Disabling PEBS through VMX MSR swapping doesn't
  2385. * provide that period, so a CPU could write host's record into
  2386. * guest's memory.
  2387. */
  2388. wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
  2389. }
  2390. i = find_msr(&m->guest, msr);
  2391. if (!entry_only)
  2392. j = find_msr(&m->host, msr);
  2393. if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) {
  2394. printk_once(KERN_WARNING "Not enough msr switch entries. "
  2395. "Can't add msr %x\n", msr);
  2396. return;
  2397. }
  2398. if (i < 0) {
  2399. i = m->guest.nr++;
  2400. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
  2401. }
  2402. m->guest.val[i].index = msr;
  2403. m->guest.val[i].value = guest_val;
  2404. if (entry_only)
  2405. return;
  2406. if (j < 0) {
  2407. j = m->host.nr++;
  2408. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
  2409. }
  2410. m->host.val[j].index = msr;
  2411. m->host.val[j].value = host_val;
  2412. }
  2413. static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
  2414. {
  2415. u64 guest_efer = vmx->vcpu.arch.efer;
  2416. u64 ignore_bits = 0;
  2417. if (!enable_ept) {
  2418. /*
  2419. * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
  2420. * host CPUID is more efficient than testing guest CPUID
  2421. * or CR4. Host SMEP is anyway a requirement for guest SMEP.
  2422. */
  2423. if (boot_cpu_has(X86_FEATURE_SMEP))
  2424. guest_efer |= EFER_NX;
  2425. else if (!(guest_efer & EFER_NX))
  2426. ignore_bits |= EFER_NX;
  2427. }
  2428. /*
  2429. * LMA and LME handled by hardware; SCE meaningless outside long mode.
  2430. */
  2431. ignore_bits |= EFER_SCE;
  2432. #ifdef CONFIG_X86_64
  2433. ignore_bits |= EFER_LMA | EFER_LME;
  2434. /* SCE is meaningful only in long mode on Intel */
  2435. if (guest_efer & EFER_LMA)
  2436. ignore_bits &= ~(u64)EFER_SCE;
  2437. #endif
  2438. /*
  2439. * On EPT, we can't emulate NX, so we must switch EFER atomically.
  2440. * On CPUs that support "load IA32_EFER", always switch EFER
  2441. * atomically, since it's faster than switching it manually.
  2442. */
  2443. if (cpu_has_load_ia32_efer ||
  2444. (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
  2445. if (!(guest_efer & EFER_LMA))
  2446. guest_efer &= ~EFER_LME;
  2447. if (guest_efer != host_efer)
  2448. add_atomic_switch_msr(vmx, MSR_EFER,
  2449. guest_efer, host_efer, false);
  2450. else
  2451. clear_atomic_switch_msr(vmx, MSR_EFER);
  2452. return false;
  2453. } else {
  2454. clear_atomic_switch_msr(vmx, MSR_EFER);
  2455. guest_efer &= ~ignore_bits;
  2456. guest_efer |= host_efer & ignore_bits;
  2457. vmx->guest_msrs[efer_offset].data = guest_efer;
  2458. vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
  2459. return true;
  2460. }
  2461. }
  2462. #ifdef CONFIG_X86_32
  2463. /*
  2464. * On 32-bit kernels, VM exits still load the FS and GS bases from the
  2465. * VMCS rather than the segment table. KVM uses this helper to figure
  2466. * out the current bases to poke them into the VMCS before entry.
  2467. */
  2468. static unsigned long segment_base(u16 selector)
  2469. {
  2470. struct desc_struct *table;
  2471. unsigned long v;
  2472. if (!(selector & ~SEGMENT_RPL_MASK))
  2473. return 0;
  2474. table = get_current_gdt_ro();
  2475. if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
  2476. u16 ldt_selector = kvm_read_ldt();
  2477. if (!(ldt_selector & ~SEGMENT_RPL_MASK))
  2478. return 0;
  2479. table = (struct desc_struct *)segment_base(ldt_selector);
  2480. }
  2481. v = get_desc_base(&table[selector >> 3]);
  2482. return v;
  2483. }
  2484. #endif
  2485. static void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
  2486. {
  2487. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2488. struct vmcs_host_state *host_state;
  2489. #ifdef CONFIG_X86_64
  2490. int cpu = raw_smp_processor_id();
  2491. #endif
  2492. unsigned long fs_base, gs_base;
  2493. u16 fs_sel, gs_sel;
  2494. int i;
  2495. vmx->req_immediate_exit = false;
  2496. if (vmx->loaded_cpu_state)
  2497. return;
  2498. vmx->loaded_cpu_state = vmx->loaded_vmcs;
  2499. host_state = &vmx->loaded_cpu_state->host_state;
  2500. /*
  2501. * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
  2502. * allow segment selectors with cpl > 0 or ti == 1.
  2503. */
  2504. host_state->ldt_sel = kvm_read_ldt();
  2505. #ifdef CONFIG_X86_64
  2506. savesegment(ds, host_state->ds_sel);
  2507. savesegment(es, host_state->es_sel);
  2508. gs_base = cpu_kernelmode_gs_base(cpu);
  2509. if (likely(is_64bit_mm(current->mm))) {
  2510. save_fsgs_for_kvm();
  2511. fs_sel = current->thread.fsindex;
  2512. gs_sel = current->thread.gsindex;
  2513. fs_base = current->thread.fsbase;
  2514. vmx->msr_host_kernel_gs_base = current->thread.gsbase;
  2515. } else {
  2516. savesegment(fs, fs_sel);
  2517. savesegment(gs, gs_sel);
  2518. fs_base = read_msr(MSR_FS_BASE);
  2519. vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
  2520. }
  2521. wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
  2522. #else
  2523. savesegment(fs, fs_sel);
  2524. savesegment(gs, gs_sel);
  2525. fs_base = segment_base(fs_sel);
  2526. gs_base = segment_base(gs_sel);
  2527. #endif
  2528. if (unlikely(fs_sel != host_state->fs_sel)) {
  2529. if (!(fs_sel & 7))
  2530. vmcs_write16(HOST_FS_SELECTOR, fs_sel);
  2531. else
  2532. vmcs_write16(HOST_FS_SELECTOR, 0);
  2533. host_state->fs_sel = fs_sel;
  2534. }
  2535. if (unlikely(gs_sel != host_state->gs_sel)) {
  2536. if (!(gs_sel & 7))
  2537. vmcs_write16(HOST_GS_SELECTOR, gs_sel);
  2538. else
  2539. vmcs_write16(HOST_GS_SELECTOR, 0);
  2540. host_state->gs_sel = gs_sel;
  2541. }
  2542. if (unlikely(fs_base != host_state->fs_base)) {
  2543. vmcs_writel(HOST_FS_BASE, fs_base);
  2544. host_state->fs_base = fs_base;
  2545. }
  2546. if (unlikely(gs_base != host_state->gs_base)) {
  2547. vmcs_writel(HOST_GS_BASE, gs_base);
  2548. host_state->gs_base = gs_base;
  2549. }
  2550. for (i = 0; i < vmx->save_nmsrs; ++i)
  2551. kvm_set_shared_msr(vmx->guest_msrs[i].index,
  2552. vmx->guest_msrs[i].data,
  2553. vmx->guest_msrs[i].mask);
  2554. }
  2555. static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
  2556. {
  2557. struct vmcs_host_state *host_state;
  2558. if (!vmx->loaded_cpu_state)
  2559. return;
  2560. WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs);
  2561. host_state = &vmx->loaded_cpu_state->host_state;
  2562. ++vmx->vcpu.stat.host_state_reload;
  2563. vmx->loaded_cpu_state = NULL;
  2564. #ifdef CONFIG_X86_64
  2565. rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
  2566. #endif
  2567. if (host_state->ldt_sel || (host_state->gs_sel & 7)) {
  2568. kvm_load_ldt(host_state->ldt_sel);
  2569. #ifdef CONFIG_X86_64
  2570. load_gs_index(host_state->gs_sel);
  2571. #else
  2572. loadsegment(gs, host_state->gs_sel);
  2573. #endif
  2574. }
  2575. if (host_state->fs_sel & 7)
  2576. loadsegment(fs, host_state->fs_sel);
  2577. #ifdef CONFIG_X86_64
  2578. if (unlikely(host_state->ds_sel | host_state->es_sel)) {
  2579. loadsegment(ds, host_state->ds_sel);
  2580. loadsegment(es, host_state->es_sel);
  2581. }
  2582. #endif
  2583. invalidate_tss_limit();
  2584. #ifdef CONFIG_X86_64
  2585. wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
  2586. #endif
  2587. load_fixmap_gdt(raw_smp_processor_id());
  2588. }
  2589. #ifdef CONFIG_X86_64
  2590. static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
  2591. {
  2592. preempt_disable();
  2593. if (vmx->loaded_cpu_state)
  2594. rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
  2595. preempt_enable();
  2596. return vmx->msr_guest_kernel_gs_base;
  2597. }
  2598. static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
  2599. {
  2600. preempt_disable();
  2601. if (vmx->loaded_cpu_state)
  2602. wrmsrl(MSR_KERNEL_GS_BASE, data);
  2603. preempt_enable();
  2604. vmx->msr_guest_kernel_gs_base = data;
  2605. }
  2606. #endif
  2607. static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
  2608. {
  2609. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  2610. struct pi_desc old, new;
  2611. unsigned int dest;
  2612. /*
  2613. * In case of hot-plug or hot-unplug, we may have to undo
  2614. * vmx_vcpu_pi_put even if there is no assigned device. And we
  2615. * always keep PI.NDST up to date for simplicity: it makes the
  2616. * code easier, and CPU migration is not a fast path.
  2617. */
  2618. if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
  2619. return;
  2620. /*
  2621. * First handle the simple case where no cmpxchg is necessary; just
  2622. * allow posting non-urgent interrupts.
  2623. *
  2624. * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
  2625. * PI.NDST: pi_post_block will do it for us and the wakeup_handler
  2626. * expects the VCPU to be on the blocked_vcpu_list that matches
  2627. * PI.NDST.
  2628. */
  2629. if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
  2630. vcpu->cpu == cpu) {
  2631. pi_clear_sn(pi_desc);
  2632. return;
  2633. }
  2634. /* The full case. */
  2635. do {
  2636. old.control = new.control = pi_desc->control;
  2637. dest = cpu_physical_id(cpu);
  2638. if (x2apic_enabled())
  2639. new.ndst = dest;
  2640. else
  2641. new.ndst = (dest << 8) & 0xFF00;
  2642. new.sn = 0;
  2643. } while (cmpxchg64(&pi_desc->control, old.control,
  2644. new.control) != old.control);
  2645. }
  2646. static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
  2647. {
  2648. vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
  2649. vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
  2650. }
  2651. /*
  2652. * Switches to specified vcpu, until a matching vcpu_put(), but assumes
  2653. * vcpu mutex is already taken.
  2654. */
  2655. static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  2656. {
  2657. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2658. bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
  2659. if (!already_loaded) {
  2660. loaded_vmcs_clear(vmx->loaded_vmcs);
  2661. local_irq_disable();
  2662. crash_disable_local_vmclear(cpu);
  2663. /*
  2664. * Read loaded_vmcs->cpu should be before fetching
  2665. * loaded_vmcs->loaded_vmcss_on_cpu_link.
  2666. * See the comments in __loaded_vmcs_clear().
  2667. */
  2668. smp_rmb();
  2669. list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
  2670. &per_cpu(loaded_vmcss_on_cpu, cpu));
  2671. crash_enable_local_vmclear(cpu);
  2672. local_irq_enable();
  2673. }
  2674. if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
  2675. per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
  2676. vmcs_load(vmx->loaded_vmcs->vmcs);
  2677. indirect_branch_prediction_barrier();
  2678. }
  2679. if (!already_loaded) {
  2680. void *gdt = get_current_gdt_ro();
  2681. unsigned long sysenter_esp;
  2682. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  2683. /*
  2684. * Linux uses per-cpu TSS and GDT, so set these when switching
  2685. * processors. See 22.2.4.
  2686. */
  2687. vmcs_writel(HOST_TR_BASE,
  2688. (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
  2689. vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
  2690. /*
  2691. * VM exits change the host TR limit to 0x67 after a VM
  2692. * exit. This is okay, since 0x67 covers everything except
  2693. * the IO bitmap and have have code to handle the IO bitmap
  2694. * being lost after a VM exit.
  2695. */
  2696. BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
  2697. rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
  2698. vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
  2699. vmx->loaded_vmcs->cpu = cpu;
  2700. }
  2701. /* Setup TSC multiplier */
  2702. if (kvm_has_tsc_control &&
  2703. vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
  2704. decache_tsc_multiplier(vmx);
  2705. vmx_vcpu_pi_load(vcpu, cpu);
  2706. vmx->host_pkru = read_pkru();
  2707. vmx->host_debugctlmsr = get_debugctlmsr();
  2708. }
  2709. static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
  2710. {
  2711. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  2712. if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
  2713. !irq_remapping_cap(IRQ_POSTING_CAP) ||
  2714. !kvm_vcpu_apicv_active(vcpu))
  2715. return;
  2716. /* Set SN when the vCPU is preempted */
  2717. if (vcpu->preempted)
  2718. pi_set_sn(pi_desc);
  2719. }
  2720. static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
  2721. {
  2722. vmx_vcpu_pi_put(vcpu);
  2723. vmx_prepare_switch_to_host(to_vmx(vcpu));
  2724. }
  2725. static bool emulation_required(struct kvm_vcpu *vcpu)
  2726. {
  2727. return emulate_invalid_guest_state && !guest_state_valid(vcpu);
  2728. }
  2729. static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
  2730. /*
  2731. * Return the cr0 value that a nested guest would read. This is a combination
  2732. * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
  2733. * its hypervisor (cr0_read_shadow).
  2734. */
  2735. static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
  2736. {
  2737. return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
  2738. (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
  2739. }
  2740. static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
  2741. {
  2742. return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
  2743. (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
  2744. }
  2745. static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
  2746. {
  2747. unsigned long rflags, save_rflags;
  2748. if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
  2749. __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
  2750. rflags = vmcs_readl(GUEST_RFLAGS);
  2751. if (to_vmx(vcpu)->rmode.vm86_active) {
  2752. rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
  2753. save_rflags = to_vmx(vcpu)->rmode.save_rflags;
  2754. rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
  2755. }
  2756. to_vmx(vcpu)->rflags = rflags;
  2757. }
  2758. return to_vmx(vcpu)->rflags;
  2759. }
  2760. static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  2761. {
  2762. unsigned long old_rflags = vmx_get_rflags(vcpu);
  2763. __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
  2764. to_vmx(vcpu)->rflags = rflags;
  2765. if (to_vmx(vcpu)->rmode.vm86_active) {
  2766. to_vmx(vcpu)->rmode.save_rflags = rflags;
  2767. rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
  2768. }
  2769. vmcs_writel(GUEST_RFLAGS, rflags);
  2770. if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
  2771. to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
  2772. }
  2773. static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
  2774. {
  2775. u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  2776. int ret = 0;
  2777. if (interruptibility & GUEST_INTR_STATE_STI)
  2778. ret |= KVM_X86_SHADOW_INT_STI;
  2779. if (interruptibility & GUEST_INTR_STATE_MOV_SS)
  2780. ret |= KVM_X86_SHADOW_INT_MOV_SS;
  2781. return ret;
  2782. }
  2783. static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
  2784. {
  2785. u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  2786. u32 interruptibility = interruptibility_old;
  2787. interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
  2788. if (mask & KVM_X86_SHADOW_INT_MOV_SS)
  2789. interruptibility |= GUEST_INTR_STATE_MOV_SS;
  2790. else if (mask & KVM_X86_SHADOW_INT_STI)
  2791. interruptibility |= GUEST_INTR_STATE_STI;
  2792. if ((interruptibility != interruptibility_old))
  2793. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
  2794. }
  2795. static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
  2796. {
  2797. unsigned long rip;
  2798. rip = kvm_rip_read(vcpu);
  2799. rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  2800. kvm_rip_write(vcpu, rip);
  2801. /* skipping an emulated instruction also counts */
  2802. vmx_set_interrupt_shadow(vcpu, 0);
  2803. }
  2804. static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
  2805. unsigned long exit_qual)
  2806. {
  2807. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  2808. unsigned int nr = vcpu->arch.exception.nr;
  2809. u32 intr_info = nr | INTR_INFO_VALID_MASK;
  2810. if (vcpu->arch.exception.has_error_code) {
  2811. vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
  2812. intr_info |= INTR_INFO_DELIVER_CODE_MASK;
  2813. }
  2814. if (kvm_exception_is_soft(nr))
  2815. intr_info |= INTR_TYPE_SOFT_EXCEPTION;
  2816. else
  2817. intr_info |= INTR_TYPE_HARD_EXCEPTION;
  2818. if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
  2819. vmx_get_nmi_mask(vcpu))
  2820. intr_info |= INTR_INFO_UNBLOCK_NMI;
  2821. nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
  2822. }
  2823. /*
  2824. * KVM wants to inject page-faults which it got to the guest. This function
  2825. * checks whether in a nested guest, we need to inject them to L1 or L2.
  2826. */
  2827. static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
  2828. {
  2829. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  2830. unsigned int nr = vcpu->arch.exception.nr;
  2831. bool has_payload = vcpu->arch.exception.has_payload;
  2832. unsigned long payload = vcpu->arch.exception.payload;
  2833. if (nr == PF_VECTOR) {
  2834. if (vcpu->arch.exception.nested_apf) {
  2835. *exit_qual = vcpu->arch.apf.nested_apf_token;
  2836. return 1;
  2837. }
  2838. if (nested_vmx_is_page_fault_vmexit(vmcs12,
  2839. vcpu->arch.exception.error_code)) {
  2840. *exit_qual = has_payload ? payload : vcpu->arch.cr2;
  2841. return 1;
  2842. }
  2843. } else if (vmcs12->exception_bitmap & (1u << nr)) {
  2844. if (nr == DB_VECTOR) {
  2845. if (!has_payload) {
  2846. payload = vcpu->arch.dr6;
  2847. payload &= ~(DR6_FIXED_1 | DR6_BT);
  2848. payload ^= DR6_RTM;
  2849. }
  2850. *exit_qual = payload;
  2851. } else
  2852. *exit_qual = 0;
  2853. return 1;
  2854. }
  2855. return 0;
  2856. }
  2857. static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
  2858. {
  2859. /*
  2860. * Ensure that we clear the HLT state in the VMCS. We don't need to
  2861. * explicitly skip the instruction because if the HLT state is set,
  2862. * then the instruction is already executing and RIP has already been
  2863. * advanced.
  2864. */
  2865. if (kvm_hlt_in_guest(vcpu->kvm) &&
  2866. vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
  2867. vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
  2868. }
  2869. static void vmx_queue_exception(struct kvm_vcpu *vcpu)
  2870. {
  2871. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2872. unsigned nr = vcpu->arch.exception.nr;
  2873. bool has_error_code = vcpu->arch.exception.has_error_code;
  2874. u32 error_code = vcpu->arch.exception.error_code;
  2875. u32 intr_info = nr | INTR_INFO_VALID_MASK;
  2876. kvm_deliver_exception_payload(vcpu);
  2877. if (has_error_code) {
  2878. vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
  2879. intr_info |= INTR_INFO_DELIVER_CODE_MASK;
  2880. }
  2881. if (vmx->rmode.vm86_active) {
  2882. int inc_eip = 0;
  2883. if (kvm_exception_is_soft(nr))
  2884. inc_eip = vcpu->arch.event_exit_inst_len;
  2885. if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
  2886. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  2887. return;
  2888. }
  2889. WARN_ON_ONCE(vmx->emulation_required);
  2890. if (kvm_exception_is_soft(nr)) {
  2891. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  2892. vmx->vcpu.arch.event_exit_inst_len);
  2893. intr_info |= INTR_TYPE_SOFT_EXCEPTION;
  2894. } else
  2895. intr_info |= INTR_TYPE_HARD_EXCEPTION;
  2896. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
  2897. vmx_clear_hlt(vcpu);
  2898. }
  2899. static bool vmx_rdtscp_supported(void)
  2900. {
  2901. return cpu_has_vmx_rdtscp();
  2902. }
  2903. static bool vmx_invpcid_supported(void)
  2904. {
  2905. return cpu_has_vmx_invpcid();
  2906. }
  2907. /*
  2908. * Swap MSR entry in host/guest MSR entry array.
  2909. */
  2910. static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
  2911. {
  2912. struct shared_msr_entry tmp;
  2913. tmp = vmx->guest_msrs[to];
  2914. vmx->guest_msrs[to] = vmx->guest_msrs[from];
  2915. vmx->guest_msrs[from] = tmp;
  2916. }
  2917. /*
  2918. * Set up the vmcs to automatically save and restore system
  2919. * msrs. Don't touch the 64-bit msrs if the guest is in legacy
  2920. * mode, as fiddling with msrs is very expensive.
  2921. */
  2922. static void setup_msrs(struct vcpu_vmx *vmx)
  2923. {
  2924. int save_nmsrs, index;
  2925. save_nmsrs = 0;
  2926. #ifdef CONFIG_X86_64
  2927. if (is_long_mode(&vmx->vcpu)) {
  2928. index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
  2929. if (index >= 0)
  2930. move_msr_up(vmx, index, save_nmsrs++);
  2931. index = __find_msr_index(vmx, MSR_LSTAR);
  2932. if (index >= 0)
  2933. move_msr_up(vmx, index, save_nmsrs++);
  2934. index = __find_msr_index(vmx, MSR_CSTAR);
  2935. if (index >= 0)
  2936. move_msr_up(vmx, index, save_nmsrs++);
  2937. index = __find_msr_index(vmx, MSR_TSC_AUX);
  2938. if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
  2939. move_msr_up(vmx, index, save_nmsrs++);
  2940. /*
  2941. * MSR_STAR is only needed on long mode guests, and only
  2942. * if efer.sce is enabled.
  2943. */
  2944. index = __find_msr_index(vmx, MSR_STAR);
  2945. if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
  2946. move_msr_up(vmx, index, save_nmsrs++);
  2947. }
  2948. #endif
  2949. index = __find_msr_index(vmx, MSR_EFER);
  2950. if (index >= 0 && update_transition_efer(vmx, index))
  2951. move_msr_up(vmx, index, save_nmsrs++);
  2952. vmx->save_nmsrs = save_nmsrs;
  2953. if (cpu_has_vmx_msr_bitmap())
  2954. vmx_update_msr_bitmap(&vmx->vcpu);
  2955. }
  2956. static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
  2957. {
  2958. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  2959. if (is_guest_mode(vcpu) &&
  2960. (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
  2961. return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
  2962. return vcpu->arch.tsc_offset;
  2963. }
  2964. /*
  2965. * writes 'offset' into guest's timestamp counter offset register
  2966. */
  2967. static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
  2968. {
  2969. if (is_guest_mode(vcpu)) {
  2970. /*
  2971. * We're here if L1 chose not to trap WRMSR to TSC. According
  2972. * to the spec, this should set L1's TSC; The offset that L1
  2973. * set for L2 remains unchanged, and still needs to be added
  2974. * to the newly set TSC to get L2's TSC.
  2975. */
  2976. struct vmcs12 *vmcs12;
  2977. /* recalculate vmcs02.TSC_OFFSET: */
  2978. vmcs12 = get_vmcs12(vcpu);
  2979. vmcs_write64(TSC_OFFSET, offset +
  2980. (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
  2981. vmcs12->tsc_offset : 0));
  2982. } else {
  2983. trace_kvm_write_tsc_offset(vcpu->vcpu_id,
  2984. vmcs_read64(TSC_OFFSET), offset);
  2985. vmcs_write64(TSC_OFFSET, offset);
  2986. }
  2987. }
  2988. /*
  2989. * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
  2990. * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
  2991. * all guests if the "nested" module option is off, and can also be disabled
  2992. * for a single guest by disabling its VMX cpuid bit.
  2993. */
  2994. static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
  2995. {
  2996. return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
  2997. }
  2998. /*
  2999. * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
  3000. * returned for the various VMX controls MSRs when nested VMX is enabled.
  3001. * The same values should also be used to verify that vmcs12 control fields are
  3002. * valid during nested entry from L1 to L2.
  3003. * Each of these control msrs has a low and high 32-bit half: A low bit is on
  3004. * if the corresponding bit in the (32-bit) control field *must* be on, and a
  3005. * bit in the high half is on if the corresponding bit in the control field
  3006. * may be on. See also vmx_control_verify().
  3007. */
  3008. static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
  3009. {
  3010. if (!nested) {
  3011. memset(msrs, 0, sizeof(*msrs));
  3012. return;
  3013. }
  3014. /*
  3015. * Note that as a general rule, the high half of the MSRs (bits in
  3016. * the control fields which may be 1) should be initialized by the
  3017. * intersection of the underlying hardware's MSR (i.e., features which
  3018. * can be supported) and the list of features we want to expose -
  3019. * because they are known to be properly supported in our code.
  3020. * Also, usually, the low half of the MSRs (bits which must be 1) can
  3021. * be set to 0, meaning that L1 may turn off any of these bits. The
  3022. * reason is that if one of these bits is necessary, it will appear
  3023. * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
  3024. * fields of vmcs01 and vmcs02, will turn these bits off - and
  3025. * nested_vmx_exit_reflected() will not pass related exits to L1.
  3026. * These rules have exceptions below.
  3027. */
  3028. /* pin-based controls */
  3029. rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
  3030. msrs->pinbased_ctls_low,
  3031. msrs->pinbased_ctls_high);
  3032. msrs->pinbased_ctls_low |=
  3033. PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  3034. msrs->pinbased_ctls_high &=
  3035. PIN_BASED_EXT_INTR_MASK |
  3036. PIN_BASED_NMI_EXITING |
  3037. PIN_BASED_VIRTUAL_NMIS |
  3038. (apicv ? PIN_BASED_POSTED_INTR : 0);
  3039. msrs->pinbased_ctls_high |=
  3040. PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
  3041. PIN_BASED_VMX_PREEMPTION_TIMER;
  3042. /* exit controls */
  3043. rdmsr(MSR_IA32_VMX_EXIT_CTLS,
  3044. msrs->exit_ctls_low,
  3045. msrs->exit_ctls_high);
  3046. msrs->exit_ctls_low =
  3047. VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
  3048. msrs->exit_ctls_high &=
  3049. #ifdef CONFIG_X86_64
  3050. VM_EXIT_HOST_ADDR_SPACE_SIZE |
  3051. #endif
  3052. VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
  3053. msrs->exit_ctls_high |=
  3054. VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
  3055. VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
  3056. VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
  3057. /* We support free control of debug control saving. */
  3058. msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
  3059. /* entry controls */
  3060. rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
  3061. msrs->entry_ctls_low,
  3062. msrs->entry_ctls_high);
  3063. msrs->entry_ctls_low =
  3064. VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
  3065. msrs->entry_ctls_high &=
  3066. #ifdef CONFIG_X86_64
  3067. VM_ENTRY_IA32E_MODE |
  3068. #endif
  3069. VM_ENTRY_LOAD_IA32_PAT;
  3070. msrs->entry_ctls_high |=
  3071. (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
  3072. /* We support free control of debug control loading. */
  3073. msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
  3074. /* cpu-based controls */
  3075. rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
  3076. msrs->procbased_ctls_low,
  3077. msrs->procbased_ctls_high);
  3078. msrs->procbased_ctls_low =
  3079. CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  3080. msrs->procbased_ctls_high &=
  3081. CPU_BASED_VIRTUAL_INTR_PENDING |
  3082. CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
  3083. CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
  3084. CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
  3085. CPU_BASED_CR3_STORE_EXITING |
  3086. #ifdef CONFIG_X86_64
  3087. CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
  3088. #endif
  3089. CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
  3090. CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
  3091. CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
  3092. CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
  3093. CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  3094. /*
  3095. * We can allow some features even when not supported by the
  3096. * hardware. For example, L1 can specify an MSR bitmap - and we
  3097. * can use it to avoid exits to L1 - even when L0 runs L2
  3098. * without MSR bitmaps.
  3099. */
  3100. msrs->procbased_ctls_high |=
  3101. CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
  3102. CPU_BASED_USE_MSR_BITMAPS;
  3103. /* We support free control of CR3 access interception. */
  3104. msrs->procbased_ctls_low &=
  3105. ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
  3106. /*
  3107. * secondary cpu-based controls. Do not include those that
  3108. * depend on CPUID bits, they are added later by vmx_cpuid_update.
  3109. */
  3110. rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
  3111. msrs->secondary_ctls_low,
  3112. msrs->secondary_ctls_high);
  3113. msrs->secondary_ctls_low = 0;
  3114. msrs->secondary_ctls_high &=
  3115. SECONDARY_EXEC_DESC |
  3116. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  3117. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  3118. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
  3119. SECONDARY_EXEC_WBINVD_EXITING;
  3120. /*
  3121. * We can emulate "VMCS shadowing," even if the hardware
  3122. * doesn't support it.
  3123. */
  3124. msrs->secondary_ctls_high |=
  3125. SECONDARY_EXEC_SHADOW_VMCS;
  3126. if (enable_ept) {
  3127. /* nested EPT: emulate EPT also to L1 */
  3128. msrs->secondary_ctls_high |=
  3129. SECONDARY_EXEC_ENABLE_EPT;
  3130. msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
  3131. VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
  3132. if (cpu_has_vmx_ept_execute_only())
  3133. msrs->ept_caps |=
  3134. VMX_EPT_EXECUTE_ONLY_BIT;
  3135. msrs->ept_caps &= vmx_capability.ept;
  3136. msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
  3137. VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
  3138. VMX_EPT_1GB_PAGE_BIT;
  3139. if (enable_ept_ad_bits) {
  3140. msrs->secondary_ctls_high |=
  3141. SECONDARY_EXEC_ENABLE_PML;
  3142. msrs->ept_caps |= VMX_EPT_AD_BIT;
  3143. }
  3144. }
  3145. if (cpu_has_vmx_vmfunc()) {
  3146. msrs->secondary_ctls_high |=
  3147. SECONDARY_EXEC_ENABLE_VMFUNC;
  3148. /*
  3149. * Advertise EPTP switching unconditionally
  3150. * since we emulate it
  3151. */
  3152. if (enable_ept)
  3153. msrs->vmfunc_controls =
  3154. VMX_VMFUNC_EPTP_SWITCHING;
  3155. }
  3156. /*
  3157. * Old versions of KVM use the single-context version without
  3158. * checking for support, so declare that it is supported even
  3159. * though it is treated as global context. The alternative is
  3160. * not failing the single-context invvpid, and it is worse.
  3161. */
  3162. if (enable_vpid) {
  3163. msrs->secondary_ctls_high |=
  3164. SECONDARY_EXEC_ENABLE_VPID;
  3165. msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
  3166. VMX_VPID_EXTENT_SUPPORTED_MASK;
  3167. }
  3168. if (enable_unrestricted_guest)
  3169. msrs->secondary_ctls_high |=
  3170. SECONDARY_EXEC_UNRESTRICTED_GUEST;
  3171. if (flexpriority_enabled)
  3172. msrs->secondary_ctls_high |=
  3173. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  3174. /* miscellaneous data */
  3175. rdmsr(MSR_IA32_VMX_MISC,
  3176. msrs->misc_low,
  3177. msrs->misc_high);
  3178. msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
  3179. msrs->misc_low |=
  3180. MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
  3181. VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
  3182. VMX_MISC_ACTIVITY_HLT;
  3183. msrs->misc_high = 0;
  3184. /*
  3185. * This MSR reports some information about VMX support. We
  3186. * should return information about the VMX we emulate for the
  3187. * guest, and the VMCS structure we give it - not about the
  3188. * VMX support of the underlying hardware.
  3189. */
  3190. msrs->basic =
  3191. VMCS12_REVISION |
  3192. VMX_BASIC_TRUE_CTLS |
  3193. ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
  3194. (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
  3195. if (cpu_has_vmx_basic_inout())
  3196. msrs->basic |= VMX_BASIC_INOUT;
  3197. /*
  3198. * These MSRs specify bits which the guest must keep fixed on
  3199. * while L1 is in VMXON mode (in L1's root mode, or running an L2).
  3200. * We picked the standard core2 setting.
  3201. */
  3202. #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
  3203. #define VMXON_CR4_ALWAYSON X86_CR4_VMXE
  3204. msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
  3205. msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
  3206. /* These MSRs specify bits which the guest must keep fixed off. */
  3207. rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
  3208. rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
  3209. /* highest index: VMX_PREEMPTION_TIMER_VALUE */
  3210. msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
  3211. }
  3212. /*
  3213. * if fixed0[i] == 1: val[i] must be 1
  3214. * if fixed1[i] == 0: val[i] must be 0
  3215. */
  3216. static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
  3217. {
  3218. return ((val & fixed1) | fixed0) == val;
  3219. }
  3220. static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
  3221. {
  3222. return fixed_bits_valid(control, low, high);
  3223. }
  3224. static inline u64 vmx_control_msr(u32 low, u32 high)
  3225. {
  3226. return low | ((u64)high << 32);
  3227. }
  3228. static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
  3229. {
  3230. superset &= mask;
  3231. subset &= mask;
  3232. return (superset | subset) == superset;
  3233. }
  3234. static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
  3235. {
  3236. const u64 feature_and_reserved =
  3237. /* feature (except bit 48; see below) */
  3238. BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
  3239. /* reserved */
  3240. BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
  3241. u64 vmx_basic = vmx->nested.msrs.basic;
  3242. if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
  3243. return -EINVAL;
  3244. /*
  3245. * KVM does not emulate a version of VMX that constrains physical
  3246. * addresses of VMX structures (e.g. VMCS) to 32-bits.
  3247. */
  3248. if (data & BIT_ULL(48))
  3249. return -EINVAL;
  3250. if (vmx_basic_vmcs_revision_id(vmx_basic) !=
  3251. vmx_basic_vmcs_revision_id(data))
  3252. return -EINVAL;
  3253. if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
  3254. return -EINVAL;
  3255. vmx->nested.msrs.basic = data;
  3256. return 0;
  3257. }
  3258. static int
  3259. vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
  3260. {
  3261. u64 supported;
  3262. u32 *lowp, *highp;
  3263. switch (msr_index) {
  3264. case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
  3265. lowp = &vmx->nested.msrs.pinbased_ctls_low;
  3266. highp = &vmx->nested.msrs.pinbased_ctls_high;
  3267. break;
  3268. case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
  3269. lowp = &vmx->nested.msrs.procbased_ctls_low;
  3270. highp = &vmx->nested.msrs.procbased_ctls_high;
  3271. break;
  3272. case MSR_IA32_VMX_TRUE_EXIT_CTLS:
  3273. lowp = &vmx->nested.msrs.exit_ctls_low;
  3274. highp = &vmx->nested.msrs.exit_ctls_high;
  3275. break;
  3276. case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
  3277. lowp = &vmx->nested.msrs.entry_ctls_low;
  3278. highp = &vmx->nested.msrs.entry_ctls_high;
  3279. break;
  3280. case MSR_IA32_VMX_PROCBASED_CTLS2:
  3281. lowp = &vmx->nested.msrs.secondary_ctls_low;
  3282. highp = &vmx->nested.msrs.secondary_ctls_high;
  3283. break;
  3284. default:
  3285. BUG();
  3286. }
  3287. supported = vmx_control_msr(*lowp, *highp);
  3288. /* Check must-be-1 bits are still 1. */
  3289. if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
  3290. return -EINVAL;
  3291. /* Check must-be-0 bits are still 0. */
  3292. if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
  3293. return -EINVAL;
  3294. *lowp = data;
  3295. *highp = data >> 32;
  3296. return 0;
  3297. }
  3298. static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
  3299. {
  3300. const u64 feature_and_reserved_bits =
  3301. /* feature */
  3302. BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
  3303. BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
  3304. /* reserved */
  3305. GENMASK_ULL(13, 9) | BIT_ULL(31);
  3306. u64 vmx_misc;
  3307. vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
  3308. vmx->nested.msrs.misc_high);
  3309. if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
  3310. return -EINVAL;
  3311. if ((vmx->nested.msrs.pinbased_ctls_high &
  3312. PIN_BASED_VMX_PREEMPTION_TIMER) &&
  3313. vmx_misc_preemption_timer_rate(data) !=
  3314. vmx_misc_preemption_timer_rate(vmx_misc))
  3315. return -EINVAL;
  3316. if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
  3317. return -EINVAL;
  3318. if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
  3319. return -EINVAL;
  3320. if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
  3321. return -EINVAL;
  3322. vmx->nested.msrs.misc_low = data;
  3323. vmx->nested.msrs.misc_high = data >> 32;
  3324. /*
  3325. * If L1 has read-only VM-exit information fields, use the
  3326. * less permissive vmx_vmwrite_bitmap to specify write
  3327. * permissions for the shadow VMCS.
  3328. */
  3329. if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
  3330. vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
  3331. return 0;
  3332. }
  3333. static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
  3334. {
  3335. u64 vmx_ept_vpid_cap;
  3336. vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
  3337. vmx->nested.msrs.vpid_caps);
  3338. /* Every bit is either reserved or a feature bit. */
  3339. if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
  3340. return -EINVAL;
  3341. vmx->nested.msrs.ept_caps = data;
  3342. vmx->nested.msrs.vpid_caps = data >> 32;
  3343. return 0;
  3344. }
  3345. static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
  3346. {
  3347. u64 *msr;
  3348. switch (msr_index) {
  3349. case MSR_IA32_VMX_CR0_FIXED0:
  3350. msr = &vmx->nested.msrs.cr0_fixed0;
  3351. break;
  3352. case MSR_IA32_VMX_CR4_FIXED0:
  3353. msr = &vmx->nested.msrs.cr4_fixed0;
  3354. break;
  3355. default:
  3356. BUG();
  3357. }
  3358. /*
  3359. * 1 bits (which indicates bits which "must-be-1" during VMX operation)
  3360. * must be 1 in the restored value.
  3361. */
  3362. if (!is_bitwise_subset(data, *msr, -1ULL))
  3363. return -EINVAL;
  3364. *msr = data;
  3365. return 0;
  3366. }
  3367. /*
  3368. * Called when userspace is restoring VMX MSRs.
  3369. *
  3370. * Returns 0 on success, non-0 otherwise.
  3371. */
  3372. static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
  3373. {
  3374. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3375. /*
  3376. * Don't allow changes to the VMX capability MSRs while the vCPU
  3377. * is in VMX operation.
  3378. */
  3379. if (vmx->nested.vmxon)
  3380. return -EBUSY;
  3381. switch (msr_index) {
  3382. case MSR_IA32_VMX_BASIC:
  3383. return vmx_restore_vmx_basic(vmx, data);
  3384. case MSR_IA32_VMX_PINBASED_CTLS:
  3385. case MSR_IA32_VMX_PROCBASED_CTLS:
  3386. case MSR_IA32_VMX_EXIT_CTLS:
  3387. case MSR_IA32_VMX_ENTRY_CTLS:
  3388. /*
  3389. * The "non-true" VMX capability MSRs are generated from the
  3390. * "true" MSRs, so we do not support restoring them directly.
  3391. *
  3392. * If userspace wants to emulate VMX_BASIC[55]=0, userspace
  3393. * should restore the "true" MSRs with the must-be-1 bits
  3394. * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
  3395. * DEFAULT SETTINGS".
  3396. */
  3397. return -EINVAL;
  3398. case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
  3399. case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
  3400. case MSR_IA32_VMX_TRUE_EXIT_CTLS:
  3401. case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
  3402. case MSR_IA32_VMX_PROCBASED_CTLS2:
  3403. return vmx_restore_control_msr(vmx, msr_index, data);
  3404. case MSR_IA32_VMX_MISC:
  3405. return vmx_restore_vmx_misc(vmx, data);
  3406. case MSR_IA32_VMX_CR0_FIXED0:
  3407. case MSR_IA32_VMX_CR4_FIXED0:
  3408. return vmx_restore_fixed0_msr(vmx, msr_index, data);
  3409. case MSR_IA32_VMX_CR0_FIXED1:
  3410. case MSR_IA32_VMX_CR4_FIXED1:
  3411. /*
  3412. * These MSRs are generated based on the vCPU's CPUID, so we
  3413. * do not support restoring them directly.
  3414. */
  3415. return -EINVAL;
  3416. case MSR_IA32_VMX_EPT_VPID_CAP:
  3417. return vmx_restore_vmx_ept_vpid_cap(vmx, data);
  3418. case MSR_IA32_VMX_VMCS_ENUM:
  3419. vmx->nested.msrs.vmcs_enum = data;
  3420. return 0;
  3421. default:
  3422. /*
  3423. * The rest of the VMX capability MSRs do not support restore.
  3424. */
  3425. return -EINVAL;
  3426. }
  3427. }
  3428. /* Returns 0 on success, non-0 otherwise. */
  3429. static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
  3430. {
  3431. switch (msr_index) {
  3432. case MSR_IA32_VMX_BASIC:
  3433. *pdata = msrs->basic;
  3434. break;
  3435. case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
  3436. case MSR_IA32_VMX_PINBASED_CTLS:
  3437. *pdata = vmx_control_msr(
  3438. msrs->pinbased_ctls_low,
  3439. msrs->pinbased_ctls_high);
  3440. if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
  3441. *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  3442. break;
  3443. case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
  3444. case MSR_IA32_VMX_PROCBASED_CTLS:
  3445. *pdata = vmx_control_msr(
  3446. msrs->procbased_ctls_low,
  3447. msrs->procbased_ctls_high);
  3448. if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
  3449. *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  3450. break;
  3451. case MSR_IA32_VMX_TRUE_EXIT_CTLS:
  3452. case MSR_IA32_VMX_EXIT_CTLS:
  3453. *pdata = vmx_control_msr(
  3454. msrs->exit_ctls_low,
  3455. msrs->exit_ctls_high);
  3456. if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
  3457. *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
  3458. break;
  3459. case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
  3460. case MSR_IA32_VMX_ENTRY_CTLS:
  3461. *pdata = vmx_control_msr(
  3462. msrs->entry_ctls_low,
  3463. msrs->entry_ctls_high);
  3464. if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
  3465. *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
  3466. break;
  3467. case MSR_IA32_VMX_MISC:
  3468. *pdata = vmx_control_msr(
  3469. msrs->misc_low,
  3470. msrs->misc_high);
  3471. break;
  3472. case MSR_IA32_VMX_CR0_FIXED0:
  3473. *pdata = msrs->cr0_fixed0;
  3474. break;
  3475. case MSR_IA32_VMX_CR0_FIXED1:
  3476. *pdata = msrs->cr0_fixed1;
  3477. break;
  3478. case MSR_IA32_VMX_CR4_FIXED0:
  3479. *pdata = msrs->cr4_fixed0;
  3480. break;
  3481. case MSR_IA32_VMX_CR4_FIXED1:
  3482. *pdata = msrs->cr4_fixed1;
  3483. break;
  3484. case MSR_IA32_VMX_VMCS_ENUM:
  3485. *pdata = msrs->vmcs_enum;
  3486. break;
  3487. case MSR_IA32_VMX_PROCBASED_CTLS2:
  3488. *pdata = vmx_control_msr(
  3489. msrs->secondary_ctls_low,
  3490. msrs->secondary_ctls_high);
  3491. break;
  3492. case MSR_IA32_VMX_EPT_VPID_CAP:
  3493. *pdata = msrs->ept_caps |
  3494. ((u64)msrs->vpid_caps << 32);
  3495. break;
  3496. case MSR_IA32_VMX_VMFUNC:
  3497. *pdata = msrs->vmfunc_controls;
  3498. break;
  3499. default:
  3500. return 1;
  3501. }
  3502. return 0;
  3503. }
  3504. static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
  3505. uint64_t val)
  3506. {
  3507. uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
  3508. return !(val & ~valid_bits);
  3509. }
  3510. static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
  3511. {
  3512. switch (msr->index) {
  3513. case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
  3514. if (!nested)
  3515. return 1;
  3516. return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
  3517. default:
  3518. return 1;
  3519. }
  3520. return 0;
  3521. }
  3522. /*
  3523. * Reads an msr value (of 'msr_index') into 'pdata'.
  3524. * Returns 0 on success, non-0 otherwise.
  3525. * Assumes vcpu_load() was already called.
  3526. */
  3527. static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  3528. {
  3529. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3530. struct shared_msr_entry *msr;
  3531. switch (msr_info->index) {
  3532. #ifdef CONFIG_X86_64
  3533. case MSR_FS_BASE:
  3534. msr_info->data = vmcs_readl(GUEST_FS_BASE);
  3535. break;
  3536. case MSR_GS_BASE:
  3537. msr_info->data = vmcs_readl(GUEST_GS_BASE);
  3538. break;
  3539. case MSR_KERNEL_GS_BASE:
  3540. msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
  3541. break;
  3542. #endif
  3543. case MSR_EFER:
  3544. return kvm_get_msr_common(vcpu, msr_info);
  3545. case MSR_IA32_SPEC_CTRL:
  3546. if (!msr_info->host_initiated &&
  3547. !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
  3548. return 1;
  3549. msr_info->data = to_vmx(vcpu)->spec_ctrl;
  3550. break;
  3551. case MSR_IA32_ARCH_CAPABILITIES:
  3552. if (!msr_info->host_initiated &&
  3553. !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
  3554. return 1;
  3555. msr_info->data = to_vmx(vcpu)->arch_capabilities;
  3556. break;
  3557. case MSR_IA32_SYSENTER_CS:
  3558. msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
  3559. break;
  3560. case MSR_IA32_SYSENTER_EIP:
  3561. msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
  3562. break;
  3563. case MSR_IA32_SYSENTER_ESP:
  3564. msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
  3565. break;
  3566. case MSR_IA32_BNDCFGS:
  3567. if (!kvm_mpx_supported() ||
  3568. (!msr_info->host_initiated &&
  3569. !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
  3570. return 1;
  3571. msr_info->data = vmcs_read64(GUEST_BNDCFGS);
  3572. break;
  3573. case MSR_IA32_MCG_EXT_CTL:
  3574. if (!msr_info->host_initiated &&
  3575. !(vmx->msr_ia32_feature_control &
  3576. FEATURE_CONTROL_LMCE))
  3577. return 1;
  3578. msr_info->data = vcpu->arch.mcg_ext_ctl;
  3579. break;
  3580. case MSR_IA32_FEATURE_CONTROL:
  3581. msr_info->data = vmx->msr_ia32_feature_control;
  3582. break;
  3583. case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
  3584. if (!nested_vmx_allowed(vcpu))
  3585. return 1;
  3586. return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
  3587. &msr_info->data);
  3588. case MSR_IA32_XSS:
  3589. if (!vmx_xsaves_supported())
  3590. return 1;
  3591. msr_info->data = vcpu->arch.ia32_xss;
  3592. break;
  3593. case MSR_TSC_AUX:
  3594. if (!msr_info->host_initiated &&
  3595. !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
  3596. return 1;
  3597. /* Otherwise falls through */
  3598. default:
  3599. msr = find_msr_entry(vmx, msr_info->index);
  3600. if (msr) {
  3601. msr_info->data = msr->data;
  3602. break;
  3603. }
  3604. return kvm_get_msr_common(vcpu, msr_info);
  3605. }
  3606. return 0;
  3607. }
  3608. static void vmx_leave_nested(struct kvm_vcpu *vcpu);
  3609. /*
  3610. * Writes msr value into into the appropriate "register".
  3611. * Returns 0 on success, non-0 otherwise.
  3612. * Assumes vcpu_load() was already called.
  3613. */
  3614. static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  3615. {
  3616. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3617. struct shared_msr_entry *msr;
  3618. int ret = 0;
  3619. u32 msr_index = msr_info->index;
  3620. u64 data = msr_info->data;
  3621. switch (msr_index) {
  3622. case MSR_EFER:
  3623. ret = kvm_set_msr_common(vcpu, msr_info);
  3624. break;
  3625. #ifdef CONFIG_X86_64
  3626. case MSR_FS_BASE:
  3627. vmx_segment_cache_clear(vmx);
  3628. vmcs_writel(GUEST_FS_BASE, data);
  3629. break;
  3630. case MSR_GS_BASE:
  3631. vmx_segment_cache_clear(vmx);
  3632. vmcs_writel(GUEST_GS_BASE, data);
  3633. break;
  3634. case MSR_KERNEL_GS_BASE:
  3635. vmx_write_guest_kernel_gs_base(vmx, data);
  3636. break;
  3637. #endif
  3638. case MSR_IA32_SYSENTER_CS:
  3639. vmcs_write32(GUEST_SYSENTER_CS, data);
  3640. break;
  3641. case MSR_IA32_SYSENTER_EIP:
  3642. vmcs_writel(GUEST_SYSENTER_EIP, data);
  3643. break;
  3644. case MSR_IA32_SYSENTER_ESP:
  3645. vmcs_writel(GUEST_SYSENTER_ESP, data);
  3646. break;
  3647. case MSR_IA32_BNDCFGS:
  3648. if (!kvm_mpx_supported() ||
  3649. (!msr_info->host_initiated &&
  3650. !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
  3651. return 1;
  3652. if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
  3653. (data & MSR_IA32_BNDCFGS_RSVD))
  3654. return 1;
  3655. vmcs_write64(GUEST_BNDCFGS, data);
  3656. break;
  3657. case MSR_IA32_SPEC_CTRL:
  3658. if (!msr_info->host_initiated &&
  3659. !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
  3660. return 1;
  3661. /* The STIBP bit doesn't fault even if it's not advertised */
  3662. if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
  3663. return 1;
  3664. vmx->spec_ctrl = data;
  3665. if (!data)
  3666. break;
  3667. /*
  3668. * For non-nested:
  3669. * When it's written (to non-zero) for the first time, pass
  3670. * it through.
  3671. *
  3672. * For nested:
  3673. * The handling of the MSR bitmap for L2 guests is done in
  3674. * nested_vmx_merge_msr_bitmap. We should not touch the
  3675. * vmcs02.msr_bitmap here since it gets completely overwritten
  3676. * in the merging. We update the vmcs01 here for L1 as well
  3677. * since it will end up touching the MSR anyway now.
  3678. */
  3679. vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
  3680. MSR_IA32_SPEC_CTRL,
  3681. MSR_TYPE_RW);
  3682. break;
  3683. case MSR_IA32_PRED_CMD:
  3684. if (!msr_info->host_initiated &&
  3685. !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
  3686. return 1;
  3687. if (data & ~PRED_CMD_IBPB)
  3688. return 1;
  3689. if (!data)
  3690. break;
  3691. wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
  3692. /*
  3693. * For non-nested:
  3694. * When it's written (to non-zero) for the first time, pass
  3695. * it through.
  3696. *
  3697. * For nested:
  3698. * The handling of the MSR bitmap for L2 guests is done in
  3699. * nested_vmx_merge_msr_bitmap. We should not touch the
  3700. * vmcs02.msr_bitmap here since it gets completely overwritten
  3701. * in the merging.
  3702. */
  3703. vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
  3704. MSR_TYPE_W);
  3705. break;
  3706. case MSR_IA32_ARCH_CAPABILITIES:
  3707. if (!msr_info->host_initiated)
  3708. return 1;
  3709. vmx->arch_capabilities = data;
  3710. break;
  3711. case MSR_IA32_CR_PAT:
  3712. if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
  3713. if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
  3714. return 1;
  3715. vmcs_write64(GUEST_IA32_PAT, data);
  3716. vcpu->arch.pat = data;
  3717. break;
  3718. }
  3719. ret = kvm_set_msr_common(vcpu, msr_info);
  3720. break;
  3721. case MSR_IA32_TSC_ADJUST:
  3722. ret = kvm_set_msr_common(vcpu, msr_info);
  3723. break;
  3724. case MSR_IA32_MCG_EXT_CTL:
  3725. if ((!msr_info->host_initiated &&
  3726. !(to_vmx(vcpu)->msr_ia32_feature_control &
  3727. FEATURE_CONTROL_LMCE)) ||
  3728. (data & ~MCG_EXT_CTL_LMCE_EN))
  3729. return 1;
  3730. vcpu->arch.mcg_ext_ctl = data;
  3731. break;
  3732. case MSR_IA32_FEATURE_CONTROL:
  3733. if (!vmx_feature_control_msr_valid(vcpu, data) ||
  3734. (to_vmx(vcpu)->msr_ia32_feature_control &
  3735. FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
  3736. return 1;
  3737. vmx->msr_ia32_feature_control = data;
  3738. if (msr_info->host_initiated && data == 0)
  3739. vmx_leave_nested(vcpu);
  3740. break;
  3741. case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
  3742. if (!msr_info->host_initiated)
  3743. return 1; /* they are read-only */
  3744. if (!nested_vmx_allowed(vcpu))
  3745. return 1;
  3746. return vmx_set_vmx_msr(vcpu, msr_index, data);
  3747. case MSR_IA32_XSS:
  3748. if (!vmx_xsaves_supported())
  3749. return 1;
  3750. /*
  3751. * The only supported bit as of Skylake is bit 8, but
  3752. * it is not supported on KVM.
  3753. */
  3754. if (data != 0)
  3755. return 1;
  3756. vcpu->arch.ia32_xss = data;
  3757. if (vcpu->arch.ia32_xss != host_xss)
  3758. add_atomic_switch_msr(vmx, MSR_IA32_XSS,
  3759. vcpu->arch.ia32_xss, host_xss, false);
  3760. else
  3761. clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
  3762. break;
  3763. case MSR_TSC_AUX:
  3764. if (!msr_info->host_initiated &&
  3765. !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
  3766. return 1;
  3767. /* Check reserved bit, higher 32 bits should be zero */
  3768. if ((data >> 32) != 0)
  3769. return 1;
  3770. /* Otherwise falls through */
  3771. default:
  3772. msr = find_msr_entry(vmx, msr_index);
  3773. if (msr) {
  3774. u64 old_msr_data = msr->data;
  3775. msr->data = data;
  3776. if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
  3777. preempt_disable();
  3778. ret = kvm_set_shared_msr(msr->index, msr->data,
  3779. msr->mask);
  3780. preempt_enable();
  3781. if (ret)
  3782. msr->data = old_msr_data;
  3783. }
  3784. break;
  3785. }
  3786. ret = kvm_set_msr_common(vcpu, msr_info);
  3787. }
  3788. return ret;
  3789. }
  3790. static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
  3791. {
  3792. __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
  3793. switch (reg) {
  3794. case VCPU_REGS_RSP:
  3795. vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
  3796. break;
  3797. case VCPU_REGS_RIP:
  3798. vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
  3799. break;
  3800. case VCPU_EXREG_PDPTR:
  3801. if (enable_ept)
  3802. ept_save_pdptrs(vcpu);
  3803. break;
  3804. default:
  3805. break;
  3806. }
  3807. }
  3808. static __init int cpu_has_kvm_support(void)
  3809. {
  3810. return cpu_has_vmx();
  3811. }
  3812. static __init int vmx_disabled_by_bios(void)
  3813. {
  3814. u64 msr;
  3815. rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
  3816. if (msr & FEATURE_CONTROL_LOCKED) {
  3817. /* launched w/ TXT and VMX disabled */
  3818. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
  3819. && tboot_enabled())
  3820. return 1;
  3821. /* launched w/o TXT and VMX only enabled w/ TXT */
  3822. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
  3823. && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
  3824. && !tboot_enabled()) {
  3825. printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
  3826. "activate TXT before enabling KVM\n");
  3827. return 1;
  3828. }
  3829. /* launched w/o TXT and VMX disabled */
  3830. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
  3831. && !tboot_enabled())
  3832. return 1;
  3833. }
  3834. return 0;
  3835. }
  3836. static void kvm_cpu_vmxon(u64 addr)
  3837. {
  3838. cr4_set_bits(X86_CR4_VMXE);
  3839. intel_pt_handle_vmx(1);
  3840. asm volatile ("vmxon %0" : : "m"(addr));
  3841. }
  3842. static int hardware_enable(void)
  3843. {
  3844. int cpu = raw_smp_processor_id();
  3845. u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
  3846. u64 old, test_bits;
  3847. if (cr4_read_shadow() & X86_CR4_VMXE)
  3848. return -EBUSY;
  3849. /*
  3850. * This can happen if we hot-added a CPU but failed to allocate
  3851. * VP assist page for it.
  3852. */
  3853. if (static_branch_unlikely(&enable_evmcs) &&
  3854. !hv_get_vp_assist_page(cpu))
  3855. return -EFAULT;
  3856. INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
  3857. INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
  3858. spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
  3859. /*
  3860. * Now we can enable the vmclear operation in kdump
  3861. * since the loaded_vmcss_on_cpu list on this cpu
  3862. * has been initialized.
  3863. *
  3864. * Though the cpu is not in VMX operation now, there
  3865. * is no problem to enable the vmclear operation
  3866. * for the loaded_vmcss_on_cpu list is empty!
  3867. */
  3868. crash_enable_local_vmclear(cpu);
  3869. rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
  3870. test_bits = FEATURE_CONTROL_LOCKED;
  3871. test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  3872. if (tboot_enabled())
  3873. test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
  3874. if ((old & test_bits) != test_bits) {
  3875. /* enable and lock */
  3876. wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
  3877. }
  3878. kvm_cpu_vmxon(phys_addr);
  3879. if (enable_ept)
  3880. ept_sync_global();
  3881. return 0;
  3882. }
  3883. static void vmclear_local_loaded_vmcss(void)
  3884. {
  3885. int cpu = raw_smp_processor_id();
  3886. struct loaded_vmcs *v, *n;
  3887. list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
  3888. loaded_vmcss_on_cpu_link)
  3889. __loaded_vmcs_clear(v);
  3890. }
  3891. /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
  3892. * tricks.
  3893. */
  3894. static void kvm_cpu_vmxoff(void)
  3895. {
  3896. asm volatile (__ex("vmxoff"));
  3897. intel_pt_handle_vmx(0);
  3898. cr4_clear_bits(X86_CR4_VMXE);
  3899. }
  3900. static void hardware_disable(void)
  3901. {
  3902. vmclear_local_loaded_vmcss();
  3903. kvm_cpu_vmxoff();
  3904. }
  3905. static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
  3906. u32 msr, u32 *result)
  3907. {
  3908. u32 vmx_msr_low, vmx_msr_high;
  3909. u32 ctl = ctl_min | ctl_opt;
  3910. rdmsr(msr, vmx_msr_low, vmx_msr_high);
  3911. ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
  3912. ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
  3913. /* Ensure minimum (required) set of control bits are supported. */
  3914. if (ctl_min & ~ctl)
  3915. return -EIO;
  3916. *result = ctl;
  3917. return 0;
  3918. }
  3919. static __init bool allow_1_setting(u32 msr, u32 ctl)
  3920. {
  3921. u32 vmx_msr_low, vmx_msr_high;
  3922. rdmsr(msr, vmx_msr_low, vmx_msr_high);
  3923. return vmx_msr_high & ctl;
  3924. }
  3925. static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
  3926. {
  3927. u32 vmx_msr_low, vmx_msr_high;
  3928. u32 min, opt, min2, opt2;
  3929. u32 _pin_based_exec_control = 0;
  3930. u32 _cpu_based_exec_control = 0;
  3931. u32 _cpu_based_2nd_exec_control = 0;
  3932. u32 _vmexit_control = 0;
  3933. u32 _vmentry_control = 0;
  3934. memset(vmcs_conf, 0, sizeof(*vmcs_conf));
  3935. min = CPU_BASED_HLT_EXITING |
  3936. #ifdef CONFIG_X86_64
  3937. CPU_BASED_CR8_LOAD_EXITING |
  3938. CPU_BASED_CR8_STORE_EXITING |
  3939. #endif
  3940. CPU_BASED_CR3_LOAD_EXITING |
  3941. CPU_BASED_CR3_STORE_EXITING |
  3942. CPU_BASED_UNCOND_IO_EXITING |
  3943. CPU_BASED_MOV_DR_EXITING |
  3944. CPU_BASED_USE_TSC_OFFSETING |
  3945. CPU_BASED_MWAIT_EXITING |
  3946. CPU_BASED_MONITOR_EXITING |
  3947. CPU_BASED_INVLPG_EXITING |
  3948. CPU_BASED_RDPMC_EXITING;
  3949. opt = CPU_BASED_TPR_SHADOW |
  3950. CPU_BASED_USE_MSR_BITMAPS |
  3951. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  3952. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
  3953. &_cpu_based_exec_control) < 0)
  3954. return -EIO;
  3955. #ifdef CONFIG_X86_64
  3956. if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
  3957. _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
  3958. ~CPU_BASED_CR8_STORE_EXITING;
  3959. #endif
  3960. if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
  3961. min2 = 0;
  3962. opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  3963. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  3964. SECONDARY_EXEC_WBINVD_EXITING |
  3965. SECONDARY_EXEC_ENABLE_VPID |
  3966. SECONDARY_EXEC_ENABLE_EPT |
  3967. SECONDARY_EXEC_UNRESTRICTED_GUEST |
  3968. SECONDARY_EXEC_PAUSE_LOOP_EXITING |
  3969. SECONDARY_EXEC_DESC |
  3970. SECONDARY_EXEC_RDTSCP |
  3971. SECONDARY_EXEC_ENABLE_INVPCID |
  3972. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  3973. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
  3974. SECONDARY_EXEC_SHADOW_VMCS |
  3975. SECONDARY_EXEC_XSAVES |
  3976. SECONDARY_EXEC_RDSEED_EXITING |
  3977. SECONDARY_EXEC_RDRAND_EXITING |
  3978. SECONDARY_EXEC_ENABLE_PML |
  3979. SECONDARY_EXEC_TSC_SCALING |
  3980. SECONDARY_EXEC_ENABLE_VMFUNC |
  3981. SECONDARY_EXEC_ENCLS_EXITING;
  3982. if (adjust_vmx_controls(min2, opt2,
  3983. MSR_IA32_VMX_PROCBASED_CTLS2,
  3984. &_cpu_based_2nd_exec_control) < 0)
  3985. return -EIO;
  3986. }
  3987. #ifndef CONFIG_X86_64
  3988. if (!(_cpu_based_2nd_exec_control &
  3989. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
  3990. _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
  3991. #endif
  3992. if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
  3993. _cpu_based_2nd_exec_control &= ~(
  3994. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  3995. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  3996. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  3997. rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
  3998. &vmx_capability.ept, &vmx_capability.vpid);
  3999. if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
  4000. /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
  4001. enabled */
  4002. _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
  4003. CPU_BASED_CR3_STORE_EXITING |
  4004. CPU_BASED_INVLPG_EXITING);
  4005. } else if (vmx_capability.ept) {
  4006. vmx_capability.ept = 0;
  4007. pr_warn_once("EPT CAP should not exist if not support "
  4008. "1-setting enable EPT VM-execution control\n");
  4009. }
  4010. if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
  4011. vmx_capability.vpid) {
  4012. vmx_capability.vpid = 0;
  4013. pr_warn_once("VPID CAP should not exist if not support "
  4014. "1-setting enable VPID VM-execution control\n");
  4015. }
  4016. min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
  4017. #ifdef CONFIG_X86_64
  4018. min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
  4019. #endif
  4020. opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
  4021. VM_EXIT_CLEAR_BNDCFGS;
  4022. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
  4023. &_vmexit_control) < 0)
  4024. return -EIO;
  4025. min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
  4026. opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
  4027. PIN_BASED_VMX_PREEMPTION_TIMER;
  4028. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
  4029. &_pin_based_exec_control) < 0)
  4030. return -EIO;
  4031. if (cpu_has_broken_vmx_preemption_timer())
  4032. _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  4033. if (!(_cpu_based_2nd_exec_control &
  4034. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
  4035. _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
  4036. min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
  4037. opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
  4038. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
  4039. &_vmentry_control) < 0)
  4040. return -EIO;
  4041. rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
  4042. /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
  4043. if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
  4044. return -EIO;
  4045. #ifdef CONFIG_X86_64
  4046. /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
  4047. if (vmx_msr_high & (1u<<16))
  4048. return -EIO;
  4049. #endif
  4050. /* Require Write-Back (WB) memory type for VMCS accesses. */
  4051. if (((vmx_msr_high >> 18) & 15) != 6)
  4052. return -EIO;
  4053. vmcs_conf->size = vmx_msr_high & 0x1fff;
  4054. vmcs_conf->order = get_order(vmcs_conf->size);
  4055. vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
  4056. vmcs_conf->revision_id = vmx_msr_low;
  4057. vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
  4058. vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
  4059. vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
  4060. vmcs_conf->vmexit_ctrl = _vmexit_control;
  4061. vmcs_conf->vmentry_ctrl = _vmentry_control;
  4062. if (static_branch_unlikely(&enable_evmcs))
  4063. evmcs_sanitize_exec_ctrls(vmcs_conf);
  4064. cpu_has_load_ia32_efer =
  4065. allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
  4066. VM_ENTRY_LOAD_IA32_EFER)
  4067. && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
  4068. VM_EXIT_LOAD_IA32_EFER);
  4069. cpu_has_load_perf_global_ctrl =
  4070. allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
  4071. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
  4072. && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
  4073. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
  4074. /*
  4075. * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
  4076. * but due to errata below it can't be used. Workaround is to use
  4077. * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
  4078. *
  4079. * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
  4080. *
  4081. * AAK155 (model 26)
  4082. * AAP115 (model 30)
  4083. * AAT100 (model 37)
  4084. * BC86,AAY89,BD102 (model 44)
  4085. * BA97 (model 46)
  4086. *
  4087. */
  4088. if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
  4089. switch (boot_cpu_data.x86_model) {
  4090. case 26:
  4091. case 30:
  4092. case 37:
  4093. case 44:
  4094. case 46:
  4095. cpu_has_load_perf_global_ctrl = false;
  4096. printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
  4097. "does not work properly. Using workaround\n");
  4098. break;
  4099. default:
  4100. break;
  4101. }
  4102. }
  4103. if (boot_cpu_has(X86_FEATURE_XSAVES))
  4104. rdmsrl(MSR_IA32_XSS, host_xss);
  4105. return 0;
  4106. }
  4107. static struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu)
  4108. {
  4109. int node = cpu_to_node(cpu);
  4110. struct page *pages;
  4111. struct vmcs *vmcs;
  4112. pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
  4113. if (!pages)
  4114. return NULL;
  4115. vmcs = page_address(pages);
  4116. memset(vmcs, 0, vmcs_config.size);
  4117. /* KVM supports Enlightened VMCS v1 only */
  4118. if (static_branch_unlikely(&enable_evmcs))
  4119. vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
  4120. else
  4121. vmcs->hdr.revision_id = vmcs_config.revision_id;
  4122. if (shadow)
  4123. vmcs->hdr.shadow_vmcs = 1;
  4124. return vmcs;
  4125. }
  4126. static void free_vmcs(struct vmcs *vmcs)
  4127. {
  4128. free_pages((unsigned long)vmcs, vmcs_config.order);
  4129. }
  4130. /*
  4131. * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
  4132. */
  4133. static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
  4134. {
  4135. if (!loaded_vmcs->vmcs)
  4136. return;
  4137. loaded_vmcs_clear(loaded_vmcs);
  4138. free_vmcs(loaded_vmcs->vmcs);
  4139. loaded_vmcs->vmcs = NULL;
  4140. if (loaded_vmcs->msr_bitmap)
  4141. free_page((unsigned long)loaded_vmcs->msr_bitmap);
  4142. WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
  4143. }
  4144. static struct vmcs *alloc_vmcs(bool shadow)
  4145. {
  4146. return alloc_vmcs_cpu(shadow, raw_smp_processor_id());
  4147. }
  4148. static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
  4149. {
  4150. loaded_vmcs->vmcs = alloc_vmcs(false);
  4151. if (!loaded_vmcs->vmcs)
  4152. return -ENOMEM;
  4153. loaded_vmcs->shadow_vmcs = NULL;
  4154. loaded_vmcs_init(loaded_vmcs);
  4155. if (cpu_has_vmx_msr_bitmap()) {
  4156. loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
  4157. if (!loaded_vmcs->msr_bitmap)
  4158. goto out_vmcs;
  4159. memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
  4160. if (IS_ENABLED(CONFIG_HYPERV) &&
  4161. static_branch_unlikely(&enable_evmcs) &&
  4162. (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
  4163. struct hv_enlightened_vmcs *evmcs =
  4164. (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
  4165. evmcs->hv_enlightenments_control.msr_bitmap = 1;
  4166. }
  4167. }
  4168. memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state));
  4169. return 0;
  4170. out_vmcs:
  4171. free_loaded_vmcs(loaded_vmcs);
  4172. return -ENOMEM;
  4173. }
  4174. static void free_kvm_area(void)
  4175. {
  4176. int cpu;
  4177. for_each_possible_cpu(cpu) {
  4178. free_vmcs(per_cpu(vmxarea, cpu));
  4179. per_cpu(vmxarea, cpu) = NULL;
  4180. }
  4181. }
  4182. enum vmcs_field_width {
  4183. VMCS_FIELD_WIDTH_U16 = 0,
  4184. VMCS_FIELD_WIDTH_U64 = 1,
  4185. VMCS_FIELD_WIDTH_U32 = 2,
  4186. VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
  4187. };
  4188. static inline int vmcs_field_width(unsigned long field)
  4189. {
  4190. if (0x1 & field) /* the *_HIGH fields are all 32 bit */
  4191. return VMCS_FIELD_WIDTH_U32;
  4192. return (field >> 13) & 0x3 ;
  4193. }
  4194. static inline int vmcs_field_readonly(unsigned long field)
  4195. {
  4196. return (((field >> 10) & 0x3) == 1);
  4197. }
  4198. static void init_vmcs_shadow_fields(void)
  4199. {
  4200. int i, j;
  4201. for (i = j = 0; i < max_shadow_read_only_fields; i++) {
  4202. u16 field = shadow_read_only_fields[i];
  4203. if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
  4204. (i + 1 == max_shadow_read_only_fields ||
  4205. shadow_read_only_fields[i + 1] != field + 1))
  4206. pr_err("Missing field from shadow_read_only_field %x\n",
  4207. field + 1);
  4208. clear_bit(field, vmx_vmread_bitmap);
  4209. #ifdef CONFIG_X86_64
  4210. if (field & 1)
  4211. continue;
  4212. #endif
  4213. if (j < i)
  4214. shadow_read_only_fields[j] = field;
  4215. j++;
  4216. }
  4217. max_shadow_read_only_fields = j;
  4218. for (i = j = 0; i < max_shadow_read_write_fields; i++) {
  4219. u16 field = shadow_read_write_fields[i];
  4220. if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
  4221. (i + 1 == max_shadow_read_write_fields ||
  4222. shadow_read_write_fields[i + 1] != field + 1))
  4223. pr_err("Missing field from shadow_read_write_field %x\n",
  4224. field + 1);
  4225. /*
  4226. * PML and the preemption timer can be emulated, but the
  4227. * processor cannot vmwrite to fields that don't exist
  4228. * on bare metal.
  4229. */
  4230. switch (field) {
  4231. case GUEST_PML_INDEX:
  4232. if (!cpu_has_vmx_pml())
  4233. continue;
  4234. break;
  4235. case VMX_PREEMPTION_TIMER_VALUE:
  4236. if (!cpu_has_vmx_preemption_timer())
  4237. continue;
  4238. break;
  4239. case GUEST_INTR_STATUS:
  4240. if (!cpu_has_vmx_apicv())
  4241. continue;
  4242. break;
  4243. default:
  4244. break;
  4245. }
  4246. clear_bit(field, vmx_vmwrite_bitmap);
  4247. clear_bit(field, vmx_vmread_bitmap);
  4248. #ifdef CONFIG_X86_64
  4249. if (field & 1)
  4250. continue;
  4251. #endif
  4252. if (j < i)
  4253. shadow_read_write_fields[j] = field;
  4254. j++;
  4255. }
  4256. max_shadow_read_write_fields = j;
  4257. }
  4258. static __init int alloc_kvm_area(void)
  4259. {
  4260. int cpu;
  4261. for_each_possible_cpu(cpu) {
  4262. struct vmcs *vmcs;
  4263. vmcs = alloc_vmcs_cpu(false, cpu);
  4264. if (!vmcs) {
  4265. free_kvm_area();
  4266. return -ENOMEM;
  4267. }
  4268. /*
  4269. * When eVMCS is enabled, alloc_vmcs_cpu() sets
  4270. * vmcs->revision_id to KVM_EVMCS_VERSION instead of
  4271. * revision_id reported by MSR_IA32_VMX_BASIC.
  4272. *
  4273. * However, even though not explictly documented by
  4274. * TLFS, VMXArea passed as VMXON argument should
  4275. * still be marked with revision_id reported by
  4276. * physical CPU.
  4277. */
  4278. if (static_branch_unlikely(&enable_evmcs))
  4279. vmcs->hdr.revision_id = vmcs_config.revision_id;
  4280. per_cpu(vmxarea, cpu) = vmcs;
  4281. }
  4282. return 0;
  4283. }
  4284. static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
  4285. struct kvm_segment *save)
  4286. {
  4287. if (!emulate_invalid_guest_state) {
  4288. /*
  4289. * CS and SS RPL should be equal during guest entry according
  4290. * to VMX spec, but in reality it is not always so. Since vcpu
  4291. * is in the middle of the transition from real mode to
  4292. * protected mode it is safe to assume that RPL 0 is a good
  4293. * default value.
  4294. */
  4295. if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
  4296. save->selector &= ~SEGMENT_RPL_MASK;
  4297. save->dpl = save->selector & SEGMENT_RPL_MASK;
  4298. save->s = 1;
  4299. }
  4300. vmx_set_segment(vcpu, save, seg);
  4301. }
  4302. static void enter_pmode(struct kvm_vcpu *vcpu)
  4303. {
  4304. unsigned long flags;
  4305. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4306. /*
  4307. * Update real mode segment cache. It may be not up-to-date if sement
  4308. * register was written while vcpu was in a guest mode.
  4309. */
  4310. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
  4311. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
  4312. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
  4313. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
  4314. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
  4315. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
  4316. vmx->rmode.vm86_active = 0;
  4317. vmx_segment_cache_clear(vmx);
  4318. vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
  4319. flags = vmcs_readl(GUEST_RFLAGS);
  4320. flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
  4321. flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
  4322. vmcs_writel(GUEST_RFLAGS, flags);
  4323. vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
  4324. (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
  4325. update_exception_bitmap(vcpu);
  4326. fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
  4327. fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
  4328. fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
  4329. fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
  4330. fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
  4331. fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
  4332. }
  4333. static void fix_rmode_seg(int seg, struct kvm_segment *save)
  4334. {
  4335. const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  4336. struct kvm_segment var = *save;
  4337. var.dpl = 0x3;
  4338. if (seg == VCPU_SREG_CS)
  4339. var.type = 0x3;
  4340. if (!emulate_invalid_guest_state) {
  4341. var.selector = var.base >> 4;
  4342. var.base = var.base & 0xffff0;
  4343. var.limit = 0xffff;
  4344. var.g = 0;
  4345. var.db = 0;
  4346. var.present = 1;
  4347. var.s = 1;
  4348. var.l = 0;
  4349. var.unusable = 0;
  4350. var.type = 0x3;
  4351. var.avl = 0;
  4352. if (save->base & 0xf)
  4353. printk_once(KERN_WARNING "kvm: segment base is not "
  4354. "paragraph aligned when entering "
  4355. "protected mode (seg=%d)", seg);
  4356. }
  4357. vmcs_write16(sf->selector, var.selector);
  4358. vmcs_writel(sf->base, var.base);
  4359. vmcs_write32(sf->limit, var.limit);
  4360. vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
  4361. }
  4362. static void enter_rmode(struct kvm_vcpu *vcpu)
  4363. {
  4364. unsigned long flags;
  4365. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4366. struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
  4367. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
  4368. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
  4369. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
  4370. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
  4371. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
  4372. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
  4373. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
  4374. vmx->rmode.vm86_active = 1;
  4375. /*
  4376. * Very old userspace does not call KVM_SET_TSS_ADDR before entering
  4377. * vcpu. Warn the user that an update is overdue.
  4378. */
  4379. if (!kvm_vmx->tss_addr)
  4380. printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
  4381. "called before entering vcpu\n");
  4382. vmx_segment_cache_clear(vmx);
  4383. vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
  4384. vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
  4385. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  4386. flags = vmcs_readl(GUEST_RFLAGS);
  4387. vmx->rmode.save_rflags = flags;
  4388. flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
  4389. vmcs_writel(GUEST_RFLAGS, flags);
  4390. vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
  4391. update_exception_bitmap(vcpu);
  4392. fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
  4393. fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
  4394. fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
  4395. fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
  4396. fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
  4397. fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
  4398. kvm_mmu_reset_context(vcpu);
  4399. }
  4400. static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
  4401. {
  4402. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4403. struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
  4404. if (!msr)
  4405. return;
  4406. vcpu->arch.efer = efer;
  4407. if (efer & EFER_LMA) {
  4408. vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
  4409. msr->data = efer;
  4410. } else {
  4411. vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
  4412. msr->data = efer & ~EFER_LME;
  4413. }
  4414. setup_msrs(vmx);
  4415. }
  4416. #ifdef CONFIG_X86_64
  4417. static void enter_lmode(struct kvm_vcpu *vcpu)
  4418. {
  4419. u32 guest_tr_ar;
  4420. vmx_segment_cache_clear(to_vmx(vcpu));
  4421. guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
  4422. if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
  4423. pr_debug_ratelimited("%s: tss fixup for long mode. \n",
  4424. __func__);
  4425. vmcs_write32(GUEST_TR_AR_BYTES,
  4426. (guest_tr_ar & ~VMX_AR_TYPE_MASK)
  4427. | VMX_AR_TYPE_BUSY_64_TSS);
  4428. }
  4429. vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
  4430. }
  4431. static void exit_lmode(struct kvm_vcpu *vcpu)
  4432. {
  4433. vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
  4434. vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
  4435. }
  4436. #endif
  4437. static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
  4438. bool invalidate_gpa)
  4439. {
  4440. if (enable_ept && (invalidate_gpa || !enable_vpid)) {
  4441. if (!VALID_PAGE(vcpu->arch.mmu->root_hpa))
  4442. return;
  4443. ept_sync_context(construct_eptp(vcpu,
  4444. vcpu->arch.mmu->root_hpa));
  4445. } else {
  4446. vpid_sync_context(vpid);
  4447. }
  4448. }
  4449. static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
  4450. {
  4451. __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
  4452. }
  4453. static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
  4454. {
  4455. int vpid = to_vmx(vcpu)->vpid;
  4456. if (!vpid_sync_vcpu_addr(vpid, addr))
  4457. vpid_sync_context(vpid);
  4458. /*
  4459. * If VPIDs are not supported or enabled, then the above is a no-op.
  4460. * But we don't really need a TLB flush in that case anyway, because
  4461. * each VM entry/exit includes an implicit flush when VPID is 0.
  4462. */
  4463. }
  4464. static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
  4465. {
  4466. ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
  4467. vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
  4468. vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
  4469. }
  4470. static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
  4471. {
  4472. if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
  4473. vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
  4474. __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
  4475. }
  4476. static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
  4477. {
  4478. ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
  4479. vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
  4480. vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
  4481. }
  4482. static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
  4483. {
  4484. struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
  4485. if (!test_bit(VCPU_EXREG_PDPTR,
  4486. (unsigned long *)&vcpu->arch.regs_dirty))
  4487. return;
  4488. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  4489. vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
  4490. vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
  4491. vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
  4492. vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
  4493. }
  4494. }
  4495. static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
  4496. {
  4497. struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
  4498. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  4499. mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
  4500. mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
  4501. mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
  4502. mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
  4503. }
  4504. __set_bit(VCPU_EXREG_PDPTR,
  4505. (unsigned long *)&vcpu->arch.regs_avail);
  4506. __set_bit(VCPU_EXREG_PDPTR,
  4507. (unsigned long *)&vcpu->arch.regs_dirty);
  4508. }
  4509. static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
  4510. {
  4511. u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
  4512. u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
  4513. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  4514. if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
  4515. SECONDARY_EXEC_UNRESTRICTED_GUEST &&
  4516. nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
  4517. fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
  4518. return fixed_bits_valid(val, fixed0, fixed1);
  4519. }
  4520. static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
  4521. {
  4522. u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
  4523. u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
  4524. return fixed_bits_valid(val, fixed0, fixed1);
  4525. }
  4526. static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
  4527. {
  4528. u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
  4529. u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
  4530. return fixed_bits_valid(val, fixed0, fixed1);
  4531. }
  4532. /* No difference in the restrictions on guest and host CR4 in VMX operation. */
  4533. #define nested_guest_cr4_valid nested_cr4_valid
  4534. #define nested_host_cr4_valid nested_cr4_valid
  4535. static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
  4536. static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
  4537. unsigned long cr0,
  4538. struct kvm_vcpu *vcpu)
  4539. {
  4540. if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
  4541. vmx_decache_cr3(vcpu);
  4542. if (!(cr0 & X86_CR0_PG)) {
  4543. /* From paging/starting to nonpaging */
  4544. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  4545. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
  4546. (CPU_BASED_CR3_LOAD_EXITING |
  4547. CPU_BASED_CR3_STORE_EXITING));
  4548. vcpu->arch.cr0 = cr0;
  4549. vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
  4550. } else if (!is_paging(vcpu)) {
  4551. /* From nonpaging to paging */
  4552. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  4553. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
  4554. ~(CPU_BASED_CR3_LOAD_EXITING |
  4555. CPU_BASED_CR3_STORE_EXITING));
  4556. vcpu->arch.cr0 = cr0;
  4557. vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
  4558. }
  4559. if (!(cr0 & X86_CR0_WP))
  4560. *hw_cr0 &= ~X86_CR0_WP;
  4561. }
  4562. static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  4563. {
  4564. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4565. unsigned long hw_cr0;
  4566. hw_cr0 = (cr0 & ~KVM_VM_CR0_ALWAYS_OFF);
  4567. if (enable_unrestricted_guest)
  4568. hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
  4569. else {
  4570. hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
  4571. if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
  4572. enter_pmode(vcpu);
  4573. if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
  4574. enter_rmode(vcpu);
  4575. }
  4576. #ifdef CONFIG_X86_64
  4577. if (vcpu->arch.efer & EFER_LME) {
  4578. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
  4579. enter_lmode(vcpu);
  4580. if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
  4581. exit_lmode(vcpu);
  4582. }
  4583. #endif
  4584. if (enable_ept && !enable_unrestricted_guest)
  4585. ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
  4586. vmcs_writel(CR0_READ_SHADOW, cr0);
  4587. vmcs_writel(GUEST_CR0, hw_cr0);
  4588. vcpu->arch.cr0 = cr0;
  4589. /* depends on vcpu->arch.cr0 to be set to a new value */
  4590. vmx->emulation_required = emulation_required(vcpu);
  4591. }
  4592. static int get_ept_level(struct kvm_vcpu *vcpu)
  4593. {
  4594. if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
  4595. return 5;
  4596. return 4;
  4597. }
  4598. static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
  4599. {
  4600. u64 eptp = VMX_EPTP_MT_WB;
  4601. eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
  4602. if (enable_ept_ad_bits &&
  4603. (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
  4604. eptp |= VMX_EPTP_AD_ENABLE_BIT;
  4605. eptp |= (root_hpa & PAGE_MASK);
  4606. return eptp;
  4607. }
  4608. static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
  4609. {
  4610. struct kvm *kvm = vcpu->kvm;
  4611. unsigned long guest_cr3;
  4612. u64 eptp;
  4613. guest_cr3 = cr3;
  4614. if (enable_ept) {
  4615. eptp = construct_eptp(vcpu, cr3);
  4616. vmcs_write64(EPT_POINTER, eptp);
  4617. if (kvm_x86_ops->tlb_remote_flush) {
  4618. spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
  4619. to_vmx(vcpu)->ept_pointer = eptp;
  4620. to_kvm_vmx(kvm)->ept_pointers_match
  4621. = EPT_POINTERS_CHECK;
  4622. spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
  4623. }
  4624. if (enable_unrestricted_guest || is_paging(vcpu) ||
  4625. is_guest_mode(vcpu))
  4626. guest_cr3 = kvm_read_cr3(vcpu);
  4627. else
  4628. guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
  4629. ept_load_pdptrs(vcpu);
  4630. }
  4631. vmcs_writel(GUEST_CR3, guest_cr3);
  4632. }
  4633. static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  4634. {
  4635. /*
  4636. * Pass through host's Machine Check Enable value to hw_cr4, which
  4637. * is in force while we are in guest mode. Do not let guests control
  4638. * this bit, even if host CR4.MCE == 0.
  4639. */
  4640. unsigned long hw_cr4;
  4641. hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
  4642. if (enable_unrestricted_guest)
  4643. hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
  4644. else if (to_vmx(vcpu)->rmode.vm86_active)
  4645. hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
  4646. else
  4647. hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
  4648. if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
  4649. if (cr4 & X86_CR4_UMIP) {
  4650. vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
  4651. SECONDARY_EXEC_DESC);
  4652. hw_cr4 &= ~X86_CR4_UMIP;
  4653. } else if (!is_guest_mode(vcpu) ||
  4654. !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
  4655. vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
  4656. SECONDARY_EXEC_DESC);
  4657. }
  4658. if (cr4 & X86_CR4_VMXE) {
  4659. /*
  4660. * To use VMXON (and later other VMX instructions), a guest
  4661. * must first be able to turn on cr4.VMXE (see handle_vmon()).
  4662. * So basically the check on whether to allow nested VMX
  4663. * is here. We operate under the default treatment of SMM,
  4664. * so VMX cannot be enabled under SMM.
  4665. */
  4666. if (!nested_vmx_allowed(vcpu) || is_smm(vcpu))
  4667. return 1;
  4668. }
  4669. if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
  4670. return 1;
  4671. vcpu->arch.cr4 = cr4;
  4672. if (!enable_unrestricted_guest) {
  4673. if (enable_ept) {
  4674. if (!is_paging(vcpu)) {
  4675. hw_cr4 &= ~X86_CR4_PAE;
  4676. hw_cr4 |= X86_CR4_PSE;
  4677. } else if (!(cr4 & X86_CR4_PAE)) {
  4678. hw_cr4 &= ~X86_CR4_PAE;
  4679. }
  4680. }
  4681. /*
  4682. * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
  4683. * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
  4684. * to be manually disabled when guest switches to non-paging
  4685. * mode.
  4686. *
  4687. * If !enable_unrestricted_guest, the CPU is always running
  4688. * with CR0.PG=1 and CR4 needs to be modified.
  4689. * If enable_unrestricted_guest, the CPU automatically
  4690. * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
  4691. */
  4692. if (!is_paging(vcpu))
  4693. hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
  4694. }
  4695. vmcs_writel(CR4_READ_SHADOW, cr4);
  4696. vmcs_writel(GUEST_CR4, hw_cr4);
  4697. return 0;
  4698. }
  4699. static void vmx_get_segment(struct kvm_vcpu *vcpu,
  4700. struct kvm_segment *var, int seg)
  4701. {
  4702. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4703. u32 ar;
  4704. if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
  4705. *var = vmx->rmode.segs[seg];
  4706. if (seg == VCPU_SREG_TR
  4707. || var->selector == vmx_read_guest_seg_selector(vmx, seg))
  4708. return;
  4709. var->base = vmx_read_guest_seg_base(vmx, seg);
  4710. var->selector = vmx_read_guest_seg_selector(vmx, seg);
  4711. return;
  4712. }
  4713. var->base = vmx_read_guest_seg_base(vmx, seg);
  4714. var->limit = vmx_read_guest_seg_limit(vmx, seg);
  4715. var->selector = vmx_read_guest_seg_selector(vmx, seg);
  4716. ar = vmx_read_guest_seg_ar(vmx, seg);
  4717. var->unusable = (ar >> 16) & 1;
  4718. var->type = ar & 15;
  4719. var->s = (ar >> 4) & 1;
  4720. var->dpl = (ar >> 5) & 3;
  4721. /*
  4722. * Some userspaces do not preserve unusable property. Since usable
  4723. * segment has to be present according to VMX spec we can use present
  4724. * property to amend userspace bug by making unusable segment always
  4725. * nonpresent. vmx_segment_access_rights() already marks nonpresent
  4726. * segment as unusable.
  4727. */
  4728. var->present = !var->unusable;
  4729. var->avl = (ar >> 12) & 1;
  4730. var->l = (ar >> 13) & 1;
  4731. var->db = (ar >> 14) & 1;
  4732. var->g = (ar >> 15) & 1;
  4733. }
  4734. static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
  4735. {
  4736. struct kvm_segment s;
  4737. if (to_vmx(vcpu)->rmode.vm86_active) {
  4738. vmx_get_segment(vcpu, &s, seg);
  4739. return s.base;
  4740. }
  4741. return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
  4742. }
  4743. static int vmx_get_cpl(struct kvm_vcpu *vcpu)
  4744. {
  4745. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4746. if (unlikely(vmx->rmode.vm86_active))
  4747. return 0;
  4748. else {
  4749. int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
  4750. return VMX_AR_DPL(ar);
  4751. }
  4752. }
  4753. static u32 vmx_segment_access_rights(struct kvm_segment *var)
  4754. {
  4755. u32 ar;
  4756. if (var->unusable || !var->present)
  4757. ar = 1 << 16;
  4758. else {
  4759. ar = var->type & 15;
  4760. ar |= (var->s & 1) << 4;
  4761. ar |= (var->dpl & 3) << 5;
  4762. ar |= (var->present & 1) << 7;
  4763. ar |= (var->avl & 1) << 12;
  4764. ar |= (var->l & 1) << 13;
  4765. ar |= (var->db & 1) << 14;
  4766. ar |= (var->g & 1) << 15;
  4767. }
  4768. return ar;
  4769. }
  4770. static void vmx_set_segment(struct kvm_vcpu *vcpu,
  4771. struct kvm_segment *var, int seg)
  4772. {
  4773. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4774. const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  4775. vmx_segment_cache_clear(vmx);
  4776. if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
  4777. vmx->rmode.segs[seg] = *var;
  4778. if (seg == VCPU_SREG_TR)
  4779. vmcs_write16(sf->selector, var->selector);
  4780. else if (var->s)
  4781. fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
  4782. goto out;
  4783. }
  4784. vmcs_writel(sf->base, var->base);
  4785. vmcs_write32(sf->limit, var->limit);
  4786. vmcs_write16(sf->selector, var->selector);
  4787. /*
  4788. * Fix the "Accessed" bit in AR field of segment registers for older
  4789. * qemu binaries.
  4790. * IA32 arch specifies that at the time of processor reset the
  4791. * "Accessed" bit in the AR field of segment registers is 1. And qemu
  4792. * is setting it to 0 in the userland code. This causes invalid guest
  4793. * state vmexit when "unrestricted guest" mode is turned on.
  4794. * Fix for this setup issue in cpu_reset is being pushed in the qemu
  4795. * tree. Newer qemu binaries with that qemu fix would not need this
  4796. * kvm hack.
  4797. */
  4798. if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
  4799. var->type |= 0x1; /* Accessed */
  4800. vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
  4801. out:
  4802. vmx->emulation_required = emulation_required(vcpu);
  4803. }
  4804. static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
  4805. {
  4806. u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
  4807. *db = (ar >> 14) & 1;
  4808. *l = (ar >> 13) & 1;
  4809. }
  4810. static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  4811. {
  4812. dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
  4813. dt->address = vmcs_readl(GUEST_IDTR_BASE);
  4814. }
  4815. static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  4816. {
  4817. vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
  4818. vmcs_writel(GUEST_IDTR_BASE, dt->address);
  4819. }
  4820. static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  4821. {
  4822. dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
  4823. dt->address = vmcs_readl(GUEST_GDTR_BASE);
  4824. }
  4825. static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  4826. {
  4827. vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
  4828. vmcs_writel(GUEST_GDTR_BASE, dt->address);
  4829. }
  4830. static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
  4831. {
  4832. struct kvm_segment var;
  4833. u32 ar;
  4834. vmx_get_segment(vcpu, &var, seg);
  4835. var.dpl = 0x3;
  4836. if (seg == VCPU_SREG_CS)
  4837. var.type = 0x3;
  4838. ar = vmx_segment_access_rights(&var);
  4839. if (var.base != (var.selector << 4))
  4840. return false;
  4841. if (var.limit != 0xffff)
  4842. return false;
  4843. if (ar != 0xf3)
  4844. return false;
  4845. return true;
  4846. }
  4847. static bool code_segment_valid(struct kvm_vcpu *vcpu)
  4848. {
  4849. struct kvm_segment cs;
  4850. unsigned int cs_rpl;
  4851. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  4852. cs_rpl = cs.selector & SEGMENT_RPL_MASK;
  4853. if (cs.unusable)
  4854. return false;
  4855. if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
  4856. return false;
  4857. if (!cs.s)
  4858. return false;
  4859. if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
  4860. if (cs.dpl > cs_rpl)
  4861. return false;
  4862. } else {
  4863. if (cs.dpl != cs_rpl)
  4864. return false;
  4865. }
  4866. if (!cs.present)
  4867. return false;
  4868. /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
  4869. return true;
  4870. }
  4871. static bool stack_segment_valid(struct kvm_vcpu *vcpu)
  4872. {
  4873. struct kvm_segment ss;
  4874. unsigned int ss_rpl;
  4875. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  4876. ss_rpl = ss.selector & SEGMENT_RPL_MASK;
  4877. if (ss.unusable)
  4878. return true;
  4879. if (ss.type != 3 && ss.type != 7)
  4880. return false;
  4881. if (!ss.s)
  4882. return false;
  4883. if (ss.dpl != ss_rpl) /* DPL != RPL */
  4884. return false;
  4885. if (!ss.present)
  4886. return false;
  4887. return true;
  4888. }
  4889. static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
  4890. {
  4891. struct kvm_segment var;
  4892. unsigned int rpl;
  4893. vmx_get_segment(vcpu, &var, seg);
  4894. rpl = var.selector & SEGMENT_RPL_MASK;
  4895. if (var.unusable)
  4896. return true;
  4897. if (!var.s)
  4898. return false;
  4899. if (!var.present)
  4900. return false;
  4901. if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
  4902. if (var.dpl < rpl) /* DPL < RPL */
  4903. return false;
  4904. }
  4905. /* TODO: Add other members to kvm_segment_field to allow checking for other access
  4906. * rights flags
  4907. */
  4908. return true;
  4909. }
  4910. static bool tr_valid(struct kvm_vcpu *vcpu)
  4911. {
  4912. struct kvm_segment tr;
  4913. vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
  4914. if (tr.unusable)
  4915. return false;
  4916. if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
  4917. return false;
  4918. if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
  4919. return false;
  4920. if (!tr.present)
  4921. return false;
  4922. return true;
  4923. }
  4924. static bool ldtr_valid(struct kvm_vcpu *vcpu)
  4925. {
  4926. struct kvm_segment ldtr;
  4927. vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
  4928. if (ldtr.unusable)
  4929. return true;
  4930. if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
  4931. return false;
  4932. if (ldtr.type != 2)
  4933. return false;
  4934. if (!ldtr.present)
  4935. return false;
  4936. return true;
  4937. }
  4938. static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
  4939. {
  4940. struct kvm_segment cs, ss;
  4941. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  4942. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  4943. return ((cs.selector & SEGMENT_RPL_MASK) ==
  4944. (ss.selector & SEGMENT_RPL_MASK));
  4945. }
  4946. /*
  4947. * Check if guest state is valid. Returns true if valid, false if
  4948. * not.
  4949. * We assume that registers are always usable
  4950. */
  4951. static bool guest_state_valid(struct kvm_vcpu *vcpu)
  4952. {
  4953. if (enable_unrestricted_guest)
  4954. return true;
  4955. /* real mode guest state checks */
  4956. if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
  4957. if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
  4958. return false;
  4959. if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
  4960. return false;
  4961. if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
  4962. return false;
  4963. if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
  4964. return false;
  4965. if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
  4966. return false;
  4967. if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
  4968. return false;
  4969. } else {
  4970. /* protected mode guest state checks */
  4971. if (!cs_ss_rpl_check(vcpu))
  4972. return false;
  4973. if (!code_segment_valid(vcpu))
  4974. return false;
  4975. if (!stack_segment_valid(vcpu))
  4976. return false;
  4977. if (!data_segment_valid(vcpu, VCPU_SREG_DS))
  4978. return false;
  4979. if (!data_segment_valid(vcpu, VCPU_SREG_ES))
  4980. return false;
  4981. if (!data_segment_valid(vcpu, VCPU_SREG_FS))
  4982. return false;
  4983. if (!data_segment_valid(vcpu, VCPU_SREG_GS))
  4984. return false;
  4985. if (!tr_valid(vcpu))
  4986. return false;
  4987. if (!ldtr_valid(vcpu))
  4988. return false;
  4989. }
  4990. /* TODO:
  4991. * - Add checks on RIP
  4992. * - Add checks on RFLAGS
  4993. */
  4994. return true;
  4995. }
  4996. static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
  4997. {
  4998. return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
  4999. }
  5000. static int init_rmode_tss(struct kvm *kvm)
  5001. {
  5002. gfn_t fn;
  5003. u16 data = 0;
  5004. int idx, r;
  5005. idx = srcu_read_lock(&kvm->srcu);
  5006. fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
  5007. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  5008. if (r < 0)
  5009. goto out;
  5010. data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
  5011. r = kvm_write_guest_page(kvm, fn++, &data,
  5012. TSS_IOPB_BASE_OFFSET, sizeof(u16));
  5013. if (r < 0)
  5014. goto out;
  5015. r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
  5016. if (r < 0)
  5017. goto out;
  5018. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  5019. if (r < 0)
  5020. goto out;
  5021. data = ~0;
  5022. r = kvm_write_guest_page(kvm, fn, &data,
  5023. RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
  5024. sizeof(u8));
  5025. out:
  5026. srcu_read_unlock(&kvm->srcu, idx);
  5027. return r;
  5028. }
  5029. static int init_rmode_identity_map(struct kvm *kvm)
  5030. {
  5031. struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
  5032. int i, idx, r = 0;
  5033. kvm_pfn_t identity_map_pfn;
  5034. u32 tmp;
  5035. /* Protect kvm_vmx->ept_identity_pagetable_done. */
  5036. mutex_lock(&kvm->slots_lock);
  5037. if (likely(kvm_vmx->ept_identity_pagetable_done))
  5038. goto out2;
  5039. if (!kvm_vmx->ept_identity_map_addr)
  5040. kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
  5041. identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
  5042. r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
  5043. kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
  5044. if (r < 0)
  5045. goto out2;
  5046. idx = srcu_read_lock(&kvm->srcu);
  5047. r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
  5048. if (r < 0)
  5049. goto out;
  5050. /* Set up identity-mapping pagetable for EPT in real mode */
  5051. for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
  5052. tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
  5053. _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
  5054. r = kvm_write_guest_page(kvm, identity_map_pfn,
  5055. &tmp, i * sizeof(tmp), sizeof(tmp));
  5056. if (r < 0)
  5057. goto out;
  5058. }
  5059. kvm_vmx->ept_identity_pagetable_done = true;
  5060. out:
  5061. srcu_read_unlock(&kvm->srcu, idx);
  5062. out2:
  5063. mutex_unlock(&kvm->slots_lock);
  5064. return r;
  5065. }
  5066. static void seg_setup(int seg)
  5067. {
  5068. const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  5069. unsigned int ar;
  5070. vmcs_write16(sf->selector, 0);
  5071. vmcs_writel(sf->base, 0);
  5072. vmcs_write32(sf->limit, 0xffff);
  5073. ar = 0x93;
  5074. if (seg == VCPU_SREG_CS)
  5075. ar |= 0x08; /* code segment */
  5076. vmcs_write32(sf->ar_bytes, ar);
  5077. }
  5078. static int alloc_apic_access_page(struct kvm *kvm)
  5079. {
  5080. struct page *page;
  5081. int r = 0;
  5082. mutex_lock(&kvm->slots_lock);
  5083. if (kvm->arch.apic_access_page_done)
  5084. goto out;
  5085. r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
  5086. APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
  5087. if (r)
  5088. goto out;
  5089. page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
  5090. if (is_error_page(page)) {
  5091. r = -EFAULT;
  5092. goto out;
  5093. }
  5094. /*
  5095. * Do not pin the page in memory, so that memory hot-unplug
  5096. * is able to migrate it.
  5097. */
  5098. put_page(page);
  5099. kvm->arch.apic_access_page_done = true;
  5100. out:
  5101. mutex_unlock(&kvm->slots_lock);
  5102. return r;
  5103. }
  5104. static int allocate_vpid(void)
  5105. {
  5106. int vpid;
  5107. if (!enable_vpid)
  5108. return 0;
  5109. spin_lock(&vmx_vpid_lock);
  5110. vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
  5111. if (vpid < VMX_NR_VPIDS)
  5112. __set_bit(vpid, vmx_vpid_bitmap);
  5113. else
  5114. vpid = 0;
  5115. spin_unlock(&vmx_vpid_lock);
  5116. return vpid;
  5117. }
  5118. static void free_vpid(int vpid)
  5119. {
  5120. if (!enable_vpid || vpid == 0)
  5121. return;
  5122. spin_lock(&vmx_vpid_lock);
  5123. __clear_bit(vpid, vmx_vpid_bitmap);
  5124. spin_unlock(&vmx_vpid_lock);
  5125. }
  5126. static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
  5127. u32 msr, int type)
  5128. {
  5129. int f = sizeof(unsigned long);
  5130. if (!cpu_has_vmx_msr_bitmap())
  5131. return;
  5132. if (static_branch_unlikely(&enable_evmcs))
  5133. evmcs_touch_msr_bitmap();
  5134. /*
  5135. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  5136. * have the write-low and read-high bitmap offsets the wrong way round.
  5137. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  5138. */
  5139. if (msr <= 0x1fff) {
  5140. if (type & MSR_TYPE_R)
  5141. /* read-low */
  5142. __clear_bit(msr, msr_bitmap + 0x000 / f);
  5143. if (type & MSR_TYPE_W)
  5144. /* write-low */
  5145. __clear_bit(msr, msr_bitmap + 0x800 / f);
  5146. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  5147. msr &= 0x1fff;
  5148. if (type & MSR_TYPE_R)
  5149. /* read-high */
  5150. __clear_bit(msr, msr_bitmap + 0x400 / f);
  5151. if (type & MSR_TYPE_W)
  5152. /* write-high */
  5153. __clear_bit(msr, msr_bitmap + 0xc00 / f);
  5154. }
  5155. }
  5156. static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
  5157. u32 msr, int type)
  5158. {
  5159. int f = sizeof(unsigned long);
  5160. if (!cpu_has_vmx_msr_bitmap())
  5161. return;
  5162. if (static_branch_unlikely(&enable_evmcs))
  5163. evmcs_touch_msr_bitmap();
  5164. /*
  5165. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  5166. * have the write-low and read-high bitmap offsets the wrong way round.
  5167. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  5168. */
  5169. if (msr <= 0x1fff) {
  5170. if (type & MSR_TYPE_R)
  5171. /* read-low */
  5172. __set_bit(msr, msr_bitmap + 0x000 / f);
  5173. if (type & MSR_TYPE_W)
  5174. /* write-low */
  5175. __set_bit(msr, msr_bitmap + 0x800 / f);
  5176. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  5177. msr &= 0x1fff;
  5178. if (type & MSR_TYPE_R)
  5179. /* read-high */
  5180. __set_bit(msr, msr_bitmap + 0x400 / f);
  5181. if (type & MSR_TYPE_W)
  5182. /* write-high */
  5183. __set_bit(msr, msr_bitmap + 0xc00 / f);
  5184. }
  5185. }
  5186. static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
  5187. u32 msr, int type, bool value)
  5188. {
  5189. if (value)
  5190. vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
  5191. else
  5192. vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
  5193. }
  5194. /*
  5195. * If a msr is allowed by L0, we should check whether it is allowed by L1.
  5196. * The corresponding bit will be cleared unless both of L0 and L1 allow it.
  5197. */
  5198. static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
  5199. unsigned long *msr_bitmap_nested,
  5200. u32 msr, int type)
  5201. {
  5202. int f = sizeof(unsigned long);
  5203. /*
  5204. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  5205. * have the write-low and read-high bitmap offsets the wrong way round.
  5206. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  5207. */
  5208. if (msr <= 0x1fff) {
  5209. if (type & MSR_TYPE_R &&
  5210. !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
  5211. /* read-low */
  5212. __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
  5213. if (type & MSR_TYPE_W &&
  5214. !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
  5215. /* write-low */
  5216. __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
  5217. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  5218. msr &= 0x1fff;
  5219. if (type & MSR_TYPE_R &&
  5220. !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
  5221. /* read-high */
  5222. __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
  5223. if (type & MSR_TYPE_W &&
  5224. !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
  5225. /* write-high */
  5226. __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
  5227. }
  5228. }
  5229. static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
  5230. {
  5231. u8 mode = 0;
  5232. if (cpu_has_secondary_exec_ctrls() &&
  5233. (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
  5234. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
  5235. mode |= MSR_BITMAP_MODE_X2APIC;
  5236. if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
  5237. mode |= MSR_BITMAP_MODE_X2APIC_APICV;
  5238. }
  5239. return mode;
  5240. }
  5241. #define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
  5242. static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
  5243. u8 mode)
  5244. {
  5245. int msr;
  5246. for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
  5247. unsigned word = msr / BITS_PER_LONG;
  5248. msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
  5249. msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
  5250. }
  5251. if (mode & MSR_BITMAP_MODE_X2APIC) {
  5252. /*
  5253. * TPR reads and writes can be virtualized even if virtual interrupt
  5254. * delivery is not in use.
  5255. */
  5256. vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
  5257. if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
  5258. vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
  5259. vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
  5260. vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
  5261. }
  5262. }
  5263. }
  5264. static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
  5265. {
  5266. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5267. unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
  5268. u8 mode = vmx_msr_bitmap_mode(vcpu);
  5269. u8 changed = mode ^ vmx->msr_bitmap_mode;
  5270. if (!changed)
  5271. return;
  5272. if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
  5273. vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
  5274. vmx->msr_bitmap_mode = mode;
  5275. }
  5276. static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
  5277. {
  5278. return enable_apicv;
  5279. }
  5280. static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
  5281. {
  5282. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  5283. gfn_t gfn;
  5284. /*
  5285. * Don't need to mark the APIC access page dirty; it is never
  5286. * written to by the CPU during APIC virtualization.
  5287. */
  5288. if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
  5289. gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
  5290. kvm_vcpu_mark_page_dirty(vcpu, gfn);
  5291. }
  5292. if (nested_cpu_has_posted_intr(vmcs12)) {
  5293. gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
  5294. kvm_vcpu_mark_page_dirty(vcpu, gfn);
  5295. }
  5296. }
  5297. static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
  5298. {
  5299. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5300. int max_irr;
  5301. void *vapic_page;
  5302. u16 status;
  5303. if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
  5304. return;
  5305. vmx->nested.pi_pending = false;
  5306. if (!pi_test_and_clear_on(vmx->nested.pi_desc))
  5307. return;
  5308. max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
  5309. if (max_irr != 256) {
  5310. vapic_page = kmap(vmx->nested.virtual_apic_page);
  5311. __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
  5312. vapic_page, &max_irr);
  5313. kunmap(vmx->nested.virtual_apic_page);
  5314. status = vmcs_read16(GUEST_INTR_STATUS);
  5315. if ((u8)max_irr > ((u8)status & 0xff)) {
  5316. status &= ~0xff;
  5317. status |= (u8)max_irr;
  5318. vmcs_write16(GUEST_INTR_STATUS, status);
  5319. }
  5320. }
  5321. nested_mark_vmcs12_pages_dirty(vcpu);
  5322. }
  5323. static u8 vmx_get_rvi(void)
  5324. {
  5325. return vmcs_read16(GUEST_INTR_STATUS) & 0xff;
  5326. }
  5327. static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
  5328. {
  5329. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5330. void *vapic_page;
  5331. u32 vppr;
  5332. int rvi;
  5333. if (WARN_ON_ONCE(!is_guest_mode(vcpu)) ||
  5334. !nested_cpu_has_vid(get_vmcs12(vcpu)) ||
  5335. WARN_ON_ONCE(!vmx->nested.virtual_apic_page))
  5336. return false;
  5337. rvi = vmx_get_rvi();
  5338. vapic_page = kmap(vmx->nested.virtual_apic_page);
  5339. vppr = *((u32 *)(vapic_page + APIC_PROCPRI));
  5340. kunmap(vmx->nested.virtual_apic_page);
  5341. return ((rvi & 0xf0) > (vppr & 0xf0));
  5342. }
  5343. static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
  5344. bool nested)
  5345. {
  5346. #ifdef CONFIG_SMP
  5347. int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
  5348. if (vcpu->mode == IN_GUEST_MODE) {
  5349. /*
  5350. * The vector of interrupt to be delivered to vcpu had
  5351. * been set in PIR before this function.
  5352. *
  5353. * Following cases will be reached in this block, and
  5354. * we always send a notification event in all cases as
  5355. * explained below.
  5356. *
  5357. * Case 1: vcpu keeps in non-root mode. Sending a
  5358. * notification event posts the interrupt to vcpu.
  5359. *
  5360. * Case 2: vcpu exits to root mode and is still
  5361. * runnable. PIR will be synced to vIRR before the
  5362. * next vcpu entry. Sending a notification event in
  5363. * this case has no effect, as vcpu is not in root
  5364. * mode.
  5365. *
  5366. * Case 3: vcpu exits to root mode and is blocked.
  5367. * vcpu_block() has already synced PIR to vIRR and
  5368. * never blocks vcpu if vIRR is not cleared. Therefore,
  5369. * a blocked vcpu here does not wait for any requested
  5370. * interrupts in PIR, and sending a notification event
  5371. * which has no effect is safe here.
  5372. */
  5373. apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
  5374. return true;
  5375. }
  5376. #endif
  5377. return false;
  5378. }
  5379. static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
  5380. int vector)
  5381. {
  5382. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5383. if (is_guest_mode(vcpu) &&
  5384. vector == vmx->nested.posted_intr_nv) {
  5385. /*
  5386. * If a posted intr is not recognized by hardware,
  5387. * we will accomplish it in the next vmentry.
  5388. */
  5389. vmx->nested.pi_pending = true;
  5390. kvm_make_request(KVM_REQ_EVENT, vcpu);
  5391. /* the PIR and ON have been set by L1. */
  5392. if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
  5393. kvm_vcpu_kick(vcpu);
  5394. return 0;
  5395. }
  5396. return -1;
  5397. }
  5398. /*
  5399. * Send interrupt to vcpu via posted interrupt way.
  5400. * 1. If target vcpu is running(non-root mode), send posted interrupt
  5401. * notification to vcpu and hardware will sync PIR to vIRR atomically.
  5402. * 2. If target vcpu isn't running(root mode), kick it to pick up the
  5403. * interrupt from PIR in next vmentry.
  5404. */
  5405. static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
  5406. {
  5407. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5408. int r;
  5409. r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
  5410. if (!r)
  5411. return;
  5412. if (pi_test_and_set_pir(vector, &vmx->pi_desc))
  5413. return;
  5414. /* If a previous notification has sent the IPI, nothing to do. */
  5415. if (pi_test_and_set_on(&vmx->pi_desc))
  5416. return;
  5417. if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
  5418. kvm_vcpu_kick(vcpu);
  5419. }
  5420. /*
  5421. * Set up the vmcs's constant host-state fields, i.e., host-state fields that
  5422. * will not change in the lifetime of the guest.
  5423. * Note that host-state that does change is set elsewhere. E.g., host-state
  5424. * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
  5425. */
  5426. static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
  5427. {
  5428. u32 low32, high32;
  5429. unsigned long tmpl;
  5430. struct desc_ptr dt;
  5431. unsigned long cr0, cr3, cr4;
  5432. cr0 = read_cr0();
  5433. WARN_ON(cr0 & X86_CR0_TS);
  5434. vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
  5435. /*
  5436. * Save the most likely value for this task's CR3 in the VMCS.
  5437. * We can't use __get_current_cr3_fast() because we're not atomic.
  5438. */
  5439. cr3 = __read_cr3();
  5440. vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
  5441. vmx->loaded_vmcs->host_state.cr3 = cr3;
  5442. /* Save the most likely value for this task's CR4 in the VMCS. */
  5443. cr4 = cr4_read_shadow();
  5444. vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
  5445. vmx->loaded_vmcs->host_state.cr4 = cr4;
  5446. vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
  5447. #ifdef CONFIG_X86_64
  5448. /*
  5449. * Load null selectors, so we can avoid reloading them in
  5450. * vmx_prepare_switch_to_host(), in case userspace uses
  5451. * the null selectors too (the expected case).
  5452. */
  5453. vmcs_write16(HOST_DS_SELECTOR, 0);
  5454. vmcs_write16(HOST_ES_SELECTOR, 0);
  5455. #else
  5456. vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  5457. vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  5458. #endif
  5459. vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  5460. vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
  5461. store_idt(&dt);
  5462. vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
  5463. vmx->host_idt_base = dt.address;
  5464. vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
  5465. rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
  5466. vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
  5467. rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
  5468. vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
  5469. if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
  5470. rdmsr(MSR_IA32_CR_PAT, low32, high32);
  5471. vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
  5472. }
  5473. if (cpu_has_load_ia32_efer)
  5474. vmcs_write64(HOST_IA32_EFER, host_efer);
  5475. }
  5476. static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
  5477. {
  5478. vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
  5479. if (enable_ept)
  5480. vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
  5481. if (is_guest_mode(&vmx->vcpu))
  5482. vmx->vcpu.arch.cr4_guest_owned_bits &=
  5483. ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
  5484. vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
  5485. }
  5486. static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
  5487. {
  5488. u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
  5489. if (!kvm_vcpu_apicv_active(&vmx->vcpu))
  5490. pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
  5491. if (!enable_vnmi)
  5492. pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
  5493. /* Enable the preemption timer dynamically */
  5494. pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  5495. return pin_based_exec_ctrl;
  5496. }
  5497. static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
  5498. {
  5499. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5500. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
  5501. if (cpu_has_secondary_exec_ctrls()) {
  5502. if (kvm_vcpu_apicv_active(vcpu))
  5503. vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
  5504. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  5505. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  5506. else
  5507. vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
  5508. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  5509. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  5510. }
  5511. if (cpu_has_vmx_msr_bitmap())
  5512. vmx_update_msr_bitmap(vcpu);
  5513. }
  5514. static u32 vmx_exec_control(struct vcpu_vmx *vmx)
  5515. {
  5516. u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
  5517. if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
  5518. exec_control &= ~CPU_BASED_MOV_DR_EXITING;
  5519. if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
  5520. exec_control &= ~CPU_BASED_TPR_SHADOW;
  5521. #ifdef CONFIG_X86_64
  5522. exec_control |= CPU_BASED_CR8_STORE_EXITING |
  5523. CPU_BASED_CR8_LOAD_EXITING;
  5524. #endif
  5525. }
  5526. if (!enable_ept)
  5527. exec_control |= CPU_BASED_CR3_STORE_EXITING |
  5528. CPU_BASED_CR3_LOAD_EXITING |
  5529. CPU_BASED_INVLPG_EXITING;
  5530. if (kvm_mwait_in_guest(vmx->vcpu.kvm))
  5531. exec_control &= ~(CPU_BASED_MWAIT_EXITING |
  5532. CPU_BASED_MONITOR_EXITING);
  5533. if (kvm_hlt_in_guest(vmx->vcpu.kvm))
  5534. exec_control &= ~CPU_BASED_HLT_EXITING;
  5535. return exec_control;
  5536. }
  5537. static bool vmx_rdrand_supported(void)
  5538. {
  5539. return vmcs_config.cpu_based_2nd_exec_ctrl &
  5540. SECONDARY_EXEC_RDRAND_EXITING;
  5541. }
  5542. static bool vmx_rdseed_supported(void)
  5543. {
  5544. return vmcs_config.cpu_based_2nd_exec_ctrl &
  5545. SECONDARY_EXEC_RDSEED_EXITING;
  5546. }
  5547. static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
  5548. {
  5549. struct kvm_vcpu *vcpu = &vmx->vcpu;
  5550. u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
  5551. if (!cpu_need_virtualize_apic_accesses(vcpu))
  5552. exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  5553. if (vmx->vpid == 0)
  5554. exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
  5555. if (!enable_ept) {
  5556. exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
  5557. enable_unrestricted_guest = 0;
  5558. }
  5559. if (!enable_unrestricted_guest)
  5560. exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
  5561. if (kvm_pause_in_guest(vmx->vcpu.kvm))
  5562. exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  5563. if (!kvm_vcpu_apicv_active(vcpu))
  5564. exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
  5565. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  5566. exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  5567. /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
  5568. * in vmx_set_cr4. */
  5569. exec_control &= ~SECONDARY_EXEC_DESC;
  5570. /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
  5571. (handle_vmptrld).
  5572. We can NOT enable shadow_vmcs here because we don't have yet
  5573. a current VMCS12
  5574. */
  5575. exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
  5576. if (!enable_pml)
  5577. exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
  5578. if (vmx_xsaves_supported()) {
  5579. /* Exposing XSAVES only when XSAVE is exposed */
  5580. bool xsaves_enabled =
  5581. guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
  5582. guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
  5583. if (!xsaves_enabled)
  5584. exec_control &= ~SECONDARY_EXEC_XSAVES;
  5585. if (nested) {
  5586. if (xsaves_enabled)
  5587. vmx->nested.msrs.secondary_ctls_high |=
  5588. SECONDARY_EXEC_XSAVES;
  5589. else
  5590. vmx->nested.msrs.secondary_ctls_high &=
  5591. ~SECONDARY_EXEC_XSAVES;
  5592. }
  5593. }
  5594. if (vmx_rdtscp_supported()) {
  5595. bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
  5596. if (!rdtscp_enabled)
  5597. exec_control &= ~SECONDARY_EXEC_RDTSCP;
  5598. if (nested) {
  5599. if (rdtscp_enabled)
  5600. vmx->nested.msrs.secondary_ctls_high |=
  5601. SECONDARY_EXEC_RDTSCP;
  5602. else
  5603. vmx->nested.msrs.secondary_ctls_high &=
  5604. ~SECONDARY_EXEC_RDTSCP;
  5605. }
  5606. }
  5607. if (vmx_invpcid_supported()) {
  5608. /* Exposing INVPCID only when PCID is exposed */
  5609. bool invpcid_enabled =
  5610. guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
  5611. guest_cpuid_has(vcpu, X86_FEATURE_PCID);
  5612. if (!invpcid_enabled) {
  5613. exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
  5614. guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
  5615. }
  5616. if (nested) {
  5617. if (invpcid_enabled)
  5618. vmx->nested.msrs.secondary_ctls_high |=
  5619. SECONDARY_EXEC_ENABLE_INVPCID;
  5620. else
  5621. vmx->nested.msrs.secondary_ctls_high &=
  5622. ~SECONDARY_EXEC_ENABLE_INVPCID;
  5623. }
  5624. }
  5625. if (vmx_rdrand_supported()) {
  5626. bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
  5627. if (rdrand_enabled)
  5628. exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
  5629. if (nested) {
  5630. if (rdrand_enabled)
  5631. vmx->nested.msrs.secondary_ctls_high |=
  5632. SECONDARY_EXEC_RDRAND_EXITING;
  5633. else
  5634. vmx->nested.msrs.secondary_ctls_high &=
  5635. ~SECONDARY_EXEC_RDRAND_EXITING;
  5636. }
  5637. }
  5638. if (vmx_rdseed_supported()) {
  5639. bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
  5640. if (rdseed_enabled)
  5641. exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
  5642. if (nested) {
  5643. if (rdseed_enabled)
  5644. vmx->nested.msrs.secondary_ctls_high |=
  5645. SECONDARY_EXEC_RDSEED_EXITING;
  5646. else
  5647. vmx->nested.msrs.secondary_ctls_high &=
  5648. ~SECONDARY_EXEC_RDSEED_EXITING;
  5649. }
  5650. }
  5651. vmx->secondary_exec_control = exec_control;
  5652. }
  5653. static void ept_set_mmio_spte_mask(void)
  5654. {
  5655. /*
  5656. * EPT Misconfigurations can be generated if the value of bits 2:0
  5657. * of an EPT paging-structure entry is 110b (write/execute).
  5658. */
  5659. kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
  5660. VMX_EPT_MISCONFIG_WX_VALUE);
  5661. }
  5662. #define VMX_XSS_EXIT_BITMAP 0
  5663. /*
  5664. * Sets up the vmcs for emulated real mode.
  5665. */
  5666. static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
  5667. {
  5668. int i;
  5669. if (enable_shadow_vmcs) {
  5670. /*
  5671. * At vCPU creation, "VMWRITE to any supported field
  5672. * in the VMCS" is supported, so use the more
  5673. * permissive vmx_vmread_bitmap to specify both read
  5674. * and write permissions for the shadow VMCS.
  5675. */
  5676. vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
  5677. vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
  5678. }
  5679. if (cpu_has_vmx_msr_bitmap())
  5680. vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
  5681. vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
  5682. /* Control */
  5683. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
  5684. vmx->hv_deadline_tsc = -1;
  5685. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
  5686. if (cpu_has_secondary_exec_ctrls()) {
  5687. vmx_compute_secondary_exec_control(vmx);
  5688. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  5689. vmx->secondary_exec_control);
  5690. }
  5691. if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
  5692. vmcs_write64(EOI_EXIT_BITMAP0, 0);
  5693. vmcs_write64(EOI_EXIT_BITMAP1, 0);
  5694. vmcs_write64(EOI_EXIT_BITMAP2, 0);
  5695. vmcs_write64(EOI_EXIT_BITMAP3, 0);
  5696. vmcs_write16(GUEST_INTR_STATUS, 0);
  5697. vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
  5698. vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
  5699. }
  5700. if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
  5701. vmcs_write32(PLE_GAP, ple_gap);
  5702. vmx->ple_window = ple_window;
  5703. vmx->ple_window_dirty = true;
  5704. }
  5705. vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
  5706. vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
  5707. vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
  5708. vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
  5709. vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
  5710. vmx_set_constant_host_state(vmx);
  5711. vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
  5712. vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
  5713. if (cpu_has_vmx_vmfunc())
  5714. vmcs_write64(VM_FUNCTION_CONTROL, 0);
  5715. vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
  5716. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
  5717. vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
  5718. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
  5719. vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
  5720. if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
  5721. vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
  5722. for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
  5723. u32 index = vmx_msr_index[i];
  5724. u32 data_low, data_high;
  5725. int j = vmx->nmsrs;
  5726. if (rdmsr_safe(index, &data_low, &data_high) < 0)
  5727. continue;
  5728. if (wrmsr_safe(index, data_low, data_high) < 0)
  5729. continue;
  5730. vmx->guest_msrs[j].index = i;
  5731. vmx->guest_msrs[j].data = 0;
  5732. vmx->guest_msrs[j].mask = -1ull;
  5733. ++vmx->nmsrs;
  5734. }
  5735. vmx->arch_capabilities = kvm_get_arch_capabilities();
  5736. vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
  5737. /* 22.2.1, 20.8.1 */
  5738. vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
  5739. vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
  5740. vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
  5741. set_cr4_guest_host_mask(vmx);
  5742. if (vmx_xsaves_supported())
  5743. vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
  5744. if (enable_pml) {
  5745. vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
  5746. vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
  5747. }
  5748. if (cpu_has_vmx_encls_vmexit())
  5749. vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
  5750. }
  5751. static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
  5752. {
  5753. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5754. struct msr_data apic_base_msr;
  5755. u64 cr0;
  5756. vmx->rmode.vm86_active = 0;
  5757. vmx->spec_ctrl = 0;
  5758. vcpu->arch.microcode_version = 0x100000000ULL;
  5759. vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
  5760. kvm_set_cr8(vcpu, 0);
  5761. if (!init_event) {
  5762. apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
  5763. MSR_IA32_APICBASE_ENABLE;
  5764. if (kvm_vcpu_is_reset_bsp(vcpu))
  5765. apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
  5766. apic_base_msr.host_initiated = true;
  5767. kvm_set_apic_base(vcpu, &apic_base_msr);
  5768. }
  5769. vmx_segment_cache_clear(vmx);
  5770. seg_setup(VCPU_SREG_CS);
  5771. vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
  5772. vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
  5773. seg_setup(VCPU_SREG_DS);
  5774. seg_setup(VCPU_SREG_ES);
  5775. seg_setup(VCPU_SREG_FS);
  5776. seg_setup(VCPU_SREG_GS);
  5777. seg_setup(VCPU_SREG_SS);
  5778. vmcs_write16(GUEST_TR_SELECTOR, 0);
  5779. vmcs_writel(GUEST_TR_BASE, 0);
  5780. vmcs_write32(GUEST_TR_LIMIT, 0xffff);
  5781. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  5782. vmcs_write16(GUEST_LDTR_SELECTOR, 0);
  5783. vmcs_writel(GUEST_LDTR_BASE, 0);
  5784. vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
  5785. vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
  5786. if (!init_event) {
  5787. vmcs_write32(GUEST_SYSENTER_CS, 0);
  5788. vmcs_writel(GUEST_SYSENTER_ESP, 0);
  5789. vmcs_writel(GUEST_SYSENTER_EIP, 0);
  5790. vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
  5791. }
  5792. kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
  5793. kvm_rip_write(vcpu, 0xfff0);
  5794. vmcs_writel(GUEST_GDTR_BASE, 0);
  5795. vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
  5796. vmcs_writel(GUEST_IDTR_BASE, 0);
  5797. vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
  5798. vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
  5799. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
  5800. vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
  5801. if (kvm_mpx_supported())
  5802. vmcs_write64(GUEST_BNDCFGS, 0);
  5803. setup_msrs(vmx);
  5804. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
  5805. if (cpu_has_vmx_tpr_shadow() && !init_event) {
  5806. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
  5807. if (cpu_need_tpr_shadow(vcpu))
  5808. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
  5809. __pa(vcpu->arch.apic->regs));
  5810. vmcs_write32(TPR_THRESHOLD, 0);
  5811. }
  5812. kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
  5813. if (vmx->vpid != 0)
  5814. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
  5815. cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
  5816. vmx->vcpu.arch.cr0 = cr0;
  5817. vmx_set_cr0(vcpu, cr0); /* enter rmode */
  5818. vmx_set_cr4(vcpu, 0);
  5819. vmx_set_efer(vcpu, 0);
  5820. update_exception_bitmap(vcpu);
  5821. vpid_sync_context(vmx->vpid);
  5822. if (init_event)
  5823. vmx_clear_hlt(vcpu);
  5824. }
  5825. /*
  5826. * In nested virtualization, check if L1 asked to exit on external interrupts.
  5827. * For most existing hypervisors, this will always return true.
  5828. */
  5829. static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
  5830. {
  5831. return get_vmcs12(vcpu)->pin_based_vm_exec_control &
  5832. PIN_BASED_EXT_INTR_MASK;
  5833. }
  5834. /*
  5835. * In nested virtualization, check if L1 has set
  5836. * VM_EXIT_ACK_INTR_ON_EXIT
  5837. */
  5838. static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
  5839. {
  5840. return get_vmcs12(vcpu)->vm_exit_controls &
  5841. VM_EXIT_ACK_INTR_ON_EXIT;
  5842. }
  5843. static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
  5844. {
  5845. return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
  5846. }
  5847. static void enable_irq_window(struct kvm_vcpu *vcpu)
  5848. {
  5849. vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
  5850. CPU_BASED_VIRTUAL_INTR_PENDING);
  5851. }
  5852. static void enable_nmi_window(struct kvm_vcpu *vcpu)
  5853. {
  5854. if (!enable_vnmi ||
  5855. vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
  5856. enable_irq_window(vcpu);
  5857. return;
  5858. }
  5859. vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
  5860. CPU_BASED_VIRTUAL_NMI_PENDING);
  5861. }
  5862. static void vmx_inject_irq(struct kvm_vcpu *vcpu)
  5863. {
  5864. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5865. uint32_t intr;
  5866. int irq = vcpu->arch.interrupt.nr;
  5867. trace_kvm_inj_virq(irq);
  5868. ++vcpu->stat.irq_injections;
  5869. if (vmx->rmode.vm86_active) {
  5870. int inc_eip = 0;
  5871. if (vcpu->arch.interrupt.soft)
  5872. inc_eip = vcpu->arch.event_exit_inst_len;
  5873. if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
  5874. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  5875. return;
  5876. }
  5877. intr = irq | INTR_INFO_VALID_MASK;
  5878. if (vcpu->arch.interrupt.soft) {
  5879. intr |= INTR_TYPE_SOFT_INTR;
  5880. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  5881. vmx->vcpu.arch.event_exit_inst_len);
  5882. } else
  5883. intr |= INTR_TYPE_EXT_INTR;
  5884. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
  5885. vmx_clear_hlt(vcpu);
  5886. }
  5887. static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
  5888. {
  5889. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5890. if (!enable_vnmi) {
  5891. /*
  5892. * Tracking the NMI-blocked state in software is built upon
  5893. * finding the next open IRQ window. This, in turn, depends on
  5894. * well-behaving guests: They have to keep IRQs disabled at
  5895. * least as long as the NMI handler runs. Otherwise we may
  5896. * cause NMI nesting, maybe breaking the guest. But as this is
  5897. * highly unlikely, we can live with the residual risk.
  5898. */
  5899. vmx->loaded_vmcs->soft_vnmi_blocked = 1;
  5900. vmx->loaded_vmcs->vnmi_blocked_time = 0;
  5901. }
  5902. ++vcpu->stat.nmi_injections;
  5903. vmx->loaded_vmcs->nmi_known_unmasked = false;
  5904. if (vmx->rmode.vm86_active) {
  5905. if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
  5906. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  5907. return;
  5908. }
  5909. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  5910. INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
  5911. vmx_clear_hlt(vcpu);
  5912. }
  5913. static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
  5914. {
  5915. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5916. bool masked;
  5917. if (!enable_vnmi)
  5918. return vmx->loaded_vmcs->soft_vnmi_blocked;
  5919. if (vmx->loaded_vmcs->nmi_known_unmasked)
  5920. return false;
  5921. masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
  5922. vmx->loaded_vmcs->nmi_known_unmasked = !masked;
  5923. return masked;
  5924. }
  5925. static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
  5926. {
  5927. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5928. if (!enable_vnmi) {
  5929. if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
  5930. vmx->loaded_vmcs->soft_vnmi_blocked = masked;
  5931. vmx->loaded_vmcs->vnmi_blocked_time = 0;
  5932. }
  5933. } else {
  5934. vmx->loaded_vmcs->nmi_known_unmasked = !masked;
  5935. if (masked)
  5936. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  5937. GUEST_INTR_STATE_NMI);
  5938. else
  5939. vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
  5940. GUEST_INTR_STATE_NMI);
  5941. }
  5942. }
  5943. static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
  5944. {
  5945. if (to_vmx(vcpu)->nested.nested_run_pending)
  5946. return 0;
  5947. if (!enable_vnmi &&
  5948. to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
  5949. return 0;
  5950. return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
  5951. (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
  5952. | GUEST_INTR_STATE_NMI));
  5953. }
  5954. static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
  5955. {
  5956. return (!to_vmx(vcpu)->nested.nested_run_pending &&
  5957. vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
  5958. !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
  5959. (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
  5960. }
  5961. static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
  5962. {
  5963. int ret;
  5964. if (enable_unrestricted_guest)
  5965. return 0;
  5966. ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
  5967. PAGE_SIZE * 3);
  5968. if (ret)
  5969. return ret;
  5970. to_kvm_vmx(kvm)->tss_addr = addr;
  5971. return init_rmode_tss(kvm);
  5972. }
  5973. static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
  5974. {
  5975. to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
  5976. return 0;
  5977. }
  5978. static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
  5979. {
  5980. switch (vec) {
  5981. case BP_VECTOR:
  5982. /*
  5983. * Update instruction length as we may reinject the exception
  5984. * from user space while in guest debugging mode.
  5985. */
  5986. to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
  5987. vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  5988. if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  5989. return false;
  5990. /* fall through */
  5991. case DB_VECTOR:
  5992. if (vcpu->guest_debug &
  5993. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
  5994. return false;
  5995. /* fall through */
  5996. case DE_VECTOR:
  5997. case OF_VECTOR:
  5998. case BR_VECTOR:
  5999. case UD_VECTOR:
  6000. case DF_VECTOR:
  6001. case SS_VECTOR:
  6002. case GP_VECTOR:
  6003. case MF_VECTOR:
  6004. return true;
  6005. break;
  6006. }
  6007. return false;
  6008. }
  6009. static int handle_rmode_exception(struct kvm_vcpu *vcpu,
  6010. int vec, u32 err_code)
  6011. {
  6012. /*
  6013. * Instruction with address size override prefix opcode 0x67
  6014. * Cause the #SS fault with 0 error code in VM86 mode.
  6015. */
  6016. if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
  6017. if (kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE) {
  6018. if (vcpu->arch.halt_request) {
  6019. vcpu->arch.halt_request = 0;
  6020. return kvm_vcpu_halt(vcpu);
  6021. }
  6022. return 1;
  6023. }
  6024. return 0;
  6025. }
  6026. /*
  6027. * Forward all other exceptions that are valid in real mode.
  6028. * FIXME: Breaks guest debugging in real mode, needs to be fixed with
  6029. * the required debugging infrastructure rework.
  6030. */
  6031. kvm_queue_exception(vcpu, vec);
  6032. return 1;
  6033. }
  6034. /*
  6035. * Trigger machine check on the host. We assume all the MSRs are already set up
  6036. * by the CPU and that we still run on the same CPU as the MCE occurred on.
  6037. * We pass a fake environment to the machine check handler because we want
  6038. * the guest to be always treated like user space, no matter what context
  6039. * it used internally.
  6040. */
  6041. static void kvm_machine_check(void)
  6042. {
  6043. #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
  6044. struct pt_regs regs = {
  6045. .cs = 3, /* Fake ring 3 no matter what the guest ran on */
  6046. .flags = X86_EFLAGS_IF,
  6047. };
  6048. do_machine_check(&regs, 0);
  6049. #endif
  6050. }
  6051. static int handle_machine_check(struct kvm_vcpu *vcpu)
  6052. {
  6053. /* already handled by vcpu_run */
  6054. return 1;
  6055. }
  6056. static int handle_exception(struct kvm_vcpu *vcpu)
  6057. {
  6058. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6059. struct kvm_run *kvm_run = vcpu->run;
  6060. u32 intr_info, ex_no, error_code;
  6061. unsigned long cr2, rip, dr6;
  6062. u32 vect_info;
  6063. enum emulation_result er;
  6064. vect_info = vmx->idt_vectoring_info;
  6065. intr_info = vmx->exit_intr_info;
  6066. if (is_machine_check(intr_info))
  6067. return handle_machine_check(vcpu);
  6068. if (is_nmi(intr_info))
  6069. return 1; /* already handled by vmx_vcpu_run() */
  6070. if (is_invalid_opcode(intr_info))
  6071. return handle_ud(vcpu);
  6072. error_code = 0;
  6073. if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
  6074. error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
  6075. if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
  6076. WARN_ON_ONCE(!enable_vmware_backdoor);
  6077. er = kvm_emulate_instruction(vcpu,
  6078. EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
  6079. if (er == EMULATE_USER_EXIT)
  6080. return 0;
  6081. else if (er != EMULATE_DONE)
  6082. kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
  6083. return 1;
  6084. }
  6085. /*
  6086. * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
  6087. * MMIO, it is better to report an internal error.
  6088. * See the comments in vmx_handle_exit.
  6089. */
  6090. if ((vect_info & VECTORING_INFO_VALID_MASK) &&
  6091. !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
  6092. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  6093. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
  6094. vcpu->run->internal.ndata = 3;
  6095. vcpu->run->internal.data[0] = vect_info;
  6096. vcpu->run->internal.data[1] = intr_info;
  6097. vcpu->run->internal.data[2] = error_code;
  6098. return 0;
  6099. }
  6100. if (is_page_fault(intr_info)) {
  6101. cr2 = vmcs_readl(EXIT_QUALIFICATION);
  6102. /* EPT won't cause page fault directly */
  6103. WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
  6104. return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
  6105. }
  6106. ex_no = intr_info & INTR_INFO_VECTOR_MASK;
  6107. if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
  6108. return handle_rmode_exception(vcpu, ex_no, error_code);
  6109. switch (ex_no) {
  6110. case AC_VECTOR:
  6111. kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
  6112. return 1;
  6113. case DB_VECTOR:
  6114. dr6 = vmcs_readl(EXIT_QUALIFICATION);
  6115. if (!(vcpu->guest_debug &
  6116. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
  6117. vcpu->arch.dr6 &= ~15;
  6118. vcpu->arch.dr6 |= dr6 | DR6_RTM;
  6119. if (is_icebp(intr_info))
  6120. skip_emulated_instruction(vcpu);
  6121. kvm_queue_exception(vcpu, DB_VECTOR);
  6122. return 1;
  6123. }
  6124. kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
  6125. kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
  6126. /* fall through */
  6127. case BP_VECTOR:
  6128. /*
  6129. * Update instruction length as we may reinject #BP from
  6130. * user space while in guest debugging mode. Reading it for
  6131. * #DB as well causes no harm, it is not used in that case.
  6132. */
  6133. vmx->vcpu.arch.event_exit_inst_len =
  6134. vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  6135. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  6136. rip = kvm_rip_read(vcpu);
  6137. kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
  6138. kvm_run->debug.arch.exception = ex_no;
  6139. break;
  6140. default:
  6141. kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
  6142. kvm_run->ex.exception = ex_no;
  6143. kvm_run->ex.error_code = error_code;
  6144. break;
  6145. }
  6146. return 0;
  6147. }
  6148. static int handle_external_interrupt(struct kvm_vcpu *vcpu)
  6149. {
  6150. ++vcpu->stat.irq_exits;
  6151. return 1;
  6152. }
  6153. static int handle_triple_fault(struct kvm_vcpu *vcpu)
  6154. {
  6155. vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
  6156. vcpu->mmio_needed = 0;
  6157. return 0;
  6158. }
  6159. static int handle_io(struct kvm_vcpu *vcpu)
  6160. {
  6161. unsigned long exit_qualification;
  6162. int size, in, string;
  6163. unsigned port;
  6164. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6165. string = (exit_qualification & 16) != 0;
  6166. ++vcpu->stat.io_exits;
  6167. if (string)
  6168. return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
  6169. port = exit_qualification >> 16;
  6170. size = (exit_qualification & 7) + 1;
  6171. in = (exit_qualification & 8) != 0;
  6172. return kvm_fast_pio(vcpu, size, port, in);
  6173. }
  6174. static void
  6175. vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
  6176. {
  6177. /*
  6178. * Patch in the VMCALL instruction:
  6179. */
  6180. hypercall[0] = 0x0f;
  6181. hypercall[1] = 0x01;
  6182. hypercall[2] = 0xc1;
  6183. }
  6184. /* called to set cr0 as appropriate for a mov-to-cr0 exit. */
  6185. static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
  6186. {
  6187. if (is_guest_mode(vcpu)) {
  6188. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  6189. unsigned long orig_val = val;
  6190. /*
  6191. * We get here when L2 changed cr0 in a way that did not change
  6192. * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
  6193. * but did change L0 shadowed bits. So we first calculate the
  6194. * effective cr0 value that L1 would like to write into the
  6195. * hardware. It consists of the L2-owned bits from the new
  6196. * value combined with the L1-owned bits from L1's guest_cr0.
  6197. */
  6198. val = (val & ~vmcs12->cr0_guest_host_mask) |
  6199. (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
  6200. if (!nested_guest_cr0_valid(vcpu, val))
  6201. return 1;
  6202. if (kvm_set_cr0(vcpu, val))
  6203. return 1;
  6204. vmcs_writel(CR0_READ_SHADOW, orig_val);
  6205. return 0;
  6206. } else {
  6207. if (to_vmx(vcpu)->nested.vmxon &&
  6208. !nested_host_cr0_valid(vcpu, val))
  6209. return 1;
  6210. return kvm_set_cr0(vcpu, val);
  6211. }
  6212. }
  6213. static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
  6214. {
  6215. if (is_guest_mode(vcpu)) {
  6216. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  6217. unsigned long orig_val = val;
  6218. /* analogously to handle_set_cr0 */
  6219. val = (val & ~vmcs12->cr4_guest_host_mask) |
  6220. (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
  6221. if (kvm_set_cr4(vcpu, val))
  6222. return 1;
  6223. vmcs_writel(CR4_READ_SHADOW, orig_val);
  6224. return 0;
  6225. } else
  6226. return kvm_set_cr4(vcpu, val);
  6227. }
  6228. static int handle_desc(struct kvm_vcpu *vcpu)
  6229. {
  6230. WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
  6231. return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
  6232. }
  6233. static int handle_cr(struct kvm_vcpu *vcpu)
  6234. {
  6235. unsigned long exit_qualification, val;
  6236. int cr;
  6237. int reg;
  6238. int err;
  6239. int ret;
  6240. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6241. cr = exit_qualification & 15;
  6242. reg = (exit_qualification >> 8) & 15;
  6243. switch ((exit_qualification >> 4) & 3) {
  6244. case 0: /* mov to cr */
  6245. val = kvm_register_readl(vcpu, reg);
  6246. trace_kvm_cr_write(cr, val);
  6247. switch (cr) {
  6248. case 0:
  6249. err = handle_set_cr0(vcpu, val);
  6250. return kvm_complete_insn_gp(vcpu, err);
  6251. case 3:
  6252. WARN_ON_ONCE(enable_unrestricted_guest);
  6253. err = kvm_set_cr3(vcpu, val);
  6254. return kvm_complete_insn_gp(vcpu, err);
  6255. case 4:
  6256. err = handle_set_cr4(vcpu, val);
  6257. return kvm_complete_insn_gp(vcpu, err);
  6258. case 8: {
  6259. u8 cr8_prev = kvm_get_cr8(vcpu);
  6260. u8 cr8 = (u8)val;
  6261. err = kvm_set_cr8(vcpu, cr8);
  6262. ret = kvm_complete_insn_gp(vcpu, err);
  6263. if (lapic_in_kernel(vcpu))
  6264. return ret;
  6265. if (cr8_prev <= cr8)
  6266. return ret;
  6267. /*
  6268. * TODO: we might be squashing a
  6269. * KVM_GUESTDBG_SINGLESTEP-triggered
  6270. * KVM_EXIT_DEBUG here.
  6271. */
  6272. vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
  6273. return 0;
  6274. }
  6275. }
  6276. break;
  6277. case 2: /* clts */
  6278. WARN_ONCE(1, "Guest should always own CR0.TS");
  6279. vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
  6280. trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
  6281. return kvm_skip_emulated_instruction(vcpu);
  6282. case 1: /*mov from cr*/
  6283. switch (cr) {
  6284. case 3:
  6285. WARN_ON_ONCE(enable_unrestricted_guest);
  6286. val = kvm_read_cr3(vcpu);
  6287. kvm_register_write(vcpu, reg, val);
  6288. trace_kvm_cr_read(cr, val);
  6289. return kvm_skip_emulated_instruction(vcpu);
  6290. case 8:
  6291. val = kvm_get_cr8(vcpu);
  6292. kvm_register_write(vcpu, reg, val);
  6293. trace_kvm_cr_read(cr, val);
  6294. return kvm_skip_emulated_instruction(vcpu);
  6295. }
  6296. break;
  6297. case 3: /* lmsw */
  6298. val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
  6299. trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
  6300. kvm_lmsw(vcpu, val);
  6301. return kvm_skip_emulated_instruction(vcpu);
  6302. default:
  6303. break;
  6304. }
  6305. vcpu->run->exit_reason = 0;
  6306. vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
  6307. (int)(exit_qualification >> 4) & 3, cr);
  6308. return 0;
  6309. }
  6310. static int handle_dr(struct kvm_vcpu *vcpu)
  6311. {
  6312. unsigned long exit_qualification;
  6313. int dr, dr7, reg;
  6314. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6315. dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
  6316. /* First, if DR does not exist, trigger UD */
  6317. if (!kvm_require_dr(vcpu, dr))
  6318. return 1;
  6319. /* Do not handle if the CPL > 0, will trigger GP on re-entry */
  6320. if (!kvm_require_cpl(vcpu, 0))
  6321. return 1;
  6322. dr7 = vmcs_readl(GUEST_DR7);
  6323. if (dr7 & DR7_GD) {
  6324. /*
  6325. * As the vm-exit takes precedence over the debug trap, we
  6326. * need to emulate the latter, either for the host or the
  6327. * guest debugging itself.
  6328. */
  6329. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
  6330. vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
  6331. vcpu->run->debug.arch.dr7 = dr7;
  6332. vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
  6333. vcpu->run->debug.arch.exception = DB_VECTOR;
  6334. vcpu->run->exit_reason = KVM_EXIT_DEBUG;
  6335. return 0;
  6336. } else {
  6337. vcpu->arch.dr6 &= ~15;
  6338. vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
  6339. kvm_queue_exception(vcpu, DB_VECTOR);
  6340. return 1;
  6341. }
  6342. }
  6343. if (vcpu->guest_debug == 0) {
  6344. vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
  6345. CPU_BASED_MOV_DR_EXITING);
  6346. /*
  6347. * No more DR vmexits; force a reload of the debug registers
  6348. * and reenter on this instruction. The next vmexit will
  6349. * retrieve the full state of the debug registers.
  6350. */
  6351. vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
  6352. return 1;
  6353. }
  6354. reg = DEBUG_REG_ACCESS_REG(exit_qualification);
  6355. if (exit_qualification & TYPE_MOV_FROM_DR) {
  6356. unsigned long val;
  6357. if (kvm_get_dr(vcpu, dr, &val))
  6358. return 1;
  6359. kvm_register_write(vcpu, reg, val);
  6360. } else
  6361. if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
  6362. return 1;
  6363. return kvm_skip_emulated_instruction(vcpu);
  6364. }
  6365. static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
  6366. {
  6367. return vcpu->arch.dr6;
  6368. }
  6369. static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
  6370. {
  6371. }
  6372. static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
  6373. {
  6374. get_debugreg(vcpu->arch.db[0], 0);
  6375. get_debugreg(vcpu->arch.db[1], 1);
  6376. get_debugreg(vcpu->arch.db[2], 2);
  6377. get_debugreg(vcpu->arch.db[3], 3);
  6378. get_debugreg(vcpu->arch.dr6, 6);
  6379. vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
  6380. vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
  6381. vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
  6382. }
  6383. static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
  6384. {
  6385. vmcs_writel(GUEST_DR7, val);
  6386. }
  6387. static int handle_cpuid(struct kvm_vcpu *vcpu)
  6388. {
  6389. return kvm_emulate_cpuid(vcpu);
  6390. }
  6391. static int handle_rdmsr(struct kvm_vcpu *vcpu)
  6392. {
  6393. u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
  6394. struct msr_data msr_info;
  6395. msr_info.index = ecx;
  6396. msr_info.host_initiated = false;
  6397. if (vmx_get_msr(vcpu, &msr_info)) {
  6398. trace_kvm_msr_read_ex(ecx);
  6399. kvm_inject_gp(vcpu, 0);
  6400. return 1;
  6401. }
  6402. trace_kvm_msr_read(ecx, msr_info.data);
  6403. /* FIXME: handling of bits 32:63 of rax, rdx */
  6404. vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
  6405. vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
  6406. return kvm_skip_emulated_instruction(vcpu);
  6407. }
  6408. static int handle_wrmsr(struct kvm_vcpu *vcpu)
  6409. {
  6410. struct msr_data msr;
  6411. u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
  6412. u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
  6413. | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
  6414. msr.data = data;
  6415. msr.index = ecx;
  6416. msr.host_initiated = false;
  6417. if (kvm_set_msr(vcpu, &msr) != 0) {
  6418. trace_kvm_msr_write_ex(ecx, data);
  6419. kvm_inject_gp(vcpu, 0);
  6420. return 1;
  6421. }
  6422. trace_kvm_msr_write(ecx, data);
  6423. return kvm_skip_emulated_instruction(vcpu);
  6424. }
  6425. static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
  6426. {
  6427. kvm_apic_update_ppr(vcpu);
  6428. return 1;
  6429. }
  6430. static int handle_interrupt_window(struct kvm_vcpu *vcpu)
  6431. {
  6432. vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
  6433. CPU_BASED_VIRTUAL_INTR_PENDING);
  6434. kvm_make_request(KVM_REQ_EVENT, vcpu);
  6435. ++vcpu->stat.irq_window_exits;
  6436. return 1;
  6437. }
  6438. static int handle_halt(struct kvm_vcpu *vcpu)
  6439. {
  6440. return kvm_emulate_halt(vcpu);
  6441. }
  6442. static int handle_vmcall(struct kvm_vcpu *vcpu)
  6443. {
  6444. return kvm_emulate_hypercall(vcpu);
  6445. }
  6446. static int handle_invd(struct kvm_vcpu *vcpu)
  6447. {
  6448. return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
  6449. }
  6450. static int handle_invlpg(struct kvm_vcpu *vcpu)
  6451. {
  6452. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6453. kvm_mmu_invlpg(vcpu, exit_qualification);
  6454. return kvm_skip_emulated_instruction(vcpu);
  6455. }
  6456. static int handle_rdpmc(struct kvm_vcpu *vcpu)
  6457. {
  6458. int err;
  6459. err = kvm_rdpmc(vcpu);
  6460. return kvm_complete_insn_gp(vcpu, err);
  6461. }
  6462. static int handle_wbinvd(struct kvm_vcpu *vcpu)
  6463. {
  6464. return kvm_emulate_wbinvd(vcpu);
  6465. }
  6466. static int handle_xsetbv(struct kvm_vcpu *vcpu)
  6467. {
  6468. u64 new_bv = kvm_read_edx_eax(vcpu);
  6469. u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
  6470. if (kvm_set_xcr(vcpu, index, new_bv) == 0)
  6471. return kvm_skip_emulated_instruction(vcpu);
  6472. return 1;
  6473. }
  6474. static int handle_xsaves(struct kvm_vcpu *vcpu)
  6475. {
  6476. kvm_skip_emulated_instruction(vcpu);
  6477. WARN(1, "this should never happen\n");
  6478. return 1;
  6479. }
  6480. static int handle_xrstors(struct kvm_vcpu *vcpu)
  6481. {
  6482. kvm_skip_emulated_instruction(vcpu);
  6483. WARN(1, "this should never happen\n");
  6484. return 1;
  6485. }
  6486. static int handle_apic_access(struct kvm_vcpu *vcpu)
  6487. {
  6488. if (likely(fasteoi)) {
  6489. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6490. int access_type, offset;
  6491. access_type = exit_qualification & APIC_ACCESS_TYPE;
  6492. offset = exit_qualification & APIC_ACCESS_OFFSET;
  6493. /*
  6494. * Sane guest uses MOV to write EOI, with written value
  6495. * not cared. So make a short-circuit here by avoiding
  6496. * heavy instruction emulation.
  6497. */
  6498. if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
  6499. (offset == APIC_EOI)) {
  6500. kvm_lapic_set_eoi(vcpu);
  6501. return kvm_skip_emulated_instruction(vcpu);
  6502. }
  6503. }
  6504. return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
  6505. }
  6506. static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
  6507. {
  6508. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6509. int vector = exit_qualification & 0xff;
  6510. /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
  6511. kvm_apic_set_eoi_accelerated(vcpu, vector);
  6512. return 1;
  6513. }
  6514. static int handle_apic_write(struct kvm_vcpu *vcpu)
  6515. {
  6516. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6517. u32 offset = exit_qualification & 0xfff;
  6518. /* APIC-write VM exit is trap-like and thus no need to adjust IP */
  6519. kvm_apic_write_nodecode(vcpu, offset);
  6520. return 1;
  6521. }
  6522. static int handle_task_switch(struct kvm_vcpu *vcpu)
  6523. {
  6524. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6525. unsigned long exit_qualification;
  6526. bool has_error_code = false;
  6527. u32 error_code = 0;
  6528. u16 tss_selector;
  6529. int reason, type, idt_v, idt_index;
  6530. idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
  6531. idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
  6532. type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
  6533. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6534. reason = (u32)exit_qualification >> 30;
  6535. if (reason == TASK_SWITCH_GATE && idt_v) {
  6536. switch (type) {
  6537. case INTR_TYPE_NMI_INTR:
  6538. vcpu->arch.nmi_injected = false;
  6539. vmx_set_nmi_mask(vcpu, true);
  6540. break;
  6541. case INTR_TYPE_EXT_INTR:
  6542. case INTR_TYPE_SOFT_INTR:
  6543. kvm_clear_interrupt_queue(vcpu);
  6544. break;
  6545. case INTR_TYPE_HARD_EXCEPTION:
  6546. if (vmx->idt_vectoring_info &
  6547. VECTORING_INFO_DELIVER_CODE_MASK) {
  6548. has_error_code = true;
  6549. error_code =
  6550. vmcs_read32(IDT_VECTORING_ERROR_CODE);
  6551. }
  6552. /* fall through */
  6553. case INTR_TYPE_SOFT_EXCEPTION:
  6554. kvm_clear_exception_queue(vcpu);
  6555. break;
  6556. default:
  6557. break;
  6558. }
  6559. }
  6560. tss_selector = exit_qualification;
  6561. if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
  6562. type != INTR_TYPE_EXT_INTR &&
  6563. type != INTR_TYPE_NMI_INTR))
  6564. skip_emulated_instruction(vcpu);
  6565. if (kvm_task_switch(vcpu, tss_selector,
  6566. type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
  6567. has_error_code, error_code) == EMULATE_FAIL) {
  6568. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  6569. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  6570. vcpu->run->internal.ndata = 0;
  6571. return 0;
  6572. }
  6573. /*
  6574. * TODO: What about debug traps on tss switch?
  6575. * Are we supposed to inject them and update dr6?
  6576. */
  6577. return 1;
  6578. }
  6579. static int handle_ept_violation(struct kvm_vcpu *vcpu)
  6580. {
  6581. unsigned long exit_qualification;
  6582. gpa_t gpa;
  6583. u64 error_code;
  6584. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  6585. /*
  6586. * EPT violation happened while executing iret from NMI,
  6587. * "blocked by NMI" bit has to be set before next VM entry.
  6588. * There are errata that may cause this bit to not be set:
  6589. * AAK134, BY25.
  6590. */
  6591. if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
  6592. enable_vnmi &&
  6593. (exit_qualification & INTR_INFO_UNBLOCK_NMI))
  6594. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
  6595. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  6596. trace_kvm_page_fault(gpa, exit_qualification);
  6597. /* Is it a read fault? */
  6598. error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
  6599. ? PFERR_USER_MASK : 0;
  6600. /* Is it a write fault? */
  6601. error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
  6602. ? PFERR_WRITE_MASK : 0;
  6603. /* Is it a fetch fault? */
  6604. error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
  6605. ? PFERR_FETCH_MASK : 0;
  6606. /* ept page table entry is present? */
  6607. error_code |= (exit_qualification &
  6608. (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
  6609. EPT_VIOLATION_EXECUTABLE))
  6610. ? PFERR_PRESENT_MASK : 0;
  6611. error_code |= (exit_qualification & 0x100) != 0 ?
  6612. PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
  6613. vcpu->arch.exit_qualification = exit_qualification;
  6614. return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
  6615. }
  6616. static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
  6617. {
  6618. gpa_t gpa;
  6619. /*
  6620. * A nested guest cannot optimize MMIO vmexits, because we have an
  6621. * nGPA here instead of the required GPA.
  6622. */
  6623. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  6624. if (!is_guest_mode(vcpu) &&
  6625. !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
  6626. trace_kvm_fast_mmio(gpa);
  6627. /*
  6628. * Doing kvm_skip_emulated_instruction() depends on undefined
  6629. * behavior: Intel's manual doesn't mandate
  6630. * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
  6631. * occurs and while on real hardware it was observed to be set,
  6632. * other hypervisors (namely Hyper-V) don't set it, we end up
  6633. * advancing IP with some random value. Disable fast mmio when
  6634. * running nested and keep it for real hardware in hope that
  6635. * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
  6636. */
  6637. if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
  6638. return kvm_skip_emulated_instruction(vcpu);
  6639. else
  6640. return kvm_emulate_instruction(vcpu, EMULTYPE_SKIP) ==
  6641. EMULATE_DONE;
  6642. }
  6643. return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
  6644. }
  6645. static int handle_nmi_window(struct kvm_vcpu *vcpu)
  6646. {
  6647. WARN_ON_ONCE(!enable_vnmi);
  6648. vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
  6649. CPU_BASED_VIRTUAL_NMI_PENDING);
  6650. ++vcpu->stat.nmi_window_exits;
  6651. kvm_make_request(KVM_REQ_EVENT, vcpu);
  6652. return 1;
  6653. }
  6654. static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
  6655. {
  6656. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6657. enum emulation_result err = EMULATE_DONE;
  6658. int ret = 1;
  6659. u32 cpu_exec_ctrl;
  6660. bool intr_window_requested;
  6661. unsigned count = 130;
  6662. /*
  6663. * We should never reach the point where we are emulating L2
  6664. * due to invalid guest state as that means we incorrectly
  6665. * allowed a nested VMEntry with an invalid vmcs12.
  6666. */
  6667. WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
  6668. cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  6669. intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
  6670. while (vmx->emulation_required && count-- != 0) {
  6671. if (intr_window_requested && vmx_interrupt_allowed(vcpu))
  6672. return handle_interrupt_window(&vmx->vcpu);
  6673. if (kvm_test_request(KVM_REQ_EVENT, vcpu))
  6674. return 1;
  6675. err = kvm_emulate_instruction(vcpu, 0);
  6676. if (err == EMULATE_USER_EXIT) {
  6677. ++vcpu->stat.mmio_exits;
  6678. ret = 0;
  6679. goto out;
  6680. }
  6681. if (err != EMULATE_DONE)
  6682. goto emulation_error;
  6683. if (vmx->emulation_required && !vmx->rmode.vm86_active &&
  6684. vcpu->arch.exception.pending)
  6685. goto emulation_error;
  6686. if (vcpu->arch.halt_request) {
  6687. vcpu->arch.halt_request = 0;
  6688. ret = kvm_vcpu_halt(vcpu);
  6689. goto out;
  6690. }
  6691. if (signal_pending(current))
  6692. goto out;
  6693. if (need_resched())
  6694. schedule();
  6695. }
  6696. out:
  6697. return ret;
  6698. emulation_error:
  6699. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  6700. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  6701. vcpu->run->internal.ndata = 0;
  6702. return 0;
  6703. }
  6704. static void grow_ple_window(struct kvm_vcpu *vcpu)
  6705. {
  6706. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6707. int old = vmx->ple_window;
  6708. vmx->ple_window = __grow_ple_window(old, ple_window,
  6709. ple_window_grow,
  6710. ple_window_max);
  6711. if (vmx->ple_window != old)
  6712. vmx->ple_window_dirty = true;
  6713. trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
  6714. }
  6715. static void shrink_ple_window(struct kvm_vcpu *vcpu)
  6716. {
  6717. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6718. int old = vmx->ple_window;
  6719. vmx->ple_window = __shrink_ple_window(old, ple_window,
  6720. ple_window_shrink,
  6721. ple_window);
  6722. if (vmx->ple_window != old)
  6723. vmx->ple_window_dirty = true;
  6724. trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
  6725. }
  6726. /*
  6727. * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
  6728. */
  6729. static void wakeup_handler(void)
  6730. {
  6731. struct kvm_vcpu *vcpu;
  6732. int cpu = smp_processor_id();
  6733. spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
  6734. list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
  6735. blocked_vcpu_list) {
  6736. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  6737. if (pi_test_on(pi_desc) == 1)
  6738. kvm_vcpu_kick(vcpu);
  6739. }
  6740. spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
  6741. }
  6742. static void vmx_enable_tdp(void)
  6743. {
  6744. kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
  6745. enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
  6746. enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
  6747. 0ull, VMX_EPT_EXECUTABLE_MASK,
  6748. cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
  6749. VMX_EPT_RWX_MASK, 0ull);
  6750. ept_set_mmio_spte_mask();
  6751. kvm_enable_tdp();
  6752. }
  6753. static __init int hardware_setup(void)
  6754. {
  6755. unsigned long host_bndcfgs;
  6756. int r = -ENOMEM, i;
  6757. rdmsrl_safe(MSR_EFER, &host_efer);
  6758. for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
  6759. kvm_define_shared_msr(i, vmx_msr_index[i]);
  6760. for (i = 0; i < VMX_BITMAP_NR; i++) {
  6761. vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
  6762. if (!vmx_bitmap[i])
  6763. goto out;
  6764. }
  6765. memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
  6766. memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
  6767. if (setup_vmcs_config(&vmcs_config) < 0) {
  6768. r = -EIO;
  6769. goto out;
  6770. }
  6771. if (boot_cpu_has(X86_FEATURE_NX))
  6772. kvm_enable_efer_bits(EFER_NX);
  6773. if (boot_cpu_has(X86_FEATURE_MPX)) {
  6774. rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
  6775. WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
  6776. }
  6777. if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
  6778. !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
  6779. enable_vpid = 0;
  6780. if (!cpu_has_vmx_ept() ||
  6781. !cpu_has_vmx_ept_4levels() ||
  6782. !cpu_has_vmx_ept_mt_wb() ||
  6783. !cpu_has_vmx_invept_global())
  6784. enable_ept = 0;
  6785. if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
  6786. enable_ept_ad_bits = 0;
  6787. if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
  6788. enable_unrestricted_guest = 0;
  6789. if (!cpu_has_vmx_flexpriority())
  6790. flexpriority_enabled = 0;
  6791. if (!cpu_has_virtual_nmis())
  6792. enable_vnmi = 0;
  6793. /*
  6794. * set_apic_access_page_addr() is used to reload apic access
  6795. * page upon invalidation. No need to do anything if not
  6796. * using the APIC_ACCESS_ADDR VMCS field.
  6797. */
  6798. if (!flexpriority_enabled)
  6799. kvm_x86_ops->set_apic_access_page_addr = NULL;
  6800. if (!cpu_has_vmx_tpr_shadow())
  6801. kvm_x86_ops->update_cr8_intercept = NULL;
  6802. if (enable_ept && !cpu_has_vmx_ept_2m_page())
  6803. kvm_disable_largepages();
  6804. #if IS_ENABLED(CONFIG_HYPERV)
  6805. if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
  6806. && enable_ept)
  6807. kvm_x86_ops->tlb_remote_flush = vmx_hv_remote_flush_tlb;
  6808. #endif
  6809. if (!cpu_has_vmx_ple()) {
  6810. ple_gap = 0;
  6811. ple_window = 0;
  6812. ple_window_grow = 0;
  6813. ple_window_max = 0;
  6814. ple_window_shrink = 0;
  6815. }
  6816. if (!cpu_has_vmx_apicv()) {
  6817. enable_apicv = 0;
  6818. kvm_x86_ops->sync_pir_to_irr = NULL;
  6819. }
  6820. if (cpu_has_vmx_tsc_scaling()) {
  6821. kvm_has_tsc_control = true;
  6822. kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
  6823. kvm_tsc_scaling_ratio_frac_bits = 48;
  6824. }
  6825. set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
  6826. if (enable_ept)
  6827. vmx_enable_tdp();
  6828. else
  6829. kvm_disable_tdp();
  6830. if (!nested) {
  6831. kvm_x86_ops->get_nested_state = NULL;
  6832. kvm_x86_ops->set_nested_state = NULL;
  6833. }
  6834. /*
  6835. * Only enable PML when hardware supports PML feature, and both EPT
  6836. * and EPT A/D bit features are enabled -- PML depends on them to work.
  6837. */
  6838. if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
  6839. enable_pml = 0;
  6840. if (!enable_pml) {
  6841. kvm_x86_ops->slot_enable_log_dirty = NULL;
  6842. kvm_x86_ops->slot_disable_log_dirty = NULL;
  6843. kvm_x86_ops->flush_log_dirty = NULL;
  6844. kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
  6845. }
  6846. if (!cpu_has_vmx_preemption_timer())
  6847. kvm_x86_ops->request_immediate_exit = __kvm_request_immediate_exit;
  6848. if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
  6849. u64 vmx_msr;
  6850. rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
  6851. cpu_preemption_timer_multi =
  6852. vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
  6853. } else {
  6854. kvm_x86_ops->set_hv_timer = NULL;
  6855. kvm_x86_ops->cancel_hv_timer = NULL;
  6856. }
  6857. if (!cpu_has_vmx_shadow_vmcs())
  6858. enable_shadow_vmcs = 0;
  6859. if (enable_shadow_vmcs)
  6860. init_vmcs_shadow_fields();
  6861. kvm_set_posted_intr_wakeup_handler(wakeup_handler);
  6862. nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
  6863. kvm_mce_cap_supported |= MCG_LMCE_P;
  6864. return alloc_kvm_area();
  6865. out:
  6866. for (i = 0; i < VMX_BITMAP_NR; i++)
  6867. free_page((unsigned long)vmx_bitmap[i]);
  6868. return r;
  6869. }
  6870. static __exit void hardware_unsetup(void)
  6871. {
  6872. int i;
  6873. for (i = 0; i < VMX_BITMAP_NR; i++)
  6874. free_page((unsigned long)vmx_bitmap[i]);
  6875. free_kvm_area();
  6876. }
  6877. /*
  6878. * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
  6879. * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
  6880. */
  6881. static int handle_pause(struct kvm_vcpu *vcpu)
  6882. {
  6883. if (!kvm_pause_in_guest(vcpu->kvm))
  6884. grow_ple_window(vcpu);
  6885. /*
  6886. * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
  6887. * VM-execution control is ignored if CPL > 0. OTOH, KVM
  6888. * never set PAUSE_EXITING and just set PLE if supported,
  6889. * so the vcpu must be CPL=0 if it gets a PAUSE exit.
  6890. */
  6891. kvm_vcpu_on_spin(vcpu, true);
  6892. return kvm_skip_emulated_instruction(vcpu);
  6893. }
  6894. static int handle_nop(struct kvm_vcpu *vcpu)
  6895. {
  6896. return kvm_skip_emulated_instruction(vcpu);
  6897. }
  6898. static int handle_mwait(struct kvm_vcpu *vcpu)
  6899. {
  6900. printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
  6901. return handle_nop(vcpu);
  6902. }
  6903. static int handle_invalid_op(struct kvm_vcpu *vcpu)
  6904. {
  6905. kvm_queue_exception(vcpu, UD_VECTOR);
  6906. return 1;
  6907. }
  6908. static int handle_monitor_trap(struct kvm_vcpu *vcpu)
  6909. {
  6910. return 1;
  6911. }
  6912. static int handle_monitor(struct kvm_vcpu *vcpu)
  6913. {
  6914. printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
  6915. return handle_nop(vcpu);
  6916. }
  6917. /*
  6918. * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
  6919. * set the success or error code of an emulated VMX instruction (as specified
  6920. * by Vol 2B, VMX Instruction Reference, "Conventions"), and skip the emulated
  6921. * instruction.
  6922. */
  6923. static int nested_vmx_succeed(struct kvm_vcpu *vcpu)
  6924. {
  6925. vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
  6926. & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
  6927. X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
  6928. return kvm_skip_emulated_instruction(vcpu);
  6929. }
  6930. static int nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
  6931. {
  6932. vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
  6933. & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
  6934. X86_EFLAGS_SF | X86_EFLAGS_OF))
  6935. | X86_EFLAGS_CF);
  6936. return kvm_skip_emulated_instruction(vcpu);
  6937. }
  6938. static int nested_vmx_failValid(struct kvm_vcpu *vcpu,
  6939. u32 vm_instruction_error)
  6940. {
  6941. struct vcpu_vmx *vmx = to_vmx(vcpu);
  6942. /*
  6943. * failValid writes the error number to the current VMCS, which
  6944. * can't be done if there isn't a current VMCS.
  6945. */
  6946. if (vmx->nested.current_vmptr == -1ull && !vmx->nested.hv_evmcs)
  6947. return nested_vmx_failInvalid(vcpu);
  6948. vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
  6949. & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
  6950. X86_EFLAGS_SF | X86_EFLAGS_OF))
  6951. | X86_EFLAGS_ZF);
  6952. get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
  6953. /*
  6954. * We don't need to force a shadow sync because
  6955. * VM_INSTRUCTION_ERROR is not shadowed
  6956. */
  6957. return kvm_skip_emulated_instruction(vcpu);
  6958. }
  6959. static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
  6960. {
  6961. /* TODO: not to reset guest simply here. */
  6962. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  6963. pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
  6964. }
  6965. static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
  6966. {
  6967. struct vcpu_vmx *vmx =
  6968. container_of(timer, struct vcpu_vmx, nested.preemption_timer);
  6969. vmx->nested.preemption_timer_expired = true;
  6970. kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
  6971. kvm_vcpu_kick(&vmx->vcpu);
  6972. return HRTIMER_NORESTART;
  6973. }
  6974. /*
  6975. * Decode the memory-address operand of a vmx instruction, as recorded on an
  6976. * exit caused by such an instruction (run by a guest hypervisor).
  6977. * On success, returns 0. When the operand is invalid, returns 1 and throws
  6978. * #UD or #GP.
  6979. */
  6980. static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
  6981. unsigned long exit_qualification,
  6982. u32 vmx_instruction_info, bool wr, gva_t *ret)
  6983. {
  6984. gva_t off;
  6985. bool exn;
  6986. struct kvm_segment s;
  6987. /*
  6988. * According to Vol. 3B, "Information for VM Exits Due to Instruction
  6989. * Execution", on an exit, vmx_instruction_info holds most of the
  6990. * addressing components of the operand. Only the displacement part
  6991. * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
  6992. * For how an actual address is calculated from all these components,
  6993. * refer to Vol. 1, "Operand Addressing".
  6994. */
  6995. int scaling = vmx_instruction_info & 3;
  6996. int addr_size = (vmx_instruction_info >> 7) & 7;
  6997. bool is_reg = vmx_instruction_info & (1u << 10);
  6998. int seg_reg = (vmx_instruction_info >> 15) & 7;
  6999. int index_reg = (vmx_instruction_info >> 18) & 0xf;
  7000. bool index_is_valid = !(vmx_instruction_info & (1u << 22));
  7001. int base_reg = (vmx_instruction_info >> 23) & 0xf;
  7002. bool base_is_valid = !(vmx_instruction_info & (1u << 27));
  7003. if (is_reg) {
  7004. kvm_queue_exception(vcpu, UD_VECTOR);
  7005. return 1;
  7006. }
  7007. /* Addr = segment_base + offset */
  7008. /* offset = base + [index * scale] + displacement */
  7009. off = exit_qualification; /* holds the displacement */
  7010. if (base_is_valid)
  7011. off += kvm_register_read(vcpu, base_reg);
  7012. if (index_is_valid)
  7013. off += kvm_register_read(vcpu, index_reg)<<scaling;
  7014. vmx_get_segment(vcpu, &s, seg_reg);
  7015. *ret = s.base + off;
  7016. if (addr_size == 1) /* 32 bit */
  7017. *ret &= 0xffffffff;
  7018. /* Checks for #GP/#SS exceptions. */
  7019. exn = false;
  7020. if (is_long_mode(vcpu)) {
  7021. /* Long mode: #GP(0)/#SS(0) if the memory address is in a
  7022. * non-canonical form. This is the only check on the memory
  7023. * destination for long mode!
  7024. */
  7025. exn = is_noncanonical_address(*ret, vcpu);
  7026. } else if (is_protmode(vcpu)) {
  7027. /* Protected mode: apply checks for segment validity in the
  7028. * following order:
  7029. * - segment type check (#GP(0) may be thrown)
  7030. * - usability check (#GP(0)/#SS(0))
  7031. * - limit check (#GP(0)/#SS(0))
  7032. */
  7033. if (wr)
  7034. /* #GP(0) if the destination operand is located in a
  7035. * read-only data segment or any code segment.
  7036. */
  7037. exn = ((s.type & 0xa) == 0 || (s.type & 8));
  7038. else
  7039. /* #GP(0) if the source operand is located in an
  7040. * execute-only code segment
  7041. */
  7042. exn = ((s.type & 0xa) == 8);
  7043. if (exn) {
  7044. kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
  7045. return 1;
  7046. }
  7047. /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
  7048. */
  7049. exn = (s.unusable != 0);
  7050. /* Protected mode: #GP(0)/#SS(0) if the memory
  7051. * operand is outside the segment limit.
  7052. */
  7053. exn = exn || (off + sizeof(u64) > s.limit);
  7054. }
  7055. if (exn) {
  7056. kvm_queue_exception_e(vcpu,
  7057. seg_reg == VCPU_SREG_SS ?
  7058. SS_VECTOR : GP_VECTOR,
  7059. 0);
  7060. return 1;
  7061. }
  7062. return 0;
  7063. }
  7064. static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
  7065. {
  7066. gva_t gva;
  7067. struct x86_exception e;
  7068. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  7069. vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
  7070. return 1;
  7071. if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
  7072. kvm_inject_page_fault(vcpu, &e);
  7073. return 1;
  7074. }
  7075. return 0;
  7076. }
  7077. /*
  7078. * Allocate a shadow VMCS and associate it with the currently loaded
  7079. * VMCS, unless such a shadow VMCS already exists. The newly allocated
  7080. * VMCS is also VMCLEARed, so that it is ready for use.
  7081. */
  7082. static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
  7083. {
  7084. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7085. struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
  7086. /*
  7087. * We should allocate a shadow vmcs for vmcs01 only when L1
  7088. * executes VMXON and free it when L1 executes VMXOFF.
  7089. * As it is invalid to execute VMXON twice, we shouldn't reach
  7090. * here when vmcs01 already have an allocated shadow vmcs.
  7091. */
  7092. WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
  7093. if (!loaded_vmcs->shadow_vmcs) {
  7094. loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
  7095. if (loaded_vmcs->shadow_vmcs)
  7096. vmcs_clear(loaded_vmcs->shadow_vmcs);
  7097. }
  7098. return loaded_vmcs->shadow_vmcs;
  7099. }
  7100. static int enter_vmx_operation(struct kvm_vcpu *vcpu)
  7101. {
  7102. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7103. int r;
  7104. r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
  7105. if (r < 0)
  7106. goto out_vmcs02;
  7107. vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
  7108. if (!vmx->nested.cached_vmcs12)
  7109. goto out_cached_vmcs12;
  7110. vmx->nested.cached_shadow_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
  7111. if (!vmx->nested.cached_shadow_vmcs12)
  7112. goto out_cached_shadow_vmcs12;
  7113. if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
  7114. goto out_shadow_vmcs;
  7115. hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
  7116. HRTIMER_MODE_REL_PINNED);
  7117. vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
  7118. vmx->nested.vpid02 = allocate_vpid();
  7119. vmx->nested.vmcs02_initialized = false;
  7120. vmx->nested.vmxon = true;
  7121. return 0;
  7122. out_shadow_vmcs:
  7123. kfree(vmx->nested.cached_shadow_vmcs12);
  7124. out_cached_shadow_vmcs12:
  7125. kfree(vmx->nested.cached_vmcs12);
  7126. out_cached_vmcs12:
  7127. free_loaded_vmcs(&vmx->nested.vmcs02);
  7128. out_vmcs02:
  7129. return -ENOMEM;
  7130. }
  7131. /*
  7132. * Emulate the VMXON instruction.
  7133. * Currently, we just remember that VMX is active, and do not save or even
  7134. * inspect the argument to VMXON (the so-called "VMXON pointer") because we
  7135. * do not currently need to store anything in that guest-allocated memory
  7136. * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
  7137. * argument is different from the VMXON pointer (which the spec says they do).
  7138. */
  7139. static int handle_vmon(struct kvm_vcpu *vcpu)
  7140. {
  7141. int ret;
  7142. gpa_t vmptr;
  7143. struct page *page;
  7144. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7145. const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
  7146. | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  7147. /*
  7148. * The Intel VMX Instruction Reference lists a bunch of bits that are
  7149. * prerequisite to running VMXON, most notably cr4.VMXE must be set to
  7150. * 1 (see vmx_set_cr4() for when we allow the guest to set this).
  7151. * Otherwise, we should fail with #UD. But most faulting conditions
  7152. * have already been checked by hardware, prior to the VM-exit for
  7153. * VMXON. We do test guest cr4.VMXE because processor CR4 always has
  7154. * that bit set to 1 in non-root mode.
  7155. */
  7156. if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
  7157. kvm_queue_exception(vcpu, UD_VECTOR);
  7158. return 1;
  7159. }
  7160. /* CPL=0 must be checked manually. */
  7161. if (vmx_get_cpl(vcpu)) {
  7162. kvm_inject_gp(vcpu, 0);
  7163. return 1;
  7164. }
  7165. if (vmx->nested.vmxon)
  7166. return nested_vmx_failValid(vcpu,
  7167. VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
  7168. if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
  7169. != VMXON_NEEDED_FEATURES) {
  7170. kvm_inject_gp(vcpu, 0);
  7171. return 1;
  7172. }
  7173. if (nested_vmx_get_vmptr(vcpu, &vmptr))
  7174. return 1;
  7175. /*
  7176. * SDM 3: 24.11.5
  7177. * The first 4 bytes of VMXON region contain the supported
  7178. * VMCS revision identifier
  7179. *
  7180. * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
  7181. * which replaces physical address width with 32
  7182. */
  7183. if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu)))
  7184. return nested_vmx_failInvalid(vcpu);
  7185. page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
  7186. if (is_error_page(page))
  7187. return nested_vmx_failInvalid(vcpu);
  7188. if (*(u32 *)kmap(page) != VMCS12_REVISION) {
  7189. kunmap(page);
  7190. kvm_release_page_clean(page);
  7191. return nested_vmx_failInvalid(vcpu);
  7192. }
  7193. kunmap(page);
  7194. kvm_release_page_clean(page);
  7195. vmx->nested.vmxon_ptr = vmptr;
  7196. ret = enter_vmx_operation(vcpu);
  7197. if (ret)
  7198. return ret;
  7199. return nested_vmx_succeed(vcpu);
  7200. }
  7201. /*
  7202. * Intel's VMX Instruction Reference specifies a common set of prerequisites
  7203. * for running VMX instructions (except VMXON, whose prerequisites are
  7204. * slightly different). It also specifies what exception to inject otherwise.
  7205. * Note that many of these exceptions have priority over VM exits, so they
  7206. * don't have to be checked again here.
  7207. */
  7208. static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
  7209. {
  7210. if (!to_vmx(vcpu)->nested.vmxon) {
  7211. kvm_queue_exception(vcpu, UD_VECTOR);
  7212. return 0;
  7213. }
  7214. if (vmx_get_cpl(vcpu)) {
  7215. kvm_inject_gp(vcpu, 0);
  7216. return 0;
  7217. }
  7218. return 1;
  7219. }
  7220. static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
  7221. {
  7222. vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
  7223. vmcs_write64(VMCS_LINK_POINTER, -1ull);
  7224. }
  7225. static inline void nested_release_evmcs(struct kvm_vcpu *vcpu)
  7226. {
  7227. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7228. if (!vmx->nested.hv_evmcs)
  7229. return;
  7230. kunmap(vmx->nested.hv_evmcs_page);
  7231. kvm_release_page_dirty(vmx->nested.hv_evmcs_page);
  7232. vmx->nested.hv_evmcs_vmptr = -1ull;
  7233. vmx->nested.hv_evmcs_page = NULL;
  7234. vmx->nested.hv_evmcs = NULL;
  7235. }
  7236. static inline void nested_release_vmcs12(struct kvm_vcpu *vcpu)
  7237. {
  7238. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7239. if (vmx->nested.current_vmptr == -1ull)
  7240. return;
  7241. if (enable_shadow_vmcs) {
  7242. /* copy to memory all shadowed fields in case
  7243. they were modified */
  7244. copy_shadow_to_vmcs12(vmx);
  7245. vmx->nested.need_vmcs12_sync = false;
  7246. vmx_disable_shadow_vmcs(vmx);
  7247. }
  7248. vmx->nested.posted_intr_nv = -1;
  7249. /* Flush VMCS12 to guest memory */
  7250. kvm_vcpu_write_guest_page(vcpu,
  7251. vmx->nested.current_vmptr >> PAGE_SHIFT,
  7252. vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
  7253. kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
  7254. vmx->nested.current_vmptr = -1ull;
  7255. }
  7256. /*
  7257. * Free whatever needs to be freed from vmx->nested when L1 goes down, or
  7258. * just stops using VMX.
  7259. */
  7260. static void free_nested(struct kvm_vcpu *vcpu)
  7261. {
  7262. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7263. if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
  7264. return;
  7265. vmx->nested.vmxon = false;
  7266. vmx->nested.smm.vmxon = false;
  7267. free_vpid(vmx->nested.vpid02);
  7268. vmx->nested.posted_intr_nv = -1;
  7269. vmx->nested.current_vmptr = -1ull;
  7270. if (enable_shadow_vmcs) {
  7271. vmx_disable_shadow_vmcs(vmx);
  7272. vmcs_clear(vmx->vmcs01.shadow_vmcs);
  7273. free_vmcs(vmx->vmcs01.shadow_vmcs);
  7274. vmx->vmcs01.shadow_vmcs = NULL;
  7275. }
  7276. kfree(vmx->nested.cached_vmcs12);
  7277. kfree(vmx->nested.cached_shadow_vmcs12);
  7278. /* Unpin physical memory we referred to in the vmcs02 */
  7279. if (vmx->nested.apic_access_page) {
  7280. kvm_release_page_dirty(vmx->nested.apic_access_page);
  7281. vmx->nested.apic_access_page = NULL;
  7282. }
  7283. if (vmx->nested.virtual_apic_page) {
  7284. kvm_release_page_dirty(vmx->nested.virtual_apic_page);
  7285. vmx->nested.virtual_apic_page = NULL;
  7286. }
  7287. if (vmx->nested.pi_desc_page) {
  7288. kunmap(vmx->nested.pi_desc_page);
  7289. kvm_release_page_dirty(vmx->nested.pi_desc_page);
  7290. vmx->nested.pi_desc_page = NULL;
  7291. vmx->nested.pi_desc = NULL;
  7292. }
  7293. kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
  7294. nested_release_evmcs(vcpu);
  7295. free_loaded_vmcs(&vmx->nested.vmcs02);
  7296. }
  7297. /* Emulate the VMXOFF instruction */
  7298. static int handle_vmoff(struct kvm_vcpu *vcpu)
  7299. {
  7300. if (!nested_vmx_check_permission(vcpu))
  7301. return 1;
  7302. free_nested(vcpu);
  7303. return nested_vmx_succeed(vcpu);
  7304. }
  7305. /* Emulate the VMCLEAR instruction */
  7306. static int handle_vmclear(struct kvm_vcpu *vcpu)
  7307. {
  7308. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7309. u32 zero = 0;
  7310. gpa_t vmptr;
  7311. if (!nested_vmx_check_permission(vcpu))
  7312. return 1;
  7313. if (nested_vmx_get_vmptr(vcpu, &vmptr))
  7314. return 1;
  7315. if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu)))
  7316. return nested_vmx_failValid(vcpu,
  7317. VMXERR_VMCLEAR_INVALID_ADDRESS);
  7318. if (vmptr == vmx->nested.vmxon_ptr)
  7319. return nested_vmx_failValid(vcpu,
  7320. VMXERR_VMCLEAR_VMXON_POINTER);
  7321. if (vmx->nested.hv_evmcs_page) {
  7322. if (vmptr == vmx->nested.hv_evmcs_vmptr)
  7323. nested_release_evmcs(vcpu);
  7324. } else {
  7325. if (vmptr == vmx->nested.current_vmptr)
  7326. nested_release_vmcs12(vcpu);
  7327. kvm_vcpu_write_guest(vcpu,
  7328. vmptr + offsetof(struct vmcs12,
  7329. launch_state),
  7330. &zero, sizeof(zero));
  7331. }
  7332. return nested_vmx_succeed(vcpu);
  7333. }
  7334. static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
  7335. /* Emulate the VMLAUNCH instruction */
  7336. static int handle_vmlaunch(struct kvm_vcpu *vcpu)
  7337. {
  7338. return nested_vmx_run(vcpu, true);
  7339. }
  7340. /* Emulate the VMRESUME instruction */
  7341. static int handle_vmresume(struct kvm_vcpu *vcpu)
  7342. {
  7343. return nested_vmx_run(vcpu, false);
  7344. }
  7345. /*
  7346. * Read a vmcs12 field. Since these can have varying lengths and we return
  7347. * one type, we chose the biggest type (u64) and zero-extend the return value
  7348. * to that size. Note that the caller, handle_vmread, might need to use only
  7349. * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
  7350. * 64-bit fields are to be returned).
  7351. */
  7352. static inline int vmcs12_read_any(struct vmcs12 *vmcs12,
  7353. unsigned long field, u64 *ret)
  7354. {
  7355. short offset = vmcs_field_to_offset(field);
  7356. char *p;
  7357. if (offset < 0)
  7358. return offset;
  7359. p = (char *)vmcs12 + offset;
  7360. switch (vmcs_field_width(field)) {
  7361. case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
  7362. *ret = *((natural_width *)p);
  7363. return 0;
  7364. case VMCS_FIELD_WIDTH_U16:
  7365. *ret = *((u16 *)p);
  7366. return 0;
  7367. case VMCS_FIELD_WIDTH_U32:
  7368. *ret = *((u32 *)p);
  7369. return 0;
  7370. case VMCS_FIELD_WIDTH_U64:
  7371. *ret = *((u64 *)p);
  7372. return 0;
  7373. default:
  7374. WARN_ON(1);
  7375. return -ENOENT;
  7376. }
  7377. }
  7378. static inline int vmcs12_write_any(struct vmcs12 *vmcs12,
  7379. unsigned long field, u64 field_value){
  7380. short offset = vmcs_field_to_offset(field);
  7381. char *p = (char *)vmcs12 + offset;
  7382. if (offset < 0)
  7383. return offset;
  7384. switch (vmcs_field_width(field)) {
  7385. case VMCS_FIELD_WIDTH_U16:
  7386. *(u16 *)p = field_value;
  7387. return 0;
  7388. case VMCS_FIELD_WIDTH_U32:
  7389. *(u32 *)p = field_value;
  7390. return 0;
  7391. case VMCS_FIELD_WIDTH_U64:
  7392. *(u64 *)p = field_value;
  7393. return 0;
  7394. case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
  7395. *(natural_width *)p = field_value;
  7396. return 0;
  7397. default:
  7398. WARN_ON(1);
  7399. return -ENOENT;
  7400. }
  7401. }
  7402. static int copy_enlightened_to_vmcs12(struct vcpu_vmx *vmx)
  7403. {
  7404. struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
  7405. struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
  7406. vmcs12->hdr.revision_id = evmcs->revision_id;
  7407. /* HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE */
  7408. vmcs12->tpr_threshold = evmcs->tpr_threshold;
  7409. vmcs12->guest_rip = evmcs->guest_rip;
  7410. if (unlikely(!(evmcs->hv_clean_fields &
  7411. HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC))) {
  7412. vmcs12->guest_rsp = evmcs->guest_rsp;
  7413. vmcs12->guest_rflags = evmcs->guest_rflags;
  7414. vmcs12->guest_interruptibility_info =
  7415. evmcs->guest_interruptibility_info;
  7416. }
  7417. if (unlikely(!(evmcs->hv_clean_fields &
  7418. HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC))) {
  7419. vmcs12->cpu_based_vm_exec_control =
  7420. evmcs->cpu_based_vm_exec_control;
  7421. }
  7422. if (unlikely(!(evmcs->hv_clean_fields &
  7423. HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC))) {
  7424. vmcs12->exception_bitmap = evmcs->exception_bitmap;
  7425. }
  7426. if (unlikely(!(evmcs->hv_clean_fields &
  7427. HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_ENTRY))) {
  7428. vmcs12->vm_entry_controls = evmcs->vm_entry_controls;
  7429. }
  7430. if (unlikely(!(evmcs->hv_clean_fields &
  7431. HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT))) {
  7432. vmcs12->vm_entry_intr_info_field =
  7433. evmcs->vm_entry_intr_info_field;
  7434. vmcs12->vm_entry_exception_error_code =
  7435. evmcs->vm_entry_exception_error_code;
  7436. vmcs12->vm_entry_instruction_len =
  7437. evmcs->vm_entry_instruction_len;
  7438. }
  7439. if (unlikely(!(evmcs->hv_clean_fields &
  7440. HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1))) {
  7441. vmcs12->host_ia32_pat = evmcs->host_ia32_pat;
  7442. vmcs12->host_ia32_efer = evmcs->host_ia32_efer;
  7443. vmcs12->host_cr0 = evmcs->host_cr0;
  7444. vmcs12->host_cr3 = evmcs->host_cr3;
  7445. vmcs12->host_cr4 = evmcs->host_cr4;
  7446. vmcs12->host_ia32_sysenter_esp = evmcs->host_ia32_sysenter_esp;
  7447. vmcs12->host_ia32_sysenter_eip = evmcs->host_ia32_sysenter_eip;
  7448. vmcs12->host_rip = evmcs->host_rip;
  7449. vmcs12->host_ia32_sysenter_cs = evmcs->host_ia32_sysenter_cs;
  7450. vmcs12->host_es_selector = evmcs->host_es_selector;
  7451. vmcs12->host_cs_selector = evmcs->host_cs_selector;
  7452. vmcs12->host_ss_selector = evmcs->host_ss_selector;
  7453. vmcs12->host_ds_selector = evmcs->host_ds_selector;
  7454. vmcs12->host_fs_selector = evmcs->host_fs_selector;
  7455. vmcs12->host_gs_selector = evmcs->host_gs_selector;
  7456. vmcs12->host_tr_selector = evmcs->host_tr_selector;
  7457. }
  7458. if (unlikely(!(evmcs->hv_clean_fields &
  7459. HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1))) {
  7460. vmcs12->pin_based_vm_exec_control =
  7461. evmcs->pin_based_vm_exec_control;
  7462. vmcs12->vm_exit_controls = evmcs->vm_exit_controls;
  7463. vmcs12->secondary_vm_exec_control =
  7464. evmcs->secondary_vm_exec_control;
  7465. }
  7466. if (unlikely(!(evmcs->hv_clean_fields &
  7467. HV_VMX_ENLIGHTENED_CLEAN_FIELD_IO_BITMAP))) {
  7468. vmcs12->io_bitmap_a = evmcs->io_bitmap_a;
  7469. vmcs12->io_bitmap_b = evmcs->io_bitmap_b;
  7470. }
  7471. if (unlikely(!(evmcs->hv_clean_fields &
  7472. HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP))) {
  7473. vmcs12->msr_bitmap = evmcs->msr_bitmap;
  7474. }
  7475. if (unlikely(!(evmcs->hv_clean_fields &
  7476. HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2))) {
  7477. vmcs12->guest_es_base = evmcs->guest_es_base;
  7478. vmcs12->guest_cs_base = evmcs->guest_cs_base;
  7479. vmcs12->guest_ss_base = evmcs->guest_ss_base;
  7480. vmcs12->guest_ds_base = evmcs->guest_ds_base;
  7481. vmcs12->guest_fs_base = evmcs->guest_fs_base;
  7482. vmcs12->guest_gs_base = evmcs->guest_gs_base;
  7483. vmcs12->guest_ldtr_base = evmcs->guest_ldtr_base;
  7484. vmcs12->guest_tr_base = evmcs->guest_tr_base;
  7485. vmcs12->guest_gdtr_base = evmcs->guest_gdtr_base;
  7486. vmcs12->guest_idtr_base = evmcs->guest_idtr_base;
  7487. vmcs12->guest_es_limit = evmcs->guest_es_limit;
  7488. vmcs12->guest_cs_limit = evmcs->guest_cs_limit;
  7489. vmcs12->guest_ss_limit = evmcs->guest_ss_limit;
  7490. vmcs12->guest_ds_limit = evmcs->guest_ds_limit;
  7491. vmcs12->guest_fs_limit = evmcs->guest_fs_limit;
  7492. vmcs12->guest_gs_limit = evmcs->guest_gs_limit;
  7493. vmcs12->guest_ldtr_limit = evmcs->guest_ldtr_limit;
  7494. vmcs12->guest_tr_limit = evmcs->guest_tr_limit;
  7495. vmcs12->guest_gdtr_limit = evmcs->guest_gdtr_limit;
  7496. vmcs12->guest_idtr_limit = evmcs->guest_idtr_limit;
  7497. vmcs12->guest_es_ar_bytes = evmcs->guest_es_ar_bytes;
  7498. vmcs12->guest_cs_ar_bytes = evmcs->guest_cs_ar_bytes;
  7499. vmcs12->guest_ss_ar_bytes = evmcs->guest_ss_ar_bytes;
  7500. vmcs12->guest_ds_ar_bytes = evmcs->guest_ds_ar_bytes;
  7501. vmcs12->guest_fs_ar_bytes = evmcs->guest_fs_ar_bytes;
  7502. vmcs12->guest_gs_ar_bytes = evmcs->guest_gs_ar_bytes;
  7503. vmcs12->guest_ldtr_ar_bytes = evmcs->guest_ldtr_ar_bytes;
  7504. vmcs12->guest_tr_ar_bytes = evmcs->guest_tr_ar_bytes;
  7505. vmcs12->guest_es_selector = evmcs->guest_es_selector;
  7506. vmcs12->guest_cs_selector = evmcs->guest_cs_selector;
  7507. vmcs12->guest_ss_selector = evmcs->guest_ss_selector;
  7508. vmcs12->guest_ds_selector = evmcs->guest_ds_selector;
  7509. vmcs12->guest_fs_selector = evmcs->guest_fs_selector;
  7510. vmcs12->guest_gs_selector = evmcs->guest_gs_selector;
  7511. vmcs12->guest_ldtr_selector = evmcs->guest_ldtr_selector;
  7512. vmcs12->guest_tr_selector = evmcs->guest_tr_selector;
  7513. }
  7514. if (unlikely(!(evmcs->hv_clean_fields &
  7515. HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2))) {
  7516. vmcs12->tsc_offset = evmcs->tsc_offset;
  7517. vmcs12->virtual_apic_page_addr = evmcs->virtual_apic_page_addr;
  7518. vmcs12->xss_exit_bitmap = evmcs->xss_exit_bitmap;
  7519. }
  7520. if (unlikely(!(evmcs->hv_clean_fields &
  7521. HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR))) {
  7522. vmcs12->cr0_guest_host_mask = evmcs->cr0_guest_host_mask;
  7523. vmcs12->cr4_guest_host_mask = evmcs->cr4_guest_host_mask;
  7524. vmcs12->cr0_read_shadow = evmcs->cr0_read_shadow;
  7525. vmcs12->cr4_read_shadow = evmcs->cr4_read_shadow;
  7526. vmcs12->guest_cr0 = evmcs->guest_cr0;
  7527. vmcs12->guest_cr3 = evmcs->guest_cr3;
  7528. vmcs12->guest_cr4 = evmcs->guest_cr4;
  7529. vmcs12->guest_dr7 = evmcs->guest_dr7;
  7530. }
  7531. if (unlikely(!(evmcs->hv_clean_fields &
  7532. HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER))) {
  7533. vmcs12->host_fs_base = evmcs->host_fs_base;
  7534. vmcs12->host_gs_base = evmcs->host_gs_base;
  7535. vmcs12->host_tr_base = evmcs->host_tr_base;
  7536. vmcs12->host_gdtr_base = evmcs->host_gdtr_base;
  7537. vmcs12->host_idtr_base = evmcs->host_idtr_base;
  7538. vmcs12->host_rsp = evmcs->host_rsp;
  7539. }
  7540. if (unlikely(!(evmcs->hv_clean_fields &
  7541. HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_XLAT))) {
  7542. vmcs12->ept_pointer = evmcs->ept_pointer;
  7543. vmcs12->virtual_processor_id = evmcs->virtual_processor_id;
  7544. }
  7545. if (unlikely(!(evmcs->hv_clean_fields &
  7546. HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1))) {
  7547. vmcs12->vmcs_link_pointer = evmcs->vmcs_link_pointer;
  7548. vmcs12->guest_ia32_debugctl = evmcs->guest_ia32_debugctl;
  7549. vmcs12->guest_ia32_pat = evmcs->guest_ia32_pat;
  7550. vmcs12->guest_ia32_efer = evmcs->guest_ia32_efer;
  7551. vmcs12->guest_pdptr0 = evmcs->guest_pdptr0;
  7552. vmcs12->guest_pdptr1 = evmcs->guest_pdptr1;
  7553. vmcs12->guest_pdptr2 = evmcs->guest_pdptr2;
  7554. vmcs12->guest_pdptr3 = evmcs->guest_pdptr3;
  7555. vmcs12->guest_pending_dbg_exceptions =
  7556. evmcs->guest_pending_dbg_exceptions;
  7557. vmcs12->guest_sysenter_esp = evmcs->guest_sysenter_esp;
  7558. vmcs12->guest_sysenter_eip = evmcs->guest_sysenter_eip;
  7559. vmcs12->guest_bndcfgs = evmcs->guest_bndcfgs;
  7560. vmcs12->guest_activity_state = evmcs->guest_activity_state;
  7561. vmcs12->guest_sysenter_cs = evmcs->guest_sysenter_cs;
  7562. }
  7563. /*
  7564. * Not used?
  7565. * vmcs12->vm_exit_msr_store_addr = evmcs->vm_exit_msr_store_addr;
  7566. * vmcs12->vm_exit_msr_load_addr = evmcs->vm_exit_msr_load_addr;
  7567. * vmcs12->vm_entry_msr_load_addr = evmcs->vm_entry_msr_load_addr;
  7568. * vmcs12->cr3_target_value0 = evmcs->cr3_target_value0;
  7569. * vmcs12->cr3_target_value1 = evmcs->cr3_target_value1;
  7570. * vmcs12->cr3_target_value2 = evmcs->cr3_target_value2;
  7571. * vmcs12->cr3_target_value3 = evmcs->cr3_target_value3;
  7572. * vmcs12->page_fault_error_code_mask =
  7573. * evmcs->page_fault_error_code_mask;
  7574. * vmcs12->page_fault_error_code_match =
  7575. * evmcs->page_fault_error_code_match;
  7576. * vmcs12->cr3_target_count = evmcs->cr3_target_count;
  7577. * vmcs12->vm_exit_msr_store_count = evmcs->vm_exit_msr_store_count;
  7578. * vmcs12->vm_exit_msr_load_count = evmcs->vm_exit_msr_load_count;
  7579. * vmcs12->vm_entry_msr_load_count = evmcs->vm_entry_msr_load_count;
  7580. */
  7581. /*
  7582. * Read only fields:
  7583. * vmcs12->guest_physical_address = evmcs->guest_physical_address;
  7584. * vmcs12->vm_instruction_error = evmcs->vm_instruction_error;
  7585. * vmcs12->vm_exit_reason = evmcs->vm_exit_reason;
  7586. * vmcs12->vm_exit_intr_info = evmcs->vm_exit_intr_info;
  7587. * vmcs12->vm_exit_intr_error_code = evmcs->vm_exit_intr_error_code;
  7588. * vmcs12->idt_vectoring_info_field = evmcs->idt_vectoring_info_field;
  7589. * vmcs12->idt_vectoring_error_code = evmcs->idt_vectoring_error_code;
  7590. * vmcs12->vm_exit_instruction_len = evmcs->vm_exit_instruction_len;
  7591. * vmcs12->vmx_instruction_info = evmcs->vmx_instruction_info;
  7592. * vmcs12->exit_qualification = evmcs->exit_qualification;
  7593. * vmcs12->guest_linear_address = evmcs->guest_linear_address;
  7594. *
  7595. * Not present in struct vmcs12:
  7596. * vmcs12->exit_io_instruction_ecx = evmcs->exit_io_instruction_ecx;
  7597. * vmcs12->exit_io_instruction_esi = evmcs->exit_io_instruction_esi;
  7598. * vmcs12->exit_io_instruction_edi = evmcs->exit_io_instruction_edi;
  7599. * vmcs12->exit_io_instruction_eip = evmcs->exit_io_instruction_eip;
  7600. */
  7601. return 0;
  7602. }
  7603. static int copy_vmcs12_to_enlightened(struct vcpu_vmx *vmx)
  7604. {
  7605. struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12;
  7606. struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs;
  7607. /*
  7608. * Should not be changed by KVM:
  7609. *
  7610. * evmcs->host_es_selector = vmcs12->host_es_selector;
  7611. * evmcs->host_cs_selector = vmcs12->host_cs_selector;
  7612. * evmcs->host_ss_selector = vmcs12->host_ss_selector;
  7613. * evmcs->host_ds_selector = vmcs12->host_ds_selector;
  7614. * evmcs->host_fs_selector = vmcs12->host_fs_selector;
  7615. * evmcs->host_gs_selector = vmcs12->host_gs_selector;
  7616. * evmcs->host_tr_selector = vmcs12->host_tr_selector;
  7617. * evmcs->host_ia32_pat = vmcs12->host_ia32_pat;
  7618. * evmcs->host_ia32_efer = vmcs12->host_ia32_efer;
  7619. * evmcs->host_cr0 = vmcs12->host_cr0;
  7620. * evmcs->host_cr3 = vmcs12->host_cr3;
  7621. * evmcs->host_cr4 = vmcs12->host_cr4;
  7622. * evmcs->host_ia32_sysenter_esp = vmcs12->host_ia32_sysenter_esp;
  7623. * evmcs->host_ia32_sysenter_eip = vmcs12->host_ia32_sysenter_eip;
  7624. * evmcs->host_rip = vmcs12->host_rip;
  7625. * evmcs->host_ia32_sysenter_cs = vmcs12->host_ia32_sysenter_cs;
  7626. * evmcs->host_fs_base = vmcs12->host_fs_base;
  7627. * evmcs->host_gs_base = vmcs12->host_gs_base;
  7628. * evmcs->host_tr_base = vmcs12->host_tr_base;
  7629. * evmcs->host_gdtr_base = vmcs12->host_gdtr_base;
  7630. * evmcs->host_idtr_base = vmcs12->host_idtr_base;
  7631. * evmcs->host_rsp = vmcs12->host_rsp;
  7632. * sync_vmcs12() doesn't read these:
  7633. * evmcs->io_bitmap_a = vmcs12->io_bitmap_a;
  7634. * evmcs->io_bitmap_b = vmcs12->io_bitmap_b;
  7635. * evmcs->msr_bitmap = vmcs12->msr_bitmap;
  7636. * evmcs->ept_pointer = vmcs12->ept_pointer;
  7637. * evmcs->xss_exit_bitmap = vmcs12->xss_exit_bitmap;
  7638. * evmcs->vm_exit_msr_store_addr = vmcs12->vm_exit_msr_store_addr;
  7639. * evmcs->vm_exit_msr_load_addr = vmcs12->vm_exit_msr_load_addr;
  7640. * evmcs->vm_entry_msr_load_addr = vmcs12->vm_entry_msr_load_addr;
  7641. * evmcs->cr3_target_value0 = vmcs12->cr3_target_value0;
  7642. * evmcs->cr3_target_value1 = vmcs12->cr3_target_value1;
  7643. * evmcs->cr3_target_value2 = vmcs12->cr3_target_value2;
  7644. * evmcs->cr3_target_value3 = vmcs12->cr3_target_value3;
  7645. * evmcs->tpr_threshold = vmcs12->tpr_threshold;
  7646. * evmcs->virtual_processor_id = vmcs12->virtual_processor_id;
  7647. * evmcs->exception_bitmap = vmcs12->exception_bitmap;
  7648. * evmcs->vmcs_link_pointer = vmcs12->vmcs_link_pointer;
  7649. * evmcs->pin_based_vm_exec_control = vmcs12->pin_based_vm_exec_control;
  7650. * evmcs->vm_exit_controls = vmcs12->vm_exit_controls;
  7651. * evmcs->secondary_vm_exec_control = vmcs12->secondary_vm_exec_control;
  7652. * evmcs->page_fault_error_code_mask =
  7653. * vmcs12->page_fault_error_code_mask;
  7654. * evmcs->page_fault_error_code_match =
  7655. * vmcs12->page_fault_error_code_match;
  7656. * evmcs->cr3_target_count = vmcs12->cr3_target_count;
  7657. * evmcs->virtual_apic_page_addr = vmcs12->virtual_apic_page_addr;
  7658. * evmcs->tsc_offset = vmcs12->tsc_offset;
  7659. * evmcs->guest_ia32_debugctl = vmcs12->guest_ia32_debugctl;
  7660. * evmcs->cr0_guest_host_mask = vmcs12->cr0_guest_host_mask;
  7661. * evmcs->cr4_guest_host_mask = vmcs12->cr4_guest_host_mask;
  7662. * evmcs->cr0_read_shadow = vmcs12->cr0_read_shadow;
  7663. * evmcs->cr4_read_shadow = vmcs12->cr4_read_shadow;
  7664. * evmcs->vm_exit_msr_store_count = vmcs12->vm_exit_msr_store_count;
  7665. * evmcs->vm_exit_msr_load_count = vmcs12->vm_exit_msr_load_count;
  7666. * evmcs->vm_entry_msr_load_count = vmcs12->vm_entry_msr_load_count;
  7667. *
  7668. * Not present in struct vmcs12:
  7669. * evmcs->exit_io_instruction_ecx = vmcs12->exit_io_instruction_ecx;
  7670. * evmcs->exit_io_instruction_esi = vmcs12->exit_io_instruction_esi;
  7671. * evmcs->exit_io_instruction_edi = vmcs12->exit_io_instruction_edi;
  7672. * evmcs->exit_io_instruction_eip = vmcs12->exit_io_instruction_eip;
  7673. */
  7674. evmcs->guest_es_selector = vmcs12->guest_es_selector;
  7675. evmcs->guest_cs_selector = vmcs12->guest_cs_selector;
  7676. evmcs->guest_ss_selector = vmcs12->guest_ss_selector;
  7677. evmcs->guest_ds_selector = vmcs12->guest_ds_selector;
  7678. evmcs->guest_fs_selector = vmcs12->guest_fs_selector;
  7679. evmcs->guest_gs_selector = vmcs12->guest_gs_selector;
  7680. evmcs->guest_ldtr_selector = vmcs12->guest_ldtr_selector;
  7681. evmcs->guest_tr_selector = vmcs12->guest_tr_selector;
  7682. evmcs->guest_es_limit = vmcs12->guest_es_limit;
  7683. evmcs->guest_cs_limit = vmcs12->guest_cs_limit;
  7684. evmcs->guest_ss_limit = vmcs12->guest_ss_limit;
  7685. evmcs->guest_ds_limit = vmcs12->guest_ds_limit;
  7686. evmcs->guest_fs_limit = vmcs12->guest_fs_limit;
  7687. evmcs->guest_gs_limit = vmcs12->guest_gs_limit;
  7688. evmcs->guest_ldtr_limit = vmcs12->guest_ldtr_limit;
  7689. evmcs->guest_tr_limit = vmcs12->guest_tr_limit;
  7690. evmcs->guest_gdtr_limit = vmcs12->guest_gdtr_limit;
  7691. evmcs->guest_idtr_limit = vmcs12->guest_idtr_limit;
  7692. evmcs->guest_es_ar_bytes = vmcs12->guest_es_ar_bytes;
  7693. evmcs->guest_cs_ar_bytes = vmcs12->guest_cs_ar_bytes;
  7694. evmcs->guest_ss_ar_bytes = vmcs12->guest_ss_ar_bytes;
  7695. evmcs->guest_ds_ar_bytes = vmcs12->guest_ds_ar_bytes;
  7696. evmcs->guest_fs_ar_bytes = vmcs12->guest_fs_ar_bytes;
  7697. evmcs->guest_gs_ar_bytes = vmcs12->guest_gs_ar_bytes;
  7698. evmcs->guest_ldtr_ar_bytes = vmcs12->guest_ldtr_ar_bytes;
  7699. evmcs->guest_tr_ar_bytes = vmcs12->guest_tr_ar_bytes;
  7700. evmcs->guest_es_base = vmcs12->guest_es_base;
  7701. evmcs->guest_cs_base = vmcs12->guest_cs_base;
  7702. evmcs->guest_ss_base = vmcs12->guest_ss_base;
  7703. evmcs->guest_ds_base = vmcs12->guest_ds_base;
  7704. evmcs->guest_fs_base = vmcs12->guest_fs_base;
  7705. evmcs->guest_gs_base = vmcs12->guest_gs_base;
  7706. evmcs->guest_ldtr_base = vmcs12->guest_ldtr_base;
  7707. evmcs->guest_tr_base = vmcs12->guest_tr_base;
  7708. evmcs->guest_gdtr_base = vmcs12->guest_gdtr_base;
  7709. evmcs->guest_idtr_base = vmcs12->guest_idtr_base;
  7710. evmcs->guest_ia32_pat = vmcs12->guest_ia32_pat;
  7711. evmcs->guest_ia32_efer = vmcs12->guest_ia32_efer;
  7712. evmcs->guest_pdptr0 = vmcs12->guest_pdptr0;
  7713. evmcs->guest_pdptr1 = vmcs12->guest_pdptr1;
  7714. evmcs->guest_pdptr2 = vmcs12->guest_pdptr2;
  7715. evmcs->guest_pdptr3 = vmcs12->guest_pdptr3;
  7716. evmcs->guest_pending_dbg_exceptions =
  7717. vmcs12->guest_pending_dbg_exceptions;
  7718. evmcs->guest_sysenter_esp = vmcs12->guest_sysenter_esp;
  7719. evmcs->guest_sysenter_eip = vmcs12->guest_sysenter_eip;
  7720. evmcs->guest_activity_state = vmcs12->guest_activity_state;
  7721. evmcs->guest_sysenter_cs = vmcs12->guest_sysenter_cs;
  7722. evmcs->guest_cr0 = vmcs12->guest_cr0;
  7723. evmcs->guest_cr3 = vmcs12->guest_cr3;
  7724. evmcs->guest_cr4 = vmcs12->guest_cr4;
  7725. evmcs->guest_dr7 = vmcs12->guest_dr7;
  7726. evmcs->guest_physical_address = vmcs12->guest_physical_address;
  7727. evmcs->vm_instruction_error = vmcs12->vm_instruction_error;
  7728. evmcs->vm_exit_reason = vmcs12->vm_exit_reason;
  7729. evmcs->vm_exit_intr_info = vmcs12->vm_exit_intr_info;
  7730. evmcs->vm_exit_intr_error_code = vmcs12->vm_exit_intr_error_code;
  7731. evmcs->idt_vectoring_info_field = vmcs12->idt_vectoring_info_field;
  7732. evmcs->idt_vectoring_error_code = vmcs12->idt_vectoring_error_code;
  7733. evmcs->vm_exit_instruction_len = vmcs12->vm_exit_instruction_len;
  7734. evmcs->vmx_instruction_info = vmcs12->vmx_instruction_info;
  7735. evmcs->exit_qualification = vmcs12->exit_qualification;
  7736. evmcs->guest_linear_address = vmcs12->guest_linear_address;
  7737. evmcs->guest_rsp = vmcs12->guest_rsp;
  7738. evmcs->guest_rflags = vmcs12->guest_rflags;
  7739. evmcs->guest_interruptibility_info =
  7740. vmcs12->guest_interruptibility_info;
  7741. evmcs->cpu_based_vm_exec_control = vmcs12->cpu_based_vm_exec_control;
  7742. evmcs->vm_entry_controls = vmcs12->vm_entry_controls;
  7743. evmcs->vm_entry_intr_info_field = vmcs12->vm_entry_intr_info_field;
  7744. evmcs->vm_entry_exception_error_code =
  7745. vmcs12->vm_entry_exception_error_code;
  7746. evmcs->vm_entry_instruction_len = vmcs12->vm_entry_instruction_len;
  7747. evmcs->guest_rip = vmcs12->guest_rip;
  7748. evmcs->guest_bndcfgs = vmcs12->guest_bndcfgs;
  7749. return 0;
  7750. }
  7751. /*
  7752. * Copy the writable VMCS shadow fields back to the VMCS12, in case
  7753. * they have been modified by the L1 guest. Note that the "read-only"
  7754. * VM-exit information fields are actually writable if the vCPU is
  7755. * configured to support "VMWRITE to any supported field in the VMCS."
  7756. */
  7757. static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
  7758. {
  7759. const u16 *fields[] = {
  7760. shadow_read_write_fields,
  7761. shadow_read_only_fields
  7762. };
  7763. const int max_fields[] = {
  7764. max_shadow_read_write_fields,
  7765. max_shadow_read_only_fields
  7766. };
  7767. int i, q;
  7768. unsigned long field;
  7769. u64 field_value;
  7770. struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
  7771. preempt_disable();
  7772. vmcs_load(shadow_vmcs);
  7773. for (q = 0; q < ARRAY_SIZE(fields); q++) {
  7774. for (i = 0; i < max_fields[q]; i++) {
  7775. field = fields[q][i];
  7776. field_value = __vmcs_readl(field);
  7777. vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
  7778. }
  7779. /*
  7780. * Skip the VM-exit information fields if they are read-only.
  7781. */
  7782. if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
  7783. break;
  7784. }
  7785. vmcs_clear(shadow_vmcs);
  7786. vmcs_load(vmx->loaded_vmcs->vmcs);
  7787. preempt_enable();
  7788. }
  7789. static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
  7790. {
  7791. const u16 *fields[] = {
  7792. shadow_read_write_fields,
  7793. shadow_read_only_fields
  7794. };
  7795. const int max_fields[] = {
  7796. max_shadow_read_write_fields,
  7797. max_shadow_read_only_fields
  7798. };
  7799. int i, q;
  7800. unsigned long field;
  7801. u64 field_value = 0;
  7802. struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
  7803. vmcs_load(shadow_vmcs);
  7804. for (q = 0; q < ARRAY_SIZE(fields); q++) {
  7805. for (i = 0; i < max_fields[q]; i++) {
  7806. field = fields[q][i];
  7807. vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
  7808. __vmcs_writel(field, field_value);
  7809. }
  7810. }
  7811. vmcs_clear(shadow_vmcs);
  7812. vmcs_load(vmx->loaded_vmcs->vmcs);
  7813. }
  7814. static int handle_vmread(struct kvm_vcpu *vcpu)
  7815. {
  7816. unsigned long field;
  7817. u64 field_value;
  7818. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  7819. u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  7820. gva_t gva = 0;
  7821. struct vmcs12 *vmcs12;
  7822. if (!nested_vmx_check_permission(vcpu))
  7823. return 1;
  7824. if (to_vmx(vcpu)->nested.current_vmptr == -1ull)
  7825. return nested_vmx_failInvalid(vcpu);
  7826. if (!is_guest_mode(vcpu))
  7827. vmcs12 = get_vmcs12(vcpu);
  7828. else {
  7829. /*
  7830. * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
  7831. * to shadowed-field sets the ALU flags for VMfailInvalid.
  7832. */
  7833. if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull)
  7834. return nested_vmx_failInvalid(vcpu);
  7835. vmcs12 = get_shadow_vmcs12(vcpu);
  7836. }
  7837. /* Decode instruction info and find the field to read */
  7838. field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
  7839. /* Read the field, zero-extended to a u64 field_value */
  7840. if (vmcs12_read_any(vmcs12, field, &field_value) < 0)
  7841. return nested_vmx_failValid(vcpu,
  7842. VMXERR_UNSUPPORTED_VMCS_COMPONENT);
  7843. /*
  7844. * Now copy part of this value to register or memory, as requested.
  7845. * Note that the number of bits actually copied is 32 or 64 depending
  7846. * on the guest's mode (32 or 64 bit), not on the given field's length.
  7847. */
  7848. if (vmx_instruction_info & (1u << 10)) {
  7849. kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
  7850. field_value);
  7851. } else {
  7852. if (get_vmx_mem_address(vcpu, exit_qualification,
  7853. vmx_instruction_info, true, &gva))
  7854. return 1;
  7855. /* _system ok, nested_vmx_check_permission has verified cpl=0 */
  7856. kvm_write_guest_virt_system(vcpu, gva, &field_value,
  7857. (is_long_mode(vcpu) ? 8 : 4), NULL);
  7858. }
  7859. return nested_vmx_succeed(vcpu);
  7860. }
  7861. static int handle_vmwrite(struct kvm_vcpu *vcpu)
  7862. {
  7863. unsigned long field;
  7864. gva_t gva;
  7865. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7866. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  7867. u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  7868. /* The value to write might be 32 or 64 bits, depending on L1's long
  7869. * mode, and eventually we need to write that into a field of several
  7870. * possible lengths. The code below first zero-extends the value to 64
  7871. * bit (field_value), and then copies only the appropriate number of
  7872. * bits into the vmcs12 field.
  7873. */
  7874. u64 field_value = 0;
  7875. struct x86_exception e;
  7876. struct vmcs12 *vmcs12;
  7877. if (!nested_vmx_check_permission(vcpu))
  7878. return 1;
  7879. if (vmx->nested.current_vmptr == -1ull)
  7880. return nested_vmx_failInvalid(vcpu);
  7881. if (vmx_instruction_info & (1u << 10))
  7882. field_value = kvm_register_readl(vcpu,
  7883. (((vmx_instruction_info) >> 3) & 0xf));
  7884. else {
  7885. if (get_vmx_mem_address(vcpu, exit_qualification,
  7886. vmx_instruction_info, false, &gva))
  7887. return 1;
  7888. if (kvm_read_guest_virt(vcpu, gva, &field_value,
  7889. (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
  7890. kvm_inject_page_fault(vcpu, &e);
  7891. return 1;
  7892. }
  7893. }
  7894. field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
  7895. /*
  7896. * If the vCPU supports "VMWRITE to any supported field in the
  7897. * VMCS," then the "read-only" fields are actually read/write.
  7898. */
  7899. if (vmcs_field_readonly(field) &&
  7900. !nested_cpu_has_vmwrite_any_field(vcpu))
  7901. return nested_vmx_failValid(vcpu,
  7902. VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
  7903. if (!is_guest_mode(vcpu))
  7904. vmcs12 = get_vmcs12(vcpu);
  7905. else {
  7906. /*
  7907. * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
  7908. * to shadowed-field sets the ALU flags for VMfailInvalid.
  7909. */
  7910. if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull)
  7911. return nested_vmx_failInvalid(vcpu);
  7912. vmcs12 = get_shadow_vmcs12(vcpu);
  7913. }
  7914. if (vmcs12_write_any(vmcs12, field, field_value) < 0)
  7915. return nested_vmx_failValid(vcpu,
  7916. VMXERR_UNSUPPORTED_VMCS_COMPONENT);
  7917. /*
  7918. * Do not track vmcs12 dirty-state if in guest-mode
  7919. * as we actually dirty shadow vmcs12 instead of vmcs12.
  7920. */
  7921. if (!is_guest_mode(vcpu)) {
  7922. switch (field) {
  7923. #define SHADOW_FIELD_RW(x) case x:
  7924. #include "vmx_shadow_fields.h"
  7925. /*
  7926. * The fields that can be updated by L1 without a vmexit are
  7927. * always updated in the vmcs02, the others go down the slow
  7928. * path of prepare_vmcs02.
  7929. */
  7930. break;
  7931. default:
  7932. vmx->nested.dirty_vmcs12 = true;
  7933. break;
  7934. }
  7935. }
  7936. return nested_vmx_succeed(vcpu);
  7937. }
  7938. static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
  7939. {
  7940. vmx->nested.current_vmptr = vmptr;
  7941. if (enable_shadow_vmcs) {
  7942. vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
  7943. SECONDARY_EXEC_SHADOW_VMCS);
  7944. vmcs_write64(VMCS_LINK_POINTER,
  7945. __pa(vmx->vmcs01.shadow_vmcs));
  7946. vmx->nested.need_vmcs12_sync = true;
  7947. }
  7948. vmx->nested.dirty_vmcs12 = true;
  7949. }
  7950. /* Emulate the VMPTRLD instruction */
  7951. static int handle_vmptrld(struct kvm_vcpu *vcpu)
  7952. {
  7953. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7954. gpa_t vmptr;
  7955. if (!nested_vmx_check_permission(vcpu))
  7956. return 1;
  7957. if (nested_vmx_get_vmptr(vcpu, &vmptr))
  7958. return 1;
  7959. if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu)))
  7960. return nested_vmx_failValid(vcpu,
  7961. VMXERR_VMPTRLD_INVALID_ADDRESS);
  7962. if (vmptr == vmx->nested.vmxon_ptr)
  7963. return nested_vmx_failValid(vcpu,
  7964. VMXERR_VMPTRLD_VMXON_POINTER);
  7965. /* Forbid normal VMPTRLD if Enlightened version was used */
  7966. if (vmx->nested.hv_evmcs)
  7967. return 1;
  7968. if (vmx->nested.current_vmptr != vmptr) {
  7969. struct vmcs12 *new_vmcs12;
  7970. struct page *page;
  7971. page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
  7972. if (is_error_page(page))
  7973. return nested_vmx_failInvalid(vcpu);
  7974. new_vmcs12 = kmap(page);
  7975. if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
  7976. (new_vmcs12->hdr.shadow_vmcs &&
  7977. !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
  7978. kunmap(page);
  7979. kvm_release_page_clean(page);
  7980. return nested_vmx_failValid(vcpu,
  7981. VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
  7982. }
  7983. nested_release_vmcs12(vcpu);
  7984. /*
  7985. * Load VMCS12 from guest memory since it is not already
  7986. * cached.
  7987. */
  7988. memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
  7989. kunmap(page);
  7990. kvm_release_page_clean(page);
  7991. set_current_vmptr(vmx, vmptr);
  7992. }
  7993. return nested_vmx_succeed(vcpu);
  7994. }
  7995. /*
  7996. * This is an equivalent of the nested hypervisor executing the vmptrld
  7997. * instruction.
  7998. */
  7999. static int nested_vmx_handle_enlightened_vmptrld(struct kvm_vcpu *vcpu,
  8000. bool from_launch)
  8001. {
  8002. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8003. struct hv_vp_assist_page assist_page;
  8004. if (likely(!vmx->nested.enlightened_vmcs_enabled))
  8005. return 1;
  8006. if (unlikely(!kvm_hv_get_assist_page(vcpu, &assist_page)))
  8007. return 1;
  8008. if (unlikely(!assist_page.enlighten_vmentry))
  8009. return 1;
  8010. if (unlikely(assist_page.current_nested_vmcs !=
  8011. vmx->nested.hv_evmcs_vmptr)) {
  8012. if (!vmx->nested.hv_evmcs)
  8013. vmx->nested.current_vmptr = -1ull;
  8014. nested_release_evmcs(vcpu);
  8015. vmx->nested.hv_evmcs_page = kvm_vcpu_gpa_to_page(
  8016. vcpu, assist_page.current_nested_vmcs);
  8017. if (unlikely(is_error_page(vmx->nested.hv_evmcs_page)))
  8018. return 0;
  8019. vmx->nested.hv_evmcs = kmap(vmx->nested.hv_evmcs_page);
  8020. if (vmx->nested.hv_evmcs->revision_id != VMCS12_REVISION) {
  8021. nested_release_evmcs(vcpu);
  8022. return 0;
  8023. }
  8024. vmx->nested.dirty_vmcs12 = true;
  8025. /*
  8026. * As we keep L2 state for one guest only 'hv_clean_fields' mask
  8027. * can't be used when we switch between them. Reset it here for
  8028. * simplicity.
  8029. */
  8030. vmx->nested.hv_evmcs->hv_clean_fields &=
  8031. ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
  8032. vmx->nested.hv_evmcs_vmptr = assist_page.current_nested_vmcs;
  8033. /*
  8034. * Unlike normal vmcs12, enlightened vmcs12 is not fully
  8035. * reloaded from guest's memory (read only fields, fields not
  8036. * present in struct hv_enlightened_vmcs, ...). Make sure there
  8037. * are no leftovers.
  8038. */
  8039. if (from_launch)
  8040. memset(vmx->nested.cached_vmcs12, 0,
  8041. sizeof(*vmx->nested.cached_vmcs12));
  8042. }
  8043. return 1;
  8044. }
  8045. /* Emulate the VMPTRST instruction */
  8046. static int handle_vmptrst(struct kvm_vcpu *vcpu)
  8047. {
  8048. unsigned long exit_qual = vmcs_readl(EXIT_QUALIFICATION);
  8049. u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  8050. gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
  8051. struct x86_exception e;
  8052. gva_t gva;
  8053. if (!nested_vmx_check_permission(vcpu))
  8054. return 1;
  8055. if (unlikely(to_vmx(vcpu)->nested.hv_evmcs))
  8056. return 1;
  8057. if (get_vmx_mem_address(vcpu, exit_qual, instr_info, true, &gva))
  8058. return 1;
  8059. /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
  8060. if (kvm_write_guest_virt_system(vcpu, gva, (void *)&current_vmptr,
  8061. sizeof(gpa_t), &e)) {
  8062. kvm_inject_page_fault(vcpu, &e);
  8063. return 1;
  8064. }
  8065. return nested_vmx_succeed(vcpu);
  8066. }
  8067. /* Emulate the INVEPT instruction */
  8068. static int handle_invept(struct kvm_vcpu *vcpu)
  8069. {
  8070. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8071. u32 vmx_instruction_info, types;
  8072. unsigned long type;
  8073. gva_t gva;
  8074. struct x86_exception e;
  8075. struct {
  8076. u64 eptp, gpa;
  8077. } operand;
  8078. if (!(vmx->nested.msrs.secondary_ctls_high &
  8079. SECONDARY_EXEC_ENABLE_EPT) ||
  8080. !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
  8081. kvm_queue_exception(vcpu, UD_VECTOR);
  8082. return 1;
  8083. }
  8084. if (!nested_vmx_check_permission(vcpu))
  8085. return 1;
  8086. vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  8087. type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
  8088. types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
  8089. if (type >= 32 || !(types & (1 << type)))
  8090. return nested_vmx_failValid(vcpu,
  8091. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  8092. /* According to the Intel VMX instruction reference, the memory
  8093. * operand is read even if it isn't needed (e.g., for type==global)
  8094. */
  8095. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  8096. vmx_instruction_info, false, &gva))
  8097. return 1;
  8098. if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
  8099. kvm_inject_page_fault(vcpu, &e);
  8100. return 1;
  8101. }
  8102. switch (type) {
  8103. case VMX_EPT_EXTENT_GLOBAL:
  8104. /*
  8105. * TODO: track mappings and invalidate
  8106. * single context requests appropriately
  8107. */
  8108. case VMX_EPT_EXTENT_CONTEXT:
  8109. kvm_mmu_sync_roots(vcpu);
  8110. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  8111. break;
  8112. default:
  8113. BUG_ON(1);
  8114. break;
  8115. }
  8116. return nested_vmx_succeed(vcpu);
  8117. }
  8118. static u16 nested_get_vpid02(struct kvm_vcpu *vcpu)
  8119. {
  8120. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8121. return vmx->nested.vpid02 ? vmx->nested.vpid02 : vmx->vpid;
  8122. }
  8123. static int handle_invvpid(struct kvm_vcpu *vcpu)
  8124. {
  8125. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8126. u32 vmx_instruction_info;
  8127. unsigned long type, types;
  8128. gva_t gva;
  8129. struct x86_exception e;
  8130. struct {
  8131. u64 vpid;
  8132. u64 gla;
  8133. } operand;
  8134. u16 vpid02;
  8135. if (!(vmx->nested.msrs.secondary_ctls_high &
  8136. SECONDARY_EXEC_ENABLE_VPID) ||
  8137. !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
  8138. kvm_queue_exception(vcpu, UD_VECTOR);
  8139. return 1;
  8140. }
  8141. if (!nested_vmx_check_permission(vcpu))
  8142. return 1;
  8143. vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  8144. type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
  8145. types = (vmx->nested.msrs.vpid_caps &
  8146. VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
  8147. if (type >= 32 || !(types & (1 << type)))
  8148. return nested_vmx_failValid(vcpu,
  8149. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  8150. /* according to the intel vmx instruction reference, the memory
  8151. * operand is read even if it isn't needed (e.g., for type==global)
  8152. */
  8153. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  8154. vmx_instruction_info, false, &gva))
  8155. return 1;
  8156. if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
  8157. kvm_inject_page_fault(vcpu, &e);
  8158. return 1;
  8159. }
  8160. if (operand.vpid >> 16)
  8161. return nested_vmx_failValid(vcpu,
  8162. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  8163. vpid02 = nested_get_vpid02(vcpu);
  8164. switch (type) {
  8165. case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
  8166. if (!operand.vpid ||
  8167. is_noncanonical_address(operand.gla, vcpu))
  8168. return nested_vmx_failValid(vcpu,
  8169. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  8170. if (cpu_has_vmx_invvpid_individual_addr()) {
  8171. __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
  8172. vpid02, operand.gla);
  8173. } else
  8174. __vmx_flush_tlb(vcpu, vpid02, false);
  8175. break;
  8176. case VMX_VPID_EXTENT_SINGLE_CONTEXT:
  8177. case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
  8178. if (!operand.vpid)
  8179. return nested_vmx_failValid(vcpu,
  8180. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  8181. __vmx_flush_tlb(vcpu, vpid02, false);
  8182. break;
  8183. case VMX_VPID_EXTENT_ALL_CONTEXT:
  8184. __vmx_flush_tlb(vcpu, vpid02, false);
  8185. break;
  8186. default:
  8187. WARN_ON_ONCE(1);
  8188. return kvm_skip_emulated_instruction(vcpu);
  8189. }
  8190. return nested_vmx_succeed(vcpu);
  8191. }
  8192. static int handle_invpcid(struct kvm_vcpu *vcpu)
  8193. {
  8194. u32 vmx_instruction_info;
  8195. unsigned long type;
  8196. bool pcid_enabled;
  8197. gva_t gva;
  8198. struct x86_exception e;
  8199. unsigned i;
  8200. unsigned long roots_to_free = 0;
  8201. struct {
  8202. u64 pcid;
  8203. u64 gla;
  8204. } operand;
  8205. if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
  8206. kvm_queue_exception(vcpu, UD_VECTOR);
  8207. return 1;
  8208. }
  8209. vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  8210. type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
  8211. if (type > 3) {
  8212. kvm_inject_gp(vcpu, 0);
  8213. return 1;
  8214. }
  8215. /* According to the Intel instruction reference, the memory operand
  8216. * is read even if it isn't needed (e.g., for type==all)
  8217. */
  8218. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  8219. vmx_instruction_info, false, &gva))
  8220. return 1;
  8221. if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
  8222. kvm_inject_page_fault(vcpu, &e);
  8223. return 1;
  8224. }
  8225. if (operand.pcid >> 12 != 0) {
  8226. kvm_inject_gp(vcpu, 0);
  8227. return 1;
  8228. }
  8229. pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
  8230. switch (type) {
  8231. case INVPCID_TYPE_INDIV_ADDR:
  8232. if ((!pcid_enabled && (operand.pcid != 0)) ||
  8233. is_noncanonical_address(operand.gla, vcpu)) {
  8234. kvm_inject_gp(vcpu, 0);
  8235. return 1;
  8236. }
  8237. kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
  8238. return kvm_skip_emulated_instruction(vcpu);
  8239. case INVPCID_TYPE_SINGLE_CTXT:
  8240. if (!pcid_enabled && (operand.pcid != 0)) {
  8241. kvm_inject_gp(vcpu, 0);
  8242. return 1;
  8243. }
  8244. if (kvm_get_active_pcid(vcpu) == operand.pcid) {
  8245. kvm_mmu_sync_roots(vcpu);
  8246. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  8247. }
  8248. for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
  8249. if (kvm_get_pcid(vcpu, vcpu->arch.mmu->prev_roots[i].cr3)
  8250. == operand.pcid)
  8251. roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
  8252. kvm_mmu_free_roots(vcpu, vcpu->arch.mmu, roots_to_free);
  8253. /*
  8254. * If neither the current cr3 nor any of the prev_roots use the
  8255. * given PCID, then nothing needs to be done here because a
  8256. * resync will happen anyway before switching to any other CR3.
  8257. */
  8258. return kvm_skip_emulated_instruction(vcpu);
  8259. case INVPCID_TYPE_ALL_NON_GLOBAL:
  8260. /*
  8261. * Currently, KVM doesn't mark global entries in the shadow
  8262. * page tables, so a non-global flush just degenerates to a
  8263. * global flush. If needed, we could optimize this later by
  8264. * keeping track of global entries in shadow page tables.
  8265. */
  8266. /* fall-through */
  8267. case INVPCID_TYPE_ALL_INCL_GLOBAL:
  8268. kvm_mmu_unload(vcpu);
  8269. return kvm_skip_emulated_instruction(vcpu);
  8270. default:
  8271. BUG(); /* We have already checked above that type <= 3 */
  8272. }
  8273. }
  8274. static int handle_pml_full(struct kvm_vcpu *vcpu)
  8275. {
  8276. unsigned long exit_qualification;
  8277. trace_kvm_pml_full(vcpu->vcpu_id);
  8278. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  8279. /*
  8280. * PML buffer FULL happened while executing iret from NMI,
  8281. * "blocked by NMI" bit has to be set before next VM entry.
  8282. */
  8283. if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
  8284. enable_vnmi &&
  8285. (exit_qualification & INTR_INFO_UNBLOCK_NMI))
  8286. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  8287. GUEST_INTR_STATE_NMI);
  8288. /*
  8289. * PML buffer already flushed at beginning of VMEXIT. Nothing to do
  8290. * here.., and there's no userspace involvement needed for PML.
  8291. */
  8292. return 1;
  8293. }
  8294. static int handle_preemption_timer(struct kvm_vcpu *vcpu)
  8295. {
  8296. if (!to_vmx(vcpu)->req_immediate_exit)
  8297. kvm_lapic_expired_hv_timer(vcpu);
  8298. return 1;
  8299. }
  8300. static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
  8301. {
  8302. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8303. int maxphyaddr = cpuid_maxphyaddr(vcpu);
  8304. /* Check for memory type validity */
  8305. switch (address & VMX_EPTP_MT_MASK) {
  8306. case VMX_EPTP_MT_UC:
  8307. if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
  8308. return false;
  8309. break;
  8310. case VMX_EPTP_MT_WB:
  8311. if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
  8312. return false;
  8313. break;
  8314. default:
  8315. return false;
  8316. }
  8317. /* only 4 levels page-walk length are valid */
  8318. if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
  8319. return false;
  8320. /* Reserved bits should not be set */
  8321. if (address >> maxphyaddr || ((address >> 7) & 0x1f))
  8322. return false;
  8323. /* AD, if set, should be supported */
  8324. if (address & VMX_EPTP_AD_ENABLE_BIT) {
  8325. if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
  8326. return false;
  8327. }
  8328. return true;
  8329. }
  8330. static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
  8331. struct vmcs12 *vmcs12)
  8332. {
  8333. u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
  8334. u64 address;
  8335. bool accessed_dirty;
  8336. struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
  8337. if (!nested_cpu_has_eptp_switching(vmcs12) ||
  8338. !nested_cpu_has_ept(vmcs12))
  8339. return 1;
  8340. if (index >= VMFUNC_EPTP_ENTRIES)
  8341. return 1;
  8342. if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
  8343. &address, index * 8, 8))
  8344. return 1;
  8345. accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
  8346. /*
  8347. * If the (L2) guest does a vmfunc to the currently
  8348. * active ept pointer, we don't have to do anything else
  8349. */
  8350. if (vmcs12->ept_pointer != address) {
  8351. if (!valid_ept_address(vcpu, address))
  8352. return 1;
  8353. kvm_mmu_unload(vcpu);
  8354. mmu->ept_ad = accessed_dirty;
  8355. mmu->mmu_role.base.ad_disabled = !accessed_dirty;
  8356. vmcs12->ept_pointer = address;
  8357. /*
  8358. * TODO: Check what's the correct approach in case
  8359. * mmu reload fails. Currently, we just let the next
  8360. * reload potentially fail
  8361. */
  8362. kvm_mmu_reload(vcpu);
  8363. }
  8364. return 0;
  8365. }
  8366. static int handle_vmfunc(struct kvm_vcpu *vcpu)
  8367. {
  8368. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8369. struct vmcs12 *vmcs12;
  8370. u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
  8371. /*
  8372. * VMFUNC is only supported for nested guests, but we always enable the
  8373. * secondary control for simplicity; for non-nested mode, fake that we
  8374. * didn't by injecting #UD.
  8375. */
  8376. if (!is_guest_mode(vcpu)) {
  8377. kvm_queue_exception(vcpu, UD_VECTOR);
  8378. return 1;
  8379. }
  8380. vmcs12 = get_vmcs12(vcpu);
  8381. if ((vmcs12->vm_function_control & (1 << function)) == 0)
  8382. goto fail;
  8383. switch (function) {
  8384. case 0:
  8385. if (nested_vmx_eptp_switching(vcpu, vmcs12))
  8386. goto fail;
  8387. break;
  8388. default:
  8389. goto fail;
  8390. }
  8391. return kvm_skip_emulated_instruction(vcpu);
  8392. fail:
  8393. nested_vmx_vmexit(vcpu, vmx->exit_reason,
  8394. vmcs_read32(VM_EXIT_INTR_INFO),
  8395. vmcs_readl(EXIT_QUALIFICATION));
  8396. return 1;
  8397. }
  8398. static int handle_encls(struct kvm_vcpu *vcpu)
  8399. {
  8400. /*
  8401. * SGX virtualization is not yet supported. There is no software
  8402. * enable bit for SGX, so we have to trap ENCLS and inject a #UD
  8403. * to prevent the guest from executing ENCLS.
  8404. */
  8405. kvm_queue_exception(vcpu, UD_VECTOR);
  8406. return 1;
  8407. }
  8408. /*
  8409. * The exit handlers return 1 if the exit was handled fully and guest execution
  8410. * may resume. Otherwise they set the kvm_run parameter to indicate what needs
  8411. * to be done to userspace and return 0.
  8412. */
  8413. static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
  8414. [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
  8415. [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
  8416. [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
  8417. [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
  8418. [EXIT_REASON_IO_INSTRUCTION] = handle_io,
  8419. [EXIT_REASON_CR_ACCESS] = handle_cr,
  8420. [EXIT_REASON_DR_ACCESS] = handle_dr,
  8421. [EXIT_REASON_CPUID] = handle_cpuid,
  8422. [EXIT_REASON_MSR_READ] = handle_rdmsr,
  8423. [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
  8424. [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
  8425. [EXIT_REASON_HLT] = handle_halt,
  8426. [EXIT_REASON_INVD] = handle_invd,
  8427. [EXIT_REASON_INVLPG] = handle_invlpg,
  8428. [EXIT_REASON_RDPMC] = handle_rdpmc,
  8429. [EXIT_REASON_VMCALL] = handle_vmcall,
  8430. [EXIT_REASON_VMCLEAR] = handle_vmclear,
  8431. [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
  8432. [EXIT_REASON_VMPTRLD] = handle_vmptrld,
  8433. [EXIT_REASON_VMPTRST] = handle_vmptrst,
  8434. [EXIT_REASON_VMREAD] = handle_vmread,
  8435. [EXIT_REASON_VMRESUME] = handle_vmresume,
  8436. [EXIT_REASON_VMWRITE] = handle_vmwrite,
  8437. [EXIT_REASON_VMOFF] = handle_vmoff,
  8438. [EXIT_REASON_VMON] = handle_vmon,
  8439. [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
  8440. [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
  8441. [EXIT_REASON_APIC_WRITE] = handle_apic_write,
  8442. [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
  8443. [EXIT_REASON_WBINVD] = handle_wbinvd,
  8444. [EXIT_REASON_XSETBV] = handle_xsetbv,
  8445. [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
  8446. [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
  8447. [EXIT_REASON_GDTR_IDTR] = handle_desc,
  8448. [EXIT_REASON_LDTR_TR] = handle_desc,
  8449. [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
  8450. [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
  8451. [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
  8452. [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
  8453. [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
  8454. [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
  8455. [EXIT_REASON_INVEPT] = handle_invept,
  8456. [EXIT_REASON_INVVPID] = handle_invvpid,
  8457. [EXIT_REASON_RDRAND] = handle_invalid_op,
  8458. [EXIT_REASON_RDSEED] = handle_invalid_op,
  8459. [EXIT_REASON_XSAVES] = handle_xsaves,
  8460. [EXIT_REASON_XRSTORS] = handle_xrstors,
  8461. [EXIT_REASON_PML_FULL] = handle_pml_full,
  8462. [EXIT_REASON_INVPCID] = handle_invpcid,
  8463. [EXIT_REASON_VMFUNC] = handle_vmfunc,
  8464. [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
  8465. [EXIT_REASON_ENCLS] = handle_encls,
  8466. };
  8467. static const int kvm_vmx_max_exit_handlers =
  8468. ARRAY_SIZE(kvm_vmx_exit_handlers);
  8469. static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
  8470. struct vmcs12 *vmcs12)
  8471. {
  8472. unsigned long exit_qualification;
  8473. gpa_t bitmap, last_bitmap;
  8474. unsigned int port;
  8475. int size;
  8476. u8 b;
  8477. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
  8478. return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
  8479. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  8480. port = exit_qualification >> 16;
  8481. size = (exit_qualification & 7) + 1;
  8482. last_bitmap = (gpa_t)-1;
  8483. b = -1;
  8484. while (size > 0) {
  8485. if (port < 0x8000)
  8486. bitmap = vmcs12->io_bitmap_a;
  8487. else if (port < 0x10000)
  8488. bitmap = vmcs12->io_bitmap_b;
  8489. else
  8490. return true;
  8491. bitmap += (port & 0x7fff) / 8;
  8492. if (last_bitmap != bitmap)
  8493. if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
  8494. return true;
  8495. if (b & (1 << (port & 7)))
  8496. return true;
  8497. port++;
  8498. size--;
  8499. last_bitmap = bitmap;
  8500. }
  8501. return false;
  8502. }
  8503. /*
  8504. * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
  8505. * rather than handle it ourselves in L0. I.e., check whether L1 expressed
  8506. * disinterest in the current event (read or write a specific MSR) by using an
  8507. * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
  8508. */
  8509. static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
  8510. struct vmcs12 *vmcs12, u32 exit_reason)
  8511. {
  8512. u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
  8513. gpa_t bitmap;
  8514. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
  8515. return true;
  8516. /*
  8517. * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
  8518. * for the four combinations of read/write and low/high MSR numbers.
  8519. * First we need to figure out which of the four to use:
  8520. */
  8521. bitmap = vmcs12->msr_bitmap;
  8522. if (exit_reason == EXIT_REASON_MSR_WRITE)
  8523. bitmap += 2048;
  8524. if (msr_index >= 0xc0000000) {
  8525. msr_index -= 0xc0000000;
  8526. bitmap += 1024;
  8527. }
  8528. /* Then read the msr_index'th bit from this bitmap: */
  8529. if (msr_index < 1024*8) {
  8530. unsigned char b;
  8531. if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
  8532. return true;
  8533. return 1 & (b >> (msr_index & 7));
  8534. } else
  8535. return true; /* let L1 handle the wrong parameter */
  8536. }
  8537. /*
  8538. * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
  8539. * rather than handle it ourselves in L0. I.e., check if L1 wanted to
  8540. * intercept (via guest_host_mask etc.) the current event.
  8541. */
  8542. static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
  8543. struct vmcs12 *vmcs12)
  8544. {
  8545. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  8546. int cr = exit_qualification & 15;
  8547. int reg;
  8548. unsigned long val;
  8549. switch ((exit_qualification >> 4) & 3) {
  8550. case 0: /* mov to cr */
  8551. reg = (exit_qualification >> 8) & 15;
  8552. val = kvm_register_readl(vcpu, reg);
  8553. switch (cr) {
  8554. case 0:
  8555. if (vmcs12->cr0_guest_host_mask &
  8556. (val ^ vmcs12->cr0_read_shadow))
  8557. return true;
  8558. break;
  8559. case 3:
  8560. if ((vmcs12->cr3_target_count >= 1 &&
  8561. vmcs12->cr3_target_value0 == val) ||
  8562. (vmcs12->cr3_target_count >= 2 &&
  8563. vmcs12->cr3_target_value1 == val) ||
  8564. (vmcs12->cr3_target_count >= 3 &&
  8565. vmcs12->cr3_target_value2 == val) ||
  8566. (vmcs12->cr3_target_count >= 4 &&
  8567. vmcs12->cr3_target_value3 == val))
  8568. return false;
  8569. if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
  8570. return true;
  8571. break;
  8572. case 4:
  8573. if (vmcs12->cr4_guest_host_mask &
  8574. (vmcs12->cr4_read_shadow ^ val))
  8575. return true;
  8576. break;
  8577. case 8:
  8578. if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
  8579. return true;
  8580. break;
  8581. }
  8582. break;
  8583. case 2: /* clts */
  8584. if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
  8585. (vmcs12->cr0_read_shadow & X86_CR0_TS))
  8586. return true;
  8587. break;
  8588. case 1: /* mov from cr */
  8589. switch (cr) {
  8590. case 3:
  8591. if (vmcs12->cpu_based_vm_exec_control &
  8592. CPU_BASED_CR3_STORE_EXITING)
  8593. return true;
  8594. break;
  8595. case 8:
  8596. if (vmcs12->cpu_based_vm_exec_control &
  8597. CPU_BASED_CR8_STORE_EXITING)
  8598. return true;
  8599. break;
  8600. }
  8601. break;
  8602. case 3: /* lmsw */
  8603. /*
  8604. * lmsw can change bits 1..3 of cr0, and only set bit 0 of
  8605. * cr0. Other attempted changes are ignored, with no exit.
  8606. */
  8607. val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
  8608. if (vmcs12->cr0_guest_host_mask & 0xe &
  8609. (val ^ vmcs12->cr0_read_shadow))
  8610. return true;
  8611. if ((vmcs12->cr0_guest_host_mask & 0x1) &&
  8612. !(vmcs12->cr0_read_shadow & 0x1) &&
  8613. (val & 0x1))
  8614. return true;
  8615. break;
  8616. }
  8617. return false;
  8618. }
  8619. static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
  8620. struct vmcs12 *vmcs12, gpa_t bitmap)
  8621. {
  8622. u32 vmx_instruction_info;
  8623. unsigned long field;
  8624. u8 b;
  8625. if (!nested_cpu_has_shadow_vmcs(vmcs12))
  8626. return true;
  8627. /* Decode instruction info and find the field to access */
  8628. vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  8629. field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
  8630. /* Out-of-range fields always cause a VM exit from L2 to L1 */
  8631. if (field >> 15)
  8632. return true;
  8633. if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
  8634. return true;
  8635. return 1 & (b >> (field & 7));
  8636. }
  8637. /*
  8638. * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
  8639. * should handle it ourselves in L0 (and then continue L2). Only call this
  8640. * when in is_guest_mode (L2).
  8641. */
  8642. static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
  8643. {
  8644. u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  8645. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8646. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  8647. if (vmx->nested.nested_run_pending)
  8648. return false;
  8649. if (unlikely(vmx->fail)) {
  8650. pr_info_ratelimited("%s failed vm entry %x\n", __func__,
  8651. vmcs_read32(VM_INSTRUCTION_ERROR));
  8652. return true;
  8653. }
  8654. /*
  8655. * The host physical addresses of some pages of guest memory
  8656. * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
  8657. * Page). The CPU may write to these pages via their host
  8658. * physical address while L2 is running, bypassing any
  8659. * address-translation-based dirty tracking (e.g. EPT write
  8660. * protection).
  8661. *
  8662. * Mark them dirty on every exit from L2 to prevent them from
  8663. * getting out of sync with dirty tracking.
  8664. */
  8665. nested_mark_vmcs12_pages_dirty(vcpu);
  8666. trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
  8667. vmcs_readl(EXIT_QUALIFICATION),
  8668. vmx->idt_vectoring_info,
  8669. intr_info,
  8670. vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
  8671. KVM_ISA_VMX);
  8672. switch (exit_reason) {
  8673. case EXIT_REASON_EXCEPTION_NMI:
  8674. if (is_nmi(intr_info))
  8675. return false;
  8676. else if (is_page_fault(intr_info))
  8677. return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
  8678. else if (is_debug(intr_info) &&
  8679. vcpu->guest_debug &
  8680. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
  8681. return false;
  8682. else if (is_breakpoint(intr_info) &&
  8683. vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  8684. return false;
  8685. return vmcs12->exception_bitmap &
  8686. (1u << (intr_info & INTR_INFO_VECTOR_MASK));
  8687. case EXIT_REASON_EXTERNAL_INTERRUPT:
  8688. return false;
  8689. case EXIT_REASON_TRIPLE_FAULT:
  8690. return true;
  8691. case EXIT_REASON_PENDING_INTERRUPT:
  8692. return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
  8693. case EXIT_REASON_NMI_WINDOW:
  8694. return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
  8695. case EXIT_REASON_TASK_SWITCH:
  8696. return true;
  8697. case EXIT_REASON_CPUID:
  8698. return true;
  8699. case EXIT_REASON_HLT:
  8700. return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
  8701. case EXIT_REASON_INVD:
  8702. return true;
  8703. case EXIT_REASON_INVLPG:
  8704. return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
  8705. case EXIT_REASON_RDPMC:
  8706. return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
  8707. case EXIT_REASON_RDRAND:
  8708. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
  8709. case EXIT_REASON_RDSEED:
  8710. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
  8711. case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
  8712. return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
  8713. case EXIT_REASON_VMREAD:
  8714. return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
  8715. vmcs12->vmread_bitmap);
  8716. case EXIT_REASON_VMWRITE:
  8717. return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
  8718. vmcs12->vmwrite_bitmap);
  8719. case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
  8720. case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
  8721. case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
  8722. case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
  8723. case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
  8724. /*
  8725. * VMX instructions trap unconditionally. This allows L1 to
  8726. * emulate them for its L2 guest, i.e., allows 3-level nesting!
  8727. */
  8728. return true;
  8729. case EXIT_REASON_CR_ACCESS:
  8730. return nested_vmx_exit_handled_cr(vcpu, vmcs12);
  8731. case EXIT_REASON_DR_ACCESS:
  8732. return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
  8733. case EXIT_REASON_IO_INSTRUCTION:
  8734. return nested_vmx_exit_handled_io(vcpu, vmcs12);
  8735. case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
  8736. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
  8737. case EXIT_REASON_MSR_READ:
  8738. case EXIT_REASON_MSR_WRITE:
  8739. return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
  8740. case EXIT_REASON_INVALID_STATE:
  8741. return true;
  8742. case EXIT_REASON_MWAIT_INSTRUCTION:
  8743. return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
  8744. case EXIT_REASON_MONITOR_TRAP_FLAG:
  8745. return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
  8746. case EXIT_REASON_MONITOR_INSTRUCTION:
  8747. return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
  8748. case EXIT_REASON_PAUSE_INSTRUCTION:
  8749. return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
  8750. nested_cpu_has2(vmcs12,
  8751. SECONDARY_EXEC_PAUSE_LOOP_EXITING);
  8752. case EXIT_REASON_MCE_DURING_VMENTRY:
  8753. return false;
  8754. case EXIT_REASON_TPR_BELOW_THRESHOLD:
  8755. return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
  8756. case EXIT_REASON_APIC_ACCESS:
  8757. case EXIT_REASON_APIC_WRITE:
  8758. case EXIT_REASON_EOI_INDUCED:
  8759. /*
  8760. * The controls for "virtualize APIC accesses," "APIC-
  8761. * register virtualization," and "virtual-interrupt
  8762. * delivery" only come from vmcs12.
  8763. */
  8764. return true;
  8765. case EXIT_REASON_EPT_VIOLATION:
  8766. /*
  8767. * L0 always deals with the EPT violation. If nested EPT is
  8768. * used, and the nested mmu code discovers that the address is
  8769. * missing in the guest EPT table (EPT12), the EPT violation
  8770. * will be injected with nested_ept_inject_page_fault()
  8771. */
  8772. return false;
  8773. case EXIT_REASON_EPT_MISCONFIG:
  8774. /*
  8775. * L2 never uses directly L1's EPT, but rather L0's own EPT
  8776. * table (shadow on EPT) or a merged EPT table that L0 built
  8777. * (EPT on EPT). So any problems with the structure of the
  8778. * table is L0's fault.
  8779. */
  8780. return false;
  8781. case EXIT_REASON_INVPCID:
  8782. return
  8783. nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
  8784. nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
  8785. case EXIT_REASON_WBINVD:
  8786. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
  8787. case EXIT_REASON_XSETBV:
  8788. return true;
  8789. case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
  8790. /*
  8791. * This should never happen, since it is not possible to
  8792. * set XSS to a non-zero value---neither in L1 nor in L2.
  8793. * If if it were, XSS would have to be checked against
  8794. * the XSS exit bitmap in vmcs12.
  8795. */
  8796. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
  8797. case EXIT_REASON_PREEMPTION_TIMER:
  8798. return false;
  8799. case EXIT_REASON_PML_FULL:
  8800. /* We emulate PML support to L1. */
  8801. return false;
  8802. case EXIT_REASON_VMFUNC:
  8803. /* VM functions are emulated through L2->L0 vmexits. */
  8804. return false;
  8805. case EXIT_REASON_ENCLS:
  8806. /* SGX is never exposed to L1 */
  8807. return false;
  8808. default:
  8809. return true;
  8810. }
  8811. }
  8812. static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
  8813. {
  8814. u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  8815. /*
  8816. * At this point, the exit interruption info in exit_intr_info
  8817. * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
  8818. * we need to query the in-kernel LAPIC.
  8819. */
  8820. WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
  8821. if ((exit_intr_info &
  8822. (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
  8823. (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
  8824. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  8825. vmcs12->vm_exit_intr_error_code =
  8826. vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
  8827. }
  8828. nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
  8829. vmcs_readl(EXIT_QUALIFICATION));
  8830. return 1;
  8831. }
  8832. static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
  8833. {
  8834. *info1 = vmcs_readl(EXIT_QUALIFICATION);
  8835. *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
  8836. }
  8837. static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
  8838. {
  8839. if (vmx->pml_pg) {
  8840. __free_page(vmx->pml_pg);
  8841. vmx->pml_pg = NULL;
  8842. }
  8843. }
  8844. static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
  8845. {
  8846. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8847. u64 *pml_buf;
  8848. u16 pml_idx;
  8849. pml_idx = vmcs_read16(GUEST_PML_INDEX);
  8850. /* Do nothing if PML buffer is empty */
  8851. if (pml_idx == (PML_ENTITY_NUM - 1))
  8852. return;
  8853. /* PML index always points to next available PML buffer entity */
  8854. if (pml_idx >= PML_ENTITY_NUM)
  8855. pml_idx = 0;
  8856. else
  8857. pml_idx++;
  8858. pml_buf = page_address(vmx->pml_pg);
  8859. for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
  8860. u64 gpa;
  8861. gpa = pml_buf[pml_idx];
  8862. WARN_ON(gpa & (PAGE_SIZE - 1));
  8863. kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
  8864. }
  8865. /* reset PML index */
  8866. vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
  8867. }
  8868. /*
  8869. * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
  8870. * Called before reporting dirty_bitmap to userspace.
  8871. */
  8872. static void kvm_flush_pml_buffers(struct kvm *kvm)
  8873. {
  8874. int i;
  8875. struct kvm_vcpu *vcpu;
  8876. /*
  8877. * We only need to kick vcpu out of guest mode here, as PML buffer
  8878. * is flushed at beginning of all VMEXITs, and it's obvious that only
  8879. * vcpus running in guest are possible to have unflushed GPAs in PML
  8880. * buffer.
  8881. */
  8882. kvm_for_each_vcpu(i, vcpu, kvm)
  8883. kvm_vcpu_kick(vcpu);
  8884. }
  8885. static void vmx_dump_sel(char *name, uint32_t sel)
  8886. {
  8887. pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
  8888. name, vmcs_read16(sel),
  8889. vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
  8890. vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
  8891. vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
  8892. }
  8893. static void vmx_dump_dtsel(char *name, uint32_t limit)
  8894. {
  8895. pr_err("%s limit=0x%08x, base=0x%016lx\n",
  8896. name, vmcs_read32(limit),
  8897. vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
  8898. }
  8899. static void dump_vmcs(void)
  8900. {
  8901. u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
  8902. u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
  8903. u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  8904. u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
  8905. u32 secondary_exec_control = 0;
  8906. unsigned long cr4 = vmcs_readl(GUEST_CR4);
  8907. u64 efer = vmcs_read64(GUEST_IA32_EFER);
  8908. int i, n;
  8909. if (cpu_has_secondary_exec_ctrls())
  8910. secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  8911. pr_err("*** Guest State ***\n");
  8912. pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
  8913. vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
  8914. vmcs_readl(CR0_GUEST_HOST_MASK));
  8915. pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
  8916. cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
  8917. pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
  8918. if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
  8919. (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
  8920. {
  8921. pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
  8922. vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
  8923. pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
  8924. vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
  8925. }
  8926. pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
  8927. vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
  8928. pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
  8929. vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
  8930. pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
  8931. vmcs_readl(GUEST_SYSENTER_ESP),
  8932. vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
  8933. vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
  8934. vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
  8935. vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
  8936. vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
  8937. vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
  8938. vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
  8939. vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
  8940. vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
  8941. vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
  8942. vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
  8943. if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
  8944. (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
  8945. pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
  8946. efer, vmcs_read64(GUEST_IA32_PAT));
  8947. pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
  8948. vmcs_read64(GUEST_IA32_DEBUGCTL),
  8949. vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
  8950. if (cpu_has_load_perf_global_ctrl &&
  8951. vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
  8952. pr_err("PerfGlobCtl = 0x%016llx\n",
  8953. vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
  8954. if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
  8955. pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
  8956. pr_err("Interruptibility = %08x ActivityState = %08x\n",
  8957. vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
  8958. vmcs_read32(GUEST_ACTIVITY_STATE));
  8959. if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
  8960. pr_err("InterruptStatus = %04x\n",
  8961. vmcs_read16(GUEST_INTR_STATUS));
  8962. pr_err("*** Host State ***\n");
  8963. pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
  8964. vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
  8965. pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
  8966. vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
  8967. vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
  8968. vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
  8969. vmcs_read16(HOST_TR_SELECTOR));
  8970. pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
  8971. vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
  8972. vmcs_readl(HOST_TR_BASE));
  8973. pr_err("GDTBase=%016lx IDTBase=%016lx\n",
  8974. vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
  8975. pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
  8976. vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
  8977. vmcs_readl(HOST_CR4));
  8978. pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
  8979. vmcs_readl(HOST_IA32_SYSENTER_ESP),
  8980. vmcs_read32(HOST_IA32_SYSENTER_CS),
  8981. vmcs_readl(HOST_IA32_SYSENTER_EIP));
  8982. if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
  8983. pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
  8984. vmcs_read64(HOST_IA32_EFER),
  8985. vmcs_read64(HOST_IA32_PAT));
  8986. if (cpu_has_load_perf_global_ctrl &&
  8987. vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
  8988. pr_err("PerfGlobCtl = 0x%016llx\n",
  8989. vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
  8990. pr_err("*** Control State ***\n");
  8991. pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
  8992. pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
  8993. pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
  8994. pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
  8995. vmcs_read32(EXCEPTION_BITMAP),
  8996. vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
  8997. vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
  8998. pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
  8999. vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
  9000. vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
  9001. vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
  9002. pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
  9003. vmcs_read32(VM_EXIT_INTR_INFO),
  9004. vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
  9005. vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
  9006. pr_err(" reason=%08x qualification=%016lx\n",
  9007. vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
  9008. pr_err("IDTVectoring: info=%08x errcode=%08x\n",
  9009. vmcs_read32(IDT_VECTORING_INFO_FIELD),
  9010. vmcs_read32(IDT_VECTORING_ERROR_CODE));
  9011. pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
  9012. if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
  9013. pr_err("TSC Multiplier = 0x%016llx\n",
  9014. vmcs_read64(TSC_MULTIPLIER));
  9015. if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
  9016. pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
  9017. if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
  9018. pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
  9019. if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
  9020. pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
  9021. n = vmcs_read32(CR3_TARGET_COUNT);
  9022. for (i = 0; i + 1 < n; i += 4)
  9023. pr_err("CR3 target%u=%016lx target%u=%016lx\n",
  9024. i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
  9025. i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
  9026. if (i < n)
  9027. pr_err("CR3 target%u=%016lx\n",
  9028. i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
  9029. if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
  9030. pr_err("PLE Gap=%08x Window=%08x\n",
  9031. vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
  9032. if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
  9033. pr_err("Virtual processor ID = 0x%04x\n",
  9034. vmcs_read16(VIRTUAL_PROCESSOR_ID));
  9035. }
  9036. /*
  9037. * The guest has exited. See if we can fix it or if we need userspace
  9038. * assistance.
  9039. */
  9040. static int vmx_handle_exit(struct kvm_vcpu *vcpu)
  9041. {
  9042. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9043. u32 exit_reason = vmx->exit_reason;
  9044. u32 vectoring_info = vmx->idt_vectoring_info;
  9045. trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
  9046. /*
  9047. * Flush logged GPAs PML buffer, this will make dirty_bitmap more
  9048. * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
  9049. * querying dirty_bitmap, we only need to kick all vcpus out of guest
  9050. * mode as if vcpus is in root mode, the PML buffer must has been
  9051. * flushed already.
  9052. */
  9053. if (enable_pml)
  9054. vmx_flush_pml_buffer(vcpu);
  9055. /* If guest state is invalid, start emulating */
  9056. if (vmx->emulation_required)
  9057. return handle_invalid_guest_state(vcpu);
  9058. if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
  9059. return nested_vmx_reflect_vmexit(vcpu, exit_reason);
  9060. if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
  9061. dump_vmcs();
  9062. vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  9063. vcpu->run->fail_entry.hardware_entry_failure_reason
  9064. = exit_reason;
  9065. return 0;
  9066. }
  9067. if (unlikely(vmx->fail)) {
  9068. vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  9069. vcpu->run->fail_entry.hardware_entry_failure_reason
  9070. = vmcs_read32(VM_INSTRUCTION_ERROR);
  9071. return 0;
  9072. }
  9073. /*
  9074. * Note:
  9075. * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
  9076. * delivery event since it indicates guest is accessing MMIO.
  9077. * The vm-exit can be triggered again after return to guest that
  9078. * will cause infinite loop.
  9079. */
  9080. if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
  9081. (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
  9082. exit_reason != EXIT_REASON_EPT_VIOLATION &&
  9083. exit_reason != EXIT_REASON_PML_FULL &&
  9084. exit_reason != EXIT_REASON_TASK_SWITCH)) {
  9085. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  9086. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
  9087. vcpu->run->internal.ndata = 3;
  9088. vcpu->run->internal.data[0] = vectoring_info;
  9089. vcpu->run->internal.data[1] = exit_reason;
  9090. vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
  9091. if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
  9092. vcpu->run->internal.ndata++;
  9093. vcpu->run->internal.data[3] =
  9094. vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  9095. }
  9096. return 0;
  9097. }
  9098. if (unlikely(!enable_vnmi &&
  9099. vmx->loaded_vmcs->soft_vnmi_blocked)) {
  9100. if (vmx_interrupt_allowed(vcpu)) {
  9101. vmx->loaded_vmcs->soft_vnmi_blocked = 0;
  9102. } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
  9103. vcpu->arch.nmi_pending) {
  9104. /*
  9105. * This CPU don't support us in finding the end of an
  9106. * NMI-blocked window if the guest runs with IRQs
  9107. * disabled. So we pull the trigger after 1 s of
  9108. * futile waiting, but inform the user about this.
  9109. */
  9110. printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
  9111. "state on VCPU %d after 1 s timeout\n",
  9112. __func__, vcpu->vcpu_id);
  9113. vmx->loaded_vmcs->soft_vnmi_blocked = 0;
  9114. }
  9115. }
  9116. if (exit_reason < kvm_vmx_max_exit_handlers
  9117. && kvm_vmx_exit_handlers[exit_reason])
  9118. return kvm_vmx_exit_handlers[exit_reason](vcpu);
  9119. else {
  9120. vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
  9121. exit_reason);
  9122. kvm_queue_exception(vcpu, UD_VECTOR);
  9123. return 1;
  9124. }
  9125. }
  9126. /*
  9127. * Software based L1D cache flush which is used when microcode providing
  9128. * the cache control MSR is not loaded.
  9129. *
  9130. * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
  9131. * flush it is required to read in 64 KiB because the replacement algorithm
  9132. * is not exactly LRU. This could be sized at runtime via topology
  9133. * information but as all relevant affected CPUs have 32KiB L1D cache size
  9134. * there is no point in doing so.
  9135. */
  9136. static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
  9137. {
  9138. int size = PAGE_SIZE << L1D_CACHE_ORDER;
  9139. /*
  9140. * This code is only executed when the the flush mode is 'cond' or
  9141. * 'always'
  9142. */
  9143. if (static_branch_likely(&vmx_l1d_flush_cond)) {
  9144. bool flush_l1d;
  9145. /*
  9146. * Clear the per-vcpu flush bit, it gets set again
  9147. * either from vcpu_run() or from one of the unsafe
  9148. * VMEXIT handlers.
  9149. */
  9150. flush_l1d = vcpu->arch.l1tf_flush_l1d;
  9151. vcpu->arch.l1tf_flush_l1d = false;
  9152. /*
  9153. * Clear the per-cpu flush bit, it gets set again from
  9154. * the interrupt handlers.
  9155. */
  9156. flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
  9157. kvm_clear_cpu_l1tf_flush_l1d();
  9158. if (!flush_l1d)
  9159. return;
  9160. }
  9161. vcpu->stat.l1d_flush++;
  9162. if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
  9163. wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
  9164. return;
  9165. }
  9166. asm volatile(
  9167. /* First ensure the pages are in the TLB */
  9168. "xorl %%eax, %%eax\n"
  9169. ".Lpopulate_tlb:\n\t"
  9170. "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
  9171. "addl $4096, %%eax\n\t"
  9172. "cmpl %%eax, %[size]\n\t"
  9173. "jne .Lpopulate_tlb\n\t"
  9174. "xorl %%eax, %%eax\n\t"
  9175. "cpuid\n\t"
  9176. /* Now fill the cache */
  9177. "xorl %%eax, %%eax\n"
  9178. ".Lfill_cache:\n"
  9179. "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
  9180. "addl $64, %%eax\n\t"
  9181. "cmpl %%eax, %[size]\n\t"
  9182. "jne .Lfill_cache\n\t"
  9183. "lfence\n"
  9184. :: [flush_pages] "r" (vmx_l1d_flush_pages),
  9185. [size] "r" (size)
  9186. : "eax", "ebx", "ecx", "edx");
  9187. }
  9188. static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
  9189. {
  9190. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  9191. if (is_guest_mode(vcpu) &&
  9192. nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
  9193. return;
  9194. if (irr == -1 || tpr < irr) {
  9195. vmcs_write32(TPR_THRESHOLD, 0);
  9196. return;
  9197. }
  9198. vmcs_write32(TPR_THRESHOLD, irr);
  9199. }
  9200. static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
  9201. {
  9202. u32 sec_exec_control;
  9203. if (!lapic_in_kernel(vcpu))
  9204. return;
  9205. if (!flexpriority_enabled &&
  9206. !cpu_has_vmx_virtualize_x2apic_mode())
  9207. return;
  9208. /* Postpone execution until vmcs01 is the current VMCS. */
  9209. if (is_guest_mode(vcpu)) {
  9210. to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
  9211. return;
  9212. }
  9213. sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  9214. sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  9215. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
  9216. switch (kvm_get_apic_mode(vcpu)) {
  9217. case LAPIC_MODE_INVALID:
  9218. WARN_ONCE(true, "Invalid local APIC state");
  9219. case LAPIC_MODE_DISABLED:
  9220. break;
  9221. case LAPIC_MODE_XAPIC:
  9222. if (flexpriority_enabled) {
  9223. sec_exec_control |=
  9224. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  9225. vmx_flush_tlb(vcpu, true);
  9226. }
  9227. break;
  9228. case LAPIC_MODE_X2APIC:
  9229. if (cpu_has_vmx_virtualize_x2apic_mode())
  9230. sec_exec_control |=
  9231. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  9232. break;
  9233. }
  9234. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
  9235. vmx_update_msr_bitmap(vcpu);
  9236. }
  9237. static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
  9238. {
  9239. if (!is_guest_mode(vcpu)) {
  9240. vmcs_write64(APIC_ACCESS_ADDR, hpa);
  9241. vmx_flush_tlb(vcpu, true);
  9242. }
  9243. }
  9244. static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
  9245. {
  9246. u16 status;
  9247. u8 old;
  9248. if (max_isr == -1)
  9249. max_isr = 0;
  9250. status = vmcs_read16(GUEST_INTR_STATUS);
  9251. old = status >> 8;
  9252. if (max_isr != old) {
  9253. status &= 0xff;
  9254. status |= max_isr << 8;
  9255. vmcs_write16(GUEST_INTR_STATUS, status);
  9256. }
  9257. }
  9258. static void vmx_set_rvi(int vector)
  9259. {
  9260. u16 status;
  9261. u8 old;
  9262. if (vector == -1)
  9263. vector = 0;
  9264. status = vmcs_read16(GUEST_INTR_STATUS);
  9265. old = (u8)status & 0xff;
  9266. if ((u8)vector != old) {
  9267. status &= ~0xff;
  9268. status |= (u8)vector;
  9269. vmcs_write16(GUEST_INTR_STATUS, status);
  9270. }
  9271. }
  9272. static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
  9273. {
  9274. /*
  9275. * When running L2, updating RVI is only relevant when
  9276. * vmcs12 virtual-interrupt-delivery enabled.
  9277. * However, it can be enabled only when L1 also
  9278. * intercepts external-interrupts and in that case
  9279. * we should not update vmcs02 RVI but instead intercept
  9280. * interrupt. Therefore, do nothing when running L2.
  9281. */
  9282. if (!is_guest_mode(vcpu))
  9283. vmx_set_rvi(max_irr);
  9284. }
  9285. static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
  9286. {
  9287. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9288. int max_irr;
  9289. bool max_irr_updated;
  9290. WARN_ON(!vcpu->arch.apicv_active);
  9291. if (pi_test_on(&vmx->pi_desc)) {
  9292. pi_clear_on(&vmx->pi_desc);
  9293. /*
  9294. * IOMMU can write to PIR.ON, so the barrier matters even on UP.
  9295. * But on x86 this is just a compiler barrier anyway.
  9296. */
  9297. smp_mb__after_atomic();
  9298. max_irr_updated =
  9299. kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
  9300. /*
  9301. * If we are running L2 and L1 has a new pending interrupt
  9302. * which can be injected, we should re-evaluate
  9303. * what should be done with this new L1 interrupt.
  9304. * If L1 intercepts external-interrupts, we should
  9305. * exit from L2 to L1. Otherwise, interrupt should be
  9306. * delivered directly to L2.
  9307. */
  9308. if (is_guest_mode(vcpu) && max_irr_updated) {
  9309. if (nested_exit_on_intr(vcpu))
  9310. kvm_vcpu_exiting_guest_mode(vcpu);
  9311. else
  9312. kvm_make_request(KVM_REQ_EVENT, vcpu);
  9313. }
  9314. } else {
  9315. max_irr = kvm_lapic_find_highest_irr(vcpu);
  9316. }
  9317. vmx_hwapic_irr_update(vcpu, max_irr);
  9318. return max_irr;
  9319. }
  9320. static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu)
  9321. {
  9322. u8 rvi = vmx_get_rvi();
  9323. u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI);
  9324. return ((rvi & 0xf0) > (vppr & 0xf0));
  9325. }
  9326. static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
  9327. {
  9328. if (!kvm_vcpu_apicv_active(vcpu))
  9329. return;
  9330. vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
  9331. vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
  9332. vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
  9333. vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
  9334. }
  9335. static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
  9336. {
  9337. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9338. pi_clear_on(&vmx->pi_desc);
  9339. memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
  9340. }
  9341. static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
  9342. {
  9343. u32 exit_intr_info = 0;
  9344. u16 basic_exit_reason = (u16)vmx->exit_reason;
  9345. if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
  9346. || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
  9347. return;
  9348. if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
  9349. exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  9350. vmx->exit_intr_info = exit_intr_info;
  9351. /* if exit due to PF check for async PF */
  9352. if (is_page_fault(exit_intr_info))
  9353. vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
  9354. /* Handle machine checks before interrupts are enabled */
  9355. if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
  9356. is_machine_check(exit_intr_info))
  9357. kvm_machine_check();
  9358. /* We need to handle NMIs before interrupts are enabled */
  9359. if (is_nmi(exit_intr_info)) {
  9360. kvm_before_interrupt(&vmx->vcpu);
  9361. asm("int $2");
  9362. kvm_after_interrupt(&vmx->vcpu);
  9363. }
  9364. }
  9365. static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
  9366. {
  9367. u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  9368. if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
  9369. == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
  9370. unsigned int vector;
  9371. unsigned long entry;
  9372. gate_desc *desc;
  9373. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9374. #ifdef CONFIG_X86_64
  9375. unsigned long tmp;
  9376. #endif
  9377. vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
  9378. desc = (gate_desc *)vmx->host_idt_base + vector;
  9379. entry = gate_offset(desc);
  9380. asm volatile(
  9381. #ifdef CONFIG_X86_64
  9382. "mov %%" _ASM_SP ", %[sp]\n\t"
  9383. "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
  9384. "push $%c[ss]\n\t"
  9385. "push %[sp]\n\t"
  9386. #endif
  9387. "pushf\n\t"
  9388. __ASM_SIZE(push) " $%c[cs]\n\t"
  9389. CALL_NOSPEC
  9390. :
  9391. #ifdef CONFIG_X86_64
  9392. [sp]"=&r"(tmp),
  9393. #endif
  9394. ASM_CALL_CONSTRAINT
  9395. :
  9396. THUNK_TARGET(entry),
  9397. [ss]"i"(__KERNEL_DS),
  9398. [cs]"i"(__KERNEL_CS)
  9399. );
  9400. }
  9401. }
  9402. STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
  9403. static bool vmx_has_emulated_msr(int index)
  9404. {
  9405. switch (index) {
  9406. case MSR_IA32_SMBASE:
  9407. /*
  9408. * We cannot do SMM unless we can run the guest in big
  9409. * real mode.
  9410. */
  9411. return enable_unrestricted_guest || emulate_invalid_guest_state;
  9412. case MSR_AMD64_VIRT_SPEC_CTRL:
  9413. /* This is AMD only. */
  9414. return false;
  9415. default:
  9416. return true;
  9417. }
  9418. }
  9419. static bool vmx_mpx_supported(void)
  9420. {
  9421. return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
  9422. (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
  9423. }
  9424. static bool vmx_xsaves_supported(void)
  9425. {
  9426. return vmcs_config.cpu_based_2nd_exec_ctrl &
  9427. SECONDARY_EXEC_XSAVES;
  9428. }
  9429. static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
  9430. {
  9431. u32 exit_intr_info;
  9432. bool unblock_nmi;
  9433. u8 vector;
  9434. bool idtv_info_valid;
  9435. idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
  9436. if (enable_vnmi) {
  9437. if (vmx->loaded_vmcs->nmi_known_unmasked)
  9438. return;
  9439. /*
  9440. * Can't use vmx->exit_intr_info since we're not sure what
  9441. * the exit reason is.
  9442. */
  9443. exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  9444. unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
  9445. vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
  9446. /*
  9447. * SDM 3: 27.7.1.2 (September 2008)
  9448. * Re-set bit "block by NMI" before VM entry if vmexit caused by
  9449. * a guest IRET fault.
  9450. * SDM 3: 23.2.2 (September 2008)
  9451. * Bit 12 is undefined in any of the following cases:
  9452. * If the VM exit sets the valid bit in the IDT-vectoring
  9453. * information field.
  9454. * If the VM exit is due to a double fault.
  9455. */
  9456. if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
  9457. vector != DF_VECTOR && !idtv_info_valid)
  9458. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  9459. GUEST_INTR_STATE_NMI);
  9460. else
  9461. vmx->loaded_vmcs->nmi_known_unmasked =
  9462. !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
  9463. & GUEST_INTR_STATE_NMI);
  9464. } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
  9465. vmx->loaded_vmcs->vnmi_blocked_time +=
  9466. ktime_to_ns(ktime_sub(ktime_get(),
  9467. vmx->loaded_vmcs->entry_time));
  9468. }
  9469. static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
  9470. u32 idt_vectoring_info,
  9471. int instr_len_field,
  9472. int error_code_field)
  9473. {
  9474. u8 vector;
  9475. int type;
  9476. bool idtv_info_valid;
  9477. idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
  9478. vcpu->arch.nmi_injected = false;
  9479. kvm_clear_exception_queue(vcpu);
  9480. kvm_clear_interrupt_queue(vcpu);
  9481. if (!idtv_info_valid)
  9482. return;
  9483. kvm_make_request(KVM_REQ_EVENT, vcpu);
  9484. vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
  9485. type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
  9486. switch (type) {
  9487. case INTR_TYPE_NMI_INTR:
  9488. vcpu->arch.nmi_injected = true;
  9489. /*
  9490. * SDM 3: 27.7.1.2 (September 2008)
  9491. * Clear bit "block by NMI" before VM entry if a NMI
  9492. * delivery faulted.
  9493. */
  9494. vmx_set_nmi_mask(vcpu, false);
  9495. break;
  9496. case INTR_TYPE_SOFT_EXCEPTION:
  9497. vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
  9498. /* fall through */
  9499. case INTR_TYPE_HARD_EXCEPTION:
  9500. if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
  9501. u32 err = vmcs_read32(error_code_field);
  9502. kvm_requeue_exception_e(vcpu, vector, err);
  9503. } else
  9504. kvm_requeue_exception(vcpu, vector);
  9505. break;
  9506. case INTR_TYPE_SOFT_INTR:
  9507. vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
  9508. /* fall through */
  9509. case INTR_TYPE_EXT_INTR:
  9510. kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
  9511. break;
  9512. default:
  9513. break;
  9514. }
  9515. }
  9516. static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
  9517. {
  9518. __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
  9519. VM_EXIT_INSTRUCTION_LEN,
  9520. IDT_VECTORING_ERROR_CODE);
  9521. }
  9522. static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
  9523. {
  9524. __vmx_complete_interrupts(vcpu,
  9525. vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
  9526. VM_ENTRY_INSTRUCTION_LEN,
  9527. VM_ENTRY_EXCEPTION_ERROR_CODE);
  9528. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
  9529. }
  9530. static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
  9531. {
  9532. int i, nr_msrs;
  9533. struct perf_guest_switch_msr *msrs;
  9534. msrs = perf_guest_get_msrs(&nr_msrs);
  9535. if (!msrs)
  9536. return;
  9537. for (i = 0; i < nr_msrs; i++)
  9538. if (msrs[i].host == msrs[i].guest)
  9539. clear_atomic_switch_msr(vmx, msrs[i].msr);
  9540. else
  9541. add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
  9542. msrs[i].host, false);
  9543. }
  9544. static void vmx_arm_hv_timer(struct vcpu_vmx *vmx, u32 val)
  9545. {
  9546. vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, val);
  9547. if (!vmx->loaded_vmcs->hv_timer_armed)
  9548. vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
  9549. PIN_BASED_VMX_PREEMPTION_TIMER);
  9550. vmx->loaded_vmcs->hv_timer_armed = true;
  9551. }
  9552. static void vmx_update_hv_timer(struct kvm_vcpu *vcpu)
  9553. {
  9554. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9555. u64 tscl;
  9556. u32 delta_tsc;
  9557. if (vmx->req_immediate_exit) {
  9558. vmx_arm_hv_timer(vmx, 0);
  9559. return;
  9560. }
  9561. if (vmx->hv_deadline_tsc != -1) {
  9562. tscl = rdtsc();
  9563. if (vmx->hv_deadline_tsc > tscl)
  9564. /* set_hv_timer ensures the delta fits in 32-bits */
  9565. delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
  9566. cpu_preemption_timer_multi);
  9567. else
  9568. delta_tsc = 0;
  9569. vmx_arm_hv_timer(vmx, delta_tsc);
  9570. return;
  9571. }
  9572. if (vmx->loaded_vmcs->hv_timer_armed)
  9573. vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
  9574. PIN_BASED_VMX_PREEMPTION_TIMER);
  9575. vmx->loaded_vmcs->hv_timer_armed = false;
  9576. }
  9577. static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
  9578. {
  9579. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9580. unsigned long cr3, cr4, evmcs_rsp;
  9581. /* Record the guest's net vcpu time for enforced NMI injections. */
  9582. if (unlikely(!enable_vnmi &&
  9583. vmx->loaded_vmcs->soft_vnmi_blocked))
  9584. vmx->loaded_vmcs->entry_time = ktime_get();
  9585. /* Don't enter VMX if guest state is invalid, let the exit handler
  9586. start emulation until we arrive back to a valid state */
  9587. if (vmx->emulation_required)
  9588. return;
  9589. if (vmx->ple_window_dirty) {
  9590. vmx->ple_window_dirty = false;
  9591. vmcs_write32(PLE_WINDOW, vmx->ple_window);
  9592. }
  9593. if (vmx->nested.need_vmcs12_sync) {
  9594. /*
  9595. * hv_evmcs may end up being not mapped after migration (when
  9596. * L2 was running), map it here to make sure vmcs12 changes are
  9597. * properly reflected.
  9598. */
  9599. if (vmx->nested.enlightened_vmcs_enabled &&
  9600. !vmx->nested.hv_evmcs)
  9601. nested_vmx_handle_enlightened_vmptrld(vcpu, false);
  9602. if (vmx->nested.hv_evmcs) {
  9603. copy_vmcs12_to_enlightened(vmx);
  9604. /* All fields are clean */
  9605. vmx->nested.hv_evmcs->hv_clean_fields |=
  9606. HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
  9607. } else {
  9608. copy_vmcs12_to_shadow(vmx);
  9609. }
  9610. vmx->nested.need_vmcs12_sync = false;
  9611. }
  9612. if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
  9613. vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
  9614. if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
  9615. vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
  9616. cr3 = __get_current_cr3_fast();
  9617. if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
  9618. vmcs_writel(HOST_CR3, cr3);
  9619. vmx->loaded_vmcs->host_state.cr3 = cr3;
  9620. }
  9621. cr4 = cr4_read_shadow();
  9622. if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
  9623. vmcs_writel(HOST_CR4, cr4);
  9624. vmx->loaded_vmcs->host_state.cr4 = cr4;
  9625. }
  9626. /* When single-stepping over STI and MOV SS, we must clear the
  9627. * corresponding interruptibility bits in the guest state. Otherwise
  9628. * vmentry fails as it then expects bit 14 (BS) in pending debug
  9629. * exceptions being set, but that's not correct for the guest debugging
  9630. * case. */
  9631. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
  9632. vmx_set_interrupt_shadow(vcpu, 0);
  9633. if (static_cpu_has(X86_FEATURE_PKU) &&
  9634. kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
  9635. vcpu->arch.pkru != vmx->host_pkru)
  9636. __write_pkru(vcpu->arch.pkru);
  9637. atomic_switch_perf_msrs(vmx);
  9638. vmx_update_hv_timer(vcpu);
  9639. /*
  9640. * If this vCPU has touched SPEC_CTRL, restore the guest's value if
  9641. * it's non-zero. Since vmentry is serialising on affected CPUs, there
  9642. * is no need to worry about the conditional branch over the wrmsr
  9643. * being speculatively taken.
  9644. */
  9645. x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
  9646. vmx->__launched = vmx->loaded_vmcs->launched;
  9647. evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
  9648. (unsigned long)&current_evmcs->host_rsp : 0;
  9649. if (static_branch_unlikely(&vmx_l1d_should_flush))
  9650. vmx_l1d_flush(vcpu);
  9651. asm(
  9652. /* Store host registers */
  9653. "push %%" _ASM_DX "; push %%" _ASM_BP ";"
  9654. "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
  9655. "push %%" _ASM_CX " \n\t"
  9656. "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
  9657. "je 1f \n\t"
  9658. "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
  9659. /* Avoid VMWRITE when Enlightened VMCS is in use */
  9660. "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
  9661. "jz 2f \n\t"
  9662. "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
  9663. "jmp 1f \n\t"
  9664. "2: \n\t"
  9665. __ex("vmwrite %%" _ASM_SP ", %%" _ASM_DX) "\n\t"
  9666. "1: \n\t"
  9667. /* Reload cr2 if changed */
  9668. "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
  9669. "mov %%cr2, %%" _ASM_DX " \n\t"
  9670. "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
  9671. "je 3f \n\t"
  9672. "mov %%" _ASM_AX", %%cr2 \n\t"
  9673. "3: \n\t"
  9674. /* Check if vmlaunch of vmresume is needed */
  9675. "cmpl $0, %c[launched](%0) \n\t"
  9676. /* Load guest registers. Don't clobber flags. */
  9677. "mov %c[rax](%0), %%" _ASM_AX " \n\t"
  9678. "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
  9679. "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
  9680. "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
  9681. "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
  9682. "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
  9683. #ifdef CONFIG_X86_64
  9684. "mov %c[r8](%0), %%r8 \n\t"
  9685. "mov %c[r9](%0), %%r9 \n\t"
  9686. "mov %c[r10](%0), %%r10 \n\t"
  9687. "mov %c[r11](%0), %%r11 \n\t"
  9688. "mov %c[r12](%0), %%r12 \n\t"
  9689. "mov %c[r13](%0), %%r13 \n\t"
  9690. "mov %c[r14](%0), %%r14 \n\t"
  9691. "mov %c[r15](%0), %%r15 \n\t"
  9692. #endif
  9693. "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
  9694. /* Enter guest mode */
  9695. "jne 1f \n\t"
  9696. __ex("vmlaunch") "\n\t"
  9697. "jmp 2f \n\t"
  9698. "1: " __ex("vmresume") "\n\t"
  9699. "2: "
  9700. /* Save guest registers, load host registers, keep flags */
  9701. "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
  9702. "pop %0 \n\t"
  9703. "setbe %c[fail](%0)\n\t"
  9704. "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
  9705. "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
  9706. __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
  9707. "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
  9708. "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
  9709. "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
  9710. "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
  9711. #ifdef CONFIG_X86_64
  9712. "mov %%r8, %c[r8](%0) \n\t"
  9713. "mov %%r9, %c[r9](%0) \n\t"
  9714. "mov %%r10, %c[r10](%0) \n\t"
  9715. "mov %%r11, %c[r11](%0) \n\t"
  9716. "mov %%r12, %c[r12](%0) \n\t"
  9717. "mov %%r13, %c[r13](%0) \n\t"
  9718. "mov %%r14, %c[r14](%0) \n\t"
  9719. "mov %%r15, %c[r15](%0) \n\t"
  9720. /*
  9721. * Clear host registers marked as clobbered to prevent
  9722. * speculative use.
  9723. */
  9724. "xor %%r8d, %%r8d \n\t"
  9725. "xor %%r9d, %%r9d \n\t"
  9726. "xor %%r10d, %%r10d \n\t"
  9727. "xor %%r11d, %%r11d \n\t"
  9728. "xor %%r12d, %%r12d \n\t"
  9729. "xor %%r13d, %%r13d \n\t"
  9730. "xor %%r14d, %%r14d \n\t"
  9731. "xor %%r15d, %%r15d \n\t"
  9732. #endif
  9733. "mov %%cr2, %%" _ASM_AX " \n\t"
  9734. "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
  9735. "xor %%eax, %%eax \n\t"
  9736. "xor %%ebx, %%ebx \n\t"
  9737. "xor %%esi, %%esi \n\t"
  9738. "xor %%edi, %%edi \n\t"
  9739. "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
  9740. ".pushsection .rodata \n\t"
  9741. ".global vmx_return \n\t"
  9742. "vmx_return: " _ASM_PTR " 2b \n\t"
  9743. ".popsection"
  9744. : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
  9745. [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
  9746. [fail]"i"(offsetof(struct vcpu_vmx, fail)),
  9747. [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
  9748. [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
  9749. [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
  9750. [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
  9751. [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
  9752. [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
  9753. [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
  9754. [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
  9755. #ifdef CONFIG_X86_64
  9756. [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
  9757. [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
  9758. [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
  9759. [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
  9760. [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
  9761. [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
  9762. [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
  9763. [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
  9764. #endif
  9765. [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
  9766. [wordsize]"i"(sizeof(ulong))
  9767. : "cc", "memory"
  9768. #ifdef CONFIG_X86_64
  9769. , "rax", "rbx", "rdi"
  9770. , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
  9771. #else
  9772. , "eax", "ebx", "edi"
  9773. #endif
  9774. );
  9775. /*
  9776. * We do not use IBRS in the kernel. If this vCPU has used the
  9777. * SPEC_CTRL MSR it may have left it on; save the value and
  9778. * turn it off. This is much more efficient than blindly adding
  9779. * it to the atomic save/restore list. Especially as the former
  9780. * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
  9781. *
  9782. * For non-nested case:
  9783. * If the L01 MSR bitmap does not intercept the MSR, then we need to
  9784. * save it.
  9785. *
  9786. * For nested case:
  9787. * If the L02 MSR bitmap does not intercept the MSR, then we need to
  9788. * save it.
  9789. */
  9790. if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
  9791. vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
  9792. x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
  9793. /* Eliminate branch target predictions from guest mode */
  9794. vmexit_fill_RSB();
  9795. /* All fields are clean at this point */
  9796. if (static_branch_unlikely(&enable_evmcs))
  9797. current_evmcs->hv_clean_fields |=
  9798. HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
  9799. /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
  9800. if (vmx->host_debugctlmsr)
  9801. update_debugctlmsr(vmx->host_debugctlmsr);
  9802. #ifndef CONFIG_X86_64
  9803. /*
  9804. * The sysexit path does not restore ds/es, so we must set them to
  9805. * a reasonable value ourselves.
  9806. *
  9807. * We can't defer this to vmx_prepare_switch_to_host() since that
  9808. * function may be executed in interrupt context, which saves and
  9809. * restore segments around it, nullifying its effect.
  9810. */
  9811. loadsegment(ds, __USER_DS);
  9812. loadsegment(es, __USER_DS);
  9813. #endif
  9814. vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
  9815. | (1 << VCPU_EXREG_RFLAGS)
  9816. | (1 << VCPU_EXREG_PDPTR)
  9817. | (1 << VCPU_EXREG_SEGMENTS)
  9818. | (1 << VCPU_EXREG_CR3));
  9819. vcpu->arch.regs_dirty = 0;
  9820. /*
  9821. * eager fpu is enabled if PKEY is supported and CR4 is switched
  9822. * back on host, so it is safe to read guest PKRU from current
  9823. * XSAVE.
  9824. */
  9825. if (static_cpu_has(X86_FEATURE_PKU) &&
  9826. kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
  9827. vcpu->arch.pkru = __read_pkru();
  9828. if (vcpu->arch.pkru != vmx->host_pkru)
  9829. __write_pkru(vmx->host_pkru);
  9830. }
  9831. vmx->nested.nested_run_pending = 0;
  9832. vmx->idt_vectoring_info = 0;
  9833. vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
  9834. if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
  9835. return;
  9836. vmx->loaded_vmcs->launched = 1;
  9837. vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
  9838. vmx_complete_atomic_exit(vmx);
  9839. vmx_recover_nmi_blocking(vmx);
  9840. vmx_complete_interrupts(vmx);
  9841. }
  9842. STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
  9843. static struct kvm *vmx_vm_alloc(void)
  9844. {
  9845. struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
  9846. return &kvm_vmx->kvm;
  9847. }
  9848. static void vmx_vm_free(struct kvm *kvm)
  9849. {
  9850. vfree(to_kvm_vmx(kvm));
  9851. }
  9852. static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
  9853. {
  9854. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9855. int cpu;
  9856. if (vmx->loaded_vmcs == vmcs)
  9857. return;
  9858. cpu = get_cpu();
  9859. vmx_vcpu_put(vcpu);
  9860. vmx->loaded_vmcs = vmcs;
  9861. vmx_vcpu_load(vcpu, cpu);
  9862. put_cpu();
  9863. vm_entry_controls_reset_shadow(vmx);
  9864. vm_exit_controls_reset_shadow(vmx);
  9865. vmx_segment_cache_clear(vmx);
  9866. }
  9867. /*
  9868. * Ensure that the current vmcs of the logical processor is the
  9869. * vmcs01 of the vcpu before calling free_nested().
  9870. */
  9871. static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
  9872. {
  9873. vcpu_load(vcpu);
  9874. vmx_switch_vmcs(vcpu, &to_vmx(vcpu)->vmcs01);
  9875. free_nested(vcpu);
  9876. vcpu_put(vcpu);
  9877. }
  9878. static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
  9879. {
  9880. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9881. if (enable_pml)
  9882. vmx_destroy_pml_buffer(vmx);
  9883. free_vpid(vmx->vpid);
  9884. leave_guest_mode(vcpu);
  9885. vmx_free_vcpu_nested(vcpu);
  9886. free_loaded_vmcs(vmx->loaded_vmcs);
  9887. kfree(vmx->guest_msrs);
  9888. kvm_vcpu_uninit(vcpu);
  9889. kmem_cache_free(kvm_vcpu_cache, vmx);
  9890. }
  9891. static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
  9892. {
  9893. int err;
  9894. struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  9895. unsigned long *msr_bitmap;
  9896. int cpu;
  9897. if (!vmx)
  9898. return ERR_PTR(-ENOMEM);
  9899. vmx->vpid = allocate_vpid();
  9900. err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
  9901. if (err)
  9902. goto free_vcpu;
  9903. err = -ENOMEM;
  9904. /*
  9905. * If PML is turned on, failure on enabling PML just results in failure
  9906. * of creating the vcpu, therefore we can simplify PML logic (by
  9907. * avoiding dealing with cases, such as enabling PML partially on vcpus
  9908. * for the guest, etc.
  9909. */
  9910. if (enable_pml) {
  9911. vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
  9912. if (!vmx->pml_pg)
  9913. goto uninit_vcpu;
  9914. }
  9915. vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
  9916. BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
  9917. > PAGE_SIZE);
  9918. if (!vmx->guest_msrs)
  9919. goto free_pml;
  9920. err = alloc_loaded_vmcs(&vmx->vmcs01);
  9921. if (err < 0)
  9922. goto free_msrs;
  9923. msr_bitmap = vmx->vmcs01.msr_bitmap;
  9924. vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
  9925. vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
  9926. vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
  9927. vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
  9928. vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
  9929. vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
  9930. vmx->msr_bitmap_mode = 0;
  9931. vmx->loaded_vmcs = &vmx->vmcs01;
  9932. cpu = get_cpu();
  9933. vmx_vcpu_load(&vmx->vcpu, cpu);
  9934. vmx->vcpu.cpu = cpu;
  9935. vmx_vcpu_setup(vmx);
  9936. vmx_vcpu_put(&vmx->vcpu);
  9937. put_cpu();
  9938. if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
  9939. err = alloc_apic_access_page(kvm);
  9940. if (err)
  9941. goto free_vmcs;
  9942. }
  9943. if (enable_ept && !enable_unrestricted_guest) {
  9944. err = init_rmode_identity_map(kvm);
  9945. if (err)
  9946. goto free_vmcs;
  9947. }
  9948. if (nested)
  9949. nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
  9950. kvm_vcpu_apicv_active(&vmx->vcpu));
  9951. vmx->nested.posted_intr_nv = -1;
  9952. vmx->nested.current_vmptr = -1ull;
  9953. vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
  9954. /*
  9955. * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
  9956. * or POSTED_INTR_WAKEUP_VECTOR.
  9957. */
  9958. vmx->pi_desc.nv = POSTED_INTR_VECTOR;
  9959. vmx->pi_desc.sn = 1;
  9960. return &vmx->vcpu;
  9961. free_vmcs:
  9962. free_loaded_vmcs(vmx->loaded_vmcs);
  9963. free_msrs:
  9964. kfree(vmx->guest_msrs);
  9965. free_pml:
  9966. vmx_destroy_pml_buffer(vmx);
  9967. uninit_vcpu:
  9968. kvm_vcpu_uninit(&vmx->vcpu);
  9969. free_vcpu:
  9970. free_vpid(vmx->vpid);
  9971. kmem_cache_free(kvm_vcpu_cache, vmx);
  9972. return ERR_PTR(err);
  9973. }
  9974. #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"
  9975. #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"
  9976. static int vmx_vm_init(struct kvm *kvm)
  9977. {
  9978. spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
  9979. if (!ple_gap)
  9980. kvm->arch.pause_in_guest = true;
  9981. if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
  9982. switch (l1tf_mitigation) {
  9983. case L1TF_MITIGATION_OFF:
  9984. case L1TF_MITIGATION_FLUSH_NOWARN:
  9985. /* 'I explicitly don't care' is set */
  9986. break;
  9987. case L1TF_MITIGATION_FLUSH:
  9988. case L1TF_MITIGATION_FLUSH_NOSMT:
  9989. case L1TF_MITIGATION_FULL:
  9990. /*
  9991. * Warn upon starting the first VM in a potentially
  9992. * insecure environment.
  9993. */
  9994. if (cpu_smt_control == CPU_SMT_ENABLED)
  9995. pr_warn_once(L1TF_MSG_SMT);
  9996. if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
  9997. pr_warn_once(L1TF_MSG_L1D);
  9998. break;
  9999. case L1TF_MITIGATION_FULL_FORCE:
  10000. /* Flush is enforced */
  10001. break;
  10002. }
  10003. }
  10004. return 0;
  10005. }
  10006. static void __init vmx_check_processor_compat(void *rtn)
  10007. {
  10008. struct vmcs_config vmcs_conf;
  10009. *(int *)rtn = 0;
  10010. if (setup_vmcs_config(&vmcs_conf) < 0)
  10011. *(int *)rtn = -EIO;
  10012. nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
  10013. if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
  10014. printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
  10015. smp_processor_id());
  10016. *(int *)rtn = -EIO;
  10017. }
  10018. }
  10019. static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
  10020. {
  10021. u8 cache;
  10022. u64 ipat = 0;
  10023. /* For VT-d and EPT combination
  10024. * 1. MMIO: always map as UC
  10025. * 2. EPT with VT-d:
  10026. * a. VT-d without snooping control feature: can't guarantee the
  10027. * result, try to trust guest.
  10028. * b. VT-d with snooping control feature: snooping control feature of
  10029. * VT-d engine can guarantee the cache correctness. Just set it
  10030. * to WB to keep consistent with host. So the same as item 3.
  10031. * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
  10032. * consistent with host MTRR
  10033. */
  10034. if (is_mmio) {
  10035. cache = MTRR_TYPE_UNCACHABLE;
  10036. goto exit;
  10037. }
  10038. if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
  10039. ipat = VMX_EPT_IPAT_BIT;
  10040. cache = MTRR_TYPE_WRBACK;
  10041. goto exit;
  10042. }
  10043. if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
  10044. ipat = VMX_EPT_IPAT_BIT;
  10045. if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
  10046. cache = MTRR_TYPE_WRBACK;
  10047. else
  10048. cache = MTRR_TYPE_UNCACHABLE;
  10049. goto exit;
  10050. }
  10051. cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
  10052. exit:
  10053. return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
  10054. }
  10055. static int vmx_get_lpage_level(void)
  10056. {
  10057. if (enable_ept && !cpu_has_vmx_ept_1g_page())
  10058. return PT_DIRECTORY_LEVEL;
  10059. else
  10060. /* For shadow and EPT supported 1GB page */
  10061. return PT_PDPE_LEVEL;
  10062. }
  10063. static void vmcs_set_secondary_exec_control(u32 new_ctl)
  10064. {
  10065. /*
  10066. * These bits in the secondary execution controls field
  10067. * are dynamic, the others are mostly based on the hypervisor
  10068. * architecture and the guest's CPUID. Do not touch the
  10069. * dynamic bits.
  10070. */
  10071. u32 mask =
  10072. SECONDARY_EXEC_SHADOW_VMCS |
  10073. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  10074. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  10075. SECONDARY_EXEC_DESC;
  10076. u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  10077. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  10078. (new_ctl & ~mask) | (cur_ctl & mask));
  10079. }
  10080. /*
  10081. * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
  10082. * (indicating "allowed-1") if they are supported in the guest's CPUID.
  10083. */
  10084. static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
  10085. {
  10086. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10087. struct kvm_cpuid_entry2 *entry;
  10088. vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
  10089. vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
  10090. #define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
  10091. if (entry && (entry->_reg & (_cpuid_mask))) \
  10092. vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
  10093. } while (0)
  10094. entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
  10095. cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
  10096. cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
  10097. cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
  10098. cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
  10099. cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
  10100. cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
  10101. cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
  10102. cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
  10103. cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
  10104. cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
  10105. cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
  10106. cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
  10107. cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
  10108. cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
  10109. entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
  10110. cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
  10111. cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
  10112. cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
  10113. cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
  10114. cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
  10115. #undef cr4_fixed1_update
  10116. }
  10117. static void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
  10118. {
  10119. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10120. if (kvm_mpx_supported()) {
  10121. bool mpx_enabled = guest_cpuid_has(vcpu, X86_FEATURE_MPX);
  10122. if (mpx_enabled) {
  10123. vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
  10124. vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
  10125. } else {
  10126. vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS;
  10127. vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS;
  10128. }
  10129. }
  10130. }
  10131. static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
  10132. {
  10133. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10134. if (cpu_has_secondary_exec_ctrls()) {
  10135. vmx_compute_secondary_exec_control(vmx);
  10136. vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
  10137. }
  10138. if (nested_vmx_allowed(vcpu))
  10139. to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
  10140. FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  10141. else
  10142. to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
  10143. ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  10144. if (nested_vmx_allowed(vcpu)) {
  10145. nested_vmx_cr_fixed1_bits_update(vcpu);
  10146. nested_vmx_entry_exit_ctls_update(vcpu);
  10147. }
  10148. }
  10149. static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
  10150. {
  10151. if (func == 1 && nested)
  10152. entry->ecx |= bit(X86_FEATURE_VMX);
  10153. }
  10154. static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
  10155. struct x86_exception *fault)
  10156. {
  10157. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  10158. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10159. u32 exit_reason;
  10160. unsigned long exit_qualification = vcpu->arch.exit_qualification;
  10161. if (vmx->nested.pml_full) {
  10162. exit_reason = EXIT_REASON_PML_FULL;
  10163. vmx->nested.pml_full = false;
  10164. exit_qualification &= INTR_INFO_UNBLOCK_NMI;
  10165. } else if (fault->error_code & PFERR_RSVD_MASK)
  10166. exit_reason = EXIT_REASON_EPT_MISCONFIG;
  10167. else
  10168. exit_reason = EXIT_REASON_EPT_VIOLATION;
  10169. nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
  10170. vmcs12->guest_physical_address = fault->address;
  10171. }
  10172. static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
  10173. {
  10174. return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
  10175. }
  10176. /* Callbacks for nested_ept_init_mmu_context: */
  10177. static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
  10178. {
  10179. /* return the page table to be shadowed - in our case, EPT12 */
  10180. return get_vmcs12(vcpu)->ept_pointer;
  10181. }
  10182. static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
  10183. {
  10184. WARN_ON(mmu_is_nested(vcpu));
  10185. vcpu->arch.mmu = &vcpu->arch.guest_mmu;
  10186. kvm_init_shadow_ept_mmu(vcpu,
  10187. to_vmx(vcpu)->nested.msrs.ept_caps &
  10188. VMX_EPT_EXECUTE_ONLY_BIT,
  10189. nested_ept_ad_enabled(vcpu),
  10190. nested_ept_get_cr3(vcpu));
  10191. vcpu->arch.mmu->set_cr3 = vmx_set_cr3;
  10192. vcpu->arch.mmu->get_cr3 = nested_ept_get_cr3;
  10193. vcpu->arch.mmu->inject_page_fault = nested_ept_inject_page_fault;
  10194. vcpu->arch.mmu->get_pdptr = kvm_pdptr_read;
  10195. vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
  10196. }
  10197. static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
  10198. {
  10199. vcpu->arch.mmu = &vcpu->arch.root_mmu;
  10200. vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
  10201. }
  10202. static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
  10203. u16 error_code)
  10204. {
  10205. bool inequality, bit;
  10206. bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
  10207. inequality =
  10208. (error_code & vmcs12->page_fault_error_code_mask) !=
  10209. vmcs12->page_fault_error_code_match;
  10210. return inequality ^ bit;
  10211. }
  10212. static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
  10213. struct x86_exception *fault)
  10214. {
  10215. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  10216. WARN_ON(!is_guest_mode(vcpu));
  10217. if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
  10218. !to_vmx(vcpu)->nested.nested_run_pending) {
  10219. vmcs12->vm_exit_intr_error_code = fault->error_code;
  10220. nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
  10221. PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
  10222. INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
  10223. fault->address);
  10224. } else {
  10225. kvm_inject_page_fault(vcpu, fault);
  10226. }
  10227. }
  10228. static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
  10229. struct vmcs12 *vmcs12);
  10230. static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
  10231. {
  10232. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  10233. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10234. struct page *page;
  10235. u64 hpa;
  10236. if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
  10237. /*
  10238. * Translate L1 physical address to host physical
  10239. * address for vmcs02. Keep the page pinned, so this
  10240. * physical address remains valid. We keep a reference
  10241. * to it so we can release it later.
  10242. */
  10243. if (vmx->nested.apic_access_page) { /* shouldn't happen */
  10244. kvm_release_page_dirty(vmx->nested.apic_access_page);
  10245. vmx->nested.apic_access_page = NULL;
  10246. }
  10247. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
  10248. /*
  10249. * If translation failed, no matter: This feature asks
  10250. * to exit when accessing the given address, and if it
  10251. * can never be accessed, this feature won't do
  10252. * anything anyway.
  10253. */
  10254. if (!is_error_page(page)) {
  10255. vmx->nested.apic_access_page = page;
  10256. hpa = page_to_phys(vmx->nested.apic_access_page);
  10257. vmcs_write64(APIC_ACCESS_ADDR, hpa);
  10258. } else {
  10259. vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
  10260. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
  10261. }
  10262. }
  10263. if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
  10264. if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
  10265. kvm_release_page_dirty(vmx->nested.virtual_apic_page);
  10266. vmx->nested.virtual_apic_page = NULL;
  10267. }
  10268. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
  10269. /*
  10270. * If translation failed, VM entry will fail because
  10271. * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
  10272. * Failing the vm entry is _not_ what the processor
  10273. * does but it's basically the only possibility we
  10274. * have. We could still enter the guest if CR8 load
  10275. * exits are enabled, CR8 store exits are enabled, and
  10276. * virtualize APIC access is disabled; in this case
  10277. * the processor would never use the TPR shadow and we
  10278. * could simply clear the bit from the execution
  10279. * control. But such a configuration is useless, so
  10280. * let's keep the code simple.
  10281. */
  10282. if (!is_error_page(page)) {
  10283. vmx->nested.virtual_apic_page = page;
  10284. hpa = page_to_phys(vmx->nested.virtual_apic_page);
  10285. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
  10286. }
  10287. }
  10288. if (nested_cpu_has_posted_intr(vmcs12)) {
  10289. if (vmx->nested.pi_desc_page) { /* shouldn't happen */
  10290. kunmap(vmx->nested.pi_desc_page);
  10291. kvm_release_page_dirty(vmx->nested.pi_desc_page);
  10292. vmx->nested.pi_desc_page = NULL;
  10293. }
  10294. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
  10295. if (is_error_page(page))
  10296. return;
  10297. vmx->nested.pi_desc_page = page;
  10298. vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
  10299. vmx->nested.pi_desc =
  10300. (struct pi_desc *)((void *)vmx->nested.pi_desc +
  10301. (unsigned long)(vmcs12->posted_intr_desc_addr &
  10302. (PAGE_SIZE - 1)));
  10303. vmcs_write64(POSTED_INTR_DESC_ADDR,
  10304. page_to_phys(vmx->nested.pi_desc_page) +
  10305. (unsigned long)(vmcs12->posted_intr_desc_addr &
  10306. (PAGE_SIZE - 1)));
  10307. }
  10308. if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
  10309. vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
  10310. CPU_BASED_USE_MSR_BITMAPS);
  10311. else
  10312. vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
  10313. CPU_BASED_USE_MSR_BITMAPS);
  10314. }
  10315. static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
  10316. {
  10317. u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
  10318. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10319. /*
  10320. * A timer value of zero is architecturally guaranteed to cause
  10321. * a VMExit prior to executing any instructions in the guest.
  10322. */
  10323. if (preemption_timeout == 0) {
  10324. vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
  10325. return;
  10326. }
  10327. if (vcpu->arch.virtual_tsc_khz == 0)
  10328. return;
  10329. preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
  10330. preemption_timeout *= 1000000;
  10331. do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
  10332. hrtimer_start(&vmx->nested.preemption_timer,
  10333. ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
  10334. }
  10335. static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
  10336. struct vmcs12 *vmcs12)
  10337. {
  10338. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
  10339. return 0;
  10340. if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
  10341. !page_address_valid(vcpu, vmcs12->io_bitmap_b))
  10342. return -EINVAL;
  10343. return 0;
  10344. }
  10345. static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
  10346. struct vmcs12 *vmcs12)
  10347. {
  10348. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
  10349. return 0;
  10350. if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
  10351. return -EINVAL;
  10352. return 0;
  10353. }
  10354. static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
  10355. struct vmcs12 *vmcs12)
  10356. {
  10357. if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
  10358. return 0;
  10359. if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
  10360. return -EINVAL;
  10361. return 0;
  10362. }
  10363. /*
  10364. * Merge L0's and L1's MSR bitmap, return false to indicate that
  10365. * we do not use the hardware.
  10366. */
  10367. static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
  10368. struct vmcs12 *vmcs12)
  10369. {
  10370. int msr;
  10371. struct page *page;
  10372. unsigned long *msr_bitmap_l1;
  10373. unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
  10374. /*
  10375. * pred_cmd & spec_ctrl are trying to verify two things:
  10376. *
  10377. * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
  10378. * ensures that we do not accidentally generate an L02 MSR bitmap
  10379. * from the L12 MSR bitmap that is too permissive.
  10380. * 2. That L1 or L2s have actually used the MSR. This avoids
  10381. * unnecessarily merging of the bitmap if the MSR is unused. This
  10382. * works properly because we only update the L01 MSR bitmap lazily.
  10383. * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
  10384. * updated to reflect this when L1 (or its L2s) actually write to
  10385. * the MSR.
  10386. */
  10387. bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
  10388. bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
  10389. /* Nothing to do if the MSR bitmap is not in use. */
  10390. if (!cpu_has_vmx_msr_bitmap() ||
  10391. !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
  10392. return false;
  10393. if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
  10394. !pred_cmd && !spec_ctrl)
  10395. return false;
  10396. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
  10397. if (is_error_page(page))
  10398. return false;
  10399. msr_bitmap_l1 = (unsigned long *)kmap(page);
  10400. if (nested_cpu_has_apic_reg_virt(vmcs12)) {
  10401. /*
  10402. * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
  10403. * just lets the processor take the value from the virtual-APIC page;
  10404. * take those 256 bits directly from the L1 bitmap.
  10405. */
  10406. for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
  10407. unsigned word = msr / BITS_PER_LONG;
  10408. msr_bitmap_l0[word] = msr_bitmap_l1[word];
  10409. msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
  10410. }
  10411. } else {
  10412. for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
  10413. unsigned word = msr / BITS_PER_LONG;
  10414. msr_bitmap_l0[word] = ~0;
  10415. msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
  10416. }
  10417. }
  10418. nested_vmx_disable_intercept_for_msr(
  10419. msr_bitmap_l1, msr_bitmap_l0,
  10420. X2APIC_MSR(APIC_TASKPRI),
  10421. MSR_TYPE_W);
  10422. if (nested_cpu_has_vid(vmcs12)) {
  10423. nested_vmx_disable_intercept_for_msr(
  10424. msr_bitmap_l1, msr_bitmap_l0,
  10425. X2APIC_MSR(APIC_EOI),
  10426. MSR_TYPE_W);
  10427. nested_vmx_disable_intercept_for_msr(
  10428. msr_bitmap_l1, msr_bitmap_l0,
  10429. X2APIC_MSR(APIC_SELF_IPI),
  10430. MSR_TYPE_W);
  10431. }
  10432. if (spec_ctrl)
  10433. nested_vmx_disable_intercept_for_msr(
  10434. msr_bitmap_l1, msr_bitmap_l0,
  10435. MSR_IA32_SPEC_CTRL,
  10436. MSR_TYPE_R | MSR_TYPE_W);
  10437. if (pred_cmd)
  10438. nested_vmx_disable_intercept_for_msr(
  10439. msr_bitmap_l1, msr_bitmap_l0,
  10440. MSR_IA32_PRED_CMD,
  10441. MSR_TYPE_W);
  10442. kunmap(page);
  10443. kvm_release_page_clean(page);
  10444. return true;
  10445. }
  10446. static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
  10447. struct vmcs12 *vmcs12)
  10448. {
  10449. struct vmcs12 *shadow;
  10450. struct page *page;
  10451. if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
  10452. vmcs12->vmcs_link_pointer == -1ull)
  10453. return;
  10454. shadow = get_shadow_vmcs12(vcpu);
  10455. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
  10456. memcpy(shadow, kmap(page), VMCS12_SIZE);
  10457. kunmap(page);
  10458. kvm_release_page_clean(page);
  10459. }
  10460. static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
  10461. struct vmcs12 *vmcs12)
  10462. {
  10463. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10464. if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
  10465. vmcs12->vmcs_link_pointer == -1ull)
  10466. return;
  10467. kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
  10468. get_shadow_vmcs12(vcpu), VMCS12_SIZE);
  10469. }
  10470. static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
  10471. struct vmcs12 *vmcs12)
  10472. {
  10473. if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
  10474. !page_address_valid(vcpu, vmcs12->apic_access_addr))
  10475. return -EINVAL;
  10476. else
  10477. return 0;
  10478. }
  10479. static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
  10480. struct vmcs12 *vmcs12)
  10481. {
  10482. if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
  10483. !nested_cpu_has_apic_reg_virt(vmcs12) &&
  10484. !nested_cpu_has_vid(vmcs12) &&
  10485. !nested_cpu_has_posted_intr(vmcs12))
  10486. return 0;
  10487. /*
  10488. * If virtualize x2apic mode is enabled,
  10489. * virtualize apic access must be disabled.
  10490. */
  10491. if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
  10492. nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
  10493. return -EINVAL;
  10494. /*
  10495. * If virtual interrupt delivery is enabled,
  10496. * we must exit on external interrupts.
  10497. */
  10498. if (nested_cpu_has_vid(vmcs12) &&
  10499. !nested_exit_on_intr(vcpu))
  10500. return -EINVAL;
  10501. /*
  10502. * bits 15:8 should be zero in posted_intr_nv,
  10503. * the descriptor address has been already checked
  10504. * in nested_get_vmcs12_pages.
  10505. *
  10506. * bits 5:0 of posted_intr_desc_addr should be zero.
  10507. */
  10508. if (nested_cpu_has_posted_intr(vmcs12) &&
  10509. (!nested_cpu_has_vid(vmcs12) ||
  10510. !nested_exit_intr_ack_set(vcpu) ||
  10511. (vmcs12->posted_intr_nv & 0xff00) ||
  10512. (vmcs12->posted_intr_desc_addr & 0x3f) ||
  10513. (vmcs12->posted_intr_desc_addr >> cpuid_maxphyaddr(vcpu))))
  10514. return -EINVAL;
  10515. /* tpr shadow is needed by all apicv features. */
  10516. if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
  10517. return -EINVAL;
  10518. return 0;
  10519. }
  10520. static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
  10521. unsigned long count_field,
  10522. unsigned long addr_field)
  10523. {
  10524. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  10525. int maxphyaddr;
  10526. u64 count, addr;
  10527. if (vmcs12_read_any(vmcs12, count_field, &count) ||
  10528. vmcs12_read_any(vmcs12, addr_field, &addr)) {
  10529. WARN_ON(1);
  10530. return -EINVAL;
  10531. }
  10532. if (count == 0)
  10533. return 0;
  10534. maxphyaddr = cpuid_maxphyaddr(vcpu);
  10535. if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
  10536. (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
  10537. pr_debug_ratelimited(
  10538. "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
  10539. addr_field, maxphyaddr, count, addr);
  10540. return -EINVAL;
  10541. }
  10542. return 0;
  10543. }
  10544. static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
  10545. struct vmcs12 *vmcs12)
  10546. {
  10547. if (vmcs12->vm_exit_msr_load_count == 0 &&
  10548. vmcs12->vm_exit_msr_store_count == 0 &&
  10549. vmcs12->vm_entry_msr_load_count == 0)
  10550. return 0; /* Fast path */
  10551. if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
  10552. VM_EXIT_MSR_LOAD_ADDR) ||
  10553. nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
  10554. VM_EXIT_MSR_STORE_ADDR) ||
  10555. nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
  10556. VM_ENTRY_MSR_LOAD_ADDR))
  10557. return -EINVAL;
  10558. return 0;
  10559. }
  10560. static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
  10561. struct vmcs12 *vmcs12)
  10562. {
  10563. if (!nested_cpu_has_pml(vmcs12))
  10564. return 0;
  10565. if (!nested_cpu_has_ept(vmcs12) ||
  10566. !page_address_valid(vcpu, vmcs12->pml_address))
  10567. return -EINVAL;
  10568. return 0;
  10569. }
  10570. static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
  10571. struct vmcs12 *vmcs12)
  10572. {
  10573. if (!nested_cpu_has_shadow_vmcs(vmcs12))
  10574. return 0;
  10575. if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
  10576. !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
  10577. return -EINVAL;
  10578. return 0;
  10579. }
  10580. static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
  10581. struct vmx_msr_entry *e)
  10582. {
  10583. /* x2APIC MSR accesses are not allowed */
  10584. if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
  10585. return -EINVAL;
  10586. if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
  10587. e->index == MSR_IA32_UCODE_REV)
  10588. return -EINVAL;
  10589. if (e->reserved != 0)
  10590. return -EINVAL;
  10591. return 0;
  10592. }
  10593. static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
  10594. struct vmx_msr_entry *e)
  10595. {
  10596. if (e->index == MSR_FS_BASE ||
  10597. e->index == MSR_GS_BASE ||
  10598. e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
  10599. nested_vmx_msr_check_common(vcpu, e))
  10600. return -EINVAL;
  10601. return 0;
  10602. }
  10603. static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
  10604. struct vmx_msr_entry *e)
  10605. {
  10606. if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
  10607. nested_vmx_msr_check_common(vcpu, e))
  10608. return -EINVAL;
  10609. return 0;
  10610. }
  10611. /*
  10612. * Load guest's/host's msr at nested entry/exit.
  10613. * return 0 for success, entry index for failure.
  10614. */
  10615. static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
  10616. {
  10617. u32 i;
  10618. struct vmx_msr_entry e;
  10619. struct msr_data msr;
  10620. msr.host_initiated = false;
  10621. for (i = 0; i < count; i++) {
  10622. if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
  10623. &e, sizeof(e))) {
  10624. pr_debug_ratelimited(
  10625. "%s cannot read MSR entry (%u, 0x%08llx)\n",
  10626. __func__, i, gpa + i * sizeof(e));
  10627. goto fail;
  10628. }
  10629. if (nested_vmx_load_msr_check(vcpu, &e)) {
  10630. pr_debug_ratelimited(
  10631. "%s check failed (%u, 0x%x, 0x%x)\n",
  10632. __func__, i, e.index, e.reserved);
  10633. goto fail;
  10634. }
  10635. msr.index = e.index;
  10636. msr.data = e.value;
  10637. if (kvm_set_msr(vcpu, &msr)) {
  10638. pr_debug_ratelimited(
  10639. "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
  10640. __func__, i, e.index, e.value);
  10641. goto fail;
  10642. }
  10643. }
  10644. return 0;
  10645. fail:
  10646. return i + 1;
  10647. }
  10648. static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
  10649. {
  10650. u32 i;
  10651. struct vmx_msr_entry e;
  10652. for (i = 0; i < count; i++) {
  10653. struct msr_data msr_info;
  10654. if (kvm_vcpu_read_guest(vcpu,
  10655. gpa + i * sizeof(e),
  10656. &e, 2 * sizeof(u32))) {
  10657. pr_debug_ratelimited(
  10658. "%s cannot read MSR entry (%u, 0x%08llx)\n",
  10659. __func__, i, gpa + i * sizeof(e));
  10660. return -EINVAL;
  10661. }
  10662. if (nested_vmx_store_msr_check(vcpu, &e)) {
  10663. pr_debug_ratelimited(
  10664. "%s check failed (%u, 0x%x, 0x%x)\n",
  10665. __func__, i, e.index, e.reserved);
  10666. return -EINVAL;
  10667. }
  10668. msr_info.host_initiated = false;
  10669. msr_info.index = e.index;
  10670. if (kvm_get_msr(vcpu, &msr_info)) {
  10671. pr_debug_ratelimited(
  10672. "%s cannot read MSR (%u, 0x%x)\n",
  10673. __func__, i, e.index);
  10674. return -EINVAL;
  10675. }
  10676. if (kvm_vcpu_write_guest(vcpu,
  10677. gpa + i * sizeof(e) +
  10678. offsetof(struct vmx_msr_entry, value),
  10679. &msr_info.data, sizeof(msr_info.data))) {
  10680. pr_debug_ratelimited(
  10681. "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
  10682. __func__, i, e.index, msr_info.data);
  10683. return -EINVAL;
  10684. }
  10685. }
  10686. return 0;
  10687. }
  10688. static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
  10689. {
  10690. unsigned long invalid_mask;
  10691. invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
  10692. return (val & invalid_mask) == 0;
  10693. }
  10694. /*
  10695. * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
  10696. * emulating VM entry into a guest with EPT enabled.
  10697. * Returns 0 on success, 1 on failure. Invalid state exit qualification code
  10698. * is assigned to entry_failure_code on failure.
  10699. */
  10700. static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
  10701. u32 *entry_failure_code)
  10702. {
  10703. if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
  10704. if (!nested_cr3_valid(vcpu, cr3)) {
  10705. *entry_failure_code = ENTRY_FAIL_DEFAULT;
  10706. return 1;
  10707. }
  10708. /*
  10709. * If PAE paging and EPT are both on, CR3 is not used by the CPU and
  10710. * must not be dereferenced.
  10711. */
  10712. if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
  10713. !nested_ept) {
  10714. if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
  10715. *entry_failure_code = ENTRY_FAIL_PDPTE;
  10716. return 1;
  10717. }
  10718. }
  10719. }
  10720. if (!nested_ept)
  10721. kvm_mmu_new_cr3(vcpu, cr3, false);
  10722. vcpu->arch.cr3 = cr3;
  10723. __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
  10724. kvm_init_mmu(vcpu, false);
  10725. return 0;
  10726. }
  10727. /*
  10728. * Returns if KVM is able to config CPU to tag TLB entries
  10729. * populated by L2 differently than TLB entries populated
  10730. * by L1.
  10731. *
  10732. * If L1 uses EPT, then TLB entries are tagged with different EPTP.
  10733. *
  10734. * If L1 uses VPID and we allocated a vpid02, TLB entries are tagged
  10735. * with different VPID (L1 entries are tagged with vmx->vpid
  10736. * while L2 entries are tagged with vmx->nested.vpid02).
  10737. */
  10738. static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu)
  10739. {
  10740. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  10741. return nested_cpu_has_ept(vmcs12) ||
  10742. (nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02);
  10743. }
  10744. static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
  10745. {
  10746. if (vmx->nested.nested_run_pending &&
  10747. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
  10748. return vmcs12->guest_ia32_efer;
  10749. else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
  10750. return vmx->vcpu.arch.efer | (EFER_LMA | EFER_LME);
  10751. else
  10752. return vmx->vcpu.arch.efer & ~(EFER_LMA | EFER_LME);
  10753. }
  10754. static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx)
  10755. {
  10756. /*
  10757. * If vmcs02 hasn't been initialized, set the constant vmcs02 state
  10758. * according to L0's settings (vmcs12 is irrelevant here). Host
  10759. * fields that come from L0 and are not constant, e.g. HOST_CR3,
  10760. * will be set as needed prior to VMLAUNCH/VMRESUME.
  10761. */
  10762. if (vmx->nested.vmcs02_initialized)
  10763. return;
  10764. vmx->nested.vmcs02_initialized = true;
  10765. /*
  10766. * We don't care what the EPTP value is we just need to guarantee
  10767. * it's valid so we don't get a false positive when doing early
  10768. * consistency checks.
  10769. */
  10770. if (enable_ept && nested_early_check)
  10771. vmcs_write64(EPT_POINTER, construct_eptp(&vmx->vcpu, 0));
  10772. /* All VMFUNCs are currently emulated through L0 vmexits. */
  10773. if (cpu_has_vmx_vmfunc())
  10774. vmcs_write64(VM_FUNCTION_CONTROL, 0);
  10775. if (cpu_has_vmx_posted_intr())
  10776. vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
  10777. if (cpu_has_vmx_msr_bitmap())
  10778. vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
  10779. if (enable_pml)
  10780. vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
  10781. /*
  10782. * Set the MSR load/store lists to match L0's settings. Only the
  10783. * addresses are constant (for vmcs02), the counts can change based
  10784. * on L2's behavior, e.g. switching to/from long mode.
  10785. */
  10786. vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
  10787. vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
  10788. vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
  10789. vmx_set_constant_host_state(vmx);
  10790. }
  10791. static void prepare_vmcs02_early_full(struct vcpu_vmx *vmx,
  10792. struct vmcs12 *vmcs12)
  10793. {
  10794. prepare_vmcs02_constant_state(vmx);
  10795. vmcs_write64(VMCS_LINK_POINTER, -1ull);
  10796. if (enable_vpid) {
  10797. if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
  10798. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
  10799. else
  10800. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
  10801. }
  10802. }
  10803. static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
  10804. {
  10805. u32 exec_control, vmcs12_exec_ctrl;
  10806. u64 guest_efer = nested_vmx_calc_efer(vmx, vmcs12);
  10807. if (vmx->nested.dirty_vmcs12 || vmx->nested.hv_evmcs)
  10808. prepare_vmcs02_early_full(vmx, vmcs12);
  10809. /*
  10810. * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
  10811. * entry, but only if the current (host) sp changed from the value
  10812. * we wrote last (vmx->host_rsp). This cache is no longer relevant
  10813. * if we switch vmcs, and rather than hold a separate cache per vmcs,
  10814. * here we just force the write to happen on entry. host_rsp will
  10815. * also be written unconditionally by nested_vmx_check_vmentry_hw()
  10816. * if we are doing early consistency checks via hardware.
  10817. */
  10818. vmx->host_rsp = 0;
  10819. /*
  10820. * PIN CONTROLS
  10821. */
  10822. exec_control = vmcs12->pin_based_vm_exec_control;
  10823. /* Preemption timer setting is computed directly in vmx_vcpu_run. */
  10824. exec_control |= vmcs_config.pin_based_exec_ctrl;
  10825. exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  10826. vmx->loaded_vmcs->hv_timer_armed = false;
  10827. /* Posted interrupts setting is only taken from vmcs12. */
  10828. if (nested_cpu_has_posted_intr(vmcs12)) {
  10829. vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
  10830. vmx->nested.pi_pending = false;
  10831. } else {
  10832. exec_control &= ~PIN_BASED_POSTED_INTR;
  10833. }
  10834. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
  10835. /*
  10836. * EXEC CONTROLS
  10837. */
  10838. exec_control = vmx_exec_control(vmx); /* L0's desires */
  10839. exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
  10840. exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
  10841. exec_control &= ~CPU_BASED_TPR_SHADOW;
  10842. exec_control |= vmcs12->cpu_based_vm_exec_control;
  10843. /*
  10844. * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
  10845. * nested_get_vmcs12_pages can't fix it up, the illegal value
  10846. * will result in a VM entry failure.
  10847. */
  10848. if (exec_control & CPU_BASED_TPR_SHADOW) {
  10849. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
  10850. vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
  10851. } else {
  10852. #ifdef CONFIG_X86_64
  10853. exec_control |= CPU_BASED_CR8_LOAD_EXITING |
  10854. CPU_BASED_CR8_STORE_EXITING;
  10855. #endif
  10856. }
  10857. /*
  10858. * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
  10859. * for I/O port accesses.
  10860. */
  10861. exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
  10862. exec_control |= CPU_BASED_UNCOND_IO_EXITING;
  10863. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
  10864. /*
  10865. * SECONDARY EXEC CONTROLS
  10866. */
  10867. if (cpu_has_secondary_exec_ctrls()) {
  10868. exec_control = vmx->secondary_exec_control;
  10869. /* Take the following fields only from vmcs12 */
  10870. exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  10871. SECONDARY_EXEC_ENABLE_INVPCID |
  10872. SECONDARY_EXEC_RDTSCP |
  10873. SECONDARY_EXEC_XSAVES |
  10874. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
  10875. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  10876. SECONDARY_EXEC_ENABLE_VMFUNC);
  10877. if (nested_cpu_has(vmcs12,
  10878. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
  10879. vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
  10880. ~SECONDARY_EXEC_ENABLE_PML;
  10881. exec_control |= vmcs12_exec_ctrl;
  10882. }
  10883. /* VMCS shadowing for L2 is emulated for now */
  10884. exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
  10885. if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
  10886. vmcs_write16(GUEST_INTR_STATUS,
  10887. vmcs12->guest_intr_status);
  10888. /*
  10889. * Write an illegal value to APIC_ACCESS_ADDR. Later,
  10890. * nested_get_vmcs12_pages will either fix it up or
  10891. * remove the VM execution control.
  10892. */
  10893. if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
  10894. vmcs_write64(APIC_ACCESS_ADDR, -1ull);
  10895. if (exec_control & SECONDARY_EXEC_ENCLS_EXITING)
  10896. vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
  10897. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
  10898. }
  10899. /*
  10900. * ENTRY CONTROLS
  10901. *
  10902. * vmcs12's VM_{ENTRY,EXIT}_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE
  10903. * are emulated by vmx_set_efer() in prepare_vmcs02(), but speculate
  10904. * on the related bits (if supported by the CPU) in the hope that
  10905. * we can avoid VMWrites during vmx_set_efer().
  10906. */
  10907. exec_control = (vmcs12->vm_entry_controls | vmcs_config.vmentry_ctrl) &
  10908. ~VM_ENTRY_IA32E_MODE & ~VM_ENTRY_LOAD_IA32_EFER;
  10909. if (cpu_has_load_ia32_efer) {
  10910. if (guest_efer & EFER_LMA)
  10911. exec_control |= VM_ENTRY_IA32E_MODE;
  10912. if (guest_efer != host_efer)
  10913. exec_control |= VM_ENTRY_LOAD_IA32_EFER;
  10914. }
  10915. vm_entry_controls_init(vmx, exec_control);
  10916. /*
  10917. * EXIT CONTROLS
  10918. *
  10919. * L2->L1 exit controls are emulated - the hardware exit is to L0 so
  10920. * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
  10921. * bits may be modified by vmx_set_efer() in prepare_vmcs02().
  10922. */
  10923. exec_control = vmcs_config.vmexit_ctrl;
  10924. if (cpu_has_load_ia32_efer && guest_efer != host_efer)
  10925. exec_control |= VM_EXIT_LOAD_IA32_EFER;
  10926. vm_exit_controls_init(vmx, exec_control);
  10927. /*
  10928. * Conceptually we want to copy the PML address and index from
  10929. * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
  10930. * since we always flush the log on each vmexit and never change
  10931. * the PML address (once set), this happens to be equivalent to
  10932. * simply resetting the index in vmcs02.
  10933. */
  10934. if (enable_pml)
  10935. vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
  10936. /*
  10937. * Interrupt/Exception Fields
  10938. */
  10939. if (vmx->nested.nested_run_pending) {
  10940. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  10941. vmcs12->vm_entry_intr_info_field);
  10942. vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
  10943. vmcs12->vm_entry_exception_error_code);
  10944. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  10945. vmcs12->vm_entry_instruction_len);
  10946. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
  10947. vmcs12->guest_interruptibility_info);
  10948. vmx->loaded_vmcs->nmi_known_unmasked =
  10949. !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
  10950. } else {
  10951. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
  10952. }
  10953. }
  10954. static void prepare_vmcs02_full(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
  10955. {
  10956. struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs;
  10957. if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
  10958. HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) {
  10959. vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
  10960. vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
  10961. vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
  10962. vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
  10963. vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
  10964. vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
  10965. vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
  10966. vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
  10967. vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
  10968. vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
  10969. vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
  10970. vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
  10971. vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
  10972. vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
  10973. vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
  10974. vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
  10975. vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
  10976. vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
  10977. vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
  10978. vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
  10979. vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
  10980. vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
  10981. vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
  10982. vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
  10983. vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
  10984. vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
  10985. vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
  10986. vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
  10987. vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
  10988. vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
  10989. vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
  10990. vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
  10991. vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
  10992. vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
  10993. }
  10994. if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
  10995. HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1)) {
  10996. vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
  10997. vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
  10998. vmcs12->guest_pending_dbg_exceptions);
  10999. vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
  11000. vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
  11001. /*
  11002. * L1 may access the L2's PDPTR, so save them to construct
  11003. * vmcs12
  11004. */
  11005. if (enable_ept) {
  11006. vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
  11007. vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
  11008. vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
  11009. vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
  11010. }
  11011. }
  11012. if (nested_cpu_has_xsaves(vmcs12))
  11013. vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
  11014. /*
  11015. * Whether page-faults are trapped is determined by a combination of
  11016. * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
  11017. * If enable_ept, L0 doesn't care about page faults and we should
  11018. * set all of these to L1's desires. However, if !enable_ept, L0 does
  11019. * care about (at least some) page faults, and because it is not easy
  11020. * (if at all possible?) to merge L0 and L1's desires, we simply ask
  11021. * to exit on each and every L2 page fault. This is done by setting
  11022. * MASK=MATCH=0 and (see below) EB.PF=1.
  11023. * Note that below we don't need special code to set EB.PF beyond the
  11024. * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
  11025. * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
  11026. * !enable_ept, EB.PF is 1, so the "or" will always be 1.
  11027. */
  11028. vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
  11029. enable_ept ? vmcs12->page_fault_error_code_mask : 0);
  11030. vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
  11031. enable_ept ? vmcs12->page_fault_error_code_match : 0);
  11032. if (cpu_has_vmx_apicv()) {
  11033. vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
  11034. vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
  11035. vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
  11036. vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
  11037. }
  11038. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
  11039. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
  11040. set_cr4_guest_host_mask(vmx);
  11041. if (kvm_mpx_supported()) {
  11042. if (vmx->nested.nested_run_pending &&
  11043. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
  11044. vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
  11045. else
  11046. vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs);
  11047. }
  11048. }
  11049. /*
  11050. * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
  11051. * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
  11052. * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
  11053. * guest in a way that will both be appropriate to L1's requests, and our
  11054. * needs. In addition to modifying the active vmcs (which is vmcs02), this
  11055. * function also has additional necessary side-effects, like setting various
  11056. * vcpu->arch fields.
  11057. * Returns 0 on success, 1 on failure. Invalid state exit qualification code
  11058. * is assigned to entry_failure_code on failure.
  11059. */
  11060. static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
  11061. u32 *entry_failure_code)
  11062. {
  11063. struct vcpu_vmx *vmx = to_vmx(vcpu);
  11064. struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs;
  11065. if (vmx->nested.dirty_vmcs12 || vmx->nested.hv_evmcs) {
  11066. prepare_vmcs02_full(vmx, vmcs12);
  11067. vmx->nested.dirty_vmcs12 = false;
  11068. }
  11069. /*
  11070. * First, the fields that are shadowed. This must be kept in sync
  11071. * with vmx_shadow_fields.h.
  11072. */
  11073. if (!hv_evmcs || !(hv_evmcs->hv_clean_fields &
  11074. HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) {
  11075. vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
  11076. vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
  11077. }
  11078. if (vmx->nested.nested_run_pending &&
  11079. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
  11080. kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
  11081. vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
  11082. } else {
  11083. kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
  11084. vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
  11085. }
  11086. vmx_set_rflags(vcpu, vmcs12->guest_rflags);
  11087. vmx->nested.preemption_timer_expired = false;
  11088. if (nested_cpu_has_preemption_timer(vmcs12))
  11089. vmx_start_preemption_timer(vcpu);
  11090. /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
  11091. * bitwise-or of what L1 wants to trap for L2, and what we want to
  11092. * trap. Note that CR0.TS also needs updating - we do this later.
  11093. */
  11094. update_exception_bitmap(vcpu);
  11095. vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
  11096. vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
  11097. if (vmx->nested.nested_run_pending &&
  11098. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
  11099. vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
  11100. vcpu->arch.pat = vmcs12->guest_ia32_pat;
  11101. } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
  11102. vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
  11103. }
  11104. vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
  11105. if (kvm_has_tsc_control)
  11106. decache_tsc_multiplier(vmx);
  11107. if (enable_vpid) {
  11108. /*
  11109. * There is no direct mapping between vpid02 and vpid12, the
  11110. * vpid02 is per-vCPU for L0 and reused while the value of
  11111. * vpid12 is changed w/ one invvpid during nested vmentry.
  11112. * The vpid12 is allocated by L1 for L2, so it will not
  11113. * influence global bitmap(for vpid01 and vpid02 allocation)
  11114. * even if spawn a lot of nested vCPUs.
  11115. */
  11116. if (nested_cpu_has_vpid(vmcs12) && nested_has_guest_tlb_tag(vcpu)) {
  11117. if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
  11118. vmx->nested.last_vpid = vmcs12->virtual_processor_id;
  11119. __vmx_flush_tlb(vcpu, nested_get_vpid02(vcpu), false);
  11120. }
  11121. } else {
  11122. /*
  11123. * If L1 use EPT, then L0 needs to execute INVEPT on
  11124. * EPTP02 instead of EPTP01. Therefore, delay TLB
  11125. * flush until vmcs02->eptp is fully updated by
  11126. * KVM_REQ_LOAD_CR3. Note that this assumes
  11127. * KVM_REQ_TLB_FLUSH is evaluated after
  11128. * KVM_REQ_LOAD_CR3 in vcpu_enter_guest().
  11129. */
  11130. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  11131. }
  11132. }
  11133. if (nested_cpu_has_ept(vmcs12))
  11134. nested_ept_init_mmu_context(vcpu);
  11135. else if (nested_cpu_has2(vmcs12,
  11136. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
  11137. vmx_flush_tlb(vcpu, true);
  11138. /*
  11139. * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
  11140. * bits which we consider mandatory enabled.
  11141. * The CR0_READ_SHADOW is what L2 should have expected to read given
  11142. * the specifications by L1; It's not enough to take
  11143. * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
  11144. * have more bits than L1 expected.
  11145. */
  11146. vmx_set_cr0(vcpu, vmcs12->guest_cr0);
  11147. vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
  11148. vmx_set_cr4(vcpu, vmcs12->guest_cr4);
  11149. vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
  11150. vcpu->arch.efer = nested_vmx_calc_efer(vmx, vmcs12);
  11151. /* Note: may modify VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
  11152. vmx_set_efer(vcpu, vcpu->arch.efer);
  11153. /*
  11154. * Guest state is invalid and unrestricted guest is disabled,
  11155. * which means L1 attempted VMEntry to L2 with invalid state.
  11156. * Fail the VMEntry.
  11157. */
  11158. if (vmx->emulation_required) {
  11159. *entry_failure_code = ENTRY_FAIL_DEFAULT;
  11160. return 1;
  11161. }
  11162. /* Shadow page tables on either EPT or shadow page tables. */
  11163. if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
  11164. entry_failure_code))
  11165. return 1;
  11166. if (!enable_ept)
  11167. vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
  11168. kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
  11169. kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
  11170. return 0;
  11171. }
  11172. static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
  11173. {
  11174. if (!nested_cpu_has_nmi_exiting(vmcs12) &&
  11175. nested_cpu_has_virtual_nmis(vmcs12))
  11176. return -EINVAL;
  11177. if (!nested_cpu_has_virtual_nmis(vmcs12) &&
  11178. nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
  11179. return -EINVAL;
  11180. return 0;
  11181. }
  11182. static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  11183. {
  11184. struct vcpu_vmx *vmx = to_vmx(vcpu);
  11185. bool ia32e;
  11186. if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
  11187. vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
  11188. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11189. if (nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id)
  11190. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11191. if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
  11192. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11193. if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
  11194. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11195. if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
  11196. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11197. if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
  11198. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11199. if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
  11200. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11201. if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
  11202. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11203. if (nested_vmx_check_pml_controls(vcpu, vmcs12))
  11204. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11205. if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12))
  11206. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11207. if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
  11208. vmx->nested.msrs.procbased_ctls_low,
  11209. vmx->nested.msrs.procbased_ctls_high) ||
  11210. (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
  11211. !vmx_control_verify(vmcs12->secondary_vm_exec_control,
  11212. vmx->nested.msrs.secondary_ctls_low,
  11213. vmx->nested.msrs.secondary_ctls_high)) ||
  11214. !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
  11215. vmx->nested.msrs.pinbased_ctls_low,
  11216. vmx->nested.msrs.pinbased_ctls_high) ||
  11217. !vmx_control_verify(vmcs12->vm_exit_controls,
  11218. vmx->nested.msrs.exit_ctls_low,
  11219. vmx->nested.msrs.exit_ctls_high) ||
  11220. !vmx_control_verify(vmcs12->vm_entry_controls,
  11221. vmx->nested.msrs.entry_ctls_low,
  11222. vmx->nested.msrs.entry_ctls_high))
  11223. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11224. if (nested_vmx_check_nmi_controls(vmcs12))
  11225. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11226. if (nested_cpu_has_vmfunc(vmcs12)) {
  11227. if (vmcs12->vm_function_control &
  11228. ~vmx->nested.msrs.vmfunc_controls)
  11229. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11230. if (nested_cpu_has_eptp_switching(vmcs12)) {
  11231. if (!nested_cpu_has_ept(vmcs12) ||
  11232. !page_address_valid(vcpu, vmcs12->eptp_list_address))
  11233. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11234. }
  11235. }
  11236. if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
  11237. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11238. if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
  11239. !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
  11240. !nested_cr3_valid(vcpu, vmcs12->host_cr3))
  11241. return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
  11242. /*
  11243. * If the load IA32_EFER VM-exit control is 1, bits reserved in the
  11244. * IA32_EFER MSR must be 0 in the field for that register. In addition,
  11245. * the values of the LMA and LME bits in the field must each be that of
  11246. * the host address-space size VM-exit control.
  11247. */
  11248. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
  11249. ia32e = (vmcs12->vm_exit_controls &
  11250. VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
  11251. if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
  11252. ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
  11253. ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
  11254. return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
  11255. }
  11256. /*
  11257. * From the Intel SDM, volume 3:
  11258. * Fields relevant to VM-entry event injection must be set properly.
  11259. * These fields are the VM-entry interruption-information field, the
  11260. * VM-entry exception error code, and the VM-entry instruction length.
  11261. */
  11262. if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
  11263. u32 intr_info = vmcs12->vm_entry_intr_info_field;
  11264. u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
  11265. u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
  11266. bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
  11267. bool should_have_error_code;
  11268. bool urg = nested_cpu_has2(vmcs12,
  11269. SECONDARY_EXEC_UNRESTRICTED_GUEST);
  11270. bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
  11271. /* VM-entry interruption-info field: interruption type */
  11272. if (intr_type == INTR_TYPE_RESERVED ||
  11273. (intr_type == INTR_TYPE_OTHER_EVENT &&
  11274. !nested_cpu_supports_monitor_trap_flag(vcpu)))
  11275. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11276. /* VM-entry interruption-info field: vector */
  11277. if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
  11278. (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
  11279. (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
  11280. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11281. /* VM-entry interruption-info field: deliver error code */
  11282. should_have_error_code =
  11283. intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
  11284. x86_exception_has_error_code(vector);
  11285. if (has_error_code != should_have_error_code)
  11286. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11287. /* VM-entry exception error code */
  11288. if (has_error_code &&
  11289. vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
  11290. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11291. /* VM-entry interruption-info field: reserved bits */
  11292. if (intr_info & INTR_INFO_RESVD_BITS_MASK)
  11293. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11294. /* VM-entry instruction length */
  11295. switch (intr_type) {
  11296. case INTR_TYPE_SOFT_EXCEPTION:
  11297. case INTR_TYPE_SOFT_INTR:
  11298. case INTR_TYPE_PRIV_SW_EXCEPTION:
  11299. if ((vmcs12->vm_entry_instruction_len > 15) ||
  11300. (vmcs12->vm_entry_instruction_len == 0 &&
  11301. !nested_cpu_has_zero_length_injection(vcpu)))
  11302. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11303. }
  11304. }
  11305. if (nested_cpu_has_ept(vmcs12) &&
  11306. !valid_ept_address(vcpu, vmcs12->ept_pointer))
  11307. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  11308. return 0;
  11309. }
  11310. static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
  11311. struct vmcs12 *vmcs12)
  11312. {
  11313. int r;
  11314. struct page *page;
  11315. struct vmcs12 *shadow;
  11316. if (vmcs12->vmcs_link_pointer == -1ull)
  11317. return 0;
  11318. if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
  11319. return -EINVAL;
  11320. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
  11321. if (is_error_page(page))
  11322. return -EINVAL;
  11323. r = 0;
  11324. shadow = kmap(page);
  11325. if (shadow->hdr.revision_id != VMCS12_REVISION ||
  11326. shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
  11327. r = -EINVAL;
  11328. kunmap(page);
  11329. kvm_release_page_clean(page);
  11330. return r;
  11331. }
  11332. static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
  11333. u32 *exit_qual)
  11334. {
  11335. bool ia32e;
  11336. *exit_qual = ENTRY_FAIL_DEFAULT;
  11337. if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
  11338. !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
  11339. return 1;
  11340. if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
  11341. *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
  11342. return 1;
  11343. }
  11344. /*
  11345. * If the load IA32_EFER VM-entry control is 1, the following checks
  11346. * are performed on the field for the IA32_EFER MSR:
  11347. * - Bits reserved in the IA32_EFER MSR must be 0.
  11348. * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
  11349. * the IA-32e mode guest VM-exit control. It must also be identical
  11350. * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
  11351. * CR0.PG) is 1.
  11352. */
  11353. if (to_vmx(vcpu)->nested.nested_run_pending &&
  11354. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
  11355. ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
  11356. if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
  11357. ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
  11358. ((vmcs12->guest_cr0 & X86_CR0_PG) &&
  11359. ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
  11360. return 1;
  11361. }
  11362. if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
  11363. (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
  11364. (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
  11365. return 1;
  11366. return 0;
  11367. }
  11368. static int __noclone nested_vmx_check_vmentry_hw(struct kvm_vcpu *vcpu)
  11369. {
  11370. struct vcpu_vmx *vmx = to_vmx(vcpu);
  11371. unsigned long cr3, cr4;
  11372. if (!nested_early_check)
  11373. return 0;
  11374. if (vmx->msr_autoload.host.nr)
  11375. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
  11376. if (vmx->msr_autoload.guest.nr)
  11377. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
  11378. preempt_disable();
  11379. vmx_prepare_switch_to_guest(vcpu);
  11380. /*
  11381. * Induce a consistency check VMExit by clearing bit 1 in GUEST_RFLAGS,
  11382. * which is reserved to '1' by hardware. GUEST_RFLAGS is guaranteed to
  11383. * be written (by preparve_vmcs02()) before the "real" VMEnter, i.e.
  11384. * there is no need to preserve other bits or save/restore the field.
  11385. */
  11386. vmcs_writel(GUEST_RFLAGS, 0);
  11387. vmcs_writel(HOST_RIP, vmx_early_consistency_check_return);
  11388. cr3 = __get_current_cr3_fast();
  11389. if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
  11390. vmcs_writel(HOST_CR3, cr3);
  11391. vmx->loaded_vmcs->host_state.cr3 = cr3;
  11392. }
  11393. cr4 = cr4_read_shadow();
  11394. if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
  11395. vmcs_writel(HOST_CR4, cr4);
  11396. vmx->loaded_vmcs->host_state.cr4 = cr4;
  11397. }
  11398. vmx->__launched = vmx->loaded_vmcs->launched;
  11399. asm(
  11400. /* Set HOST_RSP */
  11401. __ex("vmwrite %%" _ASM_SP ", %%" _ASM_DX) "\n\t"
  11402. "mov %%" _ASM_SP ", %c[host_rsp](%0)\n\t"
  11403. /* Check if vmlaunch of vmresume is needed */
  11404. "cmpl $0, %c[launched](%0)\n\t"
  11405. "je 1f\n\t"
  11406. __ex("vmresume") "\n\t"
  11407. "jmp 2f\n\t"
  11408. "1: " __ex("vmlaunch") "\n\t"
  11409. "jmp 2f\n\t"
  11410. "2: "
  11411. /* Set vmx->fail accordingly */
  11412. "setbe %c[fail](%0)\n\t"
  11413. ".pushsection .rodata\n\t"
  11414. ".global vmx_early_consistency_check_return\n\t"
  11415. "vmx_early_consistency_check_return: " _ASM_PTR " 2b\n\t"
  11416. ".popsection"
  11417. :
  11418. : "c"(vmx), "d"((unsigned long)HOST_RSP),
  11419. [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
  11420. [fail]"i"(offsetof(struct vcpu_vmx, fail)),
  11421. [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp))
  11422. : "rax", "cc", "memory"
  11423. );
  11424. vmcs_writel(HOST_RIP, vmx_return);
  11425. preempt_enable();
  11426. if (vmx->msr_autoload.host.nr)
  11427. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
  11428. if (vmx->msr_autoload.guest.nr)
  11429. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
  11430. if (vmx->fail) {
  11431. WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) !=
  11432. VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  11433. vmx->fail = 0;
  11434. return 1;
  11435. }
  11436. /*
  11437. * VMExit clears RFLAGS.IF and DR7, even on a consistency check.
  11438. */
  11439. local_irq_enable();
  11440. if (hw_breakpoint_active())
  11441. set_debugreg(__this_cpu_read(cpu_dr7), 7);
  11442. /*
  11443. * A non-failing VMEntry means we somehow entered guest mode with
  11444. * an illegal RIP, and that's just the tip of the iceberg. There
  11445. * is no telling what memory has been modified or what state has
  11446. * been exposed to unknown code. Hitting this all but guarantees
  11447. * a (very critical) hardware issue.
  11448. */
  11449. WARN_ON(!(vmcs_read32(VM_EXIT_REASON) &
  11450. VMX_EXIT_REASONS_FAILED_VMENTRY));
  11451. return 0;
  11452. }
  11453. STACK_FRAME_NON_STANDARD(nested_vmx_check_vmentry_hw);
  11454. static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
  11455. struct vmcs12 *vmcs12);
  11456. /*
  11457. * If from_vmentry is false, this is being called from state restore (either RSM
  11458. * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
  11459. + *
  11460. + * Returns:
  11461. + * 0 - success, i.e. proceed with actual VMEnter
  11462. + * 1 - consistency check VMExit
  11463. + * -1 - consistency check VMFail
  11464. */
  11465. static int nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu,
  11466. bool from_vmentry)
  11467. {
  11468. struct vcpu_vmx *vmx = to_vmx(vcpu);
  11469. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  11470. bool evaluate_pending_interrupts;
  11471. u32 exit_reason = EXIT_REASON_INVALID_STATE;
  11472. u32 exit_qual;
  11473. evaluate_pending_interrupts = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
  11474. (CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_VIRTUAL_NMI_PENDING);
  11475. if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu))
  11476. evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu);
  11477. if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
  11478. vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
  11479. if (kvm_mpx_supported() &&
  11480. !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
  11481. vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
  11482. vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
  11483. prepare_vmcs02_early(vmx, vmcs12);
  11484. if (from_vmentry) {
  11485. nested_get_vmcs12_pages(vcpu);
  11486. if (nested_vmx_check_vmentry_hw(vcpu)) {
  11487. vmx_switch_vmcs(vcpu, &vmx->vmcs01);
  11488. return -1;
  11489. }
  11490. if (check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
  11491. goto vmentry_fail_vmexit;
  11492. }
  11493. enter_guest_mode(vcpu);
  11494. if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
  11495. vcpu->arch.tsc_offset += vmcs12->tsc_offset;
  11496. if (prepare_vmcs02(vcpu, vmcs12, &exit_qual))
  11497. goto vmentry_fail_vmexit_guest_mode;
  11498. if (from_vmentry) {
  11499. exit_reason = EXIT_REASON_MSR_LOAD_FAIL;
  11500. exit_qual = nested_vmx_load_msr(vcpu,
  11501. vmcs12->vm_entry_msr_load_addr,
  11502. vmcs12->vm_entry_msr_load_count);
  11503. if (exit_qual)
  11504. goto vmentry_fail_vmexit_guest_mode;
  11505. } else {
  11506. /*
  11507. * The MMU is not initialized to point at the right entities yet and
  11508. * "get pages" would need to read data from the guest (i.e. we will
  11509. * need to perform gpa to hpa translation). Request a call
  11510. * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
  11511. * have already been set at vmentry time and should not be reset.
  11512. */
  11513. kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
  11514. }
  11515. /*
  11516. * If L1 had a pending IRQ/NMI until it executed
  11517. * VMLAUNCH/VMRESUME which wasn't delivered because it was
  11518. * disallowed (e.g. interrupts disabled), L0 needs to
  11519. * evaluate if this pending event should cause an exit from L2
  11520. * to L1 or delivered directly to L2 (e.g. In case L1 don't
  11521. * intercept EXTERNAL_INTERRUPT).
  11522. *
  11523. * Usually this would be handled by the processor noticing an
  11524. * IRQ/NMI window request, or checking RVI during evaluation of
  11525. * pending virtual interrupts. However, this setting was done
  11526. * on VMCS01 and now VMCS02 is active instead. Thus, we force L0
  11527. * to perform pending event evaluation by requesting a KVM_REQ_EVENT.
  11528. */
  11529. if (unlikely(evaluate_pending_interrupts))
  11530. kvm_make_request(KVM_REQ_EVENT, vcpu);
  11531. /*
  11532. * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
  11533. * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
  11534. * returned as far as L1 is concerned. It will only return (and set
  11535. * the success flag) when L2 exits (see nested_vmx_vmexit()).
  11536. */
  11537. return 0;
  11538. /*
  11539. * A failed consistency check that leads to a VMExit during L1's
  11540. * VMEnter to L2 is a variation of a normal VMexit, as explained in
  11541. * 26.7 "VM-entry failures during or after loading guest state".
  11542. */
  11543. vmentry_fail_vmexit_guest_mode:
  11544. if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
  11545. vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
  11546. leave_guest_mode(vcpu);
  11547. vmentry_fail_vmexit:
  11548. vmx_switch_vmcs(vcpu, &vmx->vmcs01);
  11549. if (!from_vmentry)
  11550. return 1;
  11551. load_vmcs12_host_state(vcpu, vmcs12);
  11552. vmcs12->vm_exit_reason = exit_reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
  11553. vmcs12->exit_qualification = exit_qual;
  11554. if (enable_shadow_vmcs || vmx->nested.hv_evmcs)
  11555. vmx->nested.need_vmcs12_sync = true;
  11556. return 1;
  11557. }
  11558. /*
  11559. * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
  11560. * for running an L2 nested guest.
  11561. */
  11562. static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
  11563. {
  11564. struct vmcs12 *vmcs12;
  11565. struct vcpu_vmx *vmx = to_vmx(vcpu);
  11566. u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
  11567. int ret;
  11568. if (!nested_vmx_check_permission(vcpu))
  11569. return 1;
  11570. if (!nested_vmx_handle_enlightened_vmptrld(vcpu, true))
  11571. return 1;
  11572. if (!vmx->nested.hv_evmcs && vmx->nested.current_vmptr == -1ull)
  11573. return nested_vmx_failInvalid(vcpu);
  11574. vmcs12 = get_vmcs12(vcpu);
  11575. /*
  11576. * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
  11577. * that there *is* a valid VMCS pointer, RFLAGS.CF is set
  11578. * rather than RFLAGS.ZF, and no error number is stored to the
  11579. * VM-instruction error field.
  11580. */
  11581. if (vmcs12->hdr.shadow_vmcs)
  11582. return nested_vmx_failInvalid(vcpu);
  11583. if (vmx->nested.hv_evmcs) {
  11584. copy_enlightened_to_vmcs12(vmx);
  11585. /* Enlightened VMCS doesn't have launch state */
  11586. vmcs12->launch_state = !launch;
  11587. } else if (enable_shadow_vmcs) {
  11588. copy_shadow_to_vmcs12(vmx);
  11589. }
  11590. /*
  11591. * The nested entry process starts with enforcing various prerequisites
  11592. * on vmcs12 as required by the Intel SDM, and act appropriately when
  11593. * they fail: As the SDM explains, some conditions should cause the
  11594. * instruction to fail, while others will cause the instruction to seem
  11595. * to succeed, but return an EXIT_REASON_INVALID_STATE.
  11596. * To speed up the normal (success) code path, we should avoid checking
  11597. * for misconfigurations which will anyway be caught by the processor
  11598. * when using the merged vmcs02.
  11599. */
  11600. if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS)
  11601. return nested_vmx_failValid(vcpu,
  11602. VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
  11603. if (vmcs12->launch_state == launch)
  11604. return nested_vmx_failValid(vcpu,
  11605. launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
  11606. : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
  11607. ret = check_vmentry_prereqs(vcpu, vmcs12);
  11608. if (ret)
  11609. return nested_vmx_failValid(vcpu, ret);
  11610. /*
  11611. * We're finally done with prerequisite checking, and can start with
  11612. * the nested entry.
  11613. */
  11614. vmx->nested.nested_run_pending = 1;
  11615. ret = nested_vmx_enter_non_root_mode(vcpu, true);
  11616. vmx->nested.nested_run_pending = !ret;
  11617. if (ret > 0)
  11618. return 1;
  11619. else if (ret)
  11620. return nested_vmx_failValid(vcpu,
  11621. VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  11622. /* Hide L1D cache contents from the nested guest. */
  11623. vmx->vcpu.arch.l1tf_flush_l1d = true;
  11624. /*
  11625. * Must happen outside of nested_vmx_enter_non_root_mode() as it will
  11626. * also be used as part of restoring nVMX state for
  11627. * snapshot restore (migration).
  11628. *
  11629. * In this flow, it is assumed that vmcs12 cache was
  11630. * trasferred as part of captured nVMX state and should
  11631. * therefore not be read from guest memory (which may not
  11632. * exist on destination host yet).
  11633. */
  11634. nested_cache_shadow_vmcs12(vcpu, vmcs12);
  11635. /*
  11636. * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
  11637. * by event injection, halt vcpu.
  11638. */
  11639. if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
  11640. !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
  11641. vmx->nested.nested_run_pending = 0;
  11642. return kvm_vcpu_halt(vcpu);
  11643. }
  11644. return 1;
  11645. }
  11646. /*
  11647. * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
  11648. * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
  11649. * This function returns the new value we should put in vmcs12.guest_cr0.
  11650. * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
  11651. * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
  11652. * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
  11653. * didn't trap the bit, because if L1 did, so would L0).
  11654. * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
  11655. * been modified by L2, and L1 knows it. So just leave the old value of
  11656. * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
  11657. * isn't relevant, because if L0 traps this bit it can set it to anything.
  11658. * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
  11659. * changed these bits, and therefore they need to be updated, but L0
  11660. * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
  11661. * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
  11662. */
  11663. static inline unsigned long
  11664. vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  11665. {
  11666. return
  11667. /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
  11668. /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
  11669. /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
  11670. vcpu->arch.cr0_guest_owned_bits));
  11671. }
  11672. static inline unsigned long
  11673. vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  11674. {
  11675. return
  11676. /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
  11677. /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
  11678. /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
  11679. vcpu->arch.cr4_guest_owned_bits));
  11680. }
  11681. static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
  11682. struct vmcs12 *vmcs12)
  11683. {
  11684. u32 idt_vectoring;
  11685. unsigned int nr;
  11686. if (vcpu->arch.exception.injected) {
  11687. nr = vcpu->arch.exception.nr;
  11688. idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
  11689. if (kvm_exception_is_soft(nr)) {
  11690. vmcs12->vm_exit_instruction_len =
  11691. vcpu->arch.event_exit_inst_len;
  11692. idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
  11693. } else
  11694. idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
  11695. if (vcpu->arch.exception.has_error_code) {
  11696. idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
  11697. vmcs12->idt_vectoring_error_code =
  11698. vcpu->arch.exception.error_code;
  11699. }
  11700. vmcs12->idt_vectoring_info_field = idt_vectoring;
  11701. } else if (vcpu->arch.nmi_injected) {
  11702. vmcs12->idt_vectoring_info_field =
  11703. INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
  11704. } else if (vcpu->arch.interrupt.injected) {
  11705. nr = vcpu->arch.interrupt.nr;
  11706. idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
  11707. if (vcpu->arch.interrupt.soft) {
  11708. idt_vectoring |= INTR_TYPE_SOFT_INTR;
  11709. vmcs12->vm_entry_instruction_len =
  11710. vcpu->arch.event_exit_inst_len;
  11711. } else
  11712. idt_vectoring |= INTR_TYPE_EXT_INTR;
  11713. vmcs12->idt_vectoring_info_field = idt_vectoring;
  11714. }
  11715. }
  11716. static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
  11717. {
  11718. struct vcpu_vmx *vmx = to_vmx(vcpu);
  11719. unsigned long exit_qual;
  11720. bool block_nested_events =
  11721. vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
  11722. if (vcpu->arch.exception.pending &&
  11723. nested_vmx_check_exception(vcpu, &exit_qual)) {
  11724. if (block_nested_events)
  11725. return -EBUSY;
  11726. nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
  11727. return 0;
  11728. }
  11729. if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
  11730. vmx->nested.preemption_timer_expired) {
  11731. if (block_nested_events)
  11732. return -EBUSY;
  11733. nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
  11734. return 0;
  11735. }
  11736. if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
  11737. if (block_nested_events)
  11738. return -EBUSY;
  11739. nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
  11740. NMI_VECTOR | INTR_TYPE_NMI_INTR |
  11741. INTR_INFO_VALID_MASK, 0);
  11742. /*
  11743. * The NMI-triggered VM exit counts as injection:
  11744. * clear this one and block further NMIs.
  11745. */
  11746. vcpu->arch.nmi_pending = 0;
  11747. vmx_set_nmi_mask(vcpu, true);
  11748. return 0;
  11749. }
  11750. if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
  11751. nested_exit_on_intr(vcpu)) {
  11752. if (block_nested_events)
  11753. return -EBUSY;
  11754. nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
  11755. return 0;
  11756. }
  11757. vmx_complete_nested_posted_interrupt(vcpu);
  11758. return 0;
  11759. }
  11760. static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu)
  11761. {
  11762. to_vmx(vcpu)->req_immediate_exit = true;
  11763. }
  11764. static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
  11765. {
  11766. ktime_t remaining =
  11767. hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
  11768. u64 value;
  11769. if (ktime_to_ns(remaining) <= 0)
  11770. return 0;
  11771. value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
  11772. do_div(value, 1000000);
  11773. return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
  11774. }
  11775. /*
  11776. * Update the guest state fields of vmcs12 to reflect changes that
  11777. * occurred while L2 was running. (The "IA-32e mode guest" bit of the
  11778. * VM-entry controls is also updated, since this is really a guest
  11779. * state bit.)
  11780. */
  11781. static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  11782. {
  11783. vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
  11784. vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
  11785. vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
  11786. vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
  11787. vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
  11788. vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
  11789. vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
  11790. vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
  11791. vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
  11792. vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
  11793. vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
  11794. vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
  11795. vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
  11796. vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
  11797. vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
  11798. vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
  11799. vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
  11800. vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
  11801. vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
  11802. vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
  11803. vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
  11804. vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
  11805. vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
  11806. vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
  11807. vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
  11808. vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
  11809. vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
  11810. vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
  11811. vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
  11812. vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
  11813. vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
  11814. vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
  11815. vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
  11816. vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
  11817. vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
  11818. vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
  11819. vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
  11820. vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
  11821. vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
  11822. vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
  11823. vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
  11824. vmcs12->guest_interruptibility_info =
  11825. vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  11826. vmcs12->guest_pending_dbg_exceptions =
  11827. vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
  11828. if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
  11829. vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
  11830. else
  11831. vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
  11832. if (nested_cpu_has_preemption_timer(vmcs12)) {
  11833. if (vmcs12->vm_exit_controls &
  11834. VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
  11835. vmcs12->vmx_preemption_timer_value =
  11836. vmx_get_preemption_timer_value(vcpu);
  11837. hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
  11838. }
  11839. /*
  11840. * In some cases (usually, nested EPT), L2 is allowed to change its
  11841. * own CR3 without exiting. If it has changed it, we must keep it.
  11842. * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
  11843. * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
  11844. *
  11845. * Additionally, restore L2's PDPTR to vmcs12.
  11846. */
  11847. if (enable_ept) {
  11848. vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
  11849. vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
  11850. vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
  11851. vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
  11852. vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
  11853. }
  11854. vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
  11855. if (nested_cpu_has_vid(vmcs12))
  11856. vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
  11857. vmcs12->vm_entry_controls =
  11858. (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
  11859. (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
  11860. if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
  11861. kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
  11862. vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
  11863. }
  11864. /* TODO: These cannot have changed unless we have MSR bitmaps and
  11865. * the relevant bit asks not to trap the change */
  11866. if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
  11867. vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
  11868. if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
  11869. vmcs12->guest_ia32_efer = vcpu->arch.efer;
  11870. vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
  11871. vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
  11872. vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
  11873. if (kvm_mpx_supported())
  11874. vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
  11875. }
  11876. /*
  11877. * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
  11878. * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
  11879. * and this function updates it to reflect the changes to the guest state while
  11880. * L2 was running (and perhaps made some exits which were handled directly by L0
  11881. * without going back to L1), and to reflect the exit reason.
  11882. * Note that we do not have to copy here all VMCS fields, just those that
  11883. * could have changed by the L2 guest or the exit - i.e., the guest-state and
  11884. * exit-information fields only. Other fields are modified by L1 with VMWRITE,
  11885. * which already writes to vmcs12 directly.
  11886. */
  11887. static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
  11888. u32 exit_reason, u32 exit_intr_info,
  11889. unsigned long exit_qualification)
  11890. {
  11891. /* update guest state fields: */
  11892. sync_vmcs12(vcpu, vmcs12);
  11893. /* update exit information fields: */
  11894. vmcs12->vm_exit_reason = exit_reason;
  11895. vmcs12->exit_qualification = exit_qualification;
  11896. vmcs12->vm_exit_intr_info = exit_intr_info;
  11897. vmcs12->idt_vectoring_info_field = 0;
  11898. vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  11899. vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  11900. if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
  11901. vmcs12->launch_state = 1;
  11902. /* vm_entry_intr_info_field is cleared on exit. Emulate this
  11903. * instead of reading the real value. */
  11904. vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
  11905. /*
  11906. * Transfer the event that L0 or L1 may wanted to inject into
  11907. * L2 to IDT_VECTORING_INFO_FIELD.
  11908. */
  11909. vmcs12_save_pending_event(vcpu, vmcs12);
  11910. }
  11911. /*
  11912. * Drop what we picked up for L2 via vmx_complete_interrupts. It is
  11913. * preserved above and would only end up incorrectly in L1.
  11914. */
  11915. vcpu->arch.nmi_injected = false;
  11916. kvm_clear_exception_queue(vcpu);
  11917. kvm_clear_interrupt_queue(vcpu);
  11918. }
  11919. /*
  11920. * A part of what we need to when the nested L2 guest exits and we want to
  11921. * run its L1 parent, is to reset L1's guest state to the host state specified
  11922. * in vmcs12.
  11923. * This function is to be called not only on normal nested exit, but also on
  11924. * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
  11925. * Failures During or After Loading Guest State").
  11926. * This function should be called when the active VMCS is L1's (vmcs01).
  11927. */
  11928. static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
  11929. struct vmcs12 *vmcs12)
  11930. {
  11931. struct kvm_segment seg;
  11932. u32 entry_failure_code;
  11933. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
  11934. vcpu->arch.efer = vmcs12->host_ia32_efer;
  11935. else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
  11936. vcpu->arch.efer |= (EFER_LMA | EFER_LME);
  11937. else
  11938. vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
  11939. vmx_set_efer(vcpu, vcpu->arch.efer);
  11940. kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
  11941. kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
  11942. vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
  11943. vmx_set_interrupt_shadow(vcpu, 0);
  11944. /*
  11945. * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
  11946. * actually changed, because vmx_set_cr0 refers to efer set above.
  11947. *
  11948. * CR0_GUEST_HOST_MASK is already set in the original vmcs01
  11949. * (KVM doesn't change it);
  11950. */
  11951. vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
  11952. vmx_set_cr0(vcpu, vmcs12->host_cr0);
  11953. /* Same as above - no reason to call set_cr4_guest_host_mask(). */
  11954. vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
  11955. vmx_set_cr4(vcpu, vmcs12->host_cr4);
  11956. nested_ept_uninit_mmu_context(vcpu);
  11957. /*
  11958. * Only PDPTE load can fail as the value of cr3 was checked on entry and
  11959. * couldn't have changed.
  11960. */
  11961. if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
  11962. nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
  11963. if (!enable_ept)
  11964. vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
  11965. /*
  11966. * If vmcs01 doesn't use VPID, CPU flushes TLB on every
  11967. * VMEntry/VMExit. Thus, no need to flush TLB.
  11968. *
  11969. * If vmcs12 doesn't use VPID, L1 expects TLB to be
  11970. * flushed on every VMEntry/VMExit.
  11971. *
  11972. * Otherwise, we can preserve TLB entries as long as we are
  11973. * able to tag L1 TLB entries differently than L2 TLB entries.
  11974. *
  11975. * If vmcs12 uses EPT, we need to execute this flush on EPTP01
  11976. * and therefore we request the TLB flush to happen only after VMCS EPTP
  11977. * has been set by KVM_REQ_LOAD_CR3.
  11978. */
  11979. if (enable_vpid &&
  11980. (!nested_cpu_has_vpid(vmcs12) || !nested_has_guest_tlb_tag(vcpu))) {
  11981. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  11982. }
  11983. vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
  11984. vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
  11985. vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
  11986. vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
  11987. vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
  11988. vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
  11989. vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
  11990. /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
  11991. if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
  11992. vmcs_write64(GUEST_BNDCFGS, 0);
  11993. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
  11994. vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
  11995. vcpu->arch.pat = vmcs12->host_ia32_pat;
  11996. }
  11997. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
  11998. vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
  11999. vmcs12->host_ia32_perf_global_ctrl);
  12000. /* Set L1 segment info according to Intel SDM
  12001. 27.5.2 Loading Host Segment and Descriptor-Table Registers */
  12002. seg = (struct kvm_segment) {
  12003. .base = 0,
  12004. .limit = 0xFFFFFFFF,
  12005. .selector = vmcs12->host_cs_selector,
  12006. .type = 11,
  12007. .present = 1,
  12008. .s = 1,
  12009. .g = 1
  12010. };
  12011. if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
  12012. seg.l = 1;
  12013. else
  12014. seg.db = 1;
  12015. vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
  12016. seg = (struct kvm_segment) {
  12017. .base = 0,
  12018. .limit = 0xFFFFFFFF,
  12019. .type = 3,
  12020. .present = 1,
  12021. .s = 1,
  12022. .db = 1,
  12023. .g = 1
  12024. };
  12025. seg.selector = vmcs12->host_ds_selector;
  12026. vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
  12027. seg.selector = vmcs12->host_es_selector;
  12028. vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
  12029. seg.selector = vmcs12->host_ss_selector;
  12030. vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
  12031. seg.selector = vmcs12->host_fs_selector;
  12032. seg.base = vmcs12->host_fs_base;
  12033. vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
  12034. seg.selector = vmcs12->host_gs_selector;
  12035. seg.base = vmcs12->host_gs_base;
  12036. vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
  12037. seg = (struct kvm_segment) {
  12038. .base = vmcs12->host_tr_base,
  12039. .limit = 0x67,
  12040. .selector = vmcs12->host_tr_selector,
  12041. .type = 11,
  12042. .present = 1
  12043. };
  12044. vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
  12045. kvm_set_dr(vcpu, 7, 0x400);
  12046. vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
  12047. if (cpu_has_vmx_msr_bitmap())
  12048. vmx_update_msr_bitmap(vcpu);
  12049. if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
  12050. vmcs12->vm_exit_msr_load_count))
  12051. nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
  12052. }
  12053. static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx)
  12054. {
  12055. struct shared_msr_entry *efer_msr;
  12056. unsigned int i;
  12057. if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER)
  12058. return vmcs_read64(GUEST_IA32_EFER);
  12059. if (cpu_has_load_ia32_efer)
  12060. return host_efer;
  12061. for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) {
  12062. if (vmx->msr_autoload.guest.val[i].index == MSR_EFER)
  12063. return vmx->msr_autoload.guest.val[i].value;
  12064. }
  12065. efer_msr = find_msr_entry(vmx, MSR_EFER);
  12066. if (efer_msr)
  12067. return efer_msr->data;
  12068. return host_efer;
  12069. }
  12070. static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu)
  12071. {
  12072. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  12073. struct vcpu_vmx *vmx = to_vmx(vcpu);
  12074. struct vmx_msr_entry g, h;
  12075. struct msr_data msr;
  12076. gpa_t gpa;
  12077. u32 i, j;
  12078. vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT);
  12079. if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
  12080. /*
  12081. * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set
  12082. * as vmcs01.GUEST_DR7 contains a userspace defined value
  12083. * and vcpu->arch.dr7 is not squirreled away before the
  12084. * nested VMENTER (not worth adding a variable in nested_vmx).
  12085. */
  12086. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
  12087. kvm_set_dr(vcpu, 7, DR7_FIXED_1);
  12088. else
  12089. WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7)));
  12090. }
  12091. /*
  12092. * Note that calling vmx_set_{efer,cr0,cr4} is important as they
  12093. * handle a variety of side effects to KVM's software model.
  12094. */
  12095. vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx));
  12096. vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
  12097. vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW));
  12098. vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
  12099. vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW));
  12100. nested_ept_uninit_mmu_context(vcpu);
  12101. vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
  12102. __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
  12103. /*
  12104. * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs
  12105. * from vmcs01 (if necessary). The PDPTRs are not loaded on
  12106. * VMFail, like everything else we just need to ensure our
  12107. * software model is up-to-date.
  12108. */
  12109. ept_save_pdptrs(vcpu);
  12110. kvm_mmu_reset_context(vcpu);
  12111. if (cpu_has_vmx_msr_bitmap())
  12112. vmx_update_msr_bitmap(vcpu);
  12113. /*
  12114. * This nasty bit of open coding is a compromise between blindly
  12115. * loading L1's MSRs using the exit load lists (incorrect emulation
  12116. * of VMFail), leaving the nested VM's MSRs in the software model
  12117. * (incorrect behavior) and snapshotting the modified MSRs (too
  12118. * expensive since the lists are unbound by hardware). For each
  12119. * MSR that was (prematurely) loaded from the nested VMEntry load
  12120. * list, reload it from the exit load list if it exists and differs
  12121. * from the guest value. The intent is to stuff host state as
  12122. * silently as possible, not to fully process the exit load list.
  12123. */
  12124. msr.host_initiated = false;
  12125. for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) {
  12126. gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g));
  12127. if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) {
  12128. pr_debug_ratelimited(
  12129. "%s read MSR index failed (%u, 0x%08llx)\n",
  12130. __func__, i, gpa);
  12131. goto vmabort;
  12132. }
  12133. for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) {
  12134. gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h));
  12135. if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) {
  12136. pr_debug_ratelimited(
  12137. "%s read MSR failed (%u, 0x%08llx)\n",
  12138. __func__, j, gpa);
  12139. goto vmabort;
  12140. }
  12141. if (h.index != g.index)
  12142. continue;
  12143. if (h.value == g.value)
  12144. break;
  12145. if (nested_vmx_load_msr_check(vcpu, &h)) {
  12146. pr_debug_ratelimited(
  12147. "%s check failed (%u, 0x%x, 0x%x)\n",
  12148. __func__, j, h.index, h.reserved);
  12149. goto vmabort;
  12150. }
  12151. msr.index = h.index;
  12152. msr.data = h.value;
  12153. if (kvm_set_msr(vcpu, &msr)) {
  12154. pr_debug_ratelimited(
  12155. "%s WRMSR failed (%u, 0x%x, 0x%llx)\n",
  12156. __func__, j, h.index, h.value);
  12157. goto vmabort;
  12158. }
  12159. }
  12160. }
  12161. return;
  12162. vmabort:
  12163. nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
  12164. }
  12165. /*
  12166. * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
  12167. * and modify vmcs12 to make it see what it would expect to see there if
  12168. * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
  12169. */
  12170. static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
  12171. u32 exit_intr_info,
  12172. unsigned long exit_qualification)
  12173. {
  12174. struct vcpu_vmx *vmx = to_vmx(vcpu);
  12175. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  12176. /* trying to cancel vmlaunch/vmresume is a bug */
  12177. WARN_ON_ONCE(vmx->nested.nested_run_pending);
  12178. leave_guest_mode(vcpu);
  12179. if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
  12180. vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
  12181. if (likely(!vmx->fail)) {
  12182. if (exit_reason == -1)
  12183. sync_vmcs12(vcpu, vmcs12);
  12184. else
  12185. prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
  12186. exit_qualification);
  12187. /*
  12188. * Must happen outside of sync_vmcs12() as it will
  12189. * also be used to capture vmcs12 cache as part of
  12190. * capturing nVMX state for snapshot (migration).
  12191. *
  12192. * Otherwise, this flush will dirty guest memory at a
  12193. * point it is already assumed by user-space to be
  12194. * immutable.
  12195. */
  12196. nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
  12197. if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
  12198. vmcs12->vm_exit_msr_store_count))
  12199. nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
  12200. } else {
  12201. /*
  12202. * The only expected VM-instruction error is "VM entry with
  12203. * invalid control field(s)." Anything else indicates a
  12204. * problem with L0. And we should never get here with a
  12205. * VMFail of any type if early consistency checks are enabled.
  12206. */
  12207. WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) !=
  12208. VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  12209. WARN_ON_ONCE(nested_early_check);
  12210. }
  12211. vmx_switch_vmcs(vcpu, &vmx->vmcs01);
  12212. /* Update any VMCS fields that might have changed while L2 ran */
  12213. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
  12214. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
  12215. vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
  12216. if (kvm_has_tsc_control)
  12217. decache_tsc_multiplier(vmx);
  12218. if (vmx->nested.change_vmcs01_virtual_apic_mode) {
  12219. vmx->nested.change_vmcs01_virtual_apic_mode = false;
  12220. vmx_set_virtual_apic_mode(vcpu);
  12221. } else if (!nested_cpu_has_ept(vmcs12) &&
  12222. nested_cpu_has2(vmcs12,
  12223. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
  12224. vmx_flush_tlb(vcpu, true);
  12225. }
  12226. /* This is needed for same reason as it was needed in prepare_vmcs02 */
  12227. vmx->host_rsp = 0;
  12228. /* Unpin physical memory we referred to in vmcs02 */
  12229. if (vmx->nested.apic_access_page) {
  12230. kvm_release_page_dirty(vmx->nested.apic_access_page);
  12231. vmx->nested.apic_access_page = NULL;
  12232. }
  12233. if (vmx->nested.virtual_apic_page) {
  12234. kvm_release_page_dirty(vmx->nested.virtual_apic_page);
  12235. vmx->nested.virtual_apic_page = NULL;
  12236. }
  12237. if (vmx->nested.pi_desc_page) {
  12238. kunmap(vmx->nested.pi_desc_page);
  12239. kvm_release_page_dirty(vmx->nested.pi_desc_page);
  12240. vmx->nested.pi_desc_page = NULL;
  12241. vmx->nested.pi_desc = NULL;
  12242. }
  12243. /*
  12244. * We are now running in L2, mmu_notifier will force to reload the
  12245. * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
  12246. */
  12247. kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
  12248. if ((exit_reason != -1) && (enable_shadow_vmcs || vmx->nested.hv_evmcs))
  12249. vmx->nested.need_vmcs12_sync = true;
  12250. /* in case we halted in L2 */
  12251. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  12252. if (likely(!vmx->fail)) {
  12253. /*
  12254. * TODO: SDM says that with acknowledge interrupt on
  12255. * exit, bit 31 of the VM-exit interrupt information
  12256. * (valid interrupt) is always set to 1 on
  12257. * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
  12258. * need kvm_cpu_has_interrupt(). See the commit
  12259. * message for details.
  12260. */
  12261. if (nested_exit_intr_ack_set(vcpu) &&
  12262. exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
  12263. kvm_cpu_has_interrupt(vcpu)) {
  12264. int irq = kvm_cpu_get_interrupt(vcpu);
  12265. WARN_ON(irq < 0);
  12266. vmcs12->vm_exit_intr_info = irq |
  12267. INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
  12268. }
  12269. if (exit_reason != -1)
  12270. trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
  12271. vmcs12->exit_qualification,
  12272. vmcs12->idt_vectoring_info_field,
  12273. vmcs12->vm_exit_intr_info,
  12274. vmcs12->vm_exit_intr_error_code,
  12275. KVM_ISA_VMX);
  12276. load_vmcs12_host_state(vcpu, vmcs12);
  12277. return;
  12278. }
  12279. /*
  12280. * After an early L2 VM-entry failure, we're now back
  12281. * in L1 which thinks it just finished a VMLAUNCH or
  12282. * VMRESUME instruction, so we need to set the failure
  12283. * flag and the VM-instruction error field of the VMCS
  12284. * accordingly, and skip the emulated instruction.
  12285. */
  12286. (void)nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  12287. /*
  12288. * Restore L1's host state to KVM's software model. We're here
  12289. * because a consistency check was caught by hardware, which
  12290. * means some amount of guest state has been propagated to KVM's
  12291. * model and needs to be unwound to the host's state.
  12292. */
  12293. nested_vmx_restore_host_state(vcpu);
  12294. vmx->fail = 0;
  12295. }
  12296. /*
  12297. * Forcibly leave nested mode in order to be able to reset the VCPU later on.
  12298. */
  12299. static void vmx_leave_nested(struct kvm_vcpu *vcpu)
  12300. {
  12301. if (is_guest_mode(vcpu)) {
  12302. to_vmx(vcpu)->nested.nested_run_pending = 0;
  12303. nested_vmx_vmexit(vcpu, -1, 0, 0);
  12304. }
  12305. free_nested(vcpu);
  12306. }
  12307. static int vmx_check_intercept(struct kvm_vcpu *vcpu,
  12308. struct x86_instruction_info *info,
  12309. enum x86_intercept_stage stage)
  12310. {
  12311. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  12312. struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
  12313. /*
  12314. * RDPID causes #UD if disabled through secondary execution controls.
  12315. * Because it is marked as EmulateOnUD, we need to intercept it here.
  12316. */
  12317. if (info->intercept == x86_intercept_rdtscp &&
  12318. !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
  12319. ctxt->exception.vector = UD_VECTOR;
  12320. ctxt->exception.error_code_valid = false;
  12321. return X86EMUL_PROPAGATE_FAULT;
  12322. }
  12323. /* TODO: check more intercepts... */
  12324. return X86EMUL_CONTINUE;
  12325. }
  12326. #ifdef CONFIG_X86_64
  12327. /* (a << shift) / divisor, return 1 if overflow otherwise 0 */
  12328. static inline int u64_shl_div_u64(u64 a, unsigned int shift,
  12329. u64 divisor, u64 *result)
  12330. {
  12331. u64 low = a << shift, high = a >> (64 - shift);
  12332. /* To avoid the overflow on divq */
  12333. if (high >= divisor)
  12334. return 1;
  12335. /* Low hold the result, high hold rem which is discarded */
  12336. asm("divq %2\n\t" : "=a" (low), "=d" (high) :
  12337. "rm" (divisor), "0" (low), "1" (high));
  12338. *result = low;
  12339. return 0;
  12340. }
  12341. static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
  12342. {
  12343. struct vcpu_vmx *vmx;
  12344. u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
  12345. if (kvm_mwait_in_guest(vcpu->kvm))
  12346. return -EOPNOTSUPP;
  12347. vmx = to_vmx(vcpu);
  12348. tscl = rdtsc();
  12349. guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
  12350. delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
  12351. lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
  12352. if (delta_tsc > lapic_timer_advance_cycles)
  12353. delta_tsc -= lapic_timer_advance_cycles;
  12354. else
  12355. delta_tsc = 0;
  12356. /* Convert to host delta tsc if tsc scaling is enabled */
  12357. if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
  12358. u64_shl_div_u64(delta_tsc,
  12359. kvm_tsc_scaling_ratio_frac_bits,
  12360. vcpu->arch.tsc_scaling_ratio,
  12361. &delta_tsc))
  12362. return -ERANGE;
  12363. /*
  12364. * If the delta tsc can't fit in the 32 bit after the multi shift,
  12365. * we can't use the preemption timer.
  12366. * It's possible that it fits on later vmentries, but checking
  12367. * on every vmentry is costly so we just use an hrtimer.
  12368. */
  12369. if (delta_tsc >> (cpu_preemption_timer_multi + 32))
  12370. return -ERANGE;
  12371. vmx->hv_deadline_tsc = tscl + delta_tsc;
  12372. return delta_tsc == 0;
  12373. }
  12374. static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
  12375. {
  12376. to_vmx(vcpu)->hv_deadline_tsc = -1;
  12377. }
  12378. #endif
  12379. static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
  12380. {
  12381. if (!kvm_pause_in_guest(vcpu->kvm))
  12382. shrink_ple_window(vcpu);
  12383. }
  12384. static void vmx_slot_enable_log_dirty(struct kvm *kvm,
  12385. struct kvm_memory_slot *slot)
  12386. {
  12387. kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
  12388. kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
  12389. }
  12390. static void vmx_slot_disable_log_dirty(struct kvm *kvm,
  12391. struct kvm_memory_slot *slot)
  12392. {
  12393. kvm_mmu_slot_set_dirty(kvm, slot);
  12394. }
  12395. static void vmx_flush_log_dirty(struct kvm *kvm)
  12396. {
  12397. kvm_flush_pml_buffers(kvm);
  12398. }
  12399. static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
  12400. {
  12401. struct vmcs12 *vmcs12;
  12402. struct vcpu_vmx *vmx = to_vmx(vcpu);
  12403. gpa_t gpa;
  12404. struct page *page = NULL;
  12405. u64 *pml_address;
  12406. if (is_guest_mode(vcpu)) {
  12407. WARN_ON_ONCE(vmx->nested.pml_full);
  12408. /*
  12409. * Check if PML is enabled for the nested guest.
  12410. * Whether eptp bit 6 is set is already checked
  12411. * as part of A/D emulation.
  12412. */
  12413. vmcs12 = get_vmcs12(vcpu);
  12414. if (!nested_cpu_has_pml(vmcs12))
  12415. return 0;
  12416. if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
  12417. vmx->nested.pml_full = true;
  12418. return 1;
  12419. }
  12420. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
  12421. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
  12422. if (is_error_page(page))
  12423. return 0;
  12424. pml_address = kmap(page);
  12425. pml_address[vmcs12->guest_pml_index--] = gpa;
  12426. kunmap(page);
  12427. kvm_release_page_clean(page);
  12428. }
  12429. return 0;
  12430. }
  12431. static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
  12432. struct kvm_memory_slot *memslot,
  12433. gfn_t offset, unsigned long mask)
  12434. {
  12435. kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
  12436. }
  12437. static void __pi_post_block(struct kvm_vcpu *vcpu)
  12438. {
  12439. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  12440. struct pi_desc old, new;
  12441. unsigned int dest;
  12442. do {
  12443. old.control = new.control = pi_desc->control;
  12444. WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
  12445. "Wakeup handler not enabled while the VCPU is blocked\n");
  12446. dest = cpu_physical_id(vcpu->cpu);
  12447. if (x2apic_enabled())
  12448. new.ndst = dest;
  12449. else
  12450. new.ndst = (dest << 8) & 0xFF00;
  12451. /* set 'NV' to 'notification vector' */
  12452. new.nv = POSTED_INTR_VECTOR;
  12453. } while (cmpxchg64(&pi_desc->control, old.control,
  12454. new.control) != old.control);
  12455. if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
  12456. spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
  12457. list_del(&vcpu->blocked_vcpu_list);
  12458. spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
  12459. vcpu->pre_pcpu = -1;
  12460. }
  12461. }
  12462. /*
  12463. * This routine does the following things for vCPU which is going
  12464. * to be blocked if VT-d PI is enabled.
  12465. * - Store the vCPU to the wakeup list, so when interrupts happen
  12466. * we can find the right vCPU to wake up.
  12467. * - Change the Posted-interrupt descriptor as below:
  12468. * 'NDST' <-- vcpu->pre_pcpu
  12469. * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
  12470. * - If 'ON' is set during this process, which means at least one
  12471. * interrupt is posted for this vCPU, we cannot block it, in
  12472. * this case, return 1, otherwise, return 0.
  12473. *
  12474. */
  12475. static int pi_pre_block(struct kvm_vcpu *vcpu)
  12476. {
  12477. unsigned int dest;
  12478. struct pi_desc old, new;
  12479. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  12480. if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
  12481. !irq_remapping_cap(IRQ_POSTING_CAP) ||
  12482. !kvm_vcpu_apicv_active(vcpu))
  12483. return 0;
  12484. WARN_ON(irqs_disabled());
  12485. local_irq_disable();
  12486. if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
  12487. vcpu->pre_pcpu = vcpu->cpu;
  12488. spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
  12489. list_add_tail(&vcpu->blocked_vcpu_list,
  12490. &per_cpu(blocked_vcpu_on_cpu,
  12491. vcpu->pre_pcpu));
  12492. spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
  12493. }
  12494. do {
  12495. old.control = new.control = pi_desc->control;
  12496. WARN((pi_desc->sn == 1),
  12497. "Warning: SN field of posted-interrupts "
  12498. "is set before blocking\n");
  12499. /*
  12500. * Since vCPU can be preempted during this process,
  12501. * vcpu->cpu could be different with pre_pcpu, we
  12502. * need to set pre_pcpu as the destination of wakeup
  12503. * notification event, then we can find the right vCPU
  12504. * to wakeup in wakeup handler if interrupts happen
  12505. * when the vCPU is in blocked state.
  12506. */
  12507. dest = cpu_physical_id(vcpu->pre_pcpu);
  12508. if (x2apic_enabled())
  12509. new.ndst = dest;
  12510. else
  12511. new.ndst = (dest << 8) & 0xFF00;
  12512. /* set 'NV' to 'wakeup vector' */
  12513. new.nv = POSTED_INTR_WAKEUP_VECTOR;
  12514. } while (cmpxchg64(&pi_desc->control, old.control,
  12515. new.control) != old.control);
  12516. /* We should not block the vCPU if an interrupt is posted for it. */
  12517. if (pi_test_on(pi_desc) == 1)
  12518. __pi_post_block(vcpu);
  12519. local_irq_enable();
  12520. return (vcpu->pre_pcpu == -1);
  12521. }
  12522. static int vmx_pre_block(struct kvm_vcpu *vcpu)
  12523. {
  12524. if (pi_pre_block(vcpu))
  12525. return 1;
  12526. if (kvm_lapic_hv_timer_in_use(vcpu))
  12527. kvm_lapic_switch_to_sw_timer(vcpu);
  12528. return 0;
  12529. }
  12530. static void pi_post_block(struct kvm_vcpu *vcpu)
  12531. {
  12532. if (vcpu->pre_pcpu == -1)
  12533. return;
  12534. WARN_ON(irqs_disabled());
  12535. local_irq_disable();
  12536. __pi_post_block(vcpu);
  12537. local_irq_enable();
  12538. }
  12539. static void vmx_post_block(struct kvm_vcpu *vcpu)
  12540. {
  12541. if (kvm_x86_ops->set_hv_timer)
  12542. kvm_lapic_switch_to_hv_timer(vcpu);
  12543. pi_post_block(vcpu);
  12544. }
  12545. /*
  12546. * vmx_update_pi_irte - set IRTE for Posted-Interrupts
  12547. *
  12548. * @kvm: kvm
  12549. * @host_irq: host irq of the interrupt
  12550. * @guest_irq: gsi of the interrupt
  12551. * @set: set or unset PI
  12552. * returns 0 on success, < 0 on failure
  12553. */
  12554. static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
  12555. uint32_t guest_irq, bool set)
  12556. {
  12557. struct kvm_kernel_irq_routing_entry *e;
  12558. struct kvm_irq_routing_table *irq_rt;
  12559. struct kvm_lapic_irq irq;
  12560. struct kvm_vcpu *vcpu;
  12561. struct vcpu_data vcpu_info;
  12562. int idx, ret = 0;
  12563. if (!kvm_arch_has_assigned_device(kvm) ||
  12564. !irq_remapping_cap(IRQ_POSTING_CAP) ||
  12565. !kvm_vcpu_apicv_active(kvm->vcpus[0]))
  12566. return 0;
  12567. idx = srcu_read_lock(&kvm->irq_srcu);
  12568. irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
  12569. if (guest_irq >= irq_rt->nr_rt_entries ||
  12570. hlist_empty(&irq_rt->map[guest_irq])) {
  12571. pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
  12572. guest_irq, irq_rt->nr_rt_entries);
  12573. goto out;
  12574. }
  12575. hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
  12576. if (e->type != KVM_IRQ_ROUTING_MSI)
  12577. continue;
  12578. /*
  12579. * VT-d PI cannot support posting multicast/broadcast
  12580. * interrupts to a vCPU, we still use interrupt remapping
  12581. * for these kind of interrupts.
  12582. *
  12583. * For lowest-priority interrupts, we only support
  12584. * those with single CPU as the destination, e.g. user
  12585. * configures the interrupts via /proc/irq or uses
  12586. * irqbalance to make the interrupts single-CPU.
  12587. *
  12588. * We will support full lowest-priority interrupt later.
  12589. */
  12590. kvm_set_msi_irq(kvm, e, &irq);
  12591. if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
  12592. /*
  12593. * Make sure the IRTE is in remapped mode if
  12594. * we don't handle it in posted mode.
  12595. */
  12596. ret = irq_set_vcpu_affinity(host_irq, NULL);
  12597. if (ret < 0) {
  12598. printk(KERN_INFO
  12599. "failed to back to remapped mode, irq: %u\n",
  12600. host_irq);
  12601. goto out;
  12602. }
  12603. continue;
  12604. }
  12605. vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
  12606. vcpu_info.vector = irq.vector;
  12607. trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
  12608. vcpu_info.vector, vcpu_info.pi_desc_addr, set);
  12609. if (set)
  12610. ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
  12611. else
  12612. ret = irq_set_vcpu_affinity(host_irq, NULL);
  12613. if (ret < 0) {
  12614. printk(KERN_INFO "%s: failed to update PI IRTE\n",
  12615. __func__);
  12616. goto out;
  12617. }
  12618. }
  12619. ret = 0;
  12620. out:
  12621. srcu_read_unlock(&kvm->irq_srcu, idx);
  12622. return ret;
  12623. }
  12624. static void vmx_setup_mce(struct kvm_vcpu *vcpu)
  12625. {
  12626. if (vcpu->arch.mcg_cap & MCG_LMCE_P)
  12627. to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
  12628. FEATURE_CONTROL_LMCE;
  12629. else
  12630. to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
  12631. ~FEATURE_CONTROL_LMCE;
  12632. }
  12633. static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
  12634. {
  12635. /* we need a nested vmexit to enter SMM, postpone if run is pending */
  12636. if (to_vmx(vcpu)->nested.nested_run_pending)
  12637. return 0;
  12638. return 1;
  12639. }
  12640. static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
  12641. {
  12642. struct vcpu_vmx *vmx = to_vmx(vcpu);
  12643. vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
  12644. if (vmx->nested.smm.guest_mode)
  12645. nested_vmx_vmexit(vcpu, -1, 0, 0);
  12646. vmx->nested.smm.vmxon = vmx->nested.vmxon;
  12647. vmx->nested.vmxon = false;
  12648. vmx_clear_hlt(vcpu);
  12649. return 0;
  12650. }
  12651. static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
  12652. {
  12653. struct vcpu_vmx *vmx = to_vmx(vcpu);
  12654. int ret;
  12655. if (vmx->nested.smm.vmxon) {
  12656. vmx->nested.vmxon = true;
  12657. vmx->nested.smm.vmxon = false;
  12658. }
  12659. if (vmx->nested.smm.guest_mode) {
  12660. vcpu->arch.hflags &= ~HF_SMM_MASK;
  12661. ret = nested_vmx_enter_non_root_mode(vcpu, false);
  12662. vcpu->arch.hflags |= HF_SMM_MASK;
  12663. if (ret)
  12664. return ret;
  12665. vmx->nested.smm.guest_mode = false;
  12666. }
  12667. return 0;
  12668. }
  12669. static int enable_smi_window(struct kvm_vcpu *vcpu)
  12670. {
  12671. return 0;
  12672. }
  12673. static inline int vmx_has_valid_vmcs12(struct kvm_vcpu *vcpu)
  12674. {
  12675. struct vcpu_vmx *vmx = to_vmx(vcpu);
  12676. /*
  12677. * In case we do two consecutive get/set_nested_state()s while L2 was
  12678. * running hv_evmcs may end up not being mapped (we map it from
  12679. * nested_vmx_run()/vmx_vcpu_run()). Check is_guest_mode() as we always
  12680. * have vmcs12 if it is true.
  12681. */
  12682. return is_guest_mode(vcpu) || vmx->nested.current_vmptr != -1ull ||
  12683. vmx->nested.hv_evmcs;
  12684. }
  12685. static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
  12686. struct kvm_nested_state __user *user_kvm_nested_state,
  12687. u32 user_data_size)
  12688. {
  12689. struct vcpu_vmx *vmx;
  12690. struct vmcs12 *vmcs12;
  12691. struct kvm_nested_state kvm_state = {
  12692. .flags = 0,
  12693. .format = 0,
  12694. .size = sizeof(kvm_state),
  12695. .vmx.vmxon_pa = -1ull,
  12696. .vmx.vmcs_pa = -1ull,
  12697. };
  12698. if (!vcpu)
  12699. return kvm_state.size + 2 * VMCS12_SIZE;
  12700. vmx = to_vmx(vcpu);
  12701. vmcs12 = get_vmcs12(vcpu);
  12702. if (nested_vmx_allowed(vcpu) && vmx->nested.enlightened_vmcs_enabled)
  12703. kvm_state.flags |= KVM_STATE_NESTED_EVMCS;
  12704. if (nested_vmx_allowed(vcpu) &&
  12705. (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
  12706. kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
  12707. kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
  12708. if (vmx_has_valid_vmcs12(vcpu)) {
  12709. kvm_state.size += VMCS12_SIZE;
  12710. if (is_guest_mode(vcpu) &&
  12711. nested_cpu_has_shadow_vmcs(vmcs12) &&
  12712. vmcs12->vmcs_link_pointer != -1ull)
  12713. kvm_state.size += VMCS12_SIZE;
  12714. }
  12715. if (vmx->nested.smm.vmxon)
  12716. kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
  12717. if (vmx->nested.smm.guest_mode)
  12718. kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
  12719. if (is_guest_mode(vcpu)) {
  12720. kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
  12721. if (vmx->nested.nested_run_pending)
  12722. kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
  12723. }
  12724. }
  12725. if (user_data_size < kvm_state.size)
  12726. goto out;
  12727. if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
  12728. return -EFAULT;
  12729. if (!vmx_has_valid_vmcs12(vcpu))
  12730. goto out;
  12731. /*
  12732. * When running L2, the authoritative vmcs12 state is in the
  12733. * vmcs02. When running L1, the authoritative vmcs12 state is
  12734. * in the shadow or enlightened vmcs linked to vmcs01, unless
  12735. * need_vmcs12_sync is set, in which case, the authoritative
  12736. * vmcs12 state is in the vmcs12 already.
  12737. */
  12738. if (is_guest_mode(vcpu)) {
  12739. sync_vmcs12(vcpu, vmcs12);
  12740. } else if (!vmx->nested.need_vmcs12_sync) {
  12741. if (vmx->nested.hv_evmcs)
  12742. copy_enlightened_to_vmcs12(vmx);
  12743. else if (enable_shadow_vmcs)
  12744. copy_shadow_to_vmcs12(vmx);
  12745. }
  12746. if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12)))
  12747. return -EFAULT;
  12748. if (nested_cpu_has_shadow_vmcs(vmcs12) &&
  12749. vmcs12->vmcs_link_pointer != -1ull) {
  12750. if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
  12751. get_shadow_vmcs12(vcpu), sizeof(*vmcs12)))
  12752. return -EFAULT;
  12753. }
  12754. out:
  12755. return kvm_state.size;
  12756. }
  12757. static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
  12758. struct kvm_nested_state __user *user_kvm_nested_state,
  12759. struct kvm_nested_state *kvm_state)
  12760. {
  12761. struct vcpu_vmx *vmx = to_vmx(vcpu);
  12762. struct vmcs12 *vmcs12;
  12763. u32 exit_qual;
  12764. int ret;
  12765. if (kvm_state->format != 0)
  12766. return -EINVAL;
  12767. if (kvm_state->flags & KVM_STATE_NESTED_EVMCS)
  12768. nested_enable_evmcs(vcpu, NULL);
  12769. if (!nested_vmx_allowed(vcpu))
  12770. return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
  12771. if (kvm_state->vmx.vmxon_pa == -1ull) {
  12772. if (kvm_state->vmx.smm.flags)
  12773. return -EINVAL;
  12774. if (kvm_state->vmx.vmcs_pa != -1ull)
  12775. return -EINVAL;
  12776. vmx_leave_nested(vcpu);
  12777. return 0;
  12778. }
  12779. if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
  12780. return -EINVAL;
  12781. if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
  12782. (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
  12783. return -EINVAL;
  12784. if (kvm_state->vmx.smm.flags &
  12785. ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
  12786. return -EINVAL;
  12787. /*
  12788. * SMM temporarily disables VMX, so we cannot be in guest mode,
  12789. * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags
  12790. * must be zero.
  12791. */
  12792. if (is_smm(vcpu) ? kvm_state->flags : kvm_state->vmx.smm.flags)
  12793. return -EINVAL;
  12794. if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
  12795. !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
  12796. return -EINVAL;
  12797. vmx_leave_nested(vcpu);
  12798. if (kvm_state->vmx.vmxon_pa == -1ull)
  12799. return 0;
  12800. vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
  12801. ret = enter_vmx_operation(vcpu);
  12802. if (ret)
  12803. return ret;
  12804. /* Empty 'VMXON' state is permitted */
  12805. if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
  12806. return 0;
  12807. if (kvm_state->vmx.vmcs_pa != -1ull) {
  12808. if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
  12809. !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
  12810. return -EINVAL;
  12811. set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
  12812. } else if (kvm_state->flags & KVM_STATE_NESTED_EVMCS) {
  12813. /*
  12814. * Sync eVMCS upon entry as we may not have
  12815. * HV_X64_MSR_VP_ASSIST_PAGE set up yet.
  12816. */
  12817. vmx->nested.need_vmcs12_sync = true;
  12818. } else {
  12819. return -EINVAL;
  12820. }
  12821. if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
  12822. vmx->nested.smm.vmxon = true;
  12823. vmx->nested.vmxon = false;
  12824. if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
  12825. vmx->nested.smm.guest_mode = true;
  12826. }
  12827. vmcs12 = get_vmcs12(vcpu);
  12828. if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
  12829. return -EFAULT;
  12830. if (vmcs12->hdr.revision_id != VMCS12_REVISION)
  12831. return -EINVAL;
  12832. if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
  12833. return 0;
  12834. vmx->nested.nested_run_pending =
  12835. !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
  12836. if (nested_cpu_has_shadow_vmcs(vmcs12) &&
  12837. vmcs12->vmcs_link_pointer != -1ull) {
  12838. struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
  12839. if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12))
  12840. return -EINVAL;
  12841. if (copy_from_user(shadow_vmcs12,
  12842. user_kvm_nested_state->data + VMCS12_SIZE,
  12843. sizeof(*vmcs12)))
  12844. return -EFAULT;
  12845. if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
  12846. !shadow_vmcs12->hdr.shadow_vmcs)
  12847. return -EINVAL;
  12848. }
  12849. if (check_vmentry_prereqs(vcpu, vmcs12) ||
  12850. check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
  12851. return -EINVAL;
  12852. vmx->nested.dirty_vmcs12 = true;
  12853. ret = nested_vmx_enter_non_root_mode(vcpu, false);
  12854. if (ret)
  12855. return -EINVAL;
  12856. return 0;
  12857. }
  12858. static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
  12859. .cpu_has_kvm_support = cpu_has_kvm_support,
  12860. .disabled_by_bios = vmx_disabled_by_bios,
  12861. .hardware_setup = hardware_setup,
  12862. .hardware_unsetup = hardware_unsetup,
  12863. .check_processor_compatibility = vmx_check_processor_compat,
  12864. .hardware_enable = hardware_enable,
  12865. .hardware_disable = hardware_disable,
  12866. .cpu_has_accelerated_tpr = report_flexpriority,
  12867. .has_emulated_msr = vmx_has_emulated_msr,
  12868. .vm_init = vmx_vm_init,
  12869. .vm_alloc = vmx_vm_alloc,
  12870. .vm_free = vmx_vm_free,
  12871. .vcpu_create = vmx_create_vcpu,
  12872. .vcpu_free = vmx_free_vcpu,
  12873. .vcpu_reset = vmx_vcpu_reset,
  12874. .prepare_guest_switch = vmx_prepare_switch_to_guest,
  12875. .vcpu_load = vmx_vcpu_load,
  12876. .vcpu_put = vmx_vcpu_put,
  12877. .update_bp_intercept = update_exception_bitmap,
  12878. .get_msr_feature = vmx_get_msr_feature,
  12879. .get_msr = vmx_get_msr,
  12880. .set_msr = vmx_set_msr,
  12881. .get_segment_base = vmx_get_segment_base,
  12882. .get_segment = vmx_get_segment,
  12883. .set_segment = vmx_set_segment,
  12884. .get_cpl = vmx_get_cpl,
  12885. .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
  12886. .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
  12887. .decache_cr3 = vmx_decache_cr3,
  12888. .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
  12889. .set_cr0 = vmx_set_cr0,
  12890. .set_cr3 = vmx_set_cr3,
  12891. .set_cr4 = vmx_set_cr4,
  12892. .set_efer = vmx_set_efer,
  12893. .get_idt = vmx_get_idt,
  12894. .set_idt = vmx_set_idt,
  12895. .get_gdt = vmx_get_gdt,
  12896. .set_gdt = vmx_set_gdt,
  12897. .get_dr6 = vmx_get_dr6,
  12898. .set_dr6 = vmx_set_dr6,
  12899. .set_dr7 = vmx_set_dr7,
  12900. .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
  12901. .cache_reg = vmx_cache_reg,
  12902. .get_rflags = vmx_get_rflags,
  12903. .set_rflags = vmx_set_rflags,
  12904. .tlb_flush = vmx_flush_tlb,
  12905. .tlb_flush_gva = vmx_flush_tlb_gva,
  12906. .run = vmx_vcpu_run,
  12907. .handle_exit = vmx_handle_exit,
  12908. .skip_emulated_instruction = skip_emulated_instruction,
  12909. .set_interrupt_shadow = vmx_set_interrupt_shadow,
  12910. .get_interrupt_shadow = vmx_get_interrupt_shadow,
  12911. .patch_hypercall = vmx_patch_hypercall,
  12912. .set_irq = vmx_inject_irq,
  12913. .set_nmi = vmx_inject_nmi,
  12914. .queue_exception = vmx_queue_exception,
  12915. .cancel_injection = vmx_cancel_injection,
  12916. .interrupt_allowed = vmx_interrupt_allowed,
  12917. .nmi_allowed = vmx_nmi_allowed,
  12918. .get_nmi_mask = vmx_get_nmi_mask,
  12919. .set_nmi_mask = vmx_set_nmi_mask,
  12920. .enable_nmi_window = enable_nmi_window,
  12921. .enable_irq_window = enable_irq_window,
  12922. .update_cr8_intercept = update_cr8_intercept,
  12923. .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
  12924. .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
  12925. .get_enable_apicv = vmx_get_enable_apicv,
  12926. .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
  12927. .load_eoi_exitmap = vmx_load_eoi_exitmap,
  12928. .apicv_post_state_restore = vmx_apicv_post_state_restore,
  12929. .hwapic_irr_update = vmx_hwapic_irr_update,
  12930. .hwapic_isr_update = vmx_hwapic_isr_update,
  12931. .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt,
  12932. .sync_pir_to_irr = vmx_sync_pir_to_irr,
  12933. .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
  12934. .set_tss_addr = vmx_set_tss_addr,
  12935. .set_identity_map_addr = vmx_set_identity_map_addr,
  12936. .get_tdp_level = get_ept_level,
  12937. .get_mt_mask = vmx_get_mt_mask,
  12938. .get_exit_info = vmx_get_exit_info,
  12939. .get_lpage_level = vmx_get_lpage_level,
  12940. .cpuid_update = vmx_cpuid_update,
  12941. .rdtscp_supported = vmx_rdtscp_supported,
  12942. .invpcid_supported = vmx_invpcid_supported,
  12943. .set_supported_cpuid = vmx_set_supported_cpuid,
  12944. .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
  12945. .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
  12946. .write_tsc_offset = vmx_write_tsc_offset,
  12947. .set_tdp_cr3 = vmx_set_cr3,
  12948. .check_intercept = vmx_check_intercept,
  12949. .handle_external_intr = vmx_handle_external_intr,
  12950. .mpx_supported = vmx_mpx_supported,
  12951. .xsaves_supported = vmx_xsaves_supported,
  12952. .umip_emulated = vmx_umip_emulated,
  12953. .check_nested_events = vmx_check_nested_events,
  12954. .request_immediate_exit = vmx_request_immediate_exit,
  12955. .sched_in = vmx_sched_in,
  12956. .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
  12957. .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
  12958. .flush_log_dirty = vmx_flush_log_dirty,
  12959. .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
  12960. .write_log_dirty = vmx_write_pml_buffer,
  12961. .pre_block = vmx_pre_block,
  12962. .post_block = vmx_post_block,
  12963. .pmu_ops = &intel_pmu_ops,
  12964. .update_pi_irte = vmx_update_pi_irte,
  12965. #ifdef CONFIG_X86_64
  12966. .set_hv_timer = vmx_set_hv_timer,
  12967. .cancel_hv_timer = vmx_cancel_hv_timer,
  12968. #endif
  12969. .setup_mce = vmx_setup_mce,
  12970. .get_nested_state = vmx_get_nested_state,
  12971. .set_nested_state = vmx_set_nested_state,
  12972. .get_vmcs12_pages = nested_get_vmcs12_pages,
  12973. .smi_allowed = vmx_smi_allowed,
  12974. .pre_enter_smm = vmx_pre_enter_smm,
  12975. .pre_leave_smm = vmx_pre_leave_smm,
  12976. .enable_smi_window = enable_smi_window,
  12977. .nested_enable_evmcs = nested_enable_evmcs,
  12978. };
  12979. static void vmx_cleanup_l1d_flush(void)
  12980. {
  12981. if (vmx_l1d_flush_pages) {
  12982. free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
  12983. vmx_l1d_flush_pages = NULL;
  12984. }
  12985. /* Restore state so sysfs ignores VMX */
  12986. l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
  12987. }
  12988. static void vmx_exit(void)
  12989. {
  12990. #ifdef CONFIG_KEXEC_CORE
  12991. RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
  12992. synchronize_rcu();
  12993. #endif
  12994. kvm_exit();
  12995. #if IS_ENABLED(CONFIG_HYPERV)
  12996. if (static_branch_unlikely(&enable_evmcs)) {
  12997. int cpu;
  12998. struct hv_vp_assist_page *vp_ap;
  12999. /*
  13000. * Reset everything to support using non-enlightened VMCS
  13001. * access later (e.g. when we reload the module with
  13002. * enlightened_vmcs=0)
  13003. */
  13004. for_each_online_cpu(cpu) {
  13005. vp_ap = hv_get_vp_assist_page(cpu);
  13006. if (!vp_ap)
  13007. continue;
  13008. vp_ap->current_nested_vmcs = 0;
  13009. vp_ap->enlighten_vmentry = 0;
  13010. }
  13011. static_branch_disable(&enable_evmcs);
  13012. }
  13013. #endif
  13014. vmx_cleanup_l1d_flush();
  13015. }
  13016. module_exit(vmx_exit);
  13017. static int __init vmx_init(void)
  13018. {
  13019. int r;
  13020. #if IS_ENABLED(CONFIG_HYPERV)
  13021. /*
  13022. * Enlightened VMCS usage should be recommended and the host needs
  13023. * to support eVMCS v1 or above. We can also disable eVMCS support
  13024. * with module parameter.
  13025. */
  13026. if (enlightened_vmcs &&
  13027. ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
  13028. (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
  13029. KVM_EVMCS_VERSION) {
  13030. int cpu;
  13031. /* Check that we have assist pages on all online CPUs */
  13032. for_each_online_cpu(cpu) {
  13033. if (!hv_get_vp_assist_page(cpu)) {
  13034. enlightened_vmcs = false;
  13035. break;
  13036. }
  13037. }
  13038. if (enlightened_vmcs) {
  13039. pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
  13040. static_branch_enable(&enable_evmcs);
  13041. }
  13042. } else {
  13043. enlightened_vmcs = false;
  13044. }
  13045. #endif
  13046. r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
  13047. __alignof__(struct vcpu_vmx), THIS_MODULE);
  13048. if (r)
  13049. return r;
  13050. /*
  13051. * Must be called after kvm_init() so enable_ept is properly set
  13052. * up. Hand the parameter mitigation value in which was stored in
  13053. * the pre module init parser. If no parameter was given, it will
  13054. * contain 'auto' which will be turned into the default 'cond'
  13055. * mitigation mode.
  13056. */
  13057. if (boot_cpu_has(X86_BUG_L1TF)) {
  13058. r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
  13059. if (r) {
  13060. vmx_exit();
  13061. return r;
  13062. }
  13063. }
  13064. #ifdef CONFIG_KEXEC_CORE
  13065. rcu_assign_pointer(crash_vmclear_loaded_vmcss,
  13066. crash_vmclear_local_loaded_vmcss);
  13067. #endif
  13068. vmx_check_vmcs12_offsets();
  13069. return 0;
  13070. }
  13071. module_init(vmx_init);