vmx.c 412 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653
  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 <linux/kvm_host.h>
  23. #include <linux/module.h>
  24. #include <linux/kernel.h>
  25. #include <linux/mm.h>
  26. #include <linux/highmem.h>
  27. #include <linux/sched.h>
  28. #include <linux/sched/smt.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 = 0;
  95. module_param(nested, bool, S_IRUGO);
  96. static u64 __read_mostly host_xss;
  97. static bool __read_mostly enable_pml = 1;
  98. module_param_named(pml, enable_pml, bool, S_IRUGO);
  99. #define MSR_TYPE_R 1
  100. #define MSR_TYPE_W 2
  101. #define MSR_TYPE_RW 3
  102. #define MSR_BITMAP_MODE_X2APIC 1
  103. #define MSR_BITMAP_MODE_X2APIC_APICV 2
  104. #define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
  105. /* Guest_tsc -> host_tsc conversion requires 64-bit division. */
  106. static int __read_mostly cpu_preemption_timer_multi;
  107. static bool __read_mostly enable_preemption_timer = 1;
  108. #ifdef CONFIG_X86_64
  109. module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
  110. #endif
  111. #define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
  112. #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
  113. #define KVM_VM_CR0_ALWAYS_ON \
  114. (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
  115. X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
  116. #define KVM_CR4_GUEST_OWNED_BITS \
  117. (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
  118. | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
  119. #define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
  120. #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
  121. #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
  122. #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
  123. #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
  124. /*
  125. * Hyper-V requires all of these, so mark them as supported even though
  126. * they are just treated the same as all-context.
  127. */
  128. #define VMX_VPID_EXTENT_SUPPORTED_MASK \
  129. (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
  130. VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
  131. VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
  132. VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
  133. /*
  134. * These 2 parameters are used to config the controls for Pause-Loop Exiting:
  135. * ple_gap: upper bound on the amount of time between two successive
  136. * executions of PAUSE in a loop. Also indicate if ple enabled.
  137. * According to test, this time is usually smaller than 128 cycles.
  138. * ple_window: upper bound on the amount of time a guest is allowed to execute
  139. * in a PAUSE loop. Tests indicate that most spinlocks are held for
  140. * less than 2^12 cycles
  141. * Time is measured based on a counter that runs at the same rate as the TSC,
  142. * refer SDM volume 3b section 21.6.13 & 22.1.3.
  143. */
  144. static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
  145. module_param(ple_gap, uint, 0444);
  146. static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
  147. module_param(ple_window, uint, 0444);
  148. /* Default doubles per-vcpu window every exit. */
  149. static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
  150. module_param(ple_window_grow, uint, 0444);
  151. /* Default resets per-vcpu window every exit to ple_window. */
  152. static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
  153. module_param(ple_window_shrink, uint, 0444);
  154. /* Default is to compute the maximum so we can never overflow. */
  155. static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
  156. module_param(ple_window_max, uint, 0444);
  157. extern const ulong vmx_return;
  158. static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
  159. static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
  160. static DEFINE_MUTEX(vmx_l1d_flush_mutex);
  161. /* Storage for pre module init parameter parsing */
  162. static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
  163. static const struct {
  164. const char *option;
  165. bool for_parse;
  166. } vmentry_l1d_param[] = {
  167. [VMENTER_L1D_FLUSH_AUTO] = {"auto", true},
  168. [VMENTER_L1D_FLUSH_NEVER] = {"never", true},
  169. [VMENTER_L1D_FLUSH_COND] = {"cond", true},
  170. [VMENTER_L1D_FLUSH_ALWAYS] = {"always", true},
  171. [VMENTER_L1D_FLUSH_EPT_DISABLED] = {"EPT disabled", false},
  172. [VMENTER_L1D_FLUSH_NOT_REQUIRED] = {"not required", false},
  173. };
  174. #define L1D_CACHE_ORDER 4
  175. static void *vmx_l1d_flush_pages;
  176. static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
  177. {
  178. struct page *page;
  179. unsigned int i;
  180. if (!enable_ept) {
  181. l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
  182. return 0;
  183. }
  184. if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) {
  185. u64 msr;
  186. rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr);
  187. if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) {
  188. l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
  189. return 0;
  190. }
  191. }
  192. /* If set to auto use the default l1tf mitigation method */
  193. if (l1tf == VMENTER_L1D_FLUSH_AUTO) {
  194. switch (l1tf_mitigation) {
  195. case L1TF_MITIGATION_OFF:
  196. l1tf = VMENTER_L1D_FLUSH_NEVER;
  197. break;
  198. case L1TF_MITIGATION_FLUSH_NOWARN:
  199. case L1TF_MITIGATION_FLUSH:
  200. case L1TF_MITIGATION_FLUSH_NOSMT:
  201. l1tf = VMENTER_L1D_FLUSH_COND;
  202. break;
  203. case L1TF_MITIGATION_FULL:
  204. case L1TF_MITIGATION_FULL_FORCE:
  205. l1tf = VMENTER_L1D_FLUSH_ALWAYS;
  206. break;
  207. }
  208. } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) {
  209. l1tf = VMENTER_L1D_FLUSH_ALWAYS;
  210. }
  211. if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
  212. !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
  213. page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
  214. if (!page)
  215. return -ENOMEM;
  216. vmx_l1d_flush_pages = page_address(page);
  217. /*
  218. * Initialize each page with a different pattern in
  219. * order to protect against KSM in the nested
  220. * virtualization case.
  221. */
  222. for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) {
  223. memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1,
  224. PAGE_SIZE);
  225. }
  226. }
  227. l1tf_vmx_mitigation = l1tf;
  228. if (l1tf != VMENTER_L1D_FLUSH_NEVER)
  229. static_branch_enable(&vmx_l1d_should_flush);
  230. else
  231. static_branch_disable(&vmx_l1d_should_flush);
  232. if (l1tf == VMENTER_L1D_FLUSH_COND)
  233. static_branch_enable(&vmx_l1d_flush_cond);
  234. else
  235. static_branch_disable(&vmx_l1d_flush_cond);
  236. return 0;
  237. }
  238. static int vmentry_l1d_flush_parse(const char *s)
  239. {
  240. unsigned int i;
  241. if (s) {
  242. for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
  243. if (vmentry_l1d_param[i].for_parse &&
  244. sysfs_streq(s, vmentry_l1d_param[i].option))
  245. return i;
  246. }
  247. }
  248. return -EINVAL;
  249. }
  250. static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
  251. {
  252. int l1tf, ret;
  253. l1tf = vmentry_l1d_flush_parse(s);
  254. if (l1tf < 0)
  255. return l1tf;
  256. if (!boot_cpu_has(X86_BUG_L1TF))
  257. return 0;
  258. /*
  259. * Has vmx_init() run already? If not then this is the pre init
  260. * parameter parsing. In that case just store the value and let
  261. * vmx_init() do the proper setup after enable_ept has been
  262. * established.
  263. */
  264. if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
  265. vmentry_l1d_flush_param = l1tf;
  266. return 0;
  267. }
  268. mutex_lock(&vmx_l1d_flush_mutex);
  269. ret = vmx_setup_l1d_flush(l1tf);
  270. mutex_unlock(&vmx_l1d_flush_mutex);
  271. return ret;
  272. }
  273. static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
  274. {
  275. if (WARN_ON_ONCE(l1tf_vmx_mitigation >= ARRAY_SIZE(vmentry_l1d_param)))
  276. return sprintf(s, "???\n");
  277. return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
  278. }
  279. static const struct kernel_param_ops vmentry_l1d_flush_ops = {
  280. .set = vmentry_l1d_flush_set,
  281. .get = vmentry_l1d_flush_get,
  282. };
  283. module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644);
  284. enum ept_pointers_status {
  285. EPT_POINTERS_CHECK = 0,
  286. EPT_POINTERS_MATCH = 1,
  287. EPT_POINTERS_MISMATCH = 2
  288. };
  289. struct kvm_vmx {
  290. struct kvm kvm;
  291. unsigned int tss_addr;
  292. bool ept_identity_pagetable_done;
  293. gpa_t ept_identity_map_addr;
  294. enum ept_pointers_status ept_pointers_match;
  295. spinlock_t ept_pointer_lock;
  296. };
  297. #define NR_AUTOLOAD_MSRS 8
  298. struct vmcs_hdr {
  299. u32 revision_id:31;
  300. u32 shadow_vmcs:1;
  301. };
  302. struct vmcs {
  303. struct vmcs_hdr hdr;
  304. u32 abort;
  305. char data[0];
  306. };
  307. /*
  308. * vmcs_host_state tracks registers that are loaded from the VMCS on VMEXIT
  309. * and whose values change infrequently, but are not constant. I.e. this is
  310. * used as a write-through cache of the corresponding VMCS fields.
  311. */
  312. struct vmcs_host_state {
  313. unsigned long cr3; /* May not match real cr3 */
  314. unsigned long cr4; /* May not match real cr4 */
  315. unsigned long gs_base;
  316. unsigned long fs_base;
  317. u16 fs_sel, gs_sel, ldt_sel;
  318. #ifdef CONFIG_X86_64
  319. u16 ds_sel, es_sel;
  320. #endif
  321. };
  322. /*
  323. * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
  324. * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
  325. * loaded on this CPU (so we can clear them if the CPU goes down).
  326. */
  327. struct loaded_vmcs {
  328. struct vmcs *vmcs;
  329. struct vmcs *shadow_vmcs;
  330. int cpu;
  331. bool launched;
  332. bool nmi_known_unmasked;
  333. bool hv_timer_armed;
  334. /* Support for vnmi-less CPUs */
  335. int soft_vnmi_blocked;
  336. ktime_t entry_time;
  337. s64 vnmi_blocked_time;
  338. unsigned long *msr_bitmap;
  339. struct list_head loaded_vmcss_on_cpu_link;
  340. struct vmcs_host_state host_state;
  341. };
  342. struct shared_msr_entry {
  343. unsigned index;
  344. u64 data;
  345. u64 mask;
  346. };
  347. /*
  348. * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
  349. * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
  350. * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
  351. * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
  352. * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
  353. * More than one of these structures may exist, if L1 runs multiple L2 guests.
  354. * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
  355. * underlying hardware which will be used to run L2.
  356. * This structure is packed to ensure that its layout is identical across
  357. * machines (necessary for live migration).
  358. *
  359. * IMPORTANT: Changing the layout of existing fields in this structure
  360. * will break save/restore compatibility with older kvm releases. When
  361. * adding new fields, either use space in the reserved padding* arrays
  362. * or add the new fields to the end of the structure.
  363. */
  364. typedef u64 natural_width;
  365. struct __packed vmcs12 {
  366. /* According to the Intel spec, a VMCS region must start with the
  367. * following two fields. Then follow implementation-specific data.
  368. */
  369. struct vmcs_hdr hdr;
  370. u32 abort;
  371. u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
  372. u32 padding[7]; /* room for future expansion */
  373. u64 io_bitmap_a;
  374. u64 io_bitmap_b;
  375. u64 msr_bitmap;
  376. u64 vm_exit_msr_store_addr;
  377. u64 vm_exit_msr_load_addr;
  378. u64 vm_entry_msr_load_addr;
  379. u64 tsc_offset;
  380. u64 virtual_apic_page_addr;
  381. u64 apic_access_addr;
  382. u64 posted_intr_desc_addr;
  383. u64 ept_pointer;
  384. u64 eoi_exit_bitmap0;
  385. u64 eoi_exit_bitmap1;
  386. u64 eoi_exit_bitmap2;
  387. u64 eoi_exit_bitmap3;
  388. u64 xss_exit_bitmap;
  389. u64 guest_physical_address;
  390. u64 vmcs_link_pointer;
  391. u64 guest_ia32_debugctl;
  392. u64 guest_ia32_pat;
  393. u64 guest_ia32_efer;
  394. u64 guest_ia32_perf_global_ctrl;
  395. u64 guest_pdptr0;
  396. u64 guest_pdptr1;
  397. u64 guest_pdptr2;
  398. u64 guest_pdptr3;
  399. u64 guest_bndcfgs;
  400. u64 host_ia32_pat;
  401. u64 host_ia32_efer;
  402. u64 host_ia32_perf_global_ctrl;
  403. u64 vmread_bitmap;
  404. u64 vmwrite_bitmap;
  405. u64 vm_function_control;
  406. u64 eptp_list_address;
  407. u64 pml_address;
  408. u64 padding64[3]; /* room for future expansion */
  409. /*
  410. * To allow migration of L1 (complete with its L2 guests) between
  411. * machines of different natural widths (32 or 64 bit), we cannot have
  412. * unsigned long fields with no explict size. We use u64 (aliased
  413. * natural_width) instead. Luckily, x86 is little-endian.
  414. */
  415. natural_width cr0_guest_host_mask;
  416. natural_width cr4_guest_host_mask;
  417. natural_width cr0_read_shadow;
  418. natural_width cr4_read_shadow;
  419. natural_width cr3_target_value0;
  420. natural_width cr3_target_value1;
  421. natural_width cr3_target_value2;
  422. natural_width cr3_target_value3;
  423. natural_width exit_qualification;
  424. natural_width guest_linear_address;
  425. natural_width guest_cr0;
  426. natural_width guest_cr3;
  427. natural_width guest_cr4;
  428. natural_width guest_es_base;
  429. natural_width guest_cs_base;
  430. natural_width guest_ss_base;
  431. natural_width guest_ds_base;
  432. natural_width guest_fs_base;
  433. natural_width guest_gs_base;
  434. natural_width guest_ldtr_base;
  435. natural_width guest_tr_base;
  436. natural_width guest_gdtr_base;
  437. natural_width guest_idtr_base;
  438. natural_width guest_dr7;
  439. natural_width guest_rsp;
  440. natural_width guest_rip;
  441. natural_width guest_rflags;
  442. natural_width guest_pending_dbg_exceptions;
  443. natural_width guest_sysenter_esp;
  444. natural_width guest_sysenter_eip;
  445. natural_width host_cr0;
  446. natural_width host_cr3;
  447. natural_width host_cr4;
  448. natural_width host_fs_base;
  449. natural_width host_gs_base;
  450. natural_width host_tr_base;
  451. natural_width host_gdtr_base;
  452. natural_width host_idtr_base;
  453. natural_width host_ia32_sysenter_esp;
  454. natural_width host_ia32_sysenter_eip;
  455. natural_width host_rsp;
  456. natural_width host_rip;
  457. natural_width paddingl[8]; /* room for future expansion */
  458. u32 pin_based_vm_exec_control;
  459. u32 cpu_based_vm_exec_control;
  460. u32 exception_bitmap;
  461. u32 page_fault_error_code_mask;
  462. u32 page_fault_error_code_match;
  463. u32 cr3_target_count;
  464. u32 vm_exit_controls;
  465. u32 vm_exit_msr_store_count;
  466. u32 vm_exit_msr_load_count;
  467. u32 vm_entry_controls;
  468. u32 vm_entry_msr_load_count;
  469. u32 vm_entry_intr_info_field;
  470. u32 vm_entry_exception_error_code;
  471. u32 vm_entry_instruction_len;
  472. u32 tpr_threshold;
  473. u32 secondary_vm_exec_control;
  474. u32 vm_instruction_error;
  475. u32 vm_exit_reason;
  476. u32 vm_exit_intr_info;
  477. u32 vm_exit_intr_error_code;
  478. u32 idt_vectoring_info_field;
  479. u32 idt_vectoring_error_code;
  480. u32 vm_exit_instruction_len;
  481. u32 vmx_instruction_info;
  482. u32 guest_es_limit;
  483. u32 guest_cs_limit;
  484. u32 guest_ss_limit;
  485. u32 guest_ds_limit;
  486. u32 guest_fs_limit;
  487. u32 guest_gs_limit;
  488. u32 guest_ldtr_limit;
  489. u32 guest_tr_limit;
  490. u32 guest_gdtr_limit;
  491. u32 guest_idtr_limit;
  492. u32 guest_es_ar_bytes;
  493. u32 guest_cs_ar_bytes;
  494. u32 guest_ss_ar_bytes;
  495. u32 guest_ds_ar_bytes;
  496. u32 guest_fs_ar_bytes;
  497. u32 guest_gs_ar_bytes;
  498. u32 guest_ldtr_ar_bytes;
  499. u32 guest_tr_ar_bytes;
  500. u32 guest_interruptibility_info;
  501. u32 guest_activity_state;
  502. u32 guest_sysenter_cs;
  503. u32 host_ia32_sysenter_cs;
  504. u32 vmx_preemption_timer_value;
  505. u32 padding32[7]; /* room for future expansion */
  506. u16 virtual_processor_id;
  507. u16 posted_intr_nv;
  508. u16 guest_es_selector;
  509. u16 guest_cs_selector;
  510. u16 guest_ss_selector;
  511. u16 guest_ds_selector;
  512. u16 guest_fs_selector;
  513. u16 guest_gs_selector;
  514. u16 guest_ldtr_selector;
  515. u16 guest_tr_selector;
  516. u16 guest_intr_status;
  517. u16 host_es_selector;
  518. u16 host_cs_selector;
  519. u16 host_ss_selector;
  520. u16 host_ds_selector;
  521. u16 host_fs_selector;
  522. u16 host_gs_selector;
  523. u16 host_tr_selector;
  524. u16 guest_pml_index;
  525. };
  526. /*
  527. * For save/restore compatibility, the vmcs12 field offsets must not change.
  528. */
  529. #define CHECK_OFFSET(field, loc) \
  530. BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \
  531. "Offset of " #field " in struct vmcs12 has changed.")
  532. static inline void vmx_check_vmcs12_offsets(void) {
  533. CHECK_OFFSET(hdr, 0);
  534. CHECK_OFFSET(abort, 4);
  535. CHECK_OFFSET(launch_state, 8);
  536. CHECK_OFFSET(io_bitmap_a, 40);
  537. CHECK_OFFSET(io_bitmap_b, 48);
  538. CHECK_OFFSET(msr_bitmap, 56);
  539. CHECK_OFFSET(vm_exit_msr_store_addr, 64);
  540. CHECK_OFFSET(vm_exit_msr_load_addr, 72);
  541. CHECK_OFFSET(vm_entry_msr_load_addr, 80);
  542. CHECK_OFFSET(tsc_offset, 88);
  543. CHECK_OFFSET(virtual_apic_page_addr, 96);
  544. CHECK_OFFSET(apic_access_addr, 104);
  545. CHECK_OFFSET(posted_intr_desc_addr, 112);
  546. CHECK_OFFSET(ept_pointer, 120);
  547. CHECK_OFFSET(eoi_exit_bitmap0, 128);
  548. CHECK_OFFSET(eoi_exit_bitmap1, 136);
  549. CHECK_OFFSET(eoi_exit_bitmap2, 144);
  550. CHECK_OFFSET(eoi_exit_bitmap3, 152);
  551. CHECK_OFFSET(xss_exit_bitmap, 160);
  552. CHECK_OFFSET(guest_physical_address, 168);
  553. CHECK_OFFSET(vmcs_link_pointer, 176);
  554. CHECK_OFFSET(guest_ia32_debugctl, 184);
  555. CHECK_OFFSET(guest_ia32_pat, 192);
  556. CHECK_OFFSET(guest_ia32_efer, 200);
  557. CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208);
  558. CHECK_OFFSET(guest_pdptr0, 216);
  559. CHECK_OFFSET(guest_pdptr1, 224);
  560. CHECK_OFFSET(guest_pdptr2, 232);
  561. CHECK_OFFSET(guest_pdptr3, 240);
  562. CHECK_OFFSET(guest_bndcfgs, 248);
  563. CHECK_OFFSET(host_ia32_pat, 256);
  564. CHECK_OFFSET(host_ia32_efer, 264);
  565. CHECK_OFFSET(host_ia32_perf_global_ctrl, 272);
  566. CHECK_OFFSET(vmread_bitmap, 280);
  567. CHECK_OFFSET(vmwrite_bitmap, 288);
  568. CHECK_OFFSET(vm_function_control, 296);
  569. CHECK_OFFSET(eptp_list_address, 304);
  570. CHECK_OFFSET(pml_address, 312);
  571. CHECK_OFFSET(cr0_guest_host_mask, 344);
  572. CHECK_OFFSET(cr4_guest_host_mask, 352);
  573. CHECK_OFFSET(cr0_read_shadow, 360);
  574. CHECK_OFFSET(cr4_read_shadow, 368);
  575. CHECK_OFFSET(cr3_target_value0, 376);
  576. CHECK_OFFSET(cr3_target_value1, 384);
  577. CHECK_OFFSET(cr3_target_value2, 392);
  578. CHECK_OFFSET(cr3_target_value3, 400);
  579. CHECK_OFFSET(exit_qualification, 408);
  580. CHECK_OFFSET(guest_linear_address, 416);
  581. CHECK_OFFSET(guest_cr0, 424);
  582. CHECK_OFFSET(guest_cr3, 432);
  583. CHECK_OFFSET(guest_cr4, 440);
  584. CHECK_OFFSET(guest_es_base, 448);
  585. CHECK_OFFSET(guest_cs_base, 456);
  586. CHECK_OFFSET(guest_ss_base, 464);
  587. CHECK_OFFSET(guest_ds_base, 472);
  588. CHECK_OFFSET(guest_fs_base, 480);
  589. CHECK_OFFSET(guest_gs_base, 488);
  590. CHECK_OFFSET(guest_ldtr_base, 496);
  591. CHECK_OFFSET(guest_tr_base, 504);
  592. CHECK_OFFSET(guest_gdtr_base, 512);
  593. CHECK_OFFSET(guest_idtr_base, 520);
  594. CHECK_OFFSET(guest_dr7, 528);
  595. CHECK_OFFSET(guest_rsp, 536);
  596. CHECK_OFFSET(guest_rip, 544);
  597. CHECK_OFFSET(guest_rflags, 552);
  598. CHECK_OFFSET(guest_pending_dbg_exceptions, 560);
  599. CHECK_OFFSET(guest_sysenter_esp, 568);
  600. CHECK_OFFSET(guest_sysenter_eip, 576);
  601. CHECK_OFFSET(host_cr0, 584);
  602. CHECK_OFFSET(host_cr3, 592);
  603. CHECK_OFFSET(host_cr4, 600);
  604. CHECK_OFFSET(host_fs_base, 608);
  605. CHECK_OFFSET(host_gs_base, 616);
  606. CHECK_OFFSET(host_tr_base, 624);
  607. CHECK_OFFSET(host_gdtr_base, 632);
  608. CHECK_OFFSET(host_idtr_base, 640);
  609. CHECK_OFFSET(host_ia32_sysenter_esp, 648);
  610. CHECK_OFFSET(host_ia32_sysenter_eip, 656);
  611. CHECK_OFFSET(host_rsp, 664);
  612. CHECK_OFFSET(host_rip, 672);
  613. CHECK_OFFSET(pin_based_vm_exec_control, 744);
  614. CHECK_OFFSET(cpu_based_vm_exec_control, 748);
  615. CHECK_OFFSET(exception_bitmap, 752);
  616. CHECK_OFFSET(page_fault_error_code_mask, 756);
  617. CHECK_OFFSET(page_fault_error_code_match, 760);
  618. CHECK_OFFSET(cr3_target_count, 764);
  619. CHECK_OFFSET(vm_exit_controls, 768);
  620. CHECK_OFFSET(vm_exit_msr_store_count, 772);
  621. CHECK_OFFSET(vm_exit_msr_load_count, 776);
  622. CHECK_OFFSET(vm_entry_controls, 780);
  623. CHECK_OFFSET(vm_entry_msr_load_count, 784);
  624. CHECK_OFFSET(vm_entry_intr_info_field, 788);
  625. CHECK_OFFSET(vm_entry_exception_error_code, 792);
  626. CHECK_OFFSET(vm_entry_instruction_len, 796);
  627. CHECK_OFFSET(tpr_threshold, 800);
  628. CHECK_OFFSET(secondary_vm_exec_control, 804);
  629. CHECK_OFFSET(vm_instruction_error, 808);
  630. CHECK_OFFSET(vm_exit_reason, 812);
  631. CHECK_OFFSET(vm_exit_intr_info, 816);
  632. CHECK_OFFSET(vm_exit_intr_error_code, 820);
  633. CHECK_OFFSET(idt_vectoring_info_field, 824);
  634. CHECK_OFFSET(idt_vectoring_error_code, 828);
  635. CHECK_OFFSET(vm_exit_instruction_len, 832);
  636. CHECK_OFFSET(vmx_instruction_info, 836);
  637. CHECK_OFFSET(guest_es_limit, 840);
  638. CHECK_OFFSET(guest_cs_limit, 844);
  639. CHECK_OFFSET(guest_ss_limit, 848);
  640. CHECK_OFFSET(guest_ds_limit, 852);
  641. CHECK_OFFSET(guest_fs_limit, 856);
  642. CHECK_OFFSET(guest_gs_limit, 860);
  643. CHECK_OFFSET(guest_ldtr_limit, 864);
  644. CHECK_OFFSET(guest_tr_limit, 868);
  645. CHECK_OFFSET(guest_gdtr_limit, 872);
  646. CHECK_OFFSET(guest_idtr_limit, 876);
  647. CHECK_OFFSET(guest_es_ar_bytes, 880);
  648. CHECK_OFFSET(guest_cs_ar_bytes, 884);
  649. CHECK_OFFSET(guest_ss_ar_bytes, 888);
  650. CHECK_OFFSET(guest_ds_ar_bytes, 892);
  651. CHECK_OFFSET(guest_fs_ar_bytes, 896);
  652. CHECK_OFFSET(guest_gs_ar_bytes, 900);
  653. CHECK_OFFSET(guest_ldtr_ar_bytes, 904);
  654. CHECK_OFFSET(guest_tr_ar_bytes, 908);
  655. CHECK_OFFSET(guest_interruptibility_info, 912);
  656. CHECK_OFFSET(guest_activity_state, 916);
  657. CHECK_OFFSET(guest_sysenter_cs, 920);
  658. CHECK_OFFSET(host_ia32_sysenter_cs, 924);
  659. CHECK_OFFSET(vmx_preemption_timer_value, 928);
  660. CHECK_OFFSET(virtual_processor_id, 960);
  661. CHECK_OFFSET(posted_intr_nv, 962);
  662. CHECK_OFFSET(guest_es_selector, 964);
  663. CHECK_OFFSET(guest_cs_selector, 966);
  664. CHECK_OFFSET(guest_ss_selector, 968);
  665. CHECK_OFFSET(guest_ds_selector, 970);
  666. CHECK_OFFSET(guest_fs_selector, 972);
  667. CHECK_OFFSET(guest_gs_selector, 974);
  668. CHECK_OFFSET(guest_ldtr_selector, 976);
  669. CHECK_OFFSET(guest_tr_selector, 978);
  670. CHECK_OFFSET(guest_intr_status, 980);
  671. CHECK_OFFSET(host_es_selector, 982);
  672. CHECK_OFFSET(host_cs_selector, 984);
  673. CHECK_OFFSET(host_ss_selector, 986);
  674. CHECK_OFFSET(host_ds_selector, 988);
  675. CHECK_OFFSET(host_fs_selector, 990);
  676. CHECK_OFFSET(host_gs_selector, 992);
  677. CHECK_OFFSET(host_tr_selector, 994);
  678. CHECK_OFFSET(guest_pml_index, 996);
  679. }
  680. /*
  681. * VMCS12_REVISION is an arbitrary id that should be changed if the content or
  682. * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
  683. * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
  684. *
  685. * IMPORTANT: Changing this value will break save/restore compatibility with
  686. * older kvm releases.
  687. */
  688. #define VMCS12_REVISION 0x11e57ed0
  689. /*
  690. * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
  691. * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
  692. * current implementation, 4K are reserved to avoid future complications.
  693. */
  694. #define VMCS12_SIZE 0x1000
  695. /*
  696. * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
  697. * supported VMCS12 field encoding.
  698. */
  699. #define VMCS12_MAX_FIELD_INDEX 0x17
  700. struct nested_vmx_msrs {
  701. /*
  702. * We only store the "true" versions of the VMX capability MSRs. We
  703. * generate the "non-true" versions by setting the must-be-1 bits
  704. * according to the SDM.
  705. */
  706. u32 procbased_ctls_low;
  707. u32 procbased_ctls_high;
  708. u32 secondary_ctls_low;
  709. u32 secondary_ctls_high;
  710. u32 pinbased_ctls_low;
  711. u32 pinbased_ctls_high;
  712. u32 exit_ctls_low;
  713. u32 exit_ctls_high;
  714. u32 entry_ctls_low;
  715. u32 entry_ctls_high;
  716. u32 misc_low;
  717. u32 misc_high;
  718. u32 ept_caps;
  719. u32 vpid_caps;
  720. u64 basic;
  721. u64 cr0_fixed0;
  722. u64 cr0_fixed1;
  723. u64 cr4_fixed0;
  724. u64 cr4_fixed1;
  725. u64 vmcs_enum;
  726. u64 vmfunc_controls;
  727. };
  728. /*
  729. * The nested_vmx structure is part of vcpu_vmx, and holds information we need
  730. * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
  731. */
  732. struct nested_vmx {
  733. /* Has the level1 guest done vmxon? */
  734. bool vmxon;
  735. gpa_t vmxon_ptr;
  736. bool pml_full;
  737. /* The guest-physical address of the current VMCS L1 keeps for L2 */
  738. gpa_t current_vmptr;
  739. /*
  740. * Cache of the guest's VMCS, existing outside of guest memory.
  741. * Loaded from guest memory during VMPTRLD. Flushed to guest
  742. * memory during VMCLEAR and VMPTRLD.
  743. */
  744. struct vmcs12 *cached_vmcs12;
  745. /*
  746. * Cache of the guest's shadow VMCS, existing outside of guest
  747. * memory. Loaded from guest memory during VM entry. Flushed
  748. * to guest memory during VM exit.
  749. */
  750. struct vmcs12 *cached_shadow_vmcs12;
  751. /*
  752. * Indicates if the shadow vmcs must be updated with the
  753. * data hold by vmcs12
  754. */
  755. bool sync_shadow_vmcs;
  756. bool dirty_vmcs12;
  757. bool change_vmcs01_virtual_apic_mode;
  758. /* L2 must run next, and mustn't decide to exit to L1. */
  759. bool nested_run_pending;
  760. struct loaded_vmcs vmcs02;
  761. /*
  762. * Guest pages referred to in the vmcs02 with host-physical
  763. * pointers, so we must keep them pinned while L2 runs.
  764. */
  765. struct page *apic_access_page;
  766. struct page *virtual_apic_page;
  767. struct page *pi_desc_page;
  768. struct pi_desc *pi_desc;
  769. bool pi_pending;
  770. u16 posted_intr_nv;
  771. struct hrtimer preemption_timer;
  772. bool preemption_timer_expired;
  773. /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
  774. u64 vmcs01_debugctl;
  775. u64 vmcs01_guest_bndcfgs;
  776. u16 vpid02;
  777. u16 last_vpid;
  778. struct nested_vmx_msrs msrs;
  779. /* SMM related state */
  780. struct {
  781. /* in VMX operation on SMM entry? */
  782. bool vmxon;
  783. /* in guest mode on SMM entry? */
  784. bool guest_mode;
  785. } smm;
  786. };
  787. #define POSTED_INTR_ON 0
  788. #define POSTED_INTR_SN 1
  789. /* Posted-Interrupt Descriptor */
  790. struct pi_desc {
  791. u32 pir[8]; /* Posted interrupt requested */
  792. union {
  793. struct {
  794. /* bit 256 - Outstanding Notification */
  795. u16 on : 1,
  796. /* bit 257 - Suppress Notification */
  797. sn : 1,
  798. /* bit 271:258 - Reserved */
  799. rsvd_1 : 14;
  800. /* bit 279:272 - Notification Vector */
  801. u8 nv;
  802. /* bit 287:280 - Reserved */
  803. u8 rsvd_2;
  804. /* bit 319:288 - Notification Destination */
  805. u32 ndst;
  806. };
  807. u64 control;
  808. };
  809. u32 rsvd[6];
  810. } __aligned(64);
  811. static bool pi_test_and_set_on(struct pi_desc *pi_desc)
  812. {
  813. return test_and_set_bit(POSTED_INTR_ON,
  814. (unsigned long *)&pi_desc->control);
  815. }
  816. static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
  817. {
  818. return test_and_clear_bit(POSTED_INTR_ON,
  819. (unsigned long *)&pi_desc->control);
  820. }
  821. static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
  822. {
  823. return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
  824. }
  825. static inline void pi_clear_sn(struct pi_desc *pi_desc)
  826. {
  827. return clear_bit(POSTED_INTR_SN,
  828. (unsigned long *)&pi_desc->control);
  829. }
  830. static inline void pi_set_sn(struct pi_desc *pi_desc)
  831. {
  832. return set_bit(POSTED_INTR_SN,
  833. (unsigned long *)&pi_desc->control);
  834. }
  835. static inline void pi_clear_on(struct pi_desc *pi_desc)
  836. {
  837. clear_bit(POSTED_INTR_ON,
  838. (unsigned long *)&pi_desc->control);
  839. }
  840. static inline int pi_test_on(struct pi_desc *pi_desc)
  841. {
  842. return test_bit(POSTED_INTR_ON,
  843. (unsigned long *)&pi_desc->control);
  844. }
  845. static inline int pi_test_sn(struct pi_desc *pi_desc)
  846. {
  847. return test_bit(POSTED_INTR_SN,
  848. (unsigned long *)&pi_desc->control);
  849. }
  850. struct vmx_msrs {
  851. unsigned int nr;
  852. struct vmx_msr_entry val[NR_AUTOLOAD_MSRS];
  853. };
  854. struct vcpu_vmx {
  855. struct kvm_vcpu vcpu;
  856. unsigned long host_rsp;
  857. u8 fail;
  858. u8 msr_bitmap_mode;
  859. u32 exit_intr_info;
  860. u32 idt_vectoring_info;
  861. ulong rflags;
  862. struct shared_msr_entry *guest_msrs;
  863. int nmsrs;
  864. int save_nmsrs;
  865. bool guest_msrs_dirty;
  866. unsigned long host_idt_base;
  867. #ifdef CONFIG_X86_64
  868. u64 msr_host_kernel_gs_base;
  869. u64 msr_guest_kernel_gs_base;
  870. #endif
  871. u64 spec_ctrl;
  872. u32 vm_entry_controls_shadow;
  873. u32 vm_exit_controls_shadow;
  874. u32 secondary_exec_control;
  875. /*
  876. * loaded_vmcs points to the VMCS currently used in this vcpu. For a
  877. * non-nested (L1) guest, it always points to vmcs01. For a nested
  878. * guest (L2), it points to a different VMCS. loaded_cpu_state points
  879. * to the VMCS whose state is loaded into the CPU registers that only
  880. * need to be switched when transitioning to/from the kernel; a NULL
  881. * value indicates that host state is loaded.
  882. */
  883. struct loaded_vmcs vmcs01;
  884. struct loaded_vmcs *loaded_vmcs;
  885. struct loaded_vmcs *loaded_cpu_state;
  886. bool __launched; /* temporary, used in vmx_vcpu_run */
  887. struct msr_autoload {
  888. struct vmx_msrs guest;
  889. struct vmx_msrs host;
  890. } msr_autoload;
  891. struct {
  892. int vm86_active;
  893. ulong save_rflags;
  894. struct kvm_segment segs[8];
  895. } rmode;
  896. struct {
  897. u32 bitmask; /* 4 bits per segment (1 bit per field) */
  898. struct kvm_save_segment {
  899. u16 selector;
  900. unsigned long base;
  901. u32 limit;
  902. u32 ar;
  903. } seg[8];
  904. } segment_cache;
  905. int vpid;
  906. bool emulation_required;
  907. u32 exit_reason;
  908. /* Posted interrupt descriptor */
  909. struct pi_desc pi_desc;
  910. /* Support for a guest hypervisor (nested VMX) */
  911. struct nested_vmx nested;
  912. /* Dynamic PLE window. */
  913. int ple_window;
  914. bool ple_window_dirty;
  915. bool req_immediate_exit;
  916. /* Support for PML */
  917. #define PML_ENTITY_NUM 512
  918. struct page *pml_pg;
  919. /* apic deadline value in host tsc */
  920. u64 hv_deadline_tsc;
  921. u64 current_tsc_ratio;
  922. u32 host_pkru;
  923. unsigned long host_debugctlmsr;
  924. /*
  925. * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
  926. * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
  927. * in msr_ia32_feature_control_valid_bits.
  928. */
  929. u64 msr_ia32_feature_control;
  930. u64 msr_ia32_feature_control_valid_bits;
  931. u64 ept_pointer;
  932. };
  933. enum segment_cache_field {
  934. SEG_FIELD_SEL = 0,
  935. SEG_FIELD_BASE = 1,
  936. SEG_FIELD_LIMIT = 2,
  937. SEG_FIELD_AR = 3,
  938. SEG_FIELD_NR = 4
  939. };
  940. static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
  941. {
  942. return container_of(kvm, struct kvm_vmx, kvm);
  943. }
  944. static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
  945. {
  946. return container_of(vcpu, struct vcpu_vmx, vcpu);
  947. }
  948. static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
  949. {
  950. return &(to_vmx(vcpu)->pi_desc);
  951. }
  952. #define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
  953. #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
  954. #define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
  955. #define FIELD64(number, name) \
  956. FIELD(number, name), \
  957. [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
  958. static u16 shadow_read_only_fields[] = {
  959. #define SHADOW_FIELD_RO(x) x,
  960. #include "vmx_shadow_fields.h"
  961. };
  962. static int max_shadow_read_only_fields =
  963. ARRAY_SIZE(shadow_read_only_fields);
  964. static u16 shadow_read_write_fields[] = {
  965. #define SHADOW_FIELD_RW(x) x,
  966. #include "vmx_shadow_fields.h"
  967. };
  968. static int max_shadow_read_write_fields =
  969. ARRAY_SIZE(shadow_read_write_fields);
  970. static const unsigned short vmcs_field_to_offset_table[] = {
  971. FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
  972. FIELD(POSTED_INTR_NV, posted_intr_nv),
  973. FIELD(GUEST_ES_SELECTOR, guest_es_selector),
  974. FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
  975. FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
  976. FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
  977. FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
  978. FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
  979. FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
  980. FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
  981. FIELD(GUEST_INTR_STATUS, guest_intr_status),
  982. FIELD(GUEST_PML_INDEX, guest_pml_index),
  983. FIELD(HOST_ES_SELECTOR, host_es_selector),
  984. FIELD(HOST_CS_SELECTOR, host_cs_selector),
  985. FIELD(HOST_SS_SELECTOR, host_ss_selector),
  986. FIELD(HOST_DS_SELECTOR, host_ds_selector),
  987. FIELD(HOST_FS_SELECTOR, host_fs_selector),
  988. FIELD(HOST_GS_SELECTOR, host_gs_selector),
  989. FIELD(HOST_TR_SELECTOR, host_tr_selector),
  990. FIELD64(IO_BITMAP_A, io_bitmap_a),
  991. FIELD64(IO_BITMAP_B, io_bitmap_b),
  992. FIELD64(MSR_BITMAP, msr_bitmap),
  993. FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
  994. FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
  995. FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
  996. FIELD64(PML_ADDRESS, pml_address),
  997. FIELD64(TSC_OFFSET, tsc_offset),
  998. FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
  999. FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
  1000. FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
  1001. FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
  1002. FIELD64(EPT_POINTER, ept_pointer),
  1003. FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
  1004. FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
  1005. FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
  1006. FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
  1007. FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
  1008. FIELD64(VMREAD_BITMAP, vmread_bitmap),
  1009. FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
  1010. FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
  1011. FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
  1012. FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
  1013. FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
  1014. FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
  1015. FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
  1016. FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
  1017. FIELD64(GUEST_PDPTR0, guest_pdptr0),
  1018. FIELD64(GUEST_PDPTR1, guest_pdptr1),
  1019. FIELD64(GUEST_PDPTR2, guest_pdptr2),
  1020. FIELD64(GUEST_PDPTR3, guest_pdptr3),
  1021. FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
  1022. FIELD64(HOST_IA32_PAT, host_ia32_pat),
  1023. FIELD64(HOST_IA32_EFER, host_ia32_efer),
  1024. FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
  1025. FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
  1026. FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
  1027. FIELD(EXCEPTION_BITMAP, exception_bitmap),
  1028. FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
  1029. FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
  1030. FIELD(CR3_TARGET_COUNT, cr3_target_count),
  1031. FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
  1032. FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
  1033. FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
  1034. FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
  1035. FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
  1036. FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
  1037. FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
  1038. FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
  1039. FIELD(TPR_THRESHOLD, tpr_threshold),
  1040. FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
  1041. FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
  1042. FIELD(VM_EXIT_REASON, vm_exit_reason),
  1043. FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
  1044. FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
  1045. FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
  1046. FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
  1047. FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
  1048. FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
  1049. FIELD(GUEST_ES_LIMIT, guest_es_limit),
  1050. FIELD(GUEST_CS_LIMIT, guest_cs_limit),
  1051. FIELD(GUEST_SS_LIMIT, guest_ss_limit),
  1052. FIELD(GUEST_DS_LIMIT, guest_ds_limit),
  1053. FIELD(GUEST_FS_LIMIT, guest_fs_limit),
  1054. FIELD(GUEST_GS_LIMIT, guest_gs_limit),
  1055. FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
  1056. FIELD(GUEST_TR_LIMIT, guest_tr_limit),
  1057. FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
  1058. FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
  1059. FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
  1060. FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
  1061. FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
  1062. FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
  1063. FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
  1064. FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
  1065. FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
  1066. FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
  1067. FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
  1068. FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
  1069. FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
  1070. FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
  1071. FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
  1072. FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
  1073. FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
  1074. FIELD(CR0_READ_SHADOW, cr0_read_shadow),
  1075. FIELD(CR4_READ_SHADOW, cr4_read_shadow),
  1076. FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
  1077. FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
  1078. FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
  1079. FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
  1080. FIELD(EXIT_QUALIFICATION, exit_qualification),
  1081. FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
  1082. FIELD(GUEST_CR0, guest_cr0),
  1083. FIELD(GUEST_CR3, guest_cr3),
  1084. FIELD(GUEST_CR4, guest_cr4),
  1085. FIELD(GUEST_ES_BASE, guest_es_base),
  1086. FIELD(GUEST_CS_BASE, guest_cs_base),
  1087. FIELD(GUEST_SS_BASE, guest_ss_base),
  1088. FIELD(GUEST_DS_BASE, guest_ds_base),
  1089. FIELD(GUEST_FS_BASE, guest_fs_base),
  1090. FIELD(GUEST_GS_BASE, guest_gs_base),
  1091. FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
  1092. FIELD(GUEST_TR_BASE, guest_tr_base),
  1093. FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
  1094. FIELD(GUEST_IDTR_BASE, guest_idtr_base),
  1095. FIELD(GUEST_DR7, guest_dr7),
  1096. FIELD(GUEST_RSP, guest_rsp),
  1097. FIELD(GUEST_RIP, guest_rip),
  1098. FIELD(GUEST_RFLAGS, guest_rflags),
  1099. FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
  1100. FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
  1101. FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
  1102. FIELD(HOST_CR0, host_cr0),
  1103. FIELD(HOST_CR3, host_cr3),
  1104. FIELD(HOST_CR4, host_cr4),
  1105. FIELD(HOST_FS_BASE, host_fs_base),
  1106. FIELD(HOST_GS_BASE, host_gs_base),
  1107. FIELD(HOST_TR_BASE, host_tr_base),
  1108. FIELD(HOST_GDTR_BASE, host_gdtr_base),
  1109. FIELD(HOST_IDTR_BASE, host_idtr_base),
  1110. FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
  1111. FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
  1112. FIELD(HOST_RSP, host_rsp),
  1113. FIELD(HOST_RIP, host_rip),
  1114. };
  1115. static inline short vmcs_field_to_offset(unsigned long field)
  1116. {
  1117. const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
  1118. unsigned short offset;
  1119. unsigned index;
  1120. if (field >> 15)
  1121. return -ENOENT;
  1122. index = ROL16(field, 6);
  1123. if (index >= size)
  1124. return -ENOENT;
  1125. index = array_index_nospec(index, size);
  1126. offset = vmcs_field_to_offset_table[index];
  1127. if (offset == 0)
  1128. return -ENOENT;
  1129. return offset;
  1130. }
  1131. static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
  1132. {
  1133. return to_vmx(vcpu)->nested.cached_vmcs12;
  1134. }
  1135. static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu)
  1136. {
  1137. return to_vmx(vcpu)->nested.cached_shadow_vmcs12;
  1138. }
  1139. static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
  1140. static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
  1141. static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
  1142. static bool vmx_xsaves_supported(void);
  1143. static void vmx_set_segment(struct kvm_vcpu *vcpu,
  1144. struct kvm_segment *var, int seg);
  1145. static void vmx_get_segment(struct kvm_vcpu *vcpu,
  1146. struct kvm_segment *var, int seg);
  1147. static bool guest_state_valid(struct kvm_vcpu *vcpu);
  1148. static u32 vmx_segment_access_rights(struct kvm_segment *var);
  1149. static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
  1150. static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
  1151. static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
  1152. static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
  1153. u16 error_code);
  1154. static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
  1155. static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
  1156. u32 msr, int type);
  1157. static DEFINE_PER_CPU(struct vmcs *, vmxarea);
  1158. static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
  1159. /*
  1160. * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
  1161. * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
  1162. */
  1163. static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
  1164. /*
  1165. * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
  1166. * can find which vCPU should be waken up.
  1167. */
  1168. static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
  1169. static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
  1170. enum {
  1171. VMX_VMREAD_BITMAP,
  1172. VMX_VMWRITE_BITMAP,
  1173. VMX_BITMAP_NR
  1174. };
  1175. static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
  1176. #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
  1177. #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
  1178. static bool cpu_has_load_ia32_efer;
  1179. static bool cpu_has_load_perf_global_ctrl;
  1180. static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
  1181. static DEFINE_SPINLOCK(vmx_vpid_lock);
  1182. static struct vmcs_config {
  1183. int size;
  1184. int order;
  1185. u32 basic_cap;
  1186. u32 revision_id;
  1187. u32 pin_based_exec_ctrl;
  1188. u32 cpu_based_exec_ctrl;
  1189. u32 cpu_based_2nd_exec_ctrl;
  1190. u32 vmexit_ctrl;
  1191. u32 vmentry_ctrl;
  1192. struct nested_vmx_msrs nested;
  1193. } vmcs_config;
  1194. static struct vmx_capability {
  1195. u32 ept;
  1196. u32 vpid;
  1197. } vmx_capability;
  1198. #define VMX_SEGMENT_FIELD(seg) \
  1199. [VCPU_SREG_##seg] = { \
  1200. .selector = GUEST_##seg##_SELECTOR, \
  1201. .base = GUEST_##seg##_BASE, \
  1202. .limit = GUEST_##seg##_LIMIT, \
  1203. .ar_bytes = GUEST_##seg##_AR_BYTES, \
  1204. }
  1205. static const struct kvm_vmx_segment_field {
  1206. unsigned selector;
  1207. unsigned base;
  1208. unsigned limit;
  1209. unsigned ar_bytes;
  1210. } kvm_vmx_segment_fields[] = {
  1211. VMX_SEGMENT_FIELD(CS),
  1212. VMX_SEGMENT_FIELD(DS),
  1213. VMX_SEGMENT_FIELD(ES),
  1214. VMX_SEGMENT_FIELD(FS),
  1215. VMX_SEGMENT_FIELD(GS),
  1216. VMX_SEGMENT_FIELD(SS),
  1217. VMX_SEGMENT_FIELD(TR),
  1218. VMX_SEGMENT_FIELD(LDTR),
  1219. };
  1220. static u64 host_efer;
  1221. static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
  1222. /*
  1223. * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
  1224. * away by decrementing the array size.
  1225. */
  1226. static const u32 vmx_msr_index[] = {
  1227. #ifdef CONFIG_X86_64
  1228. MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
  1229. #endif
  1230. MSR_EFER, MSR_TSC_AUX, MSR_STAR,
  1231. };
  1232. DEFINE_STATIC_KEY_FALSE(enable_evmcs);
  1233. #define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
  1234. #define KVM_EVMCS_VERSION 1
  1235. #if IS_ENABLED(CONFIG_HYPERV)
  1236. static bool __read_mostly enlightened_vmcs = true;
  1237. module_param(enlightened_vmcs, bool, 0444);
  1238. static inline void evmcs_write64(unsigned long field, u64 value)
  1239. {
  1240. u16 clean_field;
  1241. int offset = get_evmcs_offset(field, &clean_field);
  1242. if (offset < 0)
  1243. return;
  1244. *(u64 *)((char *)current_evmcs + offset) = value;
  1245. current_evmcs->hv_clean_fields &= ~clean_field;
  1246. }
  1247. static inline void evmcs_write32(unsigned long field, u32 value)
  1248. {
  1249. u16 clean_field;
  1250. int offset = get_evmcs_offset(field, &clean_field);
  1251. if (offset < 0)
  1252. return;
  1253. *(u32 *)((char *)current_evmcs + offset) = value;
  1254. current_evmcs->hv_clean_fields &= ~clean_field;
  1255. }
  1256. static inline void evmcs_write16(unsigned long field, u16 value)
  1257. {
  1258. u16 clean_field;
  1259. int offset = get_evmcs_offset(field, &clean_field);
  1260. if (offset < 0)
  1261. return;
  1262. *(u16 *)((char *)current_evmcs + offset) = value;
  1263. current_evmcs->hv_clean_fields &= ~clean_field;
  1264. }
  1265. static inline u64 evmcs_read64(unsigned long field)
  1266. {
  1267. int offset = get_evmcs_offset(field, NULL);
  1268. if (offset < 0)
  1269. return 0;
  1270. return *(u64 *)((char *)current_evmcs + offset);
  1271. }
  1272. static inline u32 evmcs_read32(unsigned long field)
  1273. {
  1274. int offset = get_evmcs_offset(field, NULL);
  1275. if (offset < 0)
  1276. return 0;
  1277. return *(u32 *)((char *)current_evmcs + offset);
  1278. }
  1279. static inline u16 evmcs_read16(unsigned long field)
  1280. {
  1281. int offset = get_evmcs_offset(field, NULL);
  1282. if (offset < 0)
  1283. return 0;
  1284. return *(u16 *)((char *)current_evmcs + offset);
  1285. }
  1286. static inline void evmcs_touch_msr_bitmap(void)
  1287. {
  1288. if (unlikely(!current_evmcs))
  1289. return;
  1290. if (current_evmcs->hv_enlightenments_control.msr_bitmap)
  1291. current_evmcs->hv_clean_fields &=
  1292. ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
  1293. }
  1294. static void evmcs_load(u64 phys_addr)
  1295. {
  1296. struct hv_vp_assist_page *vp_ap =
  1297. hv_get_vp_assist_page(smp_processor_id());
  1298. vp_ap->current_nested_vmcs = phys_addr;
  1299. vp_ap->enlighten_vmentry = 1;
  1300. }
  1301. static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
  1302. {
  1303. /*
  1304. * Enlightened VMCSv1 doesn't support these:
  1305. *
  1306. * POSTED_INTR_NV = 0x00000002,
  1307. * GUEST_INTR_STATUS = 0x00000810,
  1308. * APIC_ACCESS_ADDR = 0x00002014,
  1309. * POSTED_INTR_DESC_ADDR = 0x00002016,
  1310. * EOI_EXIT_BITMAP0 = 0x0000201c,
  1311. * EOI_EXIT_BITMAP1 = 0x0000201e,
  1312. * EOI_EXIT_BITMAP2 = 0x00002020,
  1313. * EOI_EXIT_BITMAP3 = 0x00002022,
  1314. */
  1315. vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
  1316. vmcs_conf->cpu_based_2nd_exec_ctrl &=
  1317. ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
  1318. vmcs_conf->cpu_based_2nd_exec_ctrl &=
  1319. ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  1320. vmcs_conf->cpu_based_2nd_exec_ctrl &=
  1321. ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
  1322. /*
  1323. * GUEST_PML_INDEX = 0x00000812,
  1324. * PML_ADDRESS = 0x0000200e,
  1325. */
  1326. vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
  1327. /* VM_FUNCTION_CONTROL = 0x00002018, */
  1328. vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
  1329. /*
  1330. * EPTP_LIST_ADDRESS = 0x00002024,
  1331. * VMREAD_BITMAP = 0x00002026,
  1332. * VMWRITE_BITMAP = 0x00002028,
  1333. */
  1334. vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
  1335. /*
  1336. * TSC_MULTIPLIER = 0x00002032,
  1337. */
  1338. vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
  1339. /*
  1340. * PLE_GAP = 0x00004020,
  1341. * PLE_WINDOW = 0x00004022,
  1342. */
  1343. vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  1344. /*
  1345. * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
  1346. */
  1347. vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  1348. /*
  1349. * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
  1350. * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
  1351. */
  1352. vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
  1353. vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
  1354. /*
  1355. * Currently unsupported in KVM:
  1356. * GUEST_IA32_RTIT_CTL = 0x00002814,
  1357. */
  1358. }
  1359. /* check_ept_pointer() should be under protection of ept_pointer_lock. */
  1360. static void check_ept_pointer_match(struct kvm *kvm)
  1361. {
  1362. struct kvm_vcpu *vcpu;
  1363. u64 tmp_eptp = INVALID_PAGE;
  1364. int i;
  1365. kvm_for_each_vcpu(i, vcpu, kvm) {
  1366. if (!VALID_PAGE(tmp_eptp)) {
  1367. tmp_eptp = to_vmx(vcpu)->ept_pointer;
  1368. } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) {
  1369. to_kvm_vmx(kvm)->ept_pointers_match
  1370. = EPT_POINTERS_MISMATCH;
  1371. return;
  1372. }
  1373. }
  1374. to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH;
  1375. }
  1376. static int vmx_hv_remote_flush_tlb(struct kvm *kvm)
  1377. {
  1378. int ret;
  1379. spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
  1380. if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK)
  1381. check_ept_pointer_match(kvm);
  1382. if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
  1383. ret = -ENOTSUPP;
  1384. goto out;
  1385. }
  1386. /*
  1387. * FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE hypercall needs the address of the
  1388. * base of EPT PML4 table, strip off EPT configuration information.
  1389. */
  1390. ret = hyperv_flush_guest_mapping(
  1391. to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer & PAGE_MASK);
  1392. out:
  1393. spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
  1394. return ret;
  1395. }
  1396. #else /* !IS_ENABLED(CONFIG_HYPERV) */
  1397. static inline void evmcs_write64(unsigned long field, u64 value) {}
  1398. static inline void evmcs_write32(unsigned long field, u32 value) {}
  1399. static inline void evmcs_write16(unsigned long field, u16 value) {}
  1400. static inline u64 evmcs_read64(unsigned long field) { return 0; }
  1401. static inline u32 evmcs_read32(unsigned long field) { return 0; }
  1402. static inline u16 evmcs_read16(unsigned long field) { return 0; }
  1403. static inline void evmcs_load(u64 phys_addr) {}
  1404. static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
  1405. static inline void evmcs_touch_msr_bitmap(void) {}
  1406. #endif /* IS_ENABLED(CONFIG_HYPERV) */
  1407. static inline bool is_exception_n(u32 intr_info, u8 vector)
  1408. {
  1409. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  1410. INTR_INFO_VALID_MASK)) ==
  1411. (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
  1412. }
  1413. static inline bool is_debug(u32 intr_info)
  1414. {
  1415. return is_exception_n(intr_info, DB_VECTOR);
  1416. }
  1417. static inline bool is_breakpoint(u32 intr_info)
  1418. {
  1419. return is_exception_n(intr_info, BP_VECTOR);
  1420. }
  1421. static inline bool is_page_fault(u32 intr_info)
  1422. {
  1423. return is_exception_n(intr_info, PF_VECTOR);
  1424. }
  1425. static inline bool is_no_device(u32 intr_info)
  1426. {
  1427. return is_exception_n(intr_info, NM_VECTOR);
  1428. }
  1429. static inline bool is_invalid_opcode(u32 intr_info)
  1430. {
  1431. return is_exception_n(intr_info, UD_VECTOR);
  1432. }
  1433. static inline bool is_gp_fault(u32 intr_info)
  1434. {
  1435. return is_exception_n(intr_info, GP_VECTOR);
  1436. }
  1437. static inline bool is_external_interrupt(u32 intr_info)
  1438. {
  1439. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
  1440. == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
  1441. }
  1442. static inline bool is_machine_check(u32 intr_info)
  1443. {
  1444. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  1445. INTR_INFO_VALID_MASK)) ==
  1446. (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
  1447. }
  1448. /* Undocumented: icebp/int1 */
  1449. static inline bool is_icebp(u32 intr_info)
  1450. {
  1451. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
  1452. == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
  1453. }
  1454. static inline bool cpu_has_vmx_msr_bitmap(void)
  1455. {
  1456. return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
  1457. }
  1458. static inline bool cpu_has_vmx_tpr_shadow(void)
  1459. {
  1460. return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
  1461. }
  1462. static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
  1463. {
  1464. return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
  1465. }
  1466. static inline bool cpu_has_secondary_exec_ctrls(void)
  1467. {
  1468. return vmcs_config.cpu_based_exec_ctrl &
  1469. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  1470. }
  1471. static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
  1472. {
  1473. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1474. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  1475. }
  1476. static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
  1477. {
  1478. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1479. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  1480. }
  1481. static inline bool cpu_has_vmx_apic_register_virt(void)
  1482. {
  1483. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1484. SECONDARY_EXEC_APIC_REGISTER_VIRT;
  1485. }
  1486. static inline bool cpu_has_vmx_virtual_intr_delivery(void)
  1487. {
  1488. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1489. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
  1490. }
  1491. static inline bool cpu_has_vmx_encls_vmexit(void)
  1492. {
  1493. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1494. SECONDARY_EXEC_ENCLS_EXITING;
  1495. }
  1496. /*
  1497. * Comment's format: document - errata name - stepping - processor name.
  1498. * Refer from
  1499. * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
  1500. */
  1501. static u32 vmx_preemption_cpu_tfms[] = {
  1502. /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
  1503. 0x000206E6,
  1504. /* 323056.pdf - AAX65 - C2 - Xeon L3406 */
  1505. /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
  1506. /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
  1507. 0x00020652,
  1508. /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
  1509. 0x00020655,
  1510. /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
  1511. /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
  1512. /*
  1513. * 320767.pdf - AAP86 - B1 -
  1514. * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
  1515. */
  1516. 0x000106E5,
  1517. /* 321333.pdf - AAM126 - C0 - Xeon 3500 */
  1518. 0x000106A0,
  1519. /* 321333.pdf - AAM126 - C1 - Xeon 3500 */
  1520. 0x000106A1,
  1521. /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
  1522. 0x000106A4,
  1523. /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
  1524. /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
  1525. /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
  1526. 0x000106A5,
  1527. };
  1528. static inline bool cpu_has_broken_vmx_preemption_timer(void)
  1529. {
  1530. u32 eax = cpuid_eax(0x00000001), i;
  1531. /* Clear the reserved bits */
  1532. eax &= ~(0x3U << 14 | 0xfU << 28);
  1533. for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
  1534. if (eax == vmx_preemption_cpu_tfms[i])
  1535. return true;
  1536. return false;
  1537. }
  1538. static inline bool cpu_has_vmx_preemption_timer(void)
  1539. {
  1540. return vmcs_config.pin_based_exec_ctrl &
  1541. PIN_BASED_VMX_PREEMPTION_TIMER;
  1542. }
  1543. static inline bool cpu_has_vmx_posted_intr(void)
  1544. {
  1545. return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
  1546. vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
  1547. }
  1548. static inline bool cpu_has_vmx_apicv(void)
  1549. {
  1550. return cpu_has_vmx_apic_register_virt() &&
  1551. cpu_has_vmx_virtual_intr_delivery() &&
  1552. cpu_has_vmx_posted_intr();
  1553. }
  1554. static inline bool cpu_has_vmx_flexpriority(void)
  1555. {
  1556. return cpu_has_vmx_tpr_shadow() &&
  1557. cpu_has_vmx_virtualize_apic_accesses();
  1558. }
  1559. static inline bool cpu_has_vmx_ept_execute_only(void)
  1560. {
  1561. return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
  1562. }
  1563. static inline bool cpu_has_vmx_ept_2m_page(void)
  1564. {
  1565. return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
  1566. }
  1567. static inline bool cpu_has_vmx_ept_1g_page(void)
  1568. {
  1569. return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
  1570. }
  1571. static inline bool cpu_has_vmx_ept_4levels(void)
  1572. {
  1573. return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
  1574. }
  1575. static inline bool cpu_has_vmx_ept_mt_wb(void)
  1576. {
  1577. return vmx_capability.ept & VMX_EPTP_WB_BIT;
  1578. }
  1579. static inline bool cpu_has_vmx_ept_5levels(void)
  1580. {
  1581. return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
  1582. }
  1583. static inline bool cpu_has_vmx_ept_ad_bits(void)
  1584. {
  1585. return vmx_capability.ept & VMX_EPT_AD_BIT;
  1586. }
  1587. static inline bool cpu_has_vmx_invept_context(void)
  1588. {
  1589. return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
  1590. }
  1591. static inline bool cpu_has_vmx_invept_global(void)
  1592. {
  1593. return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
  1594. }
  1595. static inline bool cpu_has_vmx_invvpid_individual_addr(void)
  1596. {
  1597. return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
  1598. }
  1599. static inline bool cpu_has_vmx_invvpid_single(void)
  1600. {
  1601. return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
  1602. }
  1603. static inline bool cpu_has_vmx_invvpid_global(void)
  1604. {
  1605. return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
  1606. }
  1607. static inline bool cpu_has_vmx_invvpid(void)
  1608. {
  1609. return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
  1610. }
  1611. static inline bool cpu_has_vmx_ept(void)
  1612. {
  1613. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1614. SECONDARY_EXEC_ENABLE_EPT;
  1615. }
  1616. static inline bool cpu_has_vmx_unrestricted_guest(void)
  1617. {
  1618. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1619. SECONDARY_EXEC_UNRESTRICTED_GUEST;
  1620. }
  1621. static inline bool cpu_has_vmx_ple(void)
  1622. {
  1623. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1624. SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  1625. }
  1626. static inline bool cpu_has_vmx_basic_inout(void)
  1627. {
  1628. return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
  1629. }
  1630. static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
  1631. {
  1632. return flexpriority_enabled && lapic_in_kernel(vcpu);
  1633. }
  1634. static inline bool cpu_has_vmx_vpid(void)
  1635. {
  1636. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1637. SECONDARY_EXEC_ENABLE_VPID;
  1638. }
  1639. static inline bool cpu_has_vmx_rdtscp(void)
  1640. {
  1641. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1642. SECONDARY_EXEC_RDTSCP;
  1643. }
  1644. static inline bool cpu_has_vmx_invpcid(void)
  1645. {
  1646. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1647. SECONDARY_EXEC_ENABLE_INVPCID;
  1648. }
  1649. static inline bool cpu_has_virtual_nmis(void)
  1650. {
  1651. return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
  1652. }
  1653. static inline bool cpu_has_vmx_wbinvd_exit(void)
  1654. {
  1655. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1656. SECONDARY_EXEC_WBINVD_EXITING;
  1657. }
  1658. static inline bool cpu_has_vmx_shadow_vmcs(void)
  1659. {
  1660. u64 vmx_msr;
  1661. rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
  1662. /* check if the cpu supports writing r/o exit information fields */
  1663. if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
  1664. return false;
  1665. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1666. SECONDARY_EXEC_SHADOW_VMCS;
  1667. }
  1668. static inline bool cpu_has_vmx_pml(void)
  1669. {
  1670. return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
  1671. }
  1672. static inline bool cpu_has_vmx_tsc_scaling(void)
  1673. {
  1674. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1675. SECONDARY_EXEC_TSC_SCALING;
  1676. }
  1677. static inline bool cpu_has_vmx_vmfunc(void)
  1678. {
  1679. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1680. SECONDARY_EXEC_ENABLE_VMFUNC;
  1681. }
  1682. static bool vmx_umip_emulated(void)
  1683. {
  1684. return vmcs_config.cpu_based_2nd_exec_ctrl &
  1685. SECONDARY_EXEC_DESC;
  1686. }
  1687. static inline bool report_flexpriority(void)
  1688. {
  1689. return flexpriority_enabled;
  1690. }
  1691. static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
  1692. {
  1693. return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
  1694. }
  1695. /*
  1696. * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
  1697. * to modify any valid field of the VMCS, or are the VM-exit
  1698. * information fields read-only?
  1699. */
  1700. static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
  1701. {
  1702. return to_vmx(vcpu)->nested.msrs.misc_low &
  1703. MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
  1704. }
  1705. static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
  1706. {
  1707. return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
  1708. }
  1709. static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
  1710. {
  1711. return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
  1712. CPU_BASED_MONITOR_TRAP_FLAG;
  1713. }
  1714. static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu)
  1715. {
  1716. return to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
  1717. SECONDARY_EXEC_SHADOW_VMCS;
  1718. }
  1719. static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
  1720. {
  1721. return vmcs12->cpu_based_vm_exec_control & bit;
  1722. }
  1723. static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
  1724. {
  1725. return (vmcs12->cpu_based_vm_exec_control &
  1726. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
  1727. (vmcs12->secondary_vm_exec_control & bit);
  1728. }
  1729. static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
  1730. {
  1731. return vmcs12->pin_based_vm_exec_control &
  1732. PIN_BASED_VMX_PREEMPTION_TIMER;
  1733. }
  1734. static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
  1735. {
  1736. return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
  1737. }
  1738. static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
  1739. {
  1740. return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
  1741. }
  1742. static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
  1743. {
  1744. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
  1745. }
  1746. static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
  1747. {
  1748. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
  1749. }
  1750. static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
  1751. {
  1752. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
  1753. }
  1754. static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
  1755. {
  1756. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
  1757. }
  1758. static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
  1759. {
  1760. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
  1761. }
  1762. static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
  1763. {
  1764. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
  1765. }
  1766. static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
  1767. {
  1768. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  1769. }
  1770. static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
  1771. {
  1772. return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
  1773. }
  1774. static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
  1775. {
  1776. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
  1777. }
  1778. static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
  1779. {
  1780. return nested_cpu_has_vmfunc(vmcs12) &&
  1781. (vmcs12->vm_function_control &
  1782. VMX_VMFUNC_EPTP_SWITCHING);
  1783. }
  1784. static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12)
  1785. {
  1786. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS);
  1787. }
  1788. static inline bool is_nmi(u32 intr_info)
  1789. {
  1790. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
  1791. == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
  1792. }
  1793. static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
  1794. u32 exit_intr_info,
  1795. unsigned long exit_qualification);
  1796. static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
  1797. struct vmcs12 *vmcs12,
  1798. u32 reason, unsigned long qualification);
  1799. static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
  1800. {
  1801. int i;
  1802. for (i = 0; i < vmx->nmsrs; ++i)
  1803. if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
  1804. return i;
  1805. return -1;
  1806. }
  1807. static inline void __invvpid(unsigned long ext, u16 vpid, gva_t gva)
  1808. {
  1809. struct {
  1810. u64 vpid : 16;
  1811. u64 rsvd : 48;
  1812. u64 gva;
  1813. } operand = { vpid, 0, gva };
  1814. bool error;
  1815. asm volatile (__ex(ASM_VMX_INVVPID) CC_SET(na)
  1816. : CC_OUT(na) (error) : "a"(&operand), "c"(ext)
  1817. : "memory");
  1818. BUG_ON(error);
  1819. }
  1820. static inline void __invept(unsigned long ext, u64 eptp, gpa_t gpa)
  1821. {
  1822. struct {
  1823. u64 eptp, gpa;
  1824. } operand = {eptp, gpa};
  1825. bool error;
  1826. asm volatile (__ex(ASM_VMX_INVEPT) CC_SET(na)
  1827. : CC_OUT(na) (error) : "a" (&operand), "c" (ext)
  1828. : "memory");
  1829. BUG_ON(error);
  1830. }
  1831. static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
  1832. {
  1833. int i;
  1834. i = __find_msr_index(vmx, msr);
  1835. if (i >= 0)
  1836. return &vmx->guest_msrs[i];
  1837. return NULL;
  1838. }
  1839. static void vmcs_clear(struct vmcs *vmcs)
  1840. {
  1841. u64 phys_addr = __pa(vmcs);
  1842. bool error;
  1843. asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) CC_SET(na)
  1844. : CC_OUT(na) (error) : "a"(&phys_addr), "m"(phys_addr)
  1845. : "memory");
  1846. if (unlikely(error))
  1847. printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
  1848. vmcs, phys_addr);
  1849. }
  1850. static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
  1851. {
  1852. vmcs_clear(loaded_vmcs->vmcs);
  1853. if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
  1854. vmcs_clear(loaded_vmcs->shadow_vmcs);
  1855. loaded_vmcs->cpu = -1;
  1856. loaded_vmcs->launched = 0;
  1857. }
  1858. static void vmcs_load(struct vmcs *vmcs)
  1859. {
  1860. u64 phys_addr = __pa(vmcs);
  1861. bool error;
  1862. if (static_branch_unlikely(&enable_evmcs))
  1863. return evmcs_load(phys_addr);
  1864. asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) CC_SET(na)
  1865. : CC_OUT(na) (error) : "a"(&phys_addr), "m"(phys_addr)
  1866. : "memory");
  1867. if (unlikely(error))
  1868. printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
  1869. vmcs, phys_addr);
  1870. }
  1871. #ifdef CONFIG_KEXEC_CORE
  1872. /*
  1873. * This bitmap is used to indicate whether the vmclear
  1874. * operation is enabled on all cpus. All disabled by
  1875. * default.
  1876. */
  1877. static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
  1878. static inline void crash_enable_local_vmclear(int cpu)
  1879. {
  1880. cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
  1881. }
  1882. static inline void crash_disable_local_vmclear(int cpu)
  1883. {
  1884. cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
  1885. }
  1886. static inline int crash_local_vmclear_enabled(int cpu)
  1887. {
  1888. return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
  1889. }
  1890. static void crash_vmclear_local_loaded_vmcss(void)
  1891. {
  1892. int cpu = raw_smp_processor_id();
  1893. struct loaded_vmcs *v;
  1894. if (!crash_local_vmclear_enabled(cpu))
  1895. return;
  1896. list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
  1897. loaded_vmcss_on_cpu_link)
  1898. vmcs_clear(v->vmcs);
  1899. }
  1900. #else
  1901. static inline void crash_enable_local_vmclear(int cpu) { }
  1902. static inline void crash_disable_local_vmclear(int cpu) { }
  1903. #endif /* CONFIG_KEXEC_CORE */
  1904. static void __loaded_vmcs_clear(void *arg)
  1905. {
  1906. struct loaded_vmcs *loaded_vmcs = arg;
  1907. int cpu = raw_smp_processor_id();
  1908. if (loaded_vmcs->cpu != cpu)
  1909. return; /* vcpu migration can race with cpu offline */
  1910. if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
  1911. per_cpu(current_vmcs, cpu) = NULL;
  1912. crash_disable_local_vmclear(cpu);
  1913. list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
  1914. /*
  1915. * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
  1916. * is before setting loaded_vmcs->vcpu to -1 which is done in
  1917. * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
  1918. * then adds the vmcs into percpu list before it is deleted.
  1919. */
  1920. smp_wmb();
  1921. loaded_vmcs_init(loaded_vmcs);
  1922. crash_enable_local_vmclear(cpu);
  1923. }
  1924. static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
  1925. {
  1926. int cpu = loaded_vmcs->cpu;
  1927. if (cpu != -1)
  1928. smp_call_function_single(cpu,
  1929. __loaded_vmcs_clear, loaded_vmcs, 1);
  1930. }
  1931. static inline bool vpid_sync_vcpu_addr(int vpid, gva_t addr)
  1932. {
  1933. if (vpid == 0)
  1934. return true;
  1935. if (cpu_has_vmx_invvpid_individual_addr()) {
  1936. __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, vpid, addr);
  1937. return true;
  1938. }
  1939. return false;
  1940. }
  1941. static inline void vpid_sync_vcpu_single(int vpid)
  1942. {
  1943. if (vpid == 0)
  1944. return;
  1945. if (cpu_has_vmx_invvpid_single())
  1946. __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
  1947. }
  1948. static inline void vpid_sync_vcpu_global(void)
  1949. {
  1950. if (cpu_has_vmx_invvpid_global())
  1951. __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
  1952. }
  1953. static inline void vpid_sync_context(int vpid)
  1954. {
  1955. if (cpu_has_vmx_invvpid_single())
  1956. vpid_sync_vcpu_single(vpid);
  1957. else
  1958. vpid_sync_vcpu_global();
  1959. }
  1960. static inline void ept_sync_global(void)
  1961. {
  1962. __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
  1963. }
  1964. static inline void ept_sync_context(u64 eptp)
  1965. {
  1966. if (cpu_has_vmx_invept_context())
  1967. __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
  1968. else
  1969. ept_sync_global();
  1970. }
  1971. static __always_inline void vmcs_check16(unsigned long field)
  1972. {
  1973. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
  1974. "16-bit accessor invalid for 64-bit field");
  1975. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
  1976. "16-bit accessor invalid for 64-bit high field");
  1977. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
  1978. "16-bit accessor invalid for 32-bit high field");
  1979. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
  1980. "16-bit accessor invalid for natural width field");
  1981. }
  1982. static __always_inline void vmcs_check32(unsigned long field)
  1983. {
  1984. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
  1985. "32-bit accessor invalid for 16-bit field");
  1986. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
  1987. "32-bit accessor invalid for natural width field");
  1988. }
  1989. static __always_inline void vmcs_check64(unsigned long field)
  1990. {
  1991. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
  1992. "64-bit accessor invalid for 16-bit field");
  1993. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
  1994. "64-bit accessor invalid for 64-bit high field");
  1995. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
  1996. "64-bit accessor invalid for 32-bit field");
  1997. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
  1998. "64-bit accessor invalid for natural width field");
  1999. }
  2000. static __always_inline void vmcs_checkl(unsigned long field)
  2001. {
  2002. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
  2003. "Natural width accessor invalid for 16-bit field");
  2004. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
  2005. "Natural width accessor invalid for 64-bit field");
  2006. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
  2007. "Natural width accessor invalid for 64-bit high field");
  2008. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
  2009. "Natural width accessor invalid for 32-bit field");
  2010. }
  2011. static __always_inline unsigned long __vmcs_readl(unsigned long field)
  2012. {
  2013. unsigned long value;
  2014. asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
  2015. : "=a"(value) : "d"(field) : "cc");
  2016. return value;
  2017. }
  2018. static __always_inline u16 vmcs_read16(unsigned long field)
  2019. {
  2020. vmcs_check16(field);
  2021. if (static_branch_unlikely(&enable_evmcs))
  2022. return evmcs_read16(field);
  2023. return __vmcs_readl(field);
  2024. }
  2025. static __always_inline u32 vmcs_read32(unsigned long field)
  2026. {
  2027. vmcs_check32(field);
  2028. if (static_branch_unlikely(&enable_evmcs))
  2029. return evmcs_read32(field);
  2030. return __vmcs_readl(field);
  2031. }
  2032. static __always_inline u64 vmcs_read64(unsigned long field)
  2033. {
  2034. vmcs_check64(field);
  2035. if (static_branch_unlikely(&enable_evmcs))
  2036. return evmcs_read64(field);
  2037. #ifdef CONFIG_X86_64
  2038. return __vmcs_readl(field);
  2039. #else
  2040. return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
  2041. #endif
  2042. }
  2043. static __always_inline unsigned long vmcs_readl(unsigned long field)
  2044. {
  2045. vmcs_checkl(field);
  2046. if (static_branch_unlikely(&enable_evmcs))
  2047. return evmcs_read64(field);
  2048. return __vmcs_readl(field);
  2049. }
  2050. static noinline void vmwrite_error(unsigned long field, unsigned long value)
  2051. {
  2052. printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
  2053. field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
  2054. dump_stack();
  2055. }
  2056. static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
  2057. {
  2058. bool error;
  2059. asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) CC_SET(na)
  2060. : CC_OUT(na) (error) : "a"(value), "d"(field));
  2061. if (unlikely(error))
  2062. vmwrite_error(field, value);
  2063. }
  2064. static __always_inline void vmcs_write16(unsigned long field, u16 value)
  2065. {
  2066. vmcs_check16(field);
  2067. if (static_branch_unlikely(&enable_evmcs))
  2068. return evmcs_write16(field, value);
  2069. __vmcs_writel(field, value);
  2070. }
  2071. static __always_inline void vmcs_write32(unsigned long field, u32 value)
  2072. {
  2073. vmcs_check32(field);
  2074. if (static_branch_unlikely(&enable_evmcs))
  2075. return evmcs_write32(field, value);
  2076. __vmcs_writel(field, value);
  2077. }
  2078. static __always_inline void vmcs_write64(unsigned long field, u64 value)
  2079. {
  2080. vmcs_check64(field);
  2081. if (static_branch_unlikely(&enable_evmcs))
  2082. return evmcs_write64(field, value);
  2083. __vmcs_writel(field, value);
  2084. #ifndef CONFIG_X86_64
  2085. asm volatile ("");
  2086. __vmcs_writel(field+1, value >> 32);
  2087. #endif
  2088. }
  2089. static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
  2090. {
  2091. vmcs_checkl(field);
  2092. if (static_branch_unlikely(&enable_evmcs))
  2093. return evmcs_write64(field, value);
  2094. __vmcs_writel(field, value);
  2095. }
  2096. static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
  2097. {
  2098. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
  2099. "vmcs_clear_bits does not support 64-bit fields");
  2100. if (static_branch_unlikely(&enable_evmcs))
  2101. return evmcs_write32(field, evmcs_read32(field) & ~mask);
  2102. __vmcs_writel(field, __vmcs_readl(field) & ~mask);
  2103. }
  2104. static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
  2105. {
  2106. BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
  2107. "vmcs_set_bits does not support 64-bit fields");
  2108. if (static_branch_unlikely(&enable_evmcs))
  2109. return evmcs_write32(field, evmcs_read32(field) | mask);
  2110. __vmcs_writel(field, __vmcs_readl(field) | mask);
  2111. }
  2112. static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
  2113. {
  2114. vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
  2115. }
  2116. static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
  2117. {
  2118. vmcs_write32(VM_ENTRY_CONTROLS, val);
  2119. vmx->vm_entry_controls_shadow = val;
  2120. }
  2121. static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
  2122. {
  2123. if (vmx->vm_entry_controls_shadow != val)
  2124. vm_entry_controls_init(vmx, val);
  2125. }
  2126. static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
  2127. {
  2128. return vmx->vm_entry_controls_shadow;
  2129. }
  2130. static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
  2131. {
  2132. vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
  2133. }
  2134. static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
  2135. {
  2136. vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
  2137. }
  2138. static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
  2139. {
  2140. vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
  2141. }
  2142. static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
  2143. {
  2144. vmcs_write32(VM_EXIT_CONTROLS, val);
  2145. vmx->vm_exit_controls_shadow = val;
  2146. }
  2147. static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
  2148. {
  2149. if (vmx->vm_exit_controls_shadow != val)
  2150. vm_exit_controls_init(vmx, val);
  2151. }
  2152. static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
  2153. {
  2154. return vmx->vm_exit_controls_shadow;
  2155. }
  2156. static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
  2157. {
  2158. vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
  2159. }
  2160. static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
  2161. {
  2162. vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
  2163. }
  2164. static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
  2165. {
  2166. vmx->segment_cache.bitmask = 0;
  2167. }
  2168. static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
  2169. unsigned field)
  2170. {
  2171. bool ret;
  2172. u32 mask = 1 << (seg * SEG_FIELD_NR + field);
  2173. if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
  2174. vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
  2175. vmx->segment_cache.bitmask = 0;
  2176. }
  2177. ret = vmx->segment_cache.bitmask & mask;
  2178. vmx->segment_cache.bitmask |= mask;
  2179. return ret;
  2180. }
  2181. static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
  2182. {
  2183. u16 *p = &vmx->segment_cache.seg[seg].selector;
  2184. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
  2185. *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
  2186. return *p;
  2187. }
  2188. static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
  2189. {
  2190. ulong *p = &vmx->segment_cache.seg[seg].base;
  2191. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
  2192. *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
  2193. return *p;
  2194. }
  2195. static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
  2196. {
  2197. u32 *p = &vmx->segment_cache.seg[seg].limit;
  2198. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
  2199. *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
  2200. return *p;
  2201. }
  2202. static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
  2203. {
  2204. u32 *p = &vmx->segment_cache.seg[seg].ar;
  2205. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
  2206. *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
  2207. return *p;
  2208. }
  2209. static void update_exception_bitmap(struct kvm_vcpu *vcpu)
  2210. {
  2211. u32 eb;
  2212. eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
  2213. (1u << DB_VECTOR) | (1u << AC_VECTOR);
  2214. /*
  2215. * Guest access to VMware backdoor ports could legitimately
  2216. * trigger #GP because of TSS I/O permission bitmap.
  2217. * We intercept those #GP and allow access to them anyway
  2218. * as VMware does.
  2219. */
  2220. if (enable_vmware_backdoor)
  2221. eb |= (1u << GP_VECTOR);
  2222. if ((vcpu->guest_debug &
  2223. (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
  2224. (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
  2225. eb |= 1u << BP_VECTOR;
  2226. if (to_vmx(vcpu)->rmode.vm86_active)
  2227. eb = ~0;
  2228. if (enable_ept)
  2229. eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
  2230. /* When we are running a nested L2 guest and L1 specified for it a
  2231. * certain exception bitmap, we must trap the same exceptions and pass
  2232. * them to L1. When running L2, we will only handle the exceptions
  2233. * specified above if L1 did not want them.
  2234. */
  2235. if (is_guest_mode(vcpu))
  2236. eb |= get_vmcs12(vcpu)->exception_bitmap;
  2237. vmcs_write32(EXCEPTION_BITMAP, eb);
  2238. }
  2239. /*
  2240. * Check if MSR is intercepted for currently loaded MSR bitmap.
  2241. */
  2242. static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
  2243. {
  2244. unsigned long *msr_bitmap;
  2245. int f = sizeof(unsigned long);
  2246. if (!cpu_has_vmx_msr_bitmap())
  2247. return true;
  2248. msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
  2249. if (msr <= 0x1fff) {
  2250. return !!test_bit(msr, msr_bitmap + 0x800 / f);
  2251. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  2252. msr &= 0x1fff;
  2253. return !!test_bit(msr, msr_bitmap + 0xc00 / f);
  2254. }
  2255. return true;
  2256. }
  2257. /*
  2258. * Check if MSR is intercepted for L01 MSR bitmap.
  2259. */
  2260. static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
  2261. {
  2262. unsigned long *msr_bitmap;
  2263. int f = sizeof(unsigned long);
  2264. if (!cpu_has_vmx_msr_bitmap())
  2265. return true;
  2266. msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
  2267. if (msr <= 0x1fff) {
  2268. return !!test_bit(msr, msr_bitmap + 0x800 / f);
  2269. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  2270. msr &= 0x1fff;
  2271. return !!test_bit(msr, msr_bitmap + 0xc00 / f);
  2272. }
  2273. return true;
  2274. }
  2275. static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
  2276. unsigned long entry, unsigned long exit)
  2277. {
  2278. vm_entry_controls_clearbit(vmx, entry);
  2279. vm_exit_controls_clearbit(vmx, exit);
  2280. }
  2281. static int find_msr(struct vmx_msrs *m, unsigned int msr)
  2282. {
  2283. unsigned int i;
  2284. for (i = 0; i < m->nr; ++i) {
  2285. if (m->val[i].index == msr)
  2286. return i;
  2287. }
  2288. return -ENOENT;
  2289. }
  2290. static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
  2291. {
  2292. int i;
  2293. struct msr_autoload *m = &vmx->msr_autoload;
  2294. switch (msr) {
  2295. case MSR_EFER:
  2296. if (cpu_has_load_ia32_efer) {
  2297. clear_atomic_switch_msr_special(vmx,
  2298. VM_ENTRY_LOAD_IA32_EFER,
  2299. VM_EXIT_LOAD_IA32_EFER);
  2300. return;
  2301. }
  2302. break;
  2303. case MSR_CORE_PERF_GLOBAL_CTRL:
  2304. if (cpu_has_load_perf_global_ctrl) {
  2305. clear_atomic_switch_msr_special(vmx,
  2306. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
  2307. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
  2308. return;
  2309. }
  2310. break;
  2311. }
  2312. i = find_msr(&m->guest, msr);
  2313. if (i < 0)
  2314. goto skip_guest;
  2315. --m->guest.nr;
  2316. m->guest.val[i] = m->guest.val[m->guest.nr];
  2317. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
  2318. skip_guest:
  2319. i = find_msr(&m->host, msr);
  2320. if (i < 0)
  2321. return;
  2322. --m->host.nr;
  2323. m->host.val[i] = m->host.val[m->host.nr];
  2324. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
  2325. }
  2326. static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
  2327. unsigned long entry, unsigned long exit,
  2328. unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
  2329. u64 guest_val, u64 host_val)
  2330. {
  2331. vmcs_write64(guest_val_vmcs, guest_val);
  2332. vmcs_write64(host_val_vmcs, host_val);
  2333. vm_entry_controls_setbit(vmx, entry);
  2334. vm_exit_controls_setbit(vmx, exit);
  2335. }
  2336. static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
  2337. u64 guest_val, u64 host_val, bool entry_only)
  2338. {
  2339. int i, j = 0;
  2340. struct msr_autoload *m = &vmx->msr_autoload;
  2341. switch (msr) {
  2342. case MSR_EFER:
  2343. if (cpu_has_load_ia32_efer) {
  2344. add_atomic_switch_msr_special(vmx,
  2345. VM_ENTRY_LOAD_IA32_EFER,
  2346. VM_EXIT_LOAD_IA32_EFER,
  2347. GUEST_IA32_EFER,
  2348. HOST_IA32_EFER,
  2349. guest_val, host_val);
  2350. return;
  2351. }
  2352. break;
  2353. case MSR_CORE_PERF_GLOBAL_CTRL:
  2354. if (cpu_has_load_perf_global_ctrl) {
  2355. add_atomic_switch_msr_special(vmx,
  2356. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
  2357. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
  2358. GUEST_IA32_PERF_GLOBAL_CTRL,
  2359. HOST_IA32_PERF_GLOBAL_CTRL,
  2360. guest_val, host_val);
  2361. return;
  2362. }
  2363. break;
  2364. case MSR_IA32_PEBS_ENABLE:
  2365. /* PEBS needs a quiescent period after being disabled (to write
  2366. * a record). Disabling PEBS through VMX MSR swapping doesn't
  2367. * provide that period, so a CPU could write host's record into
  2368. * guest's memory.
  2369. */
  2370. wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
  2371. }
  2372. i = find_msr(&m->guest, msr);
  2373. if (!entry_only)
  2374. j = find_msr(&m->host, msr);
  2375. if ((i < 0 && m->guest.nr == NR_AUTOLOAD_MSRS) ||
  2376. (j < 0 && m->host.nr == NR_AUTOLOAD_MSRS)) {
  2377. printk_once(KERN_WARNING "Not enough msr switch entries. "
  2378. "Can't add msr %x\n", msr);
  2379. return;
  2380. }
  2381. if (i < 0) {
  2382. i = m->guest.nr++;
  2383. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
  2384. }
  2385. m->guest.val[i].index = msr;
  2386. m->guest.val[i].value = guest_val;
  2387. if (entry_only)
  2388. return;
  2389. if (j < 0) {
  2390. j = m->host.nr++;
  2391. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
  2392. }
  2393. m->host.val[j].index = msr;
  2394. m->host.val[j].value = host_val;
  2395. }
  2396. static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
  2397. {
  2398. u64 guest_efer = vmx->vcpu.arch.efer;
  2399. u64 ignore_bits = 0;
  2400. /* Shadow paging assumes NX to be available. */
  2401. if (!enable_ept)
  2402. guest_efer |= EFER_NX;
  2403. /*
  2404. * LMA and LME handled by hardware; SCE meaningless outside long mode.
  2405. */
  2406. ignore_bits |= EFER_SCE;
  2407. #ifdef CONFIG_X86_64
  2408. ignore_bits |= EFER_LMA | EFER_LME;
  2409. /* SCE is meaningful only in long mode on Intel */
  2410. if (guest_efer & EFER_LMA)
  2411. ignore_bits &= ~(u64)EFER_SCE;
  2412. #endif
  2413. clear_atomic_switch_msr(vmx, MSR_EFER);
  2414. /*
  2415. * On EPT, we can't emulate NX, so we must switch EFER atomically.
  2416. * On CPUs that support "load IA32_EFER", always switch EFER
  2417. * atomically, since it's faster than switching it manually.
  2418. */
  2419. if (cpu_has_load_ia32_efer ||
  2420. (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
  2421. if (!(guest_efer & EFER_LMA))
  2422. guest_efer &= ~EFER_LME;
  2423. if (guest_efer != host_efer)
  2424. add_atomic_switch_msr(vmx, MSR_EFER,
  2425. guest_efer, host_efer, false);
  2426. return false;
  2427. } else {
  2428. guest_efer &= ~ignore_bits;
  2429. guest_efer |= host_efer & ignore_bits;
  2430. vmx->guest_msrs[efer_offset].data = guest_efer;
  2431. vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
  2432. return true;
  2433. }
  2434. }
  2435. #ifdef CONFIG_X86_32
  2436. /*
  2437. * On 32-bit kernels, VM exits still load the FS and GS bases from the
  2438. * VMCS rather than the segment table. KVM uses this helper to figure
  2439. * out the current bases to poke them into the VMCS before entry.
  2440. */
  2441. static unsigned long segment_base(u16 selector)
  2442. {
  2443. struct desc_struct *table;
  2444. unsigned long v;
  2445. if (!(selector & ~SEGMENT_RPL_MASK))
  2446. return 0;
  2447. table = get_current_gdt_ro();
  2448. if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
  2449. u16 ldt_selector = kvm_read_ldt();
  2450. if (!(ldt_selector & ~SEGMENT_RPL_MASK))
  2451. return 0;
  2452. table = (struct desc_struct *)segment_base(ldt_selector);
  2453. }
  2454. v = get_desc_base(&table[selector >> 3]);
  2455. return v;
  2456. }
  2457. #endif
  2458. static void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
  2459. {
  2460. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2461. struct vmcs_host_state *host_state;
  2462. #ifdef CONFIG_X86_64
  2463. int cpu = raw_smp_processor_id();
  2464. #endif
  2465. unsigned long fs_base, gs_base;
  2466. u16 fs_sel, gs_sel;
  2467. int i;
  2468. vmx->req_immediate_exit = false;
  2469. /*
  2470. * Note that guest MSRs to be saved/restored can also be changed
  2471. * when guest state is loaded. This happens when guest transitions
  2472. * to/from long-mode by setting MSR_EFER.LMA.
  2473. */
  2474. if (!vmx->loaded_cpu_state || vmx->guest_msrs_dirty) {
  2475. vmx->guest_msrs_dirty = false;
  2476. for (i = 0; i < vmx->save_nmsrs; ++i)
  2477. kvm_set_shared_msr(vmx->guest_msrs[i].index,
  2478. vmx->guest_msrs[i].data,
  2479. vmx->guest_msrs[i].mask);
  2480. }
  2481. if (vmx->loaded_cpu_state)
  2482. return;
  2483. vmx->loaded_cpu_state = vmx->loaded_vmcs;
  2484. host_state = &vmx->loaded_cpu_state->host_state;
  2485. /*
  2486. * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
  2487. * allow segment selectors with cpl > 0 or ti == 1.
  2488. */
  2489. host_state->ldt_sel = kvm_read_ldt();
  2490. #ifdef CONFIG_X86_64
  2491. savesegment(ds, host_state->ds_sel);
  2492. savesegment(es, host_state->es_sel);
  2493. gs_base = cpu_kernelmode_gs_base(cpu);
  2494. if (likely(is_64bit_mm(current->mm))) {
  2495. save_fsgs_for_kvm();
  2496. fs_sel = current->thread.fsindex;
  2497. gs_sel = current->thread.gsindex;
  2498. fs_base = current->thread.fsbase;
  2499. vmx->msr_host_kernel_gs_base = current->thread.gsbase;
  2500. } else {
  2501. savesegment(fs, fs_sel);
  2502. savesegment(gs, gs_sel);
  2503. fs_base = read_msr(MSR_FS_BASE);
  2504. vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
  2505. }
  2506. wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
  2507. #else
  2508. savesegment(fs, fs_sel);
  2509. savesegment(gs, gs_sel);
  2510. fs_base = segment_base(fs_sel);
  2511. gs_base = segment_base(gs_sel);
  2512. #endif
  2513. if (unlikely(fs_sel != host_state->fs_sel)) {
  2514. if (!(fs_sel & 7))
  2515. vmcs_write16(HOST_FS_SELECTOR, fs_sel);
  2516. else
  2517. vmcs_write16(HOST_FS_SELECTOR, 0);
  2518. host_state->fs_sel = fs_sel;
  2519. }
  2520. if (unlikely(gs_sel != host_state->gs_sel)) {
  2521. if (!(gs_sel & 7))
  2522. vmcs_write16(HOST_GS_SELECTOR, gs_sel);
  2523. else
  2524. vmcs_write16(HOST_GS_SELECTOR, 0);
  2525. host_state->gs_sel = gs_sel;
  2526. }
  2527. if (unlikely(fs_base != host_state->fs_base)) {
  2528. vmcs_writel(HOST_FS_BASE, fs_base);
  2529. host_state->fs_base = fs_base;
  2530. }
  2531. if (unlikely(gs_base != host_state->gs_base)) {
  2532. vmcs_writel(HOST_GS_BASE, gs_base);
  2533. host_state->gs_base = gs_base;
  2534. }
  2535. }
  2536. static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
  2537. {
  2538. struct vmcs_host_state *host_state;
  2539. if (!vmx->loaded_cpu_state)
  2540. return;
  2541. WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs);
  2542. host_state = &vmx->loaded_cpu_state->host_state;
  2543. ++vmx->vcpu.stat.host_state_reload;
  2544. vmx->loaded_cpu_state = NULL;
  2545. #ifdef CONFIG_X86_64
  2546. rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
  2547. #endif
  2548. if (host_state->ldt_sel || (host_state->gs_sel & 7)) {
  2549. kvm_load_ldt(host_state->ldt_sel);
  2550. #ifdef CONFIG_X86_64
  2551. load_gs_index(host_state->gs_sel);
  2552. #else
  2553. loadsegment(gs, host_state->gs_sel);
  2554. #endif
  2555. }
  2556. if (host_state->fs_sel & 7)
  2557. loadsegment(fs, host_state->fs_sel);
  2558. #ifdef CONFIG_X86_64
  2559. if (unlikely(host_state->ds_sel | host_state->es_sel)) {
  2560. loadsegment(ds, host_state->ds_sel);
  2561. loadsegment(es, host_state->es_sel);
  2562. }
  2563. #endif
  2564. invalidate_tss_limit();
  2565. #ifdef CONFIG_X86_64
  2566. wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
  2567. #endif
  2568. load_fixmap_gdt(raw_smp_processor_id());
  2569. }
  2570. #ifdef CONFIG_X86_64
  2571. static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
  2572. {
  2573. preempt_disable();
  2574. if (vmx->loaded_cpu_state)
  2575. rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
  2576. preempt_enable();
  2577. return vmx->msr_guest_kernel_gs_base;
  2578. }
  2579. static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
  2580. {
  2581. preempt_disable();
  2582. if (vmx->loaded_cpu_state)
  2583. wrmsrl(MSR_KERNEL_GS_BASE, data);
  2584. preempt_enable();
  2585. vmx->msr_guest_kernel_gs_base = data;
  2586. }
  2587. #endif
  2588. static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
  2589. {
  2590. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  2591. struct pi_desc old, new;
  2592. unsigned int dest;
  2593. /*
  2594. * In case of hot-plug or hot-unplug, we may have to undo
  2595. * vmx_vcpu_pi_put even if there is no assigned device. And we
  2596. * always keep PI.NDST up to date for simplicity: it makes the
  2597. * code easier, and CPU migration is not a fast path.
  2598. */
  2599. if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
  2600. return;
  2601. /*
  2602. * First handle the simple case where no cmpxchg is necessary; just
  2603. * allow posting non-urgent interrupts.
  2604. *
  2605. * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
  2606. * PI.NDST: pi_post_block will do it for us and the wakeup_handler
  2607. * expects the VCPU to be on the blocked_vcpu_list that matches
  2608. * PI.NDST.
  2609. */
  2610. if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
  2611. vcpu->cpu == cpu) {
  2612. pi_clear_sn(pi_desc);
  2613. return;
  2614. }
  2615. /* The full case. */
  2616. do {
  2617. old.control = new.control = pi_desc->control;
  2618. dest = cpu_physical_id(cpu);
  2619. if (x2apic_enabled())
  2620. new.ndst = dest;
  2621. else
  2622. new.ndst = (dest << 8) & 0xFF00;
  2623. new.sn = 0;
  2624. } while (cmpxchg64(&pi_desc->control, old.control,
  2625. new.control) != old.control);
  2626. }
  2627. static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
  2628. {
  2629. vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
  2630. vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
  2631. }
  2632. /*
  2633. * Switches to specified vcpu, until a matching vcpu_put(), but assumes
  2634. * vcpu mutex is already taken.
  2635. */
  2636. static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  2637. {
  2638. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2639. bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
  2640. if (!already_loaded) {
  2641. loaded_vmcs_clear(vmx->loaded_vmcs);
  2642. local_irq_disable();
  2643. crash_disable_local_vmclear(cpu);
  2644. /*
  2645. * Read loaded_vmcs->cpu should be before fetching
  2646. * loaded_vmcs->loaded_vmcss_on_cpu_link.
  2647. * See the comments in __loaded_vmcs_clear().
  2648. */
  2649. smp_rmb();
  2650. list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
  2651. &per_cpu(loaded_vmcss_on_cpu, cpu));
  2652. crash_enable_local_vmclear(cpu);
  2653. local_irq_enable();
  2654. }
  2655. if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
  2656. per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
  2657. vmcs_load(vmx->loaded_vmcs->vmcs);
  2658. indirect_branch_prediction_barrier();
  2659. }
  2660. if (!already_loaded) {
  2661. void *gdt = get_current_gdt_ro();
  2662. unsigned long sysenter_esp;
  2663. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  2664. /*
  2665. * Linux uses per-cpu TSS and GDT, so set these when switching
  2666. * processors. See 22.2.4.
  2667. */
  2668. vmcs_writel(HOST_TR_BASE,
  2669. (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
  2670. vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
  2671. /*
  2672. * VM exits change the host TR limit to 0x67 after a VM
  2673. * exit. This is okay, since 0x67 covers everything except
  2674. * the IO bitmap and have have code to handle the IO bitmap
  2675. * being lost after a VM exit.
  2676. */
  2677. BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
  2678. rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
  2679. vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
  2680. vmx->loaded_vmcs->cpu = cpu;
  2681. }
  2682. /* Setup TSC multiplier */
  2683. if (kvm_has_tsc_control &&
  2684. vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
  2685. decache_tsc_multiplier(vmx);
  2686. vmx_vcpu_pi_load(vcpu, cpu);
  2687. vmx->host_pkru = read_pkru();
  2688. vmx->host_debugctlmsr = get_debugctlmsr();
  2689. }
  2690. static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
  2691. {
  2692. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  2693. if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
  2694. !irq_remapping_cap(IRQ_POSTING_CAP) ||
  2695. !kvm_vcpu_apicv_active(vcpu))
  2696. return;
  2697. /* Set SN when the vCPU is preempted */
  2698. if (vcpu->preempted)
  2699. pi_set_sn(pi_desc);
  2700. }
  2701. static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
  2702. {
  2703. vmx_vcpu_pi_put(vcpu);
  2704. vmx_prepare_switch_to_host(to_vmx(vcpu));
  2705. }
  2706. static bool emulation_required(struct kvm_vcpu *vcpu)
  2707. {
  2708. return emulate_invalid_guest_state && !guest_state_valid(vcpu);
  2709. }
  2710. static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
  2711. /*
  2712. * Return the cr0 value that a nested guest would read. This is a combination
  2713. * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
  2714. * its hypervisor (cr0_read_shadow).
  2715. */
  2716. static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
  2717. {
  2718. return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
  2719. (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
  2720. }
  2721. static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
  2722. {
  2723. return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
  2724. (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
  2725. }
  2726. static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
  2727. {
  2728. unsigned long rflags, save_rflags;
  2729. if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
  2730. __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
  2731. rflags = vmcs_readl(GUEST_RFLAGS);
  2732. if (to_vmx(vcpu)->rmode.vm86_active) {
  2733. rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
  2734. save_rflags = to_vmx(vcpu)->rmode.save_rflags;
  2735. rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
  2736. }
  2737. to_vmx(vcpu)->rflags = rflags;
  2738. }
  2739. return to_vmx(vcpu)->rflags;
  2740. }
  2741. static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  2742. {
  2743. unsigned long old_rflags = vmx_get_rflags(vcpu);
  2744. __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
  2745. to_vmx(vcpu)->rflags = rflags;
  2746. if (to_vmx(vcpu)->rmode.vm86_active) {
  2747. to_vmx(vcpu)->rmode.save_rflags = rflags;
  2748. rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
  2749. }
  2750. vmcs_writel(GUEST_RFLAGS, rflags);
  2751. if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
  2752. to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
  2753. }
  2754. static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
  2755. {
  2756. u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  2757. int ret = 0;
  2758. if (interruptibility & GUEST_INTR_STATE_STI)
  2759. ret |= KVM_X86_SHADOW_INT_STI;
  2760. if (interruptibility & GUEST_INTR_STATE_MOV_SS)
  2761. ret |= KVM_X86_SHADOW_INT_MOV_SS;
  2762. return ret;
  2763. }
  2764. static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
  2765. {
  2766. u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  2767. u32 interruptibility = interruptibility_old;
  2768. interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
  2769. if (mask & KVM_X86_SHADOW_INT_MOV_SS)
  2770. interruptibility |= GUEST_INTR_STATE_MOV_SS;
  2771. else if (mask & KVM_X86_SHADOW_INT_STI)
  2772. interruptibility |= GUEST_INTR_STATE_STI;
  2773. if ((interruptibility != interruptibility_old))
  2774. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
  2775. }
  2776. static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
  2777. {
  2778. unsigned long rip;
  2779. rip = kvm_rip_read(vcpu);
  2780. rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  2781. kvm_rip_write(vcpu, rip);
  2782. /* skipping an emulated instruction also counts */
  2783. vmx_set_interrupt_shadow(vcpu, 0);
  2784. }
  2785. static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
  2786. unsigned long exit_qual)
  2787. {
  2788. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  2789. unsigned int nr = vcpu->arch.exception.nr;
  2790. u32 intr_info = nr | INTR_INFO_VALID_MASK;
  2791. if (vcpu->arch.exception.has_error_code) {
  2792. vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
  2793. intr_info |= INTR_INFO_DELIVER_CODE_MASK;
  2794. }
  2795. if (kvm_exception_is_soft(nr))
  2796. intr_info |= INTR_TYPE_SOFT_EXCEPTION;
  2797. else
  2798. intr_info |= INTR_TYPE_HARD_EXCEPTION;
  2799. if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
  2800. vmx_get_nmi_mask(vcpu))
  2801. intr_info |= INTR_INFO_UNBLOCK_NMI;
  2802. nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
  2803. }
  2804. /*
  2805. * KVM wants to inject page-faults which it got to the guest. This function
  2806. * checks whether in a nested guest, we need to inject them to L1 or L2.
  2807. */
  2808. static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
  2809. {
  2810. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  2811. unsigned int nr = vcpu->arch.exception.nr;
  2812. if (nr == PF_VECTOR) {
  2813. if (vcpu->arch.exception.nested_apf) {
  2814. *exit_qual = vcpu->arch.apf.nested_apf_token;
  2815. return 1;
  2816. }
  2817. /*
  2818. * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
  2819. * The fix is to add the ancillary datum (CR2 or DR6) to structs
  2820. * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
  2821. * can be written only when inject_pending_event runs. This should be
  2822. * conditional on a new capability---if the capability is disabled,
  2823. * kvm_multiple_exception would write the ancillary information to
  2824. * CR2 or DR6, for backwards ABI-compatibility.
  2825. */
  2826. if (nested_vmx_is_page_fault_vmexit(vmcs12,
  2827. vcpu->arch.exception.error_code)) {
  2828. *exit_qual = vcpu->arch.cr2;
  2829. return 1;
  2830. }
  2831. } else {
  2832. if (vmcs12->exception_bitmap & (1u << nr)) {
  2833. if (nr == DB_VECTOR) {
  2834. *exit_qual = vcpu->arch.dr6;
  2835. *exit_qual &= ~(DR6_FIXED_1 | DR6_BT);
  2836. *exit_qual ^= DR6_RTM;
  2837. } else {
  2838. *exit_qual = 0;
  2839. }
  2840. return 1;
  2841. }
  2842. }
  2843. return 0;
  2844. }
  2845. static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
  2846. {
  2847. /*
  2848. * Ensure that we clear the HLT state in the VMCS. We don't need to
  2849. * explicitly skip the instruction because if the HLT state is set,
  2850. * then the instruction is already executing and RIP has already been
  2851. * advanced.
  2852. */
  2853. if (kvm_hlt_in_guest(vcpu->kvm) &&
  2854. vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
  2855. vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
  2856. }
  2857. static void vmx_queue_exception(struct kvm_vcpu *vcpu)
  2858. {
  2859. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2860. unsigned nr = vcpu->arch.exception.nr;
  2861. bool has_error_code = vcpu->arch.exception.has_error_code;
  2862. u32 error_code = vcpu->arch.exception.error_code;
  2863. u32 intr_info = nr | INTR_INFO_VALID_MASK;
  2864. if (has_error_code) {
  2865. vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
  2866. intr_info |= INTR_INFO_DELIVER_CODE_MASK;
  2867. }
  2868. if (vmx->rmode.vm86_active) {
  2869. int inc_eip = 0;
  2870. if (kvm_exception_is_soft(nr))
  2871. inc_eip = vcpu->arch.event_exit_inst_len;
  2872. if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
  2873. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  2874. return;
  2875. }
  2876. WARN_ON_ONCE(vmx->emulation_required);
  2877. if (kvm_exception_is_soft(nr)) {
  2878. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  2879. vmx->vcpu.arch.event_exit_inst_len);
  2880. intr_info |= INTR_TYPE_SOFT_EXCEPTION;
  2881. } else
  2882. intr_info |= INTR_TYPE_HARD_EXCEPTION;
  2883. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
  2884. vmx_clear_hlt(vcpu);
  2885. }
  2886. static bool vmx_rdtscp_supported(void)
  2887. {
  2888. return cpu_has_vmx_rdtscp();
  2889. }
  2890. static bool vmx_invpcid_supported(void)
  2891. {
  2892. return cpu_has_vmx_invpcid();
  2893. }
  2894. /*
  2895. * Swap MSR entry in host/guest MSR entry array.
  2896. */
  2897. static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
  2898. {
  2899. struct shared_msr_entry tmp;
  2900. tmp = vmx->guest_msrs[to];
  2901. vmx->guest_msrs[to] = vmx->guest_msrs[from];
  2902. vmx->guest_msrs[from] = tmp;
  2903. }
  2904. /*
  2905. * Set up the vmcs to automatically save and restore system
  2906. * msrs. Don't touch the 64-bit msrs if the guest is in legacy
  2907. * mode, as fiddling with msrs is very expensive.
  2908. */
  2909. static void setup_msrs(struct vcpu_vmx *vmx)
  2910. {
  2911. int save_nmsrs, index;
  2912. save_nmsrs = 0;
  2913. #ifdef CONFIG_X86_64
  2914. if (is_long_mode(&vmx->vcpu)) {
  2915. index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
  2916. if (index >= 0)
  2917. move_msr_up(vmx, index, save_nmsrs++);
  2918. index = __find_msr_index(vmx, MSR_LSTAR);
  2919. if (index >= 0)
  2920. move_msr_up(vmx, index, save_nmsrs++);
  2921. index = __find_msr_index(vmx, MSR_CSTAR);
  2922. if (index >= 0)
  2923. move_msr_up(vmx, index, save_nmsrs++);
  2924. /*
  2925. * MSR_STAR is only needed on long mode guests, and only
  2926. * if efer.sce is enabled.
  2927. */
  2928. index = __find_msr_index(vmx, MSR_STAR);
  2929. if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
  2930. move_msr_up(vmx, index, save_nmsrs++);
  2931. }
  2932. #endif
  2933. index = __find_msr_index(vmx, MSR_EFER);
  2934. if (index >= 0 && update_transition_efer(vmx, index))
  2935. move_msr_up(vmx, index, save_nmsrs++);
  2936. index = __find_msr_index(vmx, MSR_TSC_AUX);
  2937. if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
  2938. move_msr_up(vmx, index, save_nmsrs++);
  2939. vmx->save_nmsrs = save_nmsrs;
  2940. vmx->guest_msrs_dirty = true;
  2941. if (cpu_has_vmx_msr_bitmap())
  2942. vmx_update_msr_bitmap(&vmx->vcpu);
  2943. }
  2944. static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
  2945. {
  2946. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  2947. if (is_guest_mode(vcpu) &&
  2948. (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
  2949. return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
  2950. return vcpu->arch.tsc_offset;
  2951. }
  2952. static u64 vmx_write_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
  2953. {
  2954. u64 active_offset = offset;
  2955. if (is_guest_mode(vcpu)) {
  2956. /*
  2957. * We're here if L1 chose not to trap WRMSR to TSC. According
  2958. * to the spec, this should set L1's TSC; The offset that L1
  2959. * set for L2 remains unchanged, and still needs to be added
  2960. * to the newly set TSC to get L2's TSC.
  2961. */
  2962. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  2963. if (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING))
  2964. active_offset += vmcs12->tsc_offset;
  2965. } else {
  2966. trace_kvm_write_tsc_offset(vcpu->vcpu_id,
  2967. vmcs_read64(TSC_OFFSET), offset);
  2968. }
  2969. vmcs_write64(TSC_OFFSET, active_offset);
  2970. return active_offset;
  2971. }
  2972. /*
  2973. * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
  2974. * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
  2975. * all guests if the "nested" module option is off, and can also be disabled
  2976. * for a single guest by disabling its VMX cpuid bit.
  2977. */
  2978. static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
  2979. {
  2980. return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
  2981. }
  2982. /*
  2983. * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
  2984. * returned for the various VMX controls MSRs when nested VMX is enabled.
  2985. * The same values should also be used to verify that vmcs12 control fields are
  2986. * valid during nested entry from L1 to L2.
  2987. * Each of these control msrs has a low and high 32-bit half: A low bit is on
  2988. * if the corresponding bit in the (32-bit) control field *must* be on, and a
  2989. * bit in the high half is on if the corresponding bit in the control field
  2990. * may be on. See also vmx_control_verify().
  2991. */
  2992. static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
  2993. {
  2994. if (!nested) {
  2995. memset(msrs, 0, sizeof(*msrs));
  2996. return;
  2997. }
  2998. /*
  2999. * Note that as a general rule, the high half of the MSRs (bits in
  3000. * the control fields which may be 1) should be initialized by the
  3001. * intersection of the underlying hardware's MSR (i.e., features which
  3002. * can be supported) and the list of features we want to expose -
  3003. * because they are known to be properly supported in our code.
  3004. * Also, usually, the low half of the MSRs (bits which must be 1) can
  3005. * be set to 0, meaning that L1 may turn off any of these bits. The
  3006. * reason is that if one of these bits is necessary, it will appear
  3007. * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
  3008. * fields of vmcs01 and vmcs02, will turn these bits off - and
  3009. * nested_vmx_exit_reflected() will not pass related exits to L1.
  3010. * These rules have exceptions below.
  3011. */
  3012. /* pin-based controls */
  3013. rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
  3014. msrs->pinbased_ctls_low,
  3015. msrs->pinbased_ctls_high);
  3016. msrs->pinbased_ctls_low |=
  3017. PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  3018. msrs->pinbased_ctls_high &=
  3019. PIN_BASED_EXT_INTR_MASK |
  3020. PIN_BASED_NMI_EXITING |
  3021. PIN_BASED_VIRTUAL_NMIS |
  3022. (apicv ? PIN_BASED_POSTED_INTR : 0);
  3023. msrs->pinbased_ctls_high |=
  3024. PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
  3025. PIN_BASED_VMX_PREEMPTION_TIMER;
  3026. /* exit controls */
  3027. rdmsr(MSR_IA32_VMX_EXIT_CTLS,
  3028. msrs->exit_ctls_low,
  3029. msrs->exit_ctls_high);
  3030. msrs->exit_ctls_low =
  3031. VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
  3032. msrs->exit_ctls_high &=
  3033. #ifdef CONFIG_X86_64
  3034. VM_EXIT_HOST_ADDR_SPACE_SIZE |
  3035. #endif
  3036. VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
  3037. msrs->exit_ctls_high |=
  3038. VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
  3039. VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
  3040. VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
  3041. /* We support free control of debug control saving. */
  3042. msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
  3043. /* entry controls */
  3044. rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
  3045. msrs->entry_ctls_low,
  3046. msrs->entry_ctls_high);
  3047. msrs->entry_ctls_low =
  3048. VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
  3049. msrs->entry_ctls_high &=
  3050. #ifdef CONFIG_X86_64
  3051. VM_ENTRY_IA32E_MODE |
  3052. #endif
  3053. VM_ENTRY_LOAD_IA32_PAT;
  3054. msrs->entry_ctls_high |=
  3055. (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
  3056. /* We support free control of debug control loading. */
  3057. msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
  3058. /* cpu-based controls */
  3059. rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
  3060. msrs->procbased_ctls_low,
  3061. msrs->procbased_ctls_high);
  3062. msrs->procbased_ctls_low =
  3063. CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  3064. msrs->procbased_ctls_high &=
  3065. CPU_BASED_VIRTUAL_INTR_PENDING |
  3066. CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
  3067. CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
  3068. CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
  3069. CPU_BASED_CR3_STORE_EXITING |
  3070. #ifdef CONFIG_X86_64
  3071. CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
  3072. #endif
  3073. CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
  3074. CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
  3075. CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
  3076. CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
  3077. CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  3078. /*
  3079. * We can allow some features even when not supported by the
  3080. * hardware. For example, L1 can specify an MSR bitmap - and we
  3081. * can use it to avoid exits to L1 - even when L0 runs L2
  3082. * without MSR bitmaps.
  3083. */
  3084. msrs->procbased_ctls_high |=
  3085. CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
  3086. CPU_BASED_USE_MSR_BITMAPS;
  3087. /* We support free control of CR3 access interception. */
  3088. msrs->procbased_ctls_low &=
  3089. ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
  3090. /*
  3091. * secondary cpu-based controls. Do not include those that
  3092. * depend on CPUID bits, they are added later by vmx_cpuid_update.
  3093. */
  3094. if (msrs->procbased_ctls_high & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)
  3095. rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
  3096. msrs->secondary_ctls_low,
  3097. msrs->secondary_ctls_high);
  3098. msrs->secondary_ctls_low = 0;
  3099. msrs->secondary_ctls_high &=
  3100. SECONDARY_EXEC_DESC |
  3101. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  3102. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  3103. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
  3104. SECONDARY_EXEC_WBINVD_EXITING;
  3105. /*
  3106. * We can emulate "VMCS shadowing," even if the hardware
  3107. * doesn't support it.
  3108. */
  3109. msrs->secondary_ctls_high |=
  3110. SECONDARY_EXEC_SHADOW_VMCS;
  3111. if (enable_ept) {
  3112. /* nested EPT: emulate EPT also to L1 */
  3113. msrs->secondary_ctls_high |=
  3114. SECONDARY_EXEC_ENABLE_EPT;
  3115. msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
  3116. VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
  3117. if (cpu_has_vmx_ept_execute_only())
  3118. msrs->ept_caps |=
  3119. VMX_EPT_EXECUTE_ONLY_BIT;
  3120. msrs->ept_caps &= vmx_capability.ept;
  3121. msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
  3122. VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
  3123. VMX_EPT_1GB_PAGE_BIT;
  3124. if (enable_ept_ad_bits) {
  3125. msrs->secondary_ctls_high |=
  3126. SECONDARY_EXEC_ENABLE_PML;
  3127. msrs->ept_caps |= VMX_EPT_AD_BIT;
  3128. }
  3129. }
  3130. if (cpu_has_vmx_vmfunc()) {
  3131. msrs->secondary_ctls_high |=
  3132. SECONDARY_EXEC_ENABLE_VMFUNC;
  3133. /*
  3134. * Advertise EPTP switching unconditionally
  3135. * since we emulate it
  3136. */
  3137. if (enable_ept)
  3138. msrs->vmfunc_controls =
  3139. VMX_VMFUNC_EPTP_SWITCHING;
  3140. }
  3141. /*
  3142. * Old versions of KVM use the single-context version without
  3143. * checking for support, so declare that it is supported even
  3144. * though it is treated as global context. The alternative is
  3145. * not failing the single-context invvpid, and it is worse.
  3146. */
  3147. if (enable_vpid) {
  3148. msrs->secondary_ctls_high |=
  3149. SECONDARY_EXEC_ENABLE_VPID;
  3150. msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
  3151. VMX_VPID_EXTENT_SUPPORTED_MASK;
  3152. }
  3153. if (enable_unrestricted_guest)
  3154. msrs->secondary_ctls_high |=
  3155. SECONDARY_EXEC_UNRESTRICTED_GUEST;
  3156. if (flexpriority_enabled)
  3157. msrs->secondary_ctls_high |=
  3158. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  3159. /* miscellaneous data */
  3160. rdmsr(MSR_IA32_VMX_MISC,
  3161. msrs->misc_low,
  3162. msrs->misc_high);
  3163. msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
  3164. msrs->misc_low |=
  3165. MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
  3166. VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
  3167. VMX_MISC_ACTIVITY_HLT;
  3168. msrs->misc_high = 0;
  3169. /*
  3170. * This MSR reports some information about VMX support. We
  3171. * should return information about the VMX we emulate for the
  3172. * guest, and the VMCS structure we give it - not about the
  3173. * VMX support of the underlying hardware.
  3174. */
  3175. msrs->basic =
  3176. VMCS12_REVISION |
  3177. VMX_BASIC_TRUE_CTLS |
  3178. ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
  3179. (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
  3180. if (cpu_has_vmx_basic_inout())
  3181. msrs->basic |= VMX_BASIC_INOUT;
  3182. /*
  3183. * These MSRs specify bits which the guest must keep fixed on
  3184. * while L1 is in VMXON mode (in L1's root mode, or running an L2).
  3185. * We picked the standard core2 setting.
  3186. */
  3187. #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
  3188. #define VMXON_CR4_ALWAYSON X86_CR4_VMXE
  3189. msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
  3190. msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
  3191. /* These MSRs specify bits which the guest must keep fixed off. */
  3192. rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
  3193. rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
  3194. /* highest index: VMX_PREEMPTION_TIMER_VALUE */
  3195. msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
  3196. }
  3197. /*
  3198. * if fixed0[i] == 1: val[i] must be 1
  3199. * if fixed1[i] == 0: val[i] must be 0
  3200. */
  3201. static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
  3202. {
  3203. return ((val & fixed1) | fixed0) == val;
  3204. }
  3205. static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
  3206. {
  3207. return fixed_bits_valid(control, low, high);
  3208. }
  3209. static inline u64 vmx_control_msr(u32 low, u32 high)
  3210. {
  3211. return low | ((u64)high << 32);
  3212. }
  3213. static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
  3214. {
  3215. superset &= mask;
  3216. subset &= mask;
  3217. return (superset | subset) == superset;
  3218. }
  3219. static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
  3220. {
  3221. const u64 feature_and_reserved =
  3222. /* feature (except bit 48; see below) */
  3223. BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
  3224. /* reserved */
  3225. BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
  3226. u64 vmx_basic = vmx->nested.msrs.basic;
  3227. if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
  3228. return -EINVAL;
  3229. /*
  3230. * KVM does not emulate a version of VMX that constrains physical
  3231. * addresses of VMX structures (e.g. VMCS) to 32-bits.
  3232. */
  3233. if (data & BIT_ULL(48))
  3234. return -EINVAL;
  3235. if (vmx_basic_vmcs_revision_id(vmx_basic) !=
  3236. vmx_basic_vmcs_revision_id(data))
  3237. return -EINVAL;
  3238. if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
  3239. return -EINVAL;
  3240. vmx->nested.msrs.basic = data;
  3241. return 0;
  3242. }
  3243. static int
  3244. vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
  3245. {
  3246. u64 supported;
  3247. u32 *lowp, *highp;
  3248. switch (msr_index) {
  3249. case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
  3250. lowp = &vmx->nested.msrs.pinbased_ctls_low;
  3251. highp = &vmx->nested.msrs.pinbased_ctls_high;
  3252. break;
  3253. case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
  3254. lowp = &vmx->nested.msrs.procbased_ctls_low;
  3255. highp = &vmx->nested.msrs.procbased_ctls_high;
  3256. break;
  3257. case MSR_IA32_VMX_TRUE_EXIT_CTLS:
  3258. lowp = &vmx->nested.msrs.exit_ctls_low;
  3259. highp = &vmx->nested.msrs.exit_ctls_high;
  3260. break;
  3261. case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
  3262. lowp = &vmx->nested.msrs.entry_ctls_low;
  3263. highp = &vmx->nested.msrs.entry_ctls_high;
  3264. break;
  3265. case MSR_IA32_VMX_PROCBASED_CTLS2:
  3266. lowp = &vmx->nested.msrs.secondary_ctls_low;
  3267. highp = &vmx->nested.msrs.secondary_ctls_high;
  3268. break;
  3269. default:
  3270. BUG();
  3271. }
  3272. supported = vmx_control_msr(*lowp, *highp);
  3273. /* Check must-be-1 bits are still 1. */
  3274. if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
  3275. return -EINVAL;
  3276. /* Check must-be-0 bits are still 0. */
  3277. if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
  3278. return -EINVAL;
  3279. *lowp = data;
  3280. *highp = data >> 32;
  3281. return 0;
  3282. }
  3283. static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
  3284. {
  3285. const u64 feature_and_reserved_bits =
  3286. /* feature */
  3287. BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
  3288. BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
  3289. /* reserved */
  3290. GENMASK_ULL(13, 9) | BIT_ULL(31);
  3291. u64 vmx_misc;
  3292. vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
  3293. vmx->nested.msrs.misc_high);
  3294. if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
  3295. return -EINVAL;
  3296. if ((vmx->nested.msrs.pinbased_ctls_high &
  3297. PIN_BASED_VMX_PREEMPTION_TIMER) &&
  3298. vmx_misc_preemption_timer_rate(data) !=
  3299. vmx_misc_preemption_timer_rate(vmx_misc))
  3300. return -EINVAL;
  3301. if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
  3302. return -EINVAL;
  3303. if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
  3304. return -EINVAL;
  3305. if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
  3306. return -EINVAL;
  3307. vmx->nested.msrs.misc_low = data;
  3308. vmx->nested.msrs.misc_high = data >> 32;
  3309. /*
  3310. * If L1 has read-only VM-exit information fields, use the
  3311. * less permissive vmx_vmwrite_bitmap to specify write
  3312. * permissions for the shadow VMCS.
  3313. */
  3314. if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
  3315. vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
  3316. return 0;
  3317. }
  3318. static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
  3319. {
  3320. u64 vmx_ept_vpid_cap;
  3321. vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
  3322. vmx->nested.msrs.vpid_caps);
  3323. /* Every bit is either reserved or a feature bit. */
  3324. if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
  3325. return -EINVAL;
  3326. vmx->nested.msrs.ept_caps = data;
  3327. vmx->nested.msrs.vpid_caps = data >> 32;
  3328. return 0;
  3329. }
  3330. static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
  3331. {
  3332. u64 *msr;
  3333. switch (msr_index) {
  3334. case MSR_IA32_VMX_CR0_FIXED0:
  3335. msr = &vmx->nested.msrs.cr0_fixed0;
  3336. break;
  3337. case MSR_IA32_VMX_CR4_FIXED0:
  3338. msr = &vmx->nested.msrs.cr4_fixed0;
  3339. break;
  3340. default:
  3341. BUG();
  3342. }
  3343. /*
  3344. * 1 bits (which indicates bits which "must-be-1" during VMX operation)
  3345. * must be 1 in the restored value.
  3346. */
  3347. if (!is_bitwise_subset(data, *msr, -1ULL))
  3348. return -EINVAL;
  3349. *msr = data;
  3350. return 0;
  3351. }
  3352. /*
  3353. * Called when userspace is restoring VMX MSRs.
  3354. *
  3355. * Returns 0 on success, non-0 otherwise.
  3356. */
  3357. static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
  3358. {
  3359. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3360. /*
  3361. * Don't allow changes to the VMX capability MSRs while the vCPU
  3362. * is in VMX operation.
  3363. */
  3364. if (vmx->nested.vmxon)
  3365. return -EBUSY;
  3366. switch (msr_index) {
  3367. case MSR_IA32_VMX_BASIC:
  3368. return vmx_restore_vmx_basic(vmx, data);
  3369. case MSR_IA32_VMX_PINBASED_CTLS:
  3370. case MSR_IA32_VMX_PROCBASED_CTLS:
  3371. case MSR_IA32_VMX_EXIT_CTLS:
  3372. case MSR_IA32_VMX_ENTRY_CTLS:
  3373. /*
  3374. * The "non-true" VMX capability MSRs are generated from the
  3375. * "true" MSRs, so we do not support restoring them directly.
  3376. *
  3377. * If userspace wants to emulate VMX_BASIC[55]=0, userspace
  3378. * should restore the "true" MSRs with the must-be-1 bits
  3379. * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
  3380. * DEFAULT SETTINGS".
  3381. */
  3382. return -EINVAL;
  3383. case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
  3384. case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
  3385. case MSR_IA32_VMX_TRUE_EXIT_CTLS:
  3386. case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
  3387. case MSR_IA32_VMX_PROCBASED_CTLS2:
  3388. return vmx_restore_control_msr(vmx, msr_index, data);
  3389. case MSR_IA32_VMX_MISC:
  3390. return vmx_restore_vmx_misc(vmx, data);
  3391. case MSR_IA32_VMX_CR0_FIXED0:
  3392. case MSR_IA32_VMX_CR4_FIXED0:
  3393. return vmx_restore_fixed0_msr(vmx, msr_index, data);
  3394. case MSR_IA32_VMX_CR0_FIXED1:
  3395. case MSR_IA32_VMX_CR4_FIXED1:
  3396. /*
  3397. * These MSRs are generated based on the vCPU's CPUID, so we
  3398. * do not support restoring them directly.
  3399. */
  3400. return -EINVAL;
  3401. case MSR_IA32_VMX_EPT_VPID_CAP:
  3402. return vmx_restore_vmx_ept_vpid_cap(vmx, data);
  3403. case MSR_IA32_VMX_VMCS_ENUM:
  3404. vmx->nested.msrs.vmcs_enum = data;
  3405. return 0;
  3406. default:
  3407. /*
  3408. * The rest of the VMX capability MSRs do not support restore.
  3409. */
  3410. return -EINVAL;
  3411. }
  3412. }
  3413. /* Returns 0 on success, non-0 otherwise. */
  3414. static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
  3415. {
  3416. switch (msr_index) {
  3417. case MSR_IA32_VMX_BASIC:
  3418. *pdata = msrs->basic;
  3419. break;
  3420. case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
  3421. case MSR_IA32_VMX_PINBASED_CTLS:
  3422. *pdata = vmx_control_msr(
  3423. msrs->pinbased_ctls_low,
  3424. msrs->pinbased_ctls_high);
  3425. if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
  3426. *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  3427. break;
  3428. case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
  3429. case MSR_IA32_VMX_PROCBASED_CTLS:
  3430. *pdata = vmx_control_msr(
  3431. msrs->procbased_ctls_low,
  3432. msrs->procbased_ctls_high);
  3433. if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
  3434. *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
  3435. break;
  3436. case MSR_IA32_VMX_TRUE_EXIT_CTLS:
  3437. case MSR_IA32_VMX_EXIT_CTLS:
  3438. *pdata = vmx_control_msr(
  3439. msrs->exit_ctls_low,
  3440. msrs->exit_ctls_high);
  3441. if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
  3442. *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
  3443. break;
  3444. case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
  3445. case MSR_IA32_VMX_ENTRY_CTLS:
  3446. *pdata = vmx_control_msr(
  3447. msrs->entry_ctls_low,
  3448. msrs->entry_ctls_high);
  3449. if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
  3450. *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
  3451. break;
  3452. case MSR_IA32_VMX_MISC:
  3453. *pdata = vmx_control_msr(
  3454. msrs->misc_low,
  3455. msrs->misc_high);
  3456. break;
  3457. case MSR_IA32_VMX_CR0_FIXED0:
  3458. *pdata = msrs->cr0_fixed0;
  3459. break;
  3460. case MSR_IA32_VMX_CR0_FIXED1:
  3461. *pdata = msrs->cr0_fixed1;
  3462. break;
  3463. case MSR_IA32_VMX_CR4_FIXED0:
  3464. *pdata = msrs->cr4_fixed0;
  3465. break;
  3466. case MSR_IA32_VMX_CR4_FIXED1:
  3467. *pdata = msrs->cr4_fixed1;
  3468. break;
  3469. case MSR_IA32_VMX_VMCS_ENUM:
  3470. *pdata = msrs->vmcs_enum;
  3471. break;
  3472. case MSR_IA32_VMX_PROCBASED_CTLS2:
  3473. *pdata = vmx_control_msr(
  3474. msrs->secondary_ctls_low,
  3475. msrs->secondary_ctls_high);
  3476. break;
  3477. case MSR_IA32_VMX_EPT_VPID_CAP:
  3478. *pdata = msrs->ept_caps |
  3479. ((u64)msrs->vpid_caps << 32);
  3480. break;
  3481. case MSR_IA32_VMX_VMFUNC:
  3482. *pdata = msrs->vmfunc_controls;
  3483. break;
  3484. default:
  3485. return 1;
  3486. }
  3487. return 0;
  3488. }
  3489. static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
  3490. uint64_t val)
  3491. {
  3492. uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
  3493. return !(val & ~valid_bits);
  3494. }
  3495. static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
  3496. {
  3497. switch (msr->index) {
  3498. case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
  3499. if (!nested)
  3500. return 1;
  3501. return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
  3502. default:
  3503. return 1;
  3504. }
  3505. return 0;
  3506. }
  3507. /*
  3508. * Reads an msr value (of 'msr_index') into 'pdata'.
  3509. * Returns 0 on success, non-0 otherwise.
  3510. * Assumes vcpu_load() was already called.
  3511. */
  3512. static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  3513. {
  3514. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3515. struct shared_msr_entry *msr;
  3516. switch (msr_info->index) {
  3517. #ifdef CONFIG_X86_64
  3518. case MSR_FS_BASE:
  3519. msr_info->data = vmcs_readl(GUEST_FS_BASE);
  3520. break;
  3521. case MSR_GS_BASE:
  3522. msr_info->data = vmcs_readl(GUEST_GS_BASE);
  3523. break;
  3524. case MSR_KERNEL_GS_BASE:
  3525. msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
  3526. break;
  3527. #endif
  3528. case MSR_EFER:
  3529. return kvm_get_msr_common(vcpu, msr_info);
  3530. case MSR_IA32_SPEC_CTRL:
  3531. if (!msr_info->host_initiated &&
  3532. !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
  3533. return 1;
  3534. msr_info->data = to_vmx(vcpu)->spec_ctrl;
  3535. break;
  3536. case MSR_IA32_SYSENTER_CS:
  3537. msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
  3538. break;
  3539. case MSR_IA32_SYSENTER_EIP:
  3540. msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
  3541. break;
  3542. case MSR_IA32_SYSENTER_ESP:
  3543. msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
  3544. break;
  3545. case MSR_IA32_BNDCFGS:
  3546. if (!kvm_mpx_supported() ||
  3547. (!msr_info->host_initiated &&
  3548. !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
  3549. return 1;
  3550. msr_info->data = vmcs_read64(GUEST_BNDCFGS);
  3551. break;
  3552. case MSR_IA32_MCG_EXT_CTL:
  3553. if (!msr_info->host_initiated &&
  3554. !(vmx->msr_ia32_feature_control &
  3555. FEATURE_CONTROL_LMCE))
  3556. return 1;
  3557. msr_info->data = vcpu->arch.mcg_ext_ctl;
  3558. break;
  3559. case MSR_IA32_FEATURE_CONTROL:
  3560. msr_info->data = vmx->msr_ia32_feature_control;
  3561. break;
  3562. case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
  3563. if (!nested_vmx_allowed(vcpu))
  3564. return 1;
  3565. return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
  3566. &msr_info->data);
  3567. case MSR_IA32_XSS:
  3568. if (!vmx_xsaves_supported() ||
  3569. (!msr_info->host_initiated &&
  3570. !(guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
  3571. guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))))
  3572. return 1;
  3573. msr_info->data = vcpu->arch.ia32_xss;
  3574. break;
  3575. case MSR_TSC_AUX:
  3576. if (!msr_info->host_initiated &&
  3577. !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
  3578. return 1;
  3579. /* Otherwise falls through */
  3580. default:
  3581. msr = find_msr_entry(vmx, msr_info->index);
  3582. if (msr) {
  3583. msr_info->data = msr->data;
  3584. break;
  3585. }
  3586. return kvm_get_msr_common(vcpu, msr_info);
  3587. }
  3588. return 0;
  3589. }
  3590. static void vmx_leave_nested(struct kvm_vcpu *vcpu);
  3591. /*
  3592. * Writes msr value into into the appropriate "register".
  3593. * Returns 0 on success, non-0 otherwise.
  3594. * Assumes vcpu_load() was already called.
  3595. */
  3596. static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  3597. {
  3598. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3599. struct shared_msr_entry *msr;
  3600. int ret = 0;
  3601. u32 msr_index = msr_info->index;
  3602. u64 data = msr_info->data;
  3603. switch (msr_index) {
  3604. case MSR_EFER:
  3605. ret = kvm_set_msr_common(vcpu, msr_info);
  3606. break;
  3607. #ifdef CONFIG_X86_64
  3608. case MSR_FS_BASE:
  3609. vmx_segment_cache_clear(vmx);
  3610. vmcs_writel(GUEST_FS_BASE, data);
  3611. break;
  3612. case MSR_GS_BASE:
  3613. vmx_segment_cache_clear(vmx);
  3614. vmcs_writel(GUEST_GS_BASE, data);
  3615. break;
  3616. case MSR_KERNEL_GS_BASE:
  3617. vmx_write_guest_kernel_gs_base(vmx, data);
  3618. break;
  3619. #endif
  3620. case MSR_IA32_SYSENTER_CS:
  3621. vmcs_write32(GUEST_SYSENTER_CS, data);
  3622. break;
  3623. case MSR_IA32_SYSENTER_EIP:
  3624. vmcs_writel(GUEST_SYSENTER_EIP, data);
  3625. break;
  3626. case MSR_IA32_SYSENTER_ESP:
  3627. vmcs_writel(GUEST_SYSENTER_ESP, data);
  3628. break;
  3629. case MSR_IA32_BNDCFGS:
  3630. if (!kvm_mpx_supported() ||
  3631. (!msr_info->host_initiated &&
  3632. !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
  3633. return 1;
  3634. if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
  3635. (data & MSR_IA32_BNDCFGS_RSVD))
  3636. return 1;
  3637. vmcs_write64(GUEST_BNDCFGS, data);
  3638. break;
  3639. case MSR_IA32_SPEC_CTRL:
  3640. if (!msr_info->host_initiated &&
  3641. !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
  3642. return 1;
  3643. /* The STIBP bit doesn't fault even if it's not advertised */
  3644. if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
  3645. return 1;
  3646. vmx->spec_ctrl = data;
  3647. if (!data)
  3648. break;
  3649. /*
  3650. * For non-nested:
  3651. * When it's written (to non-zero) for the first time, pass
  3652. * it through.
  3653. *
  3654. * For nested:
  3655. * The handling of the MSR bitmap for L2 guests is done in
  3656. * nested_vmx_merge_msr_bitmap. We should not touch the
  3657. * vmcs02.msr_bitmap here since it gets completely overwritten
  3658. * in the merging. We update the vmcs01 here for L1 as well
  3659. * since it will end up touching the MSR anyway now.
  3660. */
  3661. vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
  3662. MSR_IA32_SPEC_CTRL,
  3663. MSR_TYPE_RW);
  3664. break;
  3665. case MSR_IA32_PRED_CMD:
  3666. if (!msr_info->host_initiated &&
  3667. !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
  3668. return 1;
  3669. if (data & ~PRED_CMD_IBPB)
  3670. return 1;
  3671. if (!data)
  3672. break;
  3673. wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
  3674. /*
  3675. * For non-nested:
  3676. * When it's written (to non-zero) for the first time, pass
  3677. * it through.
  3678. *
  3679. * For nested:
  3680. * The handling of the MSR bitmap for L2 guests is done in
  3681. * nested_vmx_merge_msr_bitmap. We should not touch the
  3682. * vmcs02.msr_bitmap here since it gets completely overwritten
  3683. * in the merging.
  3684. */
  3685. vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
  3686. MSR_TYPE_W);
  3687. break;
  3688. case MSR_IA32_CR_PAT:
  3689. if (!kvm_pat_valid(data))
  3690. return 1;
  3691. if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
  3692. vmcs_write64(GUEST_IA32_PAT, data);
  3693. vcpu->arch.pat = data;
  3694. break;
  3695. }
  3696. ret = kvm_set_msr_common(vcpu, msr_info);
  3697. break;
  3698. case MSR_IA32_TSC_ADJUST:
  3699. ret = kvm_set_msr_common(vcpu, msr_info);
  3700. break;
  3701. case MSR_IA32_MCG_EXT_CTL:
  3702. if ((!msr_info->host_initiated &&
  3703. !(to_vmx(vcpu)->msr_ia32_feature_control &
  3704. FEATURE_CONTROL_LMCE)) ||
  3705. (data & ~MCG_EXT_CTL_LMCE_EN))
  3706. return 1;
  3707. vcpu->arch.mcg_ext_ctl = data;
  3708. break;
  3709. case MSR_IA32_FEATURE_CONTROL:
  3710. if (!vmx_feature_control_msr_valid(vcpu, data) ||
  3711. (to_vmx(vcpu)->msr_ia32_feature_control &
  3712. FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
  3713. return 1;
  3714. vmx->msr_ia32_feature_control = data;
  3715. if (msr_info->host_initiated && data == 0)
  3716. vmx_leave_nested(vcpu);
  3717. break;
  3718. case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
  3719. if (!msr_info->host_initiated)
  3720. return 1; /* they are read-only */
  3721. if (!nested_vmx_allowed(vcpu))
  3722. return 1;
  3723. return vmx_set_vmx_msr(vcpu, msr_index, data);
  3724. case MSR_IA32_XSS:
  3725. if (!vmx_xsaves_supported() ||
  3726. (!msr_info->host_initiated &&
  3727. !(guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
  3728. guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))))
  3729. return 1;
  3730. /*
  3731. * The only supported bit as of Skylake is bit 8, but
  3732. * it is not supported on KVM.
  3733. */
  3734. if (data != 0)
  3735. return 1;
  3736. vcpu->arch.ia32_xss = data;
  3737. if (vcpu->arch.ia32_xss != host_xss)
  3738. add_atomic_switch_msr(vmx, MSR_IA32_XSS,
  3739. vcpu->arch.ia32_xss, host_xss, false);
  3740. else
  3741. clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
  3742. break;
  3743. case MSR_TSC_AUX:
  3744. if (!msr_info->host_initiated &&
  3745. !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
  3746. return 1;
  3747. /* Check reserved bit, higher 32 bits should be zero */
  3748. if ((data >> 32) != 0)
  3749. return 1;
  3750. /* Otherwise falls through */
  3751. default:
  3752. msr = find_msr_entry(vmx, msr_index);
  3753. if (msr) {
  3754. u64 old_msr_data = msr->data;
  3755. msr->data = data;
  3756. if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
  3757. preempt_disable();
  3758. ret = kvm_set_shared_msr(msr->index, msr->data,
  3759. msr->mask);
  3760. preempt_enable();
  3761. if (ret)
  3762. msr->data = old_msr_data;
  3763. }
  3764. break;
  3765. }
  3766. ret = kvm_set_msr_common(vcpu, msr_info);
  3767. }
  3768. return ret;
  3769. }
  3770. static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
  3771. {
  3772. __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
  3773. switch (reg) {
  3774. case VCPU_REGS_RSP:
  3775. vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
  3776. break;
  3777. case VCPU_REGS_RIP:
  3778. vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
  3779. break;
  3780. case VCPU_EXREG_PDPTR:
  3781. if (enable_ept)
  3782. ept_save_pdptrs(vcpu);
  3783. break;
  3784. default:
  3785. break;
  3786. }
  3787. }
  3788. static __init int cpu_has_kvm_support(void)
  3789. {
  3790. return cpu_has_vmx();
  3791. }
  3792. static __init int vmx_disabled_by_bios(void)
  3793. {
  3794. u64 msr;
  3795. rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
  3796. if (msr & FEATURE_CONTROL_LOCKED) {
  3797. /* launched w/ TXT and VMX disabled */
  3798. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
  3799. && tboot_enabled())
  3800. return 1;
  3801. /* launched w/o TXT and VMX only enabled w/ TXT */
  3802. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
  3803. && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
  3804. && !tboot_enabled()) {
  3805. printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
  3806. "activate TXT before enabling KVM\n");
  3807. return 1;
  3808. }
  3809. /* launched w/o TXT and VMX disabled */
  3810. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
  3811. && !tboot_enabled())
  3812. return 1;
  3813. }
  3814. return 0;
  3815. }
  3816. static void kvm_cpu_vmxon(u64 addr)
  3817. {
  3818. cr4_set_bits(X86_CR4_VMXE);
  3819. intel_pt_handle_vmx(1);
  3820. asm volatile (ASM_VMX_VMXON_RAX
  3821. : : "a"(&addr), "m"(addr)
  3822. : "memory", "cc");
  3823. }
  3824. static int hardware_enable(void)
  3825. {
  3826. int cpu = raw_smp_processor_id();
  3827. u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
  3828. u64 old, test_bits;
  3829. if (cr4_read_shadow() & X86_CR4_VMXE)
  3830. return -EBUSY;
  3831. /*
  3832. * This can happen if we hot-added a CPU but failed to allocate
  3833. * VP assist page for it.
  3834. */
  3835. if (static_branch_unlikely(&enable_evmcs) &&
  3836. !hv_get_vp_assist_page(cpu))
  3837. return -EFAULT;
  3838. INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
  3839. INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
  3840. spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
  3841. /*
  3842. * Now we can enable the vmclear operation in kdump
  3843. * since the loaded_vmcss_on_cpu list on this cpu
  3844. * has been initialized.
  3845. *
  3846. * Though the cpu is not in VMX operation now, there
  3847. * is no problem to enable the vmclear operation
  3848. * for the loaded_vmcss_on_cpu list is empty!
  3849. */
  3850. crash_enable_local_vmclear(cpu);
  3851. rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
  3852. test_bits = FEATURE_CONTROL_LOCKED;
  3853. test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  3854. if (tboot_enabled())
  3855. test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
  3856. if ((old & test_bits) != test_bits) {
  3857. /* enable and lock */
  3858. wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
  3859. }
  3860. kvm_cpu_vmxon(phys_addr);
  3861. if (enable_ept)
  3862. ept_sync_global();
  3863. return 0;
  3864. }
  3865. static void vmclear_local_loaded_vmcss(void)
  3866. {
  3867. int cpu = raw_smp_processor_id();
  3868. struct loaded_vmcs *v, *n;
  3869. list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
  3870. loaded_vmcss_on_cpu_link)
  3871. __loaded_vmcs_clear(v);
  3872. }
  3873. /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
  3874. * tricks.
  3875. */
  3876. static void kvm_cpu_vmxoff(void)
  3877. {
  3878. asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
  3879. intel_pt_handle_vmx(0);
  3880. cr4_clear_bits(X86_CR4_VMXE);
  3881. }
  3882. static void hardware_disable(void)
  3883. {
  3884. vmclear_local_loaded_vmcss();
  3885. kvm_cpu_vmxoff();
  3886. }
  3887. static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
  3888. u32 msr, u32 *result)
  3889. {
  3890. u32 vmx_msr_low, vmx_msr_high;
  3891. u32 ctl = ctl_min | ctl_opt;
  3892. rdmsr(msr, vmx_msr_low, vmx_msr_high);
  3893. ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
  3894. ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
  3895. /* Ensure minimum (required) set of control bits are supported. */
  3896. if (ctl_min & ~ctl)
  3897. return -EIO;
  3898. *result = ctl;
  3899. return 0;
  3900. }
  3901. static __init bool allow_1_setting(u32 msr, u32 ctl)
  3902. {
  3903. u32 vmx_msr_low, vmx_msr_high;
  3904. rdmsr(msr, vmx_msr_low, vmx_msr_high);
  3905. return vmx_msr_high & ctl;
  3906. }
  3907. static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
  3908. {
  3909. u32 vmx_msr_low, vmx_msr_high;
  3910. u32 min, opt, min2, opt2;
  3911. u32 _pin_based_exec_control = 0;
  3912. u32 _cpu_based_exec_control = 0;
  3913. u32 _cpu_based_2nd_exec_control = 0;
  3914. u32 _vmexit_control = 0;
  3915. u32 _vmentry_control = 0;
  3916. memset(vmcs_conf, 0, sizeof(*vmcs_conf));
  3917. min = CPU_BASED_HLT_EXITING |
  3918. #ifdef CONFIG_X86_64
  3919. CPU_BASED_CR8_LOAD_EXITING |
  3920. CPU_BASED_CR8_STORE_EXITING |
  3921. #endif
  3922. CPU_BASED_CR3_LOAD_EXITING |
  3923. CPU_BASED_CR3_STORE_EXITING |
  3924. CPU_BASED_UNCOND_IO_EXITING |
  3925. CPU_BASED_MOV_DR_EXITING |
  3926. CPU_BASED_USE_TSC_OFFSETING |
  3927. CPU_BASED_MWAIT_EXITING |
  3928. CPU_BASED_MONITOR_EXITING |
  3929. CPU_BASED_INVLPG_EXITING |
  3930. CPU_BASED_RDPMC_EXITING;
  3931. opt = CPU_BASED_TPR_SHADOW |
  3932. CPU_BASED_USE_MSR_BITMAPS |
  3933. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  3934. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
  3935. &_cpu_based_exec_control) < 0)
  3936. return -EIO;
  3937. #ifdef CONFIG_X86_64
  3938. if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
  3939. _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
  3940. ~CPU_BASED_CR8_STORE_EXITING;
  3941. #endif
  3942. if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
  3943. min2 = 0;
  3944. opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  3945. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  3946. SECONDARY_EXEC_WBINVD_EXITING |
  3947. SECONDARY_EXEC_ENABLE_VPID |
  3948. SECONDARY_EXEC_ENABLE_EPT |
  3949. SECONDARY_EXEC_UNRESTRICTED_GUEST |
  3950. SECONDARY_EXEC_PAUSE_LOOP_EXITING |
  3951. SECONDARY_EXEC_DESC |
  3952. SECONDARY_EXEC_RDTSCP |
  3953. SECONDARY_EXEC_ENABLE_INVPCID |
  3954. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  3955. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
  3956. SECONDARY_EXEC_SHADOW_VMCS |
  3957. SECONDARY_EXEC_XSAVES |
  3958. SECONDARY_EXEC_RDSEED_EXITING |
  3959. SECONDARY_EXEC_RDRAND_EXITING |
  3960. SECONDARY_EXEC_ENABLE_PML |
  3961. SECONDARY_EXEC_TSC_SCALING |
  3962. SECONDARY_EXEC_ENABLE_VMFUNC |
  3963. SECONDARY_EXEC_ENCLS_EXITING;
  3964. if (adjust_vmx_controls(min2, opt2,
  3965. MSR_IA32_VMX_PROCBASED_CTLS2,
  3966. &_cpu_based_2nd_exec_control) < 0)
  3967. return -EIO;
  3968. }
  3969. #ifndef CONFIG_X86_64
  3970. if (!(_cpu_based_2nd_exec_control &
  3971. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
  3972. _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
  3973. #endif
  3974. if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
  3975. _cpu_based_2nd_exec_control &= ~(
  3976. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  3977. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  3978. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
  3979. rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
  3980. &vmx_capability.ept, &vmx_capability.vpid);
  3981. if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
  3982. /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
  3983. enabled */
  3984. _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
  3985. CPU_BASED_CR3_STORE_EXITING |
  3986. CPU_BASED_INVLPG_EXITING);
  3987. } else if (vmx_capability.ept) {
  3988. vmx_capability.ept = 0;
  3989. pr_warn_once("EPT CAP should not exist if not support "
  3990. "1-setting enable EPT VM-execution control\n");
  3991. }
  3992. if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
  3993. vmx_capability.vpid) {
  3994. vmx_capability.vpid = 0;
  3995. pr_warn_once("VPID CAP should not exist if not support "
  3996. "1-setting enable VPID VM-execution control\n");
  3997. }
  3998. min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
  3999. #ifdef CONFIG_X86_64
  4000. min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
  4001. #endif
  4002. opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
  4003. VM_EXIT_CLEAR_BNDCFGS;
  4004. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
  4005. &_vmexit_control) < 0)
  4006. return -EIO;
  4007. min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
  4008. opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
  4009. PIN_BASED_VMX_PREEMPTION_TIMER;
  4010. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
  4011. &_pin_based_exec_control) < 0)
  4012. return -EIO;
  4013. if (cpu_has_broken_vmx_preemption_timer())
  4014. _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  4015. if (!(_cpu_based_2nd_exec_control &
  4016. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
  4017. _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
  4018. min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
  4019. opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
  4020. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
  4021. &_vmentry_control) < 0)
  4022. return -EIO;
  4023. rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
  4024. /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
  4025. if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
  4026. return -EIO;
  4027. #ifdef CONFIG_X86_64
  4028. /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
  4029. if (vmx_msr_high & (1u<<16))
  4030. return -EIO;
  4031. #endif
  4032. /* Require Write-Back (WB) memory type for VMCS accesses. */
  4033. if (((vmx_msr_high >> 18) & 15) != 6)
  4034. return -EIO;
  4035. vmcs_conf->size = vmx_msr_high & 0x1fff;
  4036. vmcs_conf->order = get_order(vmcs_conf->size);
  4037. vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
  4038. vmcs_conf->revision_id = vmx_msr_low;
  4039. vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
  4040. vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
  4041. vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
  4042. vmcs_conf->vmexit_ctrl = _vmexit_control;
  4043. vmcs_conf->vmentry_ctrl = _vmentry_control;
  4044. if (static_branch_unlikely(&enable_evmcs))
  4045. evmcs_sanitize_exec_ctrls(vmcs_conf);
  4046. cpu_has_load_ia32_efer =
  4047. allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
  4048. VM_ENTRY_LOAD_IA32_EFER)
  4049. && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
  4050. VM_EXIT_LOAD_IA32_EFER);
  4051. cpu_has_load_perf_global_ctrl =
  4052. allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
  4053. VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
  4054. && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
  4055. VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
  4056. /*
  4057. * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
  4058. * but due to errata below it can't be used. Workaround is to use
  4059. * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
  4060. *
  4061. * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
  4062. *
  4063. * AAK155 (model 26)
  4064. * AAP115 (model 30)
  4065. * AAT100 (model 37)
  4066. * BC86,AAY89,BD102 (model 44)
  4067. * BA97 (model 46)
  4068. *
  4069. */
  4070. if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
  4071. switch (boot_cpu_data.x86_model) {
  4072. case 26:
  4073. case 30:
  4074. case 37:
  4075. case 44:
  4076. case 46:
  4077. cpu_has_load_perf_global_ctrl = false;
  4078. printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
  4079. "does not work properly. Using workaround\n");
  4080. break;
  4081. default:
  4082. break;
  4083. }
  4084. }
  4085. if (boot_cpu_has(X86_FEATURE_XSAVES))
  4086. rdmsrl(MSR_IA32_XSS, host_xss);
  4087. return 0;
  4088. }
  4089. static struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu)
  4090. {
  4091. int node = cpu_to_node(cpu);
  4092. struct page *pages;
  4093. struct vmcs *vmcs;
  4094. pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
  4095. if (!pages)
  4096. return NULL;
  4097. vmcs = page_address(pages);
  4098. memset(vmcs, 0, vmcs_config.size);
  4099. /* KVM supports Enlightened VMCS v1 only */
  4100. if (static_branch_unlikely(&enable_evmcs))
  4101. vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
  4102. else
  4103. vmcs->hdr.revision_id = vmcs_config.revision_id;
  4104. if (shadow)
  4105. vmcs->hdr.shadow_vmcs = 1;
  4106. return vmcs;
  4107. }
  4108. static void free_vmcs(struct vmcs *vmcs)
  4109. {
  4110. free_pages((unsigned long)vmcs, vmcs_config.order);
  4111. }
  4112. /*
  4113. * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
  4114. */
  4115. static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
  4116. {
  4117. if (!loaded_vmcs->vmcs)
  4118. return;
  4119. loaded_vmcs_clear(loaded_vmcs);
  4120. free_vmcs(loaded_vmcs->vmcs);
  4121. loaded_vmcs->vmcs = NULL;
  4122. if (loaded_vmcs->msr_bitmap)
  4123. free_page((unsigned long)loaded_vmcs->msr_bitmap);
  4124. WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
  4125. }
  4126. static struct vmcs *alloc_vmcs(bool shadow)
  4127. {
  4128. return alloc_vmcs_cpu(shadow, raw_smp_processor_id());
  4129. }
  4130. static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
  4131. {
  4132. loaded_vmcs->vmcs = alloc_vmcs(false);
  4133. if (!loaded_vmcs->vmcs)
  4134. return -ENOMEM;
  4135. loaded_vmcs->shadow_vmcs = NULL;
  4136. loaded_vmcs_init(loaded_vmcs);
  4137. if (cpu_has_vmx_msr_bitmap()) {
  4138. loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
  4139. if (!loaded_vmcs->msr_bitmap)
  4140. goto out_vmcs;
  4141. memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
  4142. if (IS_ENABLED(CONFIG_HYPERV) &&
  4143. static_branch_unlikely(&enable_evmcs) &&
  4144. (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
  4145. struct hv_enlightened_vmcs *evmcs =
  4146. (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
  4147. evmcs->hv_enlightenments_control.msr_bitmap = 1;
  4148. }
  4149. }
  4150. memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state));
  4151. return 0;
  4152. out_vmcs:
  4153. free_loaded_vmcs(loaded_vmcs);
  4154. return -ENOMEM;
  4155. }
  4156. static void free_kvm_area(void)
  4157. {
  4158. int cpu;
  4159. for_each_possible_cpu(cpu) {
  4160. free_vmcs(per_cpu(vmxarea, cpu));
  4161. per_cpu(vmxarea, cpu) = NULL;
  4162. }
  4163. }
  4164. enum vmcs_field_width {
  4165. VMCS_FIELD_WIDTH_U16 = 0,
  4166. VMCS_FIELD_WIDTH_U64 = 1,
  4167. VMCS_FIELD_WIDTH_U32 = 2,
  4168. VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
  4169. };
  4170. static inline int vmcs_field_width(unsigned long field)
  4171. {
  4172. if (0x1 & field) /* the *_HIGH fields are all 32 bit */
  4173. return VMCS_FIELD_WIDTH_U32;
  4174. return (field >> 13) & 0x3 ;
  4175. }
  4176. static inline int vmcs_field_readonly(unsigned long field)
  4177. {
  4178. return (((field >> 10) & 0x3) == 1);
  4179. }
  4180. static void init_vmcs_shadow_fields(void)
  4181. {
  4182. int i, j;
  4183. for (i = j = 0; i < max_shadow_read_only_fields; i++) {
  4184. u16 field = shadow_read_only_fields[i];
  4185. if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
  4186. (i + 1 == max_shadow_read_only_fields ||
  4187. shadow_read_only_fields[i + 1] != field + 1))
  4188. pr_err("Missing field from shadow_read_only_field %x\n",
  4189. field + 1);
  4190. clear_bit(field, vmx_vmread_bitmap);
  4191. #ifdef CONFIG_X86_64
  4192. if (field & 1)
  4193. continue;
  4194. #endif
  4195. if (j < i)
  4196. shadow_read_only_fields[j] = field;
  4197. j++;
  4198. }
  4199. max_shadow_read_only_fields = j;
  4200. for (i = j = 0; i < max_shadow_read_write_fields; i++) {
  4201. u16 field = shadow_read_write_fields[i];
  4202. if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
  4203. (i + 1 == max_shadow_read_write_fields ||
  4204. shadow_read_write_fields[i + 1] != field + 1))
  4205. pr_err("Missing field from shadow_read_write_field %x\n",
  4206. field + 1);
  4207. /*
  4208. * PML and the preemption timer can be emulated, but the
  4209. * processor cannot vmwrite to fields that don't exist
  4210. * on bare metal.
  4211. */
  4212. switch (field) {
  4213. case GUEST_PML_INDEX:
  4214. if (!cpu_has_vmx_pml())
  4215. continue;
  4216. break;
  4217. case VMX_PREEMPTION_TIMER_VALUE:
  4218. if (!cpu_has_vmx_preemption_timer())
  4219. continue;
  4220. break;
  4221. case GUEST_INTR_STATUS:
  4222. if (!cpu_has_vmx_apicv())
  4223. continue;
  4224. break;
  4225. default:
  4226. break;
  4227. }
  4228. clear_bit(field, vmx_vmwrite_bitmap);
  4229. clear_bit(field, vmx_vmread_bitmap);
  4230. #ifdef CONFIG_X86_64
  4231. if (field & 1)
  4232. continue;
  4233. #endif
  4234. if (j < i)
  4235. shadow_read_write_fields[j] = field;
  4236. j++;
  4237. }
  4238. max_shadow_read_write_fields = j;
  4239. }
  4240. static __init int alloc_kvm_area(void)
  4241. {
  4242. int cpu;
  4243. for_each_possible_cpu(cpu) {
  4244. struct vmcs *vmcs;
  4245. vmcs = alloc_vmcs_cpu(false, cpu);
  4246. if (!vmcs) {
  4247. free_kvm_area();
  4248. return -ENOMEM;
  4249. }
  4250. /*
  4251. * When eVMCS is enabled, alloc_vmcs_cpu() sets
  4252. * vmcs->revision_id to KVM_EVMCS_VERSION instead of
  4253. * revision_id reported by MSR_IA32_VMX_BASIC.
  4254. *
  4255. * However, even though not explictly documented by
  4256. * TLFS, VMXArea passed as VMXON argument should
  4257. * still be marked with revision_id reported by
  4258. * physical CPU.
  4259. */
  4260. if (static_branch_unlikely(&enable_evmcs))
  4261. vmcs->hdr.revision_id = vmcs_config.revision_id;
  4262. per_cpu(vmxarea, cpu) = vmcs;
  4263. }
  4264. return 0;
  4265. }
  4266. static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
  4267. struct kvm_segment *save)
  4268. {
  4269. if (!emulate_invalid_guest_state) {
  4270. /*
  4271. * CS and SS RPL should be equal during guest entry according
  4272. * to VMX spec, but in reality it is not always so. Since vcpu
  4273. * is in the middle of the transition from real mode to
  4274. * protected mode it is safe to assume that RPL 0 is a good
  4275. * default value.
  4276. */
  4277. if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
  4278. save->selector &= ~SEGMENT_RPL_MASK;
  4279. save->dpl = save->selector & SEGMENT_RPL_MASK;
  4280. save->s = 1;
  4281. }
  4282. vmx_set_segment(vcpu, save, seg);
  4283. }
  4284. static void enter_pmode(struct kvm_vcpu *vcpu)
  4285. {
  4286. unsigned long flags;
  4287. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4288. /*
  4289. * Update real mode segment cache. It may be not up-to-date if sement
  4290. * register was written while vcpu was in a guest mode.
  4291. */
  4292. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
  4293. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
  4294. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
  4295. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
  4296. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
  4297. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
  4298. vmx->rmode.vm86_active = 0;
  4299. vmx_segment_cache_clear(vmx);
  4300. vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
  4301. flags = vmcs_readl(GUEST_RFLAGS);
  4302. flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
  4303. flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
  4304. vmcs_writel(GUEST_RFLAGS, flags);
  4305. vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
  4306. (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
  4307. update_exception_bitmap(vcpu);
  4308. fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
  4309. fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
  4310. fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
  4311. fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
  4312. fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
  4313. fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
  4314. }
  4315. static void fix_rmode_seg(int seg, struct kvm_segment *save)
  4316. {
  4317. const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  4318. struct kvm_segment var = *save;
  4319. var.dpl = 0x3;
  4320. if (seg == VCPU_SREG_CS)
  4321. var.type = 0x3;
  4322. if (!emulate_invalid_guest_state) {
  4323. var.selector = var.base >> 4;
  4324. var.base = var.base & 0xffff0;
  4325. var.limit = 0xffff;
  4326. var.g = 0;
  4327. var.db = 0;
  4328. var.present = 1;
  4329. var.s = 1;
  4330. var.l = 0;
  4331. var.unusable = 0;
  4332. var.type = 0x3;
  4333. var.avl = 0;
  4334. if (save->base & 0xf)
  4335. printk_once(KERN_WARNING "kvm: segment base is not "
  4336. "paragraph aligned when entering "
  4337. "protected mode (seg=%d)", seg);
  4338. }
  4339. vmcs_write16(sf->selector, var.selector);
  4340. vmcs_writel(sf->base, var.base);
  4341. vmcs_write32(sf->limit, var.limit);
  4342. vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
  4343. }
  4344. static void enter_rmode(struct kvm_vcpu *vcpu)
  4345. {
  4346. unsigned long flags;
  4347. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4348. struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
  4349. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
  4350. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
  4351. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
  4352. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
  4353. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
  4354. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
  4355. vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
  4356. vmx->rmode.vm86_active = 1;
  4357. /*
  4358. * Very old userspace does not call KVM_SET_TSS_ADDR before entering
  4359. * vcpu. Warn the user that an update is overdue.
  4360. */
  4361. if (!kvm_vmx->tss_addr)
  4362. printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
  4363. "called before entering vcpu\n");
  4364. vmx_segment_cache_clear(vmx);
  4365. vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
  4366. vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
  4367. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  4368. flags = vmcs_readl(GUEST_RFLAGS);
  4369. vmx->rmode.save_rflags = flags;
  4370. flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
  4371. vmcs_writel(GUEST_RFLAGS, flags);
  4372. vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
  4373. update_exception_bitmap(vcpu);
  4374. fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
  4375. fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
  4376. fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
  4377. fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
  4378. fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
  4379. fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
  4380. kvm_mmu_reset_context(vcpu);
  4381. }
  4382. static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
  4383. {
  4384. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4385. struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
  4386. if (!msr)
  4387. return;
  4388. vcpu->arch.efer = efer;
  4389. if (efer & EFER_LMA) {
  4390. vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
  4391. msr->data = efer;
  4392. } else {
  4393. vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
  4394. msr->data = efer & ~EFER_LME;
  4395. }
  4396. setup_msrs(vmx);
  4397. }
  4398. #ifdef CONFIG_X86_64
  4399. static void enter_lmode(struct kvm_vcpu *vcpu)
  4400. {
  4401. u32 guest_tr_ar;
  4402. vmx_segment_cache_clear(to_vmx(vcpu));
  4403. guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
  4404. if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
  4405. pr_debug_ratelimited("%s: tss fixup for long mode. \n",
  4406. __func__);
  4407. vmcs_write32(GUEST_TR_AR_BYTES,
  4408. (guest_tr_ar & ~VMX_AR_TYPE_MASK)
  4409. | VMX_AR_TYPE_BUSY_64_TSS);
  4410. }
  4411. vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
  4412. }
  4413. static void exit_lmode(struct kvm_vcpu *vcpu)
  4414. {
  4415. vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
  4416. vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
  4417. }
  4418. #endif
  4419. static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
  4420. bool invalidate_gpa)
  4421. {
  4422. if (enable_ept && (invalidate_gpa || !enable_vpid)) {
  4423. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  4424. return;
  4425. ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
  4426. } else {
  4427. vpid_sync_context(vpid);
  4428. }
  4429. }
  4430. static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
  4431. {
  4432. __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
  4433. }
  4434. static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
  4435. {
  4436. int vpid = to_vmx(vcpu)->vpid;
  4437. if (!vpid_sync_vcpu_addr(vpid, addr))
  4438. vpid_sync_context(vpid);
  4439. /*
  4440. * If VPIDs are not supported or enabled, then the above is a no-op.
  4441. * But we don't really need a TLB flush in that case anyway, because
  4442. * each VM entry/exit includes an implicit flush when VPID is 0.
  4443. */
  4444. }
  4445. static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
  4446. {
  4447. ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
  4448. vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
  4449. vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
  4450. }
  4451. static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
  4452. {
  4453. if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
  4454. vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
  4455. __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
  4456. }
  4457. static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
  4458. {
  4459. ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
  4460. vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
  4461. vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
  4462. }
  4463. static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
  4464. {
  4465. struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
  4466. if (!test_bit(VCPU_EXREG_PDPTR,
  4467. (unsigned long *)&vcpu->arch.regs_dirty))
  4468. return;
  4469. if (is_pae_paging(vcpu)) {
  4470. vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
  4471. vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
  4472. vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
  4473. vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
  4474. }
  4475. }
  4476. static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
  4477. {
  4478. struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
  4479. if (is_pae_paging(vcpu)) {
  4480. mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
  4481. mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
  4482. mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
  4483. mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
  4484. }
  4485. __set_bit(VCPU_EXREG_PDPTR,
  4486. (unsigned long *)&vcpu->arch.regs_avail);
  4487. __set_bit(VCPU_EXREG_PDPTR,
  4488. (unsigned long *)&vcpu->arch.regs_dirty);
  4489. }
  4490. static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
  4491. {
  4492. u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
  4493. u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
  4494. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  4495. if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
  4496. SECONDARY_EXEC_UNRESTRICTED_GUEST &&
  4497. nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
  4498. fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
  4499. return fixed_bits_valid(val, fixed0, fixed1);
  4500. }
  4501. static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
  4502. {
  4503. u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
  4504. u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
  4505. return fixed_bits_valid(val, fixed0, fixed1);
  4506. }
  4507. static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
  4508. {
  4509. u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
  4510. u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
  4511. return fixed_bits_valid(val, fixed0, fixed1);
  4512. }
  4513. /* No difference in the restrictions on guest and host CR4 in VMX operation. */
  4514. #define nested_guest_cr4_valid nested_cr4_valid
  4515. #define nested_host_cr4_valid nested_cr4_valid
  4516. static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
  4517. static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
  4518. unsigned long cr0,
  4519. struct kvm_vcpu *vcpu)
  4520. {
  4521. if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
  4522. vmx_decache_cr3(vcpu);
  4523. if (!(cr0 & X86_CR0_PG)) {
  4524. /* From paging/starting to nonpaging */
  4525. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  4526. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
  4527. (CPU_BASED_CR3_LOAD_EXITING |
  4528. CPU_BASED_CR3_STORE_EXITING));
  4529. vcpu->arch.cr0 = cr0;
  4530. vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
  4531. } else if (!is_paging(vcpu)) {
  4532. /* From nonpaging to paging */
  4533. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  4534. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
  4535. ~(CPU_BASED_CR3_LOAD_EXITING |
  4536. CPU_BASED_CR3_STORE_EXITING));
  4537. vcpu->arch.cr0 = cr0;
  4538. vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
  4539. }
  4540. if (!(cr0 & X86_CR0_WP))
  4541. *hw_cr0 &= ~X86_CR0_WP;
  4542. }
  4543. static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  4544. {
  4545. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4546. unsigned long hw_cr0;
  4547. hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
  4548. if (enable_unrestricted_guest)
  4549. hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
  4550. else {
  4551. hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
  4552. if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
  4553. enter_pmode(vcpu);
  4554. if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
  4555. enter_rmode(vcpu);
  4556. }
  4557. #ifdef CONFIG_X86_64
  4558. if (vcpu->arch.efer & EFER_LME) {
  4559. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
  4560. enter_lmode(vcpu);
  4561. if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
  4562. exit_lmode(vcpu);
  4563. }
  4564. #endif
  4565. if (enable_ept && !enable_unrestricted_guest)
  4566. ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
  4567. vmcs_writel(CR0_READ_SHADOW, cr0);
  4568. vmcs_writel(GUEST_CR0, hw_cr0);
  4569. vcpu->arch.cr0 = cr0;
  4570. /* depends on vcpu->arch.cr0 to be set to a new value */
  4571. vmx->emulation_required = emulation_required(vcpu);
  4572. }
  4573. static int get_ept_level(struct kvm_vcpu *vcpu)
  4574. {
  4575. /* Nested EPT currently only supports 4-level walks. */
  4576. if (is_guest_mode(vcpu) && nested_cpu_has_ept(get_vmcs12(vcpu)))
  4577. return 4;
  4578. if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
  4579. return 5;
  4580. return 4;
  4581. }
  4582. static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
  4583. {
  4584. u64 eptp = VMX_EPTP_MT_WB;
  4585. eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
  4586. if (enable_ept_ad_bits &&
  4587. (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
  4588. eptp |= VMX_EPTP_AD_ENABLE_BIT;
  4589. eptp |= (root_hpa & PAGE_MASK);
  4590. return eptp;
  4591. }
  4592. static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
  4593. {
  4594. struct kvm *kvm = vcpu->kvm;
  4595. unsigned long guest_cr3;
  4596. u64 eptp;
  4597. guest_cr3 = cr3;
  4598. if (enable_ept) {
  4599. eptp = construct_eptp(vcpu, cr3);
  4600. vmcs_write64(EPT_POINTER, eptp);
  4601. if (kvm_x86_ops->tlb_remote_flush) {
  4602. spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
  4603. to_vmx(vcpu)->ept_pointer = eptp;
  4604. to_kvm_vmx(kvm)->ept_pointers_match
  4605. = EPT_POINTERS_CHECK;
  4606. spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
  4607. }
  4608. if (enable_unrestricted_guest || is_paging(vcpu) ||
  4609. is_guest_mode(vcpu))
  4610. guest_cr3 = kvm_read_cr3(vcpu);
  4611. else
  4612. guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
  4613. ept_load_pdptrs(vcpu);
  4614. }
  4615. vmcs_writel(GUEST_CR3, guest_cr3);
  4616. }
  4617. static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  4618. {
  4619. /*
  4620. * Pass through host's Machine Check Enable value to hw_cr4, which
  4621. * is in force while we are in guest mode. Do not let guests control
  4622. * this bit, even if host CR4.MCE == 0.
  4623. */
  4624. unsigned long hw_cr4;
  4625. hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
  4626. if (enable_unrestricted_guest)
  4627. hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
  4628. else if (to_vmx(vcpu)->rmode.vm86_active)
  4629. hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
  4630. else
  4631. hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
  4632. if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
  4633. if (cr4 & X86_CR4_UMIP) {
  4634. vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
  4635. SECONDARY_EXEC_DESC);
  4636. hw_cr4 &= ~X86_CR4_UMIP;
  4637. } else if (!is_guest_mode(vcpu) ||
  4638. !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
  4639. vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
  4640. SECONDARY_EXEC_DESC);
  4641. }
  4642. if (cr4 & X86_CR4_VMXE) {
  4643. /*
  4644. * To use VMXON (and later other VMX instructions), a guest
  4645. * must first be able to turn on cr4.VMXE (see handle_vmon()).
  4646. * So basically the check on whether to allow nested VMX
  4647. * is here. We operate under the default treatment of SMM,
  4648. * so VMX cannot be enabled under SMM.
  4649. */
  4650. if (!nested_vmx_allowed(vcpu) || is_smm(vcpu))
  4651. return 1;
  4652. }
  4653. if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
  4654. return 1;
  4655. vcpu->arch.cr4 = cr4;
  4656. if (!enable_unrestricted_guest) {
  4657. if (enable_ept) {
  4658. if (!is_paging(vcpu)) {
  4659. hw_cr4 &= ~X86_CR4_PAE;
  4660. hw_cr4 |= X86_CR4_PSE;
  4661. } else if (!(cr4 & X86_CR4_PAE)) {
  4662. hw_cr4 &= ~X86_CR4_PAE;
  4663. }
  4664. }
  4665. /*
  4666. * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
  4667. * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
  4668. * to be manually disabled when guest switches to non-paging
  4669. * mode.
  4670. *
  4671. * If !enable_unrestricted_guest, the CPU is always running
  4672. * with CR0.PG=1 and CR4 needs to be modified.
  4673. * If enable_unrestricted_guest, the CPU automatically
  4674. * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
  4675. */
  4676. if (!is_paging(vcpu))
  4677. hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
  4678. }
  4679. vmcs_writel(CR4_READ_SHADOW, cr4);
  4680. vmcs_writel(GUEST_CR4, hw_cr4);
  4681. return 0;
  4682. }
  4683. static void vmx_get_segment(struct kvm_vcpu *vcpu,
  4684. struct kvm_segment *var, int seg)
  4685. {
  4686. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4687. u32 ar;
  4688. if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
  4689. *var = vmx->rmode.segs[seg];
  4690. if (seg == VCPU_SREG_TR
  4691. || var->selector == vmx_read_guest_seg_selector(vmx, seg))
  4692. return;
  4693. var->base = vmx_read_guest_seg_base(vmx, seg);
  4694. var->selector = vmx_read_guest_seg_selector(vmx, seg);
  4695. return;
  4696. }
  4697. var->base = vmx_read_guest_seg_base(vmx, seg);
  4698. var->limit = vmx_read_guest_seg_limit(vmx, seg);
  4699. var->selector = vmx_read_guest_seg_selector(vmx, seg);
  4700. ar = vmx_read_guest_seg_ar(vmx, seg);
  4701. var->unusable = (ar >> 16) & 1;
  4702. var->type = ar & 15;
  4703. var->s = (ar >> 4) & 1;
  4704. var->dpl = (ar >> 5) & 3;
  4705. /*
  4706. * Some userspaces do not preserve unusable property. Since usable
  4707. * segment has to be present according to VMX spec we can use present
  4708. * property to amend userspace bug by making unusable segment always
  4709. * nonpresent. vmx_segment_access_rights() already marks nonpresent
  4710. * segment as unusable.
  4711. */
  4712. var->present = !var->unusable;
  4713. var->avl = (ar >> 12) & 1;
  4714. var->l = (ar >> 13) & 1;
  4715. var->db = (ar >> 14) & 1;
  4716. var->g = (ar >> 15) & 1;
  4717. }
  4718. static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
  4719. {
  4720. struct kvm_segment s;
  4721. if (to_vmx(vcpu)->rmode.vm86_active) {
  4722. vmx_get_segment(vcpu, &s, seg);
  4723. return s.base;
  4724. }
  4725. return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
  4726. }
  4727. static int vmx_get_cpl(struct kvm_vcpu *vcpu)
  4728. {
  4729. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4730. if (unlikely(vmx->rmode.vm86_active))
  4731. return 0;
  4732. else {
  4733. int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
  4734. return VMX_AR_DPL(ar);
  4735. }
  4736. }
  4737. static u32 vmx_segment_access_rights(struct kvm_segment *var)
  4738. {
  4739. u32 ar;
  4740. if (var->unusable || !var->present)
  4741. ar = 1 << 16;
  4742. else {
  4743. ar = var->type & 15;
  4744. ar |= (var->s & 1) << 4;
  4745. ar |= (var->dpl & 3) << 5;
  4746. ar |= (var->present & 1) << 7;
  4747. ar |= (var->avl & 1) << 12;
  4748. ar |= (var->l & 1) << 13;
  4749. ar |= (var->db & 1) << 14;
  4750. ar |= (var->g & 1) << 15;
  4751. }
  4752. return ar;
  4753. }
  4754. static void vmx_set_segment(struct kvm_vcpu *vcpu,
  4755. struct kvm_segment *var, int seg)
  4756. {
  4757. struct vcpu_vmx *vmx = to_vmx(vcpu);
  4758. const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  4759. vmx_segment_cache_clear(vmx);
  4760. if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
  4761. vmx->rmode.segs[seg] = *var;
  4762. if (seg == VCPU_SREG_TR)
  4763. vmcs_write16(sf->selector, var->selector);
  4764. else if (var->s)
  4765. fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
  4766. goto out;
  4767. }
  4768. vmcs_writel(sf->base, var->base);
  4769. vmcs_write32(sf->limit, var->limit);
  4770. vmcs_write16(sf->selector, var->selector);
  4771. /*
  4772. * Fix the "Accessed" bit in AR field of segment registers for older
  4773. * qemu binaries.
  4774. * IA32 arch specifies that at the time of processor reset the
  4775. * "Accessed" bit in the AR field of segment registers is 1. And qemu
  4776. * is setting it to 0 in the userland code. This causes invalid guest
  4777. * state vmexit when "unrestricted guest" mode is turned on.
  4778. * Fix for this setup issue in cpu_reset is being pushed in the qemu
  4779. * tree. Newer qemu binaries with that qemu fix would not need this
  4780. * kvm hack.
  4781. */
  4782. if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
  4783. var->type |= 0x1; /* Accessed */
  4784. vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
  4785. out:
  4786. vmx->emulation_required = emulation_required(vcpu);
  4787. }
  4788. static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
  4789. {
  4790. u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
  4791. *db = (ar >> 14) & 1;
  4792. *l = (ar >> 13) & 1;
  4793. }
  4794. static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  4795. {
  4796. dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
  4797. dt->address = vmcs_readl(GUEST_IDTR_BASE);
  4798. }
  4799. static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  4800. {
  4801. vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
  4802. vmcs_writel(GUEST_IDTR_BASE, dt->address);
  4803. }
  4804. static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  4805. {
  4806. dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
  4807. dt->address = vmcs_readl(GUEST_GDTR_BASE);
  4808. }
  4809. static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  4810. {
  4811. vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
  4812. vmcs_writel(GUEST_GDTR_BASE, dt->address);
  4813. }
  4814. static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
  4815. {
  4816. struct kvm_segment var;
  4817. u32 ar;
  4818. vmx_get_segment(vcpu, &var, seg);
  4819. var.dpl = 0x3;
  4820. if (seg == VCPU_SREG_CS)
  4821. var.type = 0x3;
  4822. ar = vmx_segment_access_rights(&var);
  4823. if (var.base != (var.selector << 4))
  4824. return false;
  4825. if (var.limit != 0xffff)
  4826. return false;
  4827. if (ar != 0xf3)
  4828. return false;
  4829. return true;
  4830. }
  4831. static bool code_segment_valid(struct kvm_vcpu *vcpu)
  4832. {
  4833. struct kvm_segment cs;
  4834. unsigned int cs_rpl;
  4835. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  4836. cs_rpl = cs.selector & SEGMENT_RPL_MASK;
  4837. if (cs.unusable)
  4838. return false;
  4839. if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
  4840. return false;
  4841. if (!cs.s)
  4842. return false;
  4843. if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
  4844. if (cs.dpl > cs_rpl)
  4845. return false;
  4846. } else {
  4847. if (cs.dpl != cs_rpl)
  4848. return false;
  4849. }
  4850. if (!cs.present)
  4851. return false;
  4852. /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
  4853. return true;
  4854. }
  4855. static bool stack_segment_valid(struct kvm_vcpu *vcpu)
  4856. {
  4857. struct kvm_segment ss;
  4858. unsigned int ss_rpl;
  4859. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  4860. ss_rpl = ss.selector & SEGMENT_RPL_MASK;
  4861. if (ss.unusable)
  4862. return true;
  4863. if (ss.type != 3 && ss.type != 7)
  4864. return false;
  4865. if (!ss.s)
  4866. return false;
  4867. if (ss.dpl != ss_rpl) /* DPL != RPL */
  4868. return false;
  4869. if (!ss.present)
  4870. return false;
  4871. return true;
  4872. }
  4873. static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
  4874. {
  4875. struct kvm_segment var;
  4876. unsigned int rpl;
  4877. vmx_get_segment(vcpu, &var, seg);
  4878. rpl = var.selector & SEGMENT_RPL_MASK;
  4879. if (var.unusable)
  4880. return true;
  4881. if (!var.s)
  4882. return false;
  4883. if (!var.present)
  4884. return false;
  4885. if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
  4886. if (var.dpl < rpl) /* DPL < RPL */
  4887. return false;
  4888. }
  4889. /* TODO: Add other members to kvm_segment_field to allow checking for other access
  4890. * rights flags
  4891. */
  4892. return true;
  4893. }
  4894. static bool tr_valid(struct kvm_vcpu *vcpu)
  4895. {
  4896. struct kvm_segment tr;
  4897. vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
  4898. if (tr.unusable)
  4899. return false;
  4900. if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
  4901. return false;
  4902. if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
  4903. return false;
  4904. if (!tr.present)
  4905. return false;
  4906. return true;
  4907. }
  4908. static bool ldtr_valid(struct kvm_vcpu *vcpu)
  4909. {
  4910. struct kvm_segment ldtr;
  4911. vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
  4912. if (ldtr.unusable)
  4913. return true;
  4914. if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
  4915. return false;
  4916. if (ldtr.type != 2)
  4917. return false;
  4918. if (!ldtr.present)
  4919. return false;
  4920. return true;
  4921. }
  4922. static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
  4923. {
  4924. struct kvm_segment cs, ss;
  4925. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  4926. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  4927. return ((cs.selector & SEGMENT_RPL_MASK) ==
  4928. (ss.selector & SEGMENT_RPL_MASK));
  4929. }
  4930. static bool nested_vmx_check_io_bitmaps(struct kvm_vcpu *vcpu,
  4931. unsigned int port, int size);
  4932. static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
  4933. struct vmcs12 *vmcs12)
  4934. {
  4935. unsigned long exit_qualification;
  4936. unsigned short port;
  4937. int size;
  4938. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
  4939. return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
  4940. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  4941. port = exit_qualification >> 16;
  4942. size = (exit_qualification & 7) + 1;
  4943. return nested_vmx_check_io_bitmaps(vcpu, port, size);
  4944. }
  4945. /*
  4946. * Check if guest state is valid. Returns true if valid, false if
  4947. * not.
  4948. * We assume that registers are always usable
  4949. */
  4950. static bool guest_state_valid(struct kvm_vcpu *vcpu)
  4951. {
  4952. if (enable_unrestricted_guest)
  4953. return true;
  4954. /* real mode guest state checks */
  4955. if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
  4956. if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
  4957. return false;
  4958. if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
  4959. return false;
  4960. if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
  4961. return false;
  4962. if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
  4963. return false;
  4964. if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
  4965. return false;
  4966. if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
  4967. return false;
  4968. } else {
  4969. /* protected mode guest state checks */
  4970. if (!cs_ss_rpl_check(vcpu))
  4971. return false;
  4972. if (!code_segment_valid(vcpu))
  4973. return false;
  4974. if (!stack_segment_valid(vcpu))
  4975. return false;
  4976. if (!data_segment_valid(vcpu, VCPU_SREG_DS))
  4977. return false;
  4978. if (!data_segment_valid(vcpu, VCPU_SREG_ES))
  4979. return false;
  4980. if (!data_segment_valid(vcpu, VCPU_SREG_FS))
  4981. return false;
  4982. if (!data_segment_valid(vcpu, VCPU_SREG_GS))
  4983. return false;
  4984. if (!tr_valid(vcpu))
  4985. return false;
  4986. if (!ldtr_valid(vcpu))
  4987. return false;
  4988. }
  4989. /* TODO:
  4990. * - Add checks on RIP
  4991. * - Add checks on RFLAGS
  4992. */
  4993. return true;
  4994. }
  4995. static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
  4996. {
  4997. return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
  4998. }
  4999. static int init_rmode_tss(struct kvm *kvm)
  5000. {
  5001. gfn_t fn;
  5002. u16 data = 0;
  5003. int idx, r;
  5004. idx = srcu_read_lock(&kvm->srcu);
  5005. fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
  5006. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  5007. if (r < 0)
  5008. goto out;
  5009. data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
  5010. r = kvm_write_guest_page(kvm, fn++, &data,
  5011. TSS_IOPB_BASE_OFFSET, sizeof(u16));
  5012. if (r < 0)
  5013. goto out;
  5014. r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
  5015. if (r < 0)
  5016. goto out;
  5017. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  5018. if (r < 0)
  5019. goto out;
  5020. data = ~0;
  5021. r = kvm_write_guest_page(kvm, fn, &data,
  5022. RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
  5023. sizeof(u8));
  5024. out:
  5025. srcu_read_unlock(&kvm->srcu, idx);
  5026. return r;
  5027. }
  5028. static int init_rmode_identity_map(struct kvm *kvm)
  5029. {
  5030. struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
  5031. int i, idx, r = 0;
  5032. kvm_pfn_t identity_map_pfn;
  5033. u32 tmp;
  5034. /* Protect kvm_vmx->ept_identity_pagetable_done. */
  5035. mutex_lock(&kvm->slots_lock);
  5036. if (likely(kvm_vmx->ept_identity_pagetable_done))
  5037. goto out2;
  5038. if (!kvm_vmx->ept_identity_map_addr)
  5039. kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
  5040. identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
  5041. r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
  5042. kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
  5043. if (r < 0)
  5044. goto out2;
  5045. idx = srcu_read_lock(&kvm->srcu);
  5046. r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
  5047. if (r < 0)
  5048. goto out;
  5049. /* Set up identity-mapping pagetable for EPT in real mode */
  5050. for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
  5051. tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
  5052. _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
  5053. r = kvm_write_guest_page(kvm, identity_map_pfn,
  5054. &tmp, i * sizeof(tmp), sizeof(tmp));
  5055. if (r < 0)
  5056. goto out;
  5057. }
  5058. kvm_vmx->ept_identity_pagetable_done = true;
  5059. out:
  5060. srcu_read_unlock(&kvm->srcu, idx);
  5061. out2:
  5062. mutex_unlock(&kvm->slots_lock);
  5063. return r;
  5064. }
  5065. static void seg_setup(int seg)
  5066. {
  5067. const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  5068. unsigned int ar;
  5069. vmcs_write16(sf->selector, 0);
  5070. vmcs_writel(sf->base, 0);
  5071. vmcs_write32(sf->limit, 0xffff);
  5072. ar = 0x93;
  5073. if (seg == VCPU_SREG_CS)
  5074. ar |= 0x08; /* code segment */
  5075. vmcs_write32(sf->ar_bytes, ar);
  5076. }
  5077. static int alloc_apic_access_page(struct kvm *kvm)
  5078. {
  5079. struct page *page;
  5080. int r = 0;
  5081. mutex_lock(&kvm->slots_lock);
  5082. if (kvm->arch.apic_access_page_done)
  5083. goto out;
  5084. r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
  5085. APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
  5086. if (r)
  5087. goto out;
  5088. page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
  5089. if (is_error_page(page)) {
  5090. r = -EFAULT;
  5091. goto out;
  5092. }
  5093. /*
  5094. * Do not pin the page in memory, so that memory hot-unplug
  5095. * is able to migrate it.
  5096. */
  5097. put_page(page);
  5098. kvm->arch.apic_access_page_done = true;
  5099. out:
  5100. mutex_unlock(&kvm->slots_lock);
  5101. return r;
  5102. }
  5103. static int allocate_vpid(void)
  5104. {
  5105. int vpid;
  5106. if (!enable_vpid)
  5107. return 0;
  5108. spin_lock(&vmx_vpid_lock);
  5109. vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
  5110. if (vpid < VMX_NR_VPIDS)
  5111. __set_bit(vpid, vmx_vpid_bitmap);
  5112. else
  5113. vpid = 0;
  5114. spin_unlock(&vmx_vpid_lock);
  5115. return vpid;
  5116. }
  5117. static void free_vpid(int vpid)
  5118. {
  5119. if (!enable_vpid || vpid == 0)
  5120. return;
  5121. spin_lock(&vmx_vpid_lock);
  5122. __clear_bit(vpid, vmx_vpid_bitmap);
  5123. spin_unlock(&vmx_vpid_lock);
  5124. }
  5125. static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
  5126. u32 msr, int type)
  5127. {
  5128. int f = sizeof(unsigned long);
  5129. if (!cpu_has_vmx_msr_bitmap())
  5130. return;
  5131. if (static_branch_unlikely(&enable_evmcs))
  5132. evmcs_touch_msr_bitmap();
  5133. /*
  5134. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  5135. * have the write-low and read-high bitmap offsets the wrong way round.
  5136. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  5137. */
  5138. if (msr <= 0x1fff) {
  5139. if (type & MSR_TYPE_R)
  5140. /* read-low */
  5141. __clear_bit(msr, msr_bitmap + 0x000 / f);
  5142. if (type & MSR_TYPE_W)
  5143. /* write-low */
  5144. __clear_bit(msr, msr_bitmap + 0x800 / f);
  5145. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  5146. msr &= 0x1fff;
  5147. if (type & MSR_TYPE_R)
  5148. /* read-high */
  5149. __clear_bit(msr, msr_bitmap + 0x400 / f);
  5150. if (type & MSR_TYPE_W)
  5151. /* write-high */
  5152. __clear_bit(msr, msr_bitmap + 0xc00 / f);
  5153. }
  5154. }
  5155. static __always_inline void vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
  5156. u32 msr, int type)
  5157. {
  5158. int f = sizeof(unsigned long);
  5159. if (!cpu_has_vmx_msr_bitmap())
  5160. return;
  5161. if (static_branch_unlikely(&enable_evmcs))
  5162. evmcs_touch_msr_bitmap();
  5163. /*
  5164. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  5165. * have the write-low and read-high bitmap offsets the wrong way round.
  5166. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  5167. */
  5168. if (msr <= 0x1fff) {
  5169. if (type & MSR_TYPE_R)
  5170. /* read-low */
  5171. __set_bit(msr, msr_bitmap + 0x000 / f);
  5172. if (type & MSR_TYPE_W)
  5173. /* write-low */
  5174. __set_bit(msr, msr_bitmap + 0x800 / f);
  5175. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  5176. msr &= 0x1fff;
  5177. if (type & MSR_TYPE_R)
  5178. /* read-high */
  5179. __set_bit(msr, msr_bitmap + 0x400 / f);
  5180. if (type & MSR_TYPE_W)
  5181. /* write-high */
  5182. __set_bit(msr, msr_bitmap + 0xc00 / f);
  5183. }
  5184. }
  5185. static __always_inline void vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
  5186. u32 msr, int type, bool value)
  5187. {
  5188. if (value)
  5189. vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
  5190. else
  5191. vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
  5192. }
  5193. /*
  5194. * If a msr is allowed by L0, we should check whether it is allowed by L1.
  5195. * The corresponding bit will be cleared unless both of L0 and L1 allow it.
  5196. */
  5197. static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
  5198. unsigned long *msr_bitmap_nested,
  5199. u32 msr, int type)
  5200. {
  5201. int f = sizeof(unsigned long);
  5202. /*
  5203. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  5204. * have the write-low and read-high bitmap offsets the wrong way round.
  5205. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  5206. */
  5207. if (msr <= 0x1fff) {
  5208. if (type & MSR_TYPE_R &&
  5209. !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
  5210. /* read-low */
  5211. __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
  5212. if (type & MSR_TYPE_W &&
  5213. !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
  5214. /* write-low */
  5215. __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
  5216. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  5217. msr &= 0x1fff;
  5218. if (type & MSR_TYPE_R &&
  5219. !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
  5220. /* read-high */
  5221. __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
  5222. if (type & MSR_TYPE_W &&
  5223. !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
  5224. /* write-high */
  5225. __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
  5226. }
  5227. }
  5228. static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
  5229. {
  5230. u8 mode = 0;
  5231. if (cpu_has_secondary_exec_ctrls() &&
  5232. (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
  5233. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
  5234. mode |= MSR_BITMAP_MODE_X2APIC;
  5235. if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
  5236. mode |= MSR_BITMAP_MODE_X2APIC_APICV;
  5237. }
  5238. return mode;
  5239. }
  5240. #define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
  5241. static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
  5242. u8 mode)
  5243. {
  5244. int msr;
  5245. for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
  5246. unsigned word = msr / BITS_PER_LONG;
  5247. msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
  5248. msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
  5249. }
  5250. if (mode & MSR_BITMAP_MODE_X2APIC) {
  5251. /*
  5252. * TPR reads and writes can be virtualized even if virtual interrupt
  5253. * delivery is not in use.
  5254. */
  5255. vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
  5256. if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
  5257. vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
  5258. vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
  5259. vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
  5260. }
  5261. }
  5262. }
  5263. static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
  5264. {
  5265. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5266. unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
  5267. u8 mode = vmx_msr_bitmap_mode(vcpu);
  5268. u8 changed = mode ^ vmx->msr_bitmap_mode;
  5269. if (!changed)
  5270. return;
  5271. if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
  5272. vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
  5273. vmx->msr_bitmap_mode = mode;
  5274. }
  5275. static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
  5276. {
  5277. return enable_apicv;
  5278. }
  5279. static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
  5280. {
  5281. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  5282. gfn_t gfn;
  5283. /*
  5284. * Don't need to mark the APIC access page dirty; it is never
  5285. * written to by the CPU during APIC virtualization.
  5286. */
  5287. if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
  5288. gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
  5289. kvm_vcpu_mark_page_dirty(vcpu, gfn);
  5290. }
  5291. if (nested_cpu_has_posted_intr(vmcs12)) {
  5292. gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
  5293. kvm_vcpu_mark_page_dirty(vcpu, gfn);
  5294. }
  5295. }
  5296. static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
  5297. {
  5298. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5299. int max_irr;
  5300. void *vapic_page;
  5301. u16 status;
  5302. if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
  5303. return;
  5304. vmx->nested.pi_pending = false;
  5305. if (!pi_test_and_clear_on(vmx->nested.pi_desc))
  5306. return;
  5307. max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
  5308. if (max_irr != 256) {
  5309. vapic_page = kmap(vmx->nested.virtual_apic_page);
  5310. __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
  5311. vapic_page, &max_irr);
  5312. kunmap(vmx->nested.virtual_apic_page);
  5313. status = vmcs_read16(GUEST_INTR_STATUS);
  5314. if ((u8)max_irr > ((u8)status & 0xff)) {
  5315. status &= ~0xff;
  5316. status |= (u8)max_irr;
  5317. vmcs_write16(GUEST_INTR_STATUS, status);
  5318. }
  5319. }
  5320. nested_mark_vmcs12_pages_dirty(vcpu);
  5321. }
  5322. static u8 vmx_get_rvi(void)
  5323. {
  5324. return vmcs_read16(GUEST_INTR_STATUS) & 0xff;
  5325. }
  5326. static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
  5327. {
  5328. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5329. void *vapic_page;
  5330. u32 vppr;
  5331. int rvi;
  5332. if (WARN_ON_ONCE(!is_guest_mode(vcpu)) ||
  5333. !nested_cpu_has_vid(get_vmcs12(vcpu)) ||
  5334. WARN_ON_ONCE(!vmx->nested.virtual_apic_page))
  5335. return false;
  5336. rvi = vmx_get_rvi();
  5337. vapic_page = kmap(vmx->nested.virtual_apic_page);
  5338. vppr = *((u32 *)(vapic_page + APIC_PROCPRI));
  5339. kunmap(vmx->nested.virtual_apic_page);
  5340. return ((rvi & 0xf0) > (vppr & 0xf0));
  5341. }
  5342. static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
  5343. bool nested)
  5344. {
  5345. #ifdef CONFIG_SMP
  5346. int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
  5347. if (vcpu->mode == IN_GUEST_MODE) {
  5348. /*
  5349. * The vector of interrupt to be delivered to vcpu had
  5350. * been set in PIR before this function.
  5351. *
  5352. * Following cases will be reached in this block, and
  5353. * we always send a notification event in all cases as
  5354. * explained below.
  5355. *
  5356. * Case 1: vcpu keeps in non-root mode. Sending a
  5357. * notification event posts the interrupt to vcpu.
  5358. *
  5359. * Case 2: vcpu exits to root mode and is still
  5360. * runnable. PIR will be synced to vIRR before the
  5361. * next vcpu entry. Sending a notification event in
  5362. * this case has no effect, as vcpu is not in root
  5363. * mode.
  5364. *
  5365. * Case 3: vcpu exits to root mode and is blocked.
  5366. * vcpu_block() has already synced PIR to vIRR and
  5367. * never blocks vcpu if vIRR is not cleared. Therefore,
  5368. * a blocked vcpu here does not wait for any requested
  5369. * interrupts in PIR, and sending a notification event
  5370. * which has no effect is safe here.
  5371. */
  5372. apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
  5373. return true;
  5374. }
  5375. #endif
  5376. return false;
  5377. }
  5378. static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
  5379. int vector)
  5380. {
  5381. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5382. if (is_guest_mode(vcpu) &&
  5383. vector == vmx->nested.posted_intr_nv) {
  5384. /*
  5385. * If a posted intr is not recognized by hardware,
  5386. * we will accomplish it in the next vmentry.
  5387. */
  5388. vmx->nested.pi_pending = true;
  5389. kvm_make_request(KVM_REQ_EVENT, vcpu);
  5390. /* the PIR and ON have been set by L1. */
  5391. if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
  5392. kvm_vcpu_kick(vcpu);
  5393. return 0;
  5394. }
  5395. return -1;
  5396. }
  5397. /*
  5398. * Send interrupt to vcpu via posted interrupt way.
  5399. * 1. If target vcpu is running(non-root mode), send posted interrupt
  5400. * notification to vcpu and hardware will sync PIR to vIRR atomically.
  5401. * 2. If target vcpu isn't running(root mode), kick it to pick up the
  5402. * interrupt from PIR in next vmentry.
  5403. */
  5404. static int vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
  5405. {
  5406. struct vcpu_vmx *vmx = to_vmx(vcpu);
  5407. int r;
  5408. r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
  5409. if (!r)
  5410. return 0;
  5411. if (!vcpu->arch.apicv_active)
  5412. return -1;
  5413. if (pi_test_and_set_pir(vector, &vmx->pi_desc))
  5414. return 0;
  5415. /* If a previous notification has sent the IPI, nothing to do. */
  5416. if (pi_test_and_set_on(&vmx->pi_desc))
  5417. return 0;
  5418. if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
  5419. kvm_vcpu_kick(vcpu);
  5420. return 0;
  5421. }
  5422. /*
  5423. * Set up the vmcs's constant host-state fields, i.e., host-state fields that
  5424. * will not change in the lifetime of the guest.
  5425. * Note that host-state that does change is set elsewhere. E.g., host-state
  5426. * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
  5427. */
  5428. static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
  5429. {
  5430. u32 low32, high32;
  5431. unsigned long tmpl;
  5432. struct desc_ptr dt;
  5433. unsigned long cr0, cr3, cr4;
  5434. cr0 = read_cr0();
  5435. WARN_ON(cr0 & X86_CR0_TS);
  5436. vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
  5437. /*
  5438. * Save the most likely value for this task's CR3 in the VMCS.
  5439. * We can't use __get_current_cr3_fast() because we're not atomic.
  5440. */
  5441. cr3 = __read_cr3();
  5442. vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
  5443. vmx->loaded_vmcs->host_state.cr3 = cr3;
  5444. /* Save the most likely value for this task's CR4 in the VMCS. */
  5445. cr4 = cr4_read_shadow();
  5446. vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
  5447. vmx->loaded_vmcs->host_state.cr4 = cr4;
  5448. vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
  5449. #ifdef CONFIG_X86_64
  5450. /*
  5451. * Load null selectors, so we can avoid reloading them in
  5452. * vmx_prepare_switch_to_host(), in case userspace uses
  5453. * the null selectors too (the expected case).
  5454. */
  5455. vmcs_write16(HOST_DS_SELECTOR, 0);
  5456. vmcs_write16(HOST_ES_SELECTOR, 0);
  5457. #else
  5458. vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  5459. vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  5460. #endif
  5461. vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  5462. vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
  5463. store_idt(&dt);
  5464. vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
  5465. vmx->host_idt_base = dt.address;
  5466. vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
  5467. rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
  5468. vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
  5469. rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
  5470. vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
  5471. if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
  5472. rdmsr(MSR_IA32_CR_PAT, low32, high32);
  5473. vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
  5474. }
  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. vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
  5736. /* 22.2.1, 20.8.1 */
  5737. vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
  5738. vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
  5739. vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
  5740. set_cr4_guest_host_mask(vmx);
  5741. if (vmx_xsaves_supported())
  5742. vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
  5743. if (enable_pml) {
  5744. ASSERT(vmx->pml_pg);
  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. r = alloc_kvm_area();
  6865. if (r)
  6866. goto out;
  6867. return 0;
  6868. out:
  6869. for (i = 0; i < VMX_BITMAP_NR; i++)
  6870. free_page((unsigned long)vmx_bitmap[i]);
  6871. return r;
  6872. }
  6873. static __exit void hardware_unsetup(void)
  6874. {
  6875. int i;
  6876. for (i = 0; i < VMX_BITMAP_NR; i++)
  6877. free_page((unsigned long)vmx_bitmap[i]);
  6878. free_kvm_area();
  6879. }
  6880. /*
  6881. * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
  6882. * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
  6883. */
  6884. static int handle_pause(struct kvm_vcpu *vcpu)
  6885. {
  6886. if (!kvm_pause_in_guest(vcpu->kvm))
  6887. grow_ple_window(vcpu);
  6888. /*
  6889. * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
  6890. * VM-execution control is ignored if CPL > 0. OTOH, KVM
  6891. * never set PAUSE_EXITING and just set PLE if supported,
  6892. * so the vcpu must be CPL=0 if it gets a PAUSE exit.
  6893. */
  6894. kvm_vcpu_on_spin(vcpu, true);
  6895. return kvm_skip_emulated_instruction(vcpu);
  6896. }
  6897. static int handle_nop(struct kvm_vcpu *vcpu)
  6898. {
  6899. return kvm_skip_emulated_instruction(vcpu);
  6900. }
  6901. static int handle_mwait(struct kvm_vcpu *vcpu)
  6902. {
  6903. printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
  6904. return handle_nop(vcpu);
  6905. }
  6906. static int handle_invalid_op(struct kvm_vcpu *vcpu)
  6907. {
  6908. kvm_queue_exception(vcpu, UD_VECTOR);
  6909. return 1;
  6910. }
  6911. static int handle_monitor_trap(struct kvm_vcpu *vcpu)
  6912. {
  6913. return 1;
  6914. }
  6915. static int handle_monitor(struct kvm_vcpu *vcpu)
  6916. {
  6917. printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
  6918. return handle_nop(vcpu);
  6919. }
  6920. /*
  6921. * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
  6922. * set the success or error code of an emulated VMX instruction, as specified
  6923. * by Vol 2B, VMX Instruction Reference, "Conventions".
  6924. */
  6925. static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
  6926. {
  6927. vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
  6928. & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
  6929. X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
  6930. }
  6931. static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
  6932. {
  6933. vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
  6934. & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
  6935. X86_EFLAGS_SF | X86_EFLAGS_OF))
  6936. | X86_EFLAGS_CF);
  6937. }
  6938. static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
  6939. u32 vm_instruction_error)
  6940. {
  6941. if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
  6942. /*
  6943. * failValid writes the error number to the current VMCS, which
  6944. * can't be done there isn't a current VMCS.
  6945. */
  6946. nested_vmx_failInvalid(vcpu);
  6947. return;
  6948. }
  6949. vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
  6950. & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
  6951. X86_EFLAGS_SF | X86_EFLAGS_OF))
  6952. | X86_EFLAGS_ZF);
  6953. get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
  6954. /*
  6955. * We don't need to force a shadow sync because
  6956. * VM_INSTRUCTION_ERROR is not shadowed
  6957. */
  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 (addr_size == 1)
  7011. off = (gva_t)sign_extend64(off, 31);
  7012. else if (addr_size == 0)
  7013. off = (gva_t)sign_extend64(off, 15);
  7014. if (base_is_valid)
  7015. off += kvm_register_read(vcpu, base_reg);
  7016. if (index_is_valid)
  7017. off += kvm_register_read(vcpu, index_reg)<<scaling;
  7018. vmx_get_segment(vcpu, &s, seg_reg);
  7019. /*
  7020. * The effective address, i.e. @off, of a memory operand is truncated
  7021. * based on the address size of the instruction. Note that this is
  7022. * the *effective address*, i.e. the address prior to accounting for
  7023. * the segment's base.
  7024. */
  7025. if (addr_size == 1) /* 32 bit */
  7026. off &= 0xffffffff;
  7027. else if (addr_size == 0) /* 16 bit */
  7028. off &= 0xffff;
  7029. /* Checks for #GP/#SS exceptions. */
  7030. exn = false;
  7031. if (is_long_mode(vcpu)) {
  7032. /*
  7033. * The virtual/linear address is never truncated in 64-bit
  7034. * mode, e.g. a 32-bit address size can yield a 64-bit virtual
  7035. * address when using FS/GS with a non-zero base.
  7036. */
  7037. *ret = s.base + off;
  7038. /* Long mode: #GP(0)/#SS(0) if the memory address is in a
  7039. * non-canonical form. This is the only check on the memory
  7040. * destination for long mode!
  7041. */
  7042. exn = is_noncanonical_address(*ret, vcpu);
  7043. } else if (is_protmode(vcpu)) {
  7044. /*
  7045. * When not in long mode, the virtual/linear address is
  7046. * unconditionally truncated to 32 bits regardless of the
  7047. * address size.
  7048. */
  7049. *ret = (s.base + off) & 0xffffffff;
  7050. /* Protected mode: apply checks for segment validity in the
  7051. * following order:
  7052. * - segment type check (#GP(0) may be thrown)
  7053. * - usability check (#GP(0)/#SS(0))
  7054. * - limit check (#GP(0)/#SS(0))
  7055. */
  7056. if (wr)
  7057. /* #GP(0) if the destination operand is located in a
  7058. * read-only data segment or any code segment.
  7059. */
  7060. exn = ((s.type & 0xa) == 0 || (s.type & 8));
  7061. else
  7062. /* #GP(0) if the source operand is located in an
  7063. * execute-only code segment
  7064. */
  7065. exn = ((s.type & 0xa) == 8);
  7066. if (exn) {
  7067. kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
  7068. return 1;
  7069. }
  7070. /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
  7071. */
  7072. exn = (s.unusable != 0);
  7073. /*
  7074. * Protected mode: #GP(0)/#SS(0) if the memory operand is
  7075. * outside the segment limit. All CPUs that support VMX ignore
  7076. * limit checks for flat segments, i.e. segments with base==0,
  7077. * limit==0xffffffff and of type expand-up data or code.
  7078. */
  7079. if (!(s.base == 0 && s.limit == 0xffffffff &&
  7080. ((s.type & 8) || !(s.type & 4))))
  7081. exn = exn || (off + sizeof(u64) > s.limit);
  7082. }
  7083. if (exn) {
  7084. kvm_queue_exception_e(vcpu,
  7085. seg_reg == VCPU_SREG_SS ?
  7086. SS_VECTOR : GP_VECTOR,
  7087. 0);
  7088. return 1;
  7089. }
  7090. return 0;
  7091. }
  7092. static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
  7093. {
  7094. gva_t gva;
  7095. struct x86_exception e;
  7096. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  7097. vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
  7098. return 1;
  7099. if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
  7100. kvm_inject_page_fault(vcpu, &e);
  7101. return 1;
  7102. }
  7103. return 0;
  7104. }
  7105. /*
  7106. * Allocate a shadow VMCS and associate it with the currently loaded
  7107. * VMCS, unless such a shadow VMCS already exists. The newly allocated
  7108. * VMCS is also VMCLEARed, so that it is ready for use.
  7109. */
  7110. static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
  7111. {
  7112. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7113. struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
  7114. /*
  7115. * We should allocate a shadow vmcs for vmcs01 only when L1
  7116. * executes VMXON and free it when L1 executes VMXOFF.
  7117. * As it is invalid to execute VMXON twice, we shouldn't reach
  7118. * here when vmcs01 already have an allocated shadow vmcs.
  7119. */
  7120. WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
  7121. if (!loaded_vmcs->shadow_vmcs) {
  7122. loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
  7123. if (loaded_vmcs->shadow_vmcs)
  7124. vmcs_clear(loaded_vmcs->shadow_vmcs);
  7125. }
  7126. return loaded_vmcs->shadow_vmcs;
  7127. }
  7128. static int enter_vmx_operation(struct kvm_vcpu *vcpu)
  7129. {
  7130. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7131. int r;
  7132. r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
  7133. if (r < 0)
  7134. goto out_vmcs02;
  7135. vmx->nested.cached_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL);
  7136. if (!vmx->nested.cached_vmcs12)
  7137. goto out_cached_vmcs12;
  7138. vmx->nested.cached_shadow_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL);
  7139. if (!vmx->nested.cached_shadow_vmcs12)
  7140. goto out_cached_shadow_vmcs12;
  7141. if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
  7142. goto out_shadow_vmcs;
  7143. hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
  7144. HRTIMER_MODE_REL_PINNED);
  7145. vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
  7146. vmx->nested.vpid02 = allocate_vpid();
  7147. vmx->nested.vmxon = true;
  7148. return 0;
  7149. out_shadow_vmcs:
  7150. kfree(vmx->nested.cached_shadow_vmcs12);
  7151. out_cached_shadow_vmcs12:
  7152. kfree(vmx->nested.cached_vmcs12);
  7153. out_cached_vmcs12:
  7154. free_loaded_vmcs(&vmx->nested.vmcs02);
  7155. out_vmcs02:
  7156. return -ENOMEM;
  7157. }
  7158. /*
  7159. * Emulate the VMXON instruction.
  7160. * Currently, we just remember that VMX is active, and do not save or even
  7161. * inspect the argument to VMXON (the so-called "VMXON pointer") because we
  7162. * do not currently need to store anything in that guest-allocated memory
  7163. * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
  7164. * argument is different from the VMXON pointer (which the spec says they do).
  7165. */
  7166. static int handle_vmon(struct kvm_vcpu *vcpu)
  7167. {
  7168. int ret;
  7169. gpa_t vmptr;
  7170. struct page *page;
  7171. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7172. const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
  7173. | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  7174. /*
  7175. * The Intel VMX Instruction Reference lists a bunch of bits that are
  7176. * prerequisite to running VMXON, most notably cr4.VMXE must be set to
  7177. * 1 (see vmx_set_cr4() for when we allow the guest to set this).
  7178. * Otherwise, we should fail with #UD. But most faulting conditions
  7179. * have already been checked by hardware, prior to the VM-exit for
  7180. * VMXON. We do test guest cr4.VMXE because processor CR4 always has
  7181. * that bit set to 1 in non-root mode.
  7182. */
  7183. if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
  7184. kvm_queue_exception(vcpu, UD_VECTOR);
  7185. return 1;
  7186. }
  7187. /* CPL=0 must be checked manually. */
  7188. if (vmx_get_cpl(vcpu)) {
  7189. kvm_inject_gp(vcpu, 0);
  7190. return 1;
  7191. }
  7192. if (vmx->nested.vmxon) {
  7193. nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
  7194. return kvm_skip_emulated_instruction(vcpu);
  7195. }
  7196. if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
  7197. != VMXON_NEEDED_FEATURES) {
  7198. kvm_inject_gp(vcpu, 0);
  7199. return 1;
  7200. }
  7201. if (nested_vmx_get_vmptr(vcpu, &vmptr))
  7202. return 1;
  7203. /*
  7204. * SDM 3: 24.11.5
  7205. * The first 4 bytes of VMXON region contain the supported
  7206. * VMCS revision identifier
  7207. *
  7208. * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
  7209. * which replaces physical address width with 32
  7210. */
  7211. if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
  7212. nested_vmx_failInvalid(vcpu);
  7213. return kvm_skip_emulated_instruction(vcpu);
  7214. }
  7215. page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
  7216. if (is_error_page(page)) {
  7217. nested_vmx_failInvalid(vcpu);
  7218. return kvm_skip_emulated_instruction(vcpu);
  7219. }
  7220. if (*(u32 *)kmap(page) != VMCS12_REVISION) {
  7221. kunmap(page);
  7222. kvm_release_page_clean(page);
  7223. nested_vmx_failInvalid(vcpu);
  7224. return kvm_skip_emulated_instruction(vcpu);
  7225. }
  7226. kunmap(page);
  7227. kvm_release_page_clean(page);
  7228. vmx->nested.vmxon_ptr = vmptr;
  7229. ret = enter_vmx_operation(vcpu);
  7230. if (ret)
  7231. return ret;
  7232. nested_vmx_succeed(vcpu);
  7233. return kvm_skip_emulated_instruction(vcpu);
  7234. }
  7235. /*
  7236. * Intel's VMX Instruction Reference specifies a common set of prerequisites
  7237. * for running VMX instructions (except VMXON, whose prerequisites are
  7238. * slightly different). It also specifies what exception to inject otherwise.
  7239. * Note that many of these exceptions have priority over VM exits, so they
  7240. * don't have to be checked again here.
  7241. */
  7242. static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
  7243. {
  7244. if (!to_vmx(vcpu)->nested.vmxon) {
  7245. kvm_queue_exception(vcpu, UD_VECTOR);
  7246. return 0;
  7247. }
  7248. if (vmx_get_cpl(vcpu)) {
  7249. kvm_inject_gp(vcpu, 0);
  7250. return 0;
  7251. }
  7252. return 1;
  7253. }
  7254. static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
  7255. {
  7256. vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
  7257. vmcs_write64(VMCS_LINK_POINTER, -1ull);
  7258. vmx->nested.sync_shadow_vmcs = false;
  7259. }
  7260. static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
  7261. {
  7262. if (vmx->nested.current_vmptr == -1ull)
  7263. return;
  7264. if (enable_shadow_vmcs) {
  7265. /* copy to memory all shadowed fields in case
  7266. they were modified */
  7267. copy_shadow_to_vmcs12(vmx);
  7268. vmx_disable_shadow_vmcs(vmx);
  7269. }
  7270. vmx->nested.posted_intr_nv = -1;
  7271. /* Flush VMCS12 to guest memory */
  7272. kvm_vcpu_write_guest_page(&vmx->vcpu,
  7273. vmx->nested.current_vmptr >> PAGE_SHIFT,
  7274. vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
  7275. vmx->nested.current_vmptr = -1ull;
  7276. }
  7277. /*
  7278. * Free whatever needs to be freed from vmx->nested when L1 goes down, or
  7279. * just stops using VMX.
  7280. */
  7281. static void free_nested(struct vcpu_vmx *vmx)
  7282. {
  7283. if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
  7284. return;
  7285. kvm_clear_request(KVM_REQ_GET_VMCS12_PAGES, &vmx->vcpu);
  7286. hrtimer_cancel(&vmx->nested.preemption_timer);
  7287. vmx->nested.vmxon = false;
  7288. vmx->nested.smm.vmxon = false;
  7289. free_vpid(vmx->nested.vpid02);
  7290. vmx->nested.posted_intr_nv = -1;
  7291. vmx->nested.current_vmptr = -1ull;
  7292. if (enable_shadow_vmcs) {
  7293. vmx_disable_shadow_vmcs(vmx);
  7294. vmcs_clear(vmx->vmcs01.shadow_vmcs);
  7295. free_vmcs(vmx->vmcs01.shadow_vmcs);
  7296. vmx->vmcs01.shadow_vmcs = NULL;
  7297. }
  7298. kfree(vmx->nested.cached_vmcs12);
  7299. kfree(vmx->nested.cached_shadow_vmcs12);
  7300. /* Unpin physical memory we referred to in the vmcs02 */
  7301. if (vmx->nested.apic_access_page) {
  7302. kvm_release_page_dirty(vmx->nested.apic_access_page);
  7303. vmx->nested.apic_access_page = NULL;
  7304. }
  7305. if (vmx->nested.virtual_apic_page) {
  7306. kvm_release_page_dirty(vmx->nested.virtual_apic_page);
  7307. vmx->nested.virtual_apic_page = NULL;
  7308. }
  7309. if (vmx->nested.pi_desc_page) {
  7310. kunmap(vmx->nested.pi_desc_page);
  7311. kvm_release_page_dirty(vmx->nested.pi_desc_page);
  7312. vmx->nested.pi_desc_page = NULL;
  7313. vmx->nested.pi_desc = NULL;
  7314. }
  7315. free_loaded_vmcs(&vmx->nested.vmcs02);
  7316. }
  7317. /* Emulate the VMXOFF instruction */
  7318. static int handle_vmoff(struct kvm_vcpu *vcpu)
  7319. {
  7320. if (!nested_vmx_check_permission(vcpu))
  7321. return 1;
  7322. free_nested(to_vmx(vcpu));
  7323. nested_vmx_succeed(vcpu);
  7324. return kvm_skip_emulated_instruction(vcpu);
  7325. }
  7326. /* Emulate the VMCLEAR instruction */
  7327. static int handle_vmclear(struct kvm_vcpu *vcpu)
  7328. {
  7329. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7330. u32 zero = 0;
  7331. gpa_t vmptr;
  7332. if (!nested_vmx_check_permission(vcpu))
  7333. return 1;
  7334. if (nested_vmx_get_vmptr(vcpu, &vmptr))
  7335. return 1;
  7336. if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
  7337. nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
  7338. return kvm_skip_emulated_instruction(vcpu);
  7339. }
  7340. if (vmptr == vmx->nested.vmxon_ptr) {
  7341. nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
  7342. return kvm_skip_emulated_instruction(vcpu);
  7343. }
  7344. if (vmptr == vmx->nested.current_vmptr)
  7345. nested_release_vmcs12(vmx);
  7346. kvm_vcpu_write_guest(vcpu,
  7347. vmptr + offsetof(struct vmcs12, launch_state),
  7348. &zero, sizeof(zero));
  7349. nested_vmx_succeed(vcpu);
  7350. return kvm_skip_emulated_instruction(vcpu);
  7351. }
  7352. static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
  7353. /* Emulate the VMLAUNCH instruction */
  7354. static int handle_vmlaunch(struct kvm_vcpu *vcpu)
  7355. {
  7356. return nested_vmx_run(vcpu, true);
  7357. }
  7358. /* Emulate the VMRESUME instruction */
  7359. static int handle_vmresume(struct kvm_vcpu *vcpu)
  7360. {
  7361. return nested_vmx_run(vcpu, false);
  7362. }
  7363. /*
  7364. * Read a vmcs12 field. Since these can have varying lengths and we return
  7365. * one type, we chose the biggest type (u64) and zero-extend the return value
  7366. * to that size. Note that the caller, handle_vmread, might need to use only
  7367. * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
  7368. * 64-bit fields are to be returned).
  7369. */
  7370. static inline int vmcs12_read_any(struct vmcs12 *vmcs12,
  7371. unsigned long field, u64 *ret)
  7372. {
  7373. short offset = vmcs_field_to_offset(field);
  7374. char *p;
  7375. if (offset < 0)
  7376. return offset;
  7377. p = (char *)vmcs12 + offset;
  7378. switch (vmcs_field_width(field)) {
  7379. case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
  7380. *ret = *((natural_width *)p);
  7381. return 0;
  7382. case VMCS_FIELD_WIDTH_U16:
  7383. *ret = *((u16 *)p);
  7384. return 0;
  7385. case VMCS_FIELD_WIDTH_U32:
  7386. *ret = *((u32 *)p);
  7387. return 0;
  7388. case VMCS_FIELD_WIDTH_U64:
  7389. *ret = *((u64 *)p);
  7390. return 0;
  7391. default:
  7392. WARN_ON(1);
  7393. return -ENOENT;
  7394. }
  7395. }
  7396. static inline int vmcs12_write_any(struct vmcs12 *vmcs12,
  7397. unsigned long field, u64 field_value){
  7398. short offset = vmcs_field_to_offset(field);
  7399. char *p = (char *)vmcs12 + offset;
  7400. if (offset < 0)
  7401. return offset;
  7402. switch (vmcs_field_width(field)) {
  7403. case VMCS_FIELD_WIDTH_U16:
  7404. *(u16 *)p = field_value;
  7405. return 0;
  7406. case VMCS_FIELD_WIDTH_U32:
  7407. *(u32 *)p = field_value;
  7408. return 0;
  7409. case VMCS_FIELD_WIDTH_U64:
  7410. *(u64 *)p = field_value;
  7411. return 0;
  7412. case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
  7413. *(natural_width *)p = field_value;
  7414. return 0;
  7415. default:
  7416. WARN_ON(1);
  7417. return -ENOENT;
  7418. }
  7419. }
  7420. /*
  7421. * Copy the writable VMCS shadow fields back to the VMCS12, in case
  7422. * they have been modified by the L1 guest. Note that the "read-only"
  7423. * VM-exit information fields are actually writable if the vCPU is
  7424. * configured to support "VMWRITE to any supported field in the VMCS."
  7425. */
  7426. static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
  7427. {
  7428. const u16 *fields[] = {
  7429. shadow_read_write_fields,
  7430. shadow_read_only_fields
  7431. };
  7432. const int max_fields[] = {
  7433. max_shadow_read_write_fields,
  7434. max_shadow_read_only_fields
  7435. };
  7436. int i, q;
  7437. unsigned long field;
  7438. u64 field_value;
  7439. struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
  7440. if (WARN_ON(!shadow_vmcs))
  7441. return;
  7442. preempt_disable();
  7443. vmcs_load(shadow_vmcs);
  7444. for (q = 0; q < ARRAY_SIZE(fields); q++) {
  7445. for (i = 0; i < max_fields[q]; i++) {
  7446. field = fields[q][i];
  7447. field_value = __vmcs_readl(field);
  7448. vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
  7449. }
  7450. /*
  7451. * Skip the VM-exit information fields if they are read-only.
  7452. */
  7453. if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
  7454. break;
  7455. }
  7456. vmcs_clear(shadow_vmcs);
  7457. vmcs_load(vmx->loaded_vmcs->vmcs);
  7458. preempt_enable();
  7459. }
  7460. static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
  7461. {
  7462. const u16 *fields[] = {
  7463. shadow_read_write_fields,
  7464. shadow_read_only_fields
  7465. };
  7466. const int max_fields[] = {
  7467. max_shadow_read_write_fields,
  7468. max_shadow_read_only_fields
  7469. };
  7470. int i, q;
  7471. unsigned long field;
  7472. u64 field_value = 0;
  7473. struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
  7474. if (WARN_ON(!shadow_vmcs))
  7475. return;
  7476. vmcs_load(shadow_vmcs);
  7477. for (q = 0; q < ARRAY_SIZE(fields); q++) {
  7478. for (i = 0; i < max_fields[q]; i++) {
  7479. field = fields[q][i];
  7480. vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
  7481. __vmcs_writel(field, field_value);
  7482. }
  7483. }
  7484. vmcs_clear(shadow_vmcs);
  7485. vmcs_load(vmx->loaded_vmcs->vmcs);
  7486. }
  7487. /*
  7488. * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
  7489. * used before) all generate the same failure when it is missing.
  7490. */
  7491. static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
  7492. {
  7493. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7494. if (vmx->nested.current_vmptr == -1ull) {
  7495. nested_vmx_failInvalid(vcpu);
  7496. return 0;
  7497. }
  7498. return 1;
  7499. }
  7500. static int handle_vmread(struct kvm_vcpu *vcpu)
  7501. {
  7502. unsigned long field;
  7503. u64 field_value;
  7504. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  7505. u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  7506. gva_t gva = 0;
  7507. struct vmcs12 *vmcs12;
  7508. struct x86_exception e;
  7509. if (!nested_vmx_check_permission(vcpu))
  7510. return 1;
  7511. if (!nested_vmx_check_vmcs12(vcpu))
  7512. return kvm_skip_emulated_instruction(vcpu);
  7513. if (!is_guest_mode(vcpu))
  7514. vmcs12 = get_vmcs12(vcpu);
  7515. else {
  7516. /*
  7517. * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
  7518. * to shadowed-field sets the ALU flags for VMfailInvalid.
  7519. */
  7520. if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
  7521. nested_vmx_failInvalid(vcpu);
  7522. return kvm_skip_emulated_instruction(vcpu);
  7523. }
  7524. vmcs12 = get_shadow_vmcs12(vcpu);
  7525. }
  7526. /* Decode instruction info and find the field to read */
  7527. field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
  7528. /* Read the field, zero-extended to a u64 field_value */
  7529. if (vmcs12_read_any(vmcs12, field, &field_value) < 0) {
  7530. nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
  7531. return kvm_skip_emulated_instruction(vcpu);
  7532. }
  7533. /*
  7534. * Now copy part of this value to register or memory, as requested.
  7535. * Note that the number of bits actually copied is 32 or 64 depending
  7536. * on the guest's mode (32 or 64 bit), not on the given field's length.
  7537. */
  7538. if (vmx_instruction_info & (1u << 10)) {
  7539. kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
  7540. field_value);
  7541. } else {
  7542. if (get_vmx_mem_address(vcpu, exit_qualification,
  7543. vmx_instruction_info, true, &gva))
  7544. return 1;
  7545. /* _system ok, nested_vmx_check_permission has verified cpl=0 */
  7546. if (kvm_write_guest_virt_system(vcpu, gva, &field_value,
  7547. (is_long_mode(vcpu) ? 8 : 4),
  7548. &e)) {
  7549. kvm_inject_page_fault(vcpu, &e);
  7550. return 1;
  7551. }
  7552. }
  7553. nested_vmx_succeed(vcpu);
  7554. return kvm_skip_emulated_instruction(vcpu);
  7555. }
  7556. static int handle_vmwrite(struct kvm_vcpu *vcpu)
  7557. {
  7558. unsigned long field;
  7559. gva_t gva;
  7560. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7561. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  7562. u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  7563. /* The value to write might be 32 or 64 bits, depending on L1's long
  7564. * mode, and eventually we need to write that into a field of several
  7565. * possible lengths. The code below first zero-extends the value to 64
  7566. * bit (field_value), and then copies only the appropriate number of
  7567. * bits into the vmcs12 field.
  7568. */
  7569. u64 field_value = 0;
  7570. struct x86_exception e;
  7571. struct vmcs12 *vmcs12;
  7572. if (!nested_vmx_check_permission(vcpu))
  7573. return 1;
  7574. if (!nested_vmx_check_vmcs12(vcpu))
  7575. return kvm_skip_emulated_instruction(vcpu);
  7576. if (vmx_instruction_info & (1u << 10))
  7577. field_value = kvm_register_readl(vcpu,
  7578. (((vmx_instruction_info) >> 3) & 0xf));
  7579. else {
  7580. if (get_vmx_mem_address(vcpu, exit_qualification,
  7581. vmx_instruction_info, false, &gva))
  7582. return 1;
  7583. if (kvm_read_guest_virt(vcpu, gva, &field_value,
  7584. (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
  7585. kvm_inject_page_fault(vcpu, &e);
  7586. return 1;
  7587. }
  7588. }
  7589. field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
  7590. /*
  7591. * If the vCPU supports "VMWRITE to any supported field in the
  7592. * VMCS," then the "read-only" fields are actually read/write.
  7593. */
  7594. if (vmcs_field_readonly(field) &&
  7595. !nested_cpu_has_vmwrite_any_field(vcpu)) {
  7596. nested_vmx_failValid(vcpu,
  7597. VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
  7598. return kvm_skip_emulated_instruction(vcpu);
  7599. }
  7600. if (!is_guest_mode(vcpu))
  7601. vmcs12 = get_vmcs12(vcpu);
  7602. else {
  7603. /*
  7604. * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
  7605. * to shadowed-field sets the ALU flags for VMfailInvalid.
  7606. */
  7607. if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
  7608. nested_vmx_failInvalid(vcpu);
  7609. return kvm_skip_emulated_instruction(vcpu);
  7610. }
  7611. vmcs12 = get_shadow_vmcs12(vcpu);
  7612. }
  7613. if (vmcs12_write_any(vmcs12, field, field_value) < 0) {
  7614. nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
  7615. return kvm_skip_emulated_instruction(vcpu);
  7616. }
  7617. /*
  7618. * Do not track vmcs12 dirty-state if in guest-mode
  7619. * as we actually dirty shadow vmcs12 instead of vmcs12.
  7620. */
  7621. if (!is_guest_mode(vcpu)) {
  7622. switch (field) {
  7623. #define SHADOW_FIELD_RW(x) case x:
  7624. #include "vmx_shadow_fields.h"
  7625. /*
  7626. * The fields that can be updated by L1 without a vmexit are
  7627. * always updated in the vmcs02, the others go down the slow
  7628. * path of prepare_vmcs02.
  7629. */
  7630. break;
  7631. default:
  7632. vmx->nested.dirty_vmcs12 = true;
  7633. break;
  7634. }
  7635. }
  7636. nested_vmx_succeed(vcpu);
  7637. return kvm_skip_emulated_instruction(vcpu);
  7638. }
  7639. static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
  7640. {
  7641. vmx->nested.current_vmptr = vmptr;
  7642. if (enable_shadow_vmcs) {
  7643. vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
  7644. SECONDARY_EXEC_SHADOW_VMCS);
  7645. vmcs_write64(VMCS_LINK_POINTER,
  7646. __pa(vmx->vmcs01.shadow_vmcs));
  7647. vmx->nested.sync_shadow_vmcs = true;
  7648. }
  7649. vmx->nested.dirty_vmcs12 = true;
  7650. }
  7651. /* Emulate the VMPTRLD instruction */
  7652. static int handle_vmptrld(struct kvm_vcpu *vcpu)
  7653. {
  7654. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7655. gpa_t vmptr;
  7656. if (!nested_vmx_check_permission(vcpu))
  7657. return 1;
  7658. if (nested_vmx_get_vmptr(vcpu, &vmptr))
  7659. return 1;
  7660. if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
  7661. nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
  7662. return kvm_skip_emulated_instruction(vcpu);
  7663. }
  7664. if (vmptr == vmx->nested.vmxon_ptr) {
  7665. nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
  7666. return kvm_skip_emulated_instruction(vcpu);
  7667. }
  7668. if (vmx->nested.current_vmptr != vmptr) {
  7669. struct vmcs12 *new_vmcs12;
  7670. struct page *page;
  7671. page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
  7672. if (is_error_page(page)) {
  7673. nested_vmx_failInvalid(vcpu);
  7674. return kvm_skip_emulated_instruction(vcpu);
  7675. }
  7676. new_vmcs12 = kmap(page);
  7677. if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
  7678. (new_vmcs12->hdr.shadow_vmcs &&
  7679. !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
  7680. kunmap(page);
  7681. kvm_release_page_clean(page);
  7682. nested_vmx_failValid(vcpu,
  7683. VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
  7684. return kvm_skip_emulated_instruction(vcpu);
  7685. }
  7686. nested_release_vmcs12(vmx);
  7687. /*
  7688. * Load VMCS12 from guest memory since it is not already
  7689. * cached.
  7690. */
  7691. memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
  7692. kunmap(page);
  7693. kvm_release_page_clean(page);
  7694. set_current_vmptr(vmx, vmptr);
  7695. }
  7696. nested_vmx_succeed(vcpu);
  7697. return kvm_skip_emulated_instruction(vcpu);
  7698. }
  7699. /* Emulate the VMPTRST instruction */
  7700. static int handle_vmptrst(struct kvm_vcpu *vcpu)
  7701. {
  7702. unsigned long exit_qual = vmcs_readl(EXIT_QUALIFICATION);
  7703. u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  7704. gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
  7705. struct x86_exception e;
  7706. gva_t gva;
  7707. if (!nested_vmx_check_permission(vcpu))
  7708. return 1;
  7709. if (get_vmx_mem_address(vcpu, exit_qual, instr_info, true, &gva))
  7710. return 1;
  7711. /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
  7712. if (kvm_write_guest_virt_system(vcpu, gva, (void *)&current_vmptr,
  7713. sizeof(gpa_t), &e)) {
  7714. kvm_inject_page_fault(vcpu, &e);
  7715. return 1;
  7716. }
  7717. nested_vmx_succeed(vcpu);
  7718. return kvm_skip_emulated_instruction(vcpu);
  7719. }
  7720. /* Emulate the INVEPT instruction */
  7721. static int handle_invept(struct kvm_vcpu *vcpu)
  7722. {
  7723. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7724. u32 vmx_instruction_info, types;
  7725. unsigned long type;
  7726. gva_t gva;
  7727. struct x86_exception e;
  7728. struct {
  7729. u64 eptp, gpa;
  7730. } operand;
  7731. if (!(vmx->nested.msrs.secondary_ctls_high &
  7732. SECONDARY_EXEC_ENABLE_EPT) ||
  7733. !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
  7734. kvm_queue_exception(vcpu, UD_VECTOR);
  7735. return 1;
  7736. }
  7737. if (!nested_vmx_check_permission(vcpu))
  7738. return 1;
  7739. vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  7740. type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
  7741. types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
  7742. if (type >= 32 || !(types & (1 << type))) {
  7743. nested_vmx_failValid(vcpu,
  7744. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  7745. return kvm_skip_emulated_instruction(vcpu);
  7746. }
  7747. /* According to the Intel VMX instruction reference, the memory
  7748. * operand is read even if it isn't needed (e.g., for type==global)
  7749. */
  7750. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  7751. vmx_instruction_info, false, &gva))
  7752. return 1;
  7753. if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
  7754. kvm_inject_page_fault(vcpu, &e);
  7755. return 1;
  7756. }
  7757. switch (type) {
  7758. case VMX_EPT_EXTENT_GLOBAL:
  7759. /*
  7760. * TODO: track mappings and invalidate
  7761. * single context requests appropriately
  7762. */
  7763. case VMX_EPT_EXTENT_CONTEXT:
  7764. kvm_mmu_sync_roots(vcpu);
  7765. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  7766. nested_vmx_succeed(vcpu);
  7767. break;
  7768. default:
  7769. BUG_ON(1);
  7770. break;
  7771. }
  7772. return kvm_skip_emulated_instruction(vcpu);
  7773. }
  7774. static int handle_invvpid(struct kvm_vcpu *vcpu)
  7775. {
  7776. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7777. u32 vmx_instruction_info;
  7778. unsigned long type, types;
  7779. gva_t gva;
  7780. struct x86_exception e;
  7781. struct {
  7782. u64 vpid;
  7783. u64 gla;
  7784. } operand;
  7785. if (!(vmx->nested.msrs.secondary_ctls_high &
  7786. SECONDARY_EXEC_ENABLE_VPID) ||
  7787. !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
  7788. kvm_queue_exception(vcpu, UD_VECTOR);
  7789. return 1;
  7790. }
  7791. if (!nested_vmx_check_permission(vcpu))
  7792. return 1;
  7793. vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  7794. type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
  7795. types = (vmx->nested.msrs.vpid_caps &
  7796. VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
  7797. if (type >= 32 || !(types & (1 << type))) {
  7798. nested_vmx_failValid(vcpu,
  7799. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  7800. return kvm_skip_emulated_instruction(vcpu);
  7801. }
  7802. /* according to the intel vmx instruction reference, the memory
  7803. * operand is read even if it isn't needed (e.g., for type==global)
  7804. */
  7805. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  7806. vmx_instruction_info, false, &gva))
  7807. return 1;
  7808. if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
  7809. kvm_inject_page_fault(vcpu, &e);
  7810. return 1;
  7811. }
  7812. if (operand.vpid >> 16) {
  7813. nested_vmx_failValid(vcpu,
  7814. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  7815. return kvm_skip_emulated_instruction(vcpu);
  7816. }
  7817. switch (type) {
  7818. case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
  7819. if (!operand.vpid ||
  7820. is_noncanonical_address(operand.gla, vcpu)) {
  7821. nested_vmx_failValid(vcpu,
  7822. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  7823. return kvm_skip_emulated_instruction(vcpu);
  7824. }
  7825. if (cpu_has_vmx_invvpid_individual_addr() &&
  7826. vmx->nested.vpid02) {
  7827. __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
  7828. vmx->nested.vpid02, operand.gla);
  7829. } else
  7830. __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
  7831. break;
  7832. case VMX_VPID_EXTENT_SINGLE_CONTEXT:
  7833. case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
  7834. if (!operand.vpid) {
  7835. nested_vmx_failValid(vcpu,
  7836. VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
  7837. return kvm_skip_emulated_instruction(vcpu);
  7838. }
  7839. __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
  7840. break;
  7841. case VMX_VPID_EXTENT_ALL_CONTEXT:
  7842. __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
  7843. break;
  7844. default:
  7845. WARN_ON_ONCE(1);
  7846. return kvm_skip_emulated_instruction(vcpu);
  7847. }
  7848. nested_vmx_succeed(vcpu);
  7849. return kvm_skip_emulated_instruction(vcpu);
  7850. }
  7851. static int handle_invpcid(struct kvm_vcpu *vcpu)
  7852. {
  7853. u32 vmx_instruction_info;
  7854. unsigned long type;
  7855. bool pcid_enabled;
  7856. gva_t gva;
  7857. struct x86_exception e;
  7858. unsigned i;
  7859. unsigned long roots_to_free = 0;
  7860. struct {
  7861. u64 pcid;
  7862. u64 gla;
  7863. } operand;
  7864. if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
  7865. kvm_queue_exception(vcpu, UD_VECTOR);
  7866. return 1;
  7867. }
  7868. vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  7869. type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
  7870. if (type > 3) {
  7871. kvm_inject_gp(vcpu, 0);
  7872. return 1;
  7873. }
  7874. /* According to the Intel instruction reference, the memory operand
  7875. * is read even if it isn't needed (e.g., for type==all)
  7876. */
  7877. if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
  7878. vmx_instruction_info, false, &gva))
  7879. return 1;
  7880. if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
  7881. kvm_inject_page_fault(vcpu, &e);
  7882. return 1;
  7883. }
  7884. if (operand.pcid >> 12 != 0) {
  7885. kvm_inject_gp(vcpu, 0);
  7886. return 1;
  7887. }
  7888. pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
  7889. switch (type) {
  7890. case INVPCID_TYPE_INDIV_ADDR:
  7891. if ((!pcid_enabled && (operand.pcid != 0)) ||
  7892. is_noncanonical_address(operand.gla, vcpu)) {
  7893. kvm_inject_gp(vcpu, 0);
  7894. return 1;
  7895. }
  7896. kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
  7897. return kvm_skip_emulated_instruction(vcpu);
  7898. case INVPCID_TYPE_SINGLE_CTXT:
  7899. if (!pcid_enabled && (operand.pcid != 0)) {
  7900. kvm_inject_gp(vcpu, 0);
  7901. return 1;
  7902. }
  7903. if (kvm_get_active_pcid(vcpu) == operand.pcid) {
  7904. kvm_mmu_sync_roots(vcpu);
  7905. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  7906. }
  7907. for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
  7908. if (kvm_get_pcid(vcpu, vcpu->arch.mmu.prev_roots[i].cr3)
  7909. == operand.pcid)
  7910. roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
  7911. kvm_mmu_free_roots(vcpu, roots_to_free);
  7912. /*
  7913. * If neither the current cr3 nor any of the prev_roots use the
  7914. * given PCID, then nothing needs to be done here because a
  7915. * resync will happen anyway before switching to any other CR3.
  7916. */
  7917. return kvm_skip_emulated_instruction(vcpu);
  7918. case INVPCID_TYPE_ALL_NON_GLOBAL:
  7919. /*
  7920. * Currently, KVM doesn't mark global entries in the shadow
  7921. * page tables, so a non-global flush just degenerates to a
  7922. * global flush. If needed, we could optimize this later by
  7923. * keeping track of global entries in shadow page tables.
  7924. */
  7925. /* fall-through */
  7926. case INVPCID_TYPE_ALL_INCL_GLOBAL:
  7927. kvm_mmu_unload(vcpu);
  7928. return kvm_skip_emulated_instruction(vcpu);
  7929. default:
  7930. BUG(); /* We have already checked above that type <= 3 */
  7931. }
  7932. }
  7933. static int handle_pml_full(struct kvm_vcpu *vcpu)
  7934. {
  7935. unsigned long exit_qualification;
  7936. trace_kvm_pml_full(vcpu->vcpu_id);
  7937. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  7938. /*
  7939. * PML buffer FULL happened while executing iret from NMI,
  7940. * "blocked by NMI" bit has to be set before next VM entry.
  7941. */
  7942. if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
  7943. enable_vnmi &&
  7944. (exit_qualification & INTR_INFO_UNBLOCK_NMI))
  7945. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  7946. GUEST_INTR_STATE_NMI);
  7947. /*
  7948. * PML buffer already flushed at beginning of VMEXIT. Nothing to do
  7949. * here.., and there's no userspace involvement needed for PML.
  7950. */
  7951. return 1;
  7952. }
  7953. static int handle_preemption_timer(struct kvm_vcpu *vcpu)
  7954. {
  7955. if (!to_vmx(vcpu)->req_immediate_exit)
  7956. kvm_lapic_expired_hv_timer(vcpu);
  7957. return 1;
  7958. }
  7959. static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
  7960. {
  7961. struct vcpu_vmx *vmx = to_vmx(vcpu);
  7962. int maxphyaddr = cpuid_maxphyaddr(vcpu);
  7963. /* Check for memory type validity */
  7964. switch (address & VMX_EPTP_MT_MASK) {
  7965. case VMX_EPTP_MT_UC:
  7966. if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
  7967. return false;
  7968. break;
  7969. case VMX_EPTP_MT_WB:
  7970. if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
  7971. return false;
  7972. break;
  7973. default:
  7974. return false;
  7975. }
  7976. /* only 4 levels page-walk length are valid */
  7977. if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
  7978. return false;
  7979. /* Reserved bits should not be set */
  7980. if (address >> maxphyaddr || ((address >> 7) & 0x1f))
  7981. return false;
  7982. /* AD, if set, should be supported */
  7983. if (address & VMX_EPTP_AD_ENABLE_BIT) {
  7984. if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
  7985. return false;
  7986. }
  7987. return true;
  7988. }
  7989. static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
  7990. struct vmcs12 *vmcs12)
  7991. {
  7992. u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
  7993. u64 address;
  7994. bool accessed_dirty;
  7995. struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
  7996. if (!nested_cpu_has_eptp_switching(vmcs12) ||
  7997. !nested_cpu_has_ept(vmcs12))
  7998. return 1;
  7999. if (index >= VMFUNC_EPTP_ENTRIES)
  8000. return 1;
  8001. if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
  8002. &address, index * 8, 8))
  8003. return 1;
  8004. accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
  8005. /*
  8006. * If the (L2) guest does a vmfunc to the currently
  8007. * active ept pointer, we don't have to do anything else
  8008. */
  8009. if (vmcs12->ept_pointer != address) {
  8010. if (!valid_ept_address(vcpu, address))
  8011. return 1;
  8012. kvm_mmu_unload(vcpu);
  8013. mmu->ept_ad = accessed_dirty;
  8014. mmu->base_role.ad_disabled = !accessed_dirty;
  8015. vmcs12->ept_pointer = address;
  8016. /*
  8017. * TODO: Check what's the correct approach in case
  8018. * mmu reload fails. Currently, we just let the next
  8019. * reload potentially fail
  8020. */
  8021. kvm_mmu_reload(vcpu);
  8022. }
  8023. return 0;
  8024. }
  8025. static int handle_vmfunc(struct kvm_vcpu *vcpu)
  8026. {
  8027. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8028. struct vmcs12 *vmcs12;
  8029. u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
  8030. /*
  8031. * VMFUNC is only supported for nested guests, but we always enable the
  8032. * secondary control for simplicity; for non-nested mode, fake that we
  8033. * didn't by injecting #UD.
  8034. */
  8035. if (!is_guest_mode(vcpu)) {
  8036. kvm_queue_exception(vcpu, UD_VECTOR);
  8037. return 1;
  8038. }
  8039. vmcs12 = get_vmcs12(vcpu);
  8040. if ((vmcs12->vm_function_control & (1 << function)) == 0)
  8041. goto fail;
  8042. switch (function) {
  8043. case 0:
  8044. if (nested_vmx_eptp_switching(vcpu, vmcs12))
  8045. goto fail;
  8046. break;
  8047. default:
  8048. goto fail;
  8049. }
  8050. return kvm_skip_emulated_instruction(vcpu);
  8051. fail:
  8052. nested_vmx_vmexit(vcpu, vmx->exit_reason,
  8053. vmcs_read32(VM_EXIT_INTR_INFO),
  8054. vmcs_readl(EXIT_QUALIFICATION));
  8055. return 1;
  8056. }
  8057. static int handle_encls(struct kvm_vcpu *vcpu)
  8058. {
  8059. /*
  8060. * SGX virtualization is not yet supported. There is no software
  8061. * enable bit for SGX, so we have to trap ENCLS and inject a #UD
  8062. * to prevent the guest from executing ENCLS.
  8063. */
  8064. kvm_queue_exception(vcpu, UD_VECTOR);
  8065. return 1;
  8066. }
  8067. /*
  8068. * The exit handlers return 1 if the exit was handled fully and guest execution
  8069. * may resume. Otherwise they set the kvm_run parameter to indicate what needs
  8070. * to be done to userspace and return 0.
  8071. */
  8072. static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
  8073. [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
  8074. [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
  8075. [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
  8076. [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
  8077. [EXIT_REASON_IO_INSTRUCTION] = handle_io,
  8078. [EXIT_REASON_CR_ACCESS] = handle_cr,
  8079. [EXIT_REASON_DR_ACCESS] = handle_dr,
  8080. [EXIT_REASON_CPUID] = handle_cpuid,
  8081. [EXIT_REASON_MSR_READ] = handle_rdmsr,
  8082. [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
  8083. [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
  8084. [EXIT_REASON_HLT] = handle_halt,
  8085. [EXIT_REASON_INVD] = handle_invd,
  8086. [EXIT_REASON_INVLPG] = handle_invlpg,
  8087. [EXIT_REASON_RDPMC] = handle_rdpmc,
  8088. [EXIT_REASON_VMCALL] = handle_vmcall,
  8089. [EXIT_REASON_VMCLEAR] = handle_vmclear,
  8090. [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
  8091. [EXIT_REASON_VMPTRLD] = handle_vmptrld,
  8092. [EXIT_REASON_VMPTRST] = handle_vmptrst,
  8093. [EXIT_REASON_VMREAD] = handle_vmread,
  8094. [EXIT_REASON_VMRESUME] = handle_vmresume,
  8095. [EXIT_REASON_VMWRITE] = handle_vmwrite,
  8096. [EXIT_REASON_VMOFF] = handle_vmoff,
  8097. [EXIT_REASON_VMON] = handle_vmon,
  8098. [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
  8099. [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
  8100. [EXIT_REASON_APIC_WRITE] = handle_apic_write,
  8101. [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
  8102. [EXIT_REASON_WBINVD] = handle_wbinvd,
  8103. [EXIT_REASON_XSETBV] = handle_xsetbv,
  8104. [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
  8105. [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
  8106. [EXIT_REASON_GDTR_IDTR] = handle_desc,
  8107. [EXIT_REASON_LDTR_TR] = handle_desc,
  8108. [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
  8109. [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
  8110. [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
  8111. [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
  8112. [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
  8113. [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
  8114. [EXIT_REASON_INVEPT] = handle_invept,
  8115. [EXIT_REASON_INVVPID] = handle_invvpid,
  8116. [EXIT_REASON_RDRAND] = handle_invalid_op,
  8117. [EXIT_REASON_RDSEED] = handle_invalid_op,
  8118. [EXIT_REASON_XSAVES] = handle_xsaves,
  8119. [EXIT_REASON_XRSTORS] = handle_xrstors,
  8120. [EXIT_REASON_PML_FULL] = handle_pml_full,
  8121. [EXIT_REASON_INVPCID] = handle_invpcid,
  8122. [EXIT_REASON_VMFUNC] = handle_vmfunc,
  8123. [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
  8124. [EXIT_REASON_ENCLS] = handle_encls,
  8125. };
  8126. static const int kvm_vmx_max_exit_handlers =
  8127. ARRAY_SIZE(kvm_vmx_exit_handlers);
  8128. /*
  8129. * Return true if an IO instruction with the specified port and size should cause
  8130. * a VM-exit into L1.
  8131. */
  8132. bool nested_vmx_check_io_bitmaps(struct kvm_vcpu *vcpu, unsigned int port,
  8133. int size)
  8134. {
  8135. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  8136. gpa_t bitmap, last_bitmap;
  8137. u8 b;
  8138. last_bitmap = (gpa_t)-1;
  8139. b = -1;
  8140. while (size > 0) {
  8141. if (port < 0x8000)
  8142. bitmap = vmcs12->io_bitmap_a;
  8143. else if (port < 0x10000)
  8144. bitmap = vmcs12->io_bitmap_b;
  8145. else
  8146. return true;
  8147. bitmap += (port & 0x7fff) / 8;
  8148. if (last_bitmap != bitmap)
  8149. if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
  8150. return true;
  8151. if (b & (1 << (port & 7)))
  8152. return true;
  8153. port++;
  8154. size--;
  8155. last_bitmap = bitmap;
  8156. }
  8157. return false;
  8158. }
  8159. /*
  8160. * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
  8161. * rather than handle it ourselves in L0. I.e., check whether L1 expressed
  8162. * disinterest in the current event (read or write a specific MSR) by using an
  8163. * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
  8164. */
  8165. static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
  8166. struct vmcs12 *vmcs12, u32 exit_reason)
  8167. {
  8168. u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
  8169. gpa_t bitmap;
  8170. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
  8171. return true;
  8172. /*
  8173. * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
  8174. * for the four combinations of read/write and low/high MSR numbers.
  8175. * First we need to figure out which of the four to use:
  8176. */
  8177. bitmap = vmcs12->msr_bitmap;
  8178. if (exit_reason == EXIT_REASON_MSR_WRITE)
  8179. bitmap += 2048;
  8180. if (msr_index >= 0xc0000000) {
  8181. msr_index -= 0xc0000000;
  8182. bitmap += 1024;
  8183. }
  8184. /* Then read the msr_index'th bit from this bitmap: */
  8185. if (msr_index < 1024*8) {
  8186. unsigned char b;
  8187. if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
  8188. return true;
  8189. return 1 & (b >> (msr_index & 7));
  8190. } else
  8191. return true; /* let L1 handle the wrong parameter */
  8192. }
  8193. /*
  8194. * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
  8195. * rather than handle it ourselves in L0. I.e., check if L1 wanted to
  8196. * intercept (via guest_host_mask etc.) the current event.
  8197. */
  8198. static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
  8199. struct vmcs12 *vmcs12)
  8200. {
  8201. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  8202. int cr = exit_qualification & 15;
  8203. int reg;
  8204. unsigned long val;
  8205. switch ((exit_qualification >> 4) & 3) {
  8206. case 0: /* mov to cr */
  8207. reg = (exit_qualification >> 8) & 15;
  8208. val = kvm_register_readl(vcpu, reg);
  8209. switch (cr) {
  8210. case 0:
  8211. if (vmcs12->cr0_guest_host_mask &
  8212. (val ^ vmcs12->cr0_read_shadow))
  8213. return true;
  8214. break;
  8215. case 3:
  8216. if ((vmcs12->cr3_target_count >= 1 &&
  8217. vmcs12->cr3_target_value0 == val) ||
  8218. (vmcs12->cr3_target_count >= 2 &&
  8219. vmcs12->cr3_target_value1 == val) ||
  8220. (vmcs12->cr3_target_count >= 3 &&
  8221. vmcs12->cr3_target_value2 == val) ||
  8222. (vmcs12->cr3_target_count >= 4 &&
  8223. vmcs12->cr3_target_value3 == val))
  8224. return false;
  8225. if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
  8226. return true;
  8227. break;
  8228. case 4:
  8229. if (vmcs12->cr4_guest_host_mask &
  8230. (vmcs12->cr4_read_shadow ^ val))
  8231. return true;
  8232. break;
  8233. case 8:
  8234. if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
  8235. return true;
  8236. break;
  8237. }
  8238. break;
  8239. case 2: /* clts */
  8240. if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
  8241. (vmcs12->cr0_read_shadow & X86_CR0_TS))
  8242. return true;
  8243. break;
  8244. case 1: /* mov from cr */
  8245. switch (cr) {
  8246. case 3:
  8247. if (vmcs12->cpu_based_vm_exec_control &
  8248. CPU_BASED_CR3_STORE_EXITING)
  8249. return true;
  8250. break;
  8251. case 8:
  8252. if (vmcs12->cpu_based_vm_exec_control &
  8253. CPU_BASED_CR8_STORE_EXITING)
  8254. return true;
  8255. break;
  8256. }
  8257. break;
  8258. case 3: /* lmsw */
  8259. /*
  8260. * lmsw can change bits 1..3 of cr0, and only set bit 0 of
  8261. * cr0. Other attempted changes are ignored, with no exit.
  8262. */
  8263. val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
  8264. if (vmcs12->cr0_guest_host_mask & 0xe &
  8265. (val ^ vmcs12->cr0_read_shadow))
  8266. return true;
  8267. if ((vmcs12->cr0_guest_host_mask & 0x1) &&
  8268. !(vmcs12->cr0_read_shadow & 0x1) &&
  8269. (val & 0x1))
  8270. return true;
  8271. break;
  8272. }
  8273. return false;
  8274. }
  8275. static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
  8276. struct vmcs12 *vmcs12, gpa_t bitmap)
  8277. {
  8278. u32 vmx_instruction_info;
  8279. unsigned long field;
  8280. u8 b;
  8281. if (!nested_cpu_has_shadow_vmcs(vmcs12))
  8282. return true;
  8283. /* Decode instruction info and find the field to access */
  8284. vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  8285. field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
  8286. /* Out-of-range fields always cause a VM exit from L2 to L1 */
  8287. if (field >> 15)
  8288. return true;
  8289. if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
  8290. return true;
  8291. return 1 & (b >> (field & 7));
  8292. }
  8293. /*
  8294. * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
  8295. * should handle it ourselves in L0 (and then continue L2). Only call this
  8296. * when in is_guest_mode (L2).
  8297. */
  8298. static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
  8299. {
  8300. u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  8301. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8302. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  8303. if (vmx->nested.nested_run_pending)
  8304. return false;
  8305. if (unlikely(vmx->fail)) {
  8306. pr_info_ratelimited("%s failed vm entry %x\n", __func__,
  8307. vmcs_read32(VM_INSTRUCTION_ERROR));
  8308. return true;
  8309. }
  8310. /*
  8311. * The host physical addresses of some pages of guest memory
  8312. * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
  8313. * Page). The CPU may write to these pages via their host
  8314. * physical address while L2 is running, bypassing any
  8315. * address-translation-based dirty tracking (e.g. EPT write
  8316. * protection).
  8317. *
  8318. * Mark them dirty on every exit from L2 to prevent them from
  8319. * getting out of sync with dirty tracking.
  8320. */
  8321. nested_mark_vmcs12_pages_dirty(vcpu);
  8322. trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
  8323. vmcs_readl(EXIT_QUALIFICATION),
  8324. vmx->idt_vectoring_info,
  8325. intr_info,
  8326. vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
  8327. KVM_ISA_VMX);
  8328. switch (exit_reason) {
  8329. case EXIT_REASON_EXCEPTION_NMI:
  8330. if (is_nmi(intr_info))
  8331. return false;
  8332. else if (is_page_fault(intr_info))
  8333. return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
  8334. else if (is_no_device(intr_info) &&
  8335. !(vmcs12->guest_cr0 & X86_CR0_TS))
  8336. return false;
  8337. else if (is_debug(intr_info) &&
  8338. vcpu->guest_debug &
  8339. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
  8340. return false;
  8341. else if (is_breakpoint(intr_info) &&
  8342. vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  8343. return false;
  8344. return vmcs12->exception_bitmap &
  8345. (1u << (intr_info & INTR_INFO_VECTOR_MASK));
  8346. case EXIT_REASON_EXTERNAL_INTERRUPT:
  8347. return false;
  8348. case EXIT_REASON_TRIPLE_FAULT:
  8349. return true;
  8350. case EXIT_REASON_PENDING_INTERRUPT:
  8351. return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
  8352. case EXIT_REASON_NMI_WINDOW:
  8353. return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
  8354. case EXIT_REASON_TASK_SWITCH:
  8355. return true;
  8356. case EXIT_REASON_CPUID:
  8357. return true;
  8358. case EXIT_REASON_HLT:
  8359. return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
  8360. case EXIT_REASON_INVD:
  8361. return true;
  8362. case EXIT_REASON_INVLPG:
  8363. return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
  8364. case EXIT_REASON_RDPMC:
  8365. return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
  8366. case EXIT_REASON_RDRAND:
  8367. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
  8368. case EXIT_REASON_RDSEED:
  8369. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
  8370. case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
  8371. return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
  8372. case EXIT_REASON_VMREAD:
  8373. return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
  8374. vmcs12->vmread_bitmap);
  8375. case EXIT_REASON_VMWRITE:
  8376. return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
  8377. vmcs12->vmwrite_bitmap);
  8378. case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
  8379. case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
  8380. case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
  8381. case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
  8382. case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
  8383. /*
  8384. * VMX instructions trap unconditionally. This allows L1 to
  8385. * emulate them for its L2 guest, i.e., allows 3-level nesting!
  8386. */
  8387. return true;
  8388. case EXIT_REASON_CR_ACCESS:
  8389. return nested_vmx_exit_handled_cr(vcpu, vmcs12);
  8390. case EXIT_REASON_DR_ACCESS:
  8391. return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
  8392. case EXIT_REASON_IO_INSTRUCTION:
  8393. return nested_vmx_exit_handled_io(vcpu, vmcs12);
  8394. case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
  8395. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
  8396. case EXIT_REASON_MSR_READ:
  8397. case EXIT_REASON_MSR_WRITE:
  8398. return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
  8399. case EXIT_REASON_INVALID_STATE:
  8400. return true;
  8401. case EXIT_REASON_MWAIT_INSTRUCTION:
  8402. return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
  8403. case EXIT_REASON_MONITOR_TRAP_FLAG:
  8404. return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
  8405. case EXIT_REASON_MONITOR_INSTRUCTION:
  8406. return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
  8407. case EXIT_REASON_PAUSE_INSTRUCTION:
  8408. return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
  8409. nested_cpu_has2(vmcs12,
  8410. SECONDARY_EXEC_PAUSE_LOOP_EXITING);
  8411. case EXIT_REASON_MCE_DURING_VMENTRY:
  8412. return false;
  8413. case EXIT_REASON_TPR_BELOW_THRESHOLD:
  8414. return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
  8415. case EXIT_REASON_APIC_ACCESS:
  8416. case EXIT_REASON_APIC_WRITE:
  8417. case EXIT_REASON_EOI_INDUCED:
  8418. /*
  8419. * The controls for "virtualize APIC accesses," "APIC-
  8420. * register virtualization," and "virtual-interrupt
  8421. * delivery" only come from vmcs12.
  8422. */
  8423. return true;
  8424. case EXIT_REASON_EPT_VIOLATION:
  8425. /*
  8426. * L0 always deals with the EPT violation. If nested EPT is
  8427. * used, and the nested mmu code discovers that the address is
  8428. * missing in the guest EPT table (EPT12), the EPT violation
  8429. * will be injected with nested_ept_inject_page_fault()
  8430. */
  8431. return false;
  8432. case EXIT_REASON_EPT_MISCONFIG:
  8433. /*
  8434. * L2 never uses directly L1's EPT, but rather L0's own EPT
  8435. * table (shadow on EPT) or a merged EPT table that L0 built
  8436. * (EPT on EPT). So any problems with the structure of the
  8437. * table is L0's fault.
  8438. */
  8439. return false;
  8440. case EXIT_REASON_INVPCID:
  8441. return
  8442. nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
  8443. nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
  8444. case EXIT_REASON_WBINVD:
  8445. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
  8446. case EXIT_REASON_XSETBV:
  8447. return true;
  8448. case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
  8449. /*
  8450. * This should never happen, since it is not possible to
  8451. * set XSS to a non-zero value---neither in L1 nor in L2.
  8452. * If if it were, XSS would have to be checked against
  8453. * the XSS exit bitmap in vmcs12.
  8454. */
  8455. return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
  8456. case EXIT_REASON_PREEMPTION_TIMER:
  8457. return false;
  8458. case EXIT_REASON_PML_FULL:
  8459. /* We emulate PML support to L1. */
  8460. return false;
  8461. case EXIT_REASON_VMFUNC:
  8462. /* VM functions are emulated through L2->L0 vmexits. */
  8463. return false;
  8464. case EXIT_REASON_ENCLS:
  8465. /* SGX is never exposed to L1 */
  8466. return false;
  8467. default:
  8468. return true;
  8469. }
  8470. }
  8471. static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
  8472. {
  8473. u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  8474. /*
  8475. * At this point, the exit interruption info in exit_intr_info
  8476. * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
  8477. * we need to query the in-kernel LAPIC.
  8478. */
  8479. WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
  8480. if ((exit_intr_info &
  8481. (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
  8482. (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
  8483. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  8484. vmcs12->vm_exit_intr_error_code =
  8485. vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
  8486. }
  8487. nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
  8488. vmcs_readl(EXIT_QUALIFICATION));
  8489. return 1;
  8490. }
  8491. static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
  8492. {
  8493. *info1 = vmcs_readl(EXIT_QUALIFICATION);
  8494. *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
  8495. }
  8496. static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
  8497. {
  8498. if (vmx->pml_pg) {
  8499. __free_page(vmx->pml_pg);
  8500. vmx->pml_pg = NULL;
  8501. }
  8502. }
  8503. static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
  8504. {
  8505. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8506. u64 *pml_buf;
  8507. u16 pml_idx;
  8508. pml_idx = vmcs_read16(GUEST_PML_INDEX);
  8509. /* Do nothing if PML buffer is empty */
  8510. if (pml_idx == (PML_ENTITY_NUM - 1))
  8511. return;
  8512. /* PML index always points to next available PML buffer entity */
  8513. if (pml_idx >= PML_ENTITY_NUM)
  8514. pml_idx = 0;
  8515. else
  8516. pml_idx++;
  8517. pml_buf = page_address(vmx->pml_pg);
  8518. for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
  8519. u64 gpa;
  8520. gpa = pml_buf[pml_idx];
  8521. WARN_ON(gpa & (PAGE_SIZE - 1));
  8522. kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
  8523. }
  8524. /* reset PML index */
  8525. vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
  8526. }
  8527. /*
  8528. * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
  8529. * Called before reporting dirty_bitmap to userspace.
  8530. */
  8531. static void kvm_flush_pml_buffers(struct kvm *kvm)
  8532. {
  8533. int i;
  8534. struct kvm_vcpu *vcpu;
  8535. /*
  8536. * We only need to kick vcpu out of guest mode here, as PML buffer
  8537. * is flushed at beginning of all VMEXITs, and it's obvious that only
  8538. * vcpus running in guest are possible to have unflushed GPAs in PML
  8539. * buffer.
  8540. */
  8541. kvm_for_each_vcpu(i, vcpu, kvm)
  8542. kvm_vcpu_kick(vcpu);
  8543. }
  8544. static void vmx_dump_sel(char *name, uint32_t sel)
  8545. {
  8546. pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
  8547. name, vmcs_read16(sel),
  8548. vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
  8549. vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
  8550. vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
  8551. }
  8552. static void vmx_dump_dtsel(char *name, uint32_t limit)
  8553. {
  8554. pr_err("%s limit=0x%08x, base=0x%016lx\n",
  8555. name, vmcs_read32(limit),
  8556. vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
  8557. }
  8558. static void dump_vmcs(void)
  8559. {
  8560. u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
  8561. u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
  8562. u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  8563. u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
  8564. u32 secondary_exec_control = 0;
  8565. unsigned long cr4 = vmcs_readl(GUEST_CR4);
  8566. u64 efer = vmcs_read64(GUEST_IA32_EFER);
  8567. int i, n;
  8568. if (cpu_has_secondary_exec_ctrls())
  8569. secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  8570. pr_err("*** Guest State ***\n");
  8571. pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
  8572. vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
  8573. vmcs_readl(CR0_GUEST_HOST_MASK));
  8574. pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
  8575. cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
  8576. pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
  8577. if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
  8578. (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
  8579. {
  8580. pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
  8581. vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
  8582. pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
  8583. vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
  8584. }
  8585. pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
  8586. vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
  8587. pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
  8588. vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
  8589. pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
  8590. vmcs_readl(GUEST_SYSENTER_ESP),
  8591. vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
  8592. vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
  8593. vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
  8594. vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
  8595. vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
  8596. vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
  8597. vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
  8598. vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
  8599. vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
  8600. vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
  8601. vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
  8602. if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
  8603. (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
  8604. pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
  8605. efer, vmcs_read64(GUEST_IA32_PAT));
  8606. pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
  8607. vmcs_read64(GUEST_IA32_DEBUGCTL),
  8608. vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
  8609. if (cpu_has_load_perf_global_ctrl &&
  8610. vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
  8611. pr_err("PerfGlobCtl = 0x%016llx\n",
  8612. vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
  8613. if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
  8614. pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
  8615. pr_err("Interruptibility = %08x ActivityState = %08x\n",
  8616. vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
  8617. vmcs_read32(GUEST_ACTIVITY_STATE));
  8618. if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
  8619. pr_err("InterruptStatus = %04x\n",
  8620. vmcs_read16(GUEST_INTR_STATUS));
  8621. pr_err("*** Host State ***\n");
  8622. pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
  8623. vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
  8624. pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
  8625. vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
  8626. vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
  8627. vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
  8628. vmcs_read16(HOST_TR_SELECTOR));
  8629. pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
  8630. vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
  8631. vmcs_readl(HOST_TR_BASE));
  8632. pr_err("GDTBase=%016lx IDTBase=%016lx\n",
  8633. vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
  8634. pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
  8635. vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
  8636. vmcs_readl(HOST_CR4));
  8637. pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
  8638. vmcs_readl(HOST_IA32_SYSENTER_ESP),
  8639. vmcs_read32(HOST_IA32_SYSENTER_CS),
  8640. vmcs_readl(HOST_IA32_SYSENTER_EIP));
  8641. if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
  8642. pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
  8643. vmcs_read64(HOST_IA32_EFER),
  8644. vmcs_read64(HOST_IA32_PAT));
  8645. if (cpu_has_load_perf_global_ctrl &&
  8646. vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
  8647. pr_err("PerfGlobCtl = 0x%016llx\n",
  8648. vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
  8649. pr_err("*** Control State ***\n");
  8650. pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
  8651. pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
  8652. pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
  8653. pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
  8654. vmcs_read32(EXCEPTION_BITMAP),
  8655. vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
  8656. vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
  8657. pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
  8658. vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
  8659. vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
  8660. vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
  8661. pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
  8662. vmcs_read32(VM_EXIT_INTR_INFO),
  8663. vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
  8664. vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
  8665. pr_err(" reason=%08x qualification=%016lx\n",
  8666. vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
  8667. pr_err("IDTVectoring: info=%08x errcode=%08x\n",
  8668. vmcs_read32(IDT_VECTORING_INFO_FIELD),
  8669. vmcs_read32(IDT_VECTORING_ERROR_CODE));
  8670. pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
  8671. if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
  8672. pr_err("TSC Multiplier = 0x%016llx\n",
  8673. vmcs_read64(TSC_MULTIPLIER));
  8674. if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
  8675. pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
  8676. if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
  8677. pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
  8678. if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
  8679. pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
  8680. n = vmcs_read32(CR3_TARGET_COUNT);
  8681. for (i = 0; i + 1 < n; i += 4)
  8682. pr_err("CR3 target%u=%016lx target%u=%016lx\n",
  8683. i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
  8684. i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
  8685. if (i < n)
  8686. pr_err("CR3 target%u=%016lx\n",
  8687. i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
  8688. if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
  8689. pr_err("PLE Gap=%08x Window=%08x\n",
  8690. vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
  8691. if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
  8692. pr_err("Virtual processor ID = 0x%04x\n",
  8693. vmcs_read16(VIRTUAL_PROCESSOR_ID));
  8694. }
  8695. /*
  8696. * The guest has exited. See if we can fix it or if we need userspace
  8697. * assistance.
  8698. */
  8699. static int vmx_handle_exit(struct kvm_vcpu *vcpu)
  8700. {
  8701. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8702. u32 exit_reason = vmx->exit_reason;
  8703. u32 vectoring_info = vmx->idt_vectoring_info;
  8704. trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
  8705. /*
  8706. * Flush logged GPAs PML buffer, this will make dirty_bitmap more
  8707. * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
  8708. * querying dirty_bitmap, we only need to kick all vcpus out of guest
  8709. * mode as if vcpus is in root mode, the PML buffer must has been
  8710. * flushed already.
  8711. */
  8712. if (enable_pml)
  8713. vmx_flush_pml_buffer(vcpu);
  8714. /* If guest state is invalid, start emulating */
  8715. if (vmx->emulation_required)
  8716. return handle_invalid_guest_state(vcpu);
  8717. if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
  8718. return nested_vmx_reflect_vmexit(vcpu, exit_reason);
  8719. if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
  8720. dump_vmcs();
  8721. vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  8722. vcpu->run->fail_entry.hardware_entry_failure_reason
  8723. = exit_reason;
  8724. return 0;
  8725. }
  8726. if (unlikely(vmx->fail)) {
  8727. vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  8728. vcpu->run->fail_entry.hardware_entry_failure_reason
  8729. = vmcs_read32(VM_INSTRUCTION_ERROR);
  8730. return 0;
  8731. }
  8732. /*
  8733. * Note:
  8734. * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
  8735. * delivery event since it indicates guest is accessing MMIO.
  8736. * The vm-exit can be triggered again after return to guest that
  8737. * will cause infinite loop.
  8738. */
  8739. if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
  8740. (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
  8741. exit_reason != EXIT_REASON_EPT_VIOLATION &&
  8742. exit_reason != EXIT_REASON_PML_FULL &&
  8743. exit_reason != EXIT_REASON_TASK_SWITCH)) {
  8744. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  8745. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
  8746. vcpu->run->internal.ndata = 3;
  8747. vcpu->run->internal.data[0] = vectoring_info;
  8748. vcpu->run->internal.data[1] = exit_reason;
  8749. vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
  8750. if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
  8751. vcpu->run->internal.ndata++;
  8752. vcpu->run->internal.data[3] =
  8753. vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  8754. }
  8755. return 0;
  8756. }
  8757. if (unlikely(!enable_vnmi &&
  8758. vmx->loaded_vmcs->soft_vnmi_blocked)) {
  8759. if (vmx_interrupt_allowed(vcpu)) {
  8760. vmx->loaded_vmcs->soft_vnmi_blocked = 0;
  8761. } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
  8762. vcpu->arch.nmi_pending) {
  8763. /*
  8764. * This CPU don't support us in finding the end of an
  8765. * NMI-blocked window if the guest runs with IRQs
  8766. * disabled. So we pull the trigger after 1 s of
  8767. * futile waiting, but inform the user about this.
  8768. */
  8769. printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
  8770. "state on VCPU %d after 1 s timeout\n",
  8771. __func__, vcpu->vcpu_id);
  8772. vmx->loaded_vmcs->soft_vnmi_blocked = 0;
  8773. }
  8774. }
  8775. if (exit_reason < kvm_vmx_max_exit_handlers
  8776. && kvm_vmx_exit_handlers[exit_reason])
  8777. return kvm_vmx_exit_handlers[exit_reason](vcpu);
  8778. else {
  8779. vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
  8780. exit_reason);
  8781. kvm_queue_exception(vcpu, UD_VECTOR);
  8782. return 1;
  8783. }
  8784. }
  8785. /*
  8786. * Software based L1D cache flush which is used when microcode providing
  8787. * the cache control MSR is not loaded.
  8788. *
  8789. * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
  8790. * flush it is required to read in 64 KiB because the replacement algorithm
  8791. * is not exactly LRU. This could be sized at runtime via topology
  8792. * information but as all relevant affected CPUs have 32KiB L1D cache size
  8793. * there is no point in doing so.
  8794. */
  8795. static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
  8796. {
  8797. int size = PAGE_SIZE << L1D_CACHE_ORDER;
  8798. /*
  8799. * This code is only executed when the the flush mode is 'cond' or
  8800. * 'always'
  8801. */
  8802. if (static_branch_likely(&vmx_l1d_flush_cond)) {
  8803. bool flush_l1d;
  8804. /*
  8805. * Clear the per-vcpu flush bit, it gets set again
  8806. * either from vcpu_run() or from one of the unsafe
  8807. * VMEXIT handlers.
  8808. */
  8809. flush_l1d = vcpu->arch.l1tf_flush_l1d;
  8810. vcpu->arch.l1tf_flush_l1d = false;
  8811. /*
  8812. * Clear the per-cpu flush bit, it gets set again from
  8813. * the interrupt handlers.
  8814. */
  8815. flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
  8816. kvm_clear_cpu_l1tf_flush_l1d();
  8817. if (!flush_l1d)
  8818. return;
  8819. }
  8820. vcpu->stat.l1d_flush++;
  8821. if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
  8822. wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
  8823. return;
  8824. }
  8825. asm volatile(
  8826. /* First ensure the pages are in the TLB */
  8827. "xorl %%eax, %%eax\n"
  8828. ".Lpopulate_tlb:\n\t"
  8829. "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
  8830. "addl $4096, %%eax\n\t"
  8831. "cmpl %%eax, %[size]\n\t"
  8832. "jne .Lpopulate_tlb\n\t"
  8833. "xorl %%eax, %%eax\n\t"
  8834. "cpuid\n\t"
  8835. /* Now fill the cache */
  8836. "xorl %%eax, %%eax\n"
  8837. ".Lfill_cache:\n"
  8838. "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
  8839. "addl $64, %%eax\n\t"
  8840. "cmpl %%eax, %[size]\n\t"
  8841. "jne .Lfill_cache\n\t"
  8842. "lfence\n"
  8843. :: [flush_pages] "r" (vmx_l1d_flush_pages),
  8844. [size] "r" (size)
  8845. : "eax", "ebx", "ecx", "edx");
  8846. }
  8847. static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
  8848. {
  8849. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  8850. if (is_guest_mode(vcpu) &&
  8851. nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
  8852. return;
  8853. if (irr == -1 || tpr < irr) {
  8854. vmcs_write32(TPR_THRESHOLD, 0);
  8855. return;
  8856. }
  8857. vmcs_write32(TPR_THRESHOLD, irr);
  8858. }
  8859. static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
  8860. {
  8861. u32 sec_exec_control;
  8862. if (!lapic_in_kernel(vcpu))
  8863. return;
  8864. if (!flexpriority_enabled &&
  8865. !cpu_has_vmx_virtualize_x2apic_mode())
  8866. return;
  8867. /* Postpone execution until vmcs01 is the current VMCS. */
  8868. if (is_guest_mode(vcpu)) {
  8869. to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
  8870. return;
  8871. }
  8872. sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  8873. sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  8874. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
  8875. switch (kvm_get_apic_mode(vcpu)) {
  8876. case LAPIC_MODE_INVALID:
  8877. WARN_ONCE(true, "Invalid local APIC state");
  8878. case LAPIC_MODE_DISABLED:
  8879. break;
  8880. case LAPIC_MODE_XAPIC:
  8881. if (flexpriority_enabled) {
  8882. sec_exec_control |=
  8883. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  8884. vmx_flush_tlb(vcpu, true);
  8885. }
  8886. break;
  8887. case LAPIC_MODE_X2APIC:
  8888. if (cpu_has_vmx_virtualize_x2apic_mode())
  8889. sec_exec_control |=
  8890. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
  8891. break;
  8892. }
  8893. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
  8894. vmx_update_msr_bitmap(vcpu);
  8895. }
  8896. static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
  8897. {
  8898. if (!is_guest_mode(vcpu)) {
  8899. vmcs_write64(APIC_ACCESS_ADDR, hpa);
  8900. vmx_flush_tlb(vcpu, true);
  8901. }
  8902. }
  8903. static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
  8904. {
  8905. u16 status;
  8906. u8 old;
  8907. if (max_isr == -1)
  8908. max_isr = 0;
  8909. status = vmcs_read16(GUEST_INTR_STATUS);
  8910. old = status >> 8;
  8911. if (max_isr != old) {
  8912. status &= 0xff;
  8913. status |= max_isr << 8;
  8914. vmcs_write16(GUEST_INTR_STATUS, status);
  8915. }
  8916. }
  8917. static void vmx_set_rvi(int vector)
  8918. {
  8919. u16 status;
  8920. u8 old;
  8921. if (vector == -1)
  8922. vector = 0;
  8923. status = vmcs_read16(GUEST_INTR_STATUS);
  8924. old = (u8)status & 0xff;
  8925. if ((u8)vector != old) {
  8926. status &= ~0xff;
  8927. status |= (u8)vector;
  8928. vmcs_write16(GUEST_INTR_STATUS, status);
  8929. }
  8930. }
  8931. static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
  8932. {
  8933. /*
  8934. * When running L2, updating RVI is only relevant when
  8935. * vmcs12 virtual-interrupt-delivery enabled.
  8936. * However, it can be enabled only when L1 also
  8937. * intercepts external-interrupts and in that case
  8938. * we should not update vmcs02 RVI but instead intercept
  8939. * interrupt. Therefore, do nothing when running L2.
  8940. */
  8941. if (!is_guest_mode(vcpu))
  8942. vmx_set_rvi(max_irr);
  8943. }
  8944. static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
  8945. {
  8946. struct vcpu_vmx *vmx = to_vmx(vcpu);
  8947. int max_irr;
  8948. bool max_irr_updated;
  8949. WARN_ON(!vcpu->arch.apicv_active);
  8950. if (pi_test_on(&vmx->pi_desc)) {
  8951. pi_clear_on(&vmx->pi_desc);
  8952. /*
  8953. * IOMMU can write to PIR.ON, so the barrier matters even on UP.
  8954. * But on x86 this is just a compiler barrier anyway.
  8955. */
  8956. smp_mb__after_atomic();
  8957. max_irr_updated =
  8958. kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
  8959. /*
  8960. * If we are running L2 and L1 has a new pending interrupt
  8961. * which can be injected, we should re-evaluate
  8962. * what should be done with this new L1 interrupt.
  8963. * If L1 intercepts external-interrupts, we should
  8964. * exit from L2 to L1. Otherwise, interrupt should be
  8965. * delivered directly to L2.
  8966. */
  8967. if (is_guest_mode(vcpu) && max_irr_updated) {
  8968. if (nested_exit_on_intr(vcpu))
  8969. kvm_vcpu_exiting_guest_mode(vcpu);
  8970. else
  8971. kvm_make_request(KVM_REQ_EVENT, vcpu);
  8972. }
  8973. } else {
  8974. max_irr = kvm_lapic_find_highest_irr(vcpu);
  8975. }
  8976. vmx_hwapic_irr_update(vcpu, max_irr);
  8977. return max_irr;
  8978. }
  8979. static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu)
  8980. {
  8981. u8 rvi = vmx_get_rvi();
  8982. u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI);
  8983. return ((rvi & 0xf0) > (vppr & 0xf0));
  8984. }
  8985. static bool vmx_dy_apicv_has_pending_interrupt(struct kvm_vcpu *vcpu)
  8986. {
  8987. return pi_test_on(vcpu_to_pi_desc(vcpu));
  8988. }
  8989. static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
  8990. {
  8991. if (!kvm_vcpu_apicv_active(vcpu))
  8992. return;
  8993. vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
  8994. vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
  8995. vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
  8996. vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
  8997. }
  8998. static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
  8999. {
  9000. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9001. pi_clear_on(&vmx->pi_desc);
  9002. memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
  9003. }
  9004. static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
  9005. {
  9006. if (vmx->exit_reason != EXIT_REASON_EXCEPTION_NMI)
  9007. return;
  9008. vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  9009. /* if exit due to PF check for async PF */
  9010. if (is_page_fault(vmx->exit_intr_info))
  9011. vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
  9012. /* Handle machine checks before interrupts are enabled */
  9013. if (is_machine_check(vmx->exit_intr_info))
  9014. kvm_machine_check();
  9015. /* We need to handle NMIs before interrupts are enabled */
  9016. if (is_nmi(vmx->exit_intr_info)) {
  9017. kvm_before_interrupt(&vmx->vcpu);
  9018. asm("int $2");
  9019. kvm_after_interrupt(&vmx->vcpu);
  9020. }
  9021. }
  9022. static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
  9023. {
  9024. u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  9025. if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
  9026. == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
  9027. unsigned int vector;
  9028. unsigned long entry;
  9029. gate_desc *desc;
  9030. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9031. #ifdef CONFIG_X86_64
  9032. unsigned long tmp;
  9033. #endif
  9034. vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
  9035. desc = (gate_desc *)vmx->host_idt_base + vector;
  9036. entry = gate_offset(desc);
  9037. asm volatile(
  9038. #ifdef CONFIG_X86_64
  9039. "mov %%" _ASM_SP ", %[sp]\n\t"
  9040. "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
  9041. "push $%c[ss]\n\t"
  9042. "push %[sp]\n\t"
  9043. #endif
  9044. "pushf\n\t"
  9045. __ASM_SIZE(push) " $%c[cs]\n\t"
  9046. CALL_NOSPEC
  9047. :
  9048. #ifdef CONFIG_X86_64
  9049. [sp]"=&r"(tmp),
  9050. #endif
  9051. ASM_CALL_CONSTRAINT
  9052. :
  9053. THUNK_TARGET(entry),
  9054. [ss]"i"(__KERNEL_DS),
  9055. [cs]"i"(__KERNEL_CS)
  9056. );
  9057. }
  9058. }
  9059. STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
  9060. static bool vmx_has_emulated_msr(int index)
  9061. {
  9062. switch (index) {
  9063. case MSR_IA32_SMBASE:
  9064. /*
  9065. * We cannot do SMM unless we can run the guest in big
  9066. * real mode.
  9067. */
  9068. return enable_unrestricted_guest || emulate_invalid_guest_state;
  9069. case MSR_AMD64_VIRT_SPEC_CTRL:
  9070. /* This is AMD only. */
  9071. return false;
  9072. default:
  9073. return true;
  9074. }
  9075. }
  9076. static bool vmx_mpx_supported(void)
  9077. {
  9078. return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
  9079. (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
  9080. }
  9081. static bool vmx_xsaves_supported(void)
  9082. {
  9083. return vmcs_config.cpu_based_2nd_exec_ctrl &
  9084. SECONDARY_EXEC_XSAVES;
  9085. }
  9086. static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
  9087. {
  9088. u32 exit_intr_info;
  9089. bool unblock_nmi;
  9090. u8 vector;
  9091. bool idtv_info_valid;
  9092. idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
  9093. if (enable_vnmi) {
  9094. if (vmx->loaded_vmcs->nmi_known_unmasked)
  9095. return;
  9096. /*
  9097. * Can't use vmx->exit_intr_info since we're not sure what
  9098. * the exit reason is.
  9099. */
  9100. exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  9101. unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
  9102. vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
  9103. /*
  9104. * SDM 3: 27.7.1.2 (September 2008)
  9105. * Re-set bit "block by NMI" before VM entry if vmexit caused by
  9106. * a guest IRET fault.
  9107. * SDM 3: 23.2.2 (September 2008)
  9108. * Bit 12 is undefined in any of the following cases:
  9109. * If the VM exit sets the valid bit in the IDT-vectoring
  9110. * information field.
  9111. * If the VM exit is due to a double fault.
  9112. */
  9113. if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
  9114. vector != DF_VECTOR && !idtv_info_valid)
  9115. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  9116. GUEST_INTR_STATE_NMI);
  9117. else
  9118. vmx->loaded_vmcs->nmi_known_unmasked =
  9119. !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
  9120. & GUEST_INTR_STATE_NMI);
  9121. } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
  9122. vmx->loaded_vmcs->vnmi_blocked_time +=
  9123. ktime_to_ns(ktime_sub(ktime_get(),
  9124. vmx->loaded_vmcs->entry_time));
  9125. }
  9126. static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
  9127. u32 idt_vectoring_info,
  9128. int instr_len_field,
  9129. int error_code_field)
  9130. {
  9131. u8 vector;
  9132. int type;
  9133. bool idtv_info_valid;
  9134. idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
  9135. vcpu->arch.nmi_injected = false;
  9136. kvm_clear_exception_queue(vcpu);
  9137. kvm_clear_interrupt_queue(vcpu);
  9138. if (!idtv_info_valid)
  9139. return;
  9140. kvm_make_request(KVM_REQ_EVENT, vcpu);
  9141. vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
  9142. type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
  9143. switch (type) {
  9144. case INTR_TYPE_NMI_INTR:
  9145. vcpu->arch.nmi_injected = true;
  9146. /*
  9147. * SDM 3: 27.7.1.2 (September 2008)
  9148. * Clear bit "block by NMI" before VM entry if a NMI
  9149. * delivery faulted.
  9150. */
  9151. vmx_set_nmi_mask(vcpu, false);
  9152. break;
  9153. case INTR_TYPE_SOFT_EXCEPTION:
  9154. vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
  9155. /* fall through */
  9156. case INTR_TYPE_HARD_EXCEPTION:
  9157. if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
  9158. u32 err = vmcs_read32(error_code_field);
  9159. kvm_requeue_exception_e(vcpu, vector, err);
  9160. } else
  9161. kvm_requeue_exception(vcpu, vector);
  9162. break;
  9163. case INTR_TYPE_SOFT_INTR:
  9164. vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
  9165. /* fall through */
  9166. case INTR_TYPE_EXT_INTR:
  9167. kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
  9168. break;
  9169. default:
  9170. break;
  9171. }
  9172. }
  9173. static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
  9174. {
  9175. __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
  9176. VM_EXIT_INSTRUCTION_LEN,
  9177. IDT_VECTORING_ERROR_CODE);
  9178. }
  9179. static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
  9180. {
  9181. __vmx_complete_interrupts(vcpu,
  9182. vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
  9183. VM_ENTRY_INSTRUCTION_LEN,
  9184. VM_ENTRY_EXCEPTION_ERROR_CODE);
  9185. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
  9186. }
  9187. static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
  9188. {
  9189. int i, nr_msrs;
  9190. struct perf_guest_switch_msr *msrs;
  9191. msrs = perf_guest_get_msrs(&nr_msrs);
  9192. if (!msrs)
  9193. return;
  9194. for (i = 0; i < nr_msrs; i++)
  9195. if (msrs[i].host == msrs[i].guest)
  9196. clear_atomic_switch_msr(vmx, msrs[i].msr);
  9197. else
  9198. add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
  9199. msrs[i].host, false);
  9200. }
  9201. static void vmx_arm_hv_timer(struct vcpu_vmx *vmx, u32 val)
  9202. {
  9203. vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, val);
  9204. if (!vmx->loaded_vmcs->hv_timer_armed)
  9205. vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
  9206. PIN_BASED_VMX_PREEMPTION_TIMER);
  9207. vmx->loaded_vmcs->hv_timer_armed = true;
  9208. }
  9209. static void vmx_update_hv_timer(struct kvm_vcpu *vcpu)
  9210. {
  9211. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9212. u64 tscl;
  9213. u32 delta_tsc;
  9214. if (vmx->req_immediate_exit) {
  9215. vmx_arm_hv_timer(vmx, 0);
  9216. return;
  9217. }
  9218. if (vmx->hv_deadline_tsc != -1) {
  9219. tscl = rdtsc();
  9220. if (vmx->hv_deadline_tsc > tscl)
  9221. /* set_hv_timer ensures the delta fits in 32-bits */
  9222. delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
  9223. cpu_preemption_timer_multi);
  9224. else
  9225. delta_tsc = 0;
  9226. vmx_arm_hv_timer(vmx, delta_tsc);
  9227. return;
  9228. }
  9229. if (vmx->loaded_vmcs->hv_timer_armed)
  9230. vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
  9231. PIN_BASED_VMX_PREEMPTION_TIMER);
  9232. vmx->loaded_vmcs->hv_timer_armed = false;
  9233. }
  9234. static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
  9235. {
  9236. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9237. unsigned long cr3, cr4, evmcs_rsp;
  9238. /* Record the guest's net vcpu time for enforced NMI injections. */
  9239. if (unlikely(!enable_vnmi &&
  9240. vmx->loaded_vmcs->soft_vnmi_blocked))
  9241. vmx->loaded_vmcs->entry_time = ktime_get();
  9242. /* Don't enter VMX if guest state is invalid, let the exit handler
  9243. start emulation until we arrive back to a valid state */
  9244. if (vmx->emulation_required)
  9245. return;
  9246. if (vmx->ple_window_dirty) {
  9247. vmx->ple_window_dirty = false;
  9248. vmcs_write32(PLE_WINDOW, vmx->ple_window);
  9249. }
  9250. if (vmx->nested.sync_shadow_vmcs) {
  9251. copy_vmcs12_to_shadow(vmx);
  9252. vmx->nested.sync_shadow_vmcs = false;
  9253. }
  9254. if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
  9255. vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
  9256. if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
  9257. vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
  9258. cr3 = __get_current_cr3_fast();
  9259. if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
  9260. vmcs_writel(HOST_CR3, cr3);
  9261. vmx->loaded_vmcs->host_state.cr3 = cr3;
  9262. }
  9263. cr4 = cr4_read_shadow();
  9264. if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
  9265. vmcs_writel(HOST_CR4, cr4);
  9266. vmx->loaded_vmcs->host_state.cr4 = cr4;
  9267. }
  9268. /* When single-stepping over STI and MOV SS, we must clear the
  9269. * corresponding interruptibility bits in the guest state. Otherwise
  9270. * vmentry fails as it then expects bit 14 (BS) in pending debug
  9271. * exceptions being set, but that's not correct for the guest debugging
  9272. * case. */
  9273. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
  9274. vmx_set_interrupt_shadow(vcpu, 0);
  9275. kvm_load_guest_xcr0(vcpu);
  9276. if (static_cpu_has(X86_FEATURE_PKU) &&
  9277. kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
  9278. vcpu->arch.pkru != vmx->host_pkru)
  9279. __write_pkru(vcpu->arch.pkru);
  9280. atomic_switch_perf_msrs(vmx);
  9281. vmx_update_hv_timer(vcpu);
  9282. /*
  9283. * If this vCPU has touched SPEC_CTRL, restore the guest's value if
  9284. * it's non-zero. Since vmentry is serialising on affected CPUs, there
  9285. * is no need to worry about the conditional branch over the wrmsr
  9286. * being speculatively taken.
  9287. */
  9288. x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
  9289. vmx->__launched = vmx->loaded_vmcs->launched;
  9290. evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
  9291. (unsigned long)&current_evmcs->host_rsp : 0;
  9292. /* L1D Flush includes CPU buffer clear to mitigate MDS */
  9293. if (static_branch_unlikely(&vmx_l1d_should_flush))
  9294. vmx_l1d_flush(vcpu);
  9295. else if (static_branch_unlikely(&mds_user_clear))
  9296. mds_clear_cpu_buffers();
  9297. asm(
  9298. /* Store host registers */
  9299. "push %%" _ASM_DX "; push %%" _ASM_BP ";"
  9300. "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
  9301. "push %%" _ASM_CX " \n\t"
  9302. "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
  9303. "je 1f \n\t"
  9304. "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
  9305. /* Avoid VMWRITE when Enlightened VMCS is in use */
  9306. "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
  9307. "jz 2f \n\t"
  9308. "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
  9309. "jmp 1f \n\t"
  9310. "2: \n\t"
  9311. __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
  9312. "1: \n\t"
  9313. /* Reload cr2 if changed */
  9314. "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
  9315. "mov %%cr2, %%" _ASM_DX " \n\t"
  9316. "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
  9317. "je 3f \n\t"
  9318. "mov %%" _ASM_AX", %%cr2 \n\t"
  9319. "3: \n\t"
  9320. /* Check if vmlaunch of vmresume is needed */
  9321. "cmpb $0, %c[launched](%0) \n\t"
  9322. /* Load guest registers. Don't clobber flags. */
  9323. "mov %c[rax](%0), %%" _ASM_AX " \n\t"
  9324. "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
  9325. "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
  9326. "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
  9327. "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
  9328. "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
  9329. #ifdef CONFIG_X86_64
  9330. "mov %c[r8](%0), %%r8 \n\t"
  9331. "mov %c[r9](%0), %%r9 \n\t"
  9332. "mov %c[r10](%0), %%r10 \n\t"
  9333. "mov %c[r11](%0), %%r11 \n\t"
  9334. "mov %c[r12](%0), %%r12 \n\t"
  9335. "mov %c[r13](%0), %%r13 \n\t"
  9336. "mov %c[r14](%0), %%r14 \n\t"
  9337. "mov %c[r15](%0), %%r15 \n\t"
  9338. #endif
  9339. "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
  9340. /* Enter guest mode */
  9341. "jne 1f \n\t"
  9342. __ex(ASM_VMX_VMLAUNCH) "\n\t"
  9343. "jmp 2f \n\t"
  9344. "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
  9345. "2: "
  9346. /* Save guest registers, load host registers, keep flags */
  9347. "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
  9348. "pop %0 \n\t"
  9349. "setbe %c[fail](%0)\n\t"
  9350. "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
  9351. "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
  9352. __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
  9353. "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
  9354. "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
  9355. "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
  9356. "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
  9357. #ifdef CONFIG_X86_64
  9358. "mov %%r8, %c[r8](%0) \n\t"
  9359. "mov %%r9, %c[r9](%0) \n\t"
  9360. "mov %%r10, %c[r10](%0) \n\t"
  9361. "mov %%r11, %c[r11](%0) \n\t"
  9362. "mov %%r12, %c[r12](%0) \n\t"
  9363. "mov %%r13, %c[r13](%0) \n\t"
  9364. "mov %%r14, %c[r14](%0) \n\t"
  9365. "mov %%r15, %c[r15](%0) \n\t"
  9366. "xor %%r8d, %%r8d \n\t"
  9367. "xor %%r9d, %%r9d \n\t"
  9368. "xor %%r10d, %%r10d \n\t"
  9369. "xor %%r11d, %%r11d \n\t"
  9370. "xor %%r12d, %%r12d \n\t"
  9371. "xor %%r13d, %%r13d \n\t"
  9372. "xor %%r14d, %%r14d \n\t"
  9373. "xor %%r15d, %%r15d \n\t"
  9374. #endif
  9375. "mov %%cr2, %%" _ASM_AX " \n\t"
  9376. "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
  9377. "xor %%eax, %%eax \n\t"
  9378. "xor %%ebx, %%ebx \n\t"
  9379. "xor %%esi, %%esi \n\t"
  9380. "xor %%edi, %%edi \n\t"
  9381. "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
  9382. ".pushsection .rodata \n\t"
  9383. ".global vmx_return \n\t"
  9384. "vmx_return: " _ASM_PTR " 2b \n\t"
  9385. ".popsection"
  9386. : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
  9387. [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
  9388. [fail]"i"(offsetof(struct vcpu_vmx, fail)),
  9389. [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
  9390. [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
  9391. [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
  9392. [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
  9393. [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
  9394. [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
  9395. [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
  9396. [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
  9397. #ifdef CONFIG_X86_64
  9398. [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
  9399. [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
  9400. [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
  9401. [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
  9402. [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
  9403. [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
  9404. [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
  9405. [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
  9406. #endif
  9407. [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
  9408. [wordsize]"i"(sizeof(ulong))
  9409. : "cc", "memory"
  9410. #ifdef CONFIG_X86_64
  9411. , "rax", "rbx", "rdi"
  9412. , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
  9413. #else
  9414. , "eax", "ebx", "edi"
  9415. #endif
  9416. );
  9417. /*
  9418. * We do not use IBRS in the kernel. If this vCPU has used the
  9419. * SPEC_CTRL MSR it may have left it on; save the value and
  9420. * turn it off. This is much more efficient than blindly adding
  9421. * it to the atomic save/restore list. Especially as the former
  9422. * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
  9423. *
  9424. * For non-nested case:
  9425. * If the L01 MSR bitmap does not intercept the MSR, then we need to
  9426. * save it.
  9427. *
  9428. * For nested case:
  9429. * If the L02 MSR bitmap does not intercept the MSR, then we need to
  9430. * save it.
  9431. */
  9432. if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
  9433. vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
  9434. x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
  9435. /* Eliminate branch target predictions from guest mode */
  9436. vmexit_fill_RSB();
  9437. /* All fields are clean at this point */
  9438. if (static_branch_unlikely(&enable_evmcs))
  9439. current_evmcs->hv_clean_fields |=
  9440. HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
  9441. /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
  9442. if (vmx->host_debugctlmsr)
  9443. update_debugctlmsr(vmx->host_debugctlmsr);
  9444. #ifndef CONFIG_X86_64
  9445. /*
  9446. * The sysexit path does not restore ds/es, so we must set them to
  9447. * a reasonable value ourselves.
  9448. *
  9449. * We can't defer this to vmx_prepare_switch_to_host() since that
  9450. * function may be executed in interrupt context, which saves and
  9451. * restore segments around it, nullifying its effect.
  9452. */
  9453. loadsegment(ds, __USER_DS);
  9454. loadsegment(es, __USER_DS);
  9455. #endif
  9456. vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
  9457. | (1 << VCPU_EXREG_RFLAGS)
  9458. | (1 << VCPU_EXREG_PDPTR)
  9459. | (1 << VCPU_EXREG_SEGMENTS)
  9460. | (1 << VCPU_EXREG_CR3));
  9461. vcpu->arch.regs_dirty = 0;
  9462. /*
  9463. * eager fpu is enabled if PKEY is supported and CR4 is switched
  9464. * back on host, so it is safe to read guest PKRU from current
  9465. * XSAVE.
  9466. */
  9467. if (static_cpu_has(X86_FEATURE_PKU) &&
  9468. kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
  9469. vcpu->arch.pkru = __read_pkru();
  9470. if (vcpu->arch.pkru != vmx->host_pkru)
  9471. __write_pkru(vmx->host_pkru);
  9472. }
  9473. kvm_put_guest_xcr0(vcpu);
  9474. vmx->nested.nested_run_pending = 0;
  9475. vmx->idt_vectoring_info = 0;
  9476. vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
  9477. if ((u16)vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY)
  9478. kvm_machine_check();
  9479. if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
  9480. return;
  9481. vmx->loaded_vmcs->launched = 1;
  9482. vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
  9483. vmx_complete_atomic_exit(vmx);
  9484. vmx_recover_nmi_blocking(vmx);
  9485. vmx_complete_interrupts(vmx);
  9486. }
  9487. STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
  9488. static struct kvm *vmx_vm_alloc(void)
  9489. {
  9490. struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
  9491. return &kvm_vmx->kvm;
  9492. }
  9493. static void vmx_vm_free(struct kvm *kvm)
  9494. {
  9495. vfree(to_kvm_vmx(kvm));
  9496. }
  9497. static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
  9498. {
  9499. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9500. int cpu;
  9501. if (vmx->loaded_vmcs == vmcs)
  9502. return;
  9503. cpu = get_cpu();
  9504. vmx_vcpu_put(vcpu);
  9505. vmx->loaded_vmcs = vmcs;
  9506. vmx_vcpu_load(vcpu, cpu);
  9507. put_cpu();
  9508. }
  9509. /*
  9510. * Ensure that the current vmcs of the logical processor is the
  9511. * vmcs01 of the vcpu before calling free_nested().
  9512. */
  9513. static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
  9514. {
  9515. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9516. vcpu_load(vcpu);
  9517. vmx_switch_vmcs(vcpu, &vmx->vmcs01);
  9518. free_nested(vmx);
  9519. vcpu_put(vcpu);
  9520. }
  9521. static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
  9522. {
  9523. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9524. if (enable_pml)
  9525. vmx_destroy_pml_buffer(vmx);
  9526. free_vpid(vmx->vpid);
  9527. leave_guest_mode(vcpu);
  9528. vmx_free_vcpu_nested(vcpu);
  9529. free_loaded_vmcs(vmx->loaded_vmcs);
  9530. kfree(vmx->guest_msrs);
  9531. kvm_vcpu_uninit(vcpu);
  9532. kmem_cache_free(kvm_vcpu_cache, vmx);
  9533. }
  9534. static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
  9535. {
  9536. int err;
  9537. struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  9538. unsigned long *msr_bitmap;
  9539. int cpu;
  9540. if (!vmx)
  9541. return ERR_PTR(-ENOMEM);
  9542. vmx->vpid = allocate_vpid();
  9543. err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
  9544. if (err)
  9545. goto free_vcpu;
  9546. err = -ENOMEM;
  9547. /*
  9548. * If PML is turned on, failure on enabling PML just results in failure
  9549. * of creating the vcpu, therefore we can simplify PML logic (by
  9550. * avoiding dealing with cases, such as enabling PML partially on vcpus
  9551. * for the guest, etc.
  9552. */
  9553. if (enable_pml) {
  9554. vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
  9555. if (!vmx->pml_pg)
  9556. goto uninit_vcpu;
  9557. }
  9558. vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
  9559. BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
  9560. > PAGE_SIZE);
  9561. if (!vmx->guest_msrs)
  9562. goto free_pml;
  9563. err = alloc_loaded_vmcs(&vmx->vmcs01);
  9564. if (err < 0)
  9565. goto free_msrs;
  9566. msr_bitmap = vmx->vmcs01.msr_bitmap;
  9567. vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
  9568. vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
  9569. vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
  9570. vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
  9571. vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
  9572. vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
  9573. vmx->msr_bitmap_mode = 0;
  9574. vmx->loaded_vmcs = &vmx->vmcs01;
  9575. cpu = get_cpu();
  9576. vmx_vcpu_load(&vmx->vcpu, cpu);
  9577. vmx->vcpu.cpu = cpu;
  9578. vmx_vcpu_setup(vmx);
  9579. vmx_vcpu_put(&vmx->vcpu);
  9580. put_cpu();
  9581. if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
  9582. err = alloc_apic_access_page(kvm);
  9583. if (err)
  9584. goto free_vmcs;
  9585. }
  9586. if (enable_ept && !enable_unrestricted_guest) {
  9587. err = init_rmode_identity_map(kvm);
  9588. if (err)
  9589. goto free_vmcs;
  9590. }
  9591. if (nested)
  9592. nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
  9593. kvm_vcpu_apicv_active(&vmx->vcpu));
  9594. vmx->nested.posted_intr_nv = -1;
  9595. vmx->nested.current_vmptr = -1ull;
  9596. vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
  9597. /*
  9598. * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
  9599. * or POSTED_INTR_WAKEUP_VECTOR.
  9600. */
  9601. vmx->pi_desc.nv = POSTED_INTR_VECTOR;
  9602. vmx->pi_desc.sn = 1;
  9603. return &vmx->vcpu;
  9604. free_vmcs:
  9605. free_loaded_vmcs(vmx->loaded_vmcs);
  9606. free_msrs:
  9607. kfree(vmx->guest_msrs);
  9608. free_pml:
  9609. vmx_destroy_pml_buffer(vmx);
  9610. uninit_vcpu:
  9611. kvm_vcpu_uninit(&vmx->vcpu);
  9612. free_vcpu:
  9613. free_vpid(vmx->vpid);
  9614. kmem_cache_free(kvm_vcpu_cache, vmx);
  9615. return ERR_PTR(err);
  9616. }
  9617. #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/hw-vuln/l1tf.html for details.\n"
  9618. #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/hw-vuln/l1tf.html for details.\n"
  9619. static int vmx_vm_init(struct kvm *kvm)
  9620. {
  9621. spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
  9622. if (!ple_gap)
  9623. kvm->arch.pause_in_guest = true;
  9624. if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
  9625. switch (l1tf_mitigation) {
  9626. case L1TF_MITIGATION_OFF:
  9627. case L1TF_MITIGATION_FLUSH_NOWARN:
  9628. /* 'I explicitly don't care' is set */
  9629. break;
  9630. case L1TF_MITIGATION_FLUSH:
  9631. case L1TF_MITIGATION_FLUSH_NOSMT:
  9632. case L1TF_MITIGATION_FULL:
  9633. /*
  9634. * Warn upon starting the first VM in a potentially
  9635. * insecure environment.
  9636. */
  9637. if (sched_smt_active())
  9638. pr_warn_once(L1TF_MSG_SMT);
  9639. if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
  9640. pr_warn_once(L1TF_MSG_L1D);
  9641. break;
  9642. case L1TF_MITIGATION_FULL_FORCE:
  9643. /* Flush is enforced */
  9644. break;
  9645. }
  9646. }
  9647. return 0;
  9648. }
  9649. static void __init vmx_check_processor_compat(void *rtn)
  9650. {
  9651. struct vmcs_config vmcs_conf;
  9652. *(int *)rtn = 0;
  9653. if (setup_vmcs_config(&vmcs_conf) < 0)
  9654. *(int *)rtn = -EIO;
  9655. nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
  9656. if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
  9657. printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
  9658. smp_processor_id());
  9659. *(int *)rtn = -EIO;
  9660. }
  9661. }
  9662. static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
  9663. {
  9664. u8 cache;
  9665. u64 ipat = 0;
  9666. /* For VT-d and EPT combination
  9667. * 1. MMIO: always map as UC
  9668. * 2. EPT with VT-d:
  9669. * a. VT-d without snooping control feature: can't guarantee the
  9670. * result, try to trust guest.
  9671. * b. VT-d with snooping control feature: snooping control feature of
  9672. * VT-d engine can guarantee the cache correctness. Just set it
  9673. * to WB to keep consistent with host. So the same as item 3.
  9674. * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
  9675. * consistent with host MTRR
  9676. */
  9677. if (is_mmio) {
  9678. cache = MTRR_TYPE_UNCACHABLE;
  9679. goto exit;
  9680. }
  9681. if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
  9682. ipat = VMX_EPT_IPAT_BIT;
  9683. cache = MTRR_TYPE_WRBACK;
  9684. goto exit;
  9685. }
  9686. if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
  9687. ipat = VMX_EPT_IPAT_BIT;
  9688. if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
  9689. cache = MTRR_TYPE_WRBACK;
  9690. else
  9691. cache = MTRR_TYPE_UNCACHABLE;
  9692. goto exit;
  9693. }
  9694. cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
  9695. exit:
  9696. return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
  9697. }
  9698. static int vmx_get_lpage_level(void)
  9699. {
  9700. if (enable_ept && !cpu_has_vmx_ept_1g_page())
  9701. return PT_DIRECTORY_LEVEL;
  9702. else
  9703. /* For shadow and EPT supported 1GB page */
  9704. return PT_PDPE_LEVEL;
  9705. }
  9706. static void vmcs_set_secondary_exec_control(u32 new_ctl)
  9707. {
  9708. /*
  9709. * These bits in the secondary execution controls field
  9710. * are dynamic, the others are mostly based on the hypervisor
  9711. * architecture and the guest's CPUID. Do not touch the
  9712. * dynamic bits.
  9713. */
  9714. u32 mask =
  9715. SECONDARY_EXEC_SHADOW_VMCS |
  9716. SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
  9717. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  9718. SECONDARY_EXEC_DESC;
  9719. u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  9720. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  9721. (new_ctl & ~mask) | (cur_ctl & mask));
  9722. }
  9723. /*
  9724. * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
  9725. * (indicating "allowed-1") if they are supported in the guest's CPUID.
  9726. */
  9727. static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
  9728. {
  9729. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9730. struct kvm_cpuid_entry2 *entry;
  9731. vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
  9732. vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
  9733. #define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
  9734. if (entry && (entry->_reg & (_cpuid_mask))) \
  9735. vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
  9736. } while (0)
  9737. entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
  9738. cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
  9739. cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
  9740. cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
  9741. cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
  9742. cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
  9743. cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
  9744. cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
  9745. cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
  9746. cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
  9747. cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
  9748. cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
  9749. cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
  9750. cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
  9751. cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
  9752. entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
  9753. cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
  9754. cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
  9755. cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
  9756. cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
  9757. cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
  9758. #undef cr4_fixed1_update
  9759. }
  9760. static void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
  9761. {
  9762. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9763. if (kvm_mpx_supported()) {
  9764. bool mpx_enabled = guest_cpuid_has(vcpu, X86_FEATURE_MPX);
  9765. if (mpx_enabled) {
  9766. vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
  9767. vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
  9768. } else {
  9769. vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS;
  9770. vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS;
  9771. }
  9772. }
  9773. }
  9774. static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
  9775. {
  9776. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9777. if (cpu_has_secondary_exec_ctrls()) {
  9778. vmx_compute_secondary_exec_control(vmx);
  9779. vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
  9780. }
  9781. if (nested_vmx_allowed(vcpu))
  9782. to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
  9783. FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  9784. else
  9785. to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
  9786. ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  9787. if (nested_vmx_allowed(vcpu)) {
  9788. nested_vmx_cr_fixed1_bits_update(vcpu);
  9789. nested_vmx_entry_exit_ctls_update(vcpu);
  9790. }
  9791. }
  9792. static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
  9793. {
  9794. if (func == 1 && nested)
  9795. entry->ecx |= bit(X86_FEATURE_VMX);
  9796. }
  9797. static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
  9798. struct x86_exception *fault)
  9799. {
  9800. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  9801. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9802. u32 exit_reason;
  9803. unsigned long exit_qualification = vcpu->arch.exit_qualification;
  9804. if (vmx->nested.pml_full) {
  9805. exit_reason = EXIT_REASON_PML_FULL;
  9806. vmx->nested.pml_full = false;
  9807. exit_qualification &= INTR_INFO_UNBLOCK_NMI;
  9808. } else if (fault->error_code & PFERR_RSVD_MASK)
  9809. exit_reason = EXIT_REASON_EPT_MISCONFIG;
  9810. else
  9811. exit_reason = EXIT_REASON_EPT_VIOLATION;
  9812. nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
  9813. vmcs12->guest_physical_address = fault->address;
  9814. }
  9815. static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
  9816. {
  9817. return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
  9818. }
  9819. /* Callbacks for nested_ept_init_mmu_context: */
  9820. static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
  9821. {
  9822. /* return the page table to be shadowed - in our case, EPT12 */
  9823. return get_vmcs12(vcpu)->ept_pointer;
  9824. }
  9825. static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
  9826. {
  9827. WARN_ON(mmu_is_nested(vcpu));
  9828. if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
  9829. return 1;
  9830. kvm_init_shadow_ept_mmu(vcpu,
  9831. to_vmx(vcpu)->nested.msrs.ept_caps &
  9832. VMX_EPT_EXECUTE_ONLY_BIT,
  9833. nested_ept_ad_enabled(vcpu),
  9834. nested_ept_get_cr3(vcpu));
  9835. vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
  9836. vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
  9837. vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
  9838. vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
  9839. return 0;
  9840. }
  9841. static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
  9842. {
  9843. vcpu->arch.walk_mmu = &vcpu->arch.mmu;
  9844. }
  9845. static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
  9846. u16 error_code)
  9847. {
  9848. bool inequality, bit;
  9849. bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
  9850. inequality =
  9851. (error_code & vmcs12->page_fault_error_code_mask) !=
  9852. vmcs12->page_fault_error_code_match;
  9853. return inequality ^ bit;
  9854. }
  9855. static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
  9856. struct x86_exception *fault)
  9857. {
  9858. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  9859. WARN_ON(!is_guest_mode(vcpu));
  9860. if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
  9861. !to_vmx(vcpu)->nested.nested_run_pending) {
  9862. vmcs12->vm_exit_intr_error_code = fault->error_code;
  9863. nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
  9864. PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
  9865. INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
  9866. fault->address);
  9867. } else {
  9868. kvm_inject_page_fault(vcpu, fault);
  9869. }
  9870. }
  9871. static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
  9872. struct vmcs12 *vmcs12);
  9873. static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
  9874. {
  9875. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  9876. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9877. struct page *page;
  9878. u64 hpa;
  9879. if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
  9880. /*
  9881. * Translate L1 physical address to host physical
  9882. * address for vmcs02. Keep the page pinned, so this
  9883. * physical address remains valid. We keep a reference
  9884. * to it so we can release it later.
  9885. */
  9886. if (vmx->nested.apic_access_page) { /* shouldn't happen */
  9887. kvm_release_page_dirty(vmx->nested.apic_access_page);
  9888. vmx->nested.apic_access_page = NULL;
  9889. }
  9890. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
  9891. /*
  9892. * If translation failed, no matter: This feature asks
  9893. * to exit when accessing the given address, and if it
  9894. * can never be accessed, this feature won't do
  9895. * anything anyway.
  9896. */
  9897. if (!is_error_page(page)) {
  9898. vmx->nested.apic_access_page = page;
  9899. hpa = page_to_phys(vmx->nested.apic_access_page);
  9900. vmcs_write64(APIC_ACCESS_ADDR, hpa);
  9901. } else {
  9902. vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
  9903. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
  9904. }
  9905. }
  9906. if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
  9907. if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
  9908. kvm_release_page_dirty(vmx->nested.virtual_apic_page);
  9909. vmx->nested.virtual_apic_page = NULL;
  9910. }
  9911. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
  9912. /*
  9913. * If translation failed, VM entry will fail because
  9914. * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
  9915. * Failing the vm entry is _not_ what the processor
  9916. * does but it's basically the only possibility we
  9917. * have. We could still enter the guest if CR8 load
  9918. * exits are enabled, CR8 store exits are enabled, and
  9919. * virtualize APIC access is disabled; in this case
  9920. * the processor would never use the TPR shadow and we
  9921. * could simply clear the bit from the execution
  9922. * control. But such a configuration is useless, so
  9923. * let's keep the code simple.
  9924. */
  9925. if (!is_error_page(page)) {
  9926. vmx->nested.virtual_apic_page = page;
  9927. hpa = page_to_phys(vmx->nested.virtual_apic_page);
  9928. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
  9929. }
  9930. }
  9931. if (nested_cpu_has_posted_intr(vmcs12)) {
  9932. if (vmx->nested.pi_desc_page) { /* shouldn't happen */
  9933. kunmap(vmx->nested.pi_desc_page);
  9934. kvm_release_page_dirty(vmx->nested.pi_desc_page);
  9935. vmx->nested.pi_desc_page = NULL;
  9936. vmx->nested.pi_desc = NULL;
  9937. vmcs_write64(POSTED_INTR_DESC_ADDR, -1ull);
  9938. }
  9939. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
  9940. if (is_error_page(page))
  9941. return;
  9942. vmx->nested.pi_desc_page = page;
  9943. vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
  9944. vmx->nested.pi_desc =
  9945. (struct pi_desc *)((void *)vmx->nested.pi_desc +
  9946. (unsigned long)(vmcs12->posted_intr_desc_addr &
  9947. (PAGE_SIZE - 1)));
  9948. vmcs_write64(POSTED_INTR_DESC_ADDR,
  9949. page_to_phys(vmx->nested.pi_desc_page) +
  9950. (unsigned long)(vmcs12->posted_intr_desc_addr &
  9951. (PAGE_SIZE - 1)));
  9952. }
  9953. if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
  9954. vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
  9955. CPU_BASED_USE_MSR_BITMAPS);
  9956. else
  9957. vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
  9958. CPU_BASED_USE_MSR_BITMAPS);
  9959. }
  9960. static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
  9961. {
  9962. u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
  9963. struct vcpu_vmx *vmx = to_vmx(vcpu);
  9964. /*
  9965. * A timer value of zero is architecturally guaranteed to cause
  9966. * a VMExit prior to executing any instructions in the guest.
  9967. */
  9968. if (preemption_timeout == 0) {
  9969. vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
  9970. return;
  9971. }
  9972. if (vcpu->arch.virtual_tsc_khz == 0)
  9973. return;
  9974. preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
  9975. preemption_timeout *= 1000000;
  9976. do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
  9977. hrtimer_start(&vmx->nested.preemption_timer,
  9978. ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
  9979. }
  9980. static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
  9981. struct vmcs12 *vmcs12)
  9982. {
  9983. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
  9984. return 0;
  9985. if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
  9986. !page_address_valid(vcpu, vmcs12->io_bitmap_b))
  9987. return -EINVAL;
  9988. return 0;
  9989. }
  9990. static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
  9991. struct vmcs12 *vmcs12)
  9992. {
  9993. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
  9994. return 0;
  9995. if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
  9996. return -EINVAL;
  9997. return 0;
  9998. }
  9999. static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
  10000. struct vmcs12 *vmcs12)
  10001. {
  10002. if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
  10003. return 0;
  10004. if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
  10005. return -EINVAL;
  10006. return 0;
  10007. }
  10008. static inline void enable_x2apic_msr_intercepts(unsigned long *msr_bitmap) {
  10009. int msr;
  10010. for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
  10011. unsigned word = msr / BITS_PER_LONG;
  10012. msr_bitmap[word] = ~0;
  10013. msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
  10014. }
  10015. }
  10016. /*
  10017. * Merge L0's and L1's MSR bitmap, return false to indicate that
  10018. * we do not use the hardware.
  10019. */
  10020. static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
  10021. struct vmcs12 *vmcs12)
  10022. {
  10023. int msr;
  10024. struct page *page;
  10025. unsigned long *msr_bitmap_l1;
  10026. unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
  10027. /*
  10028. * pred_cmd & spec_ctrl are trying to verify two things:
  10029. *
  10030. * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
  10031. * ensures that we do not accidentally generate an L02 MSR bitmap
  10032. * from the L12 MSR bitmap that is too permissive.
  10033. * 2. That L1 or L2s have actually used the MSR. This avoids
  10034. * unnecessarily merging of the bitmap if the MSR is unused. This
  10035. * works properly because we only update the L01 MSR bitmap lazily.
  10036. * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
  10037. * updated to reflect this when L1 (or its L2s) actually write to
  10038. * the MSR.
  10039. */
  10040. bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
  10041. bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
  10042. /* Nothing to do if the MSR bitmap is not in use. */
  10043. if (!cpu_has_vmx_msr_bitmap() ||
  10044. !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
  10045. return false;
  10046. if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
  10047. !pred_cmd && !spec_ctrl)
  10048. return false;
  10049. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
  10050. if (is_error_page(page))
  10051. return false;
  10052. msr_bitmap_l1 = (unsigned long *)kmap(page);
  10053. /*
  10054. * To keep the control flow simple, pay eight 8-byte writes (sixteen
  10055. * 4-byte writes on 32-bit systems) up front to enable intercepts for
  10056. * the x2APIC MSR range and selectively disable them below.
  10057. */
  10058. enable_x2apic_msr_intercepts(msr_bitmap_l0);
  10059. if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
  10060. if (nested_cpu_has_apic_reg_virt(vmcs12)) {
  10061. /*
  10062. * L0 need not intercept reads for MSRs between 0x800
  10063. * and 0x8ff, it just lets the processor take the value
  10064. * from the virtual-APIC page; take those 256 bits
  10065. * directly from the L1 bitmap.
  10066. */
  10067. for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
  10068. unsigned word = msr / BITS_PER_LONG;
  10069. msr_bitmap_l0[word] = msr_bitmap_l1[word];
  10070. }
  10071. }
  10072. nested_vmx_disable_intercept_for_msr(
  10073. msr_bitmap_l1, msr_bitmap_l0,
  10074. X2APIC_MSR(APIC_TASKPRI),
  10075. MSR_TYPE_R | MSR_TYPE_W);
  10076. if (nested_cpu_has_vid(vmcs12)) {
  10077. nested_vmx_disable_intercept_for_msr(
  10078. msr_bitmap_l1, msr_bitmap_l0,
  10079. X2APIC_MSR(APIC_EOI),
  10080. MSR_TYPE_W);
  10081. nested_vmx_disable_intercept_for_msr(
  10082. msr_bitmap_l1, msr_bitmap_l0,
  10083. X2APIC_MSR(APIC_SELF_IPI),
  10084. MSR_TYPE_W);
  10085. }
  10086. }
  10087. if (spec_ctrl)
  10088. nested_vmx_disable_intercept_for_msr(
  10089. msr_bitmap_l1, msr_bitmap_l0,
  10090. MSR_IA32_SPEC_CTRL,
  10091. MSR_TYPE_R | MSR_TYPE_W);
  10092. if (pred_cmd)
  10093. nested_vmx_disable_intercept_for_msr(
  10094. msr_bitmap_l1, msr_bitmap_l0,
  10095. MSR_IA32_PRED_CMD,
  10096. MSR_TYPE_W);
  10097. kunmap(page);
  10098. kvm_release_page_clean(page);
  10099. return true;
  10100. }
  10101. static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
  10102. struct vmcs12 *vmcs12)
  10103. {
  10104. struct vmcs12 *shadow;
  10105. struct page *page;
  10106. if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
  10107. vmcs12->vmcs_link_pointer == -1ull)
  10108. return;
  10109. shadow = get_shadow_vmcs12(vcpu);
  10110. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
  10111. memcpy(shadow, kmap(page), VMCS12_SIZE);
  10112. kunmap(page);
  10113. kvm_release_page_clean(page);
  10114. }
  10115. static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
  10116. struct vmcs12 *vmcs12)
  10117. {
  10118. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10119. if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
  10120. vmcs12->vmcs_link_pointer == -1ull)
  10121. return;
  10122. kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
  10123. get_shadow_vmcs12(vcpu), VMCS12_SIZE);
  10124. }
  10125. static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
  10126. struct vmcs12 *vmcs12)
  10127. {
  10128. if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
  10129. !page_address_valid(vcpu, vmcs12->apic_access_addr))
  10130. return -EINVAL;
  10131. else
  10132. return 0;
  10133. }
  10134. static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
  10135. struct vmcs12 *vmcs12)
  10136. {
  10137. if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
  10138. !nested_cpu_has_apic_reg_virt(vmcs12) &&
  10139. !nested_cpu_has_vid(vmcs12) &&
  10140. !nested_cpu_has_posted_intr(vmcs12))
  10141. return 0;
  10142. /*
  10143. * If virtualize x2apic mode is enabled,
  10144. * virtualize apic access must be disabled.
  10145. */
  10146. if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
  10147. nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
  10148. return -EINVAL;
  10149. /*
  10150. * If virtual interrupt delivery is enabled,
  10151. * we must exit on external interrupts.
  10152. */
  10153. if (nested_cpu_has_vid(vmcs12) &&
  10154. !nested_exit_on_intr(vcpu))
  10155. return -EINVAL;
  10156. /*
  10157. * bits 15:8 should be zero in posted_intr_nv,
  10158. * the descriptor address has been already checked
  10159. * in nested_get_vmcs12_pages.
  10160. *
  10161. * bits 5:0 of posted_intr_desc_addr should be zero.
  10162. */
  10163. if (nested_cpu_has_posted_intr(vmcs12) &&
  10164. (!nested_cpu_has_vid(vmcs12) ||
  10165. !nested_exit_intr_ack_set(vcpu) ||
  10166. (vmcs12->posted_intr_nv & 0xff00) ||
  10167. (vmcs12->posted_intr_desc_addr & 0x3f) ||
  10168. (vmcs12->posted_intr_desc_addr >> cpuid_maxphyaddr(vcpu))))
  10169. return -EINVAL;
  10170. /* tpr shadow is needed by all apicv features. */
  10171. if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
  10172. return -EINVAL;
  10173. return 0;
  10174. }
  10175. static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
  10176. unsigned long count_field,
  10177. unsigned long addr_field)
  10178. {
  10179. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  10180. int maxphyaddr;
  10181. u64 count, addr;
  10182. if (vmcs12_read_any(vmcs12, count_field, &count) ||
  10183. vmcs12_read_any(vmcs12, addr_field, &addr)) {
  10184. WARN_ON(1);
  10185. return -EINVAL;
  10186. }
  10187. if (count == 0)
  10188. return 0;
  10189. maxphyaddr = cpuid_maxphyaddr(vcpu);
  10190. if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
  10191. (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
  10192. pr_debug_ratelimited(
  10193. "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
  10194. addr_field, maxphyaddr, count, addr);
  10195. return -EINVAL;
  10196. }
  10197. return 0;
  10198. }
  10199. static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
  10200. struct vmcs12 *vmcs12)
  10201. {
  10202. if (vmcs12->vm_exit_msr_load_count == 0 &&
  10203. vmcs12->vm_exit_msr_store_count == 0 &&
  10204. vmcs12->vm_entry_msr_load_count == 0)
  10205. return 0; /* Fast path */
  10206. if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
  10207. VM_EXIT_MSR_LOAD_ADDR) ||
  10208. nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
  10209. VM_EXIT_MSR_STORE_ADDR) ||
  10210. nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
  10211. VM_ENTRY_MSR_LOAD_ADDR))
  10212. return -EINVAL;
  10213. return 0;
  10214. }
  10215. static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
  10216. struct vmcs12 *vmcs12)
  10217. {
  10218. u64 address = vmcs12->pml_address;
  10219. int maxphyaddr = cpuid_maxphyaddr(vcpu);
  10220. if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
  10221. if (!nested_cpu_has_ept(vmcs12) ||
  10222. !IS_ALIGNED(address, 4096) ||
  10223. address >> maxphyaddr)
  10224. return -EINVAL;
  10225. }
  10226. return 0;
  10227. }
  10228. static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
  10229. struct vmcs12 *vmcs12)
  10230. {
  10231. if (!nested_cpu_has_shadow_vmcs(vmcs12))
  10232. return 0;
  10233. if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
  10234. !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
  10235. return -EINVAL;
  10236. return 0;
  10237. }
  10238. static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
  10239. struct vmx_msr_entry *e)
  10240. {
  10241. /* x2APIC MSR accesses are not allowed */
  10242. if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
  10243. return -EINVAL;
  10244. if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
  10245. e->index == MSR_IA32_UCODE_REV)
  10246. return -EINVAL;
  10247. if (e->reserved != 0)
  10248. return -EINVAL;
  10249. return 0;
  10250. }
  10251. static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
  10252. struct vmx_msr_entry *e)
  10253. {
  10254. if (e->index == MSR_FS_BASE ||
  10255. e->index == MSR_GS_BASE ||
  10256. e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
  10257. nested_vmx_msr_check_common(vcpu, e))
  10258. return -EINVAL;
  10259. return 0;
  10260. }
  10261. static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
  10262. struct vmx_msr_entry *e)
  10263. {
  10264. if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
  10265. nested_vmx_msr_check_common(vcpu, e))
  10266. return -EINVAL;
  10267. return 0;
  10268. }
  10269. /*
  10270. * Load guest's/host's msr at nested entry/exit.
  10271. * return 0 for success, entry index for failure.
  10272. */
  10273. static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
  10274. {
  10275. u32 i;
  10276. struct vmx_msr_entry e;
  10277. struct msr_data msr;
  10278. msr.host_initiated = false;
  10279. for (i = 0; i < count; i++) {
  10280. if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
  10281. &e, sizeof(e))) {
  10282. pr_debug_ratelimited(
  10283. "%s cannot read MSR entry (%u, 0x%08llx)\n",
  10284. __func__, i, gpa + i * sizeof(e));
  10285. goto fail;
  10286. }
  10287. if (nested_vmx_load_msr_check(vcpu, &e)) {
  10288. pr_debug_ratelimited(
  10289. "%s check failed (%u, 0x%x, 0x%x)\n",
  10290. __func__, i, e.index, e.reserved);
  10291. goto fail;
  10292. }
  10293. msr.index = e.index;
  10294. msr.data = e.value;
  10295. if (kvm_set_msr(vcpu, &msr)) {
  10296. pr_debug_ratelimited(
  10297. "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
  10298. __func__, i, e.index, e.value);
  10299. goto fail;
  10300. }
  10301. }
  10302. return 0;
  10303. fail:
  10304. return i + 1;
  10305. }
  10306. static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
  10307. {
  10308. u32 i;
  10309. struct vmx_msr_entry e;
  10310. for (i = 0; i < count; i++) {
  10311. struct msr_data msr_info;
  10312. if (kvm_vcpu_read_guest(vcpu,
  10313. gpa + i * sizeof(e),
  10314. &e, 2 * sizeof(u32))) {
  10315. pr_debug_ratelimited(
  10316. "%s cannot read MSR entry (%u, 0x%08llx)\n",
  10317. __func__, i, gpa + i * sizeof(e));
  10318. return -EINVAL;
  10319. }
  10320. if (nested_vmx_store_msr_check(vcpu, &e)) {
  10321. pr_debug_ratelimited(
  10322. "%s check failed (%u, 0x%x, 0x%x)\n",
  10323. __func__, i, e.index, e.reserved);
  10324. return -EINVAL;
  10325. }
  10326. msr_info.host_initiated = false;
  10327. msr_info.index = e.index;
  10328. if (kvm_get_msr(vcpu, &msr_info)) {
  10329. pr_debug_ratelimited(
  10330. "%s cannot read MSR (%u, 0x%x)\n",
  10331. __func__, i, e.index);
  10332. return -EINVAL;
  10333. }
  10334. if (kvm_vcpu_write_guest(vcpu,
  10335. gpa + i * sizeof(e) +
  10336. offsetof(struct vmx_msr_entry, value),
  10337. &msr_info.data, sizeof(msr_info.data))) {
  10338. pr_debug_ratelimited(
  10339. "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
  10340. __func__, i, e.index, msr_info.data);
  10341. return -EINVAL;
  10342. }
  10343. }
  10344. return 0;
  10345. }
  10346. static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
  10347. {
  10348. unsigned long invalid_mask;
  10349. invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
  10350. return (val & invalid_mask) == 0;
  10351. }
  10352. /*
  10353. * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
  10354. * emulating VM entry into a guest with EPT enabled.
  10355. * Returns 0 on success, 1 on failure. Invalid state exit qualification code
  10356. * is assigned to entry_failure_code on failure.
  10357. */
  10358. static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
  10359. u32 *entry_failure_code)
  10360. {
  10361. if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
  10362. if (!nested_cr3_valid(vcpu, cr3)) {
  10363. *entry_failure_code = ENTRY_FAIL_DEFAULT;
  10364. return 1;
  10365. }
  10366. /*
  10367. * If PAE paging and EPT are both on, CR3 is not used by the CPU and
  10368. * must not be dereferenced.
  10369. */
  10370. if (is_pae_paging(vcpu) && !nested_ept) {
  10371. if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
  10372. *entry_failure_code = ENTRY_FAIL_PDPTE;
  10373. return 1;
  10374. }
  10375. }
  10376. }
  10377. if (!nested_ept)
  10378. kvm_mmu_new_cr3(vcpu, cr3, false);
  10379. vcpu->arch.cr3 = cr3;
  10380. __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
  10381. kvm_init_mmu(vcpu, false);
  10382. return 0;
  10383. }
  10384. static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  10385. {
  10386. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10387. vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
  10388. vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
  10389. vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
  10390. vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
  10391. vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
  10392. vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
  10393. vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
  10394. vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
  10395. vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
  10396. vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
  10397. vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
  10398. vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
  10399. vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
  10400. vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
  10401. vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
  10402. vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
  10403. vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
  10404. vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
  10405. vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
  10406. vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
  10407. vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
  10408. vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
  10409. vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
  10410. vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
  10411. vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
  10412. vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
  10413. vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
  10414. vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
  10415. vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
  10416. vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
  10417. vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
  10418. vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
  10419. vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
  10420. vmcs12->guest_pending_dbg_exceptions);
  10421. vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
  10422. vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
  10423. if (nested_cpu_has_xsaves(vmcs12))
  10424. vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
  10425. vmcs_write64(VMCS_LINK_POINTER, -1ull);
  10426. if (cpu_has_vmx_posted_intr())
  10427. vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
  10428. /*
  10429. * Whether page-faults are trapped is determined by a combination of
  10430. * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
  10431. * If enable_ept, L0 doesn't care about page faults and we should
  10432. * set all of these to L1's desires. However, if !enable_ept, L0 does
  10433. * care about (at least some) page faults, and because it is not easy
  10434. * (if at all possible?) to merge L0 and L1's desires, we simply ask
  10435. * to exit on each and every L2 page fault. This is done by setting
  10436. * MASK=MATCH=0 and (see below) EB.PF=1.
  10437. * Note that below we don't need special code to set EB.PF beyond the
  10438. * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
  10439. * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
  10440. * !enable_ept, EB.PF is 1, so the "or" will always be 1.
  10441. */
  10442. vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
  10443. enable_ept ? vmcs12->page_fault_error_code_mask : 0);
  10444. vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
  10445. enable_ept ? vmcs12->page_fault_error_code_match : 0);
  10446. /* All VMFUNCs are currently emulated through L0 vmexits. */
  10447. if (cpu_has_vmx_vmfunc())
  10448. vmcs_write64(VM_FUNCTION_CONTROL, 0);
  10449. if (cpu_has_vmx_apicv()) {
  10450. vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
  10451. vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
  10452. vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
  10453. vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
  10454. }
  10455. /*
  10456. * Set host-state according to L0's settings (vmcs12 is irrelevant here)
  10457. * Some constant fields are set here by vmx_set_constant_host_state().
  10458. * Other fields are different per CPU, and will be set later when
  10459. * vmx_vcpu_load() is called, and when vmx_prepare_switch_to_guest()
  10460. * is called.
  10461. */
  10462. vmx_set_constant_host_state(vmx);
  10463. /*
  10464. * Set the MSR load/store lists to match L0's settings.
  10465. */
  10466. vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
  10467. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
  10468. vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
  10469. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
  10470. vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
  10471. set_cr4_guest_host_mask(vmx);
  10472. if (kvm_mpx_supported()) {
  10473. if (vmx->nested.nested_run_pending &&
  10474. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
  10475. vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
  10476. else
  10477. vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs);
  10478. }
  10479. if (enable_vpid) {
  10480. if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
  10481. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
  10482. else
  10483. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
  10484. }
  10485. /*
  10486. * L1 may access the L2's PDPTR, so save them to construct vmcs12
  10487. */
  10488. if (enable_ept) {
  10489. vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
  10490. vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
  10491. vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
  10492. vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
  10493. }
  10494. if (cpu_has_vmx_msr_bitmap())
  10495. vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
  10496. }
  10497. /*
  10498. * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
  10499. * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
  10500. * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
  10501. * guest in a way that will both be appropriate to L1's requests, and our
  10502. * needs. In addition to modifying the active vmcs (which is vmcs02), this
  10503. * function also has additional necessary side-effects, like setting various
  10504. * vcpu->arch fields.
  10505. * Returns 0 on success, 1 on failure. Invalid state exit qualification code
  10506. * is assigned to entry_failure_code on failure.
  10507. */
  10508. static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
  10509. u32 *entry_failure_code)
  10510. {
  10511. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10512. u32 exec_control, vmcs12_exec_ctrl;
  10513. if (vmx->nested.dirty_vmcs12) {
  10514. prepare_vmcs02_full(vcpu, vmcs12);
  10515. vmx->nested.dirty_vmcs12 = false;
  10516. }
  10517. /*
  10518. * First, the fields that are shadowed. This must be kept in sync
  10519. * with vmx_shadow_fields.h.
  10520. */
  10521. vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
  10522. vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
  10523. vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
  10524. vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
  10525. vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
  10526. if (vmx->nested.nested_run_pending &&
  10527. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
  10528. kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
  10529. vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
  10530. } else {
  10531. kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
  10532. vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
  10533. }
  10534. if (vmx->nested.nested_run_pending) {
  10535. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  10536. vmcs12->vm_entry_intr_info_field);
  10537. vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
  10538. vmcs12->vm_entry_exception_error_code);
  10539. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  10540. vmcs12->vm_entry_instruction_len);
  10541. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
  10542. vmcs12->guest_interruptibility_info);
  10543. vmx->loaded_vmcs->nmi_known_unmasked =
  10544. !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
  10545. } else {
  10546. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
  10547. }
  10548. vmx_set_rflags(vcpu, vmcs12->guest_rflags);
  10549. exec_control = vmcs12->pin_based_vm_exec_control;
  10550. /* Preemption timer setting is computed directly in vmx_vcpu_run. */
  10551. exec_control |= vmcs_config.pin_based_exec_ctrl;
  10552. exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
  10553. vmx->loaded_vmcs->hv_timer_armed = false;
  10554. /* Posted interrupts setting is only taken from vmcs12. */
  10555. if (nested_cpu_has_posted_intr(vmcs12)) {
  10556. vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
  10557. vmx->nested.pi_pending = false;
  10558. } else {
  10559. exec_control &= ~PIN_BASED_POSTED_INTR;
  10560. }
  10561. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
  10562. vmx->nested.preemption_timer_expired = false;
  10563. if (nested_cpu_has_preemption_timer(vmcs12))
  10564. vmx_start_preemption_timer(vcpu);
  10565. if (cpu_has_secondary_exec_ctrls()) {
  10566. exec_control = vmx->secondary_exec_control;
  10567. /* Take the following fields only from vmcs12 */
  10568. exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  10569. SECONDARY_EXEC_ENABLE_INVPCID |
  10570. SECONDARY_EXEC_RDTSCP |
  10571. SECONDARY_EXEC_XSAVES |
  10572. SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
  10573. SECONDARY_EXEC_APIC_REGISTER_VIRT |
  10574. SECONDARY_EXEC_ENABLE_VMFUNC);
  10575. if (nested_cpu_has(vmcs12,
  10576. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
  10577. vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
  10578. ~SECONDARY_EXEC_ENABLE_PML;
  10579. exec_control |= vmcs12_exec_ctrl;
  10580. }
  10581. /* VMCS shadowing for L2 is emulated for now */
  10582. exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
  10583. if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
  10584. vmcs_write16(GUEST_INTR_STATUS,
  10585. vmcs12->guest_intr_status);
  10586. /*
  10587. * Write an illegal value to APIC_ACCESS_ADDR. Later,
  10588. * nested_get_vmcs12_pages will either fix it up or
  10589. * remove the VM execution control.
  10590. */
  10591. if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
  10592. vmcs_write64(APIC_ACCESS_ADDR, -1ull);
  10593. if (exec_control & SECONDARY_EXEC_ENCLS_EXITING)
  10594. vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
  10595. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
  10596. }
  10597. /*
  10598. * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
  10599. * entry, but only if the current (host) sp changed from the value
  10600. * we wrote last (vmx->host_rsp). This cache is no longer relevant
  10601. * if we switch vmcs, and rather than hold a separate cache per vmcs,
  10602. * here we just force the write to happen on entry.
  10603. */
  10604. vmx->host_rsp = 0;
  10605. exec_control = vmx_exec_control(vmx); /* L0's desires */
  10606. exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
  10607. exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
  10608. exec_control &= ~CPU_BASED_TPR_SHADOW;
  10609. exec_control |= vmcs12->cpu_based_vm_exec_control;
  10610. /*
  10611. * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
  10612. * nested_get_vmcs12_pages can't fix it up, the illegal value
  10613. * will result in a VM entry failure.
  10614. */
  10615. if (exec_control & CPU_BASED_TPR_SHADOW) {
  10616. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
  10617. vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
  10618. } else {
  10619. #ifdef CONFIG_X86_64
  10620. exec_control |= CPU_BASED_CR8_LOAD_EXITING |
  10621. CPU_BASED_CR8_STORE_EXITING;
  10622. #endif
  10623. }
  10624. /*
  10625. * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
  10626. * for I/O port accesses.
  10627. */
  10628. exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
  10629. exec_control |= CPU_BASED_UNCOND_IO_EXITING;
  10630. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
  10631. /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
  10632. * bitwise-or of what L1 wants to trap for L2, and what we want to
  10633. * trap. Note that CR0.TS also needs updating - we do this later.
  10634. */
  10635. update_exception_bitmap(vcpu);
  10636. vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
  10637. vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
  10638. /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
  10639. * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
  10640. * bits are further modified by vmx_set_efer() below.
  10641. */
  10642. vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
  10643. /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
  10644. * emulated by vmx_set_efer(), below.
  10645. */
  10646. vm_entry_controls_init(vmx,
  10647. (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
  10648. ~VM_ENTRY_IA32E_MODE) |
  10649. (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
  10650. if (vmx->nested.nested_run_pending &&
  10651. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
  10652. vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
  10653. vcpu->arch.pat = vmcs12->guest_ia32_pat;
  10654. } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
  10655. vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
  10656. }
  10657. vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
  10658. if (kvm_has_tsc_control)
  10659. decache_tsc_multiplier(vmx);
  10660. if (enable_vpid) {
  10661. /*
  10662. * There is no direct mapping between vpid02 and vpid12, the
  10663. * vpid02 is per-vCPU for L0 and reused while the value of
  10664. * vpid12 is changed w/ one invvpid during nested vmentry.
  10665. * The vpid12 is allocated by L1 for L2, so it will not
  10666. * influence global bitmap(for vpid01 and vpid02 allocation)
  10667. * even if spawn a lot of nested vCPUs.
  10668. */
  10669. if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
  10670. if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
  10671. vmx->nested.last_vpid = vmcs12->virtual_processor_id;
  10672. __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
  10673. }
  10674. } else {
  10675. vmx_flush_tlb(vcpu, true);
  10676. }
  10677. }
  10678. if (enable_pml) {
  10679. /*
  10680. * Conceptually we want to copy the PML address and index from
  10681. * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
  10682. * since we always flush the log on each vmexit, this happens
  10683. * to be equivalent to simply resetting the fields in vmcs02.
  10684. */
  10685. ASSERT(vmx->pml_pg);
  10686. vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
  10687. vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
  10688. }
  10689. if (nested_cpu_has_ept(vmcs12)) {
  10690. if (nested_ept_init_mmu_context(vcpu)) {
  10691. *entry_failure_code = ENTRY_FAIL_DEFAULT;
  10692. return 1;
  10693. }
  10694. } else if (nested_cpu_has2(vmcs12,
  10695. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
  10696. vmx_flush_tlb(vcpu, true);
  10697. }
  10698. /*
  10699. * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
  10700. * bits which we consider mandatory enabled.
  10701. * The CR0_READ_SHADOW is what L2 should have expected to read given
  10702. * the specifications by L1; It's not enough to take
  10703. * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
  10704. * have more bits than L1 expected.
  10705. */
  10706. vmx_set_cr0(vcpu, vmcs12->guest_cr0);
  10707. vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
  10708. vmx_set_cr4(vcpu, vmcs12->guest_cr4);
  10709. vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
  10710. if (vmx->nested.nested_run_pending &&
  10711. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
  10712. vcpu->arch.efer = vmcs12->guest_ia32_efer;
  10713. else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
  10714. vcpu->arch.efer |= (EFER_LMA | EFER_LME);
  10715. else
  10716. vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
  10717. /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
  10718. vmx_set_efer(vcpu, vcpu->arch.efer);
  10719. /*
  10720. * Guest state is invalid and unrestricted guest is disabled,
  10721. * which means L1 attempted VMEntry to L2 with invalid state.
  10722. * Fail the VMEntry.
  10723. */
  10724. if (vmx->emulation_required) {
  10725. *entry_failure_code = ENTRY_FAIL_DEFAULT;
  10726. return 1;
  10727. }
  10728. /* Shadow page tables on either EPT or shadow page tables. */
  10729. if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
  10730. entry_failure_code))
  10731. return 1;
  10732. if (!enable_ept)
  10733. vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
  10734. kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
  10735. kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
  10736. return 0;
  10737. }
  10738. static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
  10739. {
  10740. if (!nested_cpu_has_nmi_exiting(vmcs12) &&
  10741. nested_cpu_has_virtual_nmis(vmcs12))
  10742. return -EINVAL;
  10743. if (!nested_cpu_has_virtual_nmis(vmcs12) &&
  10744. nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
  10745. return -EINVAL;
  10746. return 0;
  10747. }
  10748. static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  10749. {
  10750. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10751. if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
  10752. vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
  10753. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10754. if (nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id)
  10755. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10756. if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
  10757. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10758. if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
  10759. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10760. if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
  10761. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10762. if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
  10763. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10764. if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
  10765. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10766. if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
  10767. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10768. if (nested_vmx_check_pml_controls(vcpu, vmcs12))
  10769. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10770. if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12))
  10771. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10772. if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
  10773. vmx->nested.msrs.procbased_ctls_low,
  10774. vmx->nested.msrs.procbased_ctls_high) ||
  10775. (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
  10776. !vmx_control_verify(vmcs12->secondary_vm_exec_control,
  10777. vmx->nested.msrs.secondary_ctls_low,
  10778. vmx->nested.msrs.secondary_ctls_high)) ||
  10779. !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
  10780. vmx->nested.msrs.pinbased_ctls_low,
  10781. vmx->nested.msrs.pinbased_ctls_high) ||
  10782. !vmx_control_verify(vmcs12->vm_exit_controls,
  10783. vmx->nested.msrs.exit_ctls_low,
  10784. vmx->nested.msrs.exit_ctls_high) ||
  10785. !vmx_control_verify(vmcs12->vm_entry_controls,
  10786. vmx->nested.msrs.entry_ctls_low,
  10787. vmx->nested.msrs.entry_ctls_high))
  10788. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10789. if (nested_vmx_check_nmi_controls(vmcs12))
  10790. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10791. if (nested_cpu_has_vmfunc(vmcs12)) {
  10792. if (vmcs12->vm_function_control &
  10793. ~vmx->nested.msrs.vmfunc_controls)
  10794. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10795. if (nested_cpu_has_eptp_switching(vmcs12)) {
  10796. if (!nested_cpu_has_ept(vmcs12) ||
  10797. !page_address_valid(vcpu, vmcs12->eptp_list_address))
  10798. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10799. }
  10800. }
  10801. if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
  10802. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10803. if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
  10804. !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
  10805. !nested_cr3_valid(vcpu, vmcs12->host_cr3))
  10806. return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
  10807. /*
  10808. * From the Intel SDM, volume 3:
  10809. * Fields relevant to VM-entry event injection must be set properly.
  10810. * These fields are the VM-entry interruption-information field, the
  10811. * VM-entry exception error code, and the VM-entry instruction length.
  10812. */
  10813. if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
  10814. u32 intr_info = vmcs12->vm_entry_intr_info_field;
  10815. u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
  10816. u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
  10817. bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
  10818. bool should_have_error_code;
  10819. bool urg = nested_cpu_has2(vmcs12,
  10820. SECONDARY_EXEC_UNRESTRICTED_GUEST);
  10821. bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
  10822. /* VM-entry interruption-info field: interruption type */
  10823. if (intr_type == INTR_TYPE_RESERVED ||
  10824. (intr_type == INTR_TYPE_OTHER_EVENT &&
  10825. !nested_cpu_supports_monitor_trap_flag(vcpu)))
  10826. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10827. /* VM-entry interruption-info field: vector */
  10828. if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
  10829. (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
  10830. (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
  10831. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10832. /* VM-entry interruption-info field: deliver error code */
  10833. should_have_error_code =
  10834. intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
  10835. x86_exception_has_error_code(vector);
  10836. if (has_error_code != should_have_error_code)
  10837. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10838. /* VM-entry exception error code */
  10839. if (has_error_code &&
  10840. vmcs12->vm_entry_exception_error_code & GENMASK(31, 16))
  10841. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10842. /* VM-entry interruption-info field: reserved bits */
  10843. if (intr_info & INTR_INFO_RESVD_BITS_MASK)
  10844. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10845. /* VM-entry instruction length */
  10846. switch (intr_type) {
  10847. case INTR_TYPE_SOFT_EXCEPTION:
  10848. case INTR_TYPE_SOFT_INTR:
  10849. case INTR_TYPE_PRIV_SW_EXCEPTION:
  10850. if ((vmcs12->vm_entry_instruction_len > 15) ||
  10851. (vmcs12->vm_entry_instruction_len == 0 &&
  10852. !nested_cpu_has_zero_length_injection(vcpu)))
  10853. return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
  10854. }
  10855. }
  10856. return 0;
  10857. }
  10858. static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
  10859. struct vmcs12 *vmcs12)
  10860. {
  10861. int r;
  10862. struct page *page;
  10863. struct vmcs12 *shadow;
  10864. if (vmcs12->vmcs_link_pointer == -1ull)
  10865. return 0;
  10866. if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
  10867. return -EINVAL;
  10868. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
  10869. if (is_error_page(page))
  10870. return -EINVAL;
  10871. r = 0;
  10872. shadow = kmap(page);
  10873. if (shadow->hdr.revision_id != VMCS12_REVISION ||
  10874. shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
  10875. r = -EINVAL;
  10876. kunmap(page);
  10877. kvm_release_page_clean(page);
  10878. return r;
  10879. }
  10880. static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
  10881. u32 *exit_qual)
  10882. {
  10883. bool ia32e;
  10884. *exit_qual = ENTRY_FAIL_DEFAULT;
  10885. if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
  10886. !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
  10887. return 1;
  10888. if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
  10889. *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
  10890. return 1;
  10891. }
  10892. /*
  10893. * If the load IA32_EFER VM-entry control is 1, the following checks
  10894. * are performed on the field for the IA32_EFER MSR:
  10895. * - Bits reserved in the IA32_EFER MSR must be 0.
  10896. * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
  10897. * the IA-32e mode guest VM-exit control. It must also be identical
  10898. * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
  10899. * CR0.PG) is 1.
  10900. */
  10901. if (to_vmx(vcpu)->nested.nested_run_pending &&
  10902. (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
  10903. ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
  10904. if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
  10905. ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
  10906. ((vmcs12->guest_cr0 & X86_CR0_PG) &&
  10907. ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
  10908. return 1;
  10909. }
  10910. /*
  10911. * If the load IA32_EFER VM-exit control is 1, bits reserved in the
  10912. * IA32_EFER MSR must be 0 in the field for that register. In addition,
  10913. * the values of the LMA and LME bits in the field must each be that of
  10914. * the host address-space size VM-exit control.
  10915. */
  10916. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
  10917. ia32e = (vmcs12->vm_exit_controls &
  10918. VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
  10919. if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
  10920. ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
  10921. ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
  10922. return 1;
  10923. }
  10924. if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
  10925. (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
  10926. (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
  10927. return 1;
  10928. return 0;
  10929. }
  10930. /*
  10931. * If exit_qual is NULL, this is being called from state restore (either RSM
  10932. * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
  10933. */
  10934. static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, u32 *exit_qual)
  10935. {
  10936. struct vcpu_vmx *vmx = to_vmx(vcpu);
  10937. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  10938. bool from_vmentry = !!exit_qual;
  10939. u32 dummy_exit_qual;
  10940. bool evaluate_pending_interrupts;
  10941. int r = 0;
  10942. evaluate_pending_interrupts = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
  10943. (CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_VIRTUAL_NMI_PENDING);
  10944. if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu))
  10945. evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu);
  10946. enter_guest_mode(vcpu);
  10947. if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
  10948. vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
  10949. if (kvm_mpx_supported() &&
  10950. !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
  10951. vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
  10952. vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
  10953. vmx_segment_cache_clear(vmx);
  10954. if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
  10955. vcpu->arch.tsc_offset += vmcs12->tsc_offset;
  10956. r = EXIT_REASON_INVALID_STATE;
  10957. if (prepare_vmcs02(vcpu, vmcs12, from_vmentry ? exit_qual : &dummy_exit_qual))
  10958. goto fail;
  10959. if (from_vmentry) {
  10960. nested_get_vmcs12_pages(vcpu);
  10961. r = EXIT_REASON_MSR_LOAD_FAIL;
  10962. *exit_qual = nested_vmx_load_msr(vcpu,
  10963. vmcs12->vm_entry_msr_load_addr,
  10964. vmcs12->vm_entry_msr_load_count);
  10965. if (*exit_qual)
  10966. goto fail;
  10967. } else {
  10968. /*
  10969. * The MMU is not initialized to point at the right entities yet and
  10970. * "get pages" would need to read data from the guest (i.e. we will
  10971. * need to perform gpa to hpa translation). Request a call
  10972. * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
  10973. * have already been set at vmentry time and should not be reset.
  10974. */
  10975. kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
  10976. }
  10977. /*
  10978. * If L1 had a pending IRQ/NMI until it executed
  10979. * VMLAUNCH/VMRESUME which wasn't delivered because it was
  10980. * disallowed (e.g. interrupts disabled), L0 needs to
  10981. * evaluate if this pending event should cause an exit from L2
  10982. * to L1 or delivered directly to L2 (e.g. In case L1 don't
  10983. * intercept EXTERNAL_INTERRUPT).
  10984. *
  10985. * Usually this would be handled by the processor noticing an
  10986. * IRQ/NMI window request, or checking RVI during evaluation of
  10987. * pending virtual interrupts. However, this setting was done
  10988. * on VMCS01 and now VMCS02 is active instead. Thus, we force L0
  10989. * to perform pending event evaluation by requesting a KVM_REQ_EVENT.
  10990. */
  10991. if (unlikely(evaluate_pending_interrupts))
  10992. kvm_make_request(KVM_REQ_EVENT, vcpu);
  10993. /*
  10994. * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
  10995. * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
  10996. * returned as far as L1 is concerned. It will only return (and set
  10997. * the success flag) when L2 exits (see nested_vmx_vmexit()).
  10998. */
  10999. return 0;
  11000. fail:
  11001. if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
  11002. vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
  11003. leave_guest_mode(vcpu);
  11004. vmx_switch_vmcs(vcpu, &vmx->vmcs01);
  11005. return r;
  11006. }
  11007. /*
  11008. * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
  11009. * for running an L2 nested guest.
  11010. */
  11011. static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
  11012. {
  11013. struct vmcs12 *vmcs12;
  11014. struct vcpu_vmx *vmx = to_vmx(vcpu);
  11015. u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
  11016. u32 exit_qual;
  11017. int ret;
  11018. if (!nested_vmx_check_permission(vcpu))
  11019. return 1;
  11020. if (!nested_vmx_check_vmcs12(vcpu))
  11021. goto out;
  11022. vmcs12 = get_vmcs12(vcpu);
  11023. /*
  11024. * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
  11025. * that there *is* a valid VMCS pointer, RFLAGS.CF is set
  11026. * rather than RFLAGS.ZF, and no error number is stored to the
  11027. * VM-instruction error field.
  11028. */
  11029. if (vmcs12->hdr.shadow_vmcs) {
  11030. nested_vmx_failInvalid(vcpu);
  11031. goto out;
  11032. }
  11033. if (enable_shadow_vmcs)
  11034. copy_shadow_to_vmcs12(vmx);
  11035. /*
  11036. * The nested entry process starts with enforcing various prerequisites
  11037. * on vmcs12 as required by the Intel SDM, and act appropriately when
  11038. * they fail: As the SDM explains, some conditions should cause the
  11039. * instruction to fail, while others will cause the instruction to seem
  11040. * to succeed, but return an EXIT_REASON_INVALID_STATE.
  11041. * To speed up the normal (success) code path, we should avoid checking
  11042. * for misconfigurations which will anyway be caught by the processor
  11043. * when using the merged vmcs02.
  11044. */
  11045. if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
  11046. nested_vmx_failValid(vcpu,
  11047. VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
  11048. goto out;
  11049. }
  11050. if (vmcs12->launch_state == launch) {
  11051. nested_vmx_failValid(vcpu,
  11052. launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
  11053. : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
  11054. goto out;
  11055. }
  11056. ret = check_vmentry_prereqs(vcpu, vmcs12);
  11057. if (ret) {
  11058. nested_vmx_failValid(vcpu, ret);
  11059. goto out;
  11060. }
  11061. /*
  11062. * After this point, the trap flag no longer triggers a singlestep trap
  11063. * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
  11064. * This is not 100% correct; for performance reasons, we delegate most
  11065. * of the checks on host state to the processor. If those fail,
  11066. * the singlestep trap is missed.
  11067. */
  11068. skip_emulated_instruction(vcpu);
  11069. ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
  11070. if (ret) {
  11071. nested_vmx_entry_failure(vcpu, vmcs12,
  11072. EXIT_REASON_INVALID_STATE, exit_qual);
  11073. return 1;
  11074. }
  11075. /*
  11076. * We're finally done with prerequisite checking, and can start with
  11077. * the nested entry.
  11078. */
  11079. vmx->nested.nested_run_pending = 1;
  11080. ret = enter_vmx_non_root_mode(vcpu, &exit_qual);
  11081. if (ret) {
  11082. nested_vmx_entry_failure(vcpu, vmcs12, ret, exit_qual);
  11083. vmx->nested.nested_run_pending = 0;
  11084. return 1;
  11085. }
  11086. /* Hide L1D cache contents from the nested guest. */
  11087. vmx->vcpu.arch.l1tf_flush_l1d = true;
  11088. /*
  11089. * Must happen outside of enter_vmx_non_root_mode() as it will
  11090. * also be used as part of restoring nVMX state for
  11091. * snapshot restore (migration).
  11092. *
  11093. * In this flow, it is assumed that vmcs12 cache was
  11094. * trasferred as part of captured nVMX state and should
  11095. * therefore not be read from guest memory (which may not
  11096. * exist on destination host yet).
  11097. */
  11098. nested_cache_shadow_vmcs12(vcpu, vmcs12);
  11099. /*
  11100. * If we're entering a halted L2 vcpu and the L2 vcpu won't be
  11101. * awakened by event injection or by an NMI-window VM-exit or
  11102. * by an interrupt-window VM-exit, halt the vcpu.
  11103. */
  11104. if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
  11105. !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) &&
  11106. !(vmcs12->cpu_based_vm_exec_control & CPU_BASED_VIRTUAL_NMI_PENDING) &&
  11107. !((vmcs12->cpu_based_vm_exec_control & CPU_BASED_VIRTUAL_INTR_PENDING) &&
  11108. (vmcs12->guest_rflags & X86_EFLAGS_IF))) {
  11109. vmx->nested.nested_run_pending = 0;
  11110. return kvm_vcpu_halt(vcpu);
  11111. }
  11112. return 1;
  11113. out:
  11114. return kvm_skip_emulated_instruction(vcpu);
  11115. }
  11116. /*
  11117. * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
  11118. * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
  11119. * This function returns the new value we should put in vmcs12.guest_cr0.
  11120. * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
  11121. * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
  11122. * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
  11123. * didn't trap the bit, because if L1 did, so would L0).
  11124. * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
  11125. * been modified by L2, and L1 knows it. So just leave the old value of
  11126. * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
  11127. * isn't relevant, because if L0 traps this bit it can set it to anything.
  11128. * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
  11129. * changed these bits, and therefore they need to be updated, but L0
  11130. * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
  11131. * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
  11132. */
  11133. static inline unsigned long
  11134. vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  11135. {
  11136. return
  11137. /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
  11138. /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
  11139. /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
  11140. vcpu->arch.cr0_guest_owned_bits));
  11141. }
  11142. static inline unsigned long
  11143. vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  11144. {
  11145. return
  11146. /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
  11147. /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
  11148. /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
  11149. vcpu->arch.cr4_guest_owned_bits));
  11150. }
  11151. static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
  11152. struct vmcs12 *vmcs12)
  11153. {
  11154. u32 idt_vectoring;
  11155. unsigned int nr;
  11156. if (vcpu->arch.exception.injected) {
  11157. nr = vcpu->arch.exception.nr;
  11158. idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
  11159. if (kvm_exception_is_soft(nr)) {
  11160. vmcs12->vm_exit_instruction_len =
  11161. vcpu->arch.event_exit_inst_len;
  11162. idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
  11163. } else
  11164. idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
  11165. if (vcpu->arch.exception.has_error_code) {
  11166. idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
  11167. vmcs12->idt_vectoring_error_code =
  11168. vcpu->arch.exception.error_code;
  11169. }
  11170. vmcs12->idt_vectoring_info_field = idt_vectoring;
  11171. } else if (vcpu->arch.nmi_injected) {
  11172. vmcs12->idt_vectoring_info_field =
  11173. INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
  11174. } else if (vcpu->arch.interrupt.injected) {
  11175. nr = vcpu->arch.interrupt.nr;
  11176. idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
  11177. if (vcpu->arch.interrupt.soft) {
  11178. idt_vectoring |= INTR_TYPE_SOFT_INTR;
  11179. vmcs12->vm_entry_instruction_len =
  11180. vcpu->arch.event_exit_inst_len;
  11181. } else
  11182. idt_vectoring |= INTR_TYPE_EXT_INTR;
  11183. vmcs12->idt_vectoring_info_field = idt_vectoring;
  11184. }
  11185. }
  11186. static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
  11187. {
  11188. struct vcpu_vmx *vmx = to_vmx(vcpu);
  11189. unsigned long exit_qual;
  11190. bool block_nested_events =
  11191. vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
  11192. if (vcpu->arch.exception.pending &&
  11193. nested_vmx_check_exception(vcpu, &exit_qual)) {
  11194. if (block_nested_events)
  11195. return -EBUSY;
  11196. nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
  11197. return 0;
  11198. }
  11199. if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
  11200. vmx->nested.preemption_timer_expired) {
  11201. if (block_nested_events)
  11202. return -EBUSY;
  11203. nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
  11204. return 0;
  11205. }
  11206. if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
  11207. if (block_nested_events)
  11208. return -EBUSY;
  11209. nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
  11210. NMI_VECTOR | INTR_TYPE_NMI_INTR |
  11211. INTR_INFO_VALID_MASK, 0);
  11212. /*
  11213. * The NMI-triggered VM exit counts as injection:
  11214. * clear this one and block further NMIs.
  11215. */
  11216. vcpu->arch.nmi_pending = 0;
  11217. vmx_set_nmi_mask(vcpu, true);
  11218. return 0;
  11219. }
  11220. if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
  11221. nested_exit_on_intr(vcpu)) {
  11222. if (block_nested_events)
  11223. return -EBUSY;
  11224. nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
  11225. return 0;
  11226. }
  11227. vmx_complete_nested_posted_interrupt(vcpu);
  11228. return 0;
  11229. }
  11230. static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu)
  11231. {
  11232. to_vmx(vcpu)->req_immediate_exit = true;
  11233. }
  11234. static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
  11235. {
  11236. ktime_t remaining =
  11237. hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
  11238. u64 value;
  11239. if (ktime_to_ns(remaining) <= 0)
  11240. return 0;
  11241. value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
  11242. do_div(value, 1000000);
  11243. return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
  11244. }
  11245. /*
  11246. * Update the guest state fields of vmcs12 to reflect changes that
  11247. * occurred while L2 was running. (The "IA-32e mode guest" bit of the
  11248. * VM-entry controls is also updated, since this is really a guest
  11249. * state bit.)
  11250. */
  11251. static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
  11252. {
  11253. vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
  11254. vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
  11255. vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
  11256. vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
  11257. vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
  11258. vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
  11259. vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
  11260. vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
  11261. vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
  11262. vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
  11263. vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
  11264. vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
  11265. vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
  11266. vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
  11267. vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
  11268. vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
  11269. vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
  11270. vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
  11271. vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
  11272. vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
  11273. vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
  11274. vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
  11275. vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
  11276. vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
  11277. vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
  11278. vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
  11279. vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
  11280. vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
  11281. vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
  11282. vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
  11283. vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
  11284. vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
  11285. vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
  11286. vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
  11287. vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
  11288. vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
  11289. vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
  11290. vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
  11291. vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
  11292. vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
  11293. vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
  11294. vmcs12->guest_interruptibility_info =
  11295. vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  11296. vmcs12->guest_pending_dbg_exceptions =
  11297. vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
  11298. if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
  11299. vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
  11300. else
  11301. vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
  11302. if (nested_cpu_has_preemption_timer(vmcs12)) {
  11303. if (vmcs12->vm_exit_controls &
  11304. VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
  11305. vmcs12->vmx_preemption_timer_value =
  11306. vmx_get_preemption_timer_value(vcpu);
  11307. hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
  11308. }
  11309. /*
  11310. * In some cases (usually, nested EPT), L2 is allowed to change its
  11311. * own CR3 without exiting. If it has changed it, we must keep it.
  11312. * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
  11313. * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
  11314. *
  11315. * Additionally, restore L2's PDPTR to vmcs12.
  11316. */
  11317. if (enable_ept) {
  11318. vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
  11319. vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
  11320. vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
  11321. vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
  11322. vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
  11323. }
  11324. vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
  11325. if (nested_cpu_has_vid(vmcs12))
  11326. vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
  11327. vmcs12->vm_entry_controls =
  11328. (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
  11329. (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
  11330. if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
  11331. kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
  11332. vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
  11333. }
  11334. /* TODO: These cannot have changed unless we have MSR bitmaps and
  11335. * the relevant bit asks not to trap the change */
  11336. if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
  11337. vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
  11338. if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
  11339. vmcs12->guest_ia32_efer = vcpu->arch.efer;
  11340. vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
  11341. vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
  11342. vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
  11343. if (kvm_mpx_supported())
  11344. vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
  11345. }
  11346. /*
  11347. * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
  11348. * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
  11349. * and this function updates it to reflect the changes to the guest state while
  11350. * L2 was running (and perhaps made some exits which were handled directly by L0
  11351. * without going back to L1), and to reflect the exit reason.
  11352. * Note that we do not have to copy here all VMCS fields, just those that
  11353. * could have changed by the L2 guest or the exit - i.e., the guest-state and
  11354. * exit-information fields only. Other fields are modified by L1 with VMWRITE,
  11355. * which already writes to vmcs12 directly.
  11356. */
  11357. static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
  11358. u32 exit_reason, u32 exit_intr_info,
  11359. unsigned long exit_qualification)
  11360. {
  11361. /* update guest state fields: */
  11362. sync_vmcs12(vcpu, vmcs12);
  11363. /* update exit information fields: */
  11364. vmcs12->vm_exit_reason = exit_reason;
  11365. vmcs12->exit_qualification = exit_qualification;
  11366. vmcs12->vm_exit_intr_info = exit_intr_info;
  11367. vmcs12->idt_vectoring_info_field = 0;
  11368. vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  11369. vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
  11370. if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
  11371. vmcs12->launch_state = 1;
  11372. /* vm_entry_intr_info_field is cleared on exit. Emulate this
  11373. * instead of reading the real value. */
  11374. vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
  11375. /*
  11376. * Transfer the event that L0 or L1 may wanted to inject into
  11377. * L2 to IDT_VECTORING_INFO_FIELD.
  11378. */
  11379. vmcs12_save_pending_event(vcpu, vmcs12);
  11380. }
  11381. /*
  11382. * Drop what we picked up for L2 via vmx_complete_interrupts. It is
  11383. * preserved above and would only end up incorrectly in L1.
  11384. */
  11385. vcpu->arch.nmi_injected = false;
  11386. kvm_clear_exception_queue(vcpu);
  11387. kvm_clear_interrupt_queue(vcpu);
  11388. }
  11389. /*
  11390. * A part of what we need to when the nested L2 guest exits and we want to
  11391. * run its L1 parent, is to reset L1's guest state to the host state specified
  11392. * in vmcs12.
  11393. * This function is to be called not only on normal nested exit, but also on
  11394. * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
  11395. * Failures During or After Loading Guest State").
  11396. * This function should be called when the active VMCS is L1's (vmcs01).
  11397. */
  11398. static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
  11399. struct vmcs12 *vmcs12)
  11400. {
  11401. struct kvm_segment seg;
  11402. u32 entry_failure_code;
  11403. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
  11404. vcpu->arch.efer = vmcs12->host_ia32_efer;
  11405. else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
  11406. vcpu->arch.efer |= (EFER_LMA | EFER_LME);
  11407. else
  11408. vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
  11409. vmx_set_efer(vcpu, vcpu->arch.efer);
  11410. kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
  11411. kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
  11412. vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
  11413. /*
  11414. * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
  11415. * actually changed, because vmx_set_cr0 refers to efer set above.
  11416. *
  11417. * CR0_GUEST_HOST_MASK is already set in the original vmcs01
  11418. * (KVM doesn't change it);
  11419. */
  11420. vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
  11421. vmx_set_cr0(vcpu, vmcs12->host_cr0);
  11422. /* Same as above - no reason to call set_cr4_guest_host_mask(). */
  11423. vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
  11424. vmx_set_cr4(vcpu, vmcs12->host_cr4);
  11425. nested_ept_uninit_mmu_context(vcpu);
  11426. /*
  11427. * Only PDPTE load can fail as the value of cr3 was checked on entry and
  11428. * couldn't have changed.
  11429. */
  11430. if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
  11431. nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
  11432. if (!enable_ept)
  11433. vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
  11434. /*
  11435. * If vmcs01 don't use VPID, CPU flushes TLB on every
  11436. * VMEntry/VMExit. Thus, no need to flush TLB.
  11437. *
  11438. * If vmcs12 uses VPID, TLB entries populated by L2 are
  11439. * tagged with vmx->nested.vpid02 while L1 entries are tagged
  11440. * with vmx->vpid. Thus, no need to flush TLB.
  11441. *
  11442. * Therefore, flush TLB only in case vmcs01 uses VPID and
  11443. * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
  11444. * are both tagged with vmx->vpid.
  11445. */
  11446. if (enable_vpid &&
  11447. !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
  11448. vmx_flush_tlb(vcpu, true);
  11449. }
  11450. vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
  11451. vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
  11452. vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
  11453. vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
  11454. vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
  11455. vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
  11456. vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
  11457. /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
  11458. if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
  11459. vmcs_write64(GUEST_BNDCFGS, 0);
  11460. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
  11461. vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
  11462. vcpu->arch.pat = vmcs12->host_ia32_pat;
  11463. }
  11464. if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
  11465. vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
  11466. vmcs12->host_ia32_perf_global_ctrl);
  11467. /* Set L1 segment info according to Intel SDM
  11468. 27.5.2 Loading Host Segment and Descriptor-Table Registers */
  11469. seg = (struct kvm_segment) {
  11470. .base = 0,
  11471. .limit = 0xFFFFFFFF,
  11472. .selector = vmcs12->host_cs_selector,
  11473. .type = 11,
  11474. .present = 1,
  11475. .s = 1,
  11476. .g = 1
  11477. };
  11478. if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
  11479. seg.l = 1;
  11480. else
  11481. seg.db = 1;
  11482. vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
  11483. seg = (struct kvm_segment) {
  11484. .base = 0,
  11485. .limit = 0xFFFFFFFF,
  11486. .type = 3,
  11487. .present = 1,
  11488. .s = 1,
  11489. .db = 1,
  11490. .g = 1
  11491. };
  11492. seg.selector = vmcs12->host_ds_selector;
  11493. vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
  11494. seg.selector = vmcs12->host_es_selector;
  11495. vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
  11496. seg.selector = vmcs12->host_ss_selector;
  11497. vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
  11498. seg.selector = vmcs12->host_fs_selector;
  11499. seg.base = vmcs12->host_fs_base;
  11500. vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
  11501. seg.selector = vmcs12->host_gs_selector;
  11502. seg.base = vmcs12->host_gs_base;
  11503. vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
  11504. seg = (struct kvm_segment) {
  11505. .base = vmcs12->host_tr_base,
  11506. .limit = 0x67,
  11507. .selector = vmcs12->host_tr_selector,
  11508. .type = 11,
  11509. .present = 1
  11510. };
  11511. vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
  11512. kvm_set_dr(vcpu, 7, 0x400);
  11513. vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
  11514. if (cpu_has_vmx_msr_bitmap())
  11515. vmx_update_msr_bitmap(vcpu);
  11516. if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
  11517. vmcs12->vm_exit_msr_load_count))
  11518. nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
  11519. }
  11520. static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx)
  11521. {
  11522. struct shared_msr_entry *efer_msr;
  11523. unsigned int i;
  11524. if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER)
  11525. return vmcs_read64(GUEST_IA32_EFER);
  11526. if (cpu_has_load_ia32_efer)
  11527. return host_efer;
  11528. for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) {
  11529. if (vmx->msr_autoload.guest.val[i].index == MSR_EFER)
  11530. return vmx->msr_autoload.guest.val[i].value;
  11531. }
  11532. efer_msr = find_msr_entry(vmx, MSR_EFER);
  11533. if (efer_msr)
  11534. return efer_msr->data;
  11535. return host_efer;
  11536. }
  11537. static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu)
  11538. {
  11539. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  11540. struct vcpu_vmx *vmx = to_vmx(vcpu);
  11541. struct vmx_msr_entry g, h;
  11542. struct msr_data msr;
  11543. gpa_t gpa;
  11544. u32 i, j;
  11545. vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT);
  11546. if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
  11547. /*
  11548. * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set
  11549. * as vmcs01.GUEST_DR7 contains a userspace defined value
  11550. * and vcpu->arch.dr7 is not squirreled away before the
  11551. * nested VMENTER (not worth adding a variable in nested_vmx).
  11552. */
  11553. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
  11554. kvm_set_dr(vcpu, 7, DR7_FIXED_1);
  11555. else
  11556. WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7)));
  11557. }
  11558. /*
  11559. * Note that calling vmx_set_{efer,cr0,cr4} is important as they
  11560. * handle a variety of side effects to KVM's software model.
  11561. */
  11562. vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx));
  11563. vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
  11564. vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW));
  11565. vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
  11566. vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW));
  11567. nested_ept_uninit_mmu_context(vcpu);
  11568. vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
  11569. __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
  11570. /*
  11571. * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs
  11572. * from vmcs01 (if necessary). The PDPTRs are not loaded on
  11573. * VMFail, like everything else we just need to ensure our
  11574. * software model is up-to-date.
  11575. */
  11576. ept_save_pdptrs(vcpu);
  11577. kvm_mmu_reset_context(vcpu);
  11578. if (cpu_has_vmx_msr_bitmap())
  11579. vmx_update_msr_bitmap(vcpu);
  11580. /*
  11581. * This nasty bit of open coding is a compromise between blindly
  11582. * loading L1's MSRs using the exit load lists (incorrect emulation
  11583. * of VMFail), leaving the nested VM's MSRs in the software model
  11584. * (incorrect behavior) and snapshotting the modified MSRs (too
  11585. * expensive since the lists are unbound by hardware). For each
  11586. * MSR that was (prematurely) loaded from the nested VMEntry load
  11587. * list, reload it from the exit load list if it exists and differs
  11588. * from the guest value. The intent is to stuff host state as
  11589. * silently as possible, not to fully process the exit load list.
  11590. */
  11591. msr.host_initiated = false;
  11592. for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) {
  11593. gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g));
  11594. if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) {
  11595. pr_debug_ratelimited(
  11596. "%s read MSR index failed (%u, 0x%08llx)\n",
  11597. __func__, i, gpa);
  11598. goto vmabort;
  11599. }
  11600. for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) {
  11601. gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h));
  11602. if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) {
  11603. pr_debug_ratelimited(
  11604. "%s read MSR failed (%u, 0x%08llx)\n",
  11605. __func__, j, gpa);
  11606. goto vmabort;
  11607. }
  11608. if (h.index != g.index)
  11609. continue;
  11610. if (h.value == g.value)
  11611. break;
  11612. if (nested_vmx_load_msr_check(vcpu, &h)) {
  11613. pr_debug_ratelimited(
  11614. "%s check failed (%u, 0x%x, 0x%x)\n",
  11615. __func__, j, h.index, h.reserved);
  11616. goto vmabort;
  11617. }
  11618. msr.index = h.index;
  11619. msr.data = h.value;
  11620. if (kvm_set_msr(vcpu, &msr)) {
  11621. pr_debug_ratelimited(
  11622. "%s WRMSR failed (%u, 0x%x, 0x%llx)\n",
  11623. __func__, j, h.index, h.value);
  11624. goto vmabort;
  11625. }
  11626. }
  11627. }
  11628. return;
  11629. vmabort:
  11630. nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
  11631. }
  11632. /*
  11633. * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
  11634. * and modify vmcs12 to make it see what it would expect to see there if
  11635. * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
  11636. */
  11637. static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
  11638. u32 exit_intr_info,
  11639. unsigned long exit_qualification)
  11640. {
  11641. struct vcpu_vmx *vmx = to_vmx(vcpu);
  11642. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  11643. /* trying to cancel vmlaunch/vmresume is a bug */
  11644. WARN_ON_ONCE(vmx->nested.nested_run_pending);
  11645. /*
  11646. * The only expected VM-instruction error is "VM entry with
  11647. * invalid control field(s)." Anything else indicates a
  11648. * problem with L0.
  11649. */
  11650. WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
  11651. VMXERR_ENTRY_INVALID_CONTROL_FIELD));
  11652. leave_guest_mode(vcpu);
  11653. if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
  11654. vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
  11655. if (likely(!vmx->fail)) {
  11656. if (exit_reason == -1)
  11657. sync_vmcs12(vcpu, vmcs12);
  11658. else
  11659. prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
  11660. exit_qualification);
  11661. /*
  11662. * Must happen outside of sync_vmcs12() as it will
  11663. * also be used to capture vmcs12 cache as part of
  11664. * capturing nVMX state for snapshot (migration).
  11665. *
  11666. * Otherwise, this flush will dirty guest memory at a
  11667. * point it is already assumed by user-space to be
  11668. * immutable.
  11669. */
  11670. nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
  11671. if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
  11672. vmcs12->vm_exit_msr_store_count))
  11673. nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
  11674. }
  11675. vmx_switch_vmcs(vcpu, &vmx->vmcs01);
  11676. vm_entry_controls_reset_shadow(vmx);
  11677. vm_exit_controls_reset_shadow(vmx);
  11678. vmx_segment_cache_clear(vmx);
  11679. /* Update any VMCS fields that might have changed while L2 ran */
  11680. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
  11681. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
  11682. vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
  11683. if (kvm_has_tsc_control)
  11684. decache_tsc_multiplier(vmx);
  11685. if (vmx->nested.change_vmcs01_virtual_apic_mode) {
  11686. vmx->nested.change_vmcs01_virtual_apic_mode = false;
  11687. vmx_set_virtual_apic_mode(vcpu);
  11688. } else if (!nested_cpu_has_ept(vmcs12) &&
  11689. nested_cpu_has2(vmcs12,
  11690. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
  11691. vmx_flush_tlb(vcpu, true);
  11692. }
  11693. /* This is needed for same reason as it was needed in prepare_vmcs02 */
  11694. vmx->host_rsp = 0;
  11695. /* Unpin physical memory we referred to in vmcs02 */
  11696. if (vmx->nested.apic_access_page) {
  11697. kvm_release_page_dirty(vmx->nested.apic_access_page);
  11698. vmx->nested.apic_access_page = NULL;
  11699. }
  11700. if (vmx->nested.virtual_apic_page) {
  11701. kvm_release_page_dirty(vmx->nested.virtual_apic_page);
  11702. vmx->nested.virtual_apic_page = NULL;
  11703. }
  11704. if (vmx->nested.pi_desc_page) {
  11705. kunmap(vmx->nested.pi_desc_page);
  11706. kvm_release_page_dirty(vmx->nested.pi_desc_page);
  11707. vmx->nested.pi_desc_page = NULL;
  11708. vmx->nested.pi_desc = NULL;
  11709. }
  11710. /*
  11711. * We are now running in L2, mmu_notifier will force to reload the
  11712. * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
  11713. */
  11714. kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
  11715. if (enable_shadow_vmcs && exit_reason != -1)
  11716. vmx->nested.sync_shadow_vmcs = true;
  11717. /* in case we halted in L2 */
  11718. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  11719. if (likely(!vmx->fail)) {
  11720. /*
  11721. * TODO: SDM says that with acknowledge interrupt on
  11722. * exit, bit 31 of the VM-exit interrupt information
  11723. * (valid interrupt) is always set to 1 on
  11724. * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
  11725. * need kvm_cpu_has_interrupt(). See the commit
  11726. * message for details.
  11727. */
  11728. if (nested_exit_intr_ack_set(vcpu) &&
  11729. exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
  11730. kvm_cpu_has_interrupt(vcpu)) {
  11731. int irq = kvm_cpu_get_interrupt(vcpu);
  11732. WARN_ON(irq < 0);
  11733. vmcs12->vm_exit_intr_info = irq |
  11734. INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
  11735. }
  11736. if (exit_reason != -1)
  11737. trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
  11738. vmcs12->exit_qualification,
  11739. vmcs12->idt_vectoring_info_field,
  11740. vmcs12->vm_exit_intr_info,
  11741. vmcs12->vm_exit_intr_error_code,
  11742. KVM_ISA_VMX);
  11743. load_vmcs12_host_state(vcpu, vmcs12);
  11744. return;
  11745. }
  11746. /*
  11747. * After an early L2 VM-entry failure, we're now back
  11748. * in L1 which thinks it just finished a VMLAUNCH or
  11749. * VMRESUME instruction, so we need to set the failure
  11750. * flag and the VM-instruction error field of the VMCS
  11751. * accordingly.
  11752. */
  11753. nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
  11754. /*
  11755. * Restore L1's host state to KVM's software model. We're here
  11756. * because a consistency check was caught by hardware, which
  11757. * means some amount of guest state has been propagated to KVM's
  11758. * model and needs to be unwound to the host's state.
  11759. */
  11760. nested_vmx_restore_host_state(vcpu);
  11761. /*
  11762. * The emulated instruction was already skipped in
  11763. * nested_vmx_run, but the updated RIP was never
  11764. * written back to the vmcs01.
  11765. */
  11766. skip_emulated_instruction(vcpu);
  11767. vmx->fail = 0;
  11768. }
  11769. /*
  11770. * Forcibly leave nested mode in order to be able to reset the VCPU later on.
  11771. */
  11772. static void vmx_leave_nested(struct kvm_vcpu *vcpu)
  11773. {
  11774. if (is_guest_mode(vcpu)) {
  11775. to_vmx(vcpu)->nested.nested_run_pending = 0;
  11776. nested_vmx_vmexit(vcpu, -1, 0, 0);
  11777. }
  11778. free_nested(to_vmx(vcpu));
  11779. }
  11780. /*
  11781. * L1's failure to enter L2 is a subset of a normal exit, as explained in
  11782. * 23.7 "VM-entry failures during or after loading guest state" (this also
  11783. * lists the acceptable exit-reason and exit-qualification parameters).
  11784. * It should only be called before L2 actually succeeded to run, and when
  11785. * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
  11786. */
  11787. static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
  11788. struct vmcs12 *vmcs12,
  11789. u32 reason, unsigned long qualification)
  11790. {
  11791. load_vmcs12_host_state(vcpu, vmcs12);
  11792. vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
  11793. vmcs12->exit_qualification = qualification;
  11794. nested_vmx_succeed(vcpu);
  11795. if (enable_shadow_vmcs)
  11796. to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
  11797. }
  11798. static int vmx_check_intercept_io(struct kvm_vcpu *vcpu,
  11799. struct x86_instruction_info *info)
  11800. {
  11801. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  11802. unsigned short port;
  11803. bool intercept;
  11804. int size;
  11805. if (info->intercept == x86_intercept_in ||
  11806. info->intercept == x86_intercept_ins) {
  11807. port = info->src_val;
  11808. size = info->dst_bytes;
  11809. } else {
  11810. port = info->dst_val;
  11811. size = info->src_bytes;
  11812. }
  11813. /*
  11814. * If the 'use IO bitmaps' VM-execution control is 0, IO instruction
  11815. * VM-exits depend on the 'unconditional IO exiting' VM-execution
  11816. * control.
  11817. *
  11818. * Otherwise, IO instruction VM-exits are controlled by the IO bitmaps.
  11819. */
  11820. if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
  11821. intercept = nested_cpu_has(vmcs12,
  11822. CPU_BASED_UNCOND_IO_EXITING);
  11823. else
  11824. intercept = nested_vmx_check_io_bitmaps(vcpu, port, size);
  11825. /* FIXME: produce nested vmexit and return X86EMUL_INTERCEPTED. */
  11826. return intercept ? X86EMUL_UNHANDLEABLE : X86EMUL_CONTINUE;
  11827. }
  11828. static int vmx_check_intercept(struct kvm_vcpu *vcpu,
  11829. struct x86_instruction_info *info,
  11830. enum x86_intercept_stage stage)
  11831. {
  11832. struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
  11833. struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
  11834. switch (info->intercept) {
  11835. /*
  11836. * RDPID causes #UD if disabled through secondary execution controls.
  11837. * Because it is marked as EmulateOnUD, we need to intercept it here.
  11838. */
  11839. case x86_intercept_rdtscp:
  11840. if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
  11841. ctxt->exception.vector = UD_VECTOR;
  11842. ctxt->exception.error_code_valid = false;
  11843. return X86EMUL_PROPAGATE_FAULT;
  11844. }
  11845. break;
  11846. case x86_intercept_in:
  11847. case x86_intercept_ins:
  11848. case x86_intercept_out:
  11849. case x86_intercept_outs:
  11850. return vmx_check_intercept_io(vcpu, info);
  11851. case x86_intercept_lgdt:
  11852. case x86_intercept_lidt:
  11853. case x86_intercept_lldt:
  11854. case x86_intercept_ltr:
  11855. case x86_intercept_sgdt:
  11856. case x86_intercept_sidt:
  11857. case x86_intercept_sldt:
  11858. case x86_intercept_str:
  11859. if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC))
  11860. return X86EMUL_CONTINUE;
  11861. /* FIXME: produce nested vmexit and return X86EMUL_INTERCEPTED. */
  11862. break;
  11863. /* TODO: check more intercepts... */
  11864. default:
  11865. break;
  11866. }
  11867. return X86EMUL_UNHANDLEABLE;
  11868. }
  11869. #ifdef CONFIG_X86_64
  11870. /* (a << shift) / divisor, return 1 if overflow otherwise 0 */
  11871. static inline int u64_shl_div_u64(u64 a, unsigned int shift,
  11872. u64 divisor, u64 *result)
  11873. {
  11874. u64 low = a << shift, high = a >> (64 - shift);
  11875. /* To avoid the overflow on divq */
  11876. if (high >= divisor)
  11877. return 1;
  11878. /* Low hold the result, high hold rem which is discarded */
  11879. asm("divq %2\n\t" : "=a" (low), "=d" (high) :
  11880. "rm" (divisor), "0" (low), "1" (high));
  11881. *result = low;
  11882. return 0;
  11883. }
  11884. static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
  11885. {
  11886. struct vcpu_vmx *vmx;
  11887. u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
  11888. if (kvm_mwait_in_guest(vcpu->kvm))
  11889. return -EOPNOTSUPP;
  11890. vmx = to_vmx(vcpu);
  11891. tscl = rdtsc();
  11892. guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
  11893. delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
  11894. lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
  11895. if (delta_tsc > lapic_timer_advance_cycles)
  11896. delta_tsc -= lapic_timer_advance_cycles;
  11897. else
  11898. delta_tsc = 0;
  11899. /* Convert to host delta tsc if tsc scaling is enabled */
  11900. if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
  11901. u64_shl_div_u64(delta_tsc,
  11902. kvm_tsc_scaling_ratio_frac_bits,
  11903. vcpu->arch.tsc_scaling_ratio,
  11904. &delta_tsc))
  11905. return -ERANGE;
  11906. /*
  11907. * If the delta tsc can't fit in the 32 bit after the multi shift,
  11908. * we can't use the preemption timer.
  11909. * It's possible that it fits on later vmentries, but checking
  11910. * on every vmentry is costly so we just use an hrtimer.
  11911. */
  11912. if (delta_tsc >> (cpu_preemption_timer_multi + 32))
  11913. return -ERANGE;
  11914. vmx->hv_deadline_tsc = tscl + delta_tsc;
  11915. return delta_tsc == 0;
  11916. }
  11917. static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
  11918. {
  11919. to_vmx(vcpu)->hv_deadline_tsc = -1;
  11920. }
  11921. #endif
  11922. static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
  11923. {
  11924. if (!kvm_pause_in_guest(vcpu->kvm))
  11925. shrink_ple_window(vcpu);
  11926. }
  11927. static void vmx_slot_enable_log_dirty(struct kvm *kvm,
  11928. struct kvm_memory_slot *slot)
  11929. {
  11930. kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
  11931. kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
  11932. }
  11933. static void vmx_slot_disable_log_dirty(struct kvm *kvm,
  11934. struct kvm_memory_slot *slot)
  11935. {
  11936. kvm_mmu_slot_set_dirty(kvm, slot);
  11937. }
  11938. static void vmx_flush_log_dirty(struct kvm *kvm)
  11939. {
  11940. kvm_flush_pml_buffers(kvm);
  11941. }
  11942. static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
  11943. {
  11944. struct vmcs12 *vmcs12;
  11945. struct vcpu_vmx *vmx = to_vmx(vcpu);
  11946. gpa_t gpa;
  11947. struct page *page = NULL;
  11948. u64 *pml_address;
  11949. if (is_guest_mode(vcpu)) {
  11950. WARN_ON_ONCE(vmx->nested.pml_full);
  11951. /*
  11952. * Check if PML is enabled for the nested guest.
  11953. * Whether eptp bit 6 is set is already checked
  11954. * as part of A/D emulation.
  11955. */
  11956. vmcs12 = get_vmcs12(vcpu);
  11957. if (!nested_cpu_has_pml(vmcs12))
  11958. return 0;
  11959. if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
  11960. vmx->nested.pml_full = true;
  11961. return 1;
  11962. }
  11963. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
  11964. page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
  11965. if (is_error_page(page))
  11966. return 0;
  11967. pml_address = kmap(page);
  11968. pml_address[vmcs12->guest_pml_index--] = gpa;
  11969. kunmap(page);
  11970. kvm_release_page_clean(page);
  11971. }
  11972. return 0;
  11973. }
  11974. static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
  11975. struct kvm_memory_slot *memslot,
  11976. gfn_t offset, unsigned long mask)
  11977. {
  11978. kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
  11979. }
  11980. static void __pi_post_block(struct kvm_vcpu *vcpu)
  11981. {
  11982. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  11983. struct pi_desc old, new;
  11984. unsigned int dest;
  11985. do {
  11986. old.control = new.control = pi_desc->control;
  11987. WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
  11988. "Wakeup handler not enabled while the VCPU is blocked\n");
  11989. dest = cpu_physical_id(vcpu->cpu);
  11990. if (x2apic_enabled())
  11991. new.ndst = dest;
  11992. else
  11993. new.ndst = (dest << 8) & 0xFF00;
  11994. /* set 'NV' to 'notification vector' */
  11995. new.nv = POSTED_INTR_VECTOR;
  11996. } while (cmpxchg64(&pi_desc->control, old.control,
  11997. new.control) != old.control);
  11998. if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
  11999. spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
  12000. list_del(&vcpu->blocked_vcpu_list);
  12001. spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
  12002. vcpu->pre_pcpu = -1;
  12003. }
  12004. }
  12005. /*
  12006. * This routine does the following things for vCPU which is going
  12007. * to be blocked if VT-d PI is enabled.
  12008. * - Store the vCPU to the wakeup list, so when interrupts happen
  12009. * we can find the right vCPU to wake up.
  12010. * - Change the Posted-interrupt descriptor as below:
  12011. * 'NDST' <-- vcpu->pre_pcpu
  12012. * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
  12013. * - If 'ON' is set during this process, which means at least one
  12014. * interrupt is posted for this vCPU, we cannot block it, in
  12015. * this case, return 1, otherwise, return 0.
  12016. *
  12017. */
  12018. static int pi_pre_block(struct kvm_vcpu *vcpu)
  12019. {
  12020. unsigned int dest;
  12021. struct pi_desc old, new;
  12022. struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
  12023. if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
  12024. !irq_remapping_cap(IRQ_POSTING_CAP) ||
  12025. !kvm_vcpu_apicv_active(vcpu))
  12026. return 0;
  12027. WARN_ON(irqs_disabled());
  12028. local_irq_disable();
  12029. if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
  12030. vcpu->pre_pcpu = vcpu->cpu;
  12031. spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
  12032. list_add_tail(&vcpu->blocked_vcpu_list,
  12033. &per_cpu(blocked_vcpu_on_cpu,
  12034. vcpu->pre_pcpu));
  12035. spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
  12036. }
  12037. do {
  12038. old.control = new.control = pi_desc->control;
  12039. WARN((pi_desc->sn == 1),
  12040. "Warning: SN field of posted-interrupts "
  12041. "is set before blocking\n");
  12042. /*
  12043. * Since vCPU can be preempted during this process,
  12044. * vcpu->cpu could be different with pre_pcpu, we
  12045. * need to set pre_pcpu as the destination of wakeup
  12046. * notification event, then we can find the right vCPU
  12047. * to wakeup in wakeup handler if interrupts happen
  12048. * when the vCPU is in blocked state.
  12049. */
  12050. dest = cpu_physical_id(vcpu->pre_pcpu);
  12051. if (x2apic_enabled())
  12052. new.ndst = dest;
  12053. else
  12054. new.ndst = (dest << 8) & 0xFF00;
  12055. /* set 'NV' to 'wakeup vector' */
  12056. new.nv = POSTED_INTR_WAKEUP_VECTOR;
  12057. } while (cmpxchg64(&pi_desc->control, old.control,
  12058. new.control) != old.control);
  12059. /* We should not block the vCPU if an interrupt is posted for it. */
  12060. if (pi_test_on(pi_desc) == 1)
  12061. __pi_post_block(vcpu);
  12062. local_irq_enable();
  12063. return (vcpu->pre_pcpu == -1);
  12064. }
  12065. static int vmx_pre_block(struct kvm_vcpu *vcpu)
  12066. {
  12067. if (pi_pre_block(vcpu))
  12068. return 1;
  12069. if (kvm_lapic_hv_timer_in_use(vcpu))
  12070. kvm_lapic_switch_to_sw_timer(vcpu);
  12071. return 0;
  12072. }
  12073. static void pi_post_block(struct kvm_vcpu *vcpu)
  12074. {
  12075. if (vcpu->pre_pcpu == -1)
  12076. return;
  12077. WARN_ON(irqs_disabled());
  12078. local_irq_disable();
  12079. __pi_post_block(vcpu);
  12080. local_irq_enable();
  12081. }
  12082. static void vmx_post_block(struct kvm_vcpu *vcpu)
  12083. {
  12084. if (kvm_x86_ops->set_hv_timer)
  12085. kvm_lapic_switch_to_hv_timer(vcpu);
  12086. pi_post_block(vcpu);
  12087. }
  12088. /*
  12089. * vmx_update_pi_irte - set IRTE for Posted-Interrupts
  12090. *
  12091. * @kvm: kvm
  12092. * @host_irq: host irq of the interrupt
  12093. * @guest_irq: gsi of the interrupt
  12094. * @set: set or unset PI
  12095. * returns 0 on success, < 0 on failure
  12096. */
  12097. static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
  12098. uint32_t guest_irq, bool set)
  12099. {
  12100. struct kvm_kernel_irq_routing_entry *e;
  12101. struct kvm_irq_routing_table *irq_rt;
  12102. struct kvm_lapic_irq irq;
  12103. struct kvm_vcpu *vcpu;
  12104. struct vcpu_data vcpu_info;
  12105. int idx, ret = 0;
  12106. if (!kvm_arch_has_assigned_device(kvm) ||
  12107. !irq_remapping_cap(IRQ_POSTING_CAP) ||
  12108. !kvm_vcpu_apicv_active(kvm->vcpus[0]))
  12109. return 0;
  12110. idx = srcu_read_lock(&kvm->irq_srcu);
  12111. irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
  12112. if (guest_irq >= irq_rt->nr_rt_entries ||
  12113. hlist_empty(&irq_rt->map[guest_irq])) {
  12114. pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
  12115. guest_irq, irq_rt->nr_rt_entries);
  12116. goto out;
  12117. }
  12118. hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
  12119. if (e->type != KVM_IRQ_ROUTING_MSI)
  12120. continue;
  12121. /*
  12122. * VT-d PI cannot support posting multicast/broadcast
  12123. * interrupts to a vCPU, we still use interrupt remapping
  12124. * for these kind of interrupts.
  12125. *
  12126. * For lowest-priority interrupts, we only support
  12127. * those with single CPU as the destination, e.g. user
  12128. * configures the interrupts via /proc/irq or uses
  12129. * irqbalance to make the interrupts single-CPU.
  12130. *
  12131. * We will support full lowest-priority interrupt later.
  12132. */
  12133. kvm_set_msi_irq(kvm, e, &irq);
  12134. if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
  12135. /*
  12136. * Make sure the IRTE is in remapped mode if
  12137. * we don't handle it in posted mode.
  12138. */
  12139. ret = irq_set_vcpu_affinity(host_irq, NULL);
  12140. if (ret < 0) {
  12141. printk(KERN_INFO
  12142. "failed to back to remapped mode, irq: %u\n",
  12143. host_irq);
  12144. goto out;
  12145. }
  12146. continue;
  12147. }
  12148. vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
  12149. vcpu_info.vector = irq.vector;
  12150. trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
  12151. vcpu_info.vector, vcpu_info.pi_desc_addr, set);
  12152. if (set)
  12153. ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
  12154. else
  12155. ret = irq_set_vcpu_affinity(host_irq, NULL);
  12156. if (ret < 0) {
  12157. printk(KERN_INFO "%s: failed to update PI IRTE\n",
  12158. __func__);
  12159. goto out;
  12160. }
  12161. }
  12162. ret = 0;
  12163. out:
  12164. srcu_read_unlock(&kvm->irq_srcu, idx);
  12165. return ret;
  12166. }
  12167. static void vmx_setup_mce(struct kvm_vcpu *vcpu)
  12168. {
  12169. if (vcpu->arch.mcg_cap & MCG_LMCE_P)
  12170. to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
  12171. FEATURE_CONTROL_LMCE;
  12172. else
  12173. to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
  12174. ~FEATURE_CONTROL_LMCE;
  12175. }
  12176. static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
  12177. {
  12178. /* we need a nested vmexit to enter SMM, postpone if run is pending */
  12179. if (to_vmx(vcpu)->nested.nested_run_pending)
  12180. return 0;
  12181. return 1;
  12182. }
  12183. static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
  12184. {
  12185. struct vcpu_vmx *vmx = to_vmx(vcpu);
  12186. vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
  12187. if (vmx->nested.smm.guest_mode)
  12188. nested_vmx_vmexit(vcpu, -1, 0, 0);
  12189. vmx->nested.smm.vmxon = vmx->nested.vmxon;
  12190. vmx->nested.vmxon = false;
  12191. vmx_clear_hlt(vcpu);
  12192. return 0;
  12193. }
  12194. static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
  12195. {
  12196. struct vcpu_vmx *vmx = to_vmx(vcpu);
  12197. int ret;
  12198. if (vmx->nested.smm.vmxon) {
  12199. vmx->nested.vmxon = true;
  12200. vmx->nested.smm.vmxon = false;
  12201. }
  12202. if (vmx->nested.smm.guest_mode) {
  12203. vcpu->arch.hflags &= ~HF_SMM_MASK;
  12204. ret = enter_vmx_non_root_mode(vcpu, NULL);
  12205. vcpu->arch.hflags |= HF_SMM_MASK;
  12206. if (ret)
  12207. return ret;
  12208. vmx->nested.smm.guest_mode = false;
  12209. }
  12210. return 0;
  12211. }
  12212. static int enable_smi_window(struct kvm_vcpu *vcpu)
  12213. {
  12214. return 0;
  12215. }
  12216. static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
  12217. struct kvm_nested_state __user *user_kvm_nested_state,
  12218. u32 user_data_size)
  12219. {
  12220. struct vcpu_vmx *vmx;
  12221. struct vmcs12 *vmcs12;
  12222. struct kvm_nested_state kvm_state = {
  12223. .flags = 0,
  12224. .format = 0,
  12225. .size = sizeof(kvm_state),
  12226. .vmx.vmxon_pa = -1ull,
  12227. .vmx.vmcs_pa = -1ull,
  12228. };
  12229. if (!vcpu)
  12230. return kvm_state.size + 2 * VMCS12_SIZE;
  12231. vmx = to_vmx(vcpu);
  12232. vmcs12 = get_vmcs12(vcpu);
  12233. if (nested_vmx_allowed(vcpu) &&
  12234. (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
  12235. kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
  12236. kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
  12237. if (vmx->nested.current_vmptr != -1ull) {
  12238. kvm_state.size += VMCS12_SIZE;
  12239. if (is_guest_mode(vcpu) &&
  12240. nested_cpu_has_shadow_vmcs(vmcs12) &&
  12241. vmcs12->vmcs_link_pointer != -1ull)
  12242. kvm_state.size += VMCS12_SIZE;
  12243. }
  12244. if (vmx->nested.smm.vmxon)
  12245. kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
  12246. if (vmx->nested.smm.guest_mode)
  12247. kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
  12248. if (is_guest_mode(vcpu)) {
  12249. kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
  12250. if (vmx->nested.nested_run_pending)
  12251. kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
  12252. }
  12253. }
  12254. if (user_data_size < kvm_state.size)
  12255. goto out;
  12256. if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
  12257. return -EFAULT;
  12258. if (vmx->nested.current_vmptr == -1ull)
  12259. goto out;
  12260. /*
  12261. * When running L2, the authoritative vmcs12 state is in the
  12262. * vmcs02. When running L1, the authoritative vmcs12 state is
  12263. * in the shadow vmcs linked to vmcs01, unless
  12264. * sync_shadow_vmcs is set, in which case, the authoritative
  12265. * vmcs12 state is in the vmcs12 already.
  12266. */
  12267. if (is_guest_mode(vcpu))
  12268. sync_vmcs12(vcpu, vmcs12);
  12269. else if (enable_shadow_vmcs && !vmx->nested.sync_shadow_vmcs)
  12270. copy_shadow_to_vmcs12(vmx);
  12271. /*
  12272. * Copy over the full allocated size of vmcs12 rather than just the size
  12273. * of the struct.
  12274. */
  12275. if (copy_to_user(user_kvm_nested_state->data, vmcs12, VMCS12_SIZE))
  12276. return -EFAULT;
  12277. if (nested_cpu_has_shadow_vmcs(vmcs12) &&
  12278. vmcs12->vmcs_link_pointer != -1ull) {
  12279. if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
  12280. get_shadow_vmcs12(vcpu), VMCS12_SIZE))
  12281. return -EFAULT;
  12282. }
  12283. out:
  12284. return kvm_state.size;
  12285. }
  12286. static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
  12287. struct kvm_nested_state __user *user_kvm_nested_state,
  12288. struct kvm_nested_state *kvm_state)
  12289. {
  12290. struct vcpu_vmx *vmx = to_vmx(vcpu);
  12291. struct vmcs12 *vmcs12;
  12292. u32 exit_qual;
  12293. int ret;
  12294. if (kvm_state->format != 0)
  12295. return -EINVAL;
  12296. if (!nested_vmx_allowed(vcpu))
  12297. return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
  12298. if (kvm_state->vmx.vmxon_pa == -1ull) {
  12299. if (kvm_state->vmx.smm.flags)
  12300. return -EINVAL;
  12301. if (kvm_state->vmx.vmcs_pa != -1ull)
  12302. return -EINVAL;
  12303. vmx_leave_nested(vcpu);
  12304. return 0;
  12305. }
  12306. if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
  12307. return -EINVAL;
  12308. if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
  12309. (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
  12310. return -EINVAL;
  12311. if (kvm_state->vmx.smm.flags &
  12312. ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
  12313. return -EINVAL;
  12314. /*
  12315. * SMM temporarily disables VMX, so we cannot be in guest mode,
  12316. * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags
  12317. * must be zero.
  12318. */
  12319. if (is_smm(vcpu) ? kvm_state->flags : kvm_state->vmx.smm.flags)
  12320. return -EINVAL;
  12321. if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
  12322. !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
  12323. return -EINVAL;
  12324. vmx_leave_nested(vcpu);
  12325. if (kvm_state->vmx.vmxon_pa == -1ull)
  12326. return 0;
  12327. vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
  12328. ret = enter_vmx_operation(vcpu);
  12329. if (ret)
  12330. return ret;
  12331. /* Empty 'VMXON' state is permitted */
  12332. if (kvm_state->size < sizeof(*kvm_state) + sizeof(*vmcs12))
  12333. return 0;
  12334. if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
  12335. !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
  12336. return -EINVAL;
  12337. set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
  12338. if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
  12339. vmx->nested.smm.vmxon = true;
  12340. vmx->nested.vmxon = false;
  12341. if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
  12342. vmx->nested.smm.guest_mode = true;
  12343. }
  12344. vmcs12 = get_vmcs12(vcpu);
  12345. if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
  12346. return -EFAULT;
  12347. if (vmcs12->hdr.revision_id != VMCS12_REVISION)
  12348. return -EINVAL;
  12349. if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
  12350. return 0;
  12351. vmx->nested.nested_run_pending =
  12352. !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
  12353. if (nested_cpu_has_shadow_vmcs(vmcs12) &&
  12354. vmcs12->vmcs_link_pointer != -1ull) {
  12355. struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
  12356. if (kvm_state->size < sizeof(*kvm_state) + 2 * sizeof(*vmcs12))
  12357. return -EINVAL;
  12358. if (copy_from_user(shadow_vmcs12,
  12359. user_kvm_nested_state->data + VMCS12_SIZE,
  12360. sizeof(*vmcs12)))
  12361. return -EFAULT;
  12362. if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
  12363. !shadow_vmcs12->hdr.shadow_vmcs)
  12364. return -EINVAL;
  12365. }
  12366. if (check_vmentry_prereqs(vcpu, vmcs12) ||
  12367. check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
  12368. return -EINVAL;
  12369. vmx->nested.dirty_vmcs12 = true;
  12370. ret = enter_vmx_non_root_mode(vcpu, NULL);
  12371. if (ret)
  12372. return -EINVAL;
  12373. return 0;
  12374. }
  12375. static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
  12376. .cpu_has_kvm_support = cpu_has_kvm_support,
  12377. .disabled_by_bios = vmx_disabled_by_bios,
  12378. .hardware_setup = hardware_setup,
  12379. .hardware_unsetup = hardware_unsetup,
  12380. .check_processor_compatibility = vmx_check_processor_compat,
  12381. .hardware_enable = hardware_enable,
  12382. .hardware_disable = hardware_disable,
  12383. .cpu_has_accelerated_tpr = report_flexpriority,
  12384. .has_emulated_msr = vmx_has_emulated_msr,
  12385. .vm_init = vmx_vm_init,
  12386. .vm_alloc = vmx_vm_alloc,
  12387. .vm_free = vmx_vm_free,
  12388. .vcpu_create = vmx_create_vcpu,
  12389. .vcpu_free = vmx_free_vcpu,
  12390. .vcpu_reset = vmx_vcpu_reset,
  12391. .prepare_guest_switch = vmx_prepare_switch_to_guest,
  12392. .vcpu_load = vmx_vcpu_load,
  12393. .vcpu_put = vmx_vcpu_put,
  12394. .update_bp_intercept = update_exception_bitmap,
  12395. .get_msr_feature = vmx_get_msr_feature,
  12396. .get_msr = vmx_get_msr,
  12397. .set_msr = vmx_set_msr,
  12398. .get_segment_base = vmx_get_segment_base,
  12399. .get_segment = vmx_get_segment,
  12400. .set_segment = vmx_set_segment,
  12401. .get_cpl = vmx_get_cpl,
  12402. .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
  12403. .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
  12404. .decache_cr3 = vmx_decache_cr3,
  12405. .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
  12406. .set_cr0 = vmx_set_cr0,
  12407. .set_cr3 = vmx_set_cr3,
  12408. .set_cr4 = vmx_set_cr4,
  12409. .set_efer = vmx_set_efer,
  12410. .get_idt = vmx_get_idt,
  12411. .set_idt = vmx_set_idt,
  12412. .get_gdt = vmx_get_gdt,
  12413. .set_gdt = vmx_set_gdt,
  12414. .get_dr6 = vmx_get_dr6,
  12415. .set_dr6 = vmx_set_dr6,
  12416. .set_dr7 = vmx_set_dr7,
  12417. .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
  12418. .cache_reg = vmx_cache_reg,
  12419. .get_rflags = vmx_get_rflags,
  12420. .set_rflags = vmx_set_rflags,
  12421. .tlb_flush = vmx_flush_tlb,
  12422. .tlb_flush_gva = vmx_flush_tlb_gva,
  12423. .run = vmx_vcpu_run,
  12424. .handle_exit = vmx_handle_exit,
  12425. .skip_emulated_instruction = skip_emulated_instruction,
  12426. .set_interrupt_shadow = vmx_set_interrupt_shadow,
  12427. .get_interrupt_shadow = vmx_get_interrupt_shadow,
  12428. .patch_hypercall = vmx_patch_hypercall,
  12429. .set_irq = vmx_inject_irq,
  12430. .set_nmi = vmx_inject_nmi,
  12431. .queue_exception = vmx_queue_exception,
  12432. .cancel_injection = vmx_cancel_injection,
  12433. .interrupt_allowed = vmx_interrupt_allowed,
  12434. .nmi_allowed = vmx_nmi_allowed,
  12435. .get_nmi_mask = vmx_get_nmi_mask,
  12436. .set_nmi_mask = vmx_set_nmi_mask,
  12437. .enable_nmi_window = enable_nmi_window,
  12438. .enable_irq_window = enable_irq_window,
  12439. .update_cr8_intercept = update_cr8_intercept,
  12440. .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
  12441. .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
  12442. .get_enable_apicv = vmx_get_enable_apicv,
  12443. .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
  12444. .load_eoi_exitmap = vmx_load_eoi_exitmap,
  12445. .apicv_post_state_restore = vmx_apicv_post_state_restore,
  12446. .hwapic_irr_update = vmx_hwapic_irr_update,
  12447. .hwapic_isr_update = vmx_hwapic_isr_update,
  12448. .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt,
  12449. .sync_pir_to_irr = vmx_sync_pir_to_irr,
  12450. .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
  12451. .dy_apicv_has_pending_interrupt = vmx_dy_apicv_has_pending_interrupt,
  12452. .set_tss_addr = vmx_set_tss_addr,
  12453. .set_identity_map_addr = vmx_set_identity_map_addr,
  12454. .get_tdp_level = get_ept_level,
  12455. .get_mt_mask = vmx_get_mt_mask,
  12456. .get_exit_info = vmx_get_exit_info,
  12457. .get_lpage_level = vmx_get_lpage_level,
  12458. .cpuid_update = vmx_cpuid_update,
  12459. .rdtscp_supported = vmx_rdtscp_supported,
  12460. .invpcid_supported = vmx_invpcid_supported,
  12461. .set_supported_cpuid = vmx_set_supported_cpuid,
  12462. .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
  12463. .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
  12464. .write_l1_tsc_offset = vmx_write_l1_tsc_offset,
  12465. .set_tdp_cr3 = vmx_set_cr3,
  12466. .check_intercept = vmx_check_intercept,
  12467. .handle_external_intr = vmx_handle_external_intr,
  12468. .mpx_supported = vmx_mpx_supported,
  12469. .xsaves_supported = vmx_xsaves_supported,
  12470. .umip_emulated = vmx_umip_emulated,
  12471. .check_nested_events = vmx_check_nested_events,
  12472. .request_immediate_exit = vmx_request_immediate_exit,
  12473. .sched_in = vmx_sched_in,
  12474. .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
  12475. .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
  12476. .flush_log_dirty = vmx_flush_log_dirty,
  12477. .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
  12478. .write_log_dirty = vmx_write_pml_buffer,
  12479. .pre_block = vmx_pre_block,
  12480. .post_block = vmx_post_block,
  12481. .pmu_ops = &intel_pmu_ops,
  12482. .update_pi_irte = vmx_update_pi_irte,
  12483. #ifdef CONFIG_X86_64
  12484. .set_hv_timer = vmx_set_hv_timer,
  12485. .cancel_hv_timer = vmx_cancel_hv_timer,
  12486. #endif
  12487. .setup_mce = vmx_setup_mce,
  12488. .get_nested_state = vmx_get_nested_state,
  12489. .set_nested_state = vmx_set_nested_state,
  12490. .get_vmcs12_pages = nested_get_vmcs12_pages,
  12491. .smi_allowed = vmx_smi_allowed,
  12492. .pre_enter_smm = vmx_pre_enter_smm,
  12493. .pre_leave_smm = vmx_pre_leave_smm,
  12494. .enable_smi_window = enable_smi_window,
  12495. };
  12496. static void vmx_cleanup_l1d_flush(void)
  12497. {
  12498. if (vmx_l1d_flush_pages) {
  12499. free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
  12500. vmx_l1d_flush_pages = NULL;
  12501. }
  12502. /* Restore state so sysfs ignores VMX */
  12503. l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
  12504. }
  12505. static void vmx_exit(void)
  12506. {
  12507. #ifdef CONFIG_KEXEC_CORE
  12508. RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
  12509. synchronize_rcu();
  12510. #endif
  12511. kvm_exit();
  12512. #if IS_ENABLED(CONFIG_HYPERV)
  12513. if (static_branch_unlikely(&enable_evmcs)) {
  12514. int cpu;
  12515. struct hv_vp_assist_page *vp_ap;
  12516. /*
  12517. * Reset everything to support using non-enlightened VMCS
  12518. * access later (e.g. when we reload the module with
  12519. * enlightened_vmcs=0)
  12520. */
  12521. for_each_online_cpu(cpu) {
  12522. vp_ap = hv_get_vp_assist_page(cpu);
  12523. if (!vp_ap)
  12524. continue;
  12525. vp_ap->current_nested_vmcs = 0;
  12526. vp_ap->enlighten_vmentry = 0;
  12527. }
  12528. static_branch_disable(&enable_evmcs);
  12529. }
  12530. #endif
  12531. vmx_cleanup_l1d_flush();
  12532. }
  12533. module_exit(vmx_exit);
  12534. static int __init vmx_init(void)
  12535. {
  12536. int r;
  12537. #if IS_ENABLED(CONFIG_HYPERV)
  12538. /*
  12539. * Enlightened VMCS usage should be recommended and the host needs
  12540. * to support eVMCS v1 or above. We can also disable eVMCS support
  12541. * with module parameter.
  12542. */
  12543. if (enlightened_vmcs &&
  12544. ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
  12545. (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
  12546. KVM_EVMCS_VERSION) {
  12547. int cpu;
  12548. /* Check that we have assist pages on all online CPUs */
  12549. for_each_online_cpu(cpu) {
  12550. if (!hv_get_vp_assist_page(cpu)) {
  12551. enlightened_vmcs = false;
  12552. break;
  12553. }
  12554. }
  12555. if (enlightened_vmcs) {
  12556. pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
  12557. static_branch_enable(&enable_evmcs);
  12558. }
  12559. } else {
  12560. enlightened_vmcs = false;
  12561. }
  12562. #endif
  12563. r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
  12564. __alignof__(struct vcpu_vmx), THIS_MODULE);
  12565. if (r)
  12566. return r;
  12567. /*
  12568. * Must be called after kvm_init() so enable_ept is properly set
  12569. * up. Hand the parameter mitigation value in which was stored in
  12570. * the pre module init parser. If no parameter was given, it will
  12571. * contain 'auto' which will be turned into the default 'cond'
  12572. * mitigation mode.
  12573. */
  12574. if (boot_cpu_has(X86_BUG_L1TF)) {
  12575. r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
  12576. if (r) {
  12577. vmx_exit();
  12578. return r;
  12579. }
  12580. }
  12581. #ifdef CONFIG_KEXEC_CORE
  12582. rcu_assign_pointer(crash_vmclear_loaded_vmcss,
  12583. crash_vmclear_local_loaded_vmcss);
  12584. #endif
  12585. vmx_check_vmcs12_offsets();
  12586. return 0;
  12587. }
  12588. module_init(vmx_init);